windows: Always try using POSIX_SEMANTICS/etc for rename/delete

The compile-time check against the minimum version here wasn't appropriate, since it still makes sense to try using FILE_RENAME_INFORMATION_EX even if the minimum version is something like `xp`, since that doesn't rule out the possibility of the compiled code running on Windows 10/11. This compile-time check was doubly bad since the default minimum windows version (`.win10`) was below the `.win10_rs5` that was checked for, so when providing a target like `x86_64-windows-gnu` it'd always rule out using this syscall.

After this commit, we always try using FILE_RENAME_INFORMATION_EX and then let the operating system tell us when some aspect of it is not supported. This allows us to get the benefits of these new syscalls/flags whenever it's actually possible.

The possible error returns were validated experimentally:
- INVALID_PARAMETER is returned when the underlying filesystem is FAT32
- INVALID_INFO_CLASS is returned on Windows 7 when trying to use FileRenameInformationEx/FileDispositionInformationEx
- NOT_SUPPORTED is returned on Windows 10 >= .win10_rs5 when setting a bogus flag value (I used `0x1000`)
This commit is contained in:
Ryan Liptak 2025-10-17 00:50:16 -07:00
parent 3eb3fbec9c
commit 88fd8ce860
2 changed files with 50 additions and 31 deletions

View File

@ -1071,13 +1071,18 @@ pub fn DeleteFile(sub_path_w: []const u16, options: DeleteFileOptions) DeleteFil
} }
defer CloseHandle(tmp_handle); defer CloseHandle(tmp_handle);
// FileDispositionInformationEx (and therefore FILE_DISPOSITION_POSIX_SEMANTICS and FILE_DISPOSITION_IGNORE_READONLY_ATTRIBUTE) // FileDispositionInformationEx has varying levels of support:
// are only supported on NTFS filesystems, so the version check on its own is only a partial solution. To support non-NTFS filesystems // - FILE_DISPOSITION_INFORMATION_EX requires >= win10_rs1
// like FAT32, we need to fallback to FileDispositionInformation if the usage of FileDispositionInformationEx gives // (INVALID_INFO_CLASS is returned if not supported)
// us INVALID_PARAMETER. // - Requires the NTFS filesystem
// The same reasoning for win10_rs5 as in os.renameatW() applies (FILE_DISPOSITION_IGNORE_READONLY_ATTRIBUTE requires >= win10_rs5). // (on filesystems like FAT32, INVALID_PARAMETER is returned)
var need_fallback = true; // - FILE_DISPOSITION_POSIX_SEMANTICS requires >= win10_rs1
if (comptime builtin.target.os.version_range.windows.min.isAtLeast(.win10_rs5)) { // - FILE_DISPOSITION_IGNORE_READONLY_ATTRIBUTE requires >= win10_rs5
// (NOT_SUPPORTED is returned if a flag is unsupported)
//
// The strategy here is just to try using FileDispositionInformationEx and fall back to
// FileDispositionInformation if the return value lets us know that some aspect of it is not supported.
const need_fallback = need_fallback: {
// Deletion with posix semantics if the filesystem supports it. // Deletion with posix semantics if the filesystem supports it.
var info = FILE_DISPOSITION_INFORMATION_EX{ var info = FILE_DISPOSITION_INFORMATION_EX{
.Flags = FILE_DISPOSITION_DELETE | .Flags = FILE_DISPOSITION_DELETE |
@ -1094,12 +1099,18 @@ pub fn DeleteFile(sub_path_w: []const u16, options: DeleteFileOptions) DeleteFil
); );
switch (rc) { switch (rc) {
.SUCCESS => return, .SUCCESS => return,
// INVALID_PARAMETER here means that the filesystem does not support FileDispositionInformationEx // The filesystem does not support FileDispositionInformationEx
.INVALID_PARAMETER => {}, .INVALID_PARAMETER,
// The operating system does not support FileDispositionInformationEx
.INVALID_INFO_CLASS,
// The operating system does not support one of the flags
.NOT_SUPPORTED,
=> break :need_fallback true,
// For all other statuses, fall down to the switch below to handle them. // For all other statuses, fall down to the switch below to handle them.
else => need_fallback = false, else => break :need_fallback false,
} }
} };
if (need_fallback) { if (need_fallback) {
// Deletion with file pending semantics, which requires waiting or moving // Deletion with file pending semantics, which requires waiting or moving
// files to get them removed (from here). // files to get them removed (from here).

View File

@ -2844,15 +2844,19 @@ pub fn renameatW(
}; };
defer windows.CloseHandle(src_fd); defer windows.CloseHandle(src_fd);
var need_fallback = true;
var rc: windows.NTSTATUS = undefined; var rc: windows.NTSTATUS = undefined;
// FILE_RENAME_INFORMATION_EX and FILE_RENAME_POSIX_SEMANTICS require >= win10_rs1, // FileRenameInformationEx has varying levels of support:
// but FILE_RENAME_IGNORE_READONLY_ATTRIBUTE requires >= win10_rs5. We check >= rs5 here // - FILE_RENAME_INFORMATION_EX requires >= win10_rs1
// so that we only use POSIX_SEMANTICS when we know IGNORE_READONLY_ATTRIBUTE will also be // (INVALID_INFO_CLASS is returned if not supported)
// supported in order to avoid either (1) using a redundant call that we can know in advance will return // - Requires the NTFS filesystem
// STATUS_NOT_SUPPORTED or (2) only setting IGNORE_READONLY_ATTRIBUTE when >= rs5 // (on filesystems like FAT32, INVALID_PARAMETER is returned)
// and therefore having different behavior when the Windows version is >= rs1 but < rs5. // - FILE_RENAME_POSIX_SEMANTICS requires >= win10_rs1
if (builtin.target.os.isAtLeast(.windows, .win10_rs5) orelse false) { // - FILE_RENAME_IGNORE_READONLY_ATTRIBUTE requires >= win10_rs5
// (NOT_SUPPORTED is returned if a flag is unsupported)
//
// The strategy here is just to try using FileRenameInformationEx and fall back to
// FileRenameInformation if the return value lets us know that some aspect of it is not supported.
const need_fallback = need_fallback: {
const struct_buf_len = @sizeOf(windows.FILE_RENAME_INFORMATION_EX) + (max_path_bytes - 1); const struct_buf_len = @sizeOf(windows.FILE_RENAME_INFORMATION_EX) + (max_path_bytes - 1);
var rename_info_buf: [struct_buf_len]u8 align(@alignOf(windows.FILE_RENAME_INFORMATION_EX)) = undefined; var rename_info_buf: [struct_buf_len]u8 align(@alignOf(windows.FILE_RENAME_INFORMATION_EX)) = undefined;
const struct_len = @sizeOf(windows.FILE_RENAME_INFORMATION_EX) - 1 + new_path_w.len * 2; const struct_len = @sizeOf(windows.FILE_RENAME_INFORMATION_EX) - 1 + new_path_w.len * 2;
@ -2879,12 +2883,17 @@ pub fn renameatW(
); );
switch (rc) { switch (rc) {
.SUCCESS => return, .SUCCESS => return,
// INVALID_PARAMETER here means that the filesystem does not support FileRenameInformationEx // The filesystem does not support FileDispositionInformationEx
.INVALID_PARAMETER => {}, .INVALID_PARAMETER,
// The operating system does not support FileDispositionInformationEx
.INVALID_INFO_CLASS,
// The operating system does not support one of the flags
.NOT_SUPPORTED,
=> break :need_fallback true,
// For all other statuses, fall down to the switch below to handle them. // For all other statuses, fall down to the switch below to handle them.
else => need_fallback = false, else => break :need_fallback false,
} }
} };
if (need_fallback) { if (need_fallback) {
const struct_buf_len = @sizeOf(windows.FILE_RENAME_INFORMATION) + (max_path_bytes - 1); const struct_buf_len = @sizeOf(windows.FILE_RENAME_INFORMATION) + (max_path_bytes - 1);
@ -2903,14 +2912,13 @@ pub fn renameatW(
}; };
@memcpy((&rename_info.FileName).ptr, new_path_w); @memcpy((&rename_info.FileName).ptr, new_path_w);
rc = rc = windows.ntdll.NtSetInformationFile(
windows.ntdll.NtSetInformationFile( src_fd,
src_fd, &io_status_block,
&io_status_block, rename_info,
rename_info, @intCast(struct_len), // already checked for error.NameTooLong
@intCast(struct_len), // already checked for error.NameTooLong .FileRenameInformation,
.FileRenameInformation, );
);
} }
switch (rc) { switch (rc) {