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
use clap::{crate_version, App, AppSettings, Arg};
use std::convert::TryFrom;
use std::fs::{metadata, OpenOptions};
use std::io::ErrorKind;
#[cfg(unix)]
@ -20,13 +19,13 @@ use uucore::parse_size::{parse_size, ParseSizeError};
#[derive(Debug, Eq, PartialEq)]
enum TruncateMode {
Absolute(usize),
Extend(usize),
Reduce(usize),
AtMost(usize),
AtLeast(usize),
RoundDown(usize),
RoundUp(usize),
Absolute(u64),
Extend(u64),
Reduce(u64),
AtMost(u64),
AtLeast(u64),
RoundDown(u64),
RoundUp(u64),
}
impl TruncateMode {
@ -55,7 +54,7 @@ impl TruncateMode {
/// let fsize = 3;
/// assert_eq!(mode.to_size(fsize), 0);
/// ```
fn to_size(&self, fsize: usize) -> usize {
fn to_size(&self, fsize: u64) -> u64 {
match self {
TruncateMode::Absolute(size) => *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
/// 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 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.
@ -244,7 +243,7 @@ fn truncate_reference_and_size(
),
_ => e.map_err_context(String::new),
})?;
let fsize = metadata.len() as usize;
let fsize = metadata.len();
let tsize = mode.to_size(fsize);
for filename in filenames {
#[cfg(unix)]
@ -292,7 +291,7 @@ fn truncate_reference_file_only(
),
_ => e.map_err_context(String::new),
})?;
let tsize = metadata.len() as usize;
let tsize = metadata.len();
for filename in filenames {
#[cfg(unix)]
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,
};
let tsize = mode.to_size(fsize as usize);
let tsize = mode.to_size(fsize);
match file_truncate(filename, create, tsize) {
Ok(_) => continue,
Err(e) if e.kind() == ErrorKind::NotFound && !create => continue,