df: Move to use OsString

This allows us to handle non-Unicode parameters.
This commit is contained in:
Nicolas Boichat
2025-07-24 09:26:54 +08:00
parent bd2e33bb34
commit 6db117dc76
3 changed files with 16 additions and 9 deletions
+2 -1
View File
@@ -431,7 +431,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let opt = Options::from(&matches).map_err(DfError::OptionsError)?;
// Get the list of filesystems to display in the output table.
let filesystems: Vec<Filesystem> = match matches.get_many::<String>(OPT_PATHS) {
let filesystems: Vec<Filesystem> = match matches.get_many::<OsString>(OPT_PATHS) {
None => {
let filesystems = get_all_filesystems(&opt).map_err(|e| {
let context = get_message("df-error-cannot-read-table-of-mounted-filesystems");
@@ -611,6 +611,7 @@ pub fn uu_app() -> Command {
.arg(
Arg::new(OPT_PATHS)
.action(ArgAction::Append)
.value_parser(ValueParser::os_string())
.value_hint(clap::ValueHint::AnyPath),
)
}
+5 -5
View File
@@ -8,7 +8,7 @@
//! filesystem mounted at a particular directory. It also includes
//! information on amount of space available and amount of space used.
// spell-checker:ignore canonicalized
use std::path::Path;
use std::{ffi::OsString, path::Path};
#[cfg(unix)]
use uucore::fsext::statfs;
@@ -28,7 +28,7 @@ pub(crate) struct Filesystem {
/// When invoking `df` with a positional argument, it displays
/// usage information for the filesystem that contains the given
/// file. If given, this field contains that filename.
pub file: Option<String>,
pub file: Option<OsString>,
/// Information about the mounted device, mount directory, and related options.
pub mount_info: MountInfo,
@@ -123,7 +123,7 @@ where
impl Filesystem {
// TODO: resolve uuid in `mount_info.dev_name` if exists
pub(crate) fn new(mount_info: MountInfo, file: Option<String>) -> Option<Self> {
pub(crate) fn new(mount_info: MountInfo, file: Option<OsString>) -> Option<Self> {
let _stat_path = if mount_info.mount_dir.is_empty() {
#[cfg(unix)]
{
@@ -154,7 +154,7 @@ impl Filesystem {
pub(crate) fn from_mount(
mounts: &[MountInfo],
mount: &MountInfo,
file: Option<String>,
file: Option<OsString>,
) -> Result<Self, FsError> {
if is_over_mounted(mounts, mount) {
Err(FsError::OverMounted)
@@ -189,7 +189,7 @@ impl Filesystem {
where
P: AsRef<Path>,
{
let file = path.as_ref().display().to_string();
let file = path.as_ref().as_os_str().to_owned();
let canonicalize = true;
let result = mount_info_from_path(mounts, path, canonicalize);
+9 -3
View File
@@ -16,6 +16,7 @@ use crate::{BlockSize, Options};
use uucore::fsext::{FsUsage, MountInfo};
use uucore::locale::get_message;
use std::ffi::OsString;
use std::fmt;
use std::ops::AddAssign;
@@ -25,7 +26,7 @@ use std::ops::AddAssign;
/// filesystem device, the mountpoint, the number of bytes used, etc.
pub(crate) struct Row {
/// The filename given on the command-line, if given.
file: Option<String>,
file: Option<OsString>,
/// Name of the device on which the filesystem lives.
fs_device: String,
@@ -283,7 +284,12 @@ impl<'a> RowFormatter<'a> {
Column::Iused => self.scaled_inodes(self.row.inodes_used),
Column::Iavail => self.scaled_inodes(self.row.inodes_free),
Column::Ipcent => Self::percentage(self.row.inodes_usage),
Column::File => self.row.file.as_ref().unwrap_or(&"-".into()).to_string(),
Column::File => self
.row
.file
.as_ref()
.map(|s| s.to_string_lossy().into_owned())
.unwrap_or("-".into()),
Column::Fstype => self.row.fs_type.to_string(),
#[cfg(target_os = "macos")]
@@ -516,7 +522,7 @@ mod tests {
impl Default for Row {
fn default() -> Self {
Self {
file: Some("/path/to/file".to_string()),
file: Some("/path/to/file".into()),
fs_device: "my_device".to_string(),
fs_type: "my_type".to_string(),
fs_mount: "my_mount".to_string(),