Skip to content

shred: report directories as 'failed to open for writing: Is a directory' - #15631

Open
emirmrkaljevic wants to merge 2 commits into
uutils:mainfrom
emirmrkaljevic:shred-dir-message
Open

emirmrkaljevic wants to merge 2 commits into
uutils:mainfrom
emirmrkaljevic:shred-dir-message

Conversation

@emirmrkaljevic

Copy link
Copy Markdown
Contributor

Fixes #15630

shred d on a directory now prints shred: d: failed to open for writing: Is a directory (exit 1), matching GNU and the existing trailing-slash case. Other non-regular files keep the Not a file message.

Tests: new test_shred_dir_without_trailing_slash (fails on main, passes with the fix) and the 007 case of test_numeric_looking_file_names_are_kept_verbatim updated to the GNU wording. All 31 shred tests pass, clippy and fmt clean.

Found with behave.

Comment thread src/uu/shred/src/shred.rs Outdated
// "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_dir() => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be caught as EISDIR wneh we actually write instead of calling stat.

Comment thread src/uu/shred/src/shred.rs Outdated
// "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_dir() => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with this, the path.is_dir() check in the trailing slash block above is redundant, could you please remove it?

Comment thread tests/by-util/test_shred.rs Outdated
}

#[test]
fn test_shred_dir_without_trailing_slash() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

almost the same as test_shred_trailing_slash_on_dir, could be merged into a rstest with d and d/ (and stderr_only there too), no?

Comment thread tests/by-util/test_shred.rs Outdated

#[test]
fn test_shred_dir_without_trailing_slash() {
// GNU: "shred: d: failed to open for writing: Is a directory", exit 1

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the comment seems unnecessary

no need to reference GNU; BSD shred behaves the same way

@emirmrkaljevic

Copy link
Copy Markdown
Contributor Author

Thanks for the reviews! Updated in f1567cc (rebased on main):

  • directories are no longer special-cased: they skip the "Not a file" branch and fail when opened for writing, so the message comes from the real EISDIR (@oech3)
  • removed the path.is_dir() check in the trailing-slash block and the now unused shred-failed-to-open-for-writing-is-a-directory string (@sylvestre)
  • merged the two directory tests into one rstest with d and d/, using stderr_only, and dropped the GNU comment (@xtqqczze)

I also noticed #15182 touches the same area (FIFOs etc.); happy to rebase on top of it or close this one if you prefer that PR.

Comment thread src/uu/shred/src/shred.rs Outdated
));
}
if path_str.as_encoded_bytes().ends_with(b"/")
&& fs::metadata(path).is_err_and(|e| e.kind() == io::ErrorKind::NotADirectory)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you extracted it as e, uucore::error::strip_errno should provide same error message.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 42e21b1, thanks!

@github-actions

Copy link
Copy Markdown

Binary size comparison:

Individual binary size comparison VS main (threshold: >=5% AND >=4 KB).

Total size of compared binaries: 155.04 MB (-312 KB, -0.20%)

Significant per-binary changes:
  date     2.61 MB ->    2.30 MB  (-316 KB, -11.81%)

Build the 'failed to open for writing' message from the io::Error with
strip_errno instead of a dedicated 'Not a directory' string, and drop the
now unused locale key.
Comment thread src/uu/shred/src/shred.rs
@@ -657,7 +656,8 @@ fn wipe_file(
// "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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR does not need to do that. But we should remove usage of metadata (or do after I/O error) and use proper inspection for errno.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

shred: directory argument reports 'Not a file' instead of GNU's 'failed to open for writing: Is a directory'

4 participants