Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/uu/du/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ path = "src/du.rs"
# For the --exclude & --exclude-from options
glob = { workspace = true }
clap = { workspace = true }
uucore = { workspace = true, features = ["format", "parser", "time"] }
uucore = { workspace = true, features = ["format", "fsext", "parser", "time"] }
thiserror = { workspace = true }

[target.'cfg(target_os = "windows")'.dependencies]
Expand Down
138 changes: 39 additions & 99 deletions src/uu/du/src/du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,21 @@ use clap::{Arg, ArgAction, ArgMatches, Command, builder::PossibleValue};
use glob::Pattern;
use std::collections::{HashMap, HashSet};
use std::env;
#[cfg(not(windows))]
use std::fs::Metadata;
use std::fs::{self, DirEntry, File};
use std::io::{BufRead, BufReader, stdout};
#[cfg(not(windows))]
use std::os::unix::fs::MetadataExt;
#[cfg(windows)]
use std::os::windows::fs::MetadataExt;
#[cfg(windows)]
use std::os::windows::io::AsRawHandle;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::sync::mpsc;
use std::thread;
use std::time::{Duration, UNIX_EPOCH};
use thiserror::Error;
use uucore::display::{Quotable, print_verbatim};
use uucore::error::{FromIo, UError, UResult, USimpleError, set_exit_code};
use uucore::fsext::{MetadataTimeField, metadata_get_time};
use uucore::line_ending::LineEnding;
use uucore::locale::{get_message, get_message_with_args};
use uucore::parser::parse_glob;
Expand Down Expand Up @@ -87,7 +84,7 @@ struct StatPrinter {
threshold: Option<Threshold>,
apparent_size: bool,
size_format: SizeFormat,
time: Option<Time>,
time: Option<MetadataTimeField>,
time_format: String,
line_ending: LineEnding,
summarize: bool,
Expand All @@ -101,13 +98,6 @@ enum Deref {
None,
}

#[derive(Clone, Copy)]
enum Time {
Accessed,
Modified,
Created,
}

#[derive(Clone)]
enum SizeFormat {
HumanDecimal,
Expand All @@ -123,14 +113,11 @@ struct FileInfo {

struct Stat {
path: PathBuf,
is_dir: bool,
size: u64,
blocks: u64,
inodes: u64,
inode: Option<FileInfo>,
created: Option<u64>,
accessed: u64,
modified: u64,
metadata: Metadata,
}

impl Stat {
Expand All @@ -157,69 +144,27 @@ impl Stat {
fs::symlink_metadata(path)
}?;

#[cfg(not(windows))]
{
let file_info = FileInfo {
file_id: metadata.ino() as u128,
dev_id: metadata.dev(),
};

Ok(Self {
path: path.to_path_buf(),
is_dir: metadata.is_dir(),
size: if metadata.is_dir() { 0 } else { metadata.len() },
blocks: metadata.blocks(),
inodes: 1,
inode: Some(file_info),
created: birth_u64(&metadata),
accessed: metadata.atime() as u64,
modified: metadata.mtime() as u64,
})
}

#[cfg(windows)]
{
let size_on_disk = get_size_on_disk(path);
let file_info = get_file_info(path);

Ok(Self {
path: path.to_path_buf(),
is_dir: metadata.is_dir(),
size: if metadata.is_dir() { 0 } else { metadata.len() },
blocks: size_on_disk / 1024 * 2,
inodes: 1,
inode: file_info,
created: windows_creation_time_to_unix_time(metadata.creation_time()),
accessed: windows_time_to_unix_time(metadata.last_access_time()),
modified: windows_time_to_unix_time(metadata.last_write_time()),
})
}
let file_info = get_file_info(path, &metadata);
let blocks = get_blocks(path, &metadata);

Ok(Self {
path: path.to_path_buf(),
size: if metadata.is_dir() { 0 } else { metadata.len() },
blocks,
inodes: 1,
inode: file_info,
metadata,
})
}
}

#[cfg(windows)]
/// <https://doc.rust-lang.org/std/os/windows/fs/trait.MetadataExt.html#tymethod.last_access_time>
/// "The returned 64-bit value [...] which represents the number of 100-nanosecond intervals since January 1, 1601 (UTC)."
/// "If the underlying filesystem does not support last access time, the returned value is 0."
fn windows_time_to_unix_time(win_time: u64) -> u64 {
(win_time / 10_000_000).saturating_sub(11_644_473_600)
}

#[cfg(windows)]
fn windows_creation_time_to_unix_time(win_time: u64) -> Option<u64> {
(win_time / 10_000_000).checked_sub(11_644_473_600)
}

#[cfg(not(windows))]
fn birth_u64(meta: &Metadata) -> Option<u64> {
meta.created()
.ok()
.and_then(|t| t.duration_since(UNIX_EPOCH).ok())
.map(|e| e.as_secs())
fn get_blocks(_path: &Path, metadata: &Metadata) -> u64 {
metadata.blocks()
}

#[cfg(windows)]
fn get_size_on_disk(path: &Path) -> u64 {
fn get_blocks(path: &Path, _metadata: &Metadata) -> u64 {
let mut size_on_disk = 0;

// bind file so it stays in scope until end of function
Expand All @@ -244,11 +189,19 @@ fn get_size_on_disk(path: &Path) -> u64 {
}
}

size_on_disk
size_on_disk / 1024 * 2
}

#[cfg(not(windows))]
fn get_file_info(_path: &Path, metadata: &Metadata) -> Option<FileInfo> {
Some(FileInfo {
file_id: metadata.ino() as u128,
dev_id: metadata.dev(),
})
}

#[cfg(windows)]
fn get_file_info(path: &Path) -> Option<FileInfo> {
fn get_file_info(path: &Path, _metadata: &Metadata) -> Option<FileInfo> {
let mut result = None;

let Ok(file) = File::open(path) else {
Expand Down Expand Up @@ -306,7 +259,7 @@ fn du(
seen_inodes: &mut HashSet<FileInfo>,
print_tx: &mpsc::Sender<UResult<StatPrintInfo>>,
) -> Result<Stat, Box<mpsc::SendError<UResult<StatPrintInfo>>>> {
if my_stat.is_dir {
if my_stat.metadata.is_dir() {
let read = match fs::read_dir(&my_stat.path) {
Ok(read) => read,
Err(e) => {
Expand Down Expand Up @@ -367,7 +320,7 @@ fn du(
seen_inodes.insert(inode);
}

if this_stat.is_dir {
if this_stat.metadata.is_dir() {
if options.one_file_system {
if let (Some(this_inode), Some(my_inode)) =
(this_stat.inode, my_stat.inode)
Expand Down Expand Up @@ -435,9 +388,6 @@ enum DuError {
])))]
InvalidTimeStyleArg(String),

#[error("{}", get_message("du-error-invalid-time-arg"))]
InvalidTimeArg,

#[error("{}", get_message_with_args("du-error-invalid-glob", HashMap::from([("error".to_string(), _0.to_string())])))]
InvalidGlob(String),
}
Expand All @@ -448,7 +398,6 @@ impl UError for DuError {
Self::InvalidMaxDepthArg(_)
| Self::SummarizeDepthConflict(_)
| Self::InvalidTimeStyleArg(_)
| Self::InvalidTimeArg
| Self::InvalidGlob(_) => 1,
}
}
Expand Down Expand Up @@ -577,11 +526,13 @@ impl StatPrinter {
fn print_stat(&self, stat: &Stat, size: u64) -> UResult<()> {
print!("{}\t", self.convert_size(size));

if let Some(time) = self.time {
let secs = get_time_secs(time, stat)?;
let time = UNIX_EPOCH + Duration::from_secs(secs);
uucore::time::format_system_time(&mut stdout(), time, &self.time_format, true)?;
print!("\t");
if let Some(md_time) = &self.time {
if let Some(time) = metadata_get_time(&stat.metadata, *md_time) {
uucore::time::format_system_time(&mut stdout(), time, &self.time_format, true)?;
print!("\t");
} else {
print!("???\t");
}
}

print_verbatim(&stat.path).unwrap();
Expand Down Expand Up @@ -697,12 +648,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
};

let time = matches.contains_id(options::TIME).then(|| {
match matches.get_one::<String>(options::TIME).map(AsRef::as_ref) {
None | Some("ctime" | "status") => Time::Modified,
Some("access" | "atime" | "use") => Time::Accessed,
Some("birth" | "creation") => Time::Created,
_ => unreachable!("should be caught by clap"),
}
matches
.get_one::<String>(options::TIME)
.map_or(MetadataTimeField::Modification, |s| s.as_str().into())
});

let size_format = if matches.get_flag(options::HUMAN_READABLE) {
Expand Down Expand Up @@ -853,14 +801,6 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
Ok(())
}

fn get_time_secs(time: Time, stat: &Stat) -> Result<u64, DuError> {
match time {
Time::Modified => Ok(stat.modified),
Time::Accessed => Ok(stat.accessed),
Time::Created => stat.created.ok_or(DuError::InvalidTimeArg),
}
}

fn parse_time_style(s: Option<&str>) -> UResult<&str> {
match s {
Some(s) => match s {
Expand Down
1 change: 1 addition & 0 deletions src/uu/ls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ uucore = { workspace = true, features = [
"entries",
"format",
"fs",
"fsext",
"fsxattr",
"parser",
"quoting-style",
Expand Down
57 changes: 11 additions & 46 deletions src/uu/ls/src/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use uucore::entries;
use uucore::error::USimpleError;
use uucore::format::human::{SizeFormat, human_readable};
use uucore::fs::FileInformation;
use uucore::fsext::{MetadataTimeField, metadata_get_time};
#[cfg(all(unix, not(any(target_os = "android", target_os = "macos"))))]
use uucore::fsxattr::has_acl;
#[cfg(unix)]
Expand Down Expand Up @@ -248,13 +249,6 @@ enum Files {
Normal,
}

enum Time {
Modification,
Access,
Change,
Birth,
}

fn parse_time_style(options: &clap::ArgMatches) -> Result<(String, Option<String>), LsError> {
const TIME_STYLES: [(&str, (&str, Option<&str>)); 4] = [
("full-iso", ("%Y-%m-%d %H:%M:%S.%f %z", None)),
Expand Down Expand Up @@ -332,7 +326,7 @@ pub struct Config {
ignore_patterns: Vec<Pattern>,
size_format: SizeFormat,
directory: bool,
time: Time,
time: MetadataTimeField,
#[cfg(unix)]
inode: bool,
color: Option<LsColors>,
Expand Down Expand Up @@ -467,23 +461,16 @@ fn extract_sort(options: &clap::ArgMatches) -> Sort {
///
/// # Returns
///
/// A Time variant representing the time to use.
fn extract_time(options: &clap::ArgMatches) -> Time {
/// A `MetadataTimeField` variant representing the time to use.
fn extract_time(options: &clap::ArgMatches) -> MetadataTimeField {
if let Some(field) = options.get_one::<String>(options::TIME) {
match field.as_str() {
"ctime" | "status" => Time::Change,
"access" | "atime" | "use" => Time::Access,
"mtime" | "modification" => Time::Modification,
"birth" | "creation" => Time::Birth,
// below should never happen as clap already restricts the values.
_ => unreachable!("Invalid field for --time"),
}
field.as_str().into()
} else if options.get_flag(options::time::ACCESS) {
Time::Access
MetadataTimeField::Access
} else if options.get_flag(options::time::CHANGE) {
Time::Change
MetadataTimeField::Change
} else {
Time::Modification
MetadataTimeField::Modification
}
}

Expand Down Expand Up @@ -2099,7 +2086,7 @@ fn sort_entries(entries: &mut [PathData], config: &Config, out: &mut BufWriter<S
Sort::Time => entries.sort_by_key(|k| {
Reverse(
k.get_metadata(out)
.and_then(|md| get_system_time(md, config))
.and_then(|md| metadata_get_time(md, config.time))
.unwrap_or(UNIX_EPOCH),
)
}),
Expand Down Expand Up @@ -2685,7 +2672,7 @@ fn display_grid(
/// * `group` ([`display_group`], config-optional)
/// * `author` ([`display_uname`], config-optional)
/// * `size / rdev` ([`display_len_or_rdev`])
/// * `system_time` ([`get_system_time`])
/// * `system_time` ([`display_date`])
/// * `item_name` ([`display_item_name`])
///
/// This function needs to display information in columns:
Expand Down Expand Up @@ -2963,35 +2950,13 @@ fn display_group(_metadata: &Metadata, _config: &Config, _state: &mut ListState)
"somegroup"
}

// The implementations for get_system_time are separated because some options, such
// as ctime will not be available
#[cfg(unix)]
fn get_system_time(md: &Metadata, config: &Config) -> Option<SystemTime> {
match config.time {
Time::Change => Some(UNIX_EPOCH + Duration::new(md.ctime() as u64, md.ctime_nsec() as u32)),
Time::Modification => md.modified().ok(),
Time::Access => md.accessed().ok(),
Time::Birth => md.created().ok(),
}
}

#[cfg(not(unix))]
fn get_system_time(md: &Metadata, config: &Config) -> Option<SystemTime> {
match config.time {
Time::Modification => md.modified().ok(),
Time::Access => md.accessed().ok(),
Time::Birth => md.created().ok(),
Time::Change => None,
}
}

fn display_date(
metadata: &Metadata,
config: &Config,
state: &mut ListState,
out: &mut Vec<u8>,
) -> UResult<()> {
let Some(time) = get_system_time(metadata, config) else {
let Some(time) = metadata_get_time(metadata, config.time) else {
out.extend(b"???");
return Ok(());
};
Expand Down
Loading
Loading