Skip to content

Commit

Permalink
Basic exclude functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed May 9, 2017
1 parent 137908b commit 10d113b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[package]
authors = ["Kornel Lesiński <[email protected]>"]
description = "A fast file deduplicator. Replaces duplicate files with identical content with hardlinks."
repository = "https://github.com/pornel/duplicate-kriller"
documentation = "https://github.com/pornel/duplicate-kriller#readme"
name = "duplicate-kriller"
homepage = "https://github.com/pornel/duplicate-kriller"
keywords = ["dupe", "duplicate", "deduplication"]
categories = ["command-line-utilities", "filesystem"]
license = "MIT"
version = "1.1.0"
name = "duplicate-kriller"
repository = "https://github.com/pornel/duplicate-kriller"
version = "1.2.0"

[[bin]]
name = "duplicate-kriller"
Expand Down
3 changes: 3 additions & 0 deletions src/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ fn main() {
opts.optflag("d", "dry-run", "Do not change anything on disk. Only print dupes found");
opts.optflag("s", "small", "Also dedupe small files (smaller than a disk block)");
opts.optflag("q", "quiet", "Hide regular progress output");
opts.optmulti("e", "exclude", "Don't scan directories or files with that filename (wildcards are not supported)", "<exact filename>");
opts.optflag("", "json", "Display results as JSON");
opts.optflag("h", "help", "This help text");

Expand Down Expand Up @@ -67,6 +68,8 @@ fn main() {
}
}

s.exclude(matches.opt_strs("exclude"));

match inner_main(s, matches.free) {
Ok(()) => {},
Err(err) => {
Expand Down
13 changes: 13 additions & 0 deletions src/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::io;
use file::{FileContent, FileSet};
use std::path::{Path, PathBuf};
use std::collections::BTreeMap;
use std::collections::HashSet;
use std::collections::HashMap;
use std::collections::BinaryHeap;
use metadata::Metadata;
Expand Down Expand Up @@ -71,6 +72,7 @@ pub struct Scanner {

scan_listener: Box<ScanListener>,
stats: Stats,
exclude: HashSet<String>,
pub settings: Settings,
}

Expand All @@ -86,9 +88,14 @@ impl Scanner {
to_scan: BinaryHeap::new(),
scan_listener: Box::new(SilentListener),
stats: Stats::default(),
exclude: HashSet::new(),
}
}

pub fn exclude(&mut self, exclude: Vec<String>) {
self.exclude = exclude.into_iter().collect();
}

/// Set the scan listener. Caution: This overrides previously set listeners!
/// Use a multiplexing listener if multiple listeners are required.
pub fn set_listener(&mut self, listener: Box<ScanListener>) {
Expand Down Expand Up @@ -128,6 +135,12 @@ impl Scanner {
// FIXME: store the errors somehow to report them in a controlled manner
for entry in fs::read_dir(path)?.filter_map(|p|p.ok()) {
let path = entry.path();
if let Some(file_name) = path.file_name() {
if self.exclude.contains(file_name.to_string_lossy().as_ref()) {
self.stats.skipped += 1;
continue;
}
}
self.add(path, &entry.metadata()?).unwrap_or_else(|e| println!("{:?}", e));
}
Ok(())
Expand Down
26 changes: 22 additions & 4 deletions tests/scantest.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
extern crate tempdir;
extern crate file;
extern crate duplicate_kriller;

use duplicate_kriller::*;
use std::io::Write;
use std::fs;
use tempdir::TempDir;

Expand All @@ -13,16 +13,34 @@ fn scan() {
}


#[test]
fn test_exclude() {
let dir = TempDir::new("excludetest").unwrap();
let a_path = dir.path().join("a");
let b_path = dir.path().join("b");
file::put(a_path, "foo").unwrap();
file::put(b_path, "foo").unwrap();

let mut d = Scanner::new();
d.settings.ignore_small = false;
d.settings.run_mode = RunMode::DryRunNoMerging;
d.exclude(vec!["b".to_string()]);

d.scan(dir.path()).unwrap();
let dupes = d.dupes();
assert_eq!(dupes.len(), 1);
assert_eq!(dupes[0].len(), 1);
assert_eq!(dupes[0][0].paths.len(), 1);
}

#[test]
fn scan_hardlink() {

let dir = TempDir::new("hardlinktest2").unwrap();
let a_path = dir.path().join("a");
let b_path = dir.path().join("b");

let mut a_fd = fs::File::create(&a_path).unwrap();
a_fd.write_all(b"dupe").unwrap();
drop(a_fd);
file::put(&a_path, b"dupe").unwrap();

fs::hard_link(&a_path, &b_path).unwrap();

Expand Down

0 comments on commit 10d113b

Please sign in to comment.