Skip to content

Commit

Permalink
Merge pull request #2943 from jfinkels/truncate-no-such-dir
Browse files Browse the repository at this point in the history
truncate: better error msg when dir does not exist
  • Loading branch information
sylvestre authored Jan 29, 2022
2 parents 52ab632 + 57ee6b5 commit 1dcd3b2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/uu/truncate/src/truncate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down Expand Up @@ -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(())
}
Expand Down Expand Up @@ -307,7 +309,11 @@ fn truncate_size_only(size_string: &str, filenames: Vec<String>, 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(())
Expand Down
9 changes: 9 additions & 0 deletions tests/by-util/test_truncate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 1dcd3b2

Please sign in to comment.