authorgravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2022-10-05 03:13:21-07:00
committergravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2022-10-05 03:26:13-07:00
loge9889cd25ff219f2f5ac7a336c4f99a577b19d44
tree35d9896219fc08992e9ab530cde32e4acb316181
parent8cec8f6dddeec46f609afd42c6024c55e5c126d9

fs: Add IterableDir.Iterator.reset


2 files changed, 66 insertions(+), 0 deletions(-)

lib/std/fs.zig+30
...@@ -490,6 +490,12 @@ pub const IterableDir = struct {...@@ -490,6 +490,12 @@ pub const IterableDir = struct {
490 };490 };
491 }491 }
492 }492 }
493
494 pub fn reset(self: *Self) void {
495 self.index = 0;
496 self.end_index = 0;
497 self.first_iter = true;
498 }
493 },499 },
494 .haiku => struct {500 .haiku => struct {
495 dir: Dir,501 dir: Dir,
...@@ -577,6 +583,12 @@ pub const IterableDir = struct {...@@ -577,6 +583,12 @@ pub const IterableDir = struct {
577 };583 };
578 }584 }
579 }585 }
586
587 pub fn reset(self: *Self) void {
588 self.index = 0;
589 self.end_index = 0;
590 self.first_iter = true;
591 }
580 },592 },
581 .linux => struct {593 .linux => struct {
582 dir: Dir,594 dir: Dir,
...@@ -655,6 +667,12 @@ pub const IterableDir = struct {...@@ -655,6 +667,12 @@ pub const IterableDir = struct {
655 };667 };
656 }668 }
657 }669 }
670
671 pub fn reset(self: *Self) void {
672 self.index = 0;
673 self.end_index = 0;
674 self.first_iter = true;
675 }
658 },676 },
659 .windows => struct {677 .windows => struct {
660 dir: Dir,678 dir: Dir,
...@@ -727,6 +745,12 @@ pub const IterableDir = struct {...@@ -727,6 +745,12 @@ pub const IterableDir = struct {
727 };745 };
728 }746 }
729 }747 }
748
749 pub fn reset(self: *Self) void {
750 self.index = 0;
751 self.end_index = 0;
752 self.first_iter = true;
753 }
730 },754 },
731 .wasi => struct {755 .wasi => struct {
732 dir: Dir,756 dir: Dir,
...@@ -806,6 +830,12 @@ pub const IterableDir = struct {...@@ -806,6 +830,12 @@ pub const IterableDir = struct {
806 };830 };
807 }831 }
808 }832 }
833
834 pub fn reset(self: *Self) void {
835 self.index = 0;
836 self.end_index = 0;
837 self.cookie = os.wasi.DIRCOOKIE_START;
838 }
809 },839 },
810 else => @compileError("unimplemented"),840 else => @compileError("unimplemented"),
811 };841 };
lib/std/fs/test.zig+36
...@@ -219,6 +219,42 @@ test "Dir.Iterator twice" {...@@ -219,6 +219,42 @@ test "Dir.Iterator twice" {
219 }219 }
220}220}
221221
222test "Dir.Iterator reset" {
223 var tmp_dir = tmpIterableDir(.{});
224 defer tmp_dir.cleanup();
225
226 // First, create a couple of entries to iterate over.
227 const file = try tmp_dir.iterable_dir.dir.createFile("some_file", .{});
228 file.close();
229
230 try tmp_dir.iterable_dir.dir.makeDir("some_dir");
231
232 var arena = ArenaAllocator.init(testing.allocator);
233 defer arena.deinit();
234 const allocator = arena.allocator();
235
236 // Create iterator.
237 var iter = tmp_dir.iterable_dir.iterate();
238
239 var i: u8 = 0;
240 while (i < 2) : (i += 1) {
241 var entries = std.ArrayList(IterableDir.Entry).init(allocator);
242
243 while (try iter.next()) |entry| {
244 // We cannot just store `entry` as on Windows, we're re-using the name buffer
245 // which means we'll actually share the `name` pointer between entries!
246 const name = try allocator.dupe(u8, entry.name);
247 try entries.append(.{ .name = name, .kind = entry.kind });
248 }
249
250 try testing.expect(entries.items.len == 2); // note that the Iterator skips '.' and '..'
251 try testing.expect(contains(&entries, .{ .name = "some_file", .kind = .File }));
252 try testing.expect(contains(&entries, .{ .name = "some_dir", .kind = .Directory }));
253
254 iter.reset();
255 }
256}
257
222test "Dir.Iterator but dir is deleted during iteration" {258test "Dir.Iterator but dir is deleted during iteration" {
223 var tmp = std.testing.tmpDir(.{});259 var tmp = std.testing.tmpDir(.{});
224 defer tmp.cleanup();260 defer tmp.cleanup();