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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
//! Option parsing and management.
//!
//! Use the `Options::parse()` function to get the program's configuration,
//! as parsed from the commandline.
//!
//! # Examples
//!
//! ```skip
//! let opts = Options::parse();
//! println!("{:?}", opts);
//! ```


use clap::{self, App, Arg, AppSettings};
use self::super::Algorithm;
use std::path::PathBuf;
use std::str::FromStr;
use std::fs;


/// Representation of the application's all configurable values.
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct Options {
    /// Directory to hach/verify. Default: `"."`
    pub dir: PathBuf,
    /// Hashing algorithm to use. Default: `"SHA1"`
    pub algorithm: Algorithm,
    /// Whether to verify or create checksums. Default: yes
    pub verify: bool,
    /// Max recursion depth. Default: `LastLevel`
    pub depth: DepthSetting,
    /// In-/Output filename. Default: `"./INFERRED_FROM_DIRECTORY.hash"`
    pub file: (String, PathBuf),
}

/// Representation of how deep recursion should be.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum DepthSetting {
    /// Infinite allowed recursion, `-1` in argument
    Infinite,
    /// Last recursion level, go no further. `0` in argument. The default
    LastLevel,
    /// Another `N` recursion levels remaining.
    ///
    /// Entering a value `<= 0` here yields unspecified behaviour.
    NRemaining(u32),
}


impl Options {
    /// Parse `env`-wide command-line arguments into an `Options` instance
    pub fn parse() -> Options {
        let matches = App::new("checksums")
            .setting(AppSettings::ColoredHelp)
            .version(crate_version!())
            .author(crate_authors!())
            .about("Tool for making/verifying checksums of directory trees")
            .args(&[Arg::from_usage("[DIRECTORY] 'Directory to hash/verify'").default_value(".").validator(Options::directory_validator),
                    Arg::from_usage("--algorithm=[algorithm] -a 'Hashing algorithm to use. {n}\
                                     Supported algorithms: SHA{1,2-{256,512},3-{256,512}}, BLAKE{,2}, CRC{64,32,16,8}, MD5, XOR8'")
                        .next_line_help(true)
                        .default_value("SHA1")
                        .validator(Options::algorithm_validator),
                    Arg::from_usage("--create -c 'Make checksums'").overrides_with("verify"),
                    Arg::from_usage("--verify -v 'Verify checksums (default)'").overrides_with("create"),
                    Arg::from_usage("--depth=[depth] -d 'Max recursion depth. `-1` for infinite.'. Default: don't recurse")
                        .validator(Options::depth_validator)
                        .overrides_with("recursive"),
                    Arg::from_usage("--recursive -r 'Infinite recursion depth.'").overrides_with("depth"),
                    Arg::from_usage("--file=[file] -f 'File with hashes to be read/created'").validator(Options::file_validator),
                    Arg::from_usage("--force 'Override output file'")])
            .get_matches();

        let dir = fs::canonicalize(matches.value_of("DIRECTORY").unwrap()).unwrap();
        let verify = !matches.is_present("create");
        let file = Options::file_process(matches.value_of("file"), &dir);

        if file.1.exists() && !verify && !matches.is_present("force") {
            clap::Error {
                    message: "The output file exists and was not overridden to prevent data loss.\n\
                              Pass the --force option to suppress this error."
                        .to_string(),
                    kind: clap::ErrorKind::MissingRequiredArgument,
                    info: None,
                }
                .exit();
        }

        Options {
            dir: dir,
            algorithm: Algorithm::from_str(matches.value_of("algorithm").unwrap()).unwrap(),
            verify: verify,
            depth: if matches.is_present("recursive") {
                DepthSetting::Infinite
            } else {
                DepthSetting::from_str(matches.value_of("depth").unwrap_or("0")).unwrap()
            },
            file: file,
        }
    }

    fn algorithm_validator(s: String) -> Result<(), String> {
        Algorithm::from_str(&s).map(|_| ())
    }

    fn directory_validator(s: String) -> Result<(), String> {
        fs::canonicalize(s).map_err(|e| format!("directory: {}", e.to_string())).and_then(|p| {
            if p.is_file() {
                Err("DIRECTORY cannot be a file.".to_string())
            } else {
                Ok(())
            }
        })
    }

    fn depth_validator(s: String) -> Result<(), String> {
        DepthSetting::from_str(&s).map(|_| ())
    }

    fn file_validator(s: String) -> Result<(), String> {
        let mut buf = PathBuf::from(s);
        if buf.exists() && buf.is_dir() {
            Err("file exists and is a directory".to_string())
        } else {
            buf.pop();

            // Handle pathless filename
            if buf.as_os_str().is_empty() {
                Ok(())
            } else {
                buf.canonicalize().map(|_| ()).map_err(|e| format!("file: {}", e.to_string()))
            }
        }
    }

    fn file_process(file: Option<&str>, dir: &PathBuf) -> (String, PathBuf) {
        match file {
            Some(file) => {
                let mut file = PathBuf::from(file);
                let file_name = file.file_name().unwrap().to_os_string();

                file.pop();
                // Handle pathless filename
                if file.as_os_str().is_empty() {
                    file.push(".");
                }

                (file_name.to_str().unwrap().to_string(),
                 file.canonicalize()
                    .map(|mut p| {
                        p.push(file_name);
                        p
                    })
                    .unwrap())
            }
            None => {
                let mut file = dir.clone();
                file.push(dir.file_name().unwrap());
                file.set_extension("hash");
                (file.file_name().unwrap().to_str().unwrap().to_string(), file)
            }
        }
    }
}


impl DepthSetting {
    /// Check if this depth can go one level deeper.
    ///
    /// If so, `next_level()` will return `Some`, otherwise, `None`.
    ///
    /// # Examples
    ///
    /// ```
    /// assert!(DepthSetting::Infinite.can_recurse());
    ///
    /// assert_false!(DepthSetting::LastLevel.can_recurse());
    ///
    /// assert!(DepthSetting::NRemaining(1).can_recurse());
    /// assert!(DepthSetting::NRemaining(100).can_recurse());
    /// ```
    pub fn can_recurse(&self) -> bool {
        match self {
            &DepthSetting::Infinite |
            &DepthSetting::NRemaining(_) => true,
            &DepthSetting::LastLevel => false,
        }
    }

    /// Get the next recursion level, if one exists.
    ///
    /// The next recursion level does *not* exist only for `LastLevel`.
    ///
    /// # Examples
    ///
    /// ```
    /// // Normally you'd acquire from elsewhere.
    /// let depth = DepthSetting::NRemaining(1);
    ///
    /// if let Some(next) = depth.next_level() {
    ///     // Recurse into the next level...
    ///     assert_eq!(next, DepthSetting::LastLevel);
    /// }
    /// ```
    pub fn next_level(&self) -> Option<Self> {
        match self {
            &DepthSetting::Infinite => Some(DepthSetting::Infinite),
            &DepthSetting::NRemaining(n) => Some(Self::from(n as i32 - 1)),
            &DepthSetting::LastLevel => None,
        }
    }
}

impl FromStr for DepthSetting {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        i32::from_str(&s).map(DepthSetting::from).map_err(|e| e.to_string())
    }
}

impl From<i32> for DepthSetting {
    fn from(n: i32) -> Self {
        if n < 0 {
            DepthSetting::Infinite
        } else if n > 0 {
            DepthSetting::NRemaining(n as u32)
        } else {
            DepthSetting::LastLevel
        }
    }
}


#[cfg(test)]
mod tests {
    mod depth {
        use self::super::super::DepthSetting;
        use std::str::FromStr;


        mod can_recurse {
            use self::super::super::super::DepthSetting;


            #[test]
            fn doctest() {
                assert!(DepthSetting::Infinite.can_recurse());

                assert!(!DepthSetting::LastLevel.can_recurse());

                assert!(DepthSetting::NRemaining(1).can_recurse());
                assert!(DepthSetting::NRemaining(100).can_recurse());
            }
        }

        mod next_level {
            use self::super::super::super::DepthSetting;


            #[test]
            fn doctest() {
                // Normally you'd acquire from elsewhere.
                let depth = DepthSetting::NRemaining(1);

                if let Some(next) = depth.next_level() {
                    // Recurse into the next level...
                    assert_eq!(next, DepthSetting::LastLevel);
                }
            }

            #[test]
            fn infinite() {
                assert_eq!(DepthSetting::Infinite.next_level(), Some(DepthSetting::Infinite));
            }

            #[test]
            fn last_level() {
                assert_eq!(DepthSetting::LastLevel.next_level(), None);
            }

            #[test]
            fn nremaining() {
                assert_eq!(DepthSetting::NRemaining(1).next_level(), Some(DepthSetting::LastLevel));

                assert_eq!(DepthSetting::NRemaining(2).next_level(), Some(DepthSetting::NRemaining(1)));
                assert_eq!(DepthSetting::NRemaining(100).next_level(), Some(DepthSetting::NRemaining(99)));
            }
        }

        #[test]
        fn from_str() {

            for p in &[("-1", DepthSetting::Infinite),
                       ("-100", DepthSetting::Infinite),
                       ("0", DepthSetting::LastLevel),
                       ("1", DepthSetting::NRemaining(1)),
                       ("2", DepthSetting::NRemaining(2)),
                       ("100", DepthSetting::NRemaining(100))] {
                assert_eq!(DepthSetting::from_str(p.0).unwrap(), p.1);
            }
        }

        #[test]
        fn from_str_bad() {
            for s in &["a234", "1231d"] {
                DepthSetting::from_str(s).unwrap_err();
            }
        }
    }
}