I have code like this which opens a directory and walks over its contents, recursively:
var dir: std.fs.Dir = try std.fs.cwd().openDir(path, .{ .iterate = true });
defer dir.close();
var iterable_dir = dir.iterate();
while (try iterable_dir.next()) |e| {
switch (e.kind) {
.file => {}, // handle a file
else => {}, // handle a directory
}
}
This worked fine with 0.12.0, and returned all its contents, recursively, for all depths; but after I upgraded to 0.13.0, it is only returning entries in the current directory (it does not seem to recurse anymore). I did not find an obvious reference in the docs – can anybody point me to anything obvious? Did these APIs change in 0.13.0?
Could you be more specific about what you mean? Did the name field of the returned Entry contain a path separator? What platform was this happening on?
AFAIK Dir.Iterator has not changed for multiple releases and it’s not really possible for it to be recursive, since all implementations are just calling the platform’s getdents equivalent with the Dir’s fd.
You are both right @marler8997 and @squeek502 – I was the one who broke it. My original old code was:
var dir: std.fs.Dir = try std.fs.cwd().openDir(path, .{});
defer dir.close();
var iterable_dir = try dir.openIterableDir(".", .{});
defer iterable_dir.close();
var dir_walker = try iterable_dir.walk(self.allocator);
defer dir_walker.deinit();
while (try dir_walker.next()) |e| {
// ...
}
That old code recursed into all subdirectories. But when I upgraded the code to the newer zig version, I mistakenly dropped the walker and changed the code to this:
// ...
var iterable_dir = dir.iterate();
while (try iterable_dir.next()) |e| {
// ...
}
After @tensorush’s suggestion, I restored the walker; you have now also restored my sanity.