@@ -47,13 +47,30 @@ fn extract_zip_archive<R: io::Read + io::Seek>(
4747 let out_path = target_dir. join ( & rel_path) ;
4848
4949 if file. is_dir ( ) {
50- fs:: create_dir_all ( & out_path) ?;
50+ fs:: create_dir_all ( & out_path) . with_context ( || {
51+ format ! (
52+ "Failed to create installation directory: {}" ,
53+ out_path. display( )
54+ )
55+ } ) ?;
5156 } else {
5257 if let Some ( parent) = out_path. parent ( ) {
53- fs:: create_dir_all ( parent) ?;
58+ fs:: create_dir_all ( parent) . with_context ( || {
59+ format ! (
60+ "Failed to create installation directory: {}" ,
61+ parent. display( )
62+ )
63+ } ) ?;
5464 }
55- let mut outfile = fs:: File :: create ( & out_path) ?;
56- io:: copy ( & mut file, & mut outfile) ?;
65+ let mut outfile = fs:: File :: create ( & out_path) . with_context ( || {
66+ format ! ( "Failed to create installation file: {}" , out_path. display( ) )
67+ } ) ?;
68+ io:: copy ( & mut file, & mut outfile) . with_context ( || {
69+ format ! (
70+ "Failed to extract installation file: {}" ,
71+ out_path. display( )
72+ )
73+ } ) ?;
5774 }
5875 }
5976
@@ -78,12 +95,29 @@ fn copy_directory_internal(
7895 let mut bytes_copied: u64 = 0 ;
7996
8097 if !target. exists ( ) {
81- fs:: create_dir_all ( target) ?;
98+ fs:: create_dir_all ( target) . with_context ( || {
99+ format ! (
100+ "Failed to create installation directory: {}" ,
101+ target. display( )
102+ )
103+ } ) ?;
82104 }
83105
84- for entry in fs:: read_dir ( source) ? {
85- let entry = entry?;
86- let file_type = entry. file_type ( ) ?;
106+ for entry in fs:: read_dir ( source)
107+ . with_context ( || format ! ( "Failed to read payload directory: {}" , source. display( ) ) ) ?
108+ {
109+ let entry = entry. with_context ( || {
110+ format ! (
111+ "Failed to read payload directory entry: {}" ,
112+ source. display( )
113+ )
114+ } ) ?;
115+ let file_type = entry. file_type ( ) . with_context ( || {
116+ format ! (
117+ "Failed to inspect payload entry: {}" ,
118+ entry. path( ) . display( )
119+ )
120+ } ) ?;
87121 let name = entry. file_name ( ) ;
88122 let rel = relative_prefix. join ( & name) ;
89123 let dest = target. join ( & name) ;
@@ -100,8 +134,19 @@ fn copy_directory_internal(
100134 if !should_copy_file ( & rel) {
101135 continue ;
102136 }
103- let size = entry. metadata ( ) ?. len ( ) ;
104- fs:: copy ( entry. path ( ) , & dest) ?;
137+ let size = entry
138+ . metadata ( )
139+ . with_context ( || {
140+ format ! ( "Failed to inspect payload file: {}" , entry. path( ) . display( ) )
141+ } ) ?
142+ . len ( ) ;
143+ fs:: copy ( entry. path ( ) , & dest) . with_context ( || {
144+ format ! (
145+ "Failed to copy payload file from {} to {}" ,
146+ entry. path( ) . display( ) ,
147+ dest. display( )
148+ )
149+ } ) ?;
105150 bytes_copied += size;
106151 }
107152 }
@@ -111,8 +156,103 @@ fn copy_directory_internal(
111156
112157#[ cfg( test) ]
113158mod tests {
114- use super :: { extract_zip_bytes_with_filter, extract_zip_with_filter} ;
115- use std:: { fs, path:: Path } ;
159+ use super :: {
160+ copy_directory_with_filter, extract_zip_bytes_with_filter, extract_zip_with_filter,
161+ } ;
162+ use std:: { fs, io, path:: Path } ;
163+
164+ fn assert_io_diagnostic ( error : anyhow:: Error , path : & Path ) {
165+ let cause = error. downcast_ref :: < io:: Error > ( ) . unwrap ( ) ;
166+ let os_error = cause. raw_os_error ( ) . unwrap ( ) ;
167+ let message = format ! ( "{error:#}" ) ;
168+ assert ! ( message. contains( & path. display( ) . to_string( ) ) , "{message}" ) ;
169+ assert ! ( message. contains( & cause. to_string( ) ) , "{message}" ) ;
170+ assert ! (
171+ message. contains( & format!( "os error {os_error}" ) ) ,
172+ "{message}"
173+ ) ;
174+ }
175+
176+ #[ test]
177+ fn extraction_failure_preserves_target_path_and_os_error ( ) {
178+ let root = tempfile:: tempdir ( ) . unwrap ( ) ;
179+ let target = root. path ( ) . join ( "installed" ) ;
180+ let blocked_file = target. join ( "resources/readme.txt" ) ;
181+ fs:: create_dir_all ( & blocked_file) . unwrap ( ) ;
182+
183+ let error = extract_zip_bytes_with_filter (
184+ include_bytes ! ( "fixtures/legacy-payload-zip-0.6.6.zip" ) ,
185+ & target,
186+ |path| path == Path :: new ( "resources/readme.txt" ) ,
187+ )
188+ . unwrap_err ( ) ;
189+
190+ assert_io_diagnostic ( error, & blocked_file) ;
191+ }
192+
193+ #[ test]
194+ fn extraction_directory_failure_preserves_path_and_os_error ( ) {
195+ let root = tempfile:: tempdir ( ) . unwrap ( ) ;
196+ let target = root. path ( ) . join ( "installed" ) ;
197+ fs:: create_dir ( & target) . unwrap ( ) ;
198+ let blocked_directory = target. join ( "resources" ) ;
199+ fs:: write ( & blocked_directory, b"not a directory" ) . unwrap ( ) ;
200+
201+ let error = extract_zip_bytes_with_filter (
202+ include_bytes ! ( "fixtures/legacy-payload-zip-0.6.6.zip" ) ,
203+ & target,
204+ |path| path == Path :: new ( "resources/readme.txt" ) ,
205+ )
206+ . unwrap_err ( ) ;
207+
208+ assert_io_diagnostic ( error, & blocked_directory) ;
209+ }
210+
211+ #[ test]
212+ fn copy_failure_preserves_source_target_and_os_error ( ) {
213+ let root = tempfile:: tempdir ( ) . unwrap ( ) ;
214+ let source = root. path ( ) . join ( "payload" ) ;
215+ fs:: create_dir ( & source) . unwrap ( ) ;
216+ let source_file = source. join ( "application.exe" ) ;
217+ fs:: write ( & source_file, b"new application" ) . unwrap ( ) ;
218+ let target = root. path ( ) . join ( "installed" ) ;
219+ let blocked_file = target. join ( "application.exe" ) ;
220+ fs:: create_dir_all ( & blocked_file) . unwrap ( ) ;
221+
222+ let error = copy_directory_with_filter ( & source, & target, |_| true ) . unwrap_err ( ) ;
223+ assert ! ( format!( "{error:#}" ) . contains( & source_file. display( ) . to_string( ) ) ) ;
224+ assert_io_diagnostic ( error, & blocked_file) ;
225+ }
226+
227+ #[ cfg( windows) ]
228+ #[ test]
229+ fn locked_installation_file_preserves_windows_error_code ( ) {
230+ use std:: os:: windows:: fs:: OpenOptionsExt ;
231+
232+ let root = tempfile:: tempdir ( ) . unwrap ( ) ;
233+ let target = root. path ( ) . join ( "installed" ) ;
234+ fs:: create_dir_all ( target. join ( "resources" ) ) . unwrap ( ) ;
235+ let locked_file = target. join ( "resources/readme.txt" ) ;
236+ fs:: write ( & locked_file, b"old resource" ) . unwrap ( ) ;
237+ let _locked = fs:: OpenOptions :: new ( )
238+ . read ( true )
239+ . share_mode ( 0 )
240+ . open ( & locked_file)
241+ . unwrap ( ) ;
242+
243+ let error = extract_zip_bytes_with_filter (
244+ include_bytes ! ( "fixtures/legacy-payload-zip-0.6.6.zip" ) ,
245+ & target,
246+ |path| path == Path :: new ( "resources/readme.txt" ) ,
247+ )
248+ . unwrap_err ( ) ;
249+
250+ assert_eq ! (
251+ error. downcast_ref:: <io:: Error >( ) . unwrap( ) . raw_os_error( ) ,
252+ Some ( 32 )
253+ ) ;
254+ assert_io_diagnostic ( error, & locked_file) ;
255+ }
116256
117257 #[ test]
118258 fn reads_legacy_payload_from_file_and_embedded_bytes ( ) {
0 commit comments