11//! Contains functions for zipping and unzipping projects.
22
33use crate :: policy:: StudentFilePolicy ;
4- use crate :: { Error , Result } ;
4+ use crate :: { Result , TmcError } ;
55use std:: collections:: HashSet ;
66use std:: fs:: { self , File } ;
77use std:: io:: { Cursor , Read , Seek , Write } ;
@@ -32,15 +32,15 @@ pub fn zip(policy: Box<dyn StudentFilePolicy>, root_directory: &Path) -> Result<
3232 writer. add_directory_from_path ( path, FileOptions :: default ( ) ) ?;
3333 } else {
3434 let mut file = File :: open ( entry. path ( ) )
35- . map_err ( |e| Error :: FileOpen ( entry. path ( ) . to_path_buf ( ) , e) ) ?;
35+ . map_err ( |e| TmcError :: FileOpen ( entry. path ( ) . to_path_buf ( ) , e) ) ?;
3636 let mut bytes = vec ! [ ] ;
3737 file. read_to_end ( & mut bytes)
38- . map_err ( |e| Error :: FileRead ( entry. path ( ) . to_path_buf ( ) , e) ) ?;
38+ . map_err ( |e| TmcError :: FileRead ( entry. path ( ) . to_path_buf ( ) , e) ) ?;
3939 log:: trace!( "writing file {}" , path. display( ) ) ;
4040 writer. start_file_from_path ( path, FileOptions :: default ( ) ) ?;
4141 writer
4242 . write_all ( & bytes)
43- . map_err ( |e| Error :: Write ( path. to_path_buf ( ) , e) ) ?;
43+ . map_err ( |e| TmcError :: Write ( path. to_path_buf ( ) , e) ) ?;
4444 }
4545 }
4646 }
@@ -52,7 +52,7 @@ pub fn zip(policy: Box<dyn StudentFilePolicy>, root_directory: &Path) -> Result<
5252pub fn unzip < P : StudentFilePolicy > ( policy : P , zip : & Path , target : & Path ) -> Result < ( ) > {
5353 log:: debug!( "Unzipping {} to {}" , zip. display( ) , target. display( ) ) ;
5454
55- let file = File :: open ( zip) . map_err ( |e| Error :: OpenFile ( zip. to_path_buf ( ) , e) ) ?;
55+ let file = File :: open ( zip) . map_err ( |e| TmcError :: OpenFile ( zip. to_path_buf ( ) , e) ) ?;
5656 let mut zip_archive = ZipArchive :: new ( file) ?;
5757
5858 let project_dir = find_project_dir ( & mut zip_archive) ?;
@@ -76,17 +76,17 @@ pub fn unzip<P: StudentFilePolicy>(policy: P, zip: &Path, target: &Path) -> Resu
7676 if file. is_dir ( ) {
7777 log:: trace!( "creating {:?}" , path_in_target) ;
7878 fs:: create_dir_all ( & path_in_target)
79- . map_err ( |e| Error :: CreateDir ( path_in_target. clone ( ) , e) ) ?;
79+ . map_err ( |e| TmcError :: CreateDir ( path_in_target. clone ( ) , e) ) ?;
8080 unzipped_paths. insert (
8181 path_in_target
8282 . canonicalize ( )
83- . map_err ( |e| Error :: Canonicalize ( path_in_target, e) ) ?,
83+ . map_err ( |e| TmcError :: Canonicalize ( path_in_target, e) ) ?,
8484 ) ;
8585 } else {
8686 let mut write = true ;
8787 let mut file_contents = vec ! [ ] ;
8888 file. read_to_end ( & mut file_contents)
89- . map_err ( |e| Error :: FileRead ( file_path, e) ) ?;
89+ . map_err ( |e| TmcError :: FileRead ( file_path, e) ) ?;
9090 // always overwrite .tmcproject.yml
9191 if path_in_target. exists ( )
9292 && !path_in_target
@@ -95,11 +95,11 @@ pub fn unzip<P: StudentFilePolicy>(policy: P, zip: &Path, target: &Path) -> Resu
9595 . unwrap_or_default ( )
9696 {
9797 let mut target_file = File :: open ( & path_in_target)
98- . map_err ( |e| Error :: OpenFile ( path_in_target. clone ( ) , e) ) ?;
98+ . map_err ( |e| TmcError :: OpenFile ( path_in_target. clone ( ) , e) ) ?;
9999 let mut target_file_contents = vec ! [ ] ;
100100 target_file
101101 . read_to_end ( & mut target_file_contents)
102- . map_err ( |e| Error :: FileRead ( path_in_target. clone ( ) , e) ) ?;
102+ . map_err ( |e| TmcError :: FileRead ( path_in_target. clone ( ) , e) ) ?;
103103 if file_contents == target_file_contents
104104 || ( policy. is_student_file ( & path_in_target, & target, & tmc_project_yml) ?
105105 && !policy. is_updating_forced ( & path_in_target, & tmc_project_yml) ?)
@@ -110,17 +110,18 @@ pub fn unzip<P: StudentFilePolicy>(policy: P, zip: &Path, target: &Path) -> Resu
110110 if write {
111111 log:: trace!( "writing to {}" , path_in_target. display( ) ) ;
112112 if let Some ( res) = path_in_target. parent ( ) {
113- fs:: create_dir_all ( res) . map_err ( |e| Error :: CreateDir ( res. to_path_buf ( ) , e) ) ?;
113+ fs:: create_dir_all ( res)
114+ . map_err ( |e| TmcError :: CreateDir ( res. to_path_buf ( ) , e) ) ?;
114115 }
115116 let mut overwrite_target = File :: create ( & path_in_target)
116- . map_err ( |e| Error :: CreateFile ( path_in_target. clone ( ) , e) ) ?;
117+ . map_err ( |e| TmcError :: CreateFile ( path_in_target. clone ( ) , e) ) ?;
117118 overwrite_target
118119 . write_all ( & file_contents)
119- . map_err ( |e| Error :: Write ( path_in_target. clone ( ) , e) ) ?;
120+ . map_err ( |e| TmcError :: Write ( path_in_target. clone ( ) , e) ) ?;
120121 unzipped_paths. insert (
121122 path_in_target
122123 . canonicalize ( )
123- . map_err ( |e| Error :: Canonicalize ( path_in_target, e) ) ?,
124+ . map_err ( |e| TmcError :: Canonicalize ( path_in_target, e) ) ?,
124125 ) ;
125126 }
126127 }
@@ -133,7 +134,7 @@ pub fn unzip<P: StudentFilePolicy>(policy: P, zip: &Path, target: &Path) -> Resu
133134 & entry
134135 . path ( )
135136 . canonicalize ( )
136- . map_err ( |e| Error :: Canonicalize ( entry. path ( ) . to_path_buf ( ) , e) ) ?,
137+ . map_err ( |e| TmcError :: Canonicalize ( entry. path ( ) . to_path_buf ( ) , e) ) ?,
137138 ) && ( policy. is_updating_forced ( entry. path ( ) , & tmc_project_yml) ?
138139 || !policy. is_student_file ( entry. path ( ) , & project_dir, & tmc_project_yml) ?)
139140 {
@@ -142,12 +143,12 @@ pub fn unzip<P: StudentFilePolicy>(policy: P, zip: &Path, target: &Path) -> Resu
142143 if WalkDir :: new ( entry. path ( ) ) . max_depth ( 1 ) . into_iter ( ) . count ( ) == 1 {
143144 log:: debug!( "deleting empty directory {}" , entry. path( ) . display( ) ) ;
144145 fs:: remove_dir ( entry. path ( ) )
145- . map_err ( |e| Error :: RemoveDir ( entry. path ( ) . to_path_buf ( ) , e) ) ?;
146+ . map_err ( |e| TmcError :: RemoveDir ( entry. path ( ) . to_path_buf ( ) , e) ) ?;
146147 }
147148 } else {
148149 log:: debug!( "removing file {}" , entry. path( ) . display( ) ) ;
149150 fs:: remove_file ( entry. path ( ) )
150- . map_err ( |e| Error :: RemoveFile ( entry. path ( ) . to_path_buf ( ) , e) ) ?;
151+ . map_err ( |e| TmcError :: RemoveFile ( entry. path ( ) . to_path_buf ( ) , e) ) ?;
151152 }
152153 }
153154 }
@@ -191,7 +192,7 @@ fn find_project_dir<R: Read + Seek>(zip_archive: &mut ZipArchive<R>) -> Result<P
191192 return Ok ( parent. to_path_buf ( ) ) ;
192193 }
193194 }
194- Err ( Error :: NoProjectDirInZip )
195+ Err ( TmcError :: NoProjectDirInZip )
195196}
196197
197198fn contains_tmcnosubmit ( entry : & DirEntry ) -> bool {
0 commit comments