FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

shred: report the real error for an inaccessible file by 0xfandom · Pull Request #13640 · uutils/coreutils · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .rs  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
31 changes: 20 additions & 11 deletions src/uu/shred/src/shred.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -624,17 +624,26 @@ fn wipe_file(
}
}

if !path.exists() {
return Err(USimpleError::new(
1,
translate!("shred-no-such-file-or-directory", "file" => path.maybe_quote()),
));
}
if !path.is_file() {
return Err(USimpleError::new(
1,
translate!("shred-not-a-file", "file" => path.maybe_quote()),
));
// `Path::exists()` and `Path::is_file()` both collapse any metadata error
// (including a permission error) into `false`, which made shred report a
// file whose parent directory lacks search permission as "No such file or
// directory". Inspect the metadata directly so a genuine `ENOENT` stays a
// "no such file" error while a permission error falls through to the
// open-for-writing below, which surfaces the real reason.
match fs::metadata(path) {
Ok(md) if !md.is_file() => {
return Err(USimpleError::new(
1,
translate!("shred-not-a-file", "file" => path.maybe_quote()),
));
}
Err(err) if err.kind() == io::ErrorKind::NotFound => {
return Err(USimpleError::new(
1,
translate!("shred-no-such-file-or-directory", "file" => path.maybe_quote()),
));
}
_ => {}
}

let metadata =
Expand Down
28 changes: 28 additions & 0 deletions tests/by-util/test_shred.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -470,3 +470,31 @@ fn test_shred_trailing_slash_on_dir() {
.fails()
.stderr_contains("Is a directory");
}

#[test]
#[cfg(unix)]
fn test_shred_inaccessible_file_reports_real_error() {
// A file whose parent directory lacks search permission must not be
// misreported as "No such file or directory": `Path::exists()` and
// `Path::is_file()` collapse the permission error into `false`.
use std::fs::{Permissions, set_permissions};
use std::os::unix::fs::PermissionsExt;

let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;

at.mkdir("locked");
at.touch("locked/file");
set_permissions(at.plus_as_string("locked"), Permissions::from_mode(0o000)).unwrap();

let result = scene.ucmd().arg("locked/file").fails();
result.stderr_contains("Permission denied");
assert!(
!result.stderr_str().contains("No such file"),
"shred misreported an inaccessible file as missing: {}",
result.stderr_str()
);

// Restore search permission so the fixture directory can be cleaned up.
set_permissions(at.plus_as_string("locked"), Permissions::from_mode(0o755)).unwrap();
}
Loading

Back | FazBrowse Home | New Git URL