diff --git a/.test-replica/config/config.toml b/.test-replica/config/config.toml index 7d8dfdd..6a4539f 100644 --- a/.test-replica/config/config.toml +++ b/.test-replica/config/config.toml @@ -9,6 +9,7 @@ files = [ "file2.txt", "big-file.pdf", ] +excludes = [] dbfile = ".replica/data/files.json" compress = false encrypt = false diff --git a/.test-replica/config/plaza-config.toml b/.test-replica/config/plaza-config.toml index 3a1984c..e35ef4f 100644 --- a/.test-replica/config/plaza-config.toml +++ b/.test-replica/config/plaza-config.toml @@ -6,5 +6,6 @@ logging_config = "config/console.yaml" source_folders = ["photos", "Music", ".local/bin" ] targets = ["/Volumes/plaza/plaza", "dpw@piedmont.local:backup/plaza", "dpw@orinda.local:.backup/plaza"] files = [ ".age/decrypt.sh", ".age/encrypt.sh", ".age/key.enc", ".alias", ".config", ".motd", ".profile", ".ssh/authorized_keys", ".ssh/plaza.pub", ".zprofile", ".zshenv", ".zshrc" ] +excludes = [] compress = false encrypt = false diff --git a/.test-replica/config/run-config.toml b/.test-replica/config/run-config.toml index ad64021..821bf3a 100644 --- a/.test-replica/config/run-config.toml +++ b/.test-replica/config/run-config.toml @@ -10,6 +10,7 @@ files = [ "tests/big-file.pdf", "tests/changed-file.txt", ] +excludes = [] dbfile = ".test-replica/data/run2-file.json" compress = false encrypt = false diff --git a/.test-replica/config/run1-config.toml b/.test-replica/config/run1-config.toml index 381f48d..ed66408 100644 --- a/.test-replica/config/run1-config.toml +++ b/.test-replica/config/run1-config.toml @@ -10,6 +10,7 @@ files = [ "tests/big-file.pdf", "tests/changed-file.txt", ] +excludes = [] dbfile = ".test-replica/data/run1-file.json" compress = false encrypt = false diff --git a/.test-replica/config/tiburon-config.toml b/.test-replica/config/tiburon-config.toml index a23c272..cc16ff9 100644 --- a/.test-replica/config/tiburon-config.toml +++ b/.test-replica/config/tiburon-config.toml @@ -6,5 +6,6 @@ logging_config = "config/console.yaml" source_folders = ["photos", "Music", ".local/bin"] targets = ["dpw@plaza.local:/Volumes/plaza/tiburon", "dpw@piedmont.local:backup/tiburon", "dpw@orinda.local:.backup/tiburon"] files = [ ".alias", ".config", ".motd", ".profile", ".ssh/authorized_keys", ".ssh/id_rsa.pub", ".wget-hsts", ".zprofile", ".zshenv", ".zshrc" ] +excludes = [] compress = false encrypt = false diff --git a/.test-replica/config/walk-config.toml b/.test-replica/config/walk-config.toml index 9994443..03b1cc2 100644 --- a/.test-replica/config/walk-config.toml +++ b/.test-replica/config/walk-config.toml @@ -9,6 +9,12 @@ files = [ "tests/file2.txt", "tests/big-file.pdf", ] +excludes = [ + ".config/broot", + ".config/chromium", + ".config/configstore", + ".config/dconf", +] dbfile = ".replica/data/files.json" compress = false encrypt = false diff --git a/src/config.rs b/src/config.rs index 7ac305a..52197cf 100644 --- a/src/config.rs +++ b/src/config.rs @@ -18,6 +18,7 @@ pub struct Config { pub source_folders: Vec, pub targets: Vec, pub files: Vec, + pub excludes: Vec, pub dbfile: String, pub compress: bool, pub encrypt: bool, @@ -48,6 +49,7 @@ impl Config { source_folders: self.source_folders.clone(), targets: self.targets.clone(), files: self.files.clone(), + excludes: self.excludes.clone(), dbfile: self.dbfile.clone(), compress: self.compress, encrypt: self.encrypt, diff --git a/src/file_walker.rs b/src/file_walker.rs index 7b5846b..720e147 100644 --- a/src/file_walker.rs +++ b/src/file_walker.rs @@ -2,7 +2,7 @@ use crate::config::Config; use crate::file_model::FileModel; use anyhow::Result; use log::{debug, error, info}; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use walkdir::WalkDir; pub struct FileWalker { @@ -67,7 +67,7 @@ impl FileWalker { let fname: PathBuf = [&self.home, folder].iter().collect(); for entry in WalkDir::new(fname).into_iter().filter_map(|e| e.ok()) { - if entry.file_name() == ".DS_Store" { + if self.exclude(entry.path()) || entry.file_name() == ".DS_Store" { continue; } @@ -92,12 +92,41 @@ impl FileWalker { Ok(files) } + + /// if the file path contains an exclude phrase return true, else false + fn exclude(&self, path: &Path) -> bool { + let excludes = &self.config.excludes; + let name = path.to_str().unwrap().to_string(); + + for ex in excludes.iter() { + if name.contains(ex.as_str()) { + info!("exclude: {} {}", name, ex); + return true; + } + } + + false + } } #[cfg(test)] mod tests { use super::*; + #[test] + fn excludes() { + // [ ".config/broot", ".config/chromium", ".config/configstore", ".config/dconf" ] + + let config = Config::read_config(".test-replica/config/walk-config.toml").unwrap(); + let walker = FileWalker::new(config.clone()); + + let path = Path::new("/home/dpw/.config/chromium/thing"); + assert!(walker.exclude(path)); + + let path = Path::new(".config/configstore/"); + assert!(walker.exclude(path)); + } + #[test] fn walk_files_and_folders() { // cd_test_home();