mirror of
https://github.com/uutils/coreutils
synced 2024-12-14 15:22:38 +00:00
refactor: add automatic conversion for nix::Errno
This commit is contained in:
parent
513b764434
commit
50be73d99f
3 changed files with 18 additions and 29 deletions
|
@ -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")))]
|
||||
{
|
||||
|
|
|
@ -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 = []
|
||||
|
|
|
@ -508,6 +508,17 @@ impl From<std::io::Error> for Box<dyn UError> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T> FromIo<UResult<T>> for Result<T, nix::Error> {
|
||||
fn map_err_context(self, context: impl FnOnce() -> String) -> UResult<T> {
|
||||
self.map_err(|e| {
|
||||
Box::new(UIoError {
|
||||
context: Some((context)()),
|
||||
inner: std::io::Error::from_raw_os_error(e as i32),
|
||||
}) as Box<dyn UError>
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Shorthand to construct [`UIoError`]-instances.
|
||||
///
|
||||
/// This macro serves as a convenience call to quickly construct instances of
|
||||
|
|
Loading…
Reference in a new issue