diff --git a/src/uu/sync/src/sync.rs b/src/uu/sync/src/sync.rs index 5db957178..eeff191a4 100644 --- a/src/uu/sync/src/sync.rs +++ b/src/uu/sync/src/sync.rs @@ -11,14 +11,12 @@ extern crate libc; use clap::{crate_version, Arg, ArgAction, Command}; #[cfg(any(target_os = "linux", target_os = "android"))] -use nix::errno::Errno; -#[cfg(any(target_os = "linux", target_os = "android"))] use nix::fcntl::{open, OFlag}; #[cfg(any(target_os = "linux", target_os = "android"))] use nix::sys::stat::Mode; use std::path::Path; use uucore::display::Quotable; -use uucore::error::{UResult, USimpleError}; +use uucore::error::{FromIo, UResult, USimpleError}; use uucore::format_usage; static ABOUT: &str = "Synchronize cached writes to persistent storage"; @@ -183,28 +181,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { // Use the Nix open to be able to set the NONBLOCK flags for fifo files #[cfg(any(target_os = "linux", target_os = "android"))] { - match open(Path::new(&f), OFlag::O_NONBLOCK, Mode::empty()) { - Ok(_) => {} - Err(e) => { - if e == Errno::ENOENT { - return Err(USimpleError::new( - 1, - format!("cannot stat {}: No such file or directory", f.quote()), - )); - } - if e == Errno::EACCES { - if Path::new(&f).is_dir() { - return Err(USimpleError::new( - 1, - format!("error opening {}: Permission denied", f.quote()), - )); - } else { - // ignore the issue - // ./target/debug/coreutils sync --data file - } - } - } - } + open(Path::new(&f), OFlag::O_NONBLOCK, Mode::empty()) + .map_err_context(|| format!("cannot stat {}", f.quote()))?; } #[cfg(not(any(target_os = "linux", target_os = "android")))] { diff --git a/src/uucore/Cargo.toml b/src/uucore/Cargo.toml index af78e3f94..240678b69 100644 --- a/src/uucore/Cargo.toml +++ b/src/uucore/Cargo.toml @@ -38,7 +38,7 @@ os_display = "0.1.3" [target.'cfg(unix)'.dependencies] walkdir = { version="2.3.2", optional=true } -nix = { version = "0.25", optional = true, default-features = false, features = ["fs", "uio", "zerocopy"] } +nix = { version = "0.25", default-features = false, features = ["fs", "uio", "zerocopy"] } [dev-dependencies] clap = "4.0" @@ -53,8 +53,8 @@ default = [] # * non-default features encoding = ["data-encoding", "data-encoding-macro", "z85", "thiserror"] entries = ["libc"] -fs = ["libc", "nix", "winapi-util"] -fsext = ["libc", "nix", "time"] +fs = ["libc", "winapi-util"] +fsext = ["libc", "time"] lines = [] memo = ["itertools"] mode = ["libc"] @@ -65,4 +65,4 @@ signals = [] utf8 = [] utmpx = ["time", "time/macros", "libc", "dns-lookup"] wide = [] -pipes = ["nix"] +pipes = [] diff --git a/src/uucore/src/lib/mods/error.rs b/src/uucore/src/lib/mods/error.rs index 4cc0b1519..7377c021b 100644 --- a/src/uucore/src/lib/mods/error.rs +++ b/src/uucore/src/lib/mods/error.rs @@ -508,6 +508,17 @@ impl From for Box { } } +impl FromIo> for Result { + fn map_err_context(self, context: impl FnOnce() -> String) -> UResult { + self.map_err(|e| { + Box::new(UIoError { + context: Some((context)()), + inner: std::io::Error::from_raw_os_error(e as i32), + }) as Box + }) + } +} + /// Shorthand to construct [`UIoError`]-instances. /// /// This macro serves as a convenience call to quickly construct instances of