diff --git a/src/backend/libc/fs/syscalls.rs b/src/backend/libc/fs/syscalls.rs index 5b8962f98..717650de3 100644 --- a/src/backend/libc/fs/syscalls.rs +++ b/src/backend/libc/fs/syscalls.rs @@ -33,7 +33,7 @@ use crate::fs::FallocateFlags; use crate::fs::FlockOperation; #[cfg(any(linux_kernel, target_os = "freebsd"))] use crate::fs::MemfdFlags; -#[cfg(any(linux_kernel, apple, target_os = "redox"))] +#[cfg(any(linux_kernel, apple, target_os = "freebsd", target_os = "redox"))] use crate::fs::RenameFlags; #[cfg(any(linux_kernel, target_os = "freebsd", target_os = "fuchsia"))] use crate::fs::SealFlags; @@ -619,6 +619,41 @@ pub(crate) fn renameat2( } } +#[cfg(target_os = "freebsd")] +pub(crate) fn renameat2( + old_dirfd: BorrowedFd<'_>, + old_path: &CStr, + new_dirfd: BorrowedFd<'_>, + new_path: &CStr, + flags: RenameFlags, +) -> io::Result<()> { + // `renameat2` wasn't supported in FreeBSD until 14.3 / 15.1. If no flags + // are passed, use `renameat`, which has been available since FreeBSD 8.0. + if flags.is_empty() { + return renameat(old_dirfd, old_path, new_dirfd, new_path); + } + + weakcall! { + fn renameat2( + olddirfd: c::c_int, + oldpath: *const ffi::c_char, + newdirfd: c::c_int, + newpath: *const ffi::c_char, + flags: c::c_uint + ) -> c::c_int + } + + unsafe { + ret(renameat2( + borrowed_fd(old_dirfd), + c_str(old_path), + borrowed_fd(new_dirfd), + c_str(new_path), + flags.bits(), + )) + } +} + pub(crate) fn symlink(old_path: &CStr, new_path: &CStr) -> io::Result<()> { unsafe { ret(c::symlink(c_str(old_path), c_str(new_path))) } } diff --git a/src/backend/libc/fs/types.rs b/src/backend/libc/fs/types.rs index 70c68d077..1be0381ad 100644 --- a/src/backend/libc/fs/types.rs +++ b/src/backend/libc/fs/types.rs @@ -554,6 +554,25 @@ bitflags! { } } +#[cfg(target_os = "freebsd")] +bitflags! { + /// `RENAME_*` constants for use with [`renameat_with`]. + /// + /// [`renameat_with`]: crate::fs::renameat_with + #[repr(transparent)] + #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] + pub struct RenameFlags: ffi::c_uint { + /// `RENAME_EXCHANGE` + const EXCHANGE = bitcast!(c::RENAME_EXCHANGE); + + /// `RENAME_NOREPLACE` + const NOREPLACE = bitcast!(c::RENAME_NOREPLACE); + + /// + const _ = !0; + } +} + /// `S_IF*` constants for use with [`mknodat`] and [`Stat`]'s `st_mode` field. /// /// [`mknodat`]: crate::fs::mknodat diff --git a/src/fs/at.rs b/src/fs/at.rs index a71bb2752..8b57838c1 100644 --- a/src/fs/at.rs +++ b/src/fs/at.rs @@ -16,7 +16,7 @@ use crate::fs::Access; use crate::fs::AtFlags; #[cfg(apple)] use crate::fs::CloneFlags; -#[cfg(any(linux_kernel, apple, target_os = "redox"))] +#[cfg(any(linux_kernel, apple, target_os = "freebsd", target_os = "redox"))] use crate::fs::RenameFlags; #[cfg(not(target_os = "espidf"))] use crate::fs::Stat; @@ -279,9 +279,13 @@ pub fn renameat( /// /// # References /// - [Linux] +/// - [Apple] +/// - [FreeBSD] /// /// [Linux]: https://man7.org/linux/man-pages/man2/renameat2.2.html -#[cfg(any(apple, linux_kernel, target_os = "redox"))] +/// [Apple]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/rename.2.html +/// [FreeBSD]: https://man.freebsd.org/cgi/man.cgi?query=renameat2&sektion=2 +#[cfg(any(apple, linux_kernel, target_os = "freebsd", target_os = "redox"))] #[inline] #[doc(alias = "renameat2")] #[doc(alias = "renameatx_np")] diff --git a/tests/fs/renameat.rs b/tests/fs/renameat.rs index 19510cf99..7da3f04a9 100644 --- a/tests/fs/renameat.rs +++ b/tests/fs/renameat.rs @@ -4,7 +4,7 @@ fn same(a: &Stat, b: &Stat) -> bool { a.st_ino == b.st_ino && a.st_dev == b.st_dev } -#[cfg(any(linux_kernel, target_os = "redox"))] +#[cfg(any(linux_kernel, target_os = "freebsd", target_os = "redox"))] const DIR_OPEN_FLAGS: OFlags = OFlags::RDONLY.union(OFlags::PATH); #[cfg(apple)] const DIR_OPEN_FLAGS: OFlags = OFlags::RDONLY; @@ -34,7 +34,7 @@ fn test_rename() { access(tmp.path().join("bar"), Access::EXISTS).unwrap(); } -#[cfg(any(linux_kernel, apple, target_os = "redox"))] +#[cfg(any(linux_kernel, apple, target_os = "freebsd", target_os = "redox"))] #[test] fn test_renameat() { use rustix::fs::{accessat, openat, renameat, statat, Access, AtFlags, Mode, CWD}; @@ -58,7 +58,7 @@ fn test_renameat() { /// Like `test_renameat` but the file already exists, so `renameat` /// overwrites it. -#[cfg(any(linux_kernel, apple, target_os = "redox"))] +#[cfg(any(linux_kernel, apple, target_os = "freebsd", target_os = "redox"))] #[test] fn test_renameat_overwrite() { use rustix::fs::{openat, renameat, statat, AtFlags, Mode, CWD}; @@ -74,7 +74,7 @@ fn test_renameat_overwrite() { assert!(same(&before, &renamed)); } -#[cfg(any(linux_kernel, apple, target_os = "redox"))] +#[cfg(any(linux_kernel, apple, target_os = "freebsd", target_os = "redox"))] #[test] fn test_renameat_with() { use rustix::fs::{openat, renameat_with, statat, AtFlags, Mode, RenameFlags, CWD}; @@ -102,20 +102,43 @@ fn test_renameat_with() { ) .unwrap(); - #[cfg(any(apple, all(target_os = "linux", target_env = "gnu")))] + #[cfg(any( + apple, + target_os = "freebsd", + all(target_os = "linux", target_env = "gnu") + ))] { let green = statat(&dir, "green", AtFlags::empty()).unwrap(); - renameat_with(&dir, "red", &dir, "green", RenameFlags::NOREPLACE).unwrap_err(); + match renameat_with(&dir, "red", &dir, "green", RenameFlags::NOREPLACE) { + Err(rustix::io::Errno::EXIST) => (), + #[cfg(target_os = "freebsd")] + Err(rustix::io::Errno::NOSYS | rustix::io::Errno::OPNOTSUPP) => return, + other => panic!("unexpected result from NOREPLACE collision: {:?}", other), + } let renamed = statat(&dir, "red", AtFlags::empty()).unwrap(); assert!(same(&before, &renamed)); let orig = statat(&dir, "green", AtFlags::empty()).unwrap(); assert!(same(&green, &orig)); - renameat_with(&dir, "red", &dir, "green", RenameFlags::EXCHANGE).unwrap(); - let renamed = statat(&dir, "red", AtFlags::empty()).unwrap(); - assert!(same(&green, &renamed)); - let orig = statat(&dir, "green", AtFlags::empty()).unwrap(); - assert!(same(&before, &orig)); + renameat_with(&dir, "red", &dir, "blue", RenameFlags::NOREPLACE).unwrap(); + let blue = statat(&dir, "blue", AtFlags::empty()).unwrap(); + assert!(same(&before, &blue)); + renameat_with(&dir, "blue", &dir, "red", RenameFlags::NOREPLACE).unwrap(); + + match renameat_with(&dir, "red", &dir, "green", RenameFlags::EXCHANGE) { + Ok(()) => { + let renamed = statat(&dir, "red", AtFlags::empty()).unwrap(); + assert!(same(&green, &renamed)); + let orig = statat(&dir, "green", AtFlags::empty()).unwrap(); + assert!(same(&before, &orig)); + } + // FreeBSD added `AT_RENAME_EXCHANGE` after `AT_RENAME_NOREPLACE`. + #[cfg(target_os = "freebsd")] + Err( + rustix::io::Errno::NOSYS | rustix::io::Errno::INVAL | rustix::io::Errno::OPNOTSUPP, + ) => (), + Err(err) => panic!("unexpected error from EXCHANGE: {:?}", err), + } } }