authorgravatar for suirad@users.noreply.github.comSuirad <suirad@users.noreply.github.com> 2020-09-22 20:05:12-05:00
committergravatar for suirad@users.noreply.github.comSuirad <suirad@users.noreply.github.com> 2020-09-25 18:09:05-05:00
logf78652484a2bf5ee967d0ed7ca1db495932fad48
treea4c630c5a3049a0eb32434a4b1764387f7ece4b9
parent288198e51d4b6a0bde8b6beae9dd63f68c55e1eb

Stdlib fix for os.windows.deleteFile to fail with

a proper error when attempting to delete a directory that isnt empty

2 files changed, 38 insertions(+), 1 deletions(-)

lib/std/fs/test.zig+23
......@@ -813,3 +813,26 @@ fn run_lock_file_test(contexts: []FileLockTestContext) !void {
813813 try threads.append(try std.Thread.spawn(ctx, FileLockTestContext.run));
814814 }
815815}
816
817test "deleteDir" {
818 var tmp_dir = tmpDir(.{});
819 defer tmp_dir.cleanup();
820
821 // deleting a non-existent directory
822 testing.expectError(error.FileNotFound, tmp_dir.dir.deleteDir("test_dir"));
823
824 var dir = try tmp_dir.dir.makeOpenPath("test_dir", .{});
825 var file = try dir.createFile("test_file", .{});
826 file.close();
827 dir.close();
828
829 // deleting a non-empty directory
830 testing.expectError(error.DirNotEmpty, tmp_dir.dir.deleteDir("test_dir"));
831
832 dir = try tmp_dir.dir.openDir("test_dir", .{});
833 try dir.deleteFile("test_file");
834 dir.close();
835
836 // deleting an empty directory
837 try tmp_dir.dir.deleteDir("test_dir");
838}
lib/std/os/windows.zig+15-1
......@@ -764,6 +764,7 @@ pub const DeleteFileError = error{
764764 Unexpected,
765765 NotDir,
766766 IsDir,
767 DirNotEmpty,
767768};
768769
769770pub const DeleteFileOptions = struct {
......@@ -818,7 +819,7 @@ pub fn DeleteFile(sub_path_w: []const u16, options: DeleteFileOptions) DeleteFil
818819 0,
819820 );
820821 switch (rc) {
821 .SUCCESS => return CloseHandle(tmp_handle),
822 .SUCCESS => CloseHandle(tmp_handle),
822823 .OBJECT_NAME_INVALID => unreachable,
823824 .OBJECT_NAME_NOT_FOUND => return error.FileNotFound,
824825 .INVALID_PARAMETER => unreachable,
......@@ -826,6 +827,19 @@ pub fn DeleteFile(sub_path_w: []const u16, options: DeleteFileOptions) DeleteFil
826827 .NOT_A_DIRECTORY => return error.NotDir,
827828 else => return unexpectedStatus(rc),
828829 }
830
831 if (options.remove_dir){
832 var basic_info: FILE_BASIC_INFORMATION = undefined;
833 switch (ntdll.NtQueryAttributesFile(&attr, &basic_info)) {
834 .SUCCESS => return error.DirNotEmpty,
835 .OBJECT_NAME_NOT_FOUND => return,
836 .OBJECT_PATH_NOT_FOUND => return,
837 .INVALID_PARAMETER => unreachable,
838 .ACCESS_DENIED => return error.AccessDenied,
839 .OBJECT_PATH_SYNTAX_BAD => unreachable,
840 else => |urc| return unexpectedStatus(urc),
841 }
842 }
829843}
830844
831845pub const MoveFileError = error{ FileNotFound, Unexpected };