Skip to content

Commit d1ec00f

Browse files
authored
Merge pull request #8371 from drinkcat/df-osstr
df: Move to using `OsString`
2 parents bd2e33b + f9af783 commit d1ec00f

File tree

11 files changed

+373
-200
lines changed

11 files changed

+373
-200
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fuzz/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/uu/comm/src/comm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl OrderChecker {
101101
return true;
102102
}
103103

104-
let is_ordered = current_line >= &self.last_line;
104+
let is_ordered = *current_line >= *self.last_line;
105105
if !is_ordered && !self.has_error {
106106
eprintln!(
107107
"{}",

src/uu/df/src/df.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use uucore::{format_usage, show};
2020
use clap::{Arg, ArgAction, ArgMatches, Command, parser::ValueSource};
2121

2222
use std::ffi::OsString;
23+
use std::io::stdout;
2324
use std::path::Path;
2425
use thiserror::Error;
2526

@@ -431,7 +432,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
431432

432433
let opt = Options::from(&matches).map_err(DfError::OptionsError)?;
433434
// Get the list of filesystems to display in the output table.
434-
let filesystems: Vec<Filesystem> = match matches.get_many::<String>(OPT_PATHS) {
435+
let filesystems: Vec<Filesystem> = match matches.get_many::<OsString>(OPT_PATHS) {
435436
None => {
436437
let filesystems = get_all_filesystems(&opt).map_err(|e| {
437438
let context = get_message("df-error-cannot-read-table-of-mounted-filesystems");
@@ -464,7 +465,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
464465
}
465466
};
466467

467-
println!("{}", Table::new(&opt, filesystems));
468+
Table::new(&opt, filesystems).write_to(&mut stdout())?;
468469

469470
Ok(())
470471
}
@@ -611,6 +612,7 @@ pub fn uu_app() -> Command {
611612
.arg(
612613
Arg::new(OPT_PATHS)
613614
.action(ArgAction::Append)
615+
.value_parser(ValueParser::os_string())
614616
.value_hint(clap::ValueHint::AnyPath),
615617
)
616618
}
@@ -629,9 +631,9 @@ mod tests {
629631
dev_id: String::new(),
630632
dev_name: String::from(dev_name),
631633
fs_type: String::new(),
632-
mount_dir: String::from(mount_dir),
634+
mount_dir: mount_dir.into(),
633635
mount_option: String::new(),
634-
mount_root: String::from(mount_root),
636+
mount_root: mount_root.into(),
635637
remote: false,
636638
dummy: false,
637639
}
@@ -679,9 +681,9 @@ mod tests {
679681
dev_id: String::from(dev_id),
680682
dev_name: String::new(),
681683
fs_type: String::new(),
682-
mount_dir: String::from(mount_dir),
684+
mount_dir: mount_dir.into(),
683685
mount_option: String::new(),
684-
mount_root: String::new(),
686+
mount_root: "/".into(),
685687
remote: false,
686688
dummy: false,
687689
}
@@ -724,9 +726,9 @@ mod tests {
724726
dev_id: String::new(),
725727
dev_name: String::new(),
726728
fs_type: String::from(fs_type),
727-
mount_dir: String::from(mount_dir),
729+
mount_dir: mount_dir.into(),
728730
mount_option: String::new(),
729-
mount_root: String::new(),
731+
mount_root: "/".into(),
730732
remote,
731733
dummy,
732734
}

src/uu/df/src/filesystem.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//! filesystem mounted at a particular directory. It also includes
99
//! information on amount of space available and amount of space used.
1010
// spell-checker:ignore canonicalized
11-
use std::path::Path;
11+
use std::{ffi::OsString, path::Path};
1212

1313
#[cfg(unix)]
1414
use uucore::fsext::statfs;
@@ -28,7 +28,7 @@ pub(crate) struct Filesystem {
2828
/// When invoking `df` with a positional argument, it displays
2929
/// usage information for the filesystem that contains the given
3030
/// file. If given, this field contains that filename.
31-
pub file: Option<String>,
31+
pub file: Option<OsString>,
3232

3333
/// Information about the mounted device, mount directory, and related options.
3434
pub mount_info: MountInfo,
@@ -123,22 +123,22 @@ where
123123

124124
impl Filesystem {
125125
// TODO: resolve uuid in `mount_info.dev_name` if exists
126-
pub(crate) fn new(mount_info: MountInfo, file: Option<String>) -> Option<Self> {
126+
pub(crate) fn new(mount_info: MountInfo, file: Option<OsString>) -> Option<Self> {
127127
let _stat_path = if mount_info.mount_dir.is_empty() {
128128
#[cfg(unix)]
129129
{
130-
mount_info.dev_name.clone()
130+
mount_info.dev_name.clone().into()
131131
}
132132
#[cfg(windows)]
133133
{
134134
// On windows, we expect the volume id
135-
mount_info.dev_id.clone()
135+
mount_info.dev_id.clone().into()
136136
}
137137
} else {
138138
mount_info.mount_dir.clone()
139139
};
140140
#[cfg(unix)]
141-
let usage = FsUsage::new(statfs(_stat_path).ok()?);
141+
let usage = FsUsage::new(statfs(&_stat_path).ok()?);
142142
#[cfg(windows)]
143143
let usage = FsUsage::new(Path::new(&_stat_path)).ok()?;
144144
Some(Self {
@@ -154,7 +154,7 @@ impl Filesystem {
154154
pub(crate) fn from_mount(
155155
mounts: &[MountInfo],
156156
mount: &MountInfo,
157-
file: Option<String>,
157+
file: Option<OsString>,
158158
) -> Result<Self, FsError> {
159159
if is_over_mounted(mounts, mount) {
160160
Err(FsError::OverMounted)
@@ -165,7 +165,7 @@ impl Filesystem {
165165

166166
/// Find and create the filesystem from the given mount.
167167
#[cfg(windows)]
168-
pub(crate) fn from_mount(mount: &MountInfo, file: Option<String>) -> Result<Self, FsError> {
168+
pub(crate) fn from_mount(mount: &MountInfo, file: Option<OsString>) -> Result<Self, FsError> {
169169
Self::new(mount.clone(), file).ok_or(FsError::MountMissing)
170170
}
171171

@@ -189,7 +189,7 @@ impl Filesystem {
189189
where
190190
P: AsRef<Path>,
191191
{
192-
let file = path.as_ref().display().to_string();
192+
let file = path.as_ref().as_os_str().to_owned();
193193
let canonicalize = true;
194194

195195
let result = mount_info_from_path(mounts, path, canonicalize);
@@ -205,6 +205,8 @@ mod tests {
205205

206206
mod mount_info_from_path {
207207

208+
use std::ffi::OsString;
209+
208210
use uucore::fsext::MountInfo;
209211

210212
use crate::filesystem::{FsError, mount_info_from_path};
@@ -215,9 +217,9 @@ mod tests {
215217
dev_id: String::default(),
216218
dev_name: String::default(),
217219
fs_type: String::default(),
218-
mount_dir: String::from(mount_dir),
220+
mount_dir: OsString::from(mount_dir),
219221
mount_option: String::default(),
220-
mount_root: String::default(),
222+
mount_root: OsString::default(),
221223
remote: Default::default(),
222224
dummy: Default::default(),
223225
}
@@ -312,6 +314,8 @@ mod tests {
312314

313315
#[cfg(not(windows))]
314316
mod over_mount {
317+
use std::ffi::OsString;
318+
315319
use crate::filesystem::{Filesystem, FsError, is_over_mounted};
316320
use uucore::fsext::MountInfo;
317321

@@ -320,9 +324,9 @@ mod tests {
320324
dev_id: String::default(),
321325
dev_name: dev_name.map(String::from).unwrap_or_default(),
322326
fs_type: String::default(),
323-
mount_dir: String::from(mount_dir),
327+
mount_dir: OsString::from(mount_dir),
324328
mount_option: String::default(),
325-
mount_root: String::default(),
329+
mount_root: OsString::default(),
326330
remote: Default::default(),
327331
dummy: Default::default(),
328332
}

0 commit comments

Comments
 (0)