Skip to content

Commit

Permalink
Use posix semantics when deleting files on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
SpexGuy committed Jun 4, 2022
1 parent 43db697 commit 6bd61f0
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion lib/std/os/windows.zig
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ pub fn DeleteFile(sub_path_w: []const u16, options: DeleteFileOptions) DeleteFil
0,
);
switch (rc) {
.SUCCESS => return CloseHandle(tmp_handle),
.SUCCESS => {},
.OBJECT_NAME_INVALID => unreachable,
.OBJECT_NAME_NOT_FOUND => return error.FileNotFound,
.OBJECT_PATH_NOT_FOUND => return error.FileNotFound,
Expand All @@ -910,6 +910,21 @@ pub fn DeleteFile(sub_path_w: []const u16, options: DeleteFileOptions) DeleteFil
.CANNOT_DELETE => return error.AccessDenied,
else => return unexpectedStatus(rc),
}
defer CloseHandle(tmp_handle);

var info = FILE_DISPOSITION_INFORMATION_EX{
.Flags = FILE_DISPOSITION_DELETE |
FILE_DISPOSITION_POSIX_SEMANTICS |
FILE_DISPOSITION_ON_CLOSE |
FILE_DISPOSITION_IGNORE_READONLY_ATTRIBUTE,
};
rc = ntdll.NtSetInformationFile(tmp_handle, &io, &info, @sizeOf(FILE_DISPOSITION_INFORMATION_EX), .FileDispositionInformationEx);
switch (rc) {
.SUCCESS => {},
// CANNOT_DELETE indicates that the file is currently mapped
.CANNOT_DELETE => return error.FileBusy,
else => return unexpectedStatus(rc),
}
}

pub const MoveFileError = error{ FileNotFound, AccessDenied, Unexpected };
Expand Down Expand Up @@ -2282,6 +2297,18 @@ pub const FILE_NAME_INFORMATION = extern struct {
FileName: [1]WCHAR,
};

pub const FILE_DISPOSITION_INFORMATION_EX = extern struct {
/// combination of FILE_DISPOSITION_* flags
Flags: ULONG,
};

const FILE_DISPOSITION_DO_NOT_DELETE: ULONG = 0x00000000;
const FILE_DISPOSITION_DELETE: ULONG = 0x00000001;
const FILE_DISPOSITION_POSIX_SEMANTICS: ULONG = 0x00000002;
const FILE_DISPOSITION_FORCE_IMAGE_SECTION_CHECK: ULONG = 0x00000004;
const FILE_DISPOSITION_ON_CLOSE: ULONG = 0x00000008;
const FILE_DISPOSITION_IGNORE_READONLY_ATTRIBUTE: ULONG = 0x00000010;

pub const FILE_RENAME_INFORMATION = extern struct {
ReplaceIfExists: BOOLEAN,
RootDirectory: ?HANDLE,
Expand Down

0 comments on commit 6bd61f0

Please sign in to comment.