Skip to content

Commit 745d2ad

Browse files
alexs-shBenWiederhake
authored andcommitted
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.
1 parent 05c161c commit 745d2ad

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/uu/shred/src/shred.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,11 @@ fn wipe_file(
507507
Ok(())
508508
}
509509

510+
// Aligns data size up to the nearest multiple of block size
511+
fn get_aligned_size(data_size: usize, block_size: usize) -> usize {
512+
data_size.div_ceil(block_size) * block_size
513+
}
514+
510515
fn do_pass(
511516
file: &mut File,
512517
pass_type: &PassType,
@@ -525,10 +530,16 @@ fn do_pass(
525530
}
526531

527532
// Now we might have some bytes left, so we write either that
528-
// many bytes if exact is true, or BLOCK_SIZE bytes if not.
533+
// many bytes if exact is true, or aligned by FS_BLOCK_SIZE bytes if not.
529534
let bytes_left = (file_size % BLOCK_SIZE as u64) as usize;
530535
if bytes_left > 0 {
531-
let size = if exact { bytes_left } else { BLOCK_SIZE };
536+
let size = if exact {
537+
bytes_left
538+
} else {
539+
// This alignment allows us to better match GNU shred's behavior.
540+
const FS_BLOCK_SIZE: usize = 4096;
541+
get_aligned_size(bytes_left, FS_BLOCK_SIZE)
542+
};
532543
let block = writer.bytes_for_pass(size);
533544
file.write_all(block)?;
534545
}

0 commit comments

Comments
 (0)