Skip to content

shred: truncate file to zero before unlinking - #12298

Open
gabrielhnf wants to merge 3 commits into
uutils:mainfrom
gabrielhnf:fix-shred-deallocate-before-removal
Open

gabrielhnf wants to merge 3 commits into
uutils:mainfrom
gabrielhnf:fix-shred-deallocate-before-removal

Conversation

@gabrielhnf

Copy link
Copy Markdown
Contributor

Fixes #12287

When -u is given, shred now truncates the file to zero before attempting to unlink it. This ensures the file's allocated blocks are freed even when the parent directory is not writable (which would prevent unlinking).

@github-actions

Copy link
Copy Markdown

GNU testsuite comparison:

GNU test failed: tests/misc/tty-eof. tests/misc/tty-eof is passing on 'main'. Maybe you have to rebase?
Skip an intermittent issue tests/date/date-locale-hour (fails in this run but passes in the 'main' branch)
Congrats! The gnu test tests/expand/bounded-memory is now passing!
Congrats! The gnu test tests/printf/printf-surprise is now passing!

@sylvestre
sylvestre force-pushed the fix-shred-deallocate-before-removal branch from 0c130f8 to ce9318b Compare May 16, 2026 20:18
Comment thread src/uu/shred/src/shred.rs
}

if remove_method != RemoveMethod::None {
file.set_len(0).map_err_context(

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.

a truncate failure reported as "failed to remove file" is a bit misleading, could you please use a dedicated 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, thanks!

Comment thread tests/by-util/test_shred.rs Outdated
let file_path = "truncate_test/file";

at.mkdir(dir);
// Create a 1MB file

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.

does it need to be 1MB? a few KB would be enough and faster :)

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.

Reduced to 4KB, thanks!

@sylvestre

Copy link
Copy Markdown
Contributor

sorry but it needs to be rebased

@gabrielhnf
gabrielhnf force-pushed the fix-shred-deallocate-before-removal branch from ce9318b to fd0842e Compare October 20, 2026 15:27
@gabrielhnf

Copy link
Copy Markdown
Contributor Author

Rebased onto the latest main. I've also addressed your review comments regarding the truncation error message and test file size. Thank you very much for the review!

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 -u should deallocate file independently from dir permissions

2 participants