refactor: add automatic conversion for nix::Errno

This commit is contained in:
Orhun Parmaksız 2022-10-19 00:13:59 +03:00
parent 513b764434
commit 50be73d99f
No known key found for this signature in database
GPG key ID: F83424824B3E4B90
3 changed files with 18 additions and 29 deletions

View file

@ -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")))]
{

View file

@ -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 = []

View file

@ -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