1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

shred: use show_error instead of println for verbose logs

This commit is contained in:
Terts Diepraam 2023-03-10 21:05:14 +01:00
parent 015de72931
commit cec92046ca

View file

@ -22,7 +22,22 @@ use std::path::{Path, PathBuf};
use uucore::display::Quotable; use uucore::display::Quotable;
use uucore::error::{FromIo, UResult, USimpleError, UUsageError}; use uucore::error::{FromIo, UResult, USimpleError, UUsageError};
use uucore::libc::S_IWUSR; use uucore::libc::S_IWUSR;
use uucore::{format_usage, help_about, help_section, help_usage, show, show_if_err, util_name}; use uucore::{format_usage, help_about, help_section, help_usage, show, show_error, show_if_err};
const ABOUT: &str = help_about!("shred.md");
const USAGE: &str = help_usage!("shred.md");
const AFTER_HELP: &str = help_section!("after help", "shred.md");
pub mod options {
pub const FORCE: &str = "force";
pub const FILE: &str = "file";
pub const ITERATIONS: &str = "iterations";
pub const SIZE: &str = "size";
pub const REMOVE: &str = "remove";
pub const VERBOSE: &str = "verbose";
pub const EXACT: &str = "exact";
pub const ZERO: &str = "zero";
}
// This block size seems to match GNU (2^16 = 65536) // This block size seems to match GNU (2^16 = 65536)
const BLOCK_SIZE: usize = 1 << 16; const BLOCK_SIZE: usize = 1 << 16;
@ -188,21 +203,6 @@ impl BytesWriter {
} }
} }
const ABOUT: &str = help_about!("shred.md");
const USAGE: &str = help_usage!("shred.md");
const AFTER_HELP: &str = help_section!("after help", "shred.md");
pub mod options {
pub const FORCE: &str = "force";
pub const FILE: &str = "file";
pub const ITERATIONS: &str = "iterations";
pub const SIZE: &str = "size";
pub const REMOVE: &str = "remove";
pub const VERBOSE: &str = "verbose";
pub const EXACT: &str = "exact";
pub const ZERO: &str = "zero";
}
#[uucore::main] #[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> { pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let args = args.collect_ignore(); let args = args.collect_ignore();
@ -347,11 +347,7 @@ fn get_size(size_str_opt: Option<String>) -> Option<u64> {
let coefficient = match size_str.parse::<u64>() { let coefficient = match size_str.parse::<u64>() {
Ok(u) => u, Ok(u) => u,
Err(_) => { Err(_) => {
println!( show_error!("{}: Invalid file size", size_str_opt.unwrap().maybe_quote());
"{}: {}: Invalid file size",
util_name(),
size_str_opt.unwrap().maybe_quote()
);
std::process::exit(1); std::process::exit(1);
} }
}; };
@ -465,18 +461,16 @@ fn wipe_file(
if verbose { if verbose {
let pass_name: String = pass_name(&pass_type); let pass_name: String = pass_name(&pass_type);
if total_passes.to_string().len() == 1 { if total_passes.to_string().len() == 1 {
println!( show_error!(
"{}: {}: pass {}/{} ({})... ", "{}: pass {}/{} ({})... ",
util_name(),
path.maybe_quote(), path.maybe_quote(),
i + 1, i + 1,
total_passes, total_passes,
pass_name pass_name
); );
} else { } else {
println!( show_error!(
"{}: {}: pass {:2.0}/{:2.0} ({})... ", "{}: pass {:2.0}/{:2.0} ({})... ",
util_name(),
path.maybe_quote(), path.maybe_quote(),
i + 1, i + 1,
total_passes, total_passes,
@ -535,13 +529,13 @@ fn get_file_size(path: &Path) -> Result<u64, io::Error> {
// Repeatedly renames the file with strings of decreasing length (most likely all 0s) // Repeatedly renames the file with strings of decreasing length (most likely all 0s)
// Return the path of the file after its last renaming or None if error // Return the path of the file after its last renaming or None if error
fn wipe_name(orig_path: &Path, verbose: bool) -> Option<PathBuf> { fn wipe_name(orig_path: &Path, verbose: bool) -> Option<PathBuf> {
let file_name_len: usize = orig_path.file_name().unwrap().to_str().unwrap().len(); let file_name_len = orig_path.file_name().unwrap().to_str().unwrap().len();
let mut last_path: PathBuf = PathBuf::from(orig_path); let mut last_path = PathBuf::from(orig_path);
for length in (1..=file_name_len).rev() { for length in (1..=file_name_len).rev() {
for name in FilenameGenerator::new(length) { for name in FilenameGenerator::new(length) {
let new_path: PathBuf = orig_path.with_file_name(name); let new_path = orig_path.with_file_name(name);
// We don't want the filename to already exist (don't overwrite) // We don't want the filename to already exist (don't overwrite)
// If it does, find another name that doesn't // If it does, find another name that doesn't
if new_path.exists() { if new_path.exists() {
@ -550,28 +544,24 @@ fn wipe_name(orig_path: &Path, verbose: bool) -> Option<PathBuf> {
match fs::rename(&last_path, &new_path) { match fs::rename(&last_path, &new_path) {
Ok(()) => { Ok(()) => {
if verbose { if verbose {
println!( show_error!(
"{}: {}: renamed to {}", "{}: renamed to {}",
util_name(),
last_path.maybe_quote(), last_path.maybe_quote(),
new_path.quote() new_path.quote()
); );
} }
// Sync every file rename // Sync every file rename
{ let new_file = File::open(new_path.clone())
let new_file: File = File::open(new_path.clone()) .expect("Failed to open renamed file for syncing");
.expect("Failed to open renamed file for syncing"); new_file.sync_all().expect("Failed to sync renamed file");
new_file.sync_all().expect("Failed to sync renamed file");
}
last_path = new_path; last_path = new_path;
break; break;
} }
Err(e) => { Err(e) => {
println!( show_error!(
"{}: {}: Couldn't rename to {}: {}", "{}: Couldn't rename to {}: {}",
util_name(),
last_path.maybe_quote(), last_path.maybe_quote(),
new_path.quote(), new_path.quote(),
e e
@ -587,16 +577,15 @@ fn wipe_name(orig_path: &Path, verbose: bool) -> Option<PathBuf> {
fn do_remove(path: &Path, orig_filename: &str, verbose: bool) -> Result<(), io::Error> { fn do_remove(path: &Path, orig_filename: &str, verbose: bool) -> Result<(), io::Error> {
if verbose { if verbose {
println!("{}: {}: removing", util_name(), orig_filename.maybe_quote()); show_error!("{}: removing", orig_filename.maybe_quote());
} }
let renamed_path: Option<PathBuf> = wipe_name(path, verbose); if let Some(rp) = wipe_name(path, verbose) {
if let Some(rp) = renamed_path {
fs::remove_file(rp)?; fs::remove_file(rp)?;
} }
if verbose { if verbose {
println!("{}: {}: removed", util_name(), orig_filename.maybe_quote()); show_error!("{}: removed", orig_filename.maybe_quote());
} }
Ok(()) Ok(())