|
1 | | -use std::time::SystemTime; |
| 1 | +use std::{path::PathBuf, time::SystemTime}; |
2 | 2 | use walkdir::{DirEntry, WalkDir}; |
3 | 3 |
|
4 | 4 | pub struct SyncData { |
5 | | - pub source: String, |
6 | | - pub destination: String, |
| 5 | + pub source: PathBuf, |
| 6 | + pub destination: PathBuf, |
7 | 7 | pub incremental: Option<bool>, |
8 | 8 | pub delete: Option<bool>, |
9 | 9 | pub dry_run: Option<bool>, |
10 | 10 | pub verbose: Option<bool>, |
11 | 11 | pub hash: Option<bool>, |
12 | 12 | } |
13 | 13 |
|
14 | | -fn file_name(source_entry: DirEntry) { |
15 | | - if let Some(name) = source_entry.path().file_name() { |
16 | | - println!("File: {}", name.to_string_lossy()); |
17 | | - } |
18 | | -} |
19 | | - |
20 | 14 | fn file_types(source_entry: DirEntry) { |
21 | 15 | if let Ok(metadata) = source_entry.metadata() { |
22 | 16 | let size = metadata.len(); |
@@ -58,27 +52,51 @@ fn file_last_modified(source_entry: DirEntry) { |
58 | 52 |
|
59 | 53 | impl SyncData { |
60 | 54 | pub fn sync_output(&self) { |
61 | | - for source_entry in WalkDir::new(&self.source) { |
62 | | - let source_entry = source_entry.expect("Err: failed to get the source entry"); |
63 | | - let file_type = source_entry.file_type(); |
| 55 | + let mut source_files: Vec<PathBuf> = Vec::new(); |
| 56 | + for entry in WalkDir::new(&self.source) { |
| 57 | + let entry = entry.expect("Err: failed to get the source entry"); |
| 58 | + if entry.file_type().is_file() { |
| 59 | + source_files.push(entry.path().to_path_buf()); |
| 60 | + // file_types(source_entry.clone()); |
| 61 | + // file_last_modified(source_entry); |
| 62 | + } |
| 63 | + } |
64 | 64 |
|
65 | | - if file_type.is_file() { |
66 | | - file_name(source_entry.clone()); |
67 | | - file_types(source_entry.clone()); |
68 | | - file_last_modified(source_entry); |
| 65 | + let mut destination_files: Vec<PathBuf> = Vec::new(); |
| 66 | + for entry in WalkDir::new(&self.destination) { |
| 67 | + let entry = entry.expect("Err: failed to get the destination entry"); |
| 68 | + if entry.file_type().is_file() { |
| 69 | + destination_files.push(entry.path().to_path_buf()); |
| 70 | + // file_types(entry.clone()); |
| 71 | + // file_last_modified(entry); |
69 | 72 | } |
70 | 73 | } |
71 | 74 |
|
72 | | - for destination_entry in WalkDir::new(&self.destination) { |
73 | | - let destination_entry = |
74 | | - destination_entry.expect("Err: failed to get the destination entry"); |
75 | | - let file_type = destination_entry.file_type(); |
| 75 | + let mut copied_files = Vec::new(); |
| 76 | + for src in &source_files { |
| 77 | + let src_file_name = src.file_name().expect("Err: failed to get the file name"); |
| 78 | + |
| 79 | + let mut dest_path = self.destination.clone(); |
| 80 | + dest_path.push(src_file_name); |
76 | 81 |
|
77 | | - if file_type.is_file() { |
78 | | - file_name(destination_entry.clone()); |
79 | | - file_types(destination_entry.clone()); |
80 | | - file_last_modified(destination_entry); |
| 82 | + let mut found = false; |
| 83 | + for dest in &destination_files { |
| 84 | + let dest_file_name = dest.file_name().expect("Err: failed to get the file name"); |
| 85 | + if src_file_name == dest_file_name { |
| 86 | + found = true; |
| 87 | + } |
81 | 88 | } |
| 89 | + |
| 90 | + if !found { |
| 91 | + std::fs::copy(src, dest_path).unwrap(); |
| 92 | + copied_files.push(src_file_name.to_owned()); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + if !copied_files.is_empty() { |
| 97 | + println!("Missing files are copied successfully"); |
| 98 | + } else { |
| 99 | + eprintln!("No missing files to copy. Destination is up-to-date."); |
82 | 100 | } |
83 | 101 |
|
84 | 102 | // println!("{}", self.incremental.unwrap()); |
|
0 commit comments