8000 df: better error message when executed in a chroot without /proc #3601 by lendandgit · Pull Request #3602 · uutils/coreutils · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

df: better error message when executed in a chroot without /proc #3601 #3602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions src/uu/df/src/df.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod table;
use blocks::HumanReadable;
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;
Expand Down Expand Up @@ -333,7 +334,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) -> Vec<Filesystem> {
fn get_all_filesystems(opt: &Options) -> Result<Vec<Filesystem>, std::io::Error> {
// Run a sync call before any operation if so instructed.
if opt.sync {
#[cfg(not(windows))]
Expand All @@ -349,19 +350,19 @@ fn get_all_filesystems(opt: &Options) -> Vec<Filesystem> {
//
// Filesystems excluded by the command-line options are
// not considered.
let mounts: Vec<MountInfo> = filter_mount_list(read_fs_list(), opt);
let mounts: Vec<MountInfo> = 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<P>(paths: &[P], opt: &Options) -> Vec<Filesystem>
fn get_named_filesystems<P>(paths: &[P], opt: &Options) -> Result<Vec<Filesystem>, std::io::Error>
where
P: AsRef<Path>,
{
Expand All @@ -371,7 +372,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<MountInfo> = filter_mount_list(read_fs_list(), opt)
let mounts: Vec<MountInfo> = filter_mount_list(read_fs_list()?, opt)
.into_iter()
.filter(|mi| mi.fs_type != "lofs" && !mi.dummy)
.collect();
Expand All @@ -381,7 +382,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
Expand All @@ -402,7 +403,7 @@ where
}
}
}
result
Ok(result)
}

#[derive(Debug)]
Expand Down Expand Up @@ -443,7 +444,8 @@ 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.values_of(OPT_PATHS) {
None => {
let filesystems = get_all_filesystems(&opt);
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"));
Expand All @@ -453,7 +455,8 @@ 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 = 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.
Expand Down
3 changes: 2 additions & 1 deletion src/uu/stat/src/stat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -502,6 +502,7 @@ impl Stater {
None
} else {
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::<Vec<String>>();
Expand Down
17 changes: 8 additions & 9 deletions src/uucore/src/lib/features/fsext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,22 +405,21 @@ use std::ptr;
))]
use std::slice;
/// Read file system list.
pub fn read_fs_list() -> Vec<MountInfo> {
pub fn read_fs_list() -> Result<Vec<MountInfo>, 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::<Vec<&str>>();
MountInfo::new(file_name, &raw_data)
})
.collect::<Vec<_>>()
.collect::<Vec<_>>())
}
#[cfg(any(
target_os = "freebsd",
Expand All @@ -435,10 +434,10 @@ pub fn read_fs_list() -> Vec<MountInfo> {
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::<Vec<_>>()
.collect::<Vec<_>>())
}
#[cfg(windows)]
{
Expand Down Expand Up @@ -482,12 +481,12 @@ pub fn read_fs_list() -> Vec<MountInfo> {
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())
}
}

Expand Down
0