Skip to content

Commit

Permalink
Fixed the implementation that 'solved' #1 and solved #4
Browse files Browse the repository at this point in the history
  • Loading branch information
kinire98 committed Mar 21, 2024
1 parent 5367dd1 commit c5d9be0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 10 additions & 2 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,20 @@ pub fn backup(origin_dir: PathBuf, target_dir: PathBuf) -> Result<()> {
let path_target_dir: Box<Path> = 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"));
Expand Down
25 changes: 25 additions & 0 deletions src/config/exclusions/cache.rs
Original file line number Diff line number Diff line change
@@ -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<String> = Vec::new();

pub fn get_exclusions() -> Result<Vec<String>> {
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<String> =
serde_json::from_str(&file_contents).expect("Should be valid JSON");
unsafe {
CACHED_EXCLUSIONS = deserialized.clone();
}
Ok(deserialized)
}
14 changes: 5 additions & 9 deletions src/config/exclusions.rs → src/config/exclusions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,12 @@ const SYS_EXCLUSIONS: &[&str] = &["*/.git", "*.o", "*.bin", "*.lock"];
#[derive(Serialize, Deserialize, Debug)]
struct Exclusions(Vec<String>);

mod cache;

pub fn is_excluded(pattern: &str) -> Result<bool> {
// 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)
});
Expand Down

0 comments on commit c5d9be0

Please sign in to comment.