@@ -31,10 +31,11 @@ pub fn zip(policy: Box<dyn StudentFilePolicy>, root_directory: &Path) -> Result<
3131 log:: trace!( "adding directory {}" , path. display( ) ) ;
3232 writer. add_directory_from_path ( path, FileOptions :: default ( ) ) ?;
3333 } else {
34- let file = File :: open ( entry. path ( ) ) ?;
35- let bytes = file
36- . bytes ( )
37- . collect :: < std:: result:: Result < Vec < _ > , std:: io:: Error > > ( ) ?;
34+ let mut file = File :: open ( entry. path ( ) )
35+ . map_err ( |e| Error :: FileOpen ( entry. path ( ) . to_path_buf ( ) , e) ) ?;
36+ let mut bytes = vec ! [ ] ;
37+ file. read_to_end ( & mut bytes)
38+ . map_err ( |e| Error :: FileRead ( entry. path ( ) . to_path_buf ( ) , e) ) ?;
3839 log:: trace!( "writing file {}" , path. display( ) ) ;
3940 writer. start_file_from_path ( path, FileOptions :: default ( ) ) ?;
4041 writer
@@ -62,7 +63,7 @@ pub fn unzip<P: StudentFilePolicy>(policy: P, zip: &Path, target: &Path) -> Resu
6263 let mut unzipped_paths = HashSet :: new ( ) ;
6364
6465 for i in 0 ..zip_archive. len ( ) {
65- let file = zip_archive. by_index ( i) ?;
66+ let mut file = zip_archive. by_index ( i) ?;
6667 let file_path = file. sanitized_name ( ) ;
6768 if !file_path. starts_with ( & project_dir) {
6869 log:: trace!( "skip {}, not in project dir" , file. name( ) ) ;
@@ -76,22 +77,29 @@ pub fn unzip<P: StudentFilePolicy>(policy: P, zip: &Path, target: &Path) -> Resu
7677 log:: trace!( "creating {:?}" , path_in_target) ;
7778 fs:: create_dir_all ( & path_in_target)
7879 . map_err ( |e| Error :: CreateDir ( path_in_target. clone ( ) , e) ) ?;
79- unzipped_paths. insert ( path_in_target. canonicalize ( ) ?) ;
80+ unzipped_paths. insert (
81+ path_in_target
82+ . canonicalize ( )
83+ . map_err ( |e| Error :: Canonicalize ( path_in_target, e) ) ?,
84+ ) ;
8085 } else {
8186 let mut write = true ;
82- let file_contents = file. bytes ( ) . collect :: < std:: result:: Result < Vec < _ > , _ > > ( ) ?;
87+ let mut file_contents = vec ! [ ] ;
88+ file. read_to_end ( & mut file_contents)
89+ . map_err ( |e| Error :: FileRead ( file_path, e) ) ?;
8390 // always overwrite .tmcproject.yml
8491 if path_in_target. exists ( )
8592 && !path_in_target
8693 . file_name ( )
8794 . map ( |o| o == ".tmcproject.yml" )
8895 . unwrap_or_default ( )
8996 {
90- let target_file = File :: open ( & path_in_target)
97+ let mut target_file = File :: open ( & path_in_target)
9198 . map_err ( |e| Error :: OpenFile ( path_in_target. clone ( ) , e) ) ?;
92- let target_file_contents = target_file
93- . bytes ( )
94- . collect :: < std:: result:: Result < Vec < _ > , _ > > ( ) ?;
99+ let mut target_file_contents = vec ! [ ] ;
100+ target_file
101+ . read_to_end ( & mut target_file_contents)
102+ . map_err ( |e| Error :: FileRead ( path_in_target. clone ( ) , e) ) ?;
95103 if file_contents == target_file_contents
96104 || ( policy. is_student_file ( & path_in_target, & target, & tmc_project_yml) ?
97105 && !policy. is_updating_forced ( & path_in_target, & tmc_project_yml) ?)
@@ -101,35 +109,45 @@ pub fn unzip<P: StudentFilePolicy>(policy: P, zip: &Path, target: &Path) -> Resu
101109 }
102110 if write {
103111 log:: trace!( "writing to {}" , path_in_target. display( ) ) ;
104- if let Some ( res) = path_in_target. parent ( ) . map ( fs :: create_dir_all ) {
105- res?;
112+ if let Some ( res) = path_in_target. parent ( ) {
113+ fs :: create_dir_all ( res) . map_err ( |e| Error :: CreateDir ( res . to_path_buf ( ) , e ) ) ?;
106114 }
107115 let mut overwrite_target = File :: create ( & path_in_target)
108116 . map_err ( |e| Error :: CreateFile ( path_in_target. clone ( ) , e) ) ?;
109117 overwrite_target
110118 . write_all ( & file_contents)
111119 . map_err ( |e| Error :: Write ( path_in_target. clone ( ) , e) ) ?;
112- unzipped_paths. insert ( path_in_target. canonicalize ( ) ?) ;
120+ unzipped_paths. insert (
121+ path_in_target
122+ . canonicalize ( )
123+ . map_err ( |e| Error :: Canonicalize ( path_in_target, e) ) ?,
124+ ) ;
113125 }
114126 }
115127 }
116128
117129 // delete non-student files that were not in zip
118130 log:: debug!( "deleting non-student files not in zip" ) ;
119131 for entry in WalkDir :: new ( target) . into_iter ( ) . filter_map ( |e| e. ok ( ) ) {
120- if !unzipped_paths. contains ( & entry. path ( ) . canonicalize ( ) ?)
121- && ( policy. is_updating_forced ( entry. path ( ) , & tmc_project_yml) ?
122- || !policy. is_student_file ( entry. path ( ) , & project_dir, & tmc_project_yml) ?)
132+ if !unzipped_paths. contains (
133+ & entry
134+ . path ( )
135+ . canonicalize ( )
136+ . map_err ( |e| Error :: Canonicalize ( entry. path ( ) . to_path_buf ( ) , e) ) ?,
137+ ) && ( policy. is_updating_forced ( entry. path ( ) , & tmc_project_yml) ?
138+ || !policy. is_student_file ( entry. path ( ) , & project_dir, & tmc_project_yml) ?)
123139 {
124140 if entry. path ( ) . is_dir ( ) {
125141 // delete if empty
126142 if WalkDir :: new ( entry. path ( ) ) . max_depth ( 1 ) . into_iter ( ) . count ( ) == 1 {
127143 log:: debug!( "deleting empty directory {}" , entry. path( ) . display( ) ) ;
128- fs:: remove_dir ( entry. path ( ) ) ?;
144+ fs:: remove_dir ( entry. path ( ) )
145+ . map_err ( |e| Error :: RemoveDir ( entry. path ( ) . to_path_buf ( ) , e) ) ?;
129146 }
130147 } else {
131148 log:: debug!( "removing file {}" , entry. path( ) . display( ) ) ;
132- fs:: remove_file ( entry. path ( ) ) ?;
149+ fs:: remove_file ( entry. path ( ) )
150+ . map_err ( |e| Error :: RemoveFile ( entry. path ( ) . to_path_buf ( ) , e) ) ?;
133151 }
134152 }
135153 }
0 commit comments