Skip to content

Commit 44fd03b

Browse files
committed
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.
1 parent 429a223 commit 44fd03b

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ iana-time-zone = "0.1.57"
303303
indicatif = "0.17.8"
304304
itertools = "0.14.0"
305305
libc = "0.2.153"
306+
linux-raw-sys = "0.9"
306307
lscolors = { version = "0.20.0", default-features = false, features = [
307308
"gnu_legacy",
308309
] }

src/uu/cp/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ path = "src/cp.rs"
2424
clap = { workspace = true }
2525
filetime = { workspace = true }
2626
libc = { workspace = true }
27+
linux-raw-sys = { workspace = true }
2728
quick-error = { workspace = true }
2829
selinux = { workspace = true, optional = true }
2930
uucore = { workspace = true, features = [

src/uu/cp/src/platform/linux.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,6 @@ use uucore::mode::get_umask;
2020

2121
use crate::{CopyDebug, CopyResult, OffloadReflinkDebug, ReflinkMode, SparseDebug, SparseMode};
2222

23-
// From /usr/include/linux/fs.h:
24-
// #define FICLONE _IOW(0x94, 9, int)
25-
// Use a macro as libc::ioctl expects u32 or u64 depending on the arch
26-
macro_rules! FICLONE {
27-
() => {
28-
0x40049409
29-
};
30-
}
31-
3223
/// The fallback behavior for [`clone`] on failed system call.
3324
#[derive(Clone, Copy)]
3425
enum CloneFallback {
@@ -70,7 +61,15 @@ where
7061
let dst_file = File::create(&dest)?;
7162
let src_fd = src_file.as_raw_fd();
7263
let dst_fd = dst_file.as_raw_fd();
73-
let result = unsafe { libc::ioctl(dst_fd, FICLONE!(), src_fd) };
64+
// Using .try_into().unwrap() is required as glibc, musl & android all have different type for ioctl()
65+
#[allow(clippy::unnecessary_fallible_conversions)]
66+
let result = unsafe {
67+
libc::ioctl(
68+
dst_fd,
69+
linux_raw_sys::ioctl::FICLONE.try_into().unwrap(),
70+
src_fd,
71+
)
72+
};
7473
if result == 0 {
7574
return Ok(());
7675
}

0 commit comments

Comments
 (0)