Skip to content

Instantly share code, notes, and snippets.

@dignifiedquire
Last active September 26, 2019 18:58
Show Gist options
  • Save dignifiedquire/1b2640d82fc742f037c134c8e8299185 to your computer and use it in GitHub Desktop.
Save dignifiedquire/1b2640d82fc742f037c134c8e8299185 to your computer and use it in GitHub Desktop.
async fn compare_path(&self, path_files: Vec<File>) -> Vec<File> {
let changed_files = Vec::with_capacity(path_files.len());
let changed_files = Arc::new(Mutex::new(changed_files));
let mut handles = Vec::with_capacity(path_files.len());
// Check all files
for file in path_files {
let changed_files = Arc::clone(&changed_files);
handles.push(task::spawn( async move {
// Get filename
let file_name = file.path.file_name().unwrap();
// Create Path to backup file
let mut backup_file_path = PathBuf::from(&self.config.backup_path);
backup_file_path.push(file_name);
// If backup_path exists
if backup_file_path.is_file() {
// Get last change
let last_change = fs::metadata(&backup_file_path)
.await
.unwrap();
// Compare with File last change
if last_change.modified().unwrap() < file.last_change {
println!("File changed {:?}", backup_file_path);
changed_files.lock()
.await
.push(file);
}
}
// If backup_path doesn't exist
else {
println!("New file {:?}", backup_file_path);
changed_files.lock()
.await
.push(file);
}
}));
}
// wait for all things to finish
for handle in &handles {
handle.await;
}
changed_files
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment