11use std:: {
2- path:: PathBuf ,
2+ fs:: copy,
3+ path:: { Path , PathBuf } ,
34 time:: { SystemTime , UNIX_EPOCH } ,
45} ;
56use time:: { OffsetDateTime , macros:: format_description} ;
@@ -8,11 +9,11 @@ use walkdir::{DirEntry, WalkDir};
89pub struct SyncData {
910 pub source : PathBuf ,
1011 pub destination : PathBuf ,
11- pub incremental : Option < bool > ,
12- pub delete : Option < bool > ,
13- pub dry_run : Option < bool > ,
14- pub verbose : Option < bool > ,
15- pub hash : Option < bool > ,
12+ pub changed_only : bool ,
13+ pub delete : bool ,
14+ pub dry_run : bool ,
15+ pub verbose : bool ,
16+ pub hash : bool ,
1617}
1718
1819fn file_size ( source_entry : DirEntry ) -> ( f64 , & ' static str ) {
@@ -84,47 +85,71 @@ impl SyncData {
8485 }
8586 }
8687
87- // Compare source and destination files
88- for ( src_path, src_modified) in & source_files {
89- let src_file_name = src_path
90- . file_name ( )
91- . expect ( "Err: failed to get the file name" ) ;
92- let mut dest_path = self . destination . clone ( ) ;
93- dest_path. push ( src_file_name) ;
88+ if self . changed_only {
89+ // Compare source and destination files
90+ for ( src_path, src_modified) in & source_files {
91+ let src_file_name = src_path
92+ . file_name ( )
93+ . expect ( "Err: failed to get the file name(s)" ) ;
94+ let mut dest_path = self . destination . clone ( ) ;
95+ dest_path. push ( src_file_name) ;
9496
95- let mut found = false ;
96- let mut needs_update = false ;
97+ let mut found = false ;
98+ let mut needs_update = false ;
9799
98- for ( dest_path, dest_modified) in & destination_files {
99- let dest_file_name = dest_path
100- . file_name ( )
101- . expect ( "Err: failed to get the file name" ) ;
102- if src_file_name == dest_file_name {
103- found = true ;
104- if src_modified > dest_modified {
105- needs_update = true ;
100+ for ( dest_path, dest_modified) in & destination_files {
101+ let dest_file_name = dest_path
102+ . file_name ( )
103+ . expect ( "Err: failed to get the file name(s)" ) ;
104+ if src_file_name == dest_file_name {
105+ found = true ;
106+ if src_modified > dest_modified {
107+ needs_update = true ;
108+ }
109+ break ;
106110 }
107- break ;
111+ }
112+
113+ if !found {
114+ std:: fs:: copy ( src_path, & dest_path) . expect ( "Err: failed to copy file(s)" ) ;
115+ files_to_be_copied. push ( src_file_name. to_owned ( ) ) ;
116+ } else if needs_update {
117+ std:: fs:: copy ( src_path, & dest_path) . expect ( "Err: failed to update file(s)" ) ;
118+ files_to_be_updated. push ( src_file_name. to_owned ( ) ) ;
108119 }
109120 }
110121
111- if !found {
112- std:: fs:: copy ( src_path, & dest_path) . expect ( "Err: failed to copy file" ) ;
113- files_to_be_copied. push ( src_file_name. to_owned ( ) ) ;
114- } else if needs_update {
115- std:: fs:: copy ( src_path, & dest_path) . expect ( "Err: failed to update file" ) ;
116- files_to_be_updated. push ( src_file_name. to_owned ( ) ) ;
122+ if !files_to_be_copied. is_empty ( ) {
123+ println ! (
124+ "Missing files {:?} are copied to {:?} destination" ,
125+ files_to_be_copied, self . destination
126+ ) ;
117127 }
118- }
128+ if !files_to_be_updated. is_empty ( ) {
129+ println ! ( "Files are updated: {:?}" , files_to_be_updated) ;
130+ }
131+ if files_to_be_copied. is_empty ( ) && files_to_be_updated. is_empty ( ) {
132+ eprintln ! ( "No missing or outdated files. Destination is up-to-date." ) ;
133+ }
134+ } else {
135+ if !destination_files. is_empty ( ) {
136+ for ( dest_path, _) in & destination_files {
137+ std:: fs:: remove_file ( dest_path) . expect ( "Err: failed to delete the file(s)" ) ;
138+ }
119139
120- if !files_to_be_copied. is_empty ( ) {
121- println ! ( "Missing files copied: {:?}" , files_to_be_copied) ;
122- }
123- if !files_to_be_updated. is_empty ( ) {
124- println ! ( "Files updated: {:?}" , files_to_be_updated) ;
125- }
126- if files_to_be_copied. is_empty ( ) && files_to_be_updated. is_empty ( ) {
127- eprintln ! ( "No missing or outdated files. Destination is up-to-date." ) ;
140+ let dest_dir = Path :: new ( & self . destination ) ;
141+
142+ for ( src_path, _) in & source_files {
143+ let file_name = src_path
144+ . file_name ( )
145+ . expect ( "Err: failed to get the file name(s)" ) ;
146+ let new_dest_path = dest_dir. join ( file_name) ;
147+ copy ( src_path, new_dest_path) . expect ( "Err: failed to copy the file(s)" ) ;
148+ }
149+ println ! ( "All the files are copied to destination successfully" ) ;
150+ } else {
151+ eprintln ! ( "Err: destination path is empty" ) ;
152+ }
128153 }
129154 }
130155
0 commit comments