mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 19:47:45 +00:00
truncate: better error msg when dir does not exist
Improve the error message that gets printed when a directory does not exist. After this commit, the error message is truncate: cannot open '{file}' for writing: No such file or directory where `{file}` is the name of a file in a directory that does not exist.
This commit is contained in:
parent
f1d72018d7
commit
c780c96e17
2 changed files with 18 additions and 3 deletions
|
@ -218,7 +218,8 @@ fn truncate_reference_and_size(
|
||||||
let fsize = metadata.len() as usize;
|
let fsize = metadata.len() as usize;
|
||||||
let tsize = mode.to_size(fsize);
|
let tsize = mode.to_size(fsize);
|
||||||
for filename in &filenames {
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -251,7 +252,8 @@ fn truncate_reference_file_only(
|
||||||
})?;
|
})?;
|
||||||
let tsize = metadata.len() as usize;
|
let tsize = metadata.len() as usize;
|
||||||
for filename in &filenames {
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -286,7 +288,11 @@ fn truncate_size_only(size_string: &str, filenames: Vec<String>, create: bool) -
|
||||||
match file_truncate(filename, create, tsize) {
|
match file_truncate(filename, create, tsize) {
|
||||||
Ok(_) => continue,
|
Ok(_) => continue,
|
||||||
Err(e) if e.kind() == ErrorKind::NotFound && !create => 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(())
|
Ok(())
|
||||||
|
|
|
@ -377,3 +377,12 @@ fn test_division_by_zero_reference_and_size() {
|
||||||
.no_stdout()
|
.no_stdout()
|
||||||
.stderr_contains("division by zero");
|
.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");
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue