1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-08-01 21:47:46 +00:00

refactor/stat ~ polish spelling (comments, names, and exceptions)

This commit is contained in:
Roy Ivy III 2021-05-30 18:36:46 -05:00
parent 451110bba0
commit 8e824742a1

View file

@ -5,8 +5,6 @@
// For the full copyright and license information, please view the LICENSE file // For the full copyright and license information, please view the LICENSE file
// that was distributed with this source code. // that was distributed with this source code.
// spell-checker:ignore (ToDO) showfs otype fmtstr prec ftype blocksize nlink rdev fnodes fsid namelen blksize inodes fstype iosize statfs gnulib NBLOCKSIZE
#[macro_use] #[macro_use]
extern crate uucore; extern crate uucore;
use uucore::entries; use uucore::entries;
@ -208,7 +206,7 @@ pub fn group_num(s: &str) -> Cow<str> {
pub struct Stater { pub struct Stater {
follow: bool, follow: bool,
showfs: bool, show_fs: bool,
from_user: bool, from_user: bool,
files: Vec<String>, files: Vec<String>,
mount_list: Option<Vec<String>>, mount_list: Option<Vec<String>>,
@ -217,7 +215,7 @@ pub struct Stater {
} }
#[allow(clippy::cognitive_complexity)] #[allow(clippy::cognitive_complexity)]
fn print_it(arg: &str, otype: OutputType, flag: u8, width: usize, precision: i32) { fn print_it(arg: &str, output_type: OutputType, flag: u8, width: usize, precision: i32) {
// If the precision is given as just '.', the precision is taken to be zero. // If the precision is given as just '.', the precision is taken to be zero.
// A negative precision is taken as if the precision were omitted. // A negative precision is taken as if the precision were omitted.
// This gives the minimum number of digits to appear for d, i, o, u, x, and X conversions, // This gives the minimum number of digits to appear for d, i, o, u, x, and X conversions,
@ -248,7 +246,7 @@ fn print_it(arg: &str, otype: OutputType, flag: u8, width: usize, precision: i32
// By default, a sign is used only for negative numbers. // By default, a sign is used only for negative numbers.
// A + overrides a space if both are used. // A + overrides a space if both are used.
if otype == OutputType::Unknown { if output_type == OutputType::Unknown {
return print!("?"); return print!("?");
} }
@ -262,7 +260,7 @@ fn print_it(arg: &str, otype: OutputType, flag: u8, width: usize, precision: i32
let has_sign = has!(flag, F_SIGN) || has!(flag, F_SPACE); let has_sign = has!(flag, F_SIGN) || has!(flag, F_SPACE);
let should_alter = has!(flag, F_ALTER); let should_alter = has!(flag, F_ALTER);
let prefix = match otype { let prefix = match output_type {
OutputType::UnsignedOct => "0", OutputType::UnsignedOct => "0",
OutputType::UnsignedHex => "0x", OutputType::UnsignedHex => "0x",
OutputType::Integer => { OutputType::Integer => {
@ -275,7 +273,7 @@ fn print_it(arg: &str, otype: OutputType, flag: u8, width: usize, precision: i32
_ => "", _ => "",
}; };
match otype { match output_type {
OutputType::Str => { OutputType::Str => {
let limit = cmp::min(precision, arg.len() as i32); let limit = cmp::min(precision, arg.len() as i32);
let s: &str = if limit >= 0 { let s: &str = if limit >= 0 {
@ -334,10 +332,10 @@ fn print_it(arg: &str, otype: OutputType, flag: u8, width: usize, precision: i32
} }
impl Stater { impl Stater {
pub fn generate_tokens(fmtstr: &str, use_printf: bool) -> Result<Vec<Token>, String> { pub fn generate_tokens(format_str: &str, use_printf: bool) -> Result<Vec<Token>, String> {
let mut tokens = Vec::new(); let mut tokens = Vec::new();
let bound = fmtstr.len(); let bound = format_str.len();
let chars = fmtstr.chars().collect::<Vec<char>>(); let chars = format_str.chars().collect::<Vec<char>>();
let mut i = 0_usize; let mut i = 0_usize;
while i < bound { while i < bound {
match chars[i] { match chars[i] {
@ -370,32 +368,32 @@ impl Stater {
} }
i += 1; i += 1;
} }
check_bound!(fmtstr, bound, old, i); check_bound!(format_str, bound, old, i);
let mut width = 0_usize; let mut width = 0_usize;
let mut precision = -1_i32; let mut precision = -1_i32;
let mut j = i; let mut j = i;
if let Some((field_width, offset)) = fmtstr[j..].scan_num::<usize>() { if let Some((field_width, offset)) = format_str[j..].scan_num::<usize>() {
width = field_width; width = field_width;
j += offset; j += offset;
} }
check_bound!(fmtstr, bound, old, j); check_bound!(format_str, bound, old, j);
if chars[j] == '.' { if chars[j] == '.' {
j += 1; j += 1;
check_bound!(fmtstr, bound, old, j); check_bound!(format_str, bound, old, j);
match fmtstr[j..].scan_num::<i32>() { match format_str[j..].scan_num::<i32>() {
Some((prec, offset)) => { Some((value, offset)) => {
if prec >= 0 { if value >= 0 {
precision = prec; precision = value;
} }
j += offset; j += offset;
} }
None => precision = 0, None => precision = 0,
} }
check_bound!(fmtstr, bound, old, j); check_bound!(format_str, bound, old, j);
} }
i = j; i = j;
@ -418,7 +416,7 @@ impl Stater {
} }
match chars[i] { match chars[i] {
'x' if i + 1 < bound => { 'x' if i + 1 < bound => {
if let Some((c, offset)) = fmtstr[i + 1..].scan_char(16) { if let Some((c, offset)) = format_str[i + 1..].scan_char(16) {
tokens.push(Token::Char(c)); tokens.push(Token::Char(c));
i += offset; i += offset;
} else { } else {
@ -427,7 +425,7 @@ impl Stater {
} }
} }
'0'..='7' => { '0'..='7' => {
let (c, offset) = fmtstr[i..].scan_char(8).unwrap(); let (c, offset) = format_str[i..].scan_char(8).unwrap();
tokens.push(Token::Char(c)); tokens.push(Token::Char(c));
i += offset - 1; i += offset - 1;
} }
@ -452,7 +450,7 @@ impl Stater {
} }
i += 1; i += 1;
} }
if !use_printf && !fmtstr.ends_with('\n') { if !use_printf && !format_str.ends_with('\n') {
tokens.push(Token::Char('\n')); tokens.push(Token::Char('\n'));
} }
Ok(tokens) Ok(tokens)
@ -464,7 +462,7 @@ impl Stater {
.map(|v| v.map(ToString::to_string).collect()) .map(|v| v.map(ToString::to_string).collect())
.unwrap_or_default(); .unwrap_or_default();
let fmtstr = if matches.is_present(options::PRINTF) { let format_str = if matches.is_present(options::PRINTF) {
matches matches
.value_of(options::PRINTF) .value_of(options::PRINTF)
.expect("Invalid format string") .expect("Invalid format string")
@ -474,17 +472,17 @@ impl Stater {
let use_printf = matches.is_present(options::PRINTF); let use_printf = matches.is_present(options::PRINTF);
let terse = matches.is_present(options::TERSE); let terse = matches.is_present(options::TERSE);
let showfs = matches.is_present(options::FILE_SYSTEM); let show_fs = matches.is_present(options::FILE_SYSTEM);
let default_tokens = if fmtstr.is_empty() { let default_tokens = if format_str.is_empty() {
Stater::generate_tokens(&Stater::default_fmt(showfs, terse, false), use_printf).unwrap() Stater::generate_tokens(&Stater::default_format(show_fs, terse, false), use_printf).unwrap()
} else { } else {
Stater::generate_tokens(&fmtstr, use_printf)? Stater::generate_tokens(&format_str, use_printf)?
}; };
let default_dev_tokens = let default_dev_tokens =
Stater::generate_tokens(&Stater::default_fmt(showfs, terse, true), use_printf).unwrap(); Stater::generate_tokens(&Stater::default_format(show_fs, terse, true), use_printf).unwrap();
let mount_list = if showfs { let mount_list = if show_fs {
// mount points aren't displayed when showing filesystem information // mount points aren't displayed when showing filesystem information
None None
} else { } else {
@ -500,8 +498,8 @@ impl Stater {
Ok(Stater { Ok(Stater {
follow: matches.is_present(options::DEREFERENCE), follow: matches.is_present(options::DEREFERENCE),
showfs, show_fs,
from_user: !fmtstr.is_empty(), from_user: !format_str.is_empty(),
files, files,
default_tokens, default_tokens,
default_dev_tokens, default_dev_tokens,
@ -533,7 +531,7 @@ impl Stater {
} }
fn do_stat(&self, file: &str) -> i32 { fn do_stat(&self, file: &str) -> i32 {
if !self.showfs { if !self.show_fs {
let result = if self.follow { let result = if self.follow {
fs::metadata(file) fs::metadata(file)
} else { } else {
@ -541,9 +539,9 @@ impl Stater {
}; };
match result { match result {
Ok(meta) => { Ok(meta) => {
let ftype = meta.file_type(); let file_type = meta.file_type();
let tokens = let tokens =
if self.from_user || !(ftype.is_char_device() || ftype.is_block_device()) { if self.from_user || !(file_type.is_char_device() || file_type.is_block_device()) {
&self.default_tokens &self.default_tokens
} else { } else {
&self.default_dev_tokens &self.default_dev_tokens
@ -559,91 +557,91 @@ impl Stater {
format, format,
} => { } => {
let arg: String; let arg: String;
let otype: OutputType; let output_type: OutputType;
match format { match format {
// access rights in octal // access rights in octal
'a' => { 'a' => {
arg = format!("{:o}", 0o7777 & meta.mode()); arg = format!("{:o}", 0o7777 & meta.mode());
otype = OutputType::UnsignedOct; output_type = OutputType::UnsignedOct;
} }
// access rights in human readable form // access rights in human readable form
'A' => { 'A' => {
arg = display_permissions(&meta, true); arg = display_permissions(&meta, true);
otype = OutputType::Str; output_type = OutputType::Str;
} }
// number of blocks allocated (see %B) // number of blocks allocated (see %B)
'b' => { 'b' => {
arg = format!("{}", meta.blocks()); arg = format!("{}", meta.blocks());
otype = OutputType::Unsigned; output_type = OutputType::Unsigned;
} }
// the size in bytes of each block reported by %b // the size in bytes of each block reported by %b
// FIXME: blocksize differs on various platform // FIXME: blocksize differs on various platform
// See coreutils/gnulib/lib/stat-size.h ST_NBLOCKSIZE // See coreutils/gnulib/lib/stat-size.h ST_NBLOCKSIZE // spell-checker:disable-line
'B' => { 'B' => {
// the size in bytes of each block reported by %b // the size in bytes of each block reported by %b
arg = format!("{}", 512); arg = format!("{}", 512);
otype = OutputType::Unsigned; output_type = OutputType::Unsigned;
} }
// device number in decimal // device number in decimal
'd' => { 'd' => {
arg = format!("{}", meta.dev()); arg = format!("{}", meta.dev());
otype = OutputType::Unsigned; output_type = OutputType::Unsigned;
} }
// device number in hex // device number in hex
'D' => { 'D' => {
arg = format!("{:x}", meta.dev()); arg = format!("{:x}", meta.dev());
otype = OutputType::UnsignedHex; output_type = OutputType::UnsignedHex;
} }
// raw mode in hex // raw mode in hex
'f' => { 'f' => {
arg = format!("{:x}", meta.mode()); arg = format!("{:x}", meta.mode());
otype = OutputType::UnsignedHex; output_type = OutputType::UnsignedHex;
} }
// file type // file type
'F' => { 'F' => {
arg = pretty_filetype(meta.mode() as mode_t, meta.len()) arg = pretty_filetype(meta.mode() as mode_t, meta.len())
.to_owned(); .to_owned();
otype = OutputType::Str; output_type = OutputType::Str;
} }
// group ID of owner // group ID of owner
'g' => { 'g' => {
arg = format!("{}", meta.gid()); arg = format!("{}", meta.gid());
otype = OutputType::Unsigned; output_type = OutputType::Unsigned;
} }
// group name of owner // group name of owner
'G' => { 'G' => {
arg = entries::gid2grp(meta.gid()) arg = entries::gid2grp(meta.gid())
.unwrap_or_else(|_| "UNKNOWN".to_owned()); .unwrap_or_else(|_| "UNKNOWN".to_owned());
otype = OutputType::Str; output_type = OutputType::Str;
} }
// number of hard links // number of hard links
'h' => { 'h' => {
arg = format!("{}", meta.nlink()); arg = format!("{}", meta.nlink());
otype = OutputType::Unsigned; output_type = OutputType::Unsigned;
} }
// inode number // inode number
'i' => { 'i' => {
arg = format!("{}", meta.ino()); arg = format!("{}", meta.ino());
otype = OutputType::Unsigned; output_type = OutputType::Unsigned;
} }
// mount point // mount point
'm' => { 'm' => {
arg = self.find_mount_point(file).unwrap(); arg = self.find_mount_point(file).unwrap();
otype = OutputType::Str; output_type = OutputType::Str;
} }
// file name // file name
'n' => { 'n' => {
arg = file.to_owned(); arg = file.to_owned();
otype = OutputType::Str; output_type = OutputType::Str;
} }
// quoted file name with dereference if symbolic link // quoted file name with dereference if symbolic link
'N' => { 'N' => {
if ftype.is_symlink() { if file_type.is_symlink() {
let dst = match fs::read_link(file) { let dst = match fs::read_link(file) {
Ok(path) => path, Ok(path) => path,
Err(e) => { Err(e) => {
@ -659,91 +657,91 @@ impl Stater {
} else { } else {
arg = file.to_string(); arg = file.to_string();
} }
otype = OutputType::Str; output_type = OutputType::Str;
} }
// optimal I/O transfer size hint // optimal I/O transfer size hint
'o' => { 'o' => {
arg = format!("{}", meta.blksize()); arg = format!("{}", meta.blksize());
otype = OutputType::Unsigned; output_type = OutputType::Unsigned;
} }
// total size, in bytes // total size, in bytes
's' => { 's' => {
arg = format!("{}", meta.len()); arg = format!("{}", meta.len());
otype = OutputType::Integer; output_type = OutputType::Integer;
} }
// major device type in hex, for character/block device special // major device type in hex, for character/block device special
// files // files
't' => { 't' => {
arg = format!("{:x}", meta.rdev() >> 8); arg = format!("{:x}", meta.rdev() >> 8);
otype = OutputType::UnsignedHex; output_type = OutputType::UnsignedHex;
} }
// minor device type in hex, for character/block device special // minor device type in hex, for character/block device special
// files // files
'T' => { 'T' => {
arg = format!("{:x}", meta.rdev() & 0xff); arg = format!("{:x}", meta.rdev() & 0xff);
otype = OutputType::UnsignedHex; output_type = OutputType::UnsignedHex;
} }
// user ID of owner // user ID of owner
'u' => { 'u' => {
arg = format!("{}", meta.uid()); arg = format!("{}", meta.uid());
otype = OutputType::Unsigned; output_type = OutputType::Unsigned;
} }
// user name of owner // user name of owner
'U' => { 'U' => {
arg = entries::uid2usr(meta.uid()) arg = entries::uid2usr(meta.uid())
.unwrap_or_else(|_| "UNKNOWN".to_owned()); .unwrap_or_else(|_| "UNKNOWN".to_owned());
otype = OutputType::Str; output_type = OutputType::Str;
} }
// time of file birth, human-readable; - if unknown // time of file birth, human-readable; - if unknown
'w' => { 'w' => {
arg = meta.pretty_birth(); arg = meta.pretty_birth();
otype = OutputType::Str; output_type = OutputType::Str;
} }
// time of file birth, seconds since Epoch; 0 if unknown // time of file birth, seconds since Epoch; 0 if unknown
'W' => { 'W' => {
arg = meta.birth(); arg = meta.birth();
otype = OutputType::Integer; output_type = OutputType::Integer;
} }
// time of last access, human-readable // time of last access, human-readable
'x' => { 'x' => {
arg = pretty_time(meta.atime(), meta.atime_nsec()); arg = pretty_time(meta.atime(), meta.atime_nsec());
otype = OutputType::Str; output_type = OutputType::Str;
} }
// time of last access, seconds since Epoch // time of last access, seconds since Epoch
'X' => { 'X' => {
arg = format!("{}", meta.atime()); arg = format!("{}", meta.atime());
otype = OutputType::Integer; output_type = OutputType::Integer;
} }
// time of last data modification, human-readable // time of last data modification, human-readable
'y' => { 'y' => {
arg = pretty_time(meta.mtime(), meta.mtime_nsec()); arg = pretty_time(meta.mtime(), meta.mtime_nsec());
otype = OutputType::Str; output_type = OutputType::Str;
} }
// time of last data modification, seconds since Epoch // time of last data modification, seconds since Epoch
'Y' => { 'Y' => {
arg = format!("{}", meta.mtime()); arg = format!("{}", meta.mtime());
otype = OutputType::Str; output_type = OutputType::Str;
} }
// time of last status change, human-readable // time of last status change, human-readable
'z' => { 'z' => {
arg = pretty_time(meta.ctime(), meta.ctime_nsec()); arg = pretty_time(meta.ctime(), meta.ctime_nsec());
otype = OutputType::Str; output_type = OutputType::Str;
} }
// time of last status change, seconds since Epoch // time of last status change, seconds since Epoch
'Z' => { 'Z' => {
arg = format!("{}", meta.ctime()); arg = format!("{}", meta.ctime());
otype = OutputType::Integer; output_type = OutputType::Integer;
} }
_ => { _ => {
arg = "?".to_owned(); arg = "?".to_owned();
otype = OutputType::Unknown; output_type = OutputType::Unknown;
} }
} }
print_it(&arg, otype, flag, width, precision); print_it(&arg, output_type, flag, width, precision);
} }
} }
} }
@ -768,75 +766,75 @@ impl Stater {
format, format,
} => { } => {
let arg: String; let arg: String;
let otype: OutputType; let output_type: OutputType;
match format { match format {
// free blocks available to non-superuser // free blocks available to non-superuser
'a' => { 'a' => {
arg = format!("{}", meta.avail_blocks()); arg = format!("{}", meta.avail_blocks());
otype = OutputType::Integer; output_type = OutputType::Integer;
} }
// total data blocks in file system // total data blocks in file system
'b' => { 'b' => {
arg = format!("{}", meta.total_blocks()); arg = format!("{}", meta.total_blocks());
otype = OutputType::Integer; output_type = OutputType::Integer;
} }
// total file nodes in file system // total file nodes in file system
'c' => { 'c' => {
arg = format!("{}", meta.total_fnodes()); arg = format!("{}", meta.total_file_nodes());
otype = OutputType::Unsigned; output_type = OutputType::Unsigned;
} }
// free file nodes in file system // free file nodes in file system
'd' => { 'd' => {
arg = format!("{}", meta.free_fnodes()); arg = format!("{}", meta.free_file_nodes());
otype = OutputType::Integer; output_type = OutputType::Integer;
} }
// free blocks in file system // free blocks in file system
'f' => { 'f' => {
arg = format!("{}", meta.free_blocks()); arg = format!("{}", meta.free_blocks());
otype = OutputType::Integer; output_type = OutputType::Integer;
} }
// file system ID in hex // file system ID in hex
'i' => { 'i' => {
arg = format!("{:x}", meta.fsid()); arg = format!("{:x}", meta.fsid());
otype = OutputType::UnsignedHex; output_type = OutputType::UnsignedHex;
} }
// maximum length of filenames // maximum length of filenames
'l' => { 'l' => {
arg = format!("{}", meta.namelen()); arg = format!("{}", meta.namelen());
otype = OutputType::Unsigned; output_type = OutputType::Unsigned;
} }
// file name // file name
'n' => { 'n' => {
arg = file.to_owned(); arg = file.to_owned();
otype = OutputType::Str; output_type = OutputType::Str;
} }
// block size (for faster transfers) // block size (for faster transfers)
's' => { 's' => {
arg = format!("{}", meta.iosize()); arg = format!("{}", meta.io_size());
otype = OutputType::Unsigned; output_type = OutputType::Unsigned;
} }
// fundamental block size (for block counts) // fundamental block size (for block counts)
'S' => { 'S' => {
arg = format!("{}", meta.blksize()); arg = format!("{}", meta.block_size());
otype = OutputType::Unsigned; output_type = OutputType::Unsigned;
} }
// file system type in hex // file system type in hex
't' => { 't' => {
arg = format!("{:x}", meta.fs_type()); arg = format!("{:x}", meta.fs_type());
otype = OutputType::UnsignedHex; output_type = OutputType::UnsignedHex;
} }
// file system type in human readable form // file system type in human readable form
'T' => { 'T' => {
arg = pretty_fstype(meta.fs_type()).into_owned(); arg = pretty_fstype(meta.fs_type()).into_owned();
otype = OutputType::Str; output_type = OutputType::Str;
} }
_ => { _ => {
arg = "?".to_owned(); arg = "?".to_owned();
otype = OutputType::Unknown; output_type = OutputType::Unknown;
} }
} }
print_it(&arg, otype, flag, width, precision); print_it(&arg, output_type, flag, width, precision);
} }
} }
} }
@ -850,34 +848,33 @@ impl Stater {
0 0
} }
// taken from coreutils/src/stat.c fn default_format(show_fs: bool, terse: bool, show_dev_type: bool) -> String {
fn default_fmt(showfs: bool, terse: bool, dev: bool) -> String {
// SELinux related format is *ignored* // SELinux related format is *ignored*
let mut fmtstr = String::with_capacity(36); let mut format_str = String::with_capacity(36);
if showfs { if show_fs {
if terse { if terse {
fmtstr.push_str("%n %i %l %t %s %S %b %f %a %c %d\n"); format_str.push_str("%n %i %l %t %s %S %b %f %a %c %d\n");
} else { } else {
fmtstr.push_str( format_str.push_str(
" File: \"%n\"\n ID: %-8i Namelen: %-7l Type: %T\nBlock \ " File: \"%n\"\n ID: %-8i Namelen: %-7l Type: %T\nBlock \
size: %-10s Fundamental block size: %S\nBlocks: Total: %-10b \ size: %-10s Fundamental block size: %S\nBlocks: Total: %-10b \
Free: %-10f Available: %a\nInodes: Total: %-10c Free: %d\n", Free: %-10f Available: %a\nInodes: Total: %-10c Free: %d\n",
); );
} }
} else if terse { } else if terse {
fmtstr.push_str("%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %W %o\n"); format_str.push_str("%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %W %o\n");
} else { } else {
fmtstr.push_str(" File: %N\n Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"); format_str.push_str(" File: %N\n Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n");
if dev { if show_dev_type {
fmtstr.push_str("Device: %Dh/%dd\tInode: %-10i Links: %-5h Device type: %t,%T\n"); format_str.push_str("Device: %Dh/%dd\tInode: %-10i Links: %-5h Device type: %t,%T\n");
} else { } else {
fmtstr.push_str("Device: %Dh/%dd\tInode: %-10i Links: %h\n"); format_str.push_str("Device: %Dh/%dd\tInode: %-10i Links: %h\n");
} }
fmtstr.push_str("Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"); format_str.push_str("Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n");
fmtstr.push_str("Access: %x\nModify: %y\nChange: %z\n Birth: %w\n"); format_str.push_str("Access: %x\nModify: %y\nChange: %z\n Birth: %w\n");
} }
fmtstr format_str
} }
} }