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 {...@@ -48,7 +48,7 @@ pub fn main() !void {
48 var toc = try genToc(allocator, &tokenizer);48 var toc = try genToc(allocator, &tokenizer);
4949
50 try fs.cwd().makePath(tmp_dir_name);50 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
53 try genHtml(allocator, &tokenizer, &toc, buffered_out_stream.outStream(), zig_exe);53 try genHtml(allocator, &tokenizer, &toc, buffered_out_stream.outStream(), zig_exe);
54 try buffered_out_stream.flush();54 try buffered_out_stream.flush();
lib/std/build.zig+2-2
...@@ -377,7 +377,7 @@ pub const Builder = struct {...@@ -377,7 +377,7 @@ pub const Builder = struct {
377 if (self.verbose) {377 if (self.verbose) {
378 warn("rm {}\n", .{full_path});378 warn("rm {}\n", .{full_path});
379 }379 }
380 fs.deleteTree(full_path) catch {};380 fs.cwd().deleteTree(full_path) catch {};
381 }381 }
382382
383 // TODO remove empty directories383 // TODO remove empty directories
...@@ -2365,7 +2365,7 @@ pub const RemoveDirStep = struct {...@@ -2365,7 +2365,7 @@ pub const RemoveDirStep = struct {
2365 const self = @fieldParentPtr(RemoveDirStep, "step", step);2365 const self = @fieldParentPtr(RemoveDirStep, "step", step);
23662366
2367 const full_path = self.builder.pathFromRoot(self.dir_path);2367 const full_path = self.builder.pathFromRoot(self.dir_path);
2368 fs.deleteTree(full_path) catch |err| {2368 fs.cwd().deleteTree(full_path) catch |err| {
2369 warn("Unable to remove {}: {}\n", .{ full_path, @errorName(err) });2369 warn("Unable to remove {}: {}\n", .{ full_path, @errorName(err) });
2370 return err;2370 return err;
2371 };2371 };
lib/std/fs.zig+23-25
...@@ -261,28 +261,6 @@ pub fn deleteDirW(dir_path: [*:0]const u16) !void {...@@ -261,28 +261,6 @@ pub fn deleteDirW(dir_path: [*:0]const u16) !void {
261 return os.rmdirW(dir_path);261 return os.rmdirW(dir_path);
262}262}
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
286pub const Dir = struct {264pub const Dir = struct {
287 fd: os.fd_t,265 fd: os.fd_t,
288266
...@@ -518,7 +496,8 @@ pub const Dir = struct {...@@ -518,7 +496,8 @@ pub const Dir = struct {
518 self.end_index = io.Information;496 self.end_index = io.Information;
519 switch (rc) {497 switch (rc) {
520 .SUCCESS => {},498 .SUCCESS => {},
521 .ACCESS_DENIED => return error.AccessDenied,499 .ACCESS_DENIED => return error.AccessDenied, // Double-check that the Dir was opened with iteration ability
500
522 else => return w.unexpectedStatus(rc),501 else => return w.unexpectedStatus(rc),
523 }502 }
524 }503 }
...@@ -827,7 +806,7 @@ pub const Dir = struct {...@@ -827,7 +806,7 @@ pub const Dir = struct {
827 // TODO remove some of these flags if args.access_sub_paths is false806 // TODO remove some of these flags if args.access_sub_paths is false
828 const base_flags = w.STANDARD_RIGHTS_READ | w.FILE_READ_ATTRIBUTES | w.FILE_READ_EA |807 const base_flags = w.STANDARD_RIGHTS_READ | w.FILE_READ_ATTRIBUTES | w.FILE_READ_EA |
829 w.SYNCHRONIZE | w.FILE_TRAVERSE;808 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;
831 return self.openDirAccessMaskW(sub_path_w, flags);810 return self.openDirAccessMaskW(sub_path_w, flags);
832 }811 }
833812
...@@ -1304,7 +1283,7 @@ pub const Dir = struct {...@@ -1304,7 +1283,7 @@ pub const Dir = struct {
1304 }1283 }
1305};1284};
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.
1308/// Closing the returned `Dir` is checked illegal behavior. Iterating over the result is illegal behavior.1287/// Closing the returned `Dir` is checked illegal behavior. Iterating over the result is illegal behavior.
1309/// On POSIX targets, this function is comptime-callable.1288/// On POSIX targets, this function is comptime-callable.
1310pub fn cwd() Dir {1289pub fn cwd() Dir {
...@@ -1382,6 +1361,25 @@ pub fn deleteFileAbsoluteW(absolute_path_w: [*:0]const u16) DeleteFileError!void...@@ -1382,6 +1361,25 @@ pub fn deleteFileAbsoluteW(absolute_path_w: [*:0]const u16) DeleteFileError!void
1382 return cwd().deleteFileW(absolute_path_w);1361 return cwd().deleteFileW(absolute_path_w);
1383}1362}
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
1385pub const Walker = struct {1383pub const Walker = struct {
1386 stack: std.ArrayList(StackItem),1384 stack: std.ArrayList(StackItem),
1387 name_buffer: std.Buffer,1385 name_buffer: std.Buffer,
lib/std/fs/watch.zig+1-1
...@@ -619,7 +619,7 @@ test "write a file, watch it, write it again" {...@@ -619,7 +619,7 @@ test "write a file, watch it, write it again" {
619 if (true) return error.SkipZigTest;619 if (true) return error.SkipZigTest;
620620
621 try fs.cwd().makePath(test_tmp_dir);621 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
624 const allocator = std.heap.page_allocator;624 const allocator = std.heap.page_allocator;
625 return testFsWatch(&allocator);625 return testFsWatch(&allocator);
lib/std/os/test.zig+3-3
...@@ -20,7 +20,7 @@ test "makePath, put some files in it, deleteTree" {...@@ -20,7 +20,7 @@ test "makePath, put some files in it, deleteTree" {
20 try fs.cwd().makePath("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "c");20 try fs.cwd().makePath("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "c");
21 try io.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "c" ++ fs.path.sep_str ++ "file.txt", "nonsense");21 try io.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "c" ++ fs.path.sep_str ++ "file.txt", "nonsense");
22 try io.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "file2.txt", "blah");22 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");
24 if (fs.cwd().openDir("os_test_tmp", .{})) |dir| {24 if (fs.cwd().openDir("os_test_tmp", .{})) |dir| {
25 @panic("expected error");25 @panic("expected error");
26 } else |err| {26 } else |err| {
...@@ -38,7 +38,7 @@ test "access file" {...@@ -38,7 +38,7 @@ test "access file" {
3838
39 try io.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "file.txt", "");39 try io.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "file.txt", "");
40 try os.access("os_test_tmp" ++ fs.path.sep_str ++ "file.txt", os.F_OK);40 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");
42}42}
4343
44fn testThreadIdFn(thread_id: *Thread.Id) void {44fn testThreadIdFn(thread_id: *Thread.Id) void {
...@@ -47,7 +47,7 @@ fn testThreadIdFn(thread_id: *Thread.Id) void {...@@ -47,7 +47,7 @@ fn testThreadIdFn(thread_id: *Thread.Id) void {
4747
48test "sendfile" {48test "sendfile" {
49 try fs.cwd().makePath("os_test_tmp");49 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
52 var dir = try fs.cwd().openDir("os_test_tmp", .{});52 var dir = try fs.cwd().openDir("os_test_tmp", .{});
53 defer dir.close();53 defer dir.close();
src-self-hosted/compilation.zig+1-2
...@@ -520,8 +520,7 @@ pub const Compilation = struct {...@@ -520,8 +520,7 @@ pub const Compilation = struct {
520520
521 if (comp.tmp_dir.getOrNull()) |tmp_dir_result|521 if (comp.tmp_dir.getOrNull()) |tmp_dir_result|
522 if (tmp_dir_result.*) |tmp_dir| {522 if (tmp_dir_result.*) |tmp_dir| {
523 // TODO evented I/O?523 fs.cwd().deleteTree(tmp_dir) catch {};
524 fs.deleteTree(tmp_dir) catch {};
525 } else |_| {};524 } else |_| {};
526 }525 }
527526
src-self-hosted/test.zig+2-2
...@@ -57,11 +57,11 @@ pub const TestContext = struct {...@@ -57,11 +57,11 @@ pub const TestContext = struct {
57 errdefer allocator.free(self.zig_lib_dir);57 errdefer allocator.free(self.zig_lib_dir);
5858
59 try std.fs.cwd().makePath(tmp_dir_name);59 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 {};
61 }61 }
6262
63 fn deinit(self: *TestContext) void {63 fn deinit(self: *TestContext) void {
64 std.fs.deleteTree(tmp_dir_name) catch {};64 std.fs.cwd().deleteTree(tmp_dir_name) catch {};
65 allocator.free(self.zig_lib_dir);65 allocator.free(self.zig_lib_dir);
66 self.zig_compiler.deinit();66 self.zig_compiler.deinit();
67 }67 }
test/cli.zig+1-1
...@@ -36,7 +36,7 @@ pub fn main() !void {...@@ -36,7 +36,7 @@ pub fn main() !void {
36 testMissingOutputPath,36 testMissingOutputPath,
37 };37 };
38 for (test_fns) |testFn| {38 for (test_fns) |testFn| {
39 try fs.deleteTree(dir_path);39 try fs.cwd().deleteTree(dir_path);
40 try fs.cwd().makeDir(dir_path);40 try fs.cwd().makeDir(dir_path);
41 try testFn(zig_exe, dir_path);41 try testFn(zig_exe, dir_path);
42 }42 }