@@ -4,6 +4,9 @@ use std::fs::{create_dir, create_dir_all, read_dir, remove_dir_all, Metadata};
44use std:: path:: { Path , PathBuf } ;
55use std:: time:: SystemTime ;
66
7+ #[ cfg( feature = "filetime" ) ]
8+ use crate :: time:: { TimeOptions , copy_time} ;
9+
710/// Options and flags which can be used to configure how a file will be copied or moved.
811#[ derive( Clone ) ]
912pub struct CopyOptions {
@@ -21,6 +24,9 @@ pub struct CopyOptions {
2124 ///
2225 /// Warning: Work only for copy operations!
2326 pub depth : u64 ,
27+ /// File time options.
28+ #[ cfg( feature = "filetime" ) ]
29+ pub time_options : TimeOptions ,
2430}
2531
2632impl CopyOptions {
@@ -43,6 +49,8 @@ impl CopyOptions {
4349 copy_inside : false ,
4450 content_only : false ,
4551 depth : 0 ,
52+ #[ cfg( feature = "filetime" ) ]
53+ time_options : TimeOptions :: new ( ) ,
4654 }
4755 }
4856}
@@ -561,17 +569,9 @@ where
561569 }
562570
563571 let dir_content = get_dir_content2 ( from, & read_options) ?;
564- for directory in dir_content. directories {
565- let tmp_to = Path :: new ( & directory) . strip_prefix ( from) ?;
566- let dir = to. join ( & tmp_to) ;
567- if !dir. exists ( ) {
568- if options. copy_inside {
569- create_all ( dir, false ) ?;
570- } else {
571- create ( dir, false ) ?;
572- }
573- }
574- }
572+
573+ copy_dir_structure ( from, to. as_path ( ) , & dir_content, & options) ;
574+
575575 let mut result: u64 = 0 ;
576576 for file in dir_content. files {
577577 let to = to. to_path_buf ( ) ;
@@ -582,6 +582,8 @@ where
582582 overwrite : options. overwrite ,
583583 skip_exist : options. skip_exist ,
584584 buffer_size : options. buffer_size ,
585+ #[ cfg( feature = "filetime" ) ]
586+ time_options : options. time_options . clone ( ) ,
585587 } ;
586588 let mut result_copy: Result < u64 > ;
587589 let mut work = true ;
@@ -841,17 +843,8 @@ where
841843 }
842844
843845 let dir_content = get_dir_content2 ( from, & read_options) ?;
844- for directory in dir_content. directories {
845- let tmp_to = Path :: new ( & directory) . strip_prefix ( from) ?;
846- let dir = to. join ( & tmp_to) ;
847- if !dir. exists ( ) {
848- if options. copy_inside {
849- create_all ( dir, false ) ?;
850- } else {
851- create ( dir, false ) ?;
852- }
853- }
854- }
846+
847+ copy_dir_structure ( from, to. as_path ( ) , & dir_content, & options) ;
855848
856849 let mut result: u64 = 0 ;
857850 let mut info_process = TransitProcess {
@@ -880,6 +873,8 @@ where
880873 overwrite : options. overwrite ,
881874 skip_exist : options. skip_exist ,
882875 buffer_size : options. buffer_size ,
876+ #[ cfg( feature = "filetime" ) ]
877+ time_options : options. time_options . clone ( ) ,
883878 } ;
884879
885880 if let Some ( file_name) = file_name. to_str ( ) {
@@ -1054,17 +1049,9 @@ where
10541049 to. push ( dir_name) ;
10551050 }
10561051 let dir_content = get_dir_content ( from) ?;
1057- for directory in dir_content. directories {
1058- let tmp_to = Path :: new ( & directory) . strip_prefix ( from) ?;
1059- let dir = to. join ( & tmp_to) ;
1060- if !dir. exists ( ) {
1061- if options. copy_inside {
1062- create_all ( dir, false ) ?;
1063- } else {
1064- create ( dir, false ) ?;
1065- }
1066- }
1067- }
1052+
1053+ copy_dir_structure ( from, to. as_path ( ) , & dir_content, & options) ;
1054+
10681055 let mut result: u64 = 0 ;
10691056 for file in dir_content. files {
10701057 let to = to. to_path_buf ( ) ;
@@ -1075,6 +1062,8 @@ where
10751062 overwrite : options. overwrite ,
10761063 skip_exist : options. skip_exist ,
10771064 buffer_size : options. buffer_size ,
1065+ #[ cfg( feature = "filetime" ) ]
1066+ time_options : options. time_options . clone ( ) ,
10781067 } ;
10791068
10801069 let mut result_copy: Result < u64 > ;
@@ -1178,17 +1167,8 @@ where
11781167 }
11791168
11801169 let dir_content = get_dir_content ( from) ?;
1181- for directory in dir_content. directories {
1182- let tmp_to = Path :: new ( & directory) . strip_prefix ( from) ?;
1183- let dir = to. join ( & tmp_to) ;
1184- if !dir. exists ( ) {
1185- if options. copy_inside {
1186- create_all ( dir, false ) ?;
1187- } else {
1188- create ( dir, false ) ?;
1189- }
1190- }
1191- }
1170+
1171+ copy_dir_structure ( from, to. as_path ( ) , & dir_content, & options) ;
11921172
11931173 let mut result: u64 = 0 ;
11941174 let mut info_process = TransitProcess {
@@ -1217,6 +1197,8 @@ where
12171197 overwrite : options. overwrite ,
12181198 skip_exist : options. skip_exist ,
12191199 buffer_size : options. buffer_size ,
1200+ #[ cfg( feature = "filetime" ) ]
1201+ time_options : options. time_options . clone ( ) ,
12201202 } ;
12211203
12221204 if let Some ( file_name) = file_name. to_str ( ) {
@@ -1345,3 +1327,21 @@ pub fn remove<P: AsRef<Path>>(path: P) -> Result<()> {
13451327 Ok ( ( ) )
13461328 }
13471329}
1330+
1331+ fn copy_dir_structure ( from : & Path , to : & Path , dir_content : & DirContent , options : & CopyOptions ) -> Result < ( ) > {
1332+ for directory in & dir_content. directories {
1333+ let path_from = Path :: new ( directory) ;
1334+ let tmp_to = path_from. strip_prefix ( from) ?;
1335+ let dir = to. join ( & tmp_to) ;
1336+ if !dir. exists ( ) {
1337+ if options. copy_inside {
1338+ create_all ( dir. as_path ( ) , false ) ?;
1339+ } else {
1340+ create ( dir. as_path ( ) , false ) ?;
1341+ }
1342+ #[ cfg( feature = "filetime" ) ]
1343+ copy_time ( & path_from, dir, & options. time_options ) ;
1344+ }
1345+ }
1346+ Ok ( ( ) )
1347+ }
0 commit comments