| author | |
| committer | |
| log | 2f6dbaa0ea7307dd6806082b07266a43db5da950 |
| tree | c1e7c13d8d304a65e1b37d9d013ed135a2d77eb4 |
| parent | 6a5094872f10acc629543cc7f10533b438d0283a |
Closing the initial directory was unexpected to me, and does not mesh very well with how the rest of the Dir API works.
Fixes #95563 files changed, 15 insertions(+), 12 deletions(-)
lib/std/build.zig+2-1| ... | ... | @@ -3026,7 +3026,8 @@ pub const InstallDirStep = struct { |
| 3026 | 3026 | const self = @fieldParentPtr(InstallDirStep, "step", step); |
| 3027 | 3027 | const dest_prefix = self.builder.getInstallPath(self.options.install_dir, self.options.install_subdir); |
| 3028 | 3028 | const full_src_dir = self.builder.pathFromRoot(self.options.source_dir); |
| 3029 | const src_dir = try std.fs.cwd().openDir(full_src_dir, .{ .iterate = true }); | |
| 3029 | var src_dir = try std.fs.cwd().openDir(full_src_dir, .{ .iterate = true }); | |
| 3030 | defer src_dir.close(); | |
| 3030 | 3031 | var it = try src_dir.walk(self.builder.allocator); |
| 3031 | 3032 | next_entry: while (try it.next()) |entry| { |
| 3032 | 3033 | for (self.options.exclude_extensions) |ext| { |
lib/std/fs.zig+11-2| ... | ... | @@ -795,22 +795,31 @@ pub const Dir = struct { |
| 795 | 795 | .kind = base.kind, |
| 796 | 796 | }; |
| 797 | 797 | } else { |
| 798 | self.stack.pop().iter.dir.close(); | |
| 798 | var item = self.stack.pop(); | |
| 799 | if (self.stack.items.len != 0) { | |
| 800 | item.iter.dir.close(); | |
| 801 | } | |
| 799 | 802 | } |
| 800 | 803 | } |
| 801 | 804 | return null; |
| 802 | 805 | } |
| 803 | 806 | |
| 804 | 807 | pub fn deinit(self: *Walker) void { |
| 805 | while (self.stack.popOrNull()) |*item| item.iter.dir.close(); | |
| 808 | while (self.stack.popOrNull()) |*item| { | |
| 809 | if (self.stack.items.len != 0) { | |
| 810 | item.iter.dir.close(); | |
| 811 | } | |
| 812 | } | |
| 806 | 813 | self.stack.deinit(); |
| 807 | 814 | self.name_buffer.deinit(); |
| 808 | 815 | } |
| 809 | 816 | }; |
| 810 | 817 | |
| 811 | 818 | /// Recursively iterates over a directory. |
| 819 | /// `self` must have been opened with `OpenDirOptions{.iterate = true}`. | |
| 812 | 820 | /// Must call `Walker.deinit` when done. |
| 813 | 821 | /// The order of returned file system entries is undefined. |
| 822 | /// `self` will not be closed after walking it. | |
| 814 | 823 | pub fn walk(self: Dir, allocator: *Allocator) !Walker { |
| 815 | 824 | var name_buffer = std.ArrayList(u8).init(allocator); |
| 816 | 825 | errdefer name_buffer.deinit(); |
lib/std/fs/test.zig+2-9| ... | ... | @@ -909,11 +909,7 @@ test "open file with exclusive nonblocking lock twice (absolute paths)" { |
| 909 | 909 | test "walker" { |
| 910 | 910 | if (builtin.os.tag == .wasi) return error.SkipZigTest; |
| 911 | 911 | |
| 912 | var arena = ArenaAllocator.init(testing.allocator); | |
| 913 | defer arena.deinit(); | |
| 914 | var allocator = &arena.allocator; | |
| 915 | ||
| 916 | var tmp = tmpDir(.{}); | |
| 912 | var tmp = tmpDir(.{ .iterate = true }); | |
| 917 | 913 | defer tmp.cleanup(); |
| 918 | 914 | |
| 919 | 915 | // iteration order of walker is undefined, so need lookup maps to check against |
| ... | ... | @@ -942,10 +938,7 @@ test "walker" { |
| 942 | 938 | try tmp.dir.makePath(kv.key); |
| 943 | 939 | } |
| 944 | 940 | |
| 945 | const tmp_path = try fs.path.join(allocator, &[_][]const u8{ "zig-cache", "tmp", tmp.sub_path[0..] }); | |
| 946 | const tmp_dir = try fs.cwd().openDir(tmp_path, .{ .iterate = true }); | |
| 947 | ||
| 948 | var walker = try tmp_dir.walk(testing.allocator); | |
| 941 | var walker = try tmp.dir.walk(testing.allocator); | |
| 949 | 942 | defer walker.deinit(); |
| 950 | 943 | |
| 951 | 944 | var num_walked: usize = 0; |