Skip to content

Commit

Permalink
Win: Remove special casing of the win7 target for std::fs::rename
Browse files Browse the repository at this point in the history
  • Loading branch information
Fulgen301 authored and gitbot committed Feb 20, 2025
1 parent 07430bf commit d7ec7cf
Showing 1 changed file with 1 addition and 14 deletions.
15 changes: 1 addition & 14 deletions std/src/sys/pal/windows/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1316,11 +1316,7 @@ pub fn rename(old: &Path, new: &Path) -> io::Result<()> {
// SAFETY: We have allocated enough memory for a full FILE_RENAME_INFO struct and a filename.
unsafe {
(&raw mut (*file_rename_info).Anonymous).write(c::FILE_RENAME_INFO_0 {
// Don't bother with FileRenameInfo on Windows 7 since it doesn't exist.
#[cfg(not(target_vendor = "win7"))]
Flags: c::FILE_RENAME_FLAG_REPLACE_IF_EXISTS | c::FILE_RENAME_FLAG_POSIX_SEMANTICS,
#[cfg(target_vendor = "win7")]
ReplaceIfExists: 1,
});

(&raw mut (*file_rename_info).RootDirectory).write(ptr::null_mut());
Expand All @@ -1330,22 +1326,16 @@ pub fn rename(old: &Path, new: &Path) -> io::Result<()> {
.copy_to_nonoverlapping((&raw mut (*file_rename_info).FileName) as *mut u16, new.len());
}

#[cfg(not(target_vendor = "win7"))]
const FileInformationClass: c::FILE_INFO_BY_HANDLE_CLASS = c::FileRenameInfoEx;
#[cfg(target_vendor = "win7")]
const FileInformationClass: c::FILE_INFO_BY_HANDLE_CLASS = c::FileRenameInfo;

// We don't use `set_file_information_by_handle` here as `FILE_RENAME_INFO` is used for both `FileRenameInfo` and `FileRenameInfoEx`.
let result = unsafe {
cvt(c::SetFileInformationByHandle(
handle.as_raw_handle(),
FileInformationClass,
c::FileRenameInfoEx,
(&raw const *file_rename_info).cast::<c_void>(),
struct_size,
))
};

#[cfg(not(target_vendor = "win7"))]
if let Err(err) = result {
if err.raw_os_error() == Some(c::ERROR_INVALID_PARAMETER as _) {
// FileRenameInfoEx and FILE_RENAME_FLAG_POSIX_SEMANTICS were added in Windows 10 1607; retry with FileRenameInfo.
Expand All @@ -1364,9 +1354,6 @@ pub fn rename(old: &Path, new: &Path) -> io::Result<()> {
}
}

#[cfg(target_vendor = "win7")]
result?;

Ok(())
}

Expand Down

0 comments on commit d7ec7cf

Please sign in to comment.