Skip to content

Commit

Permalink
use u64 consistently for blksize. rename sane_blksize.
Browse files Browse the repository at this point in the history
  • Loading branch information
cre4ture committed Feb 7, 2024
1 parent 930c3af commit 8e51b86
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/uu/head/src/head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ fn is_seekable(input: &mut std::fs::File) -> bool {
fn head_backwards_file(input: &mut std::fs::File, options: &HeadOptions) -> std::io::Result<()> {
let st = input.metadata()?;
let seekable = is_seekable(input);
let blksize_limit = uucore::fs::get_sanity_limited_blksize(&st) as u64;
let blksize_limit = uucore::fs::sane_blksize::sane_blksize_from_st(&st);
if !seekable || st.len() <= blksize_limit {
return head_backwards_without_seek_file(input, options);
}
Expand Down
17 changes: 6 additions & 11 deletions src/uu/split/src/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ struct Settings {
/// chunks. If this is `false`, then empty files will not be
/// created.
elide_empty_files: bool,
io_blksize: Option<usize>,
io_blksize: Option<u64>,
}

/// An error when parsing settings from command-line arguments.
Expand Down Expand Up @@ -500,12 +500,9 @@ impl Settings {
None => b'\n',
};

let io_blksize: Option<usize> = if let Some(s) = matches.get_one::<String>(OPT_IO_BLKSIZE) {
let io_blksize: Option<u64> = if let Some(s) = matches.get_one::<String>(OPT_IO_BLKSIZE) {
match parse_size_u64(s) {
Ok(n) => {
let n: usize = n
.try_into()
.map_err(|_| SettingsError::InvalidIOBlockSize(s.to_string()))?;
if n > uucore::fs::sane_blksize::MAX {
return Err(SettingsError::InvalidIOBlockSize(s.to_string()));
}
Expand Down Expand Up @@ -633,7 +630,7 @@ fn get_input_size<R>(
input: &String,
reader: &mut R,
buf: &mut Vec<u8>,
io_blksize: &Option<usize>,
io_blksize: &Option<u64>,
) -> std::io::Result<u64>
where
R: BufRead,
Expand All @@ -643,10 +640,8 @@ where
*custom_blksize
} else {
// otherwise try to get it from filesystem, or use default
uucore::fs::get_sanity_limited_blksize_from_path(Path::new(input))
}
.try_into()
.unwrap();
uucore::fs::sane_blksize::sane_blksize_from_path(Path::new(input))
};

// Try to read into buffer up to a limit
let num_bytes = reader
Expand Down Expand Up @@ -1629,7 +1624,7 @@ fn split(settings: &Settings) -> UResult<()> {
Box::new(r) as Box<dyn Read>
};
let mut reader = if let Some(c) = settings.io_blksize {
BufReader::with_capacity(c, r_box)
BufReader::with_capacity(c.try_into().unwrap(), r_box)
} else {
BufReader::new(r_box)
};
Expand Down
66 changes: 42 additions & 24 deletions src/uucore/src/lib/features/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use std::collections::VecDeque;
use std::env;
use std::ffi::{OsStr, OsString};
use std::fs;
use std::fs::metadata;
use std::fs::read_dir;
use std::hash::Hash;
use std::io::{Error, ErrorKind, Result as IOResult};
Expand Down Expand Up @@ -745,38 +744,48 @@ pub fn path_ends_with_terminator(path: &Path) -> bool {
}

pub mod sane_blksize {
pub const DEFAULT: usize = 512;
pub const MAX: usize = (u32::MAX / 8 + 1) as usize;
}

/// Takes the blksize information from file metadata and limits it
/// if needed.
pub fn get_sanity_limited_blksize(_st: &std::fs::Metadata) -> usize {
#[cfg(not(target_os = "windows"))]
{
const MAX_U64: u64 = sane_blksize::MAX as u64;
let st_blksize: u64 = _st.blksize();
use std::os::unix::fs::MetadataExt;
use std::{fs::metadata, path::Path};

pub const DEFAULT: u64 = 512;
pub const MAX: u64 = (u32::MAX / 8 + 1) as u64;

/// Provides sanity checked blksize value from the provided value.
/// If the provided value is a invalid values a meaningful adaption
/// of that value is done.
pub fn sane_blksize(st_blksize: u64) -> u64 {
match st_blksize {
0 => sane_blksize::DEFAULT,
1..=MAX_U64 => st_blksize.try_into().unwrap(),
_ => sane_blksize::DEFAULT,
0 => DEFAULT,
1..=MAX => st_blksize,
_ => DEFAULT,
}
}

#[cfg(target_os = "windows")]
{
sane_blksize::DEFAULT
}
}
/// Provides the blksize information from the provided metadata.
/// If the metadata contain invalid values a meaningful adaption
/// of that value is done.
pub fn sane_blksize_from_st(_st: &std::fs::Metadata) -> u64 {
#[cfg(not(target_os = "windows"))]
{
sane_blksize(_st.blksize())
}

pub fn get_sanity_limited_blksize_from_path(path: &Path) -> usize {
if path.to_string_lossy() == "-" {
return sane_blksize::DEFAULT;
#[cfg(target_os = "windows")]
{
DEFAULT
}
}

match metadata(path) {
Ok(st) => get_sanity_limited_blksize(&st),
Err(_) => sane_blksize::DEFAULT,
/// Provides the blksize information from given file path's filesystem.
/// If the metadata can't be fetched or contain invalid values a
/// meaningful adaption of that value is done.
pub fn sane_blksize_from_path(path: &Path) -> u64 {
match metadata(path) {
Ok(st) => sane_blksize_from_st(&st),
Err(_) => DEFAULT,
}
}
}

Expand Down Expand Up @@ -1007,4 +1016,13 @@ mod tests {
assert!(path_ends_with_terminator(Path::new("/")));
assert!(path_ends_with_terminator(Path::new("C:\\")));
}

#[test]
fn test_sane_blksize() {
assert_eq!(512, sane_blksize::sane_blksize(0));
assert_eq!(512, sane_blksize::sane_blksize(512));
assert_eq!(4096, sane_blksize::sane_blksize(4096));
assert_eq!(0x2000_0000, sane_blksize::sane_blksize(0x2000_0000));
assert_eq!(512, sane_blksize::sane_blksize(0x2000_0001));
}
}

0 comments on commit 8e51b86

Please sign in to comment.