Handle running with bad parameter

Earlier refactor caused running with: 'dust -' to crash
This commit is contained in:
andy.boot
2020-03-28 15:40:33 +00:00
parent 402a8f8249
commit c0048b2ae4
2 changed files with 17 additions and 5 deletions

View File

@@ -12,11 +12,15 @@ fn get_block_size() -> u64 {
#[cfg(target_family = "unix")]
pub fn get_metadata(d: &DirEntry, use_apparent_size: bool) -> Option<(u64, Option<(u64, u64)>)> {
use std::os::unix::fs::MetadataExt;
let md = d.metadata().unwrap();
if use_apparent_size {
Some((md.len(), Some((md.ino(), md.dev()))))
} else {
Some((md.blocks() * get_block_size(), Some((md.ino(), md.dev()))))
match d.metadata() {
Ok(md) => {
if use_apparent_size {
Some((md.len(), Some((md.ino(), md.dev()))))
} else {
Some((md.blocks() * get_block_size(), Some((md.ino(), md.dev()))))
}
}
Err(_e) => None,
}
}

View File

@@ -275,3 +275,11 @@ pub fn test_ignore_dir() {
let output = str::from_utf8(&output).unwrap();
assert!(!output.contains("dir_substring"));
}
#[test]
pub fn test_with_bad_param() {
let mut cmd = Command::cargo_bin("dust").unwrap();
let stderr = cmd.arg("-").unwrap().stderr;
let stderr = str::from_utf8(&stderr).unwrap();
assert!(stderr.contains("Did not have permissions for all directories"));
}