From 2fcf892dbc225b61f9dd8cdfae8d7a72d2d6f6c1 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Fri, 10 Jun 2022 21:14:15 +0200 Subject: [PATCH] cp: Replace ioctl-sys by libc for the call to ficlone --- Cargo.lock | 7 ------- src/uu/cp/Cargo.toml | 3 --- src/uu/cp/src/cp.rs | 22 ++++++++++++++-------- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c0b50f82d..9c2134329 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -995,12 +995,6 @@ dependencies = [ "cfg-if 1.0.0", ] -[[package]] -name = "ioctl-sys" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8bd11f3a29434026f5ff98c730b668ba74b1033637b8817940b54d040696133c" - [[package]] name = "itertools" version = "0.10.3" @@ -2229,7 +2223,6 @@ dependencies = [ "clap 3.1.18", "exacl", "filetime", - "ioctl-sys", "libc", "quick-error", "selinux", diff --git a/src/uu/cp/Cargo.toml b/src/uu/cp/Cargo.toml index 43a8c35da..3342160a4 100644 --- a/src/uu/cp/Cargo.toml +++ b/src/uu/cp/Cargo.toml @@ -27,9 +27,6 @@ selinux = { version="0.2", optional=true } uucore = { version=">=0.0.11", package="uucore", path="../../uucore", features=["entries", "fs", "perms", "mode"] } walkdir = "2.2" -[target.'cfg(any(target_os = "linux", target_os = "android"))'.dependencies] -ioctl-sys = "0.8" - [target.'cfg(target_os = "windows")'.dependencies] winapi = { version="0.3", features=["fileapi"] } diff --git a/src/uu/cp/src/cp.rs b/src/uu/cp/src/cp.rs index 41466abc8..669a38601 100644 --- a/src/uu/cp/src/cp.rs +++ b/src/uu/cp/src/cp.rs @@ -10,9 +10,6 @@ // spell-checker:ignore (ToDO) ficlone linkgs lstat nlink nlinks pathbuf reflink strs xattrs symlinked -#[cfg(any(target_os = "linux", target_os = "android"))] -#[macro_use] -extern crate ioctl_sys; #[macro_use] extern crate quick_error; #[macro_use] @@ -61,9 +58,6 @@ use uucore::error::{set_exit_code, ExitCode, UClapError, UError, UResult}; use uucore::fs::{canonicalize, MissingHandling, ResolveMode}; use walkdir::WalkDir; -#[cfg(any(target_os = "linux", target_os = "android"))] -ioctl!(write ficlone with 0x94, 9; std::os::raw::c_int); - quick_error! { #[derive(Debug)] pub enum Error { @@ -230,6 +224,16 @@ pub struct Options { verbose: bool, } +// From /usr/include/linux/fs.h: +// #define FICLONE _IOW(0x94, 9, int) +#[cfg(any(target_os = "linux", target_os = "android"))] +// Use a macro as libc::ioctl expects u32 or u64 depending on the arch +macro_rules! FICLONE { + () => { + 0x40049409 + }; +} + static ABOUT: &str = "Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY."; static LONG_HELP: &str = ""; static EXIT_ERR: i32 = 1; @@ -1567,7 +1571,8 @@ fn copy_on_write_linux( .context(context)?; match mode { ReflinkMode::Always => unsafe { - let result = ficlone(dst_file.as_raw_fd(), src_file.as_raw_fd() as *const i32); + let result = libc::ioctl(dst_file.as_raw_fd(), FICLONE!(), src_file.as_raw_fd()); + if result != 0 { Err(format!( "failed to clone {:?} from {:?}: {}", @@ -1581,7 +1586,8 @@ fn copy_on_write_linux( } }, ReflinkMode::Auto => unsafe { - let result = ficlone(dst_file.as_raw_fd(), src_file.as_raw_fd() as *const i32); + let result = libc::ioctl(dst_file.as_raw_fd(), FICLONE!(), src_file.as_raw_fd()); + if result != 0 { fs::copy(source, dest).context(context)?; }