Merge pull request #18 from bootandy/fixes

Fixes
This commit is contained in:
andy boot
2018-05-02 09:51:21 +01:00
committed by GitHub
4 changed files with 35 additions and 70 deletions
+2 -2
View File
@@ -141,8 +141,8 @@ pub fn format_string(
indentation: &str, indentation: &str,
) -> String { ) -> String {
let printable_name = { let printable_name = {
if short_paths && dir_name.contains('/') { if short_paths {
dir_name.split('/').last().unwrap() dir_name.split('/').last().unwrap_or(dir_name)
} else { } else {
dir_name dir_name
} }
+17 -11
View File
@@ -5,15 +5,15 @@ extern crate walkdir;
use self::display::draw_it; use self::display::draw_it;
use clap::{App, AppSettings, Arg}; use clap::{App, AppSettings, Arg};
use std::io::{self, Write};
use utils::{find_big_ones, get_dir_tree, sort}; use utils::{find_big_ones, get_dir_tree, sort};
mod display; mod display;
mod utils; mod utils;
static DEFAULT_NUMBER_OF_LINES: &'static str = "20"; static DEFAULT_NUMBER_OF_LINES: usize = 20;
fn main() { fn main() {
let def_num_str = DEFAULT_NUMBER_OF_LINES.to_string();
let options = App::new("Dust") let options = App::new("Dust")
.setting(AppSettings::TrailingVarArg) .setting(AppSettings::TrailingVarArg)
.arg( .arg(
@@ -29,7 +29,7 @@ fn main() {
.long("number-of-lines") .long("number-of-lines")
.help("Number of lines of output to show") .help("Number of lines of output to show")
.takes_value(true) .takes_value(true)
.default_value(DEFAULT_NUMBER_OF_LINES), .default_value(def_num_str.as_ref()),
) )
.arg( .arg(
Arg::with_name("display_full_paths") Arg::with_name("display_full_paths")
@@ -53,23 +53,29 @@ fn main() {
} }
}; };
let number_of_lines = value_t!(options.value_of("number_of_lines"), usize).unwrap(); let number_of_lines = match value_t!(options.value_of("number_of_lines"), usize) {
Ok(v) => v,
Err(_) => {
eprintln!("Ignoring bad value for number_of_lines");
DEFAULT_NUMBER_OF_LINES
}
};
let depth = { let depth = {
if options.is_present("depth") { if options.is_present("depth") {
match value_t!(options.value_of("depth"), u64) { match value_t!(options.value_of("depth"), u64) {
Ok(v) => Some(v + 1), Ok(v) => Some(v + 1),
Err(_) => None, Err(_) => {
eprintln!("Ignoring bad value for depth");
None
}
} }
} else { } else {
None None
} }
}; };
if options.is_present("depth") if options.is_present("depth") && number_of_lines != DEFAULT_NUMBER_OF_LINES {
&& options.value_of("number_of_lines").unwrap() != DEFAULT_NUMBER_OF_LINES eprintln!("Use either -n or -d. Not both");
{
io::stderr()
.write(b"Use either -n for number of directories to show. Or -d for depth. Not both")
.expect("Error writing to stderr. Oh the irony!");
return; return;
} }
+6 -9
View File
@@ -1,12 +1,9 @@
use std::cmp::Ordering;
use std::collections::HashMap; use std::collections::HashMap;
use std::collections::HashSet; use std::collections::HashSet;
use std::cmp::Ordering;
use walkdir::WalkDir; use walkdir::WalkDir;
use std::path::Path;
use std::path::PathBuf;
mod platform; mod platform;
use self::platform::*; use self::platform::*;
@@ -22,7 +19,7 @@ pub fn get_dir_tree(
for b in filenames { for b in filenames {
let top_level_name = strip_end_slashes(b); let top_level_name = strip_end_slashes(b);
examine_dir( examine_dir(
&Path::new(&top_level_name).to_path_buf(), &top_level_name,
apparent_size, apparent_size,
&mut inodes, &mut inodes,
&mut data, &mut data,
@@ -42,7 +39,7 @@ fn strip_end_slashes(s: &str) -> String {
} }
fn examine_dir( fn examine_dir(
top_dir: &PathBuf, top_dir: &String,
apparent_size: bool, apparent_size: bool,
inodes: &mut HashSet<(u64, u64)>, inodes: &mut HashSet<(u64, u64)>,
data: &mut HashMap<String, u64>, data: &mut HashMap<String, u64>,
@@ -66,9 +63,9 @@ fn examine_dir(
let mut e_path = e.path().to_path_buf(); let mut e_path = e.path().to_path_buf();
loop { loop {
let path_name = e_path.to_string_lossy().to_string(); let path_name = e_path.to_string_lossy().to_string();
let s = data.entry(path_name).or_insert(0); let s = data.entry(path_name.clone()).or_insert(0);
*s += size; *s += size;
if e_path == *top_dir { if path_name == *top_dir {
break; break;
} }
e_path.pop(); e_path.pop();
@@ -81,7 +78,7 @@ fn examine_dir(
} }
} }
} }
pub fn compare_tuple(a :&(String, u64), b: &(String, u64)) -> Ordering { pub fn compare_tuple(a: &(String, u64), b: &(String, u64)) -> Ordering {
let result = b.1.cmp(&a.1); let result = b.1.cmp(&a.1);
if result == Ordering::Equal { if result == Ordering::Equal {
a.0.cmp(&b.0) a.0.cmp(&b.0)
+5 -43
View File
@@ -6,58 +6,20 @@ fn get_block_size() -> u64 {
512 512
} }
#[cfg(target_os = "linux")] #[cfg(target_family = "unix")]
pub fn get_metadata(d: &DirEntry, use_apparent_size: bool) -> Option<(u64, Option<(u64, u64)>)> {
use std::os::linux::fs::MetadataExt;
match d.metadata().ok() {
Some(md) => {
let inode = Some((md.st_ino(), md.st_dev()));
if use_apparent_size {
Some((md.len(), inode))
} else {
Some((md.st_blocks() * get_block_size(), inode))
}
}
None => None,
}
}
#[cfg(target_os = "unix")]
pub fn get_metadata(d: &DirEntry, use_apparent_size: bool) -> Option<(u64, Option<(u64, u64)>)> { pub fn get_metadata(d: &DirEntry, use_apparent_size: bool) -> Option<(u64, Option<(u64, u64)>)> {
use std::os::unix::fs::MetadataExt; use std::os::unix::fs::MetadataExt;
match d.metadata().ok() { d.metadata().ok().map_or(None, |md| {
Some(md) => {
let inode = Some((md.ino(), md.dev())); let inode = Some((md.ino(), md.dev()));
if use_apparent_size { if use_apparent_size {
Some((md.len(), inode)) Some((md.len(), inode))
} else { } else {
Some((md.blocks() * get_block_size(), inode)) Some((md.blocks() * get_block_size(), inode))
} }
} })
None => None,
}
} }
#[cfg(target_os = "macos")] #[cfg(not(target_family = "unix"))]
pub fn get_metadata(d: &DirEntry, use_apparent_size: bool) -> Option<(u64, Option<(u64, u64)>)> {
use std::os::macos::fs::MetadataExt;
match d.metadata().ok() {
Some(md) => {
let inode = Some((md.st_ino(), md.st_dev()));
if use_apparent_size {
Some((md.len(), inode))
} else {
Some((md.st_blocks() * get_block_size(), inode))
}
}
None => None,
}
}
#[cfg(not(any(target_os = "linux", target_os = "unix", target_os = "macos")))]
pub fn get_metadata(d: &DirEntry, _apparent: bool) -> Option<(u64, Option<(u64, u64)>)> { pub fn get_metadata(d: &DirEntry, _apparent: bool) -> Option<(u64, Option<(u64, u64)>)> {
match d.metadata().ok() { d.metadata().ok().map_or(None, |md| Some((md.len(), None)))
Some(md) => Some((md.len(), None)),
None => None,
}
} }