diff --git a/src/uu/ln/src/ln.rs b/src/uu/ln/src/ln.rs index a45fc17c6..0557dd4f3 100644 --- a/src/uu/ln/src/ln.rs +++ b/src/uu/ln/src/ln.rs @@ -30,7 +30,7 @@ use uucore::fs::{MissingHandling, ResolveMode, canonicalize}; pub struct Settings { overwrite: OverwriteMode, backup: BackupMode, - suffix: String, + suffix: OsString, symbolic: bool, relative: bool, logical: bool, @@ -126,7 +126,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let settings = Settings { overwrite: overwrite_mode, backup: backup_mode, - suffix: backup_suffix, + suffix: OsString::from(backup_suffix), symbolic, logical, relative: matches.get_flag(options::RELATIVE), @@ -447,16 +447,16 @@ fn link(src: &Path, dst: &Path, settings: &Settings) -> UResult<()> { Ok(()) } -fn simple_backup_path(path: &Path, suffix: &str) -> PathBuf { - let mut p = path.as_os_str().to_str().unwrap().to_owned(); - p.push_str(suffix); - PathBuf::from(p) +fn simple_backup_path(path: &Path, suffix: &OsString) -> PathBuf { + let mut file_name = path.file_name().unwrap_or_default().to_os_string(); + file_name.push(suffix); + path.with_file_name(file_name) } fn numbered_backup_path(path: &Path) -> PathBuf { let mut i: u64 = 1; loop { - let new_path = simple_backup_path(path, &format!(".~{i}~")); + let new_path = simple_backup_path(path, &OsString::from(format!(".~{i}~"))); if !new_path.exists() { return new_path; } @@ -464,8 +464,8 @@ fn numbered_backup_path(path: &Path) -> PathBuf { } } -fn existing_backup_path(path: &Path, suffix: &str) -> PathBuf { - let test_path = simple_backup_path(path, ".~1~"); +fn existing_backup_path(path: &Path, suffix: &OsString) -> PathBuf { + let test_path = simple_backup_path(path, &OsString::from(".~1~")); if test_path.exists() { return numbered_backup_path(path); } diff --git a/src/uu/readlink/src/readlink.rs b/src/uu/readlink/src/readlink.rs index 383acb409..dd2c785b4 100644 --- a/src/uu/readlink/src/readlink.rs +++ b/src/uu/readlink/src/readlink.rs @@ -11,7 +11,6 @@ use std::fs; use std::io::{Write, stdout}; use std::path::{Path, PathBuf}; use uucore::LocalizedCommand; -use uucore::display::Quotable; use uucore::error::{FromIo, UResult, USimpleError, UUsageError}; use uucore::fs::{MissingHandling, ResolveMode, canonicalize}; use uucore::line_ending::LineEnding; diff --git a/tests/by-util/test_cut.rs b/tests/by-util/test_cut.rs index 616e20a58..b94a15834 100644 --- a/tests/by-util/test_cut.rs +++ b/tests/by-util/test_cut.rs @@ -389,29 +389,24 @@ fn test_failed_write_is_reported() { #[test] #[cfg(target_os = "linux")] fn test_cut_non_utf8_paths() { - use std::fs; + use std::fs::File; + use std::io::Write; + use std::os::unix::ffi::OsStrExt; use uutests::util::TestScenario; use uutests::util_name; let ts = TestScenario::new(util_name!()); - let at = &ts.fixtures; + let test_dir = ts.fixtures.subdir.as_path(); - // Create test file with normal name first - at.write("temp.txt", "a\tb\tc\n1\t2\t3\n"); + // Create file directly with non-UTF-8 name + let file_name = std::ffi::OsStr::from_bytes(b"test_\xFF\xFE.txt"); + let mut file = File::create(test_dir.join(file_name)).unwrap(); + file.write_all(b"a\tb\tc\n1\t2\t3\n").unwrap(); - // Rename to non-UTF-8 name - #[cfg(unix)] - { - use std::os::unix::ffi::OsStrExt; - let file_name = std::ffi::OsStr::from_bytes(b"test_\xFF\xFE.txt"); - - fs::rename(at.subdir.join("temp.txt"), at.subdir.join(file_name)).unwrap(); - - // Test that cut can handle non-UTF-8 filenames - ts.ucmd() - .arg("-f1,3") - .arg(file_name) - .succeeds() - .stdout_only("a\tc\n1\t3\n"); - } + // Test that cut can handle non-UTF-8 filenames + ts.ucmd() + .arg("-f1,3") + .arg(file_name) + .succeeds() + .stdout_only("a\tc\n1\t3\n"); } diff --git a/tests/by-util/test_head.rs b/tests/by-util/test_head.rs index 884506351..c2d886266 100644 --- a/tests/by-util/test_head.rs +++ b/tests/by-util/test_head.rs @@ -874,22 +874,11 @@ fn test_head_non_utf8_paths() { std::fs::write(at.plus(non_utf8_name), "line1\nline2\nline3\n").unwrap(); - // Test that head handles non-UTF-8 file names without crashing let result = scene.ucmd().arg(non_utf8_name).succeeds(); - // The result should contain the file content let output = result.stdout_str_lossy(); assert!(output.contains("line1")); assert!(output.contains("line2")); assert!(output.contains("line3")); - - // Test with line count argument - scene - .ucmd() - .args(&["-n", "2"]) - .arg(non_utf8_name) - .succeeds() - .stdout_contains("line1") - .stdout_contains("line2"); } // Test that head handles non-UTF-8 file names without crashing diff --git a/tests/by-util/test_join.rs b/tests/by-util/test_join.rs index 65b927717..8a239b965 100644 --- a/tests/by-util/test_join.rs +++ b/tests/by-util/test_join.rs @@ -537,20 +537,13 @@ fn test_full() { #[test] #[cfg(target_os = "linux")] fn test_join_non_utf8_paths() { - use std::fs; + use std::fs::File; + use std::io::Write; let ts = TestScenario::new(util_name!()); - let at = &ts.fixtures; + let test_dir = ts.fixtures.subdir.as_path(); - // Create files with non-UTF-8 names using shell commands - // since the test framework doesn't support OsStr for file names - let test_dir = at.subdir.as_path(); - - // Create temporary files with valid names first - at.write("temp1.txt", "a 1\n"); - at.write("temp2.txt", "a 2\n"); - - // Rename them to non-UTF-8 names using std::fs + // Create files directly with non-UTF-8 names let file1_bytes = b"test_\xFF\xFE_1.txt"; let file2_bytes = b"test_\xFF\xFE_2.txt"; @@ -560,10 +553,12 @@ fn test_join_non_utf8_paths() { let file1_name = std::ffi::OsStr::from_bytes(file1_bytes); let file2_name = std::ffi::OsStr::from_bytes(file2_bytes); - fs::rename(test_dir.join("temp1.txt"), test_dir.join(file1_name)).unwrap(); - fs::rename(test_dir.join("temp2.txt"), test_dir.join(file2_name)).unwrap(); + let mut file1 = File::create(test_dir.join(file1_name)).unwrap(); + file1.write_all(b"a 1\n").unwrap(); + + let mut file2 = File::create(test_dir.join(file2_name)).unwrap(); + file2.write_all(b"a 2\n").unwrap(); - // Test that join can handle non-UTF-8 filenames ts.ucmd() .arg(file1_name) .arg(file2_name) diff --git a/tests/by-util/test_stdbuf.rs b/tests/by-util/test_stdbuf.rs index 71e368bf8..8c3fef587 100644 --- a/tests/by-util/test_stdbuf.rs +++ b/tests/by-util/test_stdbuf.rs @@ -220,6 +220,7 @@ fn test_libstdbuf_preload() { } #[cfg(target_os = "linux")] +#[cfg(not(target_env = "musl"))] #[test] fn test_stdbuf_non_utf8_paths() { use std::os::unix::ffi::OsStringExt;