Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

du: directories have apparent size of 0 #4991

Merged
merged 1 commit into from
Jun 26, 2023
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
4 changes: 2 additions & 2 deletions src/uu/du/src/du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl Stat {
return Ok(Self {
path: path.to_path_buf(),
is_dir: metadata.is_dir(),
size: metadata.len(),
size: if path.is_dir() { 0 } else { metadata.len() },
blocks: metadata.blocks(),
inodes: 1,
inode: Some(file_info),
Expand All @@ -162,7 +162,7 @@ impl Stat {
Ok(Self {
path: path.to_path_buf(),
is_dir: metadata.is_dir(),
size: metadata.len(),
size: if path.is_dir() { 0 } else { metadata.len() },
blocks: size_on_disk / 1024 * 2,
inode: file_info,
inodes: 1,
Expand Down
117 changes: 39 additions & 78 deletions tests/by-util/test_du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,97 +580,58 @@ fn test_du_invalid_threshold() {

#[test]
fn test_du_apparent_size() {
let ts = TestScenario::new(util_name!());
let result = ts.ucmd().arg("--apparent-size").succeeds();
let (at, mut ucmd) = at_and_ucmd!();

#[cfg(any(target_os = "linux", target_os = "android"))]
at.mkdir_all("a/b");

at.write("a/b/file1", "foo");
at.write("a/b/file2", "foobar");

let result = ucmd.args(&["--apparent-size", "--all", "a"]).succeeds();

#[cfg(not(target_os = "windows"))]
{
let result_reference = unwrap_or_return!(expected_result(&ts, &["--apparent-size"]));
assert_eq!(result.stdout_str(), result_reference.stdout_str());
result.stdout_contains_line("1\ta/b/file2");
result.stdout_contains_line("1\ta/b/file1");
result.stdout_contains_line("1\ta/b");
result.stdout_contains_line("1\ta");
}

#[cfg(not(any(target_os = "linux", target_os = "android")))]
_du_apparent_size(result.stdout_str());
}

#[cfg(target_os = "windows")]
fn _du_apparent_size(s: &str) {
assert_eq!(
s,
"1\t.\\subdir\\deeper\\deeper_dir
1\t.\\subdir\\deeper
6\t.\\subdir\\links
6\t.\\subdir
6\t.
"
);
}
#[cfg(target_vendor = "apple")]
fn _du_apparent_size(s: &str) {
assert_eq!(
s,
"1\t./subdir/deeper/deeper_dir
1\t./subdir/deeper
6\t./subdir/links
6\t./subdir
6\t.
"
);
}
#[cfg(target_os = "freebsd")]
fn _du_apparent_size(s: &str) {
assert_eq!(
s,
"1\t./subdir/deeper/deeper_dir
2\t./subdir/deeper
6\t./subdir/links
8\t./subdir
8\t.
"
);
}
#[cfg(all(
not(target_vendor = "apple"),
not(target_os = "windows"),
not(target_os = "freebsd")
))]
fn _du_apparent_size(s: &str) {
assert_eq!(
s,
"5\t./subdir/deeper/deeper_dir
9\t./subdir/deeper
10\t./subdir/links
22\t./subdir
26\t.
"
);
#[cfg(target_os = "windows")]
{
result.stdout_contains_line("1\ta\\b\\file2");
result.stdout_contains_line("1\ta\\b\\file1");
result.stdout_contains_line("1\ta\\b");
result.stdout_contains_line("1\ta");
}
}

#[test]
fn test_du_bytes() {
let ts = TestScenario::new(util_name!());
let result = ts.ucmd().arg("--bytes").succeeds();
let (at, mut ucmd) = at_and_ucmd!();

#[cfg(any(target_os = "linux", target_os = "android"))]
at.mkdir_all("a/b");

at.write("a/b/file1", "foo");
at.write("a/b/file2", "foobar");

let result = ucmd.args(&["--bytes", "--all", "a"]).succeeds();

#[cfg(not(target_os = "windows"))]
{
let result_reference = unwrap_or_return!(expected_result(&ts, &["--bytes"]));
assert_eq!(result.stdout_str(), result_reference.stdout_str());
result.stdout_contains_line("6\ta/b/file2");
result.stdout_contains_line("3\ta/b/file1");
result.stdout_contains_line("9\ta/b");
result.stdout_contains_line("9\ta");
}

#[cfg(target_os = "windows")]
result.stdout_contains("5145\t.\\subdir\n");
#[cfg(target_vendor = "apple")]
result.stdout_contains("5625\t./subdir\n");
#[cfg(target_os = "freebsd")]
result.stdout_contains("7193\t./subdir\n");
#[cfg(all(
not(target_vendor = "apple"),
not(target_os = "windows"),
not(target_os = "freebsd"),
not(target_os = "linux"),
not(target_os = "android"),
))]
result.stdout_contains("21529\t./subdir\n");
{
result.stdout_contains_line("6\ta\\b\\file2");
result.stdout_contains_line("3\ta\\b\\file1");
result.stdout_contains_line("9\ta\\b");
result.stdout_contains_line("9\ta");
}
}

#[test]
Expand Down
11 changes: 11 additions & 0 deletions tests/common/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,17 @@ impl CmdResult {
self
}

#[track_caller]
pub fn stdout_contains_line<T: AsRef<str>>(&self, cmp: T) -> &Self {
assert!(
self.stdout_str().lines().any(|line| line == cmp.as_ref()),
"'{}' does not contain line '{}'",
self.stdout_str(),
cmp.as_ref()
);
self
}

#[track_caller]
pub fn stderr_contains<T: AsRef<str>>(&self, cmp: T) -> &Self {
assert!(
Expand Down