From ec2e522a7157e68dadf7176b12d4758becdf1b23 Mon Sep 17 00:00:00 2001 From: Dennis Duda Date: Thu, 14 Apr 2022 22:33:27 +0200 Subject: [PATCH] Add fallback implementation for `MoveFileExW` Falls back to calls to `CopyFileW` and `DeleteFile`. --- library/std/src/sys/windows/fs.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/library/std/src/sys/windows/fs.rs b/library/std/src/sys/windows/fs.rs index ec2a2cde81706..8de5f4fa66d5c 100644 --- a/library/std/src/sys/windows/fs.rs +++ b/library/std/src/sys/windows/fs.rs @@ -1126,8 +1126,22 @@ pub fn unlink(p: &Path) -> io::Result<()> { pub fn rename(old: &Path, new: &Path) -> io::Result<()> { let old = maybe_verbatim(old)?; let new = maybe_verbatim(new)?; - cvt(unsafe { c::MoveFileExW(old.as_ptr(), new.as_ptr(), c::MOVEFILE_REPLACE_EXISTING) })?; - Ok(()) + let res = + cvt(unsafe { c::MoveFileExW(old.as_ptr(), new.as_ptr(), c::MOVEFILE_REPLACE_EXISTING) }); + + match res { + Err(ref e) if e.raw_os_error() == Some(c::ERROR_CALL_NOT_IMPLEMENTED as i32) => { + // 9x/ME doesn't support MoveFileEx, so we fall back to copy + delete and hope for the + // best + unsafe { + cvt(c::CopyFileW(old.as_ptr(), new.as_ptr(), c::TRUE))?; + cvt(c::DeleteFileW(old.as_ptr()))?; + Ok(()) + } + } + Err(e) => Err(e), + Ok(_) => Ok(()), + } } pub fn rmdir(p: &Path) -> io::Result<()> {