10
10
//! running the command-line client. So we begrudgingly have a *little*
11
11
//! configuration.
12
12
13
+ use error_chain:: bail;
13
14
#[ cfg( feature = "serde" ) ]
14
15
use serde:: { Deserialize , Serialize } ;
15
16
use std:: ffi:: OsStr ;
16
- use std:: fs:: File ;
17
- use std:: path:: PathBuf ;
17
+ use std:: fs:: { self , File } ;
18
+ use std:: path:: { Path , PathBuf } ;
18
19
use std:: sync:: atomic:: { AtomicBool , Ordering } ;
19
20
20
21
use crate :: app_dirs;
22
+ use crate :: ctry;
21
23
use crate :: errors:: { ErrorKind , Result } ;
22
24
use crate :: io:: itarbundle:: { HttpITarIoFactory , ITarBundle } ;
23
25
use crate :: io:: local_cache:: LocalCache ;
@@ -110,18 +112,19 @@ impl PersistentConfig {
110
112
& self ,
111
113
url : & str ,
112
114
only_cached : bool ,
115
+ custom_cache_root : Option < & Path > ,
113
116
status : & mut dyn StatusBackend ,
114
117
) -> Result < Box < dyn Bundle > > {
115
118
let itb = ITarBundle :: < HttpITarIoFactory > :: new ( url) ;
116
119
117
- let mut url2digest_path = app_dirs :: user_cache_dir ( "urls" ) ?;
120
+ let mut url2digest_path = cache_dir ( "urls" , custom_cache_root ) ?;
118
121
url2digest_path. push ( app_dirs:: sanitized ( url) ) ;
119
122
120
123
let bundle = LocalCache :: < ITarBundle < HttpITarIoFactory > > :: new (
121
124
itb,
122
125
& url2digest_path,
123
- & app_dirs :: user_cache_dir ( "manifests" ) ?,
124
- & app_dirs :: user_cache_dir ( "files" ) ?,
126
+ & cache_dir ( "manifests" , custom_cache_root ) ?,
127
+ & cache_dir ( "files" , custom_cache_root ) ?,
125
128
only_cached,
126
129
status,
127
130
) ?;
@@ -134,8 +137,6 @@ impl PersistentConfig {
134
137
file_path : & OsStr ,
135
138
_status : & mut dyn StatusBackend ,
136
139
) -> Result < Box < dyn Bundle > > {
137
- use std:: path:: Path ;
138
-
139
140
let zip_bundle = ZipBundle :: < File > :: open ( Path :: new ( file_path) ) ?;
140
141
141
142
Ok ( Box :: new ( zip_bundle) as _ )
@@ -172,7 +173,7 @@ impl PersistentConfig {
172
173
return Ok ( Box :: new ( zip_bundle) as _ ) ;
173
174
}
174
175
let bundle =
175
- self . make_cached_url_provider ( & self . default_bundles [ 0 ] . url , only_cached, status) ?;
176
+ self . make_cached_url_provider ( & self . default_bundles [ 0 ] . url , only_cached, None , status) ?;
176
177
Ok ( Box :: new ( bundle) as _ )
177
178
}
178
179
@@ -194,3 +195,16 @@ impl Default for PersistentConfig {
194
195
}
195
196
}
196
197
}
198
+
199
+ fn cache_dir ( path : & str , custom_cache_root : Option < & Path > ) -> Result < PathBuf > {
200
+ if let Some ( root) = custom_cache_root {
201
+ if !root. is_dir ( ) {
202
+ bail ! ( "Custom cache path {} is not a directory" , root. display( ) ) ;
203
+ }
204
+ let full_path = root. join ( path) ;
205
+ ctry ! ( fs:: create_dir_all( & full_path) ; "failed to create directory {}" , full_path. display( ) ) ;
206
+ Ok ( full_path)
207
+ } else {
208
+ app_dirs:: user_cache_dir ( path)
209
+ }
210
+ }
0 commit comments