mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-27 11:07:44 +00:00
Merge pull request #5493 from cakebaker/du_inodes_with_count_links
du: make -l/--count-links work
This commit is contained in:
commit
0a07d47243
2 changed files with 45 additions and 7 deletions
|
@ -88,6 +88,7 @@ struct Options {
|
||||||
separate_dirs: bool,
|
separate_dirs: bool,
|
||||||
one_file_system: bool,
|
one_file_system: bool,
|
||||||
dereference: Deref,
|
dereference: Deref,
|
||||||
|
count_links: bool,
|
||||||
inodes: bool,
|
inodes: bool,
|
||||||
verbose: bool,
|
verbose: bool,
|
||||||
}
|
}
|
||||||
|
@ -295,7 +296,7 @@ fn du(
|
||||||
mut my_stat: Stat,
|
mut my_stat: Stat,
|
||||||
options: &Options,
|
options: &Options,
|
||||||
depth: usize,
|
depth: usize,
|
||||||
inodes: &mut HashSet<FileInfo>,
|
seen_inodes: &mut HashSet<FileInfo>,
|
||||||
exclude: &[Pattern],
|
exclude: &[Pattern],
|
||||||
) -> Box<dyn DoubleEndedIterator<Item = Stat>> {
|
) -> Box<dyn DoubleEndedIterator<Item = Stat>> {
|
||||||
let mut stats = vec![];
|
let mut stats = vec![];
|
||||||
|
@ -335,10 +336,13 @@ fn du(
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(inode) = this_stat.inode {
|
if let Some(inode) = this_stat.inode {
|
||||||
if inodes.contains(&inode) {
|
if seen_inodes.contains(&inode) {
|
||||||
|
if options.count_links {
|
||||||
|
my_stat.inodes += 1;
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
inodes.insert(inode);
|
seen_inodes.insert(inode);
|
||||||
}
|
}
|
||||||
if this_stat.is_dir {
|
if this_stat.is_dir {
|
||||||
if options.one_file_system {
|
if options.one_file_system {
|
||||||
|
@ -350,7 +354,13 @@ fn du(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
futures.push(du(this_stat, options, depth + 1, inodes, exclude));
|
futures.push(du(
|
||||||
|
this_stat,
|
||||||
|
options,
|
||||||
|
depth + 1,
|
||||||
|
seen_inodes,
|
||||||
|
exclude,
|
||||||
|
));
|
||||||
} else {
|
} else {
|
||||||
my_stat.size += this_stat.size;
|
my_stat.size += this_stat.size;
|
||||||
my_stat.blocks += this_stat.blocks;
|
my_stat.blocks += this_stat.blocks;
|
||||||
|
@ -561,6 +571,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
} else {
|
} else {
|
||||||
Deref::None
|
Deref::None
|
||||||
},
|
},
|
||||||
|
count_links: matches.get_flag(options::COUNT_LINKS),
|
||||||
inodes: matches.get_flag(options::INODES),
|
inodes: matches.get_flag(options::INODES),
|
||||||
verbose: matches.get_flag(options::VERBOSE),
|
verbose: matches.get_flag(options::VERBOSE),
|
||||||
};
|
};
|
||||||
|
@ -632,11 +643,11 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
// Check existence of path provided in argument
|
// Check existence of path provided in argument
|
||||||
if let Ok(stat) = Stat::new(&path, &options) {
|
if let Ok(stat) = Stat::new(&path, &options) {
|
||||||
// Kick off the computation of disk usage from the initial path
|
// Kick off the computation of disk usage from the initial path
|
||||||
let mut inodes: HashSet<FileInfo> = HashSet::new();
|
let mut seen_inodes: HashSet<FileInfo> = HashSet::new();
|
||||||
if let Some(inode) = stat.inode {
|
if let Some(inode) = stat.inode {
|
||||||
inodes.insert(inode);
|
seen_inodes.insert(inode);
|
||||||
}
|
}
|
||||||
let iter = du(stat, &options, 0, &mut inodes, &excludes);
|
let iter = du(stat, &options, 0, &mut seen_inodes, &excludes);
|
||||||
|
|
||||||
// Sum up all the returned `Stat`s and display results
|
// Sum up all the returned `Stat`s and display results
|
||||||
let (_, len) = iter.size_hint();
|
let (_, len) = iter.size_hint();
|
||||||
|
|
|
@ -441,6 +441,33 @@ fn test_du_inodes() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_du_inodes_with_count_links() {
|
||||||
|
let ts = TestScenario::new(util_name!());
|
||||||
|
let at = &ts.fixtures;
|
||||||
|
|
||||||
|
at.mkdir("dir");
|
||||||
|
at.touch("dir/file");
|
||||||
|
at.hard_link("dir/file", "dir/hard_link_a");
|
||||||
|
at.hard_link("dir/file", "dir/hard_link_b");
|
||||||
|
|
||||||
|
// ensure the hard links are not counted without --count-links
|
||||||
|
ts.ucmd()
|
||||||
|
.arg("--inodes")
|
||||||
|
.arg("dir")
|
||||||
|
.succeeds()
|
||||||
|
.stdout_is("2\tdir\n");
|
||||||
|
|
||||||
|
for arg in ["-l", "--count-links"] {
|
||||||
|
ts.ucmd()
|
||||||
|
.arg("--inodes")
|
||||||
|
.arg(arg)
|
||||||
|
.arg("dir")
|
||||||
|
.succeeds()
|
||||||
|
.stdout_is("4\tdir\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_du_h_flag_empty_file() {
|
fn test_du_h_flag_empty_file() {
|
||||||
new_ucmd!()
|
new_ucmd!()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue