Skip to content

Commit

Permalink
du: move to use an enum
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvestre committed Apr 13, 2023
1 parent 687cc5f commit fdf9d87
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions src/uu/du/src/du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,18 @@ struct Options {
total: bool,
separate_dirs: bool,
one_file_system: bool,
dereference: bool,
dereference_args: Vec<PathBuf>,
dereference: Deref,
inodes: bool,
verbose: bool,
}

#[derive(PartialEq)]
enum Deref {
All,
Args(Vec<PathBuf>),
None,
}

#[derive(PartialEq, Eq, Hash, Clone, Copy)]
struct FileInfo {
file_id: u128,
Expand All @@ -115,12 +121,20 @@ struct Stat {

impl Stat {
fn new(path: &Path, options: &Options) -> Result<Self> {
let metadata =
if options.dereference || options.dereference_args.contains(&path.to_path_buf()) {
fs::metadata(path)?
} else {
fs::symlink_metadata(path)?
};
// Determine whether to dereference (follow) the symbolic link
let should_dereference = match &options.dereference {
Deref::All => true,
Deref::Args(paths) => paths.contains(&path.to_path_buf()),
Deref::None => false,
};

let metadata = if should_dereference {
// Get metadata, following symbolic links if necessary
fs::metadata(path)
} else {
// Get metadata without following symbolic links
fs::symlink_metadata(path)
}?;

#[cfg(not(windows))]
let file_info = FileInfo {
Expand Down Expand Up @@ -536,12 +550,13 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
total: matches.get_flag(options::TOTAL),
separate_dirs: matches.get_flag(options::SEPARATE_DIRS),
one_file_system: matches.get_flag(options::ONE_FILE_SYSTEM),
dereference: matches.get_flag(options::DEREFERENCE),
dereference_args: if matches.get_flag(options::DEREFERENCE_ARGS) {
dereference: if matches.get_flag(options::DEREFERENCE) {
Deref::All
} else if matches.get_flag(options::DEREFERENCE_ARGS) {
// We don't care about the cost of cloning as it is rarely used
files.clone()
Deref::Args(files.clone())
} else {
vec![]
Deref::None
},
inodes: matches.get_flag(options::INODES),
verbose: matches.get_flag(options::VERBOSE),
Expand Down

0 comments on commit fdf9d87

Please sign in to comment.