|
| 1 | +use std::fs; |
| 2 | +use std::io; |
| 3 | + |
| 4 | +pub fn sync_directories(source_dir: &str, destination_dir: &str, delete_files: bool) { |
| 5 | + // Implement synchronization logic here |
| 6 | + println!("Syncing files from '{}' to '{}'", source_dir, destination_dir); |
| 7 | + |
| 8 | + // Example: Copy all files from source to destination |
| 9 | + match fs::read_dir(source_dir) { |
| 10 | + Ok(entries) => { |
| 11 | + for entry in entries { |
| 12 | + if let Ok(entry) = entry { |
| 13 | + let file_name = entry.file_name(); |
| 14 | + let source_path = entry.path(); |
| 15 | + let dest_path = format!("{}/{}", destination_dir, file_name.to_string_lossy()); |
| 16 | + |
| 17 | + if let Err(err) = fs::copy(&source_path, &dest_path) { |
| 18 | + println!("Error copying file: {}", err); |
| 19 | + } |
| 20 | + } |
| 21 | + } |
| 22 | + } |
| 23 | + Err(err) => { |
| 24 | + eprintln!("Error reading source directory: {}", err); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + // Optionally, delete files not present in source directory |
| 29 | + if delete_files { |
| 30 | + delete_extra_files(source_dir, destination_dir); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +fn delete_extra_files(source_dir: &str, destination_dir: &str) { |
| 35 | + // Implement deletion of extra files logic here |
| 36 | + println!("Deleting extra files from '{}'", destination_dir); |
| 37 | + |
| 38 | + // Example: List all files in destination directory and delete those not present in source directory |
| 39 | + match fs::read_dir(destination_dir) { |
| 40 | + Ok(entries) => { |
| 41 | + for entry in entries { |
| 42 | + if let Ok(entry) = entry { |
| 43 | + let file_name = entry.file_name(); |
| 44 | + let source_path = format!("{}/{}", source_dir, file_name.to_string_lossy()); |
| 45 | + let dest_path = entry.path(); |
| 46 | + |
| 47 | + if !fs::metadata(&source_path).is_ok() { |
| 48 | + if let Err(err) = fs::remove_file(&dest_path) { |
| 49 | + eprintln!("Error deleting file: {}", err); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + Err(err) => { |
| 56 | + eprintln!("Error reading destination directory: {}", err); |
| 57 | + } |
| 58 | + } |
| 59 | +} |
0 commit comments