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 {...@@ -55,7 +55,7 @@ pub fn main() !void {
55 // TODO issue #70955 // TODO issue #709
56 // disabled to pass CI tests, but obviously we want to implement this56 // disabled to pass CI tests, but obviously we want to implement this
57 // and then remove this workaround57 // and then remove this workaround
58 if (builtin.os == builtin.Os.linux) {58 if (builtin.os != builtin.Os.windows) {
59 os.deleteTree(allocator, tmp_dir_name) catch {};59 os.deleteTree(allocator, tmp_dir_name) catch {};
60 }60 }
61 }61 }
std/os/index.zig+8-3
...@@ -1050,14 +1050,14 @@ const DeleteTreeError = error {...@@ -1050,14 +1050,14 @@ const DeleteTreeError = error {
1050};1050};
1051pub fn deleteTree(allocator: &Allocator, full_path: []const u8) DeleteTreeError!void {1051pub fn deleteTree(allocator: &Allocator, full_path: []const u8) DeleteTreeError!void {
1052 start_over: while (true) {1052 start_over: while (true) {
1053 var got_access_denied = false;
1053 // First, try deleting the item as a file. This way we don't follow sym links.1054 // First, try deleting the item as a file. This way we don't follow sym links.
1054 if (deleteFile(allocator, full_path)) {1055 if (deleteFile(allocator, full_path)) {
1055 return;1056 return;
1056 } else |err| switch (err) {1057 } else |err| switch (err) {
1057 error.FileNotFound => return,1058 error.FileNotFound => return,
1058
1059 error.AccessDenied,
1060 error.IsDir => {},1059 error.IsDir => {},
1060 error.AccessDenied => got_access_denied = true,
10611061
1062 error.OutOfMemory,1062 error.OutOfMemory,
1063 error.SymLinkLoop,1063 error.SymLinkLoop,
...@@ -1072,7 +1072,12 @@ pub fn deleteTree(allocator: &Allocator, full_path: []const u8) DeleteTreeError!...@@ -1072,7 +1072,12 @@ pub fn deleteTree(allocator: &Allocator, full_path: []const u8) DeleteTreeError!
1072 }1072 }
1073 {1073 {
1074 var dir = Dir.open(allocator, full_path) catch |err| switch (err) {1074 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
1077 error.OutOfMemory,1082 error.OutOfMemory,
1078 error.AccessDenied,1083 error.AccessDenied,
std/os/test.zig+10-3
...@@ -1,18 +1,25 @@...@@ -1,18 +1,25 @@
1const std = @import("../index.zig");1const std = @import("../index.zig");
2const os = std.os;2const os = std.os;
3const debug = std.debug;3const assert = std.debug.assert;
4const io = std.io;4const io = std.io;
55
6const a = std.debug.global_allocator;6const a = std.debug.global_allocator;
77
8const builtin = @import("builtin");
9
8test "makePath, put some files in it, deleteTree" {10test "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 }
9 try os.makePath(a, "os_test_tmp/b/c");16 try os.makePath(a, "os_test_tmp/b/c");
10 try io.writeFile(a, "os_test_tmp/b/c/file.txt", "nonsense");17 try io.writeFile(a, "os_test_tmp/b/c/file.txt", "nonsense");
11 try io.writeFile(a, "os_test_tmp/b/file2.txt", "blah");18 try io.writeFile(a, "os_test_tmp/b/file2.txt", "blah");
12 try os.deleteTree(a, "os_test_tmp");19 try os.deleteTree(a, "os_test_tmp");
13 if (os.Dir.open(a, "os_test_tmp")) |dir| {20 if (os.Dir.open(a, "os_test_tmp")) |dir| {
14 debug.assert(false); // this should not happen!21 @panic("expected error");
15 } else |err| {22 } else |err| {
16 debug.assert(err == error.PathNotFound);23 assert(err == error.PathNotFound);
17 }24 }
18}25}