@@ -298,9 +298,18 @@ impl Compression {
298298 Self :: Tar => {
299299 let buf = Cursor :: new ( Vec :: new ( ) ) ;
300300 let mut builder = tar:: Builder :: new ( buf) ;
301- builder
302- . append_dir_all ( "." , path)
303- . map_err ( TmcError :: TarWrite ) ?;
301+ walk_dir_for_compression ( path, |entry, relative_path| {
302+ if entry. path ( ) . is_dir ( ) {
303+ builder
304+ . append_dir ( relative_path, entry. path ( ) )
305+ . map_err ( TmcError :: TarWrite ) ?;
306+ } else if entry. path ( ) . is_file ( ) {
307+ builder
308+ . append_path_with_name ( entry. path ( ) , relative_path)
309+ . map_err ( TmcError :: TarWrite ) ?;
310+ }
311+ Ok ( ( ) )
312+ } ) ?;
304313 builder
305314 . into_inner ( )
306315 . map_err ( TmcError :: TarWrite ) ?
@@ -309,34 +318,35 @@ impl Compression {
309318 Self :: Zip => {
310319 let buf = Cursor :: new ( Vec :: new ( ) ) ;
311320 let mut writer = zip:: ZipWriter :: new ( buf) ;
312- let parent = path. parent ( ) . map ( PathBuf :: from) . unwrap_or_default ( ) ;
313- for entry in WalkDir :: new ( path) {
314- let entry = entry?;
315- let stripped = entry
316- . path ( )
317- . strip_prefix ( & parent)
318- . expect ( "entries are within parent" ) ;
319- let path_str = stripped
320- . to_str ( )
321- . ok_or_else ( || TmcError :: InvalidUtf8 ( path. to_path_buf ( ) ) ) ?;
321+ walk_dir_for_compression ( path, |entry, relative_path| {
322322 if entry. path ( ) . is_dir ( ) {
323- writer. add_directory ( path_str , Default :: default ( ) ) ?;
323+ writer. add_directory ( relative_path , Default :: default ( ) ) ?;
324324 } else if entry. path ( ) . is_file ( ) {
325- writer. start_file ( path_str , Default :: default ( ) ) ?;
325+ writer. start_file ( relative_path , Default :: default ( ) ) ?;
326326 let contents = file_util:: read_file ( entry. path ( ) ) ?;
327327 writer
328328 . write_all ( & contents)
329- . map_err ( |e | TmcError :: ZipWrite ( path. to_path_buf ( ) , e ) ) ?;
329+ . map_err ( |err | TmcError :: ZipWrite ( path. to_path_buf ( ) , err ) ) ?;
330330 }
331- }
331+ Ok ( ( ) )
332+ } ) ?;
332333 writer. finish ( ) ?. into_inner ( )
333334 }
334335 Self :: TarZstd => {
335336 let tar_buf = vec ! [ ] ;
336337 let mut builder = tar:: Builder :: new ( tar_buf) ;
337- builder
338- . append_dir_all ( "." , path)
339- . map_err ( TmcError :: TarWrite ) ?;
338+ walk_dir_for_compression ( path, |entry, relative_path| {
339+ if entry. path ( ) . is_dir ( ) {
340+ builder
341+ . append_dir ( relative_path, entry. path ( ) )
342+ . map_err ( TmcError :: TarWrite ) ?;
343+ } else if entry. path ( ) . is_file ( ) {
344+ builder
345+ . append_path_with_name ( entry. path ( ) , relative_path)
346+ . map_err ( TmcError :: TarWrite ) ?;
347+ }
348+ Ok ( ( ) )
349+ } ) ?;
340350 let tar_buf = builder. into_inner ( ) . map_err ( TmcError :: TarWrite ) ?;
341351 zstd:: stream:: encode_all ( tar_buf. as_slice ( ) , 0 ) . map_err ( TmcError :: ZstdWrite ) ?
342352 }
@@ -345,6 +355,29 @@ impl Compression {
345355 }
346356}
347357
358+ fn walk_dir_for_compression (
359+ root : & Path ,
360+ mut f : impl FnMut ( & walkdir:: DirEntry , & str ) -> Result < ( ) , TmcError > ,
361+ ) -> Result < ( ) , TmcError > {
362+ let parent = root. parent ( ) . map ( PathBuf :: from) . unwrap_or_default ( ) ;
363+ for entry in WalkDir :: new ( root)
364+ . into_iter ( )
365+ // filter windows lock files
366+ . filter_entry ( |e| e. file_name ( ) != ".tmc.lock" )
367+ {
368+ let entry = entry?;
369+ let stripped = entry
370+ . path ( )
371+ . strip_prefix ( & parent)
372+ . expect ( "entries are within parent" ) ;
373+ let path_str = stripped
374+ . to_str ( )
375+ . ok_or_else ( || TmcError :: InvalidUtf8 ( stripped. to_path_buf ( ) ) ) ?;
376+ f ( & entry, path_str) ?;
377+ }
378+ Ok ( ( ) )
379+ }
380+
348381impl Display for Compression {
349382 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
350383 match self {
0 commit comments