Skip to content

Commit 51d6efd

Browse files
authored
Merge pull request #9337 from blyxxyz/lossy-string-cleanup
Remove lossy OS string conversions
2 parents e6467b1 + fd6260c commit 51d6efd

File tree

75 files changed

+286
-306
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+286
-306
lines changed

src/uu/base32/src/base_common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl Config {
5454
if let Some(extra_op) = values.next() {
5555
return Err(UUsageError::new(
5656
BASE_CMD_PARSE_ERROR,
57-
translate!("base-common-extra-operand", "operand" => extra_op.to_string_lossy().quote()),
57+
translate!("base-common-extra-operand", "operand" => extra_op.quote()),
5858
));
5959
}
6060

src/uu/chcon/src/chcon.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ fn root_dev_ino_warn(dir_name: &Path) {
760760
} else {
761761
show_warning!(
762762
"{}",
763-
translate!("chcon-warning-dangerous-recursive-dir", "dir" => dir_name.to_string_lossy(), "option" => options::preserve_root::NO_PRESERVE_ROOT)
763+
translate!("chcon-warning-dangerous-recursive-dir", "dir" => dir_name.quote(), "option" => options::preserve_root::NO_PRESERVE_ROOT)
764764
);
765765
}
766766
}
@@ -782,7 +782,7 @@ fn cycle_warning_required(fts_options: c_int, entry: &fts::EntryRef) -> bool {
782782
fn emit_cycle_warning(file_name: &Path) {
783783
show_warning!(
784784
"{}",
785-
translate!("chcon-warning-circular-directory", "file" => file_name.to_string_lossy())
785+
translate!("chcon-warning-circular-directory", "file" => file_name.quote())
786786
);
787787
}
788788

src/uu/chmod/locales/en-US.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ chmod-error-dangling-symlink = cannot operate on dangling symlink {$file}
99
chmod-error-no-such-file = cannot access {$file}: No such file or directory
1010
chmod-error-preserve-root = it is dangerous to operate recursively on {$file}
1111
chmod: use --no-preserve-root to override this failsafe
12-
chmod-error-permission-denied = {$file}: Permission denied
12+
chmod-error-permission-denied = cannot access {$file}: Permission denied
1313
chmod-error-new-permissions = {$file}: new permissions are {$actual}, not {$expected}
1414
chmod-error-missing-operand = missing operand
1515

src/uu/chmod/locales/fr-FR.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ chmod-error-dangling-symlink = impossible d'opérer sur le lien symbolique pendo
2121
chmod-error-no-such-file = impossible d'accéder à {$file} : Aucun fichier ou répertoire de ce type
2222
chmod-error-preserve-root = il est dangereux d'opérer récursivement sur {$file}
2323
chmod: utiliser --no-preserve-root pour outrepasser cette protection
24-
chmod-error-permission-denied = {$file} : Permission refusée
24+
chmod-error-permission-denied = impossible d'accéder à {$file} : Permission refusée
2525
chmod-error-new-permissions = {$file} : les nouvelles permissions sont {$actual}, pas {$expected}
2626
chmod-error-missing-operand = opérande manquant
2727

src/uu/chmod/src/chmod.rs

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use clap::{Arg, ArgAction, Command};
99
use std::ffi::OsString;
1010
use std::fs;
1111
use std::os::unix::fs::{MetadataExt, PermissionsExt};
12-
use std::path::Path;
12+
use std::path::{Path, PathBuf};
1313
use thiserror::Error;
1414
use uucore::display::Quotable;
1515
use uucore::error::{ExitCode, UError, UResult, USimpleError, UUsageError, set_exit_code};
@@ -27,17 +27,17 @@ use uucore::translate;
2727
#[derive(Debug, Error)]
2828
enum ChmodError {
2929
#[error("{}", translate!("chmod-error-cannot-stat", "file" => _0.quote()))]
30-
CannotStat(String),
30+
CannotStat(PathBuf),
3131
#[error("{}", translate!("chmod-error-dangling-symlink", "file" => _0.quote()))]
32-
DanglingSymlink(String),
32+
DanglingSymlink(PathBuf),
3333
#[error("{}", translate!("chmod-error-no-such-file", "file" => _0.quote()))]
34-
NoSuchFile(String),
34+
NoSuchFile(PathBuf),
3535
#[error("{}", translate!("chmod-error-preserve-root", "file" => _0.quote()))]
36-
PreserveRoot(String),
36+
PreserveRoot(PathBuf),
3737
#[error("{}", translate!("chmod-error-permission-denied", "file" => _0.quote()))]
38-
PermissionDenied(String),
39-
#[error("{}", translate!("chmod-error-new-permissions", "file" => _0.clone(), "actual" => _1.clone(), "expected" => _2.clone()))]
40-
NewPermissions(String, String, String),
38+
PermissionDenied(PathBuf),
39+
#[error("{}", translate!("chmod-error-new-permissions", "file" => _0.maybe_quote(), "actual" => _1.clone(), "expected" => _2.clone()))]
40+
NewPermissions(PathBuf, String, String),
4141
}
4242

4343
impl UError for ChmodError {}
@@ -123,7 +123,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
123123
Some(fref) => match fs::metadata(fref) {
124124
Ok(meta) => Some(meta.mode() & 0o7777),
125125
Err(_) => {
126-
return Err(ChmodError::CannotStat(fref.to_string_lossy().to_string()).into());
126+
return Err(ChmodError::CannotStat(fref.into()).into());
127127
}
128128
},
129129
None => None,
@@ -384,22 +384,18 @@ impl Chmoder {
384384
}
385385

386386
if !self.quiet {
387-
show!(ChmodError::DanglingSymlink(
388-
filename.to_string_lossy().to_string()
389-
));
387+
show!(ChmodError::DanglingSymlink(filename.into()));
390388
set_exit_code(1);
391389
}
392390

393391
if self.verbose {
394392
println!(
395393
"{}",
396-
translate!("chmod-verbose-failed-dangling", "file" => filename.to_string_lossy().quote())
394+
translate!("chmod-verbose-failed-dangling", "file" => filename.quote())
397395
);
398396
}
399397
} else if !self.quiet {
400-
show!(ChmodError::NoSuchFile(
401-
filename.to_string_lossy().to_string()
402-
));
398+
show!(ChmodError::NoSuchFile(filename.into()));
403399
}
404400
// GNU exits with exit code 1 even if -q or --quiet are passed
405401
// So we set the exit code, because it hasn't been set yet if `self.quiet` is true.
@@ -412,7 +408,7 @@ impl Chmoder {
412408
continue;
413409
}
414410
if self.recursive && self.preserve_root && file == Path::new("/") {
415-
return Err(ChmodError::PreserveRoot("/".to_string()).into());
411+
return Err(ChmodError::PreserveRoot("/".into()).into());
416412
}
417413
if self.recursive {
418414
r = self.walk_dir_with_context(file, true);
@@ -474,10 +470,7 @@ impl Chmoder {
474470
Err(err) => {
475471
// Handle permission denied errors with proper file path context
476472
if err.kind() == std::io::ErrorKind::PermissionDenied {
477-
r = r.and(Err(ChmodError::PermissionDenied(
478-
file_path.to_string_lossy().to_string(),
479-
)
480-
.into()));
473+
r = r.and(Err(ChmodError::PermissionDenied(file_path.into()).into()));
481474
} else {
482475
r = r.and(Err(err.into()));
483476
}
@@ -504,7 +497,7 @@ impl Chmoder {
504497
// Handle permission denied with proper file path context
505498
let e = dir_meta.unwrap_err();
506499
let error = if e.kind() == std::io::ErrorKind::PermissionDenied {
507-
ChmodError::PermissionDenied(entry_path.to_string_lossy().to_string()).into()
500+
ChmodError::PermissionDenied(entry_path).into()
508501
} else {
509502
e.into()
510503
};
@@ -530,10 +523,7 @@ impl Chmoder {
530523
}
531524
Err(err) => {
532525
let error = if err.kind() == std::io::ErrorKind::PermissionDenied {
533-
ChmodError::PermissionDenied(
534-
entry_path.to_string_lossy().to_string(),
535-
)
536-
.into()
526+
ChmodError::PermissionDenied(entry_path).into()
537527
} else {
538528
err.into()
539529
};
@@ -599,9 +589,7 @@ impl Chmoder {
599589
new_mode
600590
);
601591
}
602-
return Err(
603-
ChmodError::PermissionDenied(file_path.to_string_lossy().to_string()).into(),
604-
);
592+
return Err(ChmodError::PermissionDenied(file_path.into()).into());
605593
}
606594

607595
// Report the change using the helper method
@@ -638,11 +626,9 @@ impl Chmoder {
638626
}
639627
Ok(()) // Skip dangling symlinks
640628
} else if err.kind() == std::io::ErrorKind::PermissionDenied {
641-
// These two filenames would normally be conditionally
642-
// quoted, but GNU's tests expect them to always be quoted
643-
Err(ChmodError::PermissionDenied(file.to_string_lossy().to_string()).into())
629+
Err(ChmodError::PermissionDenied(file.into()).into())
644630
} else {
645-
Err(ChmodError::CannotStat(file.to_string_lossy().to_string()).into())
631+
Err(ChmodError::CannotStat(file.into()).into())
646632
};
647633
}
648634
};
@@ -672,7 +658,7 @@ impl Chmoder {
672658
// if a permission would have been removed if umask was 0, but it wasn't because umask was not 0, print an error and fail
673659
if (new_mode & !naively_expected_new_mode) != 0 {
674660
return Err(ChmodError::NewPermissions(
675-
file.to_string_lossy().to_string(),
661+
file.into(),
676662
display_permissions_unix(new_mode as mode_t, false),
677663
display_permissions_unix(naively_expected_new_mode as mode_t, false),
678664
)

src/uu/chroot/src/chroot.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
183183
}
184184

185185
if !options.newroot.is_dir() {
186-
return Err(ChrootError::NoSuchDirectory(format!("{}", options.newroot.display())).into());
186+
return Err(ChrootError::NoSuchDirectory(options.newroot).into());
187187
}
188188

189189
let commands = match matches.get_many::<String>(options::COMMAND) {
@@ -429,7 +429,7 @@ fn enter_chroot(root: &Path, skip_chdir: bool) -> UResult<()> {
429429
let err = unsafe {
430430
chroot(
431431
CString::new(root.as_os_str().as_bytes().to_vec())
432-
.map_err(|e| ChrootError::CannotEnter("root".to_string(), e.into()))?
432+
.map_err(|e| ChrootError::CannotEnter("root".into(), e.into()))?
433433
.as_bytes_with_nul()
434434
.as_ptr()
435435
.cast(),
@@ -442,6 +442,6 @@ fn enter_chroot(root: &Path, skip_chdir: bool) -> UResult<()> {
442442
}
443443
Ok(())
444444
} else {
445-
Err(ChrootError::CannotEnter(format!("{}", root.display()), Error::last_os_error()).into())
445+
Err(ChrootError::CannotEnter(root.into(), Error::last_os_error()).into())
446446
}
447447
}

src/uu/chroot/src/error.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// spell-checker:ignore NEWROOT Userspec userspec
66
//! Errors returned by chroot.
77
use std::io::Error;
8+
use std::path::PathBuf;
89
use thiserror::Error;
910
use uucore::display::Quotable;
1011
use uucore::error::UError;
@@ -16,7 +17,7 @@ use uucore::translate;
1617
pub enum ChrootError {
1718
/// Failed to enter the specified directory.
1819
#[error("{}", translate!("chroot-error-cannot-enter", "dir" => _0.quote(), "err" => _1))]
19-
CannotEnter(String, #[source] Error),
20+
CannotEnter(PathBuf, #[source] Error),
2021

2122
/// Failed to execute the specified command.
2223
#[error("{}", translate!("chroot-error-command-failed", "cmd" => _0.quote(), "err" => _1))]
@@ -52,7 +53,7 @@ pub enum ChrootError {
5253

5354
/// The given directory does not exist.
5455
#[error("{}", translate!("chroot-error-no-such-directory", "dir" => _0.quote()))]
55-
NoSuchDirectory(String),
56+
NoSuchDirectory(PathBuf),
5657

5758
/// The call to `setgid()` failed.
5859
#[error("{}", translate!("chroot-error-set-gid-failed", "gid" => _0, "err" => _1))]

src/uu/comm/src/comm.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::ffi::OsString;
1010
use std::fs::{File, metadata};
1111
use std::io::{self, BufRead, BufReader, Read, StdinLock, stdin};
1212
use std::path::Path;
13+
use uucore::display::Quotable;
1314
use uucore::error::{FromIo, UResult, USimpleError};
1415
use uucore::format_usage;
1516
use uucore::fs::paths_refer_to_same_file;
@@ -310,9 +311,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
310311
let filename1 = matches.get_one::<OsString>(options::FILE_1).unwrap();
311312
let filename2 = matches.get_one::<OsString>(options::FILE_2).unwrap();
312313
let mut f1 = open_file(filename1, line_ending)
313-
.map_err_context(|| filename1.to_string_lossy().to_string())?;
314+
.map_err_context(|| filename1.maybe_quote().to_string())?;
314315
let mut f2 = open_file(filename2, line_ending)
315-
.map_err_context(|| filename2.to_string_lossy().to_string())?;
316+
.map_err_context(|| filename2.maybe_quote().to_string())?;
316317

317318
// Due to default_value(), there must be at least one value here, thus unwrap() must not panic.
318319
let all_delimiters = matches

src/uu/cp/src/cp.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,13 +1739,13 @@ pub(crate) fn copy_attributes(
17391739
if let Some(context) = context {
17401740
if let Err(e) = context.set_for_path(dest, false, false) {
17411741
return Err(CpError::Error(
1742-
translate!("cp-error-selinux-set-context", "path" => dest.display(), "error" => e),
1742+
translate!("cp-error-selinux-set-context", "path" => dest.quote(), "error" => e),
17431743
));
17441744
}
17451745
}
17461746
} else {
17471747
return Err(CpError::Error(
1748-
translate!("cp-error-selinux-get-context", "path" => source.display()),
1748+
translate!("cp-error-selinux-get-context", "path" => source.quote()),
17491749
));
17501750
}
17511751
Ok(())

src/uu/cp/src/platform/macos.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::os::unix::fs::OpenOptionsExt;
1010
use std::path::Path;
1111

1212
use uucore::buf_copy;
13+
use uucore::display::Quotable;
1314
use uucore::translate;
1415

1516
use uucore::mode::get_umask;
@@ -86,7 +87,7 @@ pub(crate) fn copy_on_write(
8687
// support COW).
8788
match reflink_mode {
8889
ReflinkMode::Always => {
89-
return Err(translate!("cp-error-failed-to-clone", "source" => source.display(), "dest" => dest.display(), "error" => error)
90+
return Err(translate!("cp-error-failed-to-clone", "source" => source.quote(), "dest" => dest.quote(), "error" => error)
9091
.into());
9192
}
9293
_ => {

0 commit comments

Comments
 (0)