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

shred: add support for hex and octal size

This commit is contained in:
John Shin 2023-05-29 20:10:16 -07:00
parent 2b3594a5f5
commit f8a46196ef
2 changed files with 22 additions and 31 deletions

View file

@ -19,6 +19,7 @@ use std::os::unix::prelude::PermissionsExt;
use std::path::{Path, PathBuf}; 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::parse_size::parse_size;
use uucore::{format_usage, help_about, help_section, help_usage, show, show_error, show_if_err}; use uucore::{format_usage, help_about, help_section, help_usage, show, show_error, show_if_err};
const ABOUT: &str = help_about!("shred.md"); const ABOUT: &str = help_about!("shred.md");
@ -318,38 +319,17 @@ pub fn uu_app() -> Command {
) )
} }
// TODO: Add support for all postfixes here up to and including EiB
// http://www.gnu.org/software/coreutils/manual/coreutils.html#Block-size
fn get_size(size_str_opt: Option<String>) -> Option<u64> { fn get_size(size_str_opt: Option<String>) -> Option<u64> {
size_str_opt.as_ref()?; match size_str_opt {
Some(size) => match parse_size(size.as_str()) {
let mut size_str = size_str_opt.as_ref().unwrap().clone(); Ok(res) => Some(res),
// Immutably look at last character of size string Err(_) => {
let unit = match size_str.chars().last().unwrap() { show_error!("invalid file size: {}", size.quote());
'K' => { std::process::exit(1)
size_str.pop(); }
1024u64 },
} None => None,
'M' => { }
size_str.pop();
(1024 * 1024) as u64
}
'G' => {
size_str.pop();
(1024 * 1024 * 1024) as u64
}
_ => 1u64,
};
let coefficient = match size_str.parse::<u64>() {
Ok(u) => u,
Err(_) => {
show_error!("{}: Invalid file size", size_str_opt.unwrap().maybe_quote());
std::process::exit(1);
}
};
Some(coefficient * unit)
} }
fn pass_name(pass_type: &PassType) -> String { fn pass_name(pass_type: &PassType) -> String {

View file

@ -51,3 +51,14 @@ fn test_shred_force() {
// file_a was deleted. // file_a was deleted.
assert!(!at.file_exists(file)); assert!(!at.file_exists(file));
} }
#[test]
fn test_hex() {
let (at, mut ucmd) = at_and_ucmd!();
let file = "test_hex";
at.touch(file);
ucmd.arg("--size=0x10").arg(file).succeeds();
}