1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-30 12:37:49 +00:00

Fix type-error when calling parse_size from truncate

This commit is contained in:
Gilad Naaman 2022-02-05 14:10:05 +02:00 committed by Omer Tuchfeld
parent 159a1dc1db
commit 6856c5dba5

View file

@ -7,7 +7,6 @@
// spell-checker:ignore (ToDO) RFILE refsize rfilename fsize tsize // spell-checker:ignore (ToDO) RFILE refsize rfilename fsize tsize
use clap::{crate_version, App, AppSettings, Arg}; use clap::{crate_version, App, AppSettings, Arg};
use std::convert::TryFrom;
use std::fs::{metadata, OpenOptions}; use std::fs::{metadata, OpenOptions};
use std::io::ErrorKind; use std::io::ErrorKind;
#[cfg(unix)] #[cfg(unix)]
@ -20,13 +19,13 @@ use uucore::parse_size::{parse_size, ParseSizeError};
#[derive(Debug, Eq, PartialEq)] #[derive(Debug, Eq, PartialEq)]
enum TruncateMode { enum TruncateMode {
Absolute(usize), Absolute(u64),
Extend(usize), Extend(u64),
Reduce(usize), Reduce(u64),
AtMost(usize), AtMost(u64),
AtLeast(usize), AtLeast(u64),
RoundDown(usize), RoundDown(u64),
RoundUp(usize), RoundUp(u64),
} }
impl TruncateMode { impl TruncateMode {
@ -55,7 +54,7 @@ impl TruncateMode {
/// let fsize = 3; /// let fsize = 3;
/// assert_eq!(mode.to_size(fsize), 0); /// assert_eq!(mode.to_size(fsize), 0);
/// ``` /// ```
fn to_size(&self, fsize: usize) -> usize { fn to_size(&self, fsize: u64) -> u64 {
match self { match self {
TruncateMode::Absolute(size) => *size, TruncateMode::Absolute(size) => *size,
TruncateMode::Extend(size) => fsize + size, TruncateMode::Extend(size) => fsize + size,
@ -192,10 +191,10 @@ pub fn uu_app<'a>() -> App<'a> {
/// ///
/// If the file could not be opened, or there was a problem setting the /// If the file could not be opened, or there was a problem setting the
/// size of the file. /// size of the file.
fn file_truncate(filename: &str, create: bool, size: usize) -> std::io::Result<()> { fn file_truncate(filename: &str, create: bool, size: u64) -> std::io::Result<()> {
let path = Path::new(filename); let path = Path::new(filename);
let f = OpenOptions::new().write(true).create(create).open(path)?; let f = OpenOptions::new().write(true).create(create).open(path)?;
f.set_len(u64::try_from(size).unwrap()) f.set_len(size)
} }
/// Truncate files to a size relative to a given file. /// Truncate files to a size relative to a given file.
@ -244,7 +243,7 @@ fn truncate_reference_and_size(
), ),
_ => e.map_err_context(String::new), _ => e.map_err_context(String::new),
})?; })?;
let fsize = metadata.len() as usize; let fsize = metadata.len();
let tsize = mode.to_size(fsize); let tsize = mode.to_size(fsize);
for filename in filenames { for filename in filenames {
#[cfg(unix)] #[cfg(unix)]
@ -292,7 +291,7 @@ fn truncate_reference_file_only(
), ),
_ => e.map_err_context(String::new), _ => e.map_err_context(String::new),
})?; })?;
let tsize = metadata.len() as usize; let tsize = metadata.len();
for filename in filenames { for filename in filenames {
#[cfg(unix)] #[cfg(unix)]
if std::fs::metadata(filename)?.file_type().is_fifo() { if std::fs::metadata(filename)?.file_type().is_fifo() {
@ -350,7 +349,7 @@ fn truncate_size_only(size_string: &str, filenames: &[String], create: bool) ->
} }
Err(_) => 0, Err(_) => 0,
}; };
let tsize = mode.to_size(fsize as usize); let tsize = mode.to_size(fsize);
match file_truncate(filename, create, tsize) { match file_truncate(filename, create, tsize) {
Ok(_) => continue, Ok(_) => continue,
Err(e) if e.kind() == ErrorKind::NotFound && !create => continue, Err(e) if e.kind() == ErrorKind::NotFound && !create => continue,