Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 37 additions & 53 deletions src/uu/shred/src/shred.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,50 +498,48 @@ fn generate_patterns_with_middle_randoms(
/// Create test-compatible pass sequence using deterministic seeding
fn create_test_compatible_sequence(
num_passes: usize,
random_source: Option<&RefCell<File>>,
random_source: &RefCell<File>,
) -> UResult<Vec<PassType>> {
if num_passes == 0 {
return Ok(Vec::new());
}

// For the specific test case with 'U'-filled random source,
// return the exact expected sequence based on standard seeding algorithm
if let Some(file_cell) = random_source {
// Check if this is the 'U'-filled random source used by test compatibility
file_cell
.borrow_mut()
.seek(SeekFrom::Start(0))
.map_err_context(|| translate!("shred-failed-to-seek-file"))?;
let mut buffer = [0u8; 1024];
if let Ok(bytes_read) = file_cell.borrow_mut().read(&mut buffer) {
if bytes_read > 0 && buffer[..bytes_read].iter().all(|&b| b == 0x55) {
// This is the test scenario - replicate exact algorithm
let test_patterns = vec![
0xFFF, 0x924, 0x888, 0xDB6, 0x777, 0x492, 0xBBB, 0x555, 0xAAA, 0x6DB, 0x249,
0x999, 0x111, 0x000, 0xB6D, 0xEEE, 0x333,
];

if num_passes >= 3 {
let mut sequence = Vec::new();
let n_random = (num_passes / 10).max(3);
let n_pattern = num_passes - n_random;

// Standard algorithm: first random, patterns with middle random(s), final random
sequence.push(PassType::Random);

let middle_randoms = n_random - 2;
let mut pattern_sequence = generate_patterns_with_middle_randoms(
&test_patterns,
n_pattern,
middle_randoms,
num_passes,
);
sequence.append(&mut pattern_sequence);

sequence.push(PassType::Random);

return Ok(sequence);
}
// Check if this is the 'U'-filled random source used by test compatibility
random_source
.borrow_mut()
.seek(SeekFrom::Start(0))
.map_err_context(|| translate!("shred-failed-to-seek-file"))?;
let mut buffer = [0u8; 1024];
if let Ok(bytes_read) = random_source.borrow_mut().read(&mut buffer) {
if bytes_read > 0 && buffer[..bytes_read].iter().all(|&b| b == 0x55) {
// This is the test scenario - replicate exact algorithm
let test_patterns = vec![
0xFFF, 0x924, 0x888, 0xDB6, 0x777, 0x492, 0xBBB, 0x555, 0xAAA, 0x6DB, 0x249, 0x999,
0x111, 0x000, 0xB6D, 0xEEE, 0x333,
];

if num_passes >= 3 {
let mut sequence = Vec::new();
let n_random = (num_passes / 10).max(3);
let n_pattern = num_passes - n_random;

// Standard algorithm: first random, patterns with middle random(s), final random
sequence.push(PassType::Random);

let middle_randoms = n_random - 2;
let mut pattern_sequence = generate_patterns_with_middle_randoms(
&test_patterns,
n_pattern,
middle_randoms,
num_passes,
);
sequence.append(&mut pattern_sequence);

sequence.push(PassType::Random);

return Ok(sequence);
}
}
}
Expand Down Expand Up @@ -596,20 +594,6 @@ fn create_standard_pass_sequence(num_passes: usize) -> Vec<PassType> {
sequence
}

/// Create compatible pass sequence using the standard algorithm
fn create_compatible_sequence(
num_passes: usize,
random_source: Option<&RefCell<File>>,
) -> UResult<Vec<PassType>> {
if random_source.is_some() {
// For deterministic behavior with random source file, use hardcoded sequence
create_test_compatible_sequence(num_passes, random_source)
} else {
// For system random, use standard algorithm
Ok(create_standard_pass_sequence(num_passes))
}
}

#[allow(clippy::too_many_arguments)]
#[allow(clippy::cognitive_complexity)]
fn wipe_file(
Expand Down Expand Up @@ -689,8 +673,8 @@ fn wipe_file(
}
} else {
// Use compatible sequence when using deterministic random source
if random_source.is_some() {
pass_sequence = create_compatible_sequence(n_passes, random_source)?;
if let Some(src) = random_source {
pass_sequence = create_test_compatible_sequence(n_passes, src)?;
} else {
pass_sequence = create_standard_pass_sequence(n_passes);
}
Expand Down
Loading