install: options support non-ut8 too

This commit is contained in:
Sylvestre Ledru 2025-08-12 00:45:58 +02:00
parent 66a1fc8bbf
commit 83e5be8a52
2 changed files with 21 additions and 7 deletions

View file

@ -569,13 +569,17 @@ fn standard(mut paths: Vec<OsString>, b: &Behavior) -> UResult<()> {
if let Some(to_create) = to_create {
// if the path ends in /, remove it
let to_create_owned;
let to_create = if to_create.to_string_lossy().ends_with('/') {
let path_str = to_create.to_string_lossy();
let trimmed = path_str.trim_end_matches('/');
to_create_owned = PathBuf::from(trimmed);
to_create_owned.as_path()
} else {
to_create
let to_create = match uucore::os_str_as_bytes(to_create.as_os_str()) {
Ok(path_bytes) if path_bytes.ends_with(b"/") => {
let mut trimmed_bytes = path_bytes;
while trimmed_bytes.ends_with(b"/") {
trimmed_bytes = &trimmed_bytes[..trimmed_bytes.len() - 1];
}
let trimmed_os_str = std::ffi::OsStr::from_bytes(trimmed_bytes);
to_create_owned = PathBuf::from(trimmed_os_str);
to_create_owned.as_path()
}
_ => to_create,
};
if !to_create.exists() {

View file

@ -2380,4 +2380,14 @@ fn test_install_non_utf8_paths() {
at.mkdir(dest_dir);
ucmd.arg(&source_filename).arg(dest_dir).succeeds();
// Test with trailing slash and directory creation (-D flag)
let (at, mut ucmd) = at_and_ucmd!();
let source_file = "source.txt";
let mut target_path = std::ffi::OsString::from_vec(vec![0xFF, 0xFE, b'd', b'i', b'r']);
target_path.push("/target.txt");
at.touch(source_file);
ucmd.arg("-D").arg(source_file).arg(&target_path).succeeds();
}