|
4 | 4 | //! these syscalls, but they can be emulated with a mix of the Windows API and the Rust standard |
5 | 5 | //! library, the latter of which calls the former. |
6 | 6 |
|
| 7 | +use core::hint::cold_path; |
7 | 8 | use std::{fs, io, path::Path}; |
8 | 9 |
|
| 10 | +use widestring::WideCString; |
| 11 | +use windows_sys::Win32::{ |
| 12 | + Foundation::FALSE, |
| 13 | + Storage::FileSystem::{MOVE_FILE_FLAGS, MOVEFILE_REPLACE_EXISTING, MoveFileExW}, |
| 14 | +}; |
| 15 | + |
9 | 16 | use crate::crt_fd; |
10 | 17 |
|
11 | | -#[expect(non_camel_case_types)] |
12 | | -pub type mode_t = u32; |
| 18 | +pub type RawMode = u32; |
13 | 19 |
|
14 | 20 | pub fn make_dir( |
15 | 21 | dir_fd: Option<crt_fd::Borrowed<'_>>, |
16 | | - path: &impl AsRef<Path>, |
17 | | - _mode: mode_t, |
| 22 | + path: impl AsRef<Path>, |
| 23 | + _mode: RawMode, |
18 | 24 | ) -> io::Result<()> { |
19 | 25 | debug_assert!(dir_fd.is_none()); |
20 | 26 | // TODO: On Windows, Python has an override if the mode is 0o700 |
21 | 27 | fs::create_dir(path) |
22 | 28 | } |
| 29 | + |
| 30 | +/// https://pubs.opengroup.org/onlinepubs/9799919799/functions/rename.html |
| 31 | +#[inline] |
| 32 | +pub fn rename( |
| 33 | + from: impl AsRef<Path>, |
| 34 | + #[cfg_attr(not(debug_assertions), expect(unused_variables))] from_fd: Option< |
| 35 | + crt_fd::Borrowed<'_>, |
| 36 | + >, |
| 37 | + to: impl AsRef<Path>, |
| 38 | + #[cfg_attr(not(debug_assertions), expect(unused_variables))] to_fd: Option< |
| 39 | + crt_fd::Borrowed<'_>, |
| 40 | + >, |
| 41 | +) -> io::Result<()> { |
| 42 | + debug_assert!(from_fd.is_none()); |
| 43 | + debug_assert!(to_fd.is_none()); |
| 44 | + |
| 45 | + rename_impl(from, to, 0) |
| 46 | +} |
| 47 | + |
| 48 | +/// https://docs.python.org/3/library/os.html#os.replace |
| 49 | +/// |
| 50 | +/// Atomically replace `to` with `from`. |
| 51 | +#[inline] |
| 52 | +pub fn replace( |
| 53 | + from: impl AsRef<Path>, |
| 54 | + #[cfg_attr(not(debug_assertions), expect(unused_variables))] from_fd: Option< |
| 55 | + crt_fd::Borrowed<'_>, |
| 56 | + >, |
| 57 | + to: impl AsRef<Path>, |
| 58 | + #[cfg_attr(not(debug_assertions), expect(unused_variables))] to_fd: Option< |
| 59 | + crt_fd::Borrowed<'_>, |
| 60 | + >, |
| 61 | +) -> io::Result<()> { |
| 62 | + debug_assert!(from_fd.is_none()); |
| 63 | + debug_assert!(to_fd.is_none()); |
| 64 | + |
| 65 | + rename_impl(from, to, MOVEFILE_REPLACE_EXISTING) |
| 66 | +} |
| 67 | + |
| 68 | +fn rename_impl( |
| 69 | + from: impl AsRef<Path>, |
| 70 | + to: impl AsRef<Path>, |
| 71 | + flags: MOVE_FILE_FLAGS, |
| 72 | +) -> io::Result<()> { |
| 73 | + let from = WideCString::from_os_str(from.as_ref()) |
| 74 | + .map_err(io::Error::other)? |
| 75 | + .into_vec_with_nul(); |
| 76 | + let to = WideCString::from_os_str(to.as_ref()) |
| 77 | + .map_err(io::Error::other)? |
| 78 | + .into_vec_with_nul(); |
| 79 | + |
| 80 | + // SAFETY: |
| 81 | + // * from and to are NUL terminated wide strings |
| 82 | + let success = unsafe { |
| 83 | + // Rust's [`std::fs::rename`] is more complicated than CPython's. Rust attempts to use modern APIs |
| 84 | + // where available, such as `FileRenameInfoEx`, which better map to POSIX. CPython simply |
| 85 | + // calls MoveFileExW so we'll do that for parity. However, it may be better to use the new |
| 86 | + // APIs and fall back if possible, especially if they're faster. |
| 87 | + // |
| 88 | + // Unlike POSIX's rename, MoveFileExW does not automatically move between volumes. |
| 89 | + // This is expected behavior in CPython. |
| 90 | + MoveFileExW(from.as_ptr(), to.as_ptr(), flags) |
| 91 | + }; |
| 92 | + |
| 93 | + if success != FALSE { |
| 94 | + Ok(()) |
| 95 | + } else { |
| 96 | + cold_path(); |
| 97 | + Err(io::Error::last_os_error()) |
| 98 | + } |
| 99 | +} |
0 commit comments