diff options
author | kf zheng <100595273+kev1n8@users.noreply.github.com> | 2024-07-07 20:20:08 +0800 |
---|---|---|
committer | kf zheng <100595273+kev1n8@users.noreply.github.com> | 2024-07-07 20:56:22 +0800 |
commit | 4faf9c08d69d509a273ddac6b38a03ca0d98f080 (patch) | |
tree | 2aa03b57989d9ac636c1eb2784cb9cd258c418b7 | |
parent | fb48e7d280f277c04d305e9d116c1830b329e85a (diff) |
replace crash! with UResult in fsext and modify related files: df, stat, and filesystem
-rw-r--r-- | src/uu/df/src/df.rs | 17 | ||||
-rw-r--r-- | src/uu/df/src/filesystem.rs | 2 | ||||
-rw-r--r-- | src/uu/stat/src/stat.rs | 11 | ||||
-rw-r--r-- | src/uucore/src/lib/features/fsext.rs | 36 |
4 files changed, 42 insertions, 24 deletions
diff --git a/src/uu/df/src/df.rs b/src/uu/df/src/df.rs index c21ba9847..ee3615c9d 100644 --- a/src/uu/df/src/df.rs +++ b/src/uu/df/src/df.rs @@ -12,7 +12,6 @@ use blocks::HumanReadable; use clap::builder::ValueParser; use table::HeaderMode; use uucore::display::Quotable; -use uucore::error::FromIo; use uucore::error::{UError, UResult, USimpleError}; use uucore::fsext::{read_fs_list, MountInfo}; use uucore::parse_size::ParseSizeError; @@ -333,7 +332,7 @@ fn filter_mount_list(vmi: Vec<MountInfo>, opt: &Options) -> Vec<MountInfo> { /// `opt` excludes certain filesystems from consideration and allows for the synchronization of filesystems before running; see /// [`Options`] for more information. -fn get_all_filesystems(opt: &Options) -> Result<Vec<Filesystem>, std::io::Error> { +fn get_all_filesystems(opt: &Options) -> UResult<Vec<Filesystem>> { // Run a sync call before any operation if so instructed. if opt.sync { #[cfg(not(any(windows, target_os = "redox")))] @@ -361,7 +360,7 @@ fn get_all_filesystems(opt: &Options) -> Result<Vec<Filesystem>, std::io::Error> } /// For each path, get the filesystem that contains that path. -fn get_named_filesystems<P>(paths: &[P], opt: &Options) -> Result<Vec<Filesystem>, std::io::Error> +fn get_named_filesystems<P>(paths: &[P], opt: &Options) -> UResult<Vec<Filesystem>> where P: AsRef<Path>, { @@ -443,8 +442,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { // Get the list of filesystems to display in the output table. let filesystems: Vec<Filesystem> = match matches.get_many::<String>(OPT_PATHS) { None => { - let filesystems = get_all_filesystems(&opt) - .map_err_context(|| "cannot read table of mounted file systems".into())?; + let filesystems = get_all_filesystems(&opt).map_err(|e| { + let context = "cannot read table of mounted file systems"; + USimpleError::new(e.code(), format!("{}: {}", context, e)) + })?; if filesystems.is_empty() { return Err(USimpleError::new(1, "no file systems processed")); @@ -454,8 +455,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { } Some(paths) => { let paths: Vec<_> = paths.collect(); - let filesystems = get_named_filesystems(&paths, &opt) - .map_err_context(|| "cannot read table of mounted file systems".into())?; + let filesystems = get_named_filesystems(&paths, &opt).map_err(|e| { + let context = "cannot read table of mounted file systems"; + USimpleError::new(e.code(), format!("{}: {}", context, e)) + })?; // This can happen if paths are given as command-line arguments // but none of the paths exist. diff --git a/src/uu/df/src/filesystem.rs b/src/uu/df/src/filesystem.rs index 821c99ab4..8cc639d68 100644 --- a/src/uu/df/src/filesystem.rs +++ b/src/uu/df/src/filesystem.rs @@ -109,7 +109,7 @@ impl Filesystem { #[cfg(unix)] let usage = FsUsage::new(statfs(_stat_path).ok()?); #[cfg(windows)] - let usage = FsUsage::new(Path::new(&_stat_path)); + let usage = FsUsage::new(Path::new(&_stat_path)).ok()?; Some(Self { mount_info, usage, diff --git a/src/uu/stat/src/stat.rs b/src/uu/stat/src/stat.rs index fe007397d..18b10807c 100644 --- a/src/uu/stat/src/stat.rs +++ b/src/uu/stat/src/stat.rs @@ -4,9 +4,13 @@ // file that was distributed with this source code. // spell-checker:ignore datetime +#[cfg(target_os = "android")] +use uucore::error::UResult; +#[cfg(not(target_os = "android"))] +use uucore::error::{UResult, USimpleError}; + use clap::builder::ValueParser; use uucore::display::Quotable; -use uucore::error::{FromIo, UResult, USimpleError}; use uucore::fs::display_permissions; use uucore::fsext::{pretty_filetype, pretty_fstype, read_fs_list, statfs, BirthTime, FsMeta}; use uucore::libc::mode_t; @@ -583,7 +587,10 @@ impl Stater { None } else { let mut mount_list = read_fs_list() - .map_err_context(|| "cannot read table of mounted file systems".into())? + .map_err(|e| { + let context = "cannot read table of mounted file systems"; + USimpleError::new(e.code(), format!("{}: {}", context, e)) + })? .iter() .map(|mi| mi.mount_dir.clone()) .collect::<Vec<String>>(); diff --git a/src/uucore/src/lib/features/fsext.rs b/src/uucore/src/lib/features/fsext.rs index eaa672472..5a6e4156d 100644 --- a/src/uucore/src/lib/features/fsext.rs +++ b/src/uucore/src/lib/features/fsext.rs @@ -25,7 +25,6 @@ static EXIT_ERR: i32 = 1; target_os = "netbsd", target_os = "openbsd" ))] -use crate::crash; #[cfg(windows)] use crate::show_warning; @@ -376,6 +375,15 @@ extern "C" { fn get_mount_info(mount_buffer_p: *mut *mut StatFs, flags: c_int) -> c_int; } +use crate::error::UResult; +#[cfg(any( + target_os = "freebsd", + target_vendor = "apple", + target_os = "netbsd", + target_os = "openbsd", + target_os = "windows" +))] +use crate::error::USimpleError; #[cfg(any(target_os = "linux", target_os = "android"))] use std::fs::File; #[cfg(any(target_os = "linux", target_os = "android"))] @@ -395,8 +403,9 @@ use std::ptr; target_os = "openbsd" ))] use std::slice; + /// Read file system list. -pub fn read_fs_list() -> Result<Vec<MountInfo>, std::io::Error> { +pub fn read_fs_list() -> UResult<Vec<MountInfo>> { #[cfg(any(target_os = "linux", target_os = "android"))] { let (file_name, f) = File::open(LINUX_MOUNTINFO) @@ -422,7 +431,7 @@ pub fn read_fs_list() -> Result<Vec<MountInfo>, std::io::Error> { let mut mount_buffer_ptr: *mut StatFs = ptr::null_mut(); let len = unsafe { get_mount_info(&mut mount_buffer_ptr, 1_i32) }; if len < 0 { - crash!(1, "get_mount_info() failed"); + return Err(USimpleError::new(1, "get_mount_info() failed")); } let mounts = unsafe { slice::from_raw_parts(mount_buffer_ptr, len as usize) }; Ok(mounts @@ -437,11 +446,9 @@ pub fn read_fs_list() -> Result<Vec<MountInfo>, std::io::Error> { let find_handle = unsafe { FindFirstVolumeW(volume_name_buf.as_mut_ptr(), volume_name_buf.len() as u32) }; if INVALID_HANDLE_VALUE == find_handle { - crash!( - EXIT_ERR, - "FindFirstVolumeW failed: {}", - IOError::last_os_error() - ); + let os_err = IOError::last_os_error(); + let msg = format!("FindFirstVolumeW failed: {}", os_err); + return Err(USimpleError::new(EXIT_ERR, msg)); } let mut mounts = Vec::<MountInfo>::new(); loop { @@ -462,7 +469,8 @@ pub fn read_fs_list() -> Result<Vec<MountInfo>, std::io::Error> { } { let err = IOError::last_os_error(); if err.raw_os_error() != Some(ERROR_NO_MORE_FILES as i32) { - crash!(EXIT_ERR, "FindNextVolumeW failed: {}", err); + let msg = format!("FindNextVolumeW failed: {err}"); + return Err(USimpleError::new(EXIT_ERR, msg)); } break; } @@ -554,7 +562,7 @@ impl FsUsage { } } #[cfg(windows)] - pub fn new(path: &Path) -> Self { + pub fn new(path: &Path) -> UResult<Self> { let mut root_path = [0u16; MAX_PATH]; let success = unsafe { let path = to_nul_terminated_wide_string(path); @@ -567,11 +575,11 @@ impl FsUsage { ) }; if 0 == success { - crash!( - EXIT_ERR, + let msg = format!( "GetVolumePathNamesForVolumeNameW failed: {}", IOError::last_os_error() ); + return Err(USimpleError::new(EXIT_ERR, msg)); } let mut sectors_per_cluster = 0; @@ -599,7 +607,7 @@ impl FsUsage { } let bytes_per_cluster = sectors_per_cluster as u64 * bytes_per_sector as u64; - Self { + Ok(Self { // f_bsize File system block size. blocksize: bytes_per_cluster, // f_blocks - Total number of blocks on the file system, in units of f_frsize. @@ -614,7 +622,7 @@ impl FsUsage { files: 0, // Not available on windows // Total number of free file nodes (inodes). ffree: 0, // Meaningless on Windows - } + }) } } |