Skip to content

Commit

Permalink
du: directories have apparent size of 0
Browse files Browse the repository at this point in the history
  • Loading branch information
shinhs0506 committed Jun 22, 2023
1 parent 47d0ac4 commit 9f8aa74
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 80 deletions.
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
105 changes: 27 additions & 78 deletions tests/by-util/test_du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,97 +580,46 @@ 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_is("1\ta/b/file2\n1\ta/b/file1\n1\ta/b\n1\ta\n");
}

#[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_is("1\ta\\b\\file2\n1\ta\\b\\file1\n1\ta\\b\n1\ta\n");
}
}

#[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_is("6\ta/b/file2\n3\ta/b/file1\n9\ta/b\n9\ta\n");
}

#[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_is("6\ta\\b\\file2\n3\ta\\b\\file1\n9\ta\\b\n9\ta\n");
}
}

#[test]
Expand Down

0 comments on commit 9f8aa74

Please sign in to comment.