@@ -51,8 +51,7 @@ const PATTERN_BUFFER_SIZE: usize = BLOCK_SIZE + PATTERN_LENGTH - 1;
51
51
52
52
/// Patterns that appear in order for the passes
53
53
///
54
- /// They are all extended to 3 bytes for consistency, even though some could be
55
- /// expressed as single bytes.
54
+ /// A single-byte pattern is equivalent to a multi-byte pattern of that byte three times.
56
55
const PATTERNS : [ Pattern ; 22 ] = [
57
56
Pattern :: Single ( b'\x00' ) ,
58
57
Pattern :: Single ( b'\xFF' ) ,
@@ -440,9 +439,13 @@ fn wipe_file(
440
439
pass_sequence. push ( PassType :: Random ) ;
441
440
}
442
441
} else {
443
- // First fill it with Patterns, shuffle it, then evenly distribute Random
444
- let n_full_arrays = n_passes / PATTERNS . len ( ) ; // How many times can we go through all the patterns?
445
- let remainder = n_passes % PATTERNS . len ( ) ; // How many do we get through on our last time through?
442
+ // Add initial random to avoid O(n) operation later
443
+ pass_sequence. push ( PassType :: Random ) ;
444
+ let n_random = ( n_passes / 10 ) . max ( 3 ) ; // Minimum 3 random passes; ratio of 10 after
445
+ let n_fixed = n_passes - n_random;
446
+ // Fill it with Patterns and all but the first and last random, then shuffle it
447
+ let n_full_arrays = n_fixed / PATTERNS . len ( ) ; // How many times can we go through all the patterns?
448
+ let remainder = n_fixed % PATTERNS . len ( ) ; // How many do we get through on our last time through, excluding randoms?
446
449
447
450
for _ in 0 ..n_full_arrays {
448
451
for p in PATTERNS {
@@ -452,14 +455,14 @@ fn wipe_file(
452
455
for pattern in PATTERNS . into_iter ( ) . take ( remainder) {
453
456
pass_sequence. push ( PassType :: Pattern ( pattern) ) ;
454
457
}
455
- let mut rng = rand:: rng ( ) ;
456
- pass_sequence. shuffle ( & mut rng) ; // randomize the order of application
457
-
458
- let n_random = 3 + n_passes / 10 ; // Minimum 3 random passes; ratio of 10 after
459
- // Evenly space random passes; ensures one at the beginning and end
460
- for i in 0 ..n_random {
461
- pass_sequence[ i * ( n_passes - 1 ) / ( n_random - 1 ) ] = PassType :: Random ;
458
+ // add random passes except one each at the beginning and end
459
+ for _ in 0 ..n_random - 2 {
460
+ pass_sequence. push ( PassType :: Random ) ;
462
461
}
462
+
463
+ let mut rng = rand:: rng ( ) ;
464
+ pass_sequence[ 1 ..] . shuffle ( & mut rng) ; // randomize the order of application
465
+ pass_sequence. push ( PassType :: Random ) ; // add the last random pass
463
466
}
464
467
465
468
// --zero specifies whether we want one final pass of 0x00 on our file
0 commit comments