1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-09-14 11:07:59 +00:00

shred: add 4K data alignment

This commit allows aligning output data by 4K to better match GNU shred.

The 4K block size is configured as a constant because it provides a
widely used value in the simplest way. However, there is a chance that
some systems may use a different value. So far, I haven't encountered
anything other than 4K, I decided not to overcomplicate the approach for
now.
This commit is contained in:
Alexander Shirokov 2025-05-11 13:10:27 +02:00 committed by Ben Wiederhake
parent 05c161cbb5
commit 745d2add08

View file

@ -507,6 +507,11 @@ fn wipe_file(
Ok(()) Ok(())
} }
// Aligns data size up to the nearest multiple of block size
fn get_aligned_size(data_size: usize, block_size: usize) -> usize {
data_size.div_ceil(block_size) * block_size
}
fn do_pass( fn do_pass(
file: &mut File, file: &mut File,
pass_type: &PassType, pass_type: &PassType,
@ -525,10 +530,16 @@ fn do_pass(
} }
// Now we might have some bytes left, so we write either that // Now we might have some bytes left, so we write either that
// many bytes if exact is true, or BLOCK_SIZE bytes if not. // many bytes if exact is true, or aligned by FS_BLOCK_SIZE bytes if not.
let bytes_left = (file_size % BLOCK_SIZE as u64) as usize; let bytes_left = (file_size % BLOCK_SIZE as u64) as usize;
if bytes_left > 0 { if bytes_left > 0 {
let size = if exact { bytes_left } else { BLOCK_SIZE }; let size = if exact {
bytes_left
} else {
// This alignment allows us to better match GNU shred's behavior.
const FS_BLOCK_SIZE: usize = 4096;
get_aligned_size(bytes_left, FS_BLOCK_SIZE)
};
let block = writer.bytes_for_pass(size); let block = writer.bytes_for_pass(size);
file.write_all(block)?; file.write_all(block)?;
} }