@@ -82,6 +82,9 @@ use crate::util::{FileLock, Filesystem, IntoUrl, IntoUrlWithBase, Rustc};
82
82
mod de;
83
83
use de:: Deserializer ;
84
84
85
+ mod dirs;
86
+ use dirs:: CargoDirs ;
87
+
85
88
mod value;
86
89
pub use value:: { Definition , OptValue , Value } ;
87
90
@@ -122,8 +125,8 @@ macro_rules! get_value_typed {
122
125
/// relating to cargo itself.
123
126
#[ derive( Debug ) ]
124
127
pub struct Config {
125
- /// The location of the user's Cargo home directory. OS-dependent.
126
- home_path : Filesystem ,
128
+ /// The location of the user's ' home' directory. OS-dependent.
129
+ dirs : CargoDirs ,
127
130
/// Information about how to write messages to the shell
128
131
shell : RefCell < Shell > ,
129
132
/// A collection of configuration options
@@ -191,7 +194,7 @@ impl Config {
191
194
///
192
195
/// This does only minimal initialization. In particular, it does not load
193
196
/// any config files from disk. Those will be loaded lazily as-needed.
194
- pub fn new ( shell : Shell , cwd : PathBuf , homedir : PathBuf ) -> Config {
197
+ pub fn new ( shell : Shell , cwd : PathBuf , homedir : PathBuf ) -> CargoResult < Config > {
195
198
static mut GLOBAL_JOBSERVER : * mut jobserver:: Client = 0 as * mut _ ;
196
199
static INIT : Once = Once :: new ( ) ;
197
200
@@ -227,8 +230,8 @@ impl Config {
227
230
_ => true ,
228
231
} ;
229
232
230
- Config {
231
- home_path : Filesystem :: new ( homedir) ,
233
+ Ok ( Config {
234
+ dirs : CargoDirs :: new ( homedir) ? ,
232
235
shell : RefCell :: new ( shell) ,
233
236
cwd,
234
237
search_stop_path : None ,
@@ -264,7 +267,7 @@ impl Config {
264
267
target_cfgs : LazyCell :: new ( ) ,
265
268
doc_extern_map : LazyCell :: new ( ) ,
266
269
progress_config : ProgressConfig :: default ( ) ,
267
- }
270
+ } )
268
271
}
269
272
270
273
/// Creates a new Config instance, with all default settings.
@@ -281,32 +284,32 @@ impl Config {
281
284
This probably means that $HOME was not set."
282
285
)
283
286
} ) ?;
284
- Ok ( Config :: new ( shell, cwd, homedir) )
287
+ Config :: new ( shell, cwd, homedir)
285
288
}
286
289
287
290
/// Gets the user's Cargo home directory (OS-dependent).
288
291
pub fn home ( & self ) -> & Filesystem {
289
- & self . home_path
292
+ & self . dirs . data_dir
290
293
}
291
294
292
295
/// Gets the Cargo Git directory (`<cargo_home>/git`).
293
296
pub fn git_path ( & self ) -> Filesystem {
294
- self . home_path . join ( "git" )
297
+ self . dirs . data_dir . join ( "git" )
295
298
}
296
299
297
300
/// Gets the Cargo registry index directory (`<cargo_home>/registry/index`).
298
301
pub fn registry_index_path ( & self ) -> Filesystem {
299
- self . home_path . join ( "registry" ) . join ( "index" )
302
+ self . dirs . data_dir . join ( "registry" ) . join ( "index" )
300
303
}
301
304
302
305
/// Gets the Cargo registry cache directory (`<cargo_home>/registry/path`).
303
306
pub fn registry_cache_path ( & self ) -> Filesystem {
304
- self . home_path . join ( "registry" ) . join ( "cache" )
307
+ self . dirs . cache_dir . clone ( )
305
308
}
306
309
307
310
/// Gets the Cargo registry source directory (`<cargo_home>/registry/src`).
308
311
pub fn registry_source_path ( & self ) -> Filesystem {
309
- self . home_path . join ( "registry" ) . join ( "src" )
312
+ self . dirs . data_dir . join ( "registry" ) . join ( "src" )
310
313
}
311
314
312
315
/// Gets the default Cargo registry.
@@ -870,7 +873,7 @@ impl Config {
870
873
// This definition path is ignored, this is just a temporary container
871
874
// representing the entire file.
872
875
let mut cfg = CV :: Table ( HashMap :: new ( ) , Definition :: Path ( PathBuf :: from ( "." ) ) ) ;
873
- let home = self . home_path . clone ( ) . into_path_unlocked ( ) ;
876
+ let home = self . dirs . home_dir . clone ( ) . into_path_unlocked ( ) ;
874
877
875
878
self . walk_tree ( path, & home, |path| {
876
879
let value = self . load_file ( path) ?;
@@ -1137,7 +1140,7 @@ impl Config {
1137
1140
1138
1141
/// Loads credentials config from the credentials file, if present.
1139
1142
pub fn load_credentials ( & mut self ) -> CargoResult < ( ) > {
1140
- let home_path = self . home_path . clone ( ) . into_path_unlocked ( ) ;
1143
+ let home_path = self . dirs . data_dir . clone ( ) . into_path_unlocked ( ) ;
1141
1144
let credentials = match self . get_file_path ( & home_path, "credentials" , true ) ? {
1142
1145
Some ( credentials) => credentials,
1143
1146
None => return Ok ( ( ) ) ,
@@ -1316,7 +1319,7 @@ impl Config {
1316
1319
"package cache lock is not currently held, Cargo forgot to call \
1317
1320
`acquire_package_cache_lock` before we got to this stack frame",
1318
1321
) ;
1319
- assert ! ( ret. starts_with( self . home_path . as_path_unlocked( ) ) ) ;
1322
+ assert ! ( ret. starts_with( self . dirs . cache_dir . as_path_unlocked( ) ) ) ;
1320
1323
ret
1321
1324
}
1322
1325
@@ -1353,11 +1356,11 @@ impl Config {
1353
1356
// someone else on the system we should synchronize with them,
1354
1357
// but if we can't even do that then we did our best and we just
1355
1358
// keep on chugging elsewhere.
1356
- match self . home_path . open_rw ( path, self , desc) {
1359
+ match self . dirs . data_dir . open_rw ( path, self , desc) {
1357
1360
Ok ( lock) => * slot = Some ( ( Some ( lock) , 1 ) ) ,
1358
1361
Err ( e) => {
1359
1362
if maybe_readonly ( & e) {
1360
- let lock = self . home_path . open_ro ( path, self , desc) . ok ( ) ;
1363
+ let lock = self . dirs . data_dir . open_ro ( path, self , desc) . ok ( ) ;
1361
1364
* slot = Some ( ( lock, 1 ) ) ;
1362
1365
return Ok ( PackageCacheLock ( self ) ) ;
1363
1366
}
@@ -1677,7 +1680,7 @@ pub fn save_credentials(
1677
1680
// If 'credentials.toml' exists, we should write to that, otherwise
1678
1681
// use the legacy 'credentials'. There's no need to print the warning
1679
1682
// here, because it would already be printed at load time.
1680
- let home_path = cfg. home_path . clone ( ) . into_path_unlocked ( ) ;
1683
+ let home_path = cfg. dirs . data_dir . clone ( ) . into_path_unlocked ( ) ;
1681
1684
let filename = match cfg. get_file_path ( & home_path, "credentials" , false ) ? {
1682
1685
Some ( path) => match path. file_name ( ) {
1683
1686
Some ( filename) => Path :: new ( filename) . to_owned ( ) ,
@@ -1687,8 +1690,9 @@ pub fn save_credentials(
1687
1690
} ;
1688
1691
1689
1692
let mut file = {
1690
- cfg. home_path . create_dir ( ) ?;
1691
- cfg. home_path
1693
+ cfg. dirs . data_dir . create_dir ( ) ?;
1694
+ cfg. dirs
1695
+ . data_dir
1692
1696
. open_rw ( filename, cfg, "credentials' config file" ) ?
1693
1697
} ;
1694
1698
0 commit comments