authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-04 00:08:10-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-04 00:08:10-04:00
logabd389209b2b25ac3d3567797bff580c2ce7d9ad
tree68400346b0bbd5fb3b124742fab41c6b7dd788b8
parente1e536e03d28942fe6dfa4a9f3881af3fb57a458

fix up logic for macos std.os.deleteTree


3 files changed, 19 insertions(+), 7 deletions(-)

doc/docgen.zig+1-1
......@@ -55,7 +55,7 @@ pub fn main() !void {
5555 // TODO issue #709
5656 // disabled to pass CI tests, but obviously we want to implement this
5757 // and then remove this workaround
58 if (builtin.os == builtin.Os.linux) {
58 if (builtin.os != builtin.Os.windows) {
5959 os.deleteTree(allocator, tmp_dir_name) catch {};
6060 }
6161 }
std/os/index.zig+8-3
......@@ -1050,14 +1050,14 @@ const DeleteTreeError = error {
10501050};
10511051pub fn deleteTree(allocator: &Allocator, full_path: []const u8) DeleteTreeError!void {
10521052 start_over: while (true) {
1053 var got_access_denied = false;
10531054 // First, try deleting the item as a file. This way we don't follow sym links.
10541055 if (deleteFile(allocator, full_path)) {
10551056 return;
10561057 } else |err| switch (err) {
10571058 error.FileNotFound => return,
1058
1059 error.AccessDenied,
10601059 error.IsDir => {},
1060 error.AccessDenied => got_access_denied = true,
10611061
10621062 error.OutOfMemory,
10631063 error.SymLinkLoop,
......@@ -1072,7 +1072,12 @@ pub fn deleteTree(allocator: &Allocator, full_path: []const u8) DeleteTreeError!
10721072 }
10731073 {
10741074 var dir = Dir.open(allocator, full_path) catch |err| switch (err) {
1075 error.NotDir => continue :start_over,
1075 error.NotDir => {
1076 if (got_access_denied) {
1077 return error.AccessDenied;
1078 }
1079 continue :start_over;
1080 },
10761081
10771082 error.OutOfMemory,
10781083 error.AccessDenied,
std/os/test.zig+10-3
......@@ -1,18 +1,25 @@
11const std = @import("../index.zig");
22const os = std.os;
3const debug = std.debug;
3const assert = std.debug.assert;
44const io = std.io;
55
66const a = std.debug.global_allocator;
77
8const builtin = @import("builtin");
9
810test "makePath, put some files in it, deleteTree" {
11 if (builtin.os == builtin.Os.windows) {
12 // TODO implement os.Dir for windows
13 // https://github.com/zig-lang/zig/issues/709
14 return;
15 }
916 try os.makePath(a, "os_test_tmp/b/c");
1017 try io.writeFile(a, "os_test_tmp/b/c/file.txt", "nonsense");
1118 try io.writeFile(a, "os_test_tmp/b/file2.txt", "blah");
1219 try os.deleteTree(a, "os_test_tmp");
1320 if (os.Dir.open(a, "os_test_tmp")) |dir| {
14 debug.assert(false); // this should not happen!
21 @panic("expected error");
1522 } else |err| {
16 debug.assert(err == error.PathNotFound);
23 assert(err == error.PathNotFound);
1724 }
1825}