1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 19:17:43 +00:00

Merge pull request #2242 from jfinkels/truncate-file-not-found-error

truncate: fix error message for file not found
This commit is contained in:
Sylvestre Ledru 2021-05-21 10:06:01 +02:00 committed by GitHub
commit d00dabca1c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 6 deletions

View file

@ -11,7 +11,8 @@
extern crate uucore;
use clap::{App, Arg};
use std::fs::{metadata, File, OpenOptions};
use std::fs::{metadata, OpenOptions};
use std::io::ErrorKind;
use std::path::Path;
#[derive(Eq, PartialEq)]
@ -174,13 +175,14 @@ fn truncate(
TruncateMode::Reduce => (),
_ => 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) {
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,

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", "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");
}