From 9d554751ca6e2c3542b3de2ead12b4c468e3794b Mon Sep 17 00:00:00 2001 From: leon Date: Wed, 8 Jun 2022 22:01:21 +0200 Subject: [PATCH 1/5] df: better error message when executed in a chroot without /proc #3601 --- src/uu/df/src/df.rs | 38 ++++++++++++++++++++-------- src/uu/stat/src/stat.rs | 2 +- src/uucore/src/lib/features/fsext.rs | 17 ++++++------- 3 files changed, 36 insertions(+), 21 deletions(-) diff --git a/src/uu/df/src/df.rs b/src/uu/df/src/df.rs index ca42b56ed..c0b0af477 100644 --- a/src/uu/df/src/df.rs +++ b/src/uu/df/src/df.rs @@ -14,7 +14,7 @@ mod table; use blocks::HumanReadable; use table::HeaderMode; use uucore::display::Quotable; -use uucore::error::{UError, UResult, USimpleError}; +use uucore::error::{UError, UIoError, UResult, USimpleError}; use uucore::fsext::{read_fs_list, MountInfo}; use uucore::parse_size::ParseSizeError; use uucore::{format_usage, show}; @@ -333,7 +333,7 @@ fn filter_mount_list(vmi: Vec, opt: &Options) -> Vec { /// `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) -> Vec { +fn get_all_filesystems(opt: &Options) -> Result, std::io::Error> { // Run a sync call before any operation if so instructed. if opt.sync { #[cfg(not(windows))] @@ -349,19 +349,19 @@ fn get_all_filesystems(opt: &Options) -> Vec { // // Filesystems excluded by the command-line options are // not considered. - let mounts: Vec = filter_mount_list(read_fs_list(), opt); + let mounts: Vec = filter_mount_list(read_fs_list()?, opt); // Convert each `MountInfo` into a `Filesystem`, which contains // both the mount information and usage information. - mounts + Ok(mounts .into_iter() .filter_map(|m| Filesystem::new(m, None)) .filter(|fs| opt.show_all_fs || fs.usage.blocks > 0) - .collect() + .collect()) } /// For each path, get the filesystem that contains that path. -fn get_named_filesystems

(paths: &[P], opt: &Options) -> Vec +fn get_named_filesystems

(paths: &[P], opt: &Options) -> Result, std::io::Error> where P: AsRef, { @@ -371,7 +371,7 @@ where // considered. The "lofs" filesystem is a loopback // filesystem present on Solaris and FreeBSD systems. It // is similar to a symbolic link. - let mounts: Vec = filter_mount_list(read_fs_list(), opt) + let mounts: Vec = filter_mount_list(read_fs_list()?, opt) .into_iter() .filter(|mi| mi.fs_type != "lofs" && !mi.dummy) .collect(); @@ -381,7 +381,7 @@ where // this happens if the file system type doesn't exist if mounts.is_empty() { show!(USimpleError::new(1, "no file systems processed")); - return result; + return Ok(result); } // Convert each path into a `Filesystem`, which contains @@ -402,7 +402,7 @@ where } } } - result + Ok(result) } #[derive(Debug)] @@ -443,7 +443,15 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { // Get the list of filesystems to display in the output table. let filesystems: Vec = match matches.values_of(OPT_PATHS) { None => { - let filesystems = get_all_filesystems(&opt); + let filesystems = match get_all_filesystems(&opt) { + Ok(filesystems) => filesystems, + Err(error) => { + return Err(UIoError::new( + error.kind(), + format!("cannot read table of mounted file systems"), + )); + } + }; if filesystems.is_empty() { return Err(USimpleError::new(1, "no file systems processed")); @@ -453,7 +461,15 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { } Some(paths) => { let paths: Vec<&str> = paths.collect(); - let filesystems = get_named_filesystems(&paths, &opt); + let filesystems = match get_named_filesystems(&paths, &opt) { + Ok(filesystems) => filesystems, + Err(error) => { + return Err(UIoError::new( + error.kind(), + format!("cannot read table of mounted file systems"), + )); + } + }; // This can happen if paths are given as command-line arguments // but none of the paths exist. diff --git a/src/uu/stat/src/stat.rs b/src/uu/stat/src/stat.rs index 71960903c..7d2ad04b0 100644 --- a/src/uu/stat/src/stat.rs +++ b/src/uu/stat/src/stat.rs @@ -501,7 +501,7 @@ impl Stater { // mount points aren't displayed when showing filesystem information None } else { - let mut mount_list = read_fs_list() + let mut mount_list = read_fs_list()? .iter() .map(|mi| mi.mount_dir.clone()) .collect::>(); diff --git a/src/uucore/src/lib/features/fsext.rs b/src/uucore/src/lib/features/fsext.rs index 787684e93..8f2047355 100644 --- a/src/uucore/src/lib/features/fsext.rs +++ b/src/uucore/src/lib/features/fsext.rs @@ -405,22 +405,21 @@ use std::ptr; ))] use std::slice; /// Read file system list. -pub fn read_fs_list() -> Vec { +pub fn read_fs_list() -> Result, std::io::Error> { #[cfg(any(target_os = "linux", target_os = "android"))] { let (file_name, f) = File::open(LINUX_MOUNTINFO) .map(|f| (LINUX_MOUNTINFO, f)) - .or_else(|_| File::open(LINUX_MTAB).map(|f| (LINUX_MTAB, f))) - .expect("failed to find mount list files"); + .or_else(|_| File::open(LINUX_MTAB).map(|f| (LINUX_MTAB, f)))?; let reader = BufReader::new(f); - reader + Ok(reader .lines() .filter_map(|line| line.ok()) .filter_map(|line| { let raw_data = line.split_whitespace().collect::>(); MountInfo::new(file_name, &raw_data) }) - .collect::>() + .collect::>()) } #[cfg(any( target_os = "freebsd", @@ -435,10 +434,10 @@ pub fn read_fs_list() -> Vec { crash!(1, "get_mount_info() failed"); } let mounts = unsafe { slice::from_raw_parts(mount_buffer_ptr, len as usize) }; - mounts + Ok(mounts .iter() .map(|m| MountInfo::from(*m)) - .collect::>() + .collect::>()) } #[cfg(windows)] { @@ -482,12 +481,12 @@ pub fn read_fs_list() -> Vec { unsafe { FindVolumeClose(find_handle); } - mounts + Ok(mounts) } #[cfg(any(target_os = "redox", target_os = "illumos", target_os = "solaris"))] { // No method to read mounts, yet - Vec::new() + Ok(Vec::new()) } } From 72b0ba0b0554bb829c11b15a85ef8a5067d7e8b1 Mon Sep 17 00:00:00 2001 From: leon Date: Thu, 9 Jun 2022 08:38:14 +0200 Subject: [PATCH 2/5] df: fixed clippy warning --- src/uu/df/src/df.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/uu/df/src/df.rs b/src/uu/df/src/df.rs index c0b0af477..bb6118ac8 100644 --- a/src/uu/df/src/df.rs +++ b/src/uu/df/src/df.rs @@ -448,7 +448,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { Err(error) => { return Err(UIoError::new( error.kind(), - format!("cannot read table of mounted file systems"), + "cannot read table of mounted file systems", )); } }; @@ -466,7 +466,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { Err(error) => { return Err(UIoError::new( error.kind(), - format!("cannot read table of mounted file systems"), + "cannot read table of mounted file systems", )); } }; From 388e14f208a8184c39386103716933f2e9f77e41 Mon Sep 17 00:00:00 2001 From: leon Date: Thu, 9 Jun 2022 12:35:37 +0200 Subject: [PATCH 3/5] df: error handling cleanup --- src/uu/df/src/df.rs | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/src/uu/df/src/df.rs b/src/uu/df/src/df.rs index bb6118ac8..63a3b31a9 100644 --- a/src/uu/df/src/df.rs +++ b/src/uu/df/src/df.rs @@ -14,6 +14,7 @@ mod table; use blocks::HumanReadable; use table::HeaderMode; use uucore::display::Quotable; +use uucore::error::FromIo; use uucore::error::{UError, UIoError, UResult, USimpleError}; use uucore::fsext::{read_fs_list, MountInfo}; use uucore::parse_size::ParseSizeError; @@ -443,15 +444,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { // Get the list of filesystems to display in the output table. let filesystems: Vec = match matches.values_of(OPT_PATHS) { None => { - let filesystems = match get_all_filesystems(&opt) { - Ok(filesystems) => filesystems, - Err(error) => { - return Err(UIoError::new( - error.kind(), - "cannot read table of mounted file systems", - )); - } - }; + let filesystems = get_all_filesystems(&opt) + .map_err_context(|| "cannot read table of mounted file systems".into())?; if filesystems.is_empty() { return Err(USimpleError::new(1, "no file systems processed")); @@ -461,15 +455,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { } Some(paths) => { let paths: Vec<&str> = paths.collect(); - let filesystems = match get_named_filesystems(&paths, &opt) { - Ok(filesystems) => filesystems, - Err(error) => { - return Err(UIoError::new( - error.kind(), - "cannot read table of mounted file systems", - )); - } - }; + let filesystems = get_named_filesystems(&paths, &opt) + .map_err_context(|| "cannot read table of mounted file systems".into())?; // This can happen if paths are given as command-line arguments // but none of the paths exist. From 97998a64dd6b9d2c48346509779fb4e65a921e2b Mon Sep 17 00:00:00 2001 From: leon Date: Thu, 9 Jun 2022 12:48:20 +0200 Subject: [PATCH 4/5] df: removed unused import --- src/uu/df/src/df.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/uu/df/src/df.rs b/src/uu/df/src/df.rs index 63a3b31a9..8aa272487 100644 --- a/src/uu/df/src/df.rs +++ b/src/uu/df/src/df.rs @@ -15,7 +15,7 @@ use blocks::HumanReadable; use table::HeaderMode; use uucore::display::Quotable; use uucore::error::FromIo; -use uucore::error::{UError, UIoError, UResult, USimpleError}; +use uucore::error::{UError, UResult, USimpleError}; use uucore::fsext::{read_fs_list, MountInfo}; use uucore::parse_size::ParseSizeError; use uucore::{format_usage, show}; From de4cfdbea6132725b2e9b0fa79f23b96f74b4358 Mon Sep 17 00:00:00 2001 From: leon Date: Thu, 9 Jun 2022 21:24:43 +0200 Subject: [PATCH 5/5] stat: improved error message --- src/uu/stat/src/stat.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/uu/stat/src/stat.rs b/src/uu/stat/src/stat.rs index 7d2ad04b0..c26bca394 100644 --- a/src/uu/stat/src/stat.rs +++ b/src/uu/stat/src/stat.rs @@ -8,7 +8,7 @@ #[macro_use] extern crate uucore; use uucore::display::Quotable; -use uucore::error::{UResult, USimpleError}; +use uucore::error::{FromIo, UResult, USimpleError}; use uucore::fs::display_permissions; use uucore::fsext::{ pretty_filetype, pretty_fstype, pretty_time, read_fs_list, statfs, BirthTime, FsMeta, @@ -501,7 +501,8 @@ impl Stater { // mount points aren't displayed when showing filesystem information None } else { - let mut mount_list = read_fs_list()? + let mut mount_list = read_fs_list() + .map_err_context(|| "cannot read table of mounted file systems".into())? .iter() .map(|mi| mi.mount_dir.clone()) .collect::>();