@@ -4,7 +4,7 @@ use std::{
4
4
path:: Path ,
5
5
} ;
6
6
7
- use color_eyre:: eyre:: { self , Report } ;
7
+ use color_eyre:: eyre:: { eyre , Report , WrapErr } ;
8
8
9
9
use zip:: {
10
10
unstable:: path_to_string,
@@ -59,9 +59,14 @@ fn enter_recursive_dir_entries(
59
59
err,
60
60
"writing top-level directory entry for {base_dirname:?}"
61
61
) ?;
62
- writer. add_directory ( & base_dirname, options) ?;
62
+ writer
63
+ . add_directory ( & base_dirname, options)
64
+ . wrap_err_with ( || eyre ! ( "{base_dirname}" ) ) ?;
63
65
64
- let mut readdir_stack: Vec < ( fs:: ReadDir , String ) > = vec ! [ ( fs:: read_dir( root) ?, base_dirname) ] ;
66
+ let mut readdir_stack: Vec < ( fs:: ReadDir , String ) > = vec ! [ (
67
+ fs:: read_dir( root) . wrap_err_with( || eyre!( "{}" , root. display( ) ) ) ?,
68
+ base_dirname,
69
+ ) ] ;
65
70
while let Some ( ( mut readdir, top_component) ) = readdir_stack. pop ( ) {
66
71
if let Some ( dir_entry) = readdir. next ( ) . transpose ( ) ? {
67
72
let mut components: Vec < & str > = readdir_stack. iter ( ) . map ( |( _, s) | s. as_ref ( ) ) . collect ( ) ;
@@ -70,36 +75,51 @@ fn enter_recursive_dir_entries(
70
75
let entry_basename: String = dir_entry
71
76
. file_name ( )
72
77
. into_string ( )
73
- . map_err ( |name| eyre:: eyre !( "failed to decode basename {name:?}" ) ) ?;
78
+ . map_err ( |name| eyre ! ( "failed to decode basename {name:?}" ) ) ?;
74
79
components. push ( & entry_basename) ;
75
80
let full_path: String = components. join ( "/" ) ;
76
81
readdir_stack. push ( ( readdir, top_component) ) ;
77
82
78
- let file_type = dir_entry. file_type ( ) ?;
83
+ let file_type = dir_entry
84
+ . file_type ( )
85
+ . wrap_err_with ( || eyre ! ( "{}" , dir_entry. path( ) . display( ) ) ) ?;
79
86
if file_type. is_symlink ( ) {
80
- let target: String = path_to_string ( fs:: read_link ( dir_entry. path ( ) ) ?) . into ( ) ;
87
+ let target: String = path_to_string (
88
+ fs:: read_link ( dir_entry. path ( ) )
89
+ . wrap_err_with ( || eyre ! ( "{}" , dir_entry. path( ) . display( ) ) ) ?,
90
+ )
91
+ . into ( ) ;
81
92
writeln ! (
82
93
err,
83
94
"writing recursive symlink entry with name {full_path:?} and target {target:?}"
84
95
) ?;
85
- writer. add_symlink ( full_path, target, options) ?;
96
+ writer
97
+ . add_symlink ( & full_path, & target, options)
98
+ . wrap_err_with ( || eyre ! ( "{full_path}->{target}" ) ) ?;
86
99
} else if file_type. is_file ( ) {
87
100
writeln ! ( err, "writing recursive file entry with name {full_path:?}" ) ?;
88
- writer. start_file ( full_path, options) ?;
89
- let mut f = fs:: File :: open ( dir_entry. path ( ) ) ?;
90
- io:: copy ( & mut f, writer) ?;
101
+ writer
102
+ . start_file ( & full_path, options)
103
+ . wrap_err_with ( || eyre ! ( "{full_path}" ) ) ?;
104
+ let mut f = fs:: File :: open ( dir_entry. path ( ) )
105
+ . wrap_err_with ( || eyre ! ( "{}" , dir_entry. path( ) . display( ) ) ) ?;
106
+ io:: copy ( & mut f, writer)
107
+ . wrap_err_with ( || eyre ! ( "{}" , dir_entry. path( ) . display( ) ) ) ?;
91
108
} else {
92
109
assert ! ( file_type. is_dir( ) ) ;
93
110
writeln ! (
94
111
err,
95
112
"writing recursive directory entry with name {full_path:?}"
96
113
) ?;
97
- writer. add_directory ( full_path, options) ?;
114
+ writer
115
+ . add_directory ( & full_path, options)
116
+ . wrap_err_with ( || eyre ! ( "{full_path}" ) ) ?;
98
117
writeln ! (
99
118
err,
100
119
"adding subdirectories depth-first for recursive directory entry {entry_basename:?}"
101
120
) ?;
102
- let new_readdir = fs:: read_dir ( dir_entry. path ( ) ) ?;
121
+ let new_readdir = fs:: read_dir ( dir_entry. path ( ) )
122
+ . wrap_err_with ( || eyre ! ( "{}" , dir_entry. path( ) . display( ) ) ) ?;
103
123
readdir_stack. push ( ( new_readdir, entry_basename) ) ;
104
124
}
105
125
}
@@ -121,7 +141,9 @@ pub fn execute_compress(
121
141
let out = match output_path {
122
142
Some ( path) => {
123
143
writeln ! ( err, "writing compressed zip to output file path {path:?}" ) ?;
124
- OutputHandle :: File ( fs:: File :: create ( path) ?)
144
+ OutputHandle :: File (
145
+ fs:: File :: create ( & path) . wrap_err_with ( || eyre ! ( "{}" , path. display( ) ) ) ?,
146
+ )
125
147
}
126
148
None => {
127
149
writeln ! (
@@ -189,7 +211,9 @@ pub fn execute_compress(
189
211
let dirname = last_name. take ( ) . unwrap_or_else ( || {
190
212
Compress :: exit_arg_invalid ( "no name provided before dir entry" )
191
213
} ) ;
192
- writer. add_directory ( dirname, options) ?;
214
+ writer
215
+ . add_directory ( & dirname, options)
216
+ . wrap_err_with ( || eyre ! ( "{dirname}" ) ) ?;
193
217
}
194
218
CompressionArg :: Symlink => {
195
219
writeln ! ( err, "setting symlink flag for next entry" ) ?;
@@ -215,7 +239,9 @@ pub fn execute_compress(
215
239
"writing immediate symlink entry with name {name:?} and target {target:?}"
216
240
) ?;
217
241
/* TODO: .add_symlink() should support OsString targets! */
218
- writer. add_symlink ( name, target, options) ?;
242
+ writer
243
+ . add_symlink ( & name, & target, options)
244
+ . wrap_err_with ( || eyre ! ( "{name}->{target}" ) ) ?;
219
245
symlink_flag = false ;
220
246
} else {
221
247
/* This is a file entry. */
@@ -224,8 +250,12 @@ pub fn execute_compress(
224
250
"writing immediate file entry with name {name:?} and data {data:?}"
225
251
) ?;
226
252
let data = data. into_encoded_bytes ( ) ;
227
- writer. start_file ( name, options) ?;
228
- writer. write_all ( data. as_ref ( ) ) ?;
253
+ writer
254
+ . start_file ( & name, options)
255
+ . wrap_err_with ( || eyre ! ( "{name}" ) ) ?;
256
+ writer
257
+ . write_all ( data. as_ref ( ) )
258
+ . wrap_err_with ( || eyre ! ( "{name}" ) ) ?;
229
259
}
230
260
}
231
261
CompressionArg :: FilePath ( path) => {
@@ -234,19 +264,27 @@ pub fn execute_compress(
234
264
. unwrap_or_else ( || path_to_string ( & path) . into ( ) ) ;
235
265
if symlink_flag {
236
266
/* This is a symlink entry. */
237
- let target: String = path_to_string ( fs:: read_link ( & path) ?) . into ( ) ;
267
+ let target: String = path_to_string (
268
+ fs:: read_link ( & path) . wrap_err_with ( || eyre ! ( "{}" , path. display( ) ) ) ?,
269
+ )
270
+ . into ( ) ;
238
271
writeln ! ( err, "writing symlink entry from path {path:?} with name {name:?} and target {target:?}" ) ?;
239
- writer. add_symlink ( name, target, options) ?;
272
+ writer
273
+ . add_symlink ( & name, & target, options)
274
+ . wrap_err_with ( || eyre ! ( "{name}->{target}" ) ) ?;
240
275
symlink_flag = false ;
241
276
} else {
242
277
/* This is a file entry. */
243
278
writeln ! (
244
279
err,
245
280
"writing file entry from path {path:?} with name {name:?}"
246
281
) ?;
247
- writer. start_file ( name, options) ?;
248
- let mut f = fs:: File :: open ( path) ?;
249
- io:: copy ( & mut f, & mut writer) ?;
282
+ writer
283
+ . start_file ( & name, options)
284
+ . wrap_err_with ( || eyre ! ( "{name}" ) ) ?;
285
+ let mut f =
286
+ fs:: File :: open ( & path) . wrap_err_with ( || eyre ! ( "{}" , path. display( ) ) ) ?;
287
+ io:: copy ( & mut f, & mut writer) . wrap_err_with ( || eyre ! ( "{}" , path. display( ) ) ) ?;
250
288
}
251
289
}
252
290
CompressionArg :: RecursiveDirPath ( r) => {
@@ -257,7 +295,8 @@ pub fn execute_compress(
257
295
err,
258
296
"writing recursive dir entries for path {r:?} with name {last_name:?}"
259
297
) ?;
260
- enter_recursive_dir_entries ( err, last_name. take ( ) , & r, & mut writer, options) ?;
298
+ enter_recursive_dir_entries ( err, last_name. take ( ) , & r, & mut writer, options)
299
+ . wrap_err_with ( || eyre ! ( "{}" , r. display( ) ) ) ?;
261
300
}
262
301
}
263
302
}
@@ -270,36 +309,48 @@ pub fn execute_compress(
270
309
) )
271
310
}
272
311
for pos_arg in positional_paths. into_iter ( ) {
273
- let file_type = fs:: symlink_metadata ( & pos_arg) ?. file_type ( ) ;
312
+ let file_type = fs:: symlink_metadata ( & pos_arg)
313
+ . wrap_err_with ( || eyre ! ( "{}" , pos_arg. display( ) ) ) ?
314
+ . file_type ( ) ;
274
315
if file_type. is_symlink ( ) {
275
- let target = fs:: read_link ( & pos_arg) ?;
316
+ let target =
317
+ fs:: read_link ( & pos_arg) . wrap_err_with ( || eyre ! ( "{}" , pos_arg. display( ) ) ) ?;
276
318
writeln ! (
277
319
err,
278
320
"writing positional symlink entry with path {pos_arg:?} and target {target:?}"
279
321
) ?;
280
- writer. add_symlink_from_path ( pos_arg, target, options) ?;
322
+ writer
323
+ . add_symlink_from_path ( & pos_arg, & target, options)
324
+ . wrap_err_with ( || eyre ! ( "{}->{}" , pos_arg. display( ) , target. display( ) ) ) ?;
281
325
} else if file_type. is_file ( ) {
282
326
writeln ! ( err, "writing positional file entry with path {pos_arg:?}" ) ?;
283
- writer. start_file_from_path ( & pos_arg, options) ?;
284
- let mut f = fs:: File :: open ( pos_arg) ?;
285
- io:: copy ( & mut f, & mut writer) ?;
327
+ writer
328
+ . start_file_from_path ( & pos_arg, options)
329
+ . wrap_err_with ( || eyre ! ( "{}" , pos_arg. display( ) ) ) ?;
330
+ let mut f =
331
+ fs:: File :: open ( & pos_arg) . wrap_err_with ( || eyre ! ( "{}" , pos_arg. display( ) ) ) ?;
332
+ io:: copy ( & mut f, & mut writer) . wrap_err_with ( || eyre ! ( "{}" , pos_arg. display( ) ) ) ?;
286
333
} else {
287
334
assert ! ( file_type. is_dir( ) ) ;
288
335
writeln ! (
289
336
err,
290
337
"writing positional recursive dir entry for {pos_arg:?}"
291
338
) ?;
292
- enter_recursive_dir_entries ( err, None , & pos_arg, & mut writer, options) ?;
339
+ enter_recursive_dir_entries ( err, None , & pos_arg, & mut writer, options)
340
+ . wrap_err_with ( || eyre ! ( "{}" , pos_arg. display( ) ) ) ?;
293
341
}
294
342
}
295
343
296
- let handle = writer. finish ( ) ?;
344
+ let handle = writer
345
+ . finish ( )
346
+ . wrap_err ( "failed to write zip to output handle" ) ?;
297
347
match handle {
298
348
OutputHandle :: File ( _) => ( ) ,
299
349
OutputHandle :: InMem ( mut cursor) => {
300
- cursor. rewind ( ) ?;
350
+ cursor. rewind ( ) . wrap_err ( "failed to rewind cursor" ) ?;
301
351
let mut stdout = io:: stdout ( ) . lock ( ) ;
302
- io:: copy ( & mut cursor, & mut stdout) ?;
352
+ io:: copy ( & mut cursor, & mut stdout)
353
+ . wrap_err ( "failed to copy contents of cursor to stdout" ) ?;
303
354
}
304
355
}
305
356
0 commit comments