1
- use crate :: utils:: copy_dir_all;
2
1
use anyhow:: { Context as _, Result } ;
3
2
use std:: {
4
- fs, io ,
3
+ fs,
5
4
path:: { Path , PathBuf } ,
6
5
} ;
7
6
use tracing:: { debug, instrument, warn} ;
8
7
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
-
30
8
/// artifact caching with cleanup
31
9
#[ derive( Debug ) ]
32
10
pub ( crate ) struct ArtifactCache {
@@ -89,7 +67,7 @@ impl ArtifactCache {
89
67
return Ok ( ( ) ) ;
90
68
}
91
69
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" ) ?;
93
71
Ok ( ( ) )
94
72
}
95
73
@@ -100,12 +78,12 @@ impl ArtifactCache {
100
78
target_dir : P ,
101
79
) -> Result < ( ) > {
102
80
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) ?;
105
83
}
106
84
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" ) ?;
109
87
self . cleanup ( & cache_dir) ?;
110
88
Ok ( ( ) )
111
89
}
0 commit comments