Skip to content

Commit 88ca1a6

Browse files
committed
fix: Directory file size shows differently across platforms
1 parent fee946e commit 88ca1a6

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

src/filesystem.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ pub fn is_empty(entry: &dir_entry::DirEntry) -> bool {
5959
}
6060
}
6161

62+
pub fn file_size(entry: &dir_entry::DirEntry) -> Option<u64> {
63+
let file_type = entry.file_type()?;
64+
if file_type.is_dir() {
65+
None
66+
} else {
67+
entry.metadata().map(|m| m.len())
68+
}
69+
}
70+
6271
#[cfg(any(unix, target_os = "redox"))]
6372
pub fn is_block_device(ft: fs::FileType) -> bool {
6473
ft.is_block_device()

src/walk.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ type SortKeyValueFn = Box<dyn Fn(&DirEntry) -> Option<SortKeyValue>>;
694694
fn dir_entry_key_fn(sort_key: SortKey) -> SortKeyValueFn {
695695
match sort_key {
696696
SortKey::Path => Box::new(|e| Some(SortKeyValue::Path(e.path().to_path_buf()))),
697-
SortKey::Size => Box::new(|e| e.metadata().map(|m| SortKeyValue::Size(m.len()))),
697+
SortKey::Size => Box::new(|e| filesystem::file_size(e).map(SortKeyValue::Size)),
698698
SortKey::Created => {
699699
Box::new(|e| e.metadata().map(|m| SortKeyValue::Time(m.created().ok())))
700700
}

0 commit comments

Comments
 (0)