Merge pull request #305 from cakebaker/mcookie_dont_use_option_for_max_size

mcookie: don't use `Option` for `max_size`
This commit is contained in:
Sylvestre Ledru
2025-05-10 09:14:35 +02:00
committed by GitHub

View File

@@ -3,8 +3,6 @@
// For the full copyright and license information, please view the LICENSE // For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code. // file that was distributed with this source code.
#[cfg(unix)]
use std::os::unix::fs::FileTypeExt;
use std::{ use std::{
fs::File, fs::File,
io::{stdin, Read}, io::{stdin, Read},
@@ -51,14 +49,14 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
if s == 0 { if s == 0 {
s = MAX_DEFAULT; s = MAX_DEFAULT;
} }
Some(s) s
} }
Err(_) => { Err(_) => {
return Err(USimpleError::new(1, "Failed to parse max-size value")); return Err(USimpleError::new(1, "Failed to parse max-size value"));
} }
} }
} else { } else {
Some(MAX_DEFAULT) MAX_DEFAULT
}; };
let mut hasher = Md5::new(); let mut hasher = Md5::new();
@@ -69,14 +67,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
if file_path == "-" { if file_path == "-" {
input_name = "stdin"; input_name = "stdin";
let mut stdin_handle = stdin(); let stdin_handle = stdin();
if let Some(max_bytes) = &max_size { let mut limited_reader = stdin_handle.take(max_size);
let mut limited_reader = stdin_handle.take(*max_bytes); limited_reader.read_to_end(&mut buffer)?;
limited_reader.read_to_end(&mut buffer)?;
} else {
stdin_handle.read_to_end(&mut buffer)?;
}
} else { } else {
input_name = file_path; input_name = file_path;
let open_result = File::open(file_path); let open_result = File::open(file_path);
@@ -85,28 +79,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
continue; continue;
} }
let mut f = open_result.unwrap(); let f = open_result.unwrap();
if let Some(max_bytes) = &max_size { let mut limited_reader = f.take(max_size);
let mut limited_reader = f.take(*max_bytes); limited_reader.read_to_end(&mut buffer)?;
limited_reader.read_to_end(&mut buffer)?;
} else {
#[cfg(unix)]
{
const DEFAULT_SEED_READ_BYTES: u64 = 1024;
let metadata = f.metadata()?;
if metadata.file_type().is_char_device() {
let mut handle = f.take(DEFAULT_SEED_READ_BYTES);
handle.read_to_end(&mut buffer)?;
} else {
f.read_to_end(&mut buffer)?;
}
}
#[cfg(not(unix))]
{
f.read_to_end(&mut buffer)?;
}
}
} }
if verbose { if verbose {