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

Merge pull request #2934 from jfinkels/truncate-division-by-zero

truncate: add a division by zero error
This commit is contained in:
Sylvestre Ledru 2022-01-29 01:25:10 +01:00 committed by GitHub
commit c702154a84
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 78 additions and 47 deletions

View file

@ -6,17 +6,13 @@
// * file that was distributed with this source code.
// spell-checker:ignore (ToDO) RFILE refsize rfilename fsize tsize
#[macro_use]
extern crate uucore;
use clap::{crate_version, App, Arg};
use std::convert::TryFrom;
use std::fs::{metadata, OpenOptions};
use std::io::ErrorKind;
use std::path::Path;
use uucore::display::Quotable;
use uucore::error::{UIoError, UResult, USimpleError, UUsageError};
use uucore::error::{FromIo, UResult, USimpleError, UUsageError};
use uucore::parse_size::{parse_size, ParseSizeError};
#[derive(Debug, Eq, PartialEq)]
@ -113,24 +109,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let no_create = matches.is_present(options::NO_CREATE);
let reference = matches.value_of(options::REFERENCE).map(String::from);
let size = matches.value_of(options::SIZE).map(String::from);
truncate(no_create, io_blocks, reference, size, files).map_err(|e| {
match e.kind() {
ErrorKind::NotFound => {
// TODO Improve error-handling so that the error
// returned by `truncate()` provides the necessary
// parameter for formatting the error message.
let reference = matches.value_of(options::REFERENCE).map(String::from);
USimpleError::new(
1,
format!(
"cannot stat {}: No such file or directory",
reference.as_deref().unwrap_or("").quote()
),
) // TODO: fix '--no-create' see test_reference and test_truncate_bytes_size
}
_ => uio_error!(e, ""),
}
})
truncate(no_create, io_blocks, reference, size, files)
}
}
@ -212,20 +191,34 @@ fn truncate_reference_and_size(
size_string: &str,
filenames: Vec<String>,
create: bool,
) -> std::io::Result<()> {
) -> UResult<()> {
let mode = match parse_mode_and_size(size_string) {
Ok(m) => match m {
TruncateMode::Absolute(_) => {
crash!(1, "you must specify a relative '--size' with '--reference'")
}
_ => m,
},
Err(e) => crash!(1, "Invalid number: {}", e.to_string()),
Err(e) => return Err(USimpleError::new(1, format!("Invalid number: {}", e))),
Ok(TruncateMode::Absolute(_)) => {
return Err(USimpleError::new(
1,
String::from("you must specify a relative '--size' with '--reference'"),
))
}
Ok(m) => m,
};
let fsize = usize::try_from(metadata(rfilename)?.len()).unwrap();
if let TruncateMode::RoundDown(0) | TruncateMode::RoundUp(0) = mode {
return Err(USimpleError::new(1, "division by zero"));
}
let metadata = metadata(rfilename).map_err(|e| match e.kind() {
ErrorKind::NotFound => USimpleError::new(
1,
format!(
"cannot stat {}: No such file or directory",
rfilename.quote()
),
),
_ => e.map_err_context(String::new),
})?;
let fsize = metadata.len() as usize;
let tsize = mode.to_size(fsize);
for filename in &filenames {
file_truncate(filename, create, tsize)?;
file_truncate(filename, create, tsize).map_err_context(String::new)?;
}
Ok(())
}
@ -245,10 +238,20 @@ fn truncate_reference_file_only(
rfilename: &str,
filenames: Vec<String>,
create: bool,
) -> std::io::Result<()> {
let tsize = usize::try_from(metadata(rfilename)?.len()).unwrap();
) -> UResult<()> {
let metadata = metadata(rfilename).map_err(|e| match e.kind() {
ErrorKind::NotFound => USimpleError::new(
1,
format!(
"cannot stat {}: No such file or directory",
rfilename.quote()
),
),
_ => e.map_err_context(String::new),
})?;
let tsize = metadata.len() as usize;
for filename in &filenames {
file_truncate(filename, create, tsize)?;
file_truncate(filename, create, tsize).map_err_context(String::new)?;
}
Ok(())
}
@ -268,15 +271,12 @@ fn truncate_reference_file_only(
///
/// If the any file could not be opened, or there was a problem setting
/// the size of at least one file.
fn truncate_size_only(
size_string: &str,
filenames: Vec<String>,
create: bool,
) -> std::io::Result<()> {
let mode = match parse_mode_and_size(size_string) {
Ok(m) => m,
Err(e) => crash!(1, "Invalid number: {}", e.to_string()),
};
fn truncate_size_only(size_string: &str, filenames: Vec<String>, create: bool) -> UResult<()> {
let mode = parse_mode_and_size(size_string)
.map_err(|e| USimpleError::new(1, format!("Invalid number: {}", e)))?;
if let TruncateMode::RoundDown(0) | TruncateMode::RoundUp(0) = mode {
return Err(USimpleError::new(1, "division by zero"));
}
for filename in &filenames {
let fsize = match metadata(filename) {
Ok(m) => m.len(),
@ -286,7 +286,7 @@ fn truncate_size_only(
match file_truncate(filename, create, tsize) {
Ok(_) => continue,
Err(e) if e.kind() == ErrorKind::NotFound && !create => continue,
Err(e) => return Err(e),
Err(e) => return Err(e.map_err_context(String::new)),
}
}
Ok(())
@ -298,7 +298,7 @@ fn truncate(
reference: Option<String>,
size: Option<String>,
filenames: Vec<String>,
) -> std::io::Result<()> {
) -> UResult<()> {
let create = !no_create;
// There are four possibilities
// - reference file given and size given,

View file

@ -346,3 +346,34 @@ fn test_new_file_no_create() {
.no_stderr();
assert!(!at.file_exists(filename));
}
#[test]
fn test_division_by_zero_size_only() {
new_ucmd!()
.args(&["-s", "/0", "file"])
.fails()
.no_stdout()
.stderr_contains("division by zero");
new_ucmd!()
.args(&["-s", "%0", "file"])
.fails()
.no_stdout()
.stderr_contains("division by zero");
}
#[test]
fn test_division_by_zero_reference_and_size() {
let (at, mut ucmd) = at_and_ucmd!();
at.make_file(FILE1);
ucmd.args(&["-r", FILE1, "-s", "/0", "file"])
.fails()
.no_stdout()
.stderr_contains("division by zero");
let (at, mut ucmd) = at_and_ucmd!();
at.make_file(FILE1);
ucmd.args(&["-r", FILE1, "-s", "%0", "file"])
.fails()
.no_stdout()
.stderr_contains("division by zero");
}