From 57115bd624faa51aa9d18731a763ae0afaeb2de3 Mon Sep 17 00:00:00 2001 From: "andy.boot" Date: Wed, 4 Jan 2023 21:16:01 +0000 Subject: [PATCH] feature: Support for dereference links -L follow du has -L flag which allows it to dereference or follow symlinks. Clone this feature into dust. https://github.com/bootandy/dust/issues/276 --- completions/_dust | 6 ++++-- completions/_dust.ps1 | 2 ++ completions/dust.bash | 2 +- completions/dust.elv | 2 ++ completions/dust.fish | 1 + src/cli.rs | 8 ++++++++ src/dir_walker.rs | 6 ++++-- src/main.rs | 2 ++ src/platform.rs | 4 +--- 9 files changed, 25 insertions(+), 8 deletions(-) diff --git a/completions/_dust b/completions/_dust index 72573f1..838d375 100644 --- a/completions/_dust +++ b/completions/_dust @@ -35,8 +35,10 @@ _dust() { '--version[Print version information]' \ '-p[Subdirectories will not have their path shortened]' \ '--full-paths[Subdirectories will not have their path shortened]' \ -'-l[Ignore links]' \ -'--ignore-links[Ignore links]' \ +'(-L --dereference-links)-l[Ignore links]' \ +'(-L --dereference-links)--ignore-links[Ignore links]' \ +'(-l --ignore-links)-L[dereference sym links - Treat sym links as directories and go into them]' \ +'(-l --ignore-links)--dereference-links[dereference sym links - Treat sym links as directories and go into them]' \ '-x[Only count the files and directories on the same filesystem as the supplied directory]' \ '--limit-filesystem[Only count the files and directories on the same filesystem as the supplied directory]' \ '-s[Use file length instead of blocks]' \ diff --git a/completions/_dust.ps1 b/completions/_dust.ps1 index deafba0..9762810 100644 --- a/completions/_dust.ps1 +++ b/completions/_dust.ps1 @@ -43,6 +43,8 @@ Register-ArgumentCompleter -Native -CommandName 'dust' -ScriptBlock { [CompletionResult]::new('--full-paths', 'full-paths', [CompletionResultType]::ParameterName, 'Subdirectories will not have their path shortened') [CompletionResult]::new('-l', 'l', [CompletionResultType]::ParameterName, 'Ignore links') [CompletionResult]::new('--ignore-links', 'ignore-links', [CompletionResultType]::ParameterName, 'Ignore links') + [CompletionResult]::new('-L', 'L', [CompletionResultType]::ParameterName, 'dereference sym links - Treat sym links as directories and go into them') + [CompletionResult]::new('--dereference-links', 'dereference-links', [CompletionResultType]::ParameterName, 'dereference sym links - Treat sym links as directories and go into them') [CompletionResult]::new('-x', 'x', [CompletionResultType]::ParameterName, 'Only count the files and directories on the same filesystem as the supplied directory') [CompletionResult]::new('--limit-filesystem', 'limit-filesystem', [CompletionResultType]::ParameterName, 'Only count the files and directories on the same filesystem as the supplied directory') [CompletionResult]::new('-s', 's', [CompletionResultType]::ParameterName, 'Use file length instead of blocks') diff --git a/completions/dust.bash b/completions/dust.bash index 87ae7b2..314b09c 100644 --- a/completions/dust.bash +++ b/completions/dust.bash @@ -19,7 +19,7 @@ _dust() { case "${cmd}" in dust) - opts="-h -V -d -n -p -X -l -x -s -r -c -b -z -f -i -v -e -t -w -H -D --help --version --depth --number-of-lines --full-paths --ignore-directory --ignore-links --limit-filesystem --apparent-size --reverse --no-colors --no-percent-bars --min-size --skip-total --filecount --ignore_hidden --invert-filter --filter --file_types --terminal_width --si --only-dir ..." + opts="-h -V -d -n -p -X -l -L -x -s -r -c -b -z -f -i -v -e -t -w -H -D --help --version --depth --number-of-lines --full-paths --ignore-directory --ignore-links --dereference-links --limit-filesystem --apparent-size --reverse --no-colors --no-percent-bars --min-size --skip-total --filecount --ignore_hidden --invert-filter --filter --file_types --terminal_width --si --only-dir ..." if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0 diff --git a/completions/dust.elv b/completions/dust.elv index 7f55818..946d1eb 100644 --- a/completions/dust.elv +++ b/completions/dust.elv @@ -40,6 +40,8 @@ set edit:completion:arg-completer[dust] = {|@words| cand --full-paths 'Subdirectories will not have their path shortened' cand -l 'Ignore links' cand --ignore-links 'Ignore links' + cand -L 'dereference sym links - Treat sym links as directories and go into them' + cand --dereference-links 'dereference sym links - Treat sym links as directories and go into them' cand -x 'Only count the files and directories on the same filesystem as the supplied directory' cand --limit-filesystem 'Only count the files and directories on the same filesystem as the supplied directory' cand -s 'Use file length instead of blocks' diff --git a/completions/dust.fish b/completions/dust.fish index bab6494..2ea53e6 100644 --- a/completions/dust.fish +++ b/completions/dust.fish @@ -9,6 +9,7 @@ complete -c dust -s h -l help -d 'Print help information' complete -c dust -s V -l version -d 'Print version information' complete -c dust -s p -l full-paths -d 'Subdirectories will not have their path shortened' complete -c dust -s l -l ignore-links -d 'Ignore links' +complete -c dust -s L -l dereference-links -d 'dereference sym links - Treat sym links as directories and go into them' complete -c dust -s x -l limit-filesystem -d 'Only count the files and directories on the same filesystem as the supplied directory' complete -c dust -s s -l apparent-size -d 'Use file length instead of blocks' complete -c dust -s r -l reverse -d 'Print tree upside down (biggest highest)' diff --git a/src/cli.rs b/src/cli.rs index 43ee8b9..d55d02e 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -38,8 +38,16 @@ pub fn build_cli() -> Command<'static> { Arg::new("ignore_links") .short('l') .long("ignore-links") + .conflicts_with("dereference_links") .help("Ignore links"), ) + .arg( + Arg::new("dereference_links") + .short('L') + .long("dereference-links") + .conflicts_with("ignore_links") + .help("dereference sym links - Treat sym links as directories and go into them"), + ) .arg( Arg::new("limit_filesystem") .short('x') diff --git a/src/dir_walker.rs b/src/dir_walker.rs index c36e02f..02255be 100644 --- a/src/dir_walker.rs +++ b/src/dir_walker.rs @@ -27,6 +27,7 @@ pub struct WalkData<'a> { pub by_filecount: bool, pub ignore_hidden: bool, pub ignore_links: bool, + pub follow_links: bool, } pub fn walk_it(dirs: HashSet, walk_data: WalkData) -> (Vec, bool) { @@ -145,9 +146,10 @@ fn walk( if !ignore_file(entry, walk_data) { if let Ok(data) = entry.file_type() { if data.is_symlink() && walk_data.ignore_links { - return None + return None; } - return if data.is_dir() && !data.is_symlink() { + return if data.is_dir() || (walk_data.follow_links && data.is_symlink()) + { walk(entry.path(), permissions_flag, walk_data, depth + 1) } else { build_node( diff --git a/src/main.rs b/src/main.rs index a418b2f..11d859c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -153,6 +153,7 @@ fn main() { let by_filecount = options.is_present("by_filecount"); let limit_filesystem = options.is_present("limit_filesystem"); let ignore_links = options.is_present("ignore_links"); + let follow_links = options.is_present("dereference_links"); let simplified_dirs = simplify_dir_names(target_dirs); let allowed_filesystems = limit_filesystem @@ -169,6 +170,7 @@ fn main() { invert_filter_regex: &invert_filter_regexs, allowed_filesystems, ignore_links, + follow_links, use_apparent_size: config.get_apparent_size(&options), by_filecount, ignore_hidden: config.get_ignore_hidden(&options), diff --git a/src/platform.rs b/src/platform.rs index 10aa427..77c578a 100644 --- a/src/platform.rs +++ b/src/platform.rs @@ -21,9 +21,7 @@ pub fn get_metadata(d: &Path, use_apparent_size: bool) -> Option<(u64, Option<(u Some((md.blocks() * get_block_size(), Some((md.ino(), md.dev())))) } } - Err(_e) => { - None - } + Err(_e) => None, } }