1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use clap::ArgMatches;
use std::collections::BTreeMap;
use std::fmt;
use std::fs;
use std::io;
#[cfg(target_os = "linux")]
use std::os::linux::fs::MetadataExt;
#[cfg(target_os = "windows")]
use std::os::windows::fs::MetadataExt;
use std::path::PathBuf;
use std::process;
use std::sync;
use std::sync::Mutex;

/// Current implementation
/// Expand upon the basic solution from ds4.rs.  Include proper error
/// handling and replace unwrap() with ? where possible.  Increase
/// functionality with additional command line options and windows
/// support.

pub enum DSError {
    IO(io::Error),
    Mutex,
}

impl fmt::Display for DSError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match &*self {
            DSError::IO(err) => write!(f, "{}", err),
            DSError::Mutex => write!(f, "Mutex poisoned"),
        }
    }
}

impl From<io::Error> for DSError {
    fn from(err: io::Error) -> DSError {
        DSError::IO(err)
    }
}

impl<T> From<sync::PoisonError<T>> for DSError {
    fn from(_: sync::PoisonError<T>) -> DSError {
        DSError::Mutex
    }
}

/// VerboseErrors
///
/// Two flags to track whether files with errors should be printed or
/// an informational message to the user.
pub struct VerboseErrors {
    pub verbose: bool,
    once: bool,
}

impl VerboseErrors {
    pub fn new() -> VerboseErrors {
        VerboseErrors {
            verbose: false,
            once: true,
        }
    }

    pub fn display(&mut self, path: &PathBuf, err: io::Error) {
        if self.verbose {
            eprintln!("{} {}", path.to_string_lossy().to_string(), err);
        } else {
            if self.once {
                eprintln!("Use -v to see skipped files");
                self.once = false;
            }
        }
    }
}

/// Traverse
///
/// Creates a Mutex of a BTreeMap and a VerboseErrors.  Supports scanning
/// multiple directories.
pub fn traverse(anchors: &Vec<String>, matches: &ArgMatches) -> BTreeMap<String, u64> {
    let mut mds = Mutex::new(BTreeMap::new());
    let mut ve = VerboseErrors::new();

    ve.verbose = matches.occurrences_of("verbose") > 0;

    for dir in anchors {
        match visit_dirs(PathBuf::from(dir), &mut mds, &mut ve) {
            Err(err) => {
                eprintln!("Error: {}", err);
                process::exit(1);
            }
            _ => (),
        }
    }

    let disk_space = mds.lock().ok().unwrap().clone();
    disk_space
}

/// Visit_Dirs
///
/// Recursively searches a directory and returns Result<>. Ignores
/// directories with errors and symlinks.
pub fn visit_dirs(
    dir: PathBuf,
    mds: &mut Mutex<BTreeMap<String, u64>>,
    ve: &mut VerboseErrors,
) -> Result<(), DSError> {
    if dir.is_dir() {
        let anchor = dir.to_owned();
        let contents = match fs::read_dir(&dir) {
            Ok(contents) => contents,
            Err(err) => {
                ve.display(&dir, err);
                return Ok(());
            }
        };
        for entry in contents {
            let entry = entry.unwrap();
            let path = entry.path();

            if symlink_or_error(&path, ve) {
                continue;
            }
            if path.is_dir() {
                visit_dirs(path.to_owned(), mds, ve)?;
            } else {
                increment(anchor.to_owned(), &mds, path, ve)?;
            }
        }
    }
    Ok(())
}

/// Symlink_or_Error
///
/// Check if a path is a symlink.  Returns true if path is a symlink
/// or if the metadata results in an error.
fn symlink_or_error(path: &PathBuf, ve: &mut VerboseErrors) -> bool {
    match fs::symlink_metadata(&path) {
        Ok(metadata) => {
            if metadata.file_type().is_symlink() {
                return true;
            }
        }
        Err(err) => {
            ve.display(path, err);
            return true;
        }
    }
    false
}

/// Increment
///
/// Finds filesize for Linux and Windows.  Effectively skips files with
/// errors.  Increment the size of the path and all ancestors.
fn increment(
    anchor: PathBuf,
    mds: &Mutex<BTreeMap<String, u64>>,
    path: PathBuf,
    ve: &mut VerboseErrors,
) -> Result<(), DSError> {
    let filesize = match path.metadata() {
        #[cfg(target_os = "linux")]
        Ok(metadata) => metadata.st_size(),
        #[cfg(target_os = "windows")]
        Ok(metadata) => metadata.file_size(),
        Err(err) => {
            ve.display(&path, err);
            0
        }
    };
    for ancestor in path.ancestors() {
        let ancestor_path = ancestor.to_string_lossy().to_string();
        *mds.lock()?.entry(ancestor_path).or_insert(0) += filesize;
        if anchor == ancestor {
            break;
        }
    }
    Ok(())
}