Support excluding filesystems with -x

https://github.com/bootandy/dust/issues/50

Add optional -x flag to limit search to the current filesystem.

Add (untested) support for windows for the equivalent of inode and
device.
This commit is contained in:
andy.boot
2020-01-15 19:51:16 +00:00
parent f395a7d768
commit bdc3d404ef
3 changed files with 74 additions and 4 deletions
+30 -1
View File
@@ -1,4 +1,5 @@
use jwalk::DirEntry;
use std::fs;
#[cfg(target_family = "unix")]
fn get_block_size() -> u64 {
@@ -20,7 +21,16 @@ pub fn get_metadata(d: &DirEntry, use_apparent_size: bool) -> Option<(u64, Optio
})
}
#[cfg(not(target_family = "unix"))]
#[cfg(target_family = "windows")]
pub fn get_metadata(d: &DirEntry, use_apparent_size: bool) -> Option<(u64, Option<(u64, u64)>)> {
use std::os::windows::fs::MetadataExt;
d.metadata.as_ref().unwrap().as_ref().ok().map(|md| {
let windows_equivalent_of_inode = Some((md.file_index(), md.volume_serial_number()));
(md.file_size(), windows_equivalent_of_inode)
})
}
#[cfg(all(not(target_family = "windows"), not(target_family = "unix")))]
pub fn get_metadata(d: &DirEntry, _apparent: bool) -> Option<(u64, Option<(u64, u64)>)> {
d.metadata
.as_ref()
@@ -29,3 +39,22 @@ pub fn get_metadata(d: &DirEntry, _apparent: bool) -> Option<(u64, Option<(u64,
.ok()
.map(|md| (md.len(), None))
}
#[cfg(target_family = "unix")]
pub fn get_filesystem(file_path: &str) -> Option<u64> {
use std::os::unix::fs::MetadataExt;
let metadata = fs::metadata(file_path).unwrap();
Some(metadata.dev())
}
#[cfg(target_family = "windows")]
pub fn get_device(file_path: &str) -> Option<u64> {
use std::os::windows::fs::MetadataExt;
let metadata = fs::metadata(file_path).unwrap();
Some(metadata.volume_serial_number())
}
#[cfg(all(not(target_family = "windows"), not(target_family = "unix")))]
pub fn get_device(file_path: &str) -> Option<u64> {
None
}