@@ -117,23 +117,33 @@ pub const KnownFolderConfig = struct {
117117};
118118
119119/// Returns a directory handle, or, if the folder does not exist, `null`.
120- pub fn open (allocator : std.mem.Allocator , folder : KnownFolder , args : std.fs.Dir.OpenOptions ) (std .fs .Dir .OpenError || Error )! ? std.fs.Dir {
121- const path = try getPath (allocator , folder ) orelse return null ;
120+ pub fn open (
121+ io : std.Io ,
122+ allocator : std.mem.Allocator ,
123+ folder : KnownFolder ,
124+ args : std.Io.Dir.OpenOptions ,
125+ ) (std .Io .Dir .OpenError || Error )! ? std.Io.Dir {
126+ const path = try getPath (io , allocator , folder ) orelse return null ;
122127 defer allocator .free (path );
123- return try std .fs . cwd ().openDir (path , args );
128+ return try std .Io . Dir . cwd ().openDir (io , path , args );
124129}
125130
126131/// Returns the path to the folder or, if the folder does not exist, `null`.
127- pub fn getPath (allocator : std.mem.Allocator , folder : KnownFolder ) Error ! ? []const u8 {
132+ pub fn getPath (
133+ io : std.Io ,
134+ allocator : std.mem.Allocator ,
135+ folder : KnownFolder ,
136+ ) Error ! ? []const u8 {
128137 var system : DefaultSystem = .{};
129138 defer system .deinit ();
130- return getPathInner (DefaultSystem , & system , allocator , folder );
139+ return getPathInner (DefaultSystem , & system , io , allocator , folder );
131140}
132141
133142fn getPathInner (
134143 /// `DefaultDefaultSystem` or `TestingDefaultSystem`
135144 comptime System : type ,
136145 system : * System ,
146+ io : std.Io ,
137147 allocator : std.mem.Allocator ,
138148 folder : KnownFolder ,
139149) Error ! ? []const u8 {
@@ -197,7 +207,7 @@ fn getPathInner(
197207 }
198208 },
199209 .macos = > {
200- if (system .config .xdg_on_mac ) return try getPathXdg (System , system , allocator , folder );
210+ if (system .config .xdg_on_mac ) return try getPathXdg (System , system , io , allocator , folder );
201211
202212 if (folder == .global_configuration ) {
203213 // special case because the returned path is absolute
@@ -215,14 +225,15 @@ fn getPathInner(
215225 },
216226
217227 // Assume unix derivatives with XDG
218- else = > return try getPathXdg (System , system , allocator , folder ),
228+ else = > return try getPathXdg (System , system , io , allocator , folder ),
219229 }
220230}
221231
222232fn getPathXdg (
223233 /// `DefaultDefaultSystem` or `TestingDefaultSystem`
224234 comptime System : type ,
225235 system : * System ,
236+ io : std.Io ,
226237 allocator : std.mem.Allocator ,
227238 folder : KnownFolder ,
228239) Error ! ? []const u8 {
@@ -242,7 +253,7 @@ fn getPathXdg(
242253 if (! folder_spec .env .user_dir )
243254 break :fallback ;
244255
245- const env = xdgUserDirLookup (System , system , allocator , folder_spec .env .name ) catch | err | switch (err ) {
256+ const env = xdgUserDirLookup (System , system , io , allocator , folder_spec .env .name ) catch | err | switch (err ) {
246257 error .OutOfMemory = > return error .OutOfMemory ,
247258 else = > break :fallback ,
248259 } orelse break :fallback ;
@@ -301,10 +312,10 @@ const DefaultSystem = struct {
301312 return std .posix .getenv (key );
302313 }
303314
304- pub fn openFile (_ : * DefaultSystem , dir_path : []const u8 , sub_path : []const u8 ) std.fs .File.OpenError ! std.fs .File {
305- var dir = try std .fs . cwd ().openDir (dir_path , .{});
306- defer dir .close ();
307- return try dir .openFile (sub_path , .{});
315+ pub fn openFile (_ : * DefaultSystem , io : std.Io , dir_path : []const u8 , sub_path : []const u8 ) std.Io .File.OpenError ! std.Io .File {
316+ var dir = try std .Io . Dir . cwd ().openDir (io , dir_path , .{});
317+ defer dir .close (io );
318+ return try dir .openFile (io , sub_path , .{});
308319 }
309320};
310321
@@ -348,7 +359,7 @@ const TestingSystem = struct {
348359 }
349360
350361 /// Asserts that the file is specified in `TestingSystem.files`.
351- pub fn openFile (system : * TestingSystem , dir_path : []const u8 , sub_path : []const u8 ) std.fs .File.OpenError ! std.fs .File {
362+ pub fn openFile (system : * TestingSystem , io : std.Io , dir_path : []const u8 , sub_path : []const u8 ) std.Io .File.OpenError ! std.Io .File {
352363 const file_path = std .fs .path .join (std .testing .allocator , &.{ dir_path , sub_path }) catch @panic ("OOM" );
353364 defer std .testing .allocator .free (file_path );
354365
@@ -380,14 +391,15 @@ const TestingSystem = struct {
380391 std .debug .panic ("failed to create file './{s}/{s}' which represents '{s}': {}" , .{ & tmp_dir .sub_path , prefix , kv .path , err });
381392 };
382393
383- return try tmp_dir .dir .openFile (prefix , .{});
394+ const dir : std.Io.Dir = .{ .handle = tmp_dir .dir .fd };
395+ return try dir .openFile (io , prefix , .{});
384396 }
385397};
386398
387399const UserDirLookupError =
388400 std .mem .Allocator .Error ||
389- std .fs .File .OpenError ||
390- std .fs . File .ReadError ;
401+ std .Io .File .OpenError ||
402+ std .posix .ReadError ;
391403
392404const xdg_user_dir_lookup_line_buffer_size : usize = 511 ;
393405
@@ -401,6 +413,7 @@ fn xdgUserDirLookup(
401413 /// `DefaultDefaultSystem` or `TestingDefaultSystem`
402414 comptime System : type ,
403415 system : * System ,
416+ io : std.Io ,
404417 allocator : std.mem.Allocator ,
405418 /// A string that specifies the type of directory.
406419 ///
@@ -436,14 +449,14 @@ fn xdgUserDirLookup(
436449 else
437450 null ;
438451
439- const file : std.fs .File = if (maybe_config_home ) | config_home |
440- try system .openFile (config_home , "user-dirs.dirs" )
452+ const file : std.Io .File = if (maybe_config_home ) | config_home |
453+ try system .openFile (io , config_home , "user-dirs.dirs" )
441454 else
442- try system .openFile (home_dir , ".config/user-dirs.dirs" );
443- defer file .close ();
455+ try system .openFile (io , home_dir , ".config/user-dirs.dirs" );
456+ defer file .close (io );
444457
445458 var buffer : [xdg_user_dir_lookup_line_buffer_size + 1 ]u8 = undefined ;
446- var line_it : LineIterator = .init (file );
459+ var line_it : LineIterator = .init (io , file );
447460
448461 var user_dir : ? []u8 = null ;
449462 while (try line_it .next (& buffer )) | line_capture | {
@@ -529,13 +542,13 @@ fn xdgUserDirLookup(
529542}
530543
531544const LineIterator = struct {
532- file_reader : std.fs .File.Reader ,
545+ file_reader : std.Io .File.Reader ,
533546 keep_reading : bool ,
534547 discard_until_newline : bool ,
535548
536- fn init (file : std.fs .File ) LineIterator {
549+ fn init (io : std.Io , file : std.Io .File ) LineIterator {
537550 return .{
538- .file_reader = file .reader (undefined ),
551+ .file_reader = file .reader (io , undefined ),
539552 .keep_reading = true ,
540553 .discard_until_newline = false ,
541554 };
@@ -693,7 +706,7 @@ fn testGetPath(get_path_params: GetPathTestParams) !void {
693706 var system = params .system ;
694707 defer system .deinit ();
695708
696- const actual = try getPathInner (TestingSystem , & system , allocator , params .folder );
709+ const actual = try getPathInner (TestingSystem , & system , std . testing . io , allocator , params .folder );
697710 defer if (actual ) | path | allocator .free (path );
698711
699712 if (params .expected == null and actual == null ) return ;
@@ -1209,7 +1222,7 @@ test "getPath - .global_configuration with xdg_on_mac=false" {
12091222
12101223test "query each known folders" {
12111224 for (std .meta .tags (KnownFolder )) | folder | {
1212- const path_or_null = try getPath (std .testing .allocator , folder );
1225+ const path_or_null = try getPath (std .testing .io , std . testing . allocator , folder );
12131226 defer if (path_or_null ) | path | std .testing .allocator .free (path );
12141227 std .debug .print ("{s} => {?s}\n " , .{ @tagName (folder ), path_or_null });
12151228 }
@@ -1219,12 +1232,12 @@ test "open each known folders" {
12191232 if (builtin .os .tag == .wasi ) return error .SkipZigTest ;
12201233
12211234 for (std .meta .tags (KnownFolder )) | folder | {
1222- var dir_or_null = open (std .testing .allocator , folder , .{}) catch | e | switch (e ) {
1235+ var dir_or_null = open (std .testing .io , std . testing . allocator , folder , .{}) catch | e | switch (e ) {
12231236 error .FileNotFound = > return ,
12241237 else = > return e ,
12251238 };
12261239 if (dir_or_null ) | * dir | {
1227- dir .close ();
1240+ dir .close (std . testing . io );
12281241 }
12291242 }
12301243}
0 commit comments