mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-08-03 22:47:46 +00:00
Open file once for all passes, seek back to start between passes. Do not use File::create, because it truncates existing files
This commit is contained in:
parent
04d2134856
commit
4a42b981f9
1 changed files with 35 additions and 27 deletions
|
@ -15,8 +15,9 @@ extern crate rand;
|
|||
use rand::{ThreadRng, Rng};
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::fs;
|
||||
use std::fs::File;
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::io;
|
||||
use std::io::SeekFrom;
|
||||
use std::io::prelude::*;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
|
@ -367,7 +368,13 @@ fn wipe_file(path_str: &str, n_passes: usize, remove: bool,
|
|||
if zero {
|
||||
pass_sequence.push(PassType::Pattern(b"\x00"));
|
||||
}
|
||||
let total_passes: usize = n_passes + { if zero { 1 } else { 0 } };
|
||||
|
||||
{
|
||||
let total_passes: usize = pass_sequence.len();
|
||||
let mut file: File = OpenOptions::new().write(true)
|
||||
.truncate(false)
|
||||
.open(path)
|
||||
.expect("Failed to open file for writing");
|
||||
|
||||
for (i, pass_type) in pass_sequence.iter().enumerate() {
|
||||
if verbose {
|
||||
|
@ -383,7 +390,8 @@ fn wipe_file(path_str: &str, n_passes: usize, remove: bool,
|
|||
}
|
||||
}
|
||||
// size is an optional argument for exactly how many bytes we want to shred
|
||||
do_pass(path, *pass_type, size, exact).expect("File write pass failed"); // Ignore failed writes; just keep trying
|
||||
do_pass(&mut file, path, *pass_type, size, exact).expect("File write pass failed"); // Ignore failed writes; just keep trying
|
||||
}
|
||||
}
|
||||
|
||||
if remove {
|
||||
|
@ -391,21 +399,21 @@ fn wipe_file(path_str: &str, n_passes: usize, remove: bool,
|
|||
}
|
||||
}
|
||||
|
||||
fn do_pass(path: &Path, generator_type: PassType,
|
||||
fn do_pass(file: &mut File, path: &Path, generator_type: PassType,
|
||||
given_file_size: Option<u64>, exact: bool) -> Result<(), io::Error> {
|
||||
|
||||
try!(file.seek(SeekFrom::Start(0)));
|
||||
|
||||
// Use the given size or the whole file if not specified
|
||||
let size: u64 = given_file_size.unwrap_or(try!(get_file_size(path)));
|
||||
|
||||
let generator = BytesGenerator::new(size, generator_type, exact);
|
||||
|
||||
let mut file: File = try!(File::create(path));
|
||||
|
||||
for block in generator {
|
||||
try!(file.write_all(&*block));
|
||||
}
|
||||
|
||||
try!(file.sync_all());
|
||||
try!(file.sync_data());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -441,7 +449,7 @@ fn wipe_name(orig_path: &Path, verbose: bool) -> Option<PathBuf> {
|
|||
|
||||
// Sync every file rename
|
||||
{
|
||||
let new_file: File = File::create(new_path.clone()).expect("Failed to open renamed file for syncing");
|
||||
let new_file: File = File::open(new_path.clone()).expect("Failed to open renamed file for syncing");
|
||||
new_file.sync_all().expect("Failed to sync renamed file");
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue