From f78652484a2bf5ee967d0ed7ca1db495932fad48 Mon Sep 17 00:00:00 2001 From: Suirad Date: Tue, 22 Sep 2020 20:05:12 -0500 Subject: [PATCH 1/2] Stdlib fix for os.windows.deleteFile to fail with a proper error when attempting to delete a directory that isnt empty --- lib/std/fs/test.zig | 23 +++++++++++++++++++++++ lib/std/os/windows.zig | 16 +++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/lib/std/fs/test.zig b/lib/std/fs/test.zig index b3cc1fe569..8d7ef5172e 100644 --- a/lib/std/fs/test.zig +++ b/lib/std/fs/test.zig @@ -813,3 +813,26 @@ fn run_lock_file_test(contexts: []FileLockTestContext) !void { try threads.append(try std.Thread.spawn(ctx, FileLockTestContext.run)); } } + +test "deleteDir" { + var tmp_dir = tmpDir(.{}); + defer tmp_dir.cleanup(); + + // deleting a non-existent directory + testing.expectError(error.FileNotFound, tmp_dir.dir.deleteDir("test_dir")); + + var dir = try tmp_dir.dir.makeOpenPath("test_dir", .{}); + var file = try dir.createFile("test_file", .{}); + file.close(); + dir.close(); + + // deleting a non-empty directory + testing.expectError(error.DirNotEmpty, tmp_dir.dir.deleteDir("test_dir")); + + dir = try tmp_dir.dir.openDir("test_dir", .{}); + try dir.deleteFile("test_file"); + dir.close(); + + // deleting an empty directory + try tmp_dir.dir.deleteDir("test_dir"); +} diff --git a/lib/std/os/windows.zig b/lib/std/os/windows.zig index de0d0ea45f..c4037ccf0a 100644 --- a/lib/std/os/windows.zig +++ b/lib/std/os/windows.zig @@ -764,6 +764,7 @@ pub const DeleteFileError = error{ Unexpected, NotDir, IsDir, + DirNotEmpty, }; pub const DeleteFileOptions = struct { @@ -818,7 +819,7 @@ pub fn DeleteFile(sub_path_w: []const u16, options: DeleteFileOptions) DeleteFil 0, ); switch (rc) { - .SUCCESS => return CloseHandle(tmp_handle), + .SUCCESS => CloseHandle(tmp_handle), .OBJECT_NAME_INVALID => unreachable, .OBJECT_NAME_NOT_FOUND => return error.FileNotFound, .INVALID_PARAMETER => unreachable, @@ -826,6 +827,19 @@ pub fn DeleteFile(sub_path_w: []const u16, options: DeleteFileOptions) DeleteFil .NOT_A_DIRECTORY => return error.NotDir, else => return unexpectedStatus(rc), } + + if (options.remove_dir){ + var basic_info: FILE_BASIC_INFORMATION = undefined; + switch (ntdll.NtQueryAttributesFile(&attr, &basic_info)) { + .SUCCESS => return error.DirNotEmpty, + .OBJECT_NAME_NOT_FOUND => return, + .OBJECT_PATH_NOT_FOUND => return, + .INVALID_PARAMETER => unreachable, + .ACCESS_DENIED => return error.AccessDenied, + .OBJECT_PATH_SYNTAX_BAD => unreachable, + else => |urc| return unexpectedStatus(urc), + } + } } pub const MoveFileError = error{ FileNotFound, Unexpected }; From 43cd9eb110f6803f4e19d92347ebf263e6e644af Mon Sep 17 00:00:00 2001 From: Suirad Date: Fri, 25 Sep 2020 18:11:31 -0500 Subject: [PATCH 2/2] Add clarification comment --- lib/std/os/windows.zig | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/std/os/windows.zig b/lib/std/os/windows.zig index c4037ccf0a..2aa222414f 100644 --- a/lib/std/os/windows.zig +++ b/lib/std/os/windows.zig @@ -828,7 +828,9 @@ pub fn DeleteFile(sub_path_w: []const u16, options: DeleteFileOptions) DeleteFil else => return unexpectedStatus(rc), } - if (options.remove_dir){ + // If a directory fails to be deleted, CloseHandle will still report success + // Check if the directory still exists and return error.DirNotEmpty if true + if (options.remove_dir) { var basic_info: FILE_BASIC_INFORMATION = undefined; switch (ntdll.NtQueryAttributesFile(&attr, &basic_info)) { .SUCCESS => return error.DirNotEmpty,