Skip to content

Commit 1ec4f81

Browse files
Revert "Win: Remove special casing of the win7 target for std::fs::rename"
This reverts commit 8975a6d.
1 parent 085c4c9 commit 1ec4f81

File tree

1 file changed

+14
-1
lines changed
  • library/std/src/sys/pal/windows

1 file changed

+14
-1
lines changed

library/std/src/sys/pal/windows/fs.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1350,7 +1350,11 @@ pub fn rename(old: &Path, new: &Path) -> io::Result<()> {
13501350
// SAFETY: We have allocated enough memory for a full FILE_RENAME_INFO struct and a filename.
13511351
unsafe {
13521352
(&raw mut (*file_rename_info).Anonymous).write(c::FILE_RENAME_INFO_0 {
1353+
// Don't bother with FileRenameInfo on Windows 7 since it doesn't exist.
1354+
#[cfg(not(target_vendor = "win7"))]
13531355
Flags: c::FILE_RENAME_FLAG_REPLACE_IF_EXISTS | c::FILE_RENAME_FLAG_POSIX_SEMANTICS,
1356+
#[cfg(target_vendor = "win7")]
1357+
ReplaceIfExists: 1,
13541358
});
13551359

13561360
(&raw mut (*file_rename_info).RootDirectory).write(ptr::null_mut());
@@ -1360,16 +1364,22 @@ pub fn rename(old: &Path, new: &Path) -> io::Result<()> {
13601364
.copy_to_nonoverlapping((&raw mut (*file_rename_info).FileName) as *mut u16, new.len());
13611365
}
13621366

1367+
#[cfg(not(target_vendor = "win7"))]
1368+
const FileInformationClass: c::FILE_INFO_BY_HANDLE_CLASS = c::FileRenameInfoEx;
1369+
#[cfg(target_vendor = "win7")]
1370+
const FileInformationClass: c::FILE_INFO_BY_HANDLE_CLASS = c::FileRenameInfo;
1371+
13631372
// We don't use `set_file_information_by_handle` here as `FILE_RENAME_INFO` is used for both `FileRenameInfo` and `FileRenameInfoEx`.
13641373
let result = unsafe {
13651374
cvt(c::SetFileInformationByHandle(
13661375
handle.as_raw_handle(),
1367-
c::FileRenameInfoEx,
1376+
FileInformationClass,
13681377
(&raw const *file_rename_info).cast::<c_void>(),
13691378
struct_size,
13701379
))
13711380
};
13721381

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

1401+
#[cfg(target_vendor = "win7")]
1402+
result?;
1403+
13911404
Ok(())
13921405
}
13931406

0 commit comments

Comments
 (0)