1- use crate :: utils:: copy_dir_all;
21use anyhow:: { Context as _, Result } ;
32use std:: {
4- fs, io ,
3+ fs,
54 path:: { Path , PathBuf } ,
65} ;
76use tracing:: { debug, instrument, warn} ;
87
9- /// move cache folder to target, falling back to copy + delete on error.
10- fn move_or_copy < P : AsRef < Path > + std:: fmt:: Debug , Q : AsRef < Path > + std:: fmt:: Debug > (
11- source : P ,
12- dest : Q ,
13- ) -> io:: Result < ( ) > {
14- if let Some ( parent) = dest. as_ref ( ) . parent ( ) {
15- fs:: create_dir_all ( parent) ?;
16- }
17- if let Err ( err) = fs:: rename ( & source, & dest) {
18- warn ! (
19- ?err,
20- ?source,
21- ?dest,
22- "could not move target directory, fall back to copy"
23- ) ;
24- copy_dir_all ( & source, & dest) ?;
25- fs:: remove_dir_all ( & source) ?;
26- }
27- Ok ( ( ) )
28- }
29-
308/// artifact caching with cleanup
319#[ derive( Debug ) ]
3210pub ( crate ) struct ArtifactCache {
@@ -89,7 +67,7 @@ impl ArtifactCache {
8967 return Ok ( ( ) ) ;
9068 }
9169
92- move_or_copy ( cache_dir, target_dir) . context ( "could not move cache directory to target" ) ?;
70+ fs :: rename ( cache_dir, target_dir) . context ( "could not move cache directory to target" ) ?;
9371 Ok ( ( ) )
9472 }
9573
@@ -100,12 +78,12 @@ impl ArtifactCache {
10078 target_dir : P ,
10179 ) -> Result < ( ) > {
10280 let cache_dir = self . cache_dir . join ( cache_key) ;
103- if ! cache_dir. exists ( ) {
104- fs:: create_dir_all ( & cache_dir) ?;
81+ if cache_dir. exists ( ) {
82+ fs:: remove_dir_all ( & cache_dir) ?;
10583 }
10684
107- move_or_copy ( & target_dir, & cache_dir)
108- . context ( "could not move target directory to cache" ) ?;
85+ debug ! ( ? target_dir, ? cache_dir, "saving artifact cache" ) ;
86+ fs :: rename ( & target_dir , & cache_dir ) . context ( "could not move target directory to cache" ) ?;
10987 self . cleanup ( & cache_dir) ?;
11088 Ok ( ( ) )
11189 }
0 commit comments