From cacb1a4fcbb4feb3291a83fed7487c7f48ddc52f Mon Sep 17 00:00:00 2001 From: Tuomas Tynkkynen Date: Wed, 12 Mar 2025 22:20:16 +0200 Subject: [PATCH] cp: Use FICLONE ioctl constant from linux-raw-sys The current ioctl operation code for FICLONE is fully open-coded instead of using the ioctl macros, which makes it non-portable to other architectures including mips, arm & powerpc. Get the constant from the linux-raw-sys crate instead, which is already a transitive dependency. --- Cargo.lock | 1 + Cargo.toml | 1 + src/uu/cp/Cargo.toml | 1 + src/uu/cp/src/platform/linux.rs | 19 +++++++++---------- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5aa535387..f1488bfc2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2617,6 +2617,7 @@ dependencies = [ "filetime", "indicatif", "libc", + "linux-raw-sys 0.9.2", "quick-error", "selinux", "uucore", diff --git a/Cargo.toml b/Cargo.toml index ff84cb43e..b9c2c8f70 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -303,6 +303,7 @@ iana-time-zone = "0.1.57" indicatif = "0.17.8" itertools = "0.14.0" libc = "0.2.153" +linux-raw-sys = "0.9" lscolors = { version = "0.20.0", default-features = false, features = [ "gnu_legacy", ] } diff --git a/src/uu/cp/Cargo.toml b/src/uu/cp/Cargo.toml index 78780aa6d..8322735a3 100644 --- a/src/uu/cp/Cargo.toml +++ b/src/uu/cp/Cargo.toml @@ -24,6 +24,7 @@ path = "src/cp.rs" clap = { workspace = true } filetime = { workspace = true } libc = { workspace = true } +linux-raw-sys = { workspace = true } quick-error = { workspace = true } selinux = { workspace = true, optional = true } uucore = { workspace = true, features = [ diff --git a/src/uu/cp/src/platform/linux.rs b/src/uu/cp/src/platform/linux.rs index 0ca39a75e..4da76522f 100644 --- a/src/uu/cp/src/platform/linux.rs +++ b/src/uu/cp/src/platform/linux.rs @@ -20,15 +20,6 @@ use uucore::mode::get_umask; use crate::{CopyDebug, CopyResult, OffloadReflinkDebug, ReflinkMode, SparseDebug, SparseMode}; -// From /usr/include/linux/fs.h: -// #define FICLONE _IOW(0x94, 9, int) -// Use a macro as libc::ioctl expects u32 or u64 depending on the arch -macro_rules! FICLONE { - () => { - 0x40049409 - }; -} - /// The fallback behavior for [`clone`] on failed system call. #[derive(Clone, Copy)] enum CloneFallback { @@ -70,7 +61,15 @@ where let dst_file = File::create(&dest)?; let src_fd = src_file.as_raw_fd(); let dst_fd = dst_file.as_raw_fd(); - let result = unsafe { libc::ioctl(dst_fd, FICLONE!(), src_fd) }; + // Using .try_into().unwrap() is required as glibc, musl & android all have different type for ioctl() + #[allow(clippy::unnecessary_fallible_conversions)] + let result = unsafe { + libc::ioctl( + dst_fd, + linux_raw_sys::ioctl::FICLONE.try_into().unwrap(), + src_fd, + ) + }; if result == 0 { return Ok(()); }