Skip to content

shred: support character and block devices - #12296

Open
gabrielhnf wants to merge 1 commit into
uutils:mainfrom
gabrielhnf:fix-shred-enospc-and-unreadable-files
Open

gabrielhnf wants to merge 1 commit into
uutils:mainfrom
gabrielhnf:fix-shred-enospc-and-unreadable-files

Conversation

@gabrielhnf

Copy link
Copy Markdown
Contributor

Fixes #12282 and #12283

shred was rejecting character and block devices with "not a file" error, and doing nothing on them even when they were valid targets. wipe_file used path.is_file() to validate the target, which returns false for all non-regular files, including devices. Additionally, the pass sequence was skipped entirely when metadata.len() == 0, which is always the case for devices.

  • Replaced path.is_file() with a check that also accepts character and block devices on Unix
  • Character devices now use a dedicated write loop that treats ENOSPC as success if no --size argument was given
  • Pass sequences are now always built for devices regardless of reported size
  • Block device size is now determined by seeking to the end of the file
  • --exact is enforced automatically for non-regular files since they have no meaningful block alignment

@github-actions

github-actions Bot commented May 14, 2026 •

Copy link
Copy Markdown

GNU testsuite comparison:

GNU test failed: tests/tail/pipe-f2. tests/tail/pipe-f2 is passing on 'main'. Maybe you have to rebase?
Skip an intermittent issue tests/cut/bounded-memory (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!

@gabrielhnf
gabrielhnf force-pushed the fix-shred-enospc-and-unreadable-files branch from 2e2cc85 to b0f36b7 Compare May 14, 2026 19:27
@codspeed

codspeed Bot commented May 14, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will degrade performance by 19.55%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

❌ 1 regressed benchmark
✅ 316 untouched benchmarks
⏩ 46 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ Memory cp_recursive_deep_tree[(120, 4)] 562.5 KB 699.2 KB -19.55%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing gabrielhnf:fix-shred-enospc-and-unreadable-files (b0f36b7) with main (6a942ba)

Open in CodSpeed

Footnotes

  1. 46 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@relative23

Copy link
Copy Markdown
Contributor

I hit this bug wiping a real disk, found this PR, and verified your approach
against GNU shred 9.11 on a loop device. It is right, including the part that
is easy to miss: is_file() rejects devices, and metadata.len() reports 0
for a block device, so without the pass-sequence change nothing would be
written even once the type check lets it through.

Reference numbers, measured through /sys/block/*/stat on a 64 MiB loop
device:

invocation GNU 9.11 this approach
-n 1 67108864 B 67108864 B
-n 3 201326592 B 201326592 B
-n 1 --size=1M 1048576 B 1048576 B
-n 2 -z 201326592 B 201326592 B

So GNU writes exactly the device length and does not round the last block up,
which confirms that forcing --exact for devices is correct. /dev/full and
/dev/null --size match GNU's exit status too.

The branch sits on main as of 2026-05-10 and git merge-tree --write-tree now
exits 1, but the rebase is cheap: the only conflict is in
tests/by-util/test_shred.rs. src/uu/shred/src/shred.rs still merges
automatically and the merged result compiles. Worth re-running the suite after
the rebase anyway, since main has reworked the surrounding code: it dropped
create_compatible_sequence, made the random source non-optional in
create_test_compatible_sequence, and removed a parameter from do_remove.

Two things I would add while you are in there:

  1. Nothing tests that a FIFO is still rejected. Widening the type check is
    exactly where that could regress, and it would be silent: GNU blocks on the
    open for a FIFO rather than failing, so uutils rejecting it is the better
    behaviour to keep.
  2. The CodSpeed report on this PR says 19.55% with a "different runtime
    environments" warning. That pattern has been noise on other PRs, so it is
    probably worth a re-run rather than a code change.

I have the same approach ported to current main from testing it, so if you have
moved on I can open that with credit to this PR. If you are still on it, a
rebase is all it needs.

@gabrielhnf
gabrielhnf force-pushed the fix-shred-enospc-and-unreadable-files branch 2 times, most recently from c08af2d to e11bbf6 Compare October 23, 2026 16:51
@gabrielhnf

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed testing and feedback! I've rebased the PR onto the latest main and added a regression test to ensure FIFOs remain rejected, as you suggested.

@RenjiSann

Copy link
Copy Markdown
Collaborator

This needs another rebase

@RenjiSann
RenjiSann self-requested a review October 26, 2026 00:38
@gabrielhnf
gabrielhnf force-pushed the fix-shred-enospc-and-unreadable-files branch from e11bbf6 to 6e67b86 Compare October 26, 2026 02:51
Comment thread tests/by-util/test_shred.rs Resolved
@gabrielhnf
gabrielhnf force-pushed the fix-shred-enospc-and-unreadable-files branch from 5dfb443 to 0782457 Compare October 26, 2026 14:59
@oech3

oech3 commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

I think we should not use any metadata at shred. We shloud always try shreding and inspect errno for correctness and less overheads.

@gabrielhnf

Copy link
Copy Markdown
Contributor Author

I see your point, and avoiding metadata checks could reduce some overhead. But shred needs to behave differently depending on the file type, and I'm not sure errno alone would provide enough information to make those distinctions.

Maybe I'm missing something, though.
How would you approach this without relying on metadata?

@oech3

oech3 commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

We could strace shread input and see open's flag and monitor stat-related calls.

@gabrielhnf

Copy link
Copy Markdown
Contributor Author

I ran strace on GNU shred with both a regular file and a FIFO.

For regular files, GNU opens with O_NONBLOCK, clears it with fcntl(), and then calls fstat() to get the file metadata.
For FIFOs without a reader, openat() returns ENXIO, and GNU follows up with newfstatat() to identify the file type.

So GNU does still rely on metadata, but I agree that our repeated metadata calls could be avoided.
For now, though, I'd prefer to keep it focused on device support and leave that optimization for a follow-up. What do you think?

@sylvestre
sylvestre force-pushed the fix-shred-enospc-and-unreadable-files branch from 0782457 to a5e1611 Compare November 1, 2026 17:40
@gabrielhnf
gabrielhnf force-pushed the fix-shred-enospc-and-unreadable-files branch from a5e1611 to 102f04f Compare November 6, 2026 12:58
@sylvestre
sylvestre force-pushed the fix-shred-enospc-and-unreadable-files branch from 102f04f to 8fd7cdb Compare November 8, 2026 12:01
@gabrielhnf
gabrielhnf force-pushed the fix-shred-enospc-and-unreadable-files branch from 8fd7cdb to 78a7631 Compare November 9, 2026 18:24
@gabrielhnf
gabrielhnf force-pushed the fix-shred-enospc-and-unreadable-files branch from 78a7631 to 1d5a965 Compare November 11, 2026 13:55

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 /dev/full should success

4 participants