mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 19:47:45 +00:00
Merge pull request #4888 from Ludmuterol/more-panic-4886
fix(#4886) more: panics if file is not readable
This commit is contained in:
commit
81e3b04b11
2 changed files with 29 additions and 1 deletions
|
@ -104,7 +104,17 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
|||
if length > 1 {
|
||||
buff.push_str(&MULTI_FILE_TOP_PROMPT.replace("{}", file.to_str().unwrap()));
|
||||
}
|
||||
let mut reader = BufReader::new(File::open(file).unwrap());
|
||||
let opened_file = match File::open(file) {
|
||||
Err(why) => {
|
||||
terminal::disable_raw_mode().unwrap();
|
||||
return Err(USimpleError::new(
|
||||
1,
|
||||
format!("cannot open {}: {}", file.quote(), why.kind()),
|
||||
));
|
||||
}
|
||||
Ok(opened_file) => opened_file,
|
||||
};
|
||||
let mut reader = BufReader::new(opened_file);
|
||||
reader.read_to_string(&mut buff).unwrap();
|
||||
more(&buff, &mut stdout, next_file.copied(), &options)?;
|
||||
buff.clear();
|
||||
|
|
|
@ -32,3 +32,21 @@ fn test_more_dir_arg() {
|
|||
.usage_error("'.' is a directory.");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(target_family = "unix")]
|
||||
fn test_more_invalid_file_perms() {
|
||||
use std::fs::{set_permissions, Permissions};
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
|
||||
if std::io::stdout().is_terminal() {
|
||||
let (at, mut ucmd) = at_and_ucmd!();
|
||||
let permissions = Permissions::from_mode(0o244);
|
||||
at.make_file("invalid-perms.txt");
|
||||
set_permissions(at.plus("invalid-perms.txt"), permissions).unwrap();
|
||||
ucmd.arg("invalid-perms.txt")
|
||||
.fails()
|
||||
.code_is(1)
|
||||
.stderr_contains("permission denied");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue