Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/2657.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Changed `renameat2` to use raw syscalls instead of libc functions, enabling support for musl libc and other Linux environments beyond GNU libc.
21 changes: 13 additions & 8 deletions src/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,22 +444,24 @@ pub fn renameat<P1: ?Sized + NixPath, P2: ?Sized + NixPath, Fd1: std::os::fd::As
}
}

#[cfg(all(target_os = "linux", target_env = "gnu"))]
#[cfg(target_os = "linux")]
#[cfg(feature = "fs")]
libc_bitflags! {
bitflags::bitflags! {
/// Flags for use with [`renameat2`].
#[cfg_attr(docsrs, doc(cfg(feature = "fs")))]
#[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[repr(transparent)]
pub struct RenameFlags: u32 {
/// Atomically exchange `old_path` and `new_path`.
RENAME_EXCHANGE;
const RENAME_EXCHANGE = 1 << 1;
/// Don't overwrite `new_path` of the rename. Return an error if `new_path` already
/// exists.
RENAME_NOREPLACE;
const RENAME_NOREPLACE = 1 << 0;
/// creates a "whiteout" object at the source of the rename at the same time as performing
/// the rename.
///
/// This operation makes sense only for overlay/union filesystem implementations.
RENAME_WHITEOUT;
const RENAME_WHITEOUT = 1 << 2;
}
}

Expand All @@ -471,7 +473,7 @@ feature! {
///
/// # See Also
/// * [`rename`](https://man7.org/linux/man-pages/man2/rename.2.html)
#[cfg(all(target_os = "linux", target_env = "gnu"))]
#[cfg(target_os = "linux")]
pub fn renameat2<P1: ?Sized + NixPath, P2: ?Sized + NixPath, Fd1: std::os::fd::AsFd, Fd2: std::os::fd::AsFd>(
old_dirfd: Fd1,
old_path: &P1,
Expand All @@ -483,7 +485,10 @@ pub fn renameat2<P1: ?Sized + NixPath, P2: ?Sized + NixPath, Fd1: std::os::fd::A

let res = old_path.with_nix_path(|old_cstr| {
new_path.with_nix_path(|new_cstr| unsafe {
libc::renameat2(
// Use raw syscall instead of libc::renameat2 to support musl libc and other
// environments where the libc function may not be available.
libc::syscall(
libc::SYS_renameat2,
old_dirfd.as_fd().as_raw_fd(),
old_cstr.as_ptr(),
new_dirfd.as_fd().as_raw_fd(),
Expand All @@ -492,7 +497,7 @@ pub fn renameat2<P1: ?Sized + NixPath, P2: ?Sized + NixPath, Fd1: std::os::fd::A
)
})
})??;
Errno::result(res).map(drop)
Errno::result(res as c_int).map(drop)
}

fn wrap_readlink_result(mut v: Vec<u8>, len: ssize_t) -> Result<OsString> {
Expand Down
40 changes: 4 additions & 36 deletions test/test_fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,7 @@ use nix::fcntl::{openat, readlinkat, renameat};
#[cfg(target_os = "linux")]
use nix::fcntl::{openat2, OpenHow, ResolveFlag};

#[cfg(all(
target_os = "linux",
target_env = "gnu",
any(
target_arch = "x86_64",
target_arch = "powerpc",
target_arch = "s390x"
)
))]
#[cfg(target_os = "linux")]
use nix::fcntl::{renameat2, RenameFlags};
#[cfg(not(target_os = "redox"))]
use nix::sys::stat::Mode;
Expand Down Expand Up @@ -132,15 +124,7 @@ fn test_renameat() {
}

#[test]
#[cfg(all(
target_os = "linux",
target_env = "gnu",
any(
target_arch = "x86_64",
target_arch = "powerpc",
target_arch = "s390x"
)
))]
#[cfg(target_os = "linux")]
fn test_renameat2_behaves_like_renameat_with_no_flags() {
let old_dir = tempfile::tempdir().unwrap();
let old_dirfd =
Expand All @@ -161,15 +145,7 @@ fn test_renameat2_behaves_like_renameat_with_no_flags() {
}

#[test]
#[cfg(all(
target_os = "linux",
target_env = "gnu",
any(
target_arch = "x86_64",
target_arch = "powerpc",
target_arch = "s390x"
)
))]
#[cfg(target_os = "linux")]
fn test_renameat2_exchange() {
let old_dir = tempfile::tempdir().unwrap();
let old_dirfd =
Expand Down Expand Up @@ -206,15 +182,7 @@ fn test_renameat2_exchange() {
}

#[test]
#[cfg(all(
target_os = "linux",
target_env = "gnu",
any(
target_arch = "x86_64",
target_arch = "powerpc",
target_arch = "s390x"
)
))]
#[cfg(target_os = "linux")]
fn test_renameat2_noreplace() {
let old_dir = tempfile::tempdir().unwrap();
let old_dirfd =
Expand Down
Loading