diff --git a/README.md b/README.md index 85ad74c..57c642f 100644 --- a/README.md +++ b/README.md @@ -10,3 +10,6 @@ cargo install klone Once installed run `klone --help` to print the help for this utility ## How to use For detailed instructions on how to use this utility, check [the project's wiki](https://github.com/kinire98/klone/wiki) + + +aksdjñfkljas diff --git a/src/app/mod.rs b/src/app/mod.rs index 7f74668..dec14f4 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -13,12 +13,20 @@ pub fn backup(origin_dir: PathBuf, target_dir: PathBuf) -> Result<()> { let path_target_dir: Box = target_dir.clone().into(); // If the target directory is empty is not worth checking the times // Just copy it directly - if !path_target_dir .read_dir() .expect("Temporary") - .any(|path| path.expect("Temporary").path() == target_dir) + .map(|path| { + path.expect("Temporary") + .path() + .canonicalize() + .expect("Shouldn't panic") + }) + .filter(|path| path.iter().last() == origin_dir.iter().last()) + .next() + .is_some() { + dbg!("Here"); return initial_copy(origin_dir, target_dir); } let target_dir = target_dir.join(origin_dir.iter().last().expect("Temporary")); diff --git a/src/config/exclusions/cache.rs b/src/config/exclusions/cache.rs new file mode 100644 index 0000000..41d21d8 --- /dev/null +++ b/src/config/exclusions/cache.rs @@ -0,0 +1,25 @@ +use crate::error::*; + +#[cfg(unix)] +const EXCLUSIONS_PATH: &str = "/etc/klone/exclusions.json"; +#[cfg(windows)] +const EXCLUSIONS_PATH: &str = r"C:\ProgramData\klone\exclusions.json"; + +static mut CACHED_EXCLUSIONS: Vec = Vec::new(); + +pub fn get_exclusions() -> Result> { + if unsafe { CACHED_EXCLUSIONS.len() != 0 } { + return unsafe { Ok(CACHED_EXCLUSIONS.clone()) }; + } + // Get the file contents + let file_contents = std::fs::read_to_string(EXCLUSIONS_PATH).map_err(|_| Error { + kind: ErrorKind::FSError, + })?; + // Deserialize the json + let deserialized: Vec = + serde_json::from_str(&file_contents).expect("Should be valid JSON"); + unsafe { + CACHED_EXCLUSIONS = deserialized.clone(); + } + Ok(deserialized) +} diff --git a/src/config/exclusions.rs b/src/config/exclusions/mod.rs similarity index 92% rename from src/config/exclusions.rs rename to src/config/exclusions/mod.rs index 9968057..65fa2c1 100644 --- a/src/config/exclusions.rs +++ b/src/config/exclusions/mod.rs @@ -15,16 +15,12 @@ const SYS_EXCLUSIONS: &[&str] = &["*/.git", "*.o", "*.bin", "*.lock"]; #[derive(Serialize, Deserialize, Debug)] struct Exclusions(Vec); +mod cache; + pub fn is_excluded(pattern: &str) -> Result { - // Get the file contents - let file_contents = std::fs::read_to_string(EXCLUSIONS_PATH).map_err(|_| Error { - kind: ErrorKind::FSError, - })?; - // Deserialize the json - let deserialized: Exclusions = - serde_json::from_str(&file_contents).expect("Should be valid JSON"); - let mut iter = deserialized.0.iter().filter(|file| { - Pattern::new(file.as_str()) + let binding = cache::get_exclusions()?; + let mut iter = binding.iter().filter(|file| { + Pattern::new(file) .expect("This should't panic") // The pattern was already checked when added .matches(pattern) });