Skip to content

Commit

Permalink
std.windows: use atomic rename, if possible
Browse files Browse the repository at this point in the history
Mitigates ziglang#14978.
  • Loading branch information
matu3ba committed Aug 21, 2023
1 parent 6a54639 commit 6d5fdd2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 14 deletions.
47 changes: 34 additions & 13 deletions lib/std/os.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2636,23 +2636,44 @@ pub fn renameatW(

const rename_info = @as(*windows.FILE_RENAME_INFORMATION, @ptrCast(&rename_info_buf));

rename_info.* = .{
.ReplaceIfExists = ReplaceIfExists,
.RootDirectory = if (std.fs.path.isAbsoluteWindowsWTF16(new_path_w)) null else new_dir_fd,
.FileNameLength = @as(u32, @intCast(new_path_w.len * 2)), // already checked error.NameTooLong
.FileName = undefined,
};
if (builtin.target.os.version_range.windows.min.isAtLeast(.win10_rs1)) {
var flags: windows.ULONG = windows.FILE_RENAME_POSIX_SEMANTICS | windows.FILE_RENAME_IGNORE_READONLY_ATTRIBUTE;
if (ReplaceIfExists == windows.TRUE) flags |= windows.FILE_RENAME_REPLACE_IF_EXISTS;
rename_info.* = .{
.Flags = flags,
.RootDirectory = if (std.fs.path.isAbsoluteWindowsWTF16(new_path_w)) null else new_dir_fd,
.FileNameLength = @intCast(new_path_w.len * 2), // already checked error.NameTooLong
.FileName = undefined,
};
} else {
rename_info.* = .{
.Flags = ReplaceIfExists,
.RootDirectory = if (std.fs.path.isAbsoluteWindowsWTF16(new_path_w)) null else new_dir_fd,
.FileNameLength = @intCast(new_path_w.len * 2), // already checked error.NameTooLong
.FileName = undefined,
};
}
@memcpy(@as([*]u16, &rename_info.FileName)[0..new_path_w.len], new_path_w);

var io_status_block: windows.IO_STATUS_BLOCK = undefined;

const rc = windows.ntdll.NtSetInformationFile(
src_fd,
&io_status_block,
rename_info,
@as(u32, @intCast(struct_len)), // already checked for error.NameTooLong
.FileRenameInformation,
);
const rc = if (comptime builtin.target.os.version_range.windows.min.isAtLeast(.win10_rs1)) {
windows.ntdll.NtSetInformationFile(
src_fd,
&io_status_block,
rename_info,
@intCast(struct_len), // already checked for error.NameTooLong
.FileRenameInformationEx,
);
} else {
windows.ntdll.NtSetInformationFile(
src_fd,
&io_status_block,
rename_info,
@intCast(struct_len), // already checked for error.NameTooLong
.FileRenameInformation,
);
};

switch (rc) {
.SUCCESS => return,
Expand Down
15 changes: 14 additions & 1 deletion lib/std/os/windows.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2856,8 +2856,21 @@ const FILE_DISPOSITION_FORCE_IMAGE_SECTION_CHECK: ULONG = 0x00000004;
const FILE_DISPOSITION_ON_CLOSE: ULONG = 0x00000008;
const FILE_DISPOSITION_IGNORE_READONLY_ATTRIBUTE: ULONG = 0x00000010;

// FILE_RENAME_INFORMATION.Flags
pub const FILE_RENAME_REPLACE_IF_EXISTS = 0x00000001;
pub const FILE_RENAME_POSIX_SEMANTICS = 0x00000002;
pub const FILE_RENAME_SUPPRESS_PIN_STATE_INHERITANCE = 0x00000004;
pub const FILE_RENAME_SUPPRESS_STORAGE_RESERVE_INHERITANCE = 0x00000008;
pub const FILE_RENAME_NO_INCREASE_AVAILABLE_SPACE = 0x00000010;
pub const FILE_RENAME_NO_DECREASE_AVAILABLE_SPACE = 0x00000020;
pub const FILE_RENAME_PRESERVE_AVAILABLE_SPACE = 0x00000030;
pub const FILE_RENAME_IGNORE_READONLY_ATTRIBUTE = 0x00000040;
pub const FILE_RENAME_FORCE_RESIZE_TARGET_SR = 0x00000080;
pub const FILE_RENAME_FORCE_RESIZE_SOURCE_SR = 0x00000100;
pub const FILE_RENAME_FORCE_RESIZE_SR = 0x00000180;

pub const FILE_RENAME_INFORMATION = extern struct {
ReplaceIfExists: BOOLEAN,
Flags: if (builtin.target.os.version_range.windows.min.isAtLeast(.win10_rs1)) ULONG else BOOLEAN,
RootDirectory: ?HANDLE,
FileNameLength: ULONG,
FileName: [1]WCHAR,
Expand Down

0 comments on commit 6d5fdd2

Please sign in to comment.