diff --git a/src/uu/truncate/src/truncate.rs b/src/uu/truncate/src/truncate.rs index 7673891a9f8..5be63736b96 100644 --- a/src/uu/truncate/src/truncate.rs +++ b/src/uu/truncate/src/truncate.rs @@ -239,7 +239,8 @@ fn truncate_reference_and_size( let fsize = metadata.len() as usize; let tsize = mode.to_size(fsize); for filename in &filenames { - file_truncate(filename, create, tsize).map_err_context(String::new)?; + file_truncate(filename, create, tsize) + .map_err_context(|| format!("cannot open {} for writing", filename.quote()))?; } Ok(()) } @@ -272,7 +273,8 @@ fn truncate_reference_file_only( })?; let tsize = metadata.len() as usize; for filename in &filenames { - file_truncate(filename, create, tsize).map_err_context(String::new)?; + file_truncate(filename, create, tsize) + .map_err_context(|| format!("cannot open {} for writing", filename.quote()))?; } Ok(()) } @@ -307,7 +309,11 @@ fn truncate_size_only(size_string: &str, filenames: Vec, create: bool) - match file_truncate(filename, create, tsize) { Ok(_) => continue, Err(e) if e.kind() == ErrorKind::NotFound && !create => continue, - Err(e) => return Err(e.map_err_context(String::new)), + Err(e) => { + return Err( + e.map_err_context(|| format!("cannot open {} for writing", filename.quote())) + ) + } } } Ok(()) diff --git a/tests/by-util/test_truncate.rs b/tests/by-util/test_truncate.rs index 801951548ae..0ef65ec1645 100644 --- a/tests/by-util/test_truncate.rs +++ b/tests/by-util/test_truncate.rs @@ -378,6 +378,15 @@ fn test_division_by_zero_reference_and_size() { .stderr_contains("division by zero"); } +#[test] +fn test_no_such_dir() { + new_ucmd!() + .args(&["-s", "0", "a/b"]) + .fails() + .no_stdout() + .stderr_contains("cannot open 'a/b' for writing: No such file or directory"); +} + /// Test that truncate with a relative size less than 0 is not an error. #[test] fn test_underflow_relative_size() {