1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

truncate: fix error message for file not found

Change the error message for when the reference file (the `-r` argument)
is not found to match GNU coreutils. This commit also eliminates a
redundant call to `File::open`; the file need not be opened because the
size in bytes can be read from the result of `std::fs::metadata()`.
This commit is contained in:
Jeffrey Finkelstein 2021-05-20 20:55:11 -04:00
parent b9e99543db
commit fc29846b45
2 changed files with 16 additions and 6 deletions

View file

@ -11,7 +11,8 @@
extern crate uucore; extern crate uucore;
use clap::{App, Arg}; use clap::{App, Arg};
use std::fs::{metadata, File, OpenOptions}; use std::fs::{metadata, OpenOptions};
use std::io::ErrorKind;
use std::path::Path; use std::path::Path;
#[derive(Eq, PartialEq)] #[derive(Eq, PartialEq)]
@ -174,13 +175,14 @@ fn truncate(
TruncateMode::Reduce => (), TruncateMode::Reduce => (),
_ => crash!(1, "you must specify a relative --size with --reference"), _ => crash!(1, "you must specify a relative --size with --reference"),
}; };
let _ = match File::open(Path::new(rfilename)) {
Ok(m) => m,
Err(f) => crash!(1, "{}", f.to_string()),
};
match metadata(rfilename) { match metadata(rfilename) {
Ok(meta) => meta.len(), Ok(meta) => meta.len(),
Err(f) => crash!(1, "{}", f.to_string()), Err(f) => match f.kind() {
ErrorKind::NotFound => {
crash!(1, "cannot stat '{}': No such file or directory", rfilename)
}
_ => crash!(1, "{}", f.to_string()),
},
} }
} }
None => 0, None => 0,

View file

@ -245,3 +245,11 @@ fn test_invalid_numbers() {
new_ucmd!().args(&["-s", "0XB", "file"]).fails().stderr_contains("Invalid number: 0XB"); new_ucmd!().args(&["-s", "0XB", "file"]).fails().stderr_contains("Invalid number: 0XB");
new_ucmd!().args(&["-s", "0B", "file"]).fails().stderr_contains("Invalid number: 0B"); new_ucmd!().args(&["-s", "0B", "file"]).fails().stderr_contains("Invalid number: 0B");
} }
#[test]
fn test_reference_file_not_found() {
new_ucmd!()
.args(&["-r", "a", "b"])
.fails()
.stderr_contains("cannot stat 'a': No such file or directory");
}