1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

Merge pull request #6549 from Kev1n8/remove-crash-fsext

fsext: remove crash!
This commit is contained in:
Sylvestre Ledru 2024-07-07 21:19:59 +02:00 committed by GitHub
commit 39a38013dc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 42 additions and 24 deletions

View file

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

View file

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

View file

@ -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>>();

View file

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