authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-18 16:09:06-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-18 16:42:47-04:00
log46ffc798b6d9bb7be8b016ef7529092d647151ee
tree5141e62de6134816b62c7ff1399dcdc651f63b11
parent27affde592653ac7f92489cec404b4bf3e0d1b29
signaturelock-open Commit is signed but in an unrecognized format.

fix swapped logic for Windows

Remove `std.fs.deleteTree`. Callers instead should use `std.fs.cwd().deleteTree`. Add `std.fs.deleteTreeAbsolute` for when the caller has an absolute path.

8 files changed, 34 insertions(+), 37 deletions(-)

doc/docgen.zig+1-1
......@@ -48,7 +48,7 @@ pub fn main() !void {
4848 var toc = try genToc(allocator, &tokenizer);
4949
5050 try fs.cwd().makePath(tmp_dir_name);
51 defer fs.deleteTree(tmp_dir_name) catch {};
51 defer fs.cwd().deleteTree(tmp_dir_name) catch {};
5252
5353 try genHtml(allocator, &tokenizer, &toc, buffered_out_stream.outStream(), zig_exe);
5454 try buffered_out_stream.flush();
lib/std/build.zig+2-2
......@@ -377,7 +377,7 @@ pub const Builder = struct {
377377 if (self.verbose) {
378378 warn("rm {}\n", .{full_path});
379379 }
380 fs.deleteTree(full_path) catch {};
380 fs.cwd().deleteTree(full_path) catch {};
381381 }
382382
383383 // TODO remove empty directories
......@@ -2365,7 +2365,7 @@ pub const RemoveDirStep = struct {
23652365 const self = @fieldParentPtr(RemoveDirStep, "step", step);
23662366
23672367 const full_path = self.builder.pathFromRoot(self.dir_path);
2368 fs.deleteTree(full_path) catch |err| {
2368 fs.cwd().deleteTree(full_path) catch |err| {
23692369 warn("Unable to remove {}: {}\n", .{ full_path, @errorName(err) });
23702370 return err;
23712371 };
lib/std/fs.zig+23-25
......@@ -261,28 +261,6 @@ pub fn deleteDirW(dir_path: [*:0]const u16) !void {
261261 return os.rmdirW(dir_path);
262262}
263263
264/// Removes a symlink, file, or directory.
265/// If `full_path` is relative, this is equivalent to `Dir.deleteTree` with the
266/// current working directory as the open directory handle.
267/// If `full_path` is absolute, this is equivalent to `Dir.deleteTree` with the
268/// base directory.
269pub fn deleteTree(full_path: []const u8) !void {
270 if (path.isAbsolute(full_path)) {
271 const dirname = path.dirname(full_path) orelse return error{
272 /// Attempt to remove the root file system path.
273 /// This error is unreachable if `full_path` is relative.
274 CannotDeleteRootDirectory,
275 }.CannotDeleteRootDirectory;
276
277 var dir = try cwd().openDir(dirname, .{});
278 defer dir.close();
279
280 return dir.deleteTree(path.basename(full_path));
281 } else {
282 return cwd().deleteTree(full_path);
283 }
284}
285
286264pub const Dir = struct {
287265 fd: os.fd_t,
288266
......@@ -518,7 +496,8 @@ pub const Dir = struct {
518496 self.end_index = io.Information;
519497 switch (rc) {
520498 .SUCCESS => {},
521 .ACCESS_DENIED => return error.AccessDenied,
499 .ACCESS_DENIED => return error.AccessDenied, // Double-check that the Dir was opened with iteration ability
500
522501 else => return w.unexpectedStatus(rc),
523502 }
524503 }
......@@ -827,7 +806,7 @@ pub const Dir = struct {
827806 // TODO remove some of these flags if args.access_sub_paths is false
828807 const base_flags = w.STANDARD_RIGHTS_READ | w.FILE_READ_ATTRIBUTES | w.FILE_READ_EA |
829808 w.SYNCHRONIZE | w.FILE_TRAVERSE;
830 const flags: u32 = if (args.iterate) base_flags else base_flags | w.FILE_LIST_DIRECTORY;
809 const flags: u32 = if (args.iterate) base_flags | w.FILE_LIST_DIRECTORY else base_flags;
831810 return self.openDirAccessMaskW(sub_path_w, flags);
832811 }
833812
......@@ -1304,7 +1283,7 @@ pub const Dir = struct {
13041283 }
13051284};
13061285
1307/// Returns an handle to the current working directory that is open for traversal.
1286/// Returns an handle to the current working directory. It is not opened with iteration capability.
13081287/// Closing the returned `Dir` is checked illegal behavior. Iterating over the result is illegal behavior.
13091288/// On POSIX targets, this function is comptime-callable.
13101289pub fn cwd() Dir {
......@@ -1382,6 +1361,25 @@ pub fn deleteFileAbsoluteW(absolute_path_w: [*:0]const u16) DeleteFileError!void
13821361 return cwd().deleteFileW(absolute_path_w);
13831362}
13841363
1364/// Removes a symlink, file, or directory.
1365/// This is equivalent to `Dir.deleteTree` with the base directory.
1366/// Asserts that the path is absolute. See `Dir.deleteTree` for a function that
1367/// operates on both absolute and relative paths.
1368/// Asserts that the path parameter has no null bytes.
1369pub fn deleteTreeAbsolute(absolute_path: []const u8) !void {
1370 assert(path.isAbsolute(absolute_path));
1371 const dirname = path.dirname(absolute_path) orelse return error{
1372 /// Attempt to remove the root file system path.
1373 /// This error is unreachable if `absolute_path` is relative.
1374 CannotDeleteRootDirectory,
1375 }.CannotDeleteRootDirectory;
1376
1377 var dir = try cwd().openDir(dirname, .{});
1378 defer dir.close();
1379
1380 return dir.deleteTree(path.basename(absolute_path));
1381}
1382
13851383pub const Walker = struct {
13861384 stack: std.ArrayList(StackItem),
13871385 name_buffer: std.Buffer,
lib/std/fs/watch.zig+1-1
......@@ -619,7 +619,7 @@ test "write a file, watch it, write it again" {
619619 if (true) return error.SkipZigTest;
620620
621621 try fs.cwd().makePath(test_tmp_dir);
622 defer os.deleteTree(test_tmp_dir) catch {};
622 defer fs.cwd().deleteTree(test_tmp_dir) catch {};
623623
624624 const allocator = std.heap.page_allocator;
625625 return testFsWatch(&allocator);
lib/std/os/test.zig+3-3
......@@ -20,7 +20,7 @@ test "makePath, put some files in it, deleteTree" {
2020 try fs.cwd().makePath("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "c");
2121 try io.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "c" ++ fs.path.sep_str ++ "file.txt", "nonsense");
2222 try io.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "file2.txt", "blah");
23 try fs.deleteTree("os_test_tmp");
23 try fs.cwd().deleteTree("os_test_tmp");
2424 if (fs.cwd().openDir("os_test_tmp", .{})) |dir| {
2525 @panic("expected error");
2626 } else |err| {
......@@ -38,7 +38,7 @@ test "access file" {
3838
3939 try io.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "file.txt", "");
4040 try os.access("os_test_tmp" ++ fs.path.sep_str ++ "file.txt", os.F_OK);
41 try fs.deleteTree("os_test_tmp");
41 try fs.cwd().deleteTree("os_test_tmp");
4242}
4343
4444fn testThreadIdFn(thread_id: *Thread.Id) void {
......@@ -47,7 +47,7 @@ fn testThreadIdFn(thread_id: *Thread.Id) void {
4747
4848test "sendfile" {
4949 try fs.cwd().makePath("os_test_tmp");
50 defer fs.deleteTree("os_test_tmp") catch {};
50 defer fs.cwd().deleteTree("os_test_tmp") catch {};
5151
5252 var dir = try fs.cwd().openDir("os_test_tmp", .{});
5353 defer dir.close();
src-self-hosted/compilation.zig+1-2
......@@ -520,8 +520,7 @@ pub const Compilation = struct {
520520
521521 if (comp.tmp_dir.getOrNull()) |tmp_dir_result|
522522 if (tmp_dir_result.*) |tmp_dir| {
523 // TODO evented I/O?
524 fs.deleteTree(tmp_dir) catch {};
523 fs.cwd().deleteTree(tmp_dir) catch {};
525524 } else |_| {};
526525 }
527526
src-self-hosted/test.zig+2-2
......@@ -57,11 +57,11 @@ pub const TestContext = struct {
5757 errdefer allocator.free(self.zig_lib_dir);
5858
5959 try std.fs.cwd().makePath(tmp_dir_name);
60 errdefer std.fs.deleteTree(tmp_dir_name) catch {};
60 errdefer std.fs.cwd().deleteTree(tmp_dir_name) catch {};
6161 }
6262
6363 fn deinit(self: *TestContext) void {
64 std.fs.deleteTree(tmp_dir_name) catch {};
64 std.fs.cwd().deleteTree(tmp_dir_name) catch {};
6565 allocator.free(self.zig_lib_dir);
6666 self.zig_compiler.deinit();
6767 }
test/cli.zig+1-1
......@@ -36,7 +36,7 @@ pub fn main() !void {
3636 testMissingOutputPath,
3737 };
3838 for (test_fns) |testFn| {
39 try fs.deleteTree(dir_path);
39 try fs.cwd().deleteTree(dir_path);
4040 try fs.cwd().makeDir(dir_path);
4141 try testFn(zig_exe, dir_path);
4242 }