mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-08-01 21:47:46 +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};
|
use clap::{crate_version, Arg, ArgAction, Command};
|
||||||
#[cfg(any(target_os = "linux", target_os = "android"))]
|
#[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};
|
use nix::fcntl::{open, OFlag};
|
||||||
#[cfg(any(target_os = "linux", target_os = "android"))]
|
#[cfg(any(target_os = "linux", target_os = "android"))]
|
||||||
use nix::sys::stat::Mode;
|
use nix::sys::stat::Mode;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use uucore::display::Quotable;
|
use uucore::display::Quotable;
|
||||||
use uucore::error::{UResult, USimpleError};
|
use uucore::error::{FromIo, UResult, USimpleError};
|
||||||
use uucore::format_usage;
|
use uucore::format_usage;
|
||||||
|
|
||||||
static ABOUT: &str = "Synchronize cached writes to persistent storage";
|
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
|
// Use the Nix open to be able to set the NONBLOCK flags for fifo files
|
||||||
#[cfg(any(target_os = "linux", target_os = "android"))]
|
#[cfg(any(target_os = "linux", target_os = "android"))]
|
||||||
{
|
{
|
||||||
match open(Path::new(&f), OFlag::O_NONBLOCK, Mode::empty()) {
|
open(Path::new(&f), OFlag::O_NONBLOCK, Mode::empty())
|
||||||
Ok(_) => {}
|
.map_err_context(|| format!("cannot stat {}", f.quote()))?;
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#[cfg(not(any(target_os = "linux", target_os = "android")))]
|
#[cfg(not(any(target_os = "linux", target_os = "android")))]
|
||||||
{
|
{
|
||||||
|
|
|
@ -38,7 +38,7 @@ os_display = "0.1.3"
|
||||||
|
|
||||||
[target.'cfg(unix)'.dependencies]
|
[target.'cfg(unix)'.dependencies]
|
||||||
walkdir = { version="2.3.2", optional=true }
|
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]
|
[dev-dependencies]
|
||||||
clap = "4.0"
|
clap = "4.0"
|
||||||
|
@ -53,8 +53,8 @@ default = []
|
||||||
# * non-default features
|
# * non-default features
|
||||||
encoding = ["data-encoding", "data-encoding-macro", "z85", "thiserror"]
|
encoding = ["data-encoding", "data-encoding-macro", "z85", "thiserror"]
|
||||||
entries = ["libc"]
|
entries = ["libc"]
|
||||||
fs = ["libc", "nix", "winapi-util"]
|
fs = ["libc", "winapi-util"]
|
||||||
fsext = ["libc", "nix", "time"]
|
fsext = ["libc", "time"]
|
||||||
lines = []
|
lines = []
|
||||||
memo = ["itertools"]
|
memo = ["itertools"]
|
||||||
mode = ["libc"]
|
mode = ["libc"]
|
||||||
|
@ -65,4 +65,4 @@ signals = []
|
||||||
utf8 = []
|
utf8 = []
|
||||||
utmpx = ["time", "time/macros", "libc", "dns-lookup"]
|
utmpx = ["time", "time/macros", "libc", "dns-lookup"]
|
||||||
wide = []
|
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.
|
/// Shorthand to construct [`UIoError`]-instances.
|
||||||
///
|
///
|
||||||
/// This macro serves as a convenience call to quickly construct instances of
|
/// This macro serves as a convenience call to quickly construct instances of
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue