Compare commits

..

3 Commits

Author SHA1 Message Date
andy.boot 15845dee3e refactor: cleanup code 2023-01-04 21:24:57 +00:00
andy.boot 57115bd624 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
2023-01-04 21:17:23 +00:00
andy.boot 0ac818a2f5 Feature: Ignore sym links
https://github.com/bootandy/dust/issues/264
2023-01-04 20:57:43 +00:00
11 changed files with 83 additions and 78 deletions
+4
View File
@@ -35,6 +35,10 @@ _dust() {
'--version[Print version information]' \ '--version[Print version information]' \
'-p[Subdirectories will not have their path shortened]' \ '-p[Subdirectories will not have their path shortened]' \
'--full-paths[Subdirectories will not have their path shortened]' \ '--full-paths[Subdirectories will not have their path shortened]' \
'(-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]' \ '-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]' \ '--limit-filesystem[Only count the files and directories on the same filesystem as the supplied directory]' \
'-s[Use file length instead of blocks]' \ '-s[Use file length instead of blocks]' \
+4
View File
@@ -41,6 +41,10 @@ Register-ArgumentCompleter -Native -CommandName 'dust' -ScriptBlock {
[CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version information') [CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version information')
[CompletionResult]::new('-p', 'p', [CompletionResultType]::ParameterName, 'Subdirectories will not have their path shortened') [CompletionResult]::new('-p', 'p', [CompletionResultType]::ParameterName, 'Subdirectories will not have their path shortened')
[CompletionResult]::new('--full-paths', 'full-paths', [CompletionResultType]::ParameterName, 'Subdirectories will not have their path shortened') [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('-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('--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') [CompletionResult]::new('-s', 's', [CompletionResultType]::ParameterName, 'Use file length instead of blocks')
+1 -1
View File
@@ -19,7 +19,7 @@ _dust() {
case "${cmd}" in case "${cmd}" in
dust) dust)
opts="-h -V -d -n -p -X -x -s -r -c -b -z -f -i -v -e -t -w -H -D --help --version --depth --number-of-lines --full-paths --ignore-directory --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 <inputs>..." 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 <inputs>..."
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0 return 0
+4
View File
@@ -38,6 +38,10 @@ set edit:completion:arg-completer[dust] = {|@words|
cand --version 'Print version information' cand --version 'Print version information'
cand -p 'Subdirectories will not have their path shortened' cand -p 'Subdirectories will not have their path shortened'
cand --full-paths 'Subdirectories will not have their path shortened' 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 -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 --limit-filesystem 'Only count the files and directories on the same filesystem as the supplied directory'
cand -s 'Use file length instead of blocks' cand -s 'Use file length instead of blocks'
+2
View File
@@ -8,6 +8,8 @@ complete -c dust -s w -l terminal_width -d 'Specify width of output overriding t
complete -c dust -s h -l help -d 'Print help information' 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 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 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 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 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)' complete -c dust -s r -l reverse -d 'Print tree upside down (biggest highest)'
+14
View File
@@ -34,6 +34,20 @@ pub fn build_cli() -> Command<'static> {
.multiple_occurrences(true) .multiple_occurrences(true)
.help("Exclude any file or directory with this name"), .help("Exclude any file or directory with this name"),
) )
.arg(
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(
Arg::new("limit_filesystem") Arg::new("limit_filesystem")
.short('x') .short('x')
+14 -10
View File
@@ -26,18 +26,19 @@ pub struct WalkData<'a> {
pub use_apparent_size: bool, pub use_apparent_size: bool,
pub by_filecount: bool, pub by_filecount: bool,
pub ignore_hidden: bool, pub ignore_hidden: bool,
pub ignore_links: bool,
pub follow_links: bool,
} }
pub fn walk_it(dirs: HashSet<PathBuf>, walk_data: WalkData) -> (Vec<Node>, bool) { pub fn walk_it(dirs: HashSet<PathBuf>, walk_data: WalkData) -> (Vec<Node>, bool) {
let permissions_flag = AtomicBool::new(false); let permissions_flag = AtomicBool::new(false);
let mut inodes = HashSet::new();
let top_level_nodes: Vec<_> = dirs let top_level_nodes: Vec<_> = dirs
.into_iter() .into_iter()
.filter_map(|d| { .filter_map(|d| {
clean_inodes( clean_inodes(
walk(d, &permissions_flag, &walk_data, 0)?, walk(d, &permissions_flag, &walk_data, 0)?,
&mut inodes, &mut HashSet::new(),
walk_data.use_apparent_size, walk_data.use_apparent_size,
) )
}) })
@@ -142,12 +143,17 @@ fn walk(
// return walk(entry.path(), permissions_flag, ignore_directories, allowed_filesystems, use_apparent_size, by_filecount, ignore_hidden); // return walk(entry.path(), permissions_flag, ignore_directories, allowed_filesystems, use_apparent_size, by_filecount, ignore_hidden);
if !ignore_file(entry, walk_data) { if ignore_file(entry, walk_data) {
return None;
}
if let Ok(data) = entry.file_type() { if let Ok(data) = entry.file_type() {
return if data.is_dir() && !data.is_symlink() { if data.is_symlink() && walk_data.ignore_links {
walk(entry.path(), permissions_flag, walk_data, depth + 1) return None;
} else { }
build_node( if data.is_dir() || (walk_data.follow_links && data.is_symlink()) {
return walk(entry.path(), permissions_flag, walk_data, depth + 1);
}
return build_node(
entry.path(), entry.path(),
vec![], vec![],
walk_data.filter_regex, walk_data.filter_regex,
@@ -157,9 +163,7 @@ fn walk(
data.is_file(), data.is_file(),
walk_data.by_filecount, walk_data.by_filecount,
depth, depth,
) );
};
}
} }
} else { } else {
permissions_flag.store(true, atomic::Ordering::Relaxed); permissions_flag.store(true, atomic::Ordering::Relaxed);
+1 -8
View File
@@ -20,20 +20,13 @@ pub fn get_biggest(
let mut heap = BinaryHeap::new(); let mut heap = BinaryHeap::new();
let number_top_level_nodes = top_level_nodes.len(); let number_top_level_nodes = top_level_nodes.len();
let root = get_new_root(top_level_nodes); let root = get_new_root(top_level_nodes);
let mut allowed_nodes = HashSet::new(); let mut allowed_nodes = HashSet::new();
allowed_nodes.insert(root.name.as_path()); allowed_nodes.insert(root.name.as_path());
if number_top_level_nodes > 1 {
heap = add_children(using_a_filter, min_size, only_dir, &root, usize::MAX, heap);
} else {
heap = add_children(using_a_filter, min_size, only_dir, &root, depth, heap); heap = add_children(using_a_filter, min_size, only_dir, &root, depth, heap);
}
let number_of_lines_in_output = n - number_top_level_nodes; for _ in number_top_level_nodes..n {
for _ in 0..number_of_lines_in_output {
let line = heap.pop(); let line = heap.pop();
match line { match line {
Some(line) => { Some(line) => {
+4
View File
@@ -152,6 +152,8 @@ fn main() {
let by_filecount = options.is_present("by_filecount"); let by_filecount = options.is_present("by_filecount");
let limit_filesystem = options.is_present("limit_filesystem"); 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 simplified_dirs = simplify_dir_names(target_dirs);
let allowed_filesystems = limit_filesystem let allowed_filesystems = limit_filesystem
@@ -167,6 +169,8 @@ fn main() {
filter_regex: &filter_regexs, filter_regex: &filter_regexs,
invert_filter_regex: &invert_filter_regexs, invert_filter_regex: &invert_filter_regexs,
allowed_filesystems, allowed_filesystems,
ignore_links,
follow_links,
use_apparent_size: config.get_apparent_size(&options), use_apparent_size: config.get_apparent_size(&options),
by_filecount, by_filecount,
ignore_hidden: config.get_ignore_hidden(&options), ignore_hidden: config.get_ignore_hidden(&options),
-1
View File
@@ -73,7 +73,6 @@ pub fn test_ignore_dir() {
let output = build_command(vec!["-c", "-X", "dir_substring", "tests/test_dir2/"]); let output = build_command(vec!["-c", "-X", "dir_substring", "tests/test_dir2/"]);
assert!(!output.contains("dir_substring")); assert!(!output.contains("dir_substring"));
} }
// Add test for multiple dirs - with -d 0 and maybe -d 1 check the
#[test] #[test]
pub fn test_with_bad_param() { pub fn test_with_bad_param() {
+24 -47
View File
@@ -17,18 +17,6 @@ fn build_temp_file(dir: &TempDir) -> PathBuf {
file_path file_path
} }
fn link_it(link_path: PathBuf, file_path_s: &str, is_soft: bool) -> String {
let link_name_s = link_path.to_str().unwrap();
let mut c = Command::new("ln");
if is_soft {
c.arg("-s");
}
c.arg(file_path_s);
c.arg(link_name_s);
assert!(c.output().is_ok());
return link_name_s.into();
}
#[cfg_attr(target_os = "windows", ignore)] #[cfg_attr(target_os = "windows", ignore)]
#[test] #[test]
pub fn test_soft_sym_link() { pub fn test_soft_sym_link() {
@@ -38,7 +26,13 @@ pub fn test_soft_sym_link() {
let file_path_s = file.to_str().unwrap(); let file_path_s = file.to_str().unwrap();
let link_name = dir.path().join("the_link"); let link_name = dir.path().join("the_link");
let link_name_s = link_it(link_name, file_path_s, true); let link_name_s = link_name.to_str().unwrap();
let c = Command::new("ln")
.arg("-s")
.arg(file_path_s)
.arg(link_name_s)
.output();
assert!(c.is_ok());
let c = format!(" ├── {}", link_name_s); let c = format!(" ├── {}", link_name_s);
let b = format!(" ┌── {}", file_path_s); let b = format!(" ┌── {}", file_path_s);
@@ -47,7 +41,7 @@ pub fn test_soft_sym_link() {
let mut cmd = Command::cargo_bin("dust").unwrap(); let mut cmd = Command::cargo_bin("dust").unwrap();
// Mac test runners create long filenames in tmp directories // Mac test runners create long filenames in tmp directories
let output = cmd let output = cmd
.args(["-p", "-c", "-s", "-w", "999", dir_s]) .args(["-p", "-c", "-s", "-w 999", dir_s])
.unwrap() .unwrap()
.stdout; .stdout;
@@ -67,14 +61,19 @@ pub fn test_hard_sym_link() {
let file_path_s = file.to_str().unwrap(); let file_path_s = file.to_str().unwrap();
let link_name = dir.path().join("the_link"); let link_name = dir.path().join("the_link");
link_it(link_name, file_path_s, false); let link_name_s = link_name.to_str().unwrap();
let c = Command::new("ln")
.arg(file_path_s)
.arg(link_name_s)
.output();
assert!(c.is_ok());
let file_output = format!(" ┌── {}", file_path_s); let file_output = format!(" ┌── {}", file_path_s);
let dirs_output = format!("─┴ {}", dir_s); let dirs_output = format!("─┴ {}", dir_s);
let mut cmd = Command::cargo_bin("dust").unwrap(); let mut cmd = Command::cargo_bin("dust").unwrap();
// Mac test runners create long filenames in tmp directories // Mac test runners create long filenames in tmp directories
let output = cmd.args(["-p", "-c", "-w", "999", dir_s]).unwrap().stdout; let output = cmd.args(["-p", "-c", "-w 999", dir_s]).unwrap().stdout;
// The link should not appear in the output because multiple inodes are now ordered // The link should not appear in the output because multiple inodes are now ordered
// then filtered. // then filtered.
@@ -83,34 +82,6 @@ pub fn test_hard_sym_link() {
assert!(output.contains(file_output.as_str())); assert!(output.contains(file_output.as_str()));
} }
#[cfg_attr(target_os = "windows", ignore)]
#[test]
pub fn test_hard_sym_link_no_dup_multi_arg() {
let dir = Builder::new().tempdir().unwrap();
let dir_link = Builder::new().tempdir().unwrap();
let file = build_temp_file(&dir);
let dir_s = dir.path().to_str().unwrap();
let dir_link_s = dir_link.path().to_str().unwrap();
let file_path_s = file.to_str().unwrap();
let link_name = dir_link.path().join("the_link");
let link_name_s = link_it(link_name, file_path_s, false);
let mut cmd = Command::cargo_bin("dust").unwrap();
// Mac test runners create long filenames in tmp directories
let output = cmd
.args(["-p", "-c", "-w", "999", "-b", dir_link_s, dir_s])
.unwrap()
.stdout;
// The link or the file should appear but not both
let output = str::from_utf8(&output).unwrap();
let has_file_only = output.contains(file_path_s) && !output.contains(&link_name_s);
let has_link_only = !output.contains(file_path_s) && output.contains(&link_name_s);
assert!(has_file_only || has_link_only);
}
#[cfg_attr(target_os = "windows", ignore)] #[cfg_attr(target_os = "windows", ignore)]
#[test] #[test]
pub fn test_recursive_sym_link() { pub fn test_recursive_sym_link() {
@@ -118,7 +89,14 @@ pub fn test_recursive_sym_link() {
let dir_s = dir.path().to_str().unwrap(); let dir_s = dir.path().to_str().unwrap();
let link_name = dir.path().join("the_link"); let link_name = dir.path().join("the_link");
let link_name_s = link_it(link_name, dir_s, true); let link_name_s = link_name.to_str().unwrap();
let c = Command::new("ln")
.arg("-s")
.arg(dir_s)
.arg(link_name_s)
.output();
assert!(c.is_ok());
let a = format!("─┬ {}", dir_s); let a = format!("─┬ {}", dir_s);
let b = format!(" └── {}", link_name_s); let b = format!(" └── {}", link_name_s);
@@ -129,8 +107,7 @@ pub fn test_recursive_sym_link() {
.arg("-c") .arg("-c")
.arg("-r") .arg("-r")
.arg("-s") .arg("-s")
.arg("-w") .arg("-w 999")
.arg("999")
.arg(dir_s) .arg(dir_s)
.unwrap() .unwrap()
.stdout; .stdout;