Skip to content

Commit

Permalink
Add fallback implementation for MoveFileExW
Browse files Browse the repository at this point in the history
Falls back to calls to `CopyFileW` and `DeleteFile`.
  • Loading branch information
seritools authored and mbilker committed Sep 16, 2023
1 parent ea62d85 commit ec2e522
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions library/std/src/sys/windows/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()> {
Expand Down

0 comments on commit ec2e522

Please sign in to comment.