@@ -81,6 +81,9 @@ use crate::util::{FileLock, Filesystem, IntoUrl, IntoUrlWithBase, Rustc};
81
81
mod de;
82
82
use de:: Deserializer ;
83
83
84
+ mod dirs;
85
+ use dirs:: CargoDirs ;
86
+
84
87
mod value;
85
88
pub use value:: { Definition , OptValue , Value } ;
86
89
@@ -122,7 +125,7 @@ macro_rules! get_value_typed {
122
125
#[ derive( Debug ) ]
123
126
pub struct Config {
124
127
/// The location of the user's 'home' directory. OS-dependent.
125
- home_path : Filesystem ,
128
+ dirs : CargoDirs ,
126
129
/// Information about how to write messages to the shell
127
130
shell : RefCell < Shell > ,
128
131
/// A collection of configuration options
@@ -182,7 +185,7 @@ impl Config {
182
185
///
183
186
/// This does only minimal initialization. In particular, it does not load
184
187
/// any config files from disk. Those will be loaded lazily as-needed.
185
- pub fn new ( shell : Shell , cwd : PathBuf , homedir : PathBuf ) -> Config {
188
+ pub fn new ( shell : Shell , cwd : PathBuf , homedir : PathBuf ) -> CargoResult < Config > {
186
189
static mut GLOBAL_JOBSERVER : * mut jobserver:: Client = 0 as * mut _ ;
187
190
static INIT : Once = Once :: new ( ) ;
188
191
@@ -209,8 +212,8 @@ impl Config {
209
212
_ => true ,
210
213
} ;
211
214
212
- Config {
213
- home_path : Filesystem :: new ( homedir) ,
215
+ Ok ( Config {
216
+ dirs : CargoDirs :: new ( homedir) ? ,
214
217
shell : RefCell :: new ( shell) ,
215
218
cwd,
216
219
values : LazyCell :: new ( ) ,
@@ -241,7 +244,7 @@ impl Config {
241
244
net_config : LazyCell :: new ( ) ,
242
245
build_config : LazyCell :: new ( ) ,
243
246
target_cfgs : LazyCell :: new ( ) ,
244
- }
247
+ } )
245
248
}
246
249
247
250
/// Creates a new Config instance, with all default settings.
@@ -258,32 +261,32 @@ impl Config {
258
261
This probably means that $HOME was not set."
259
262
)
260
263
} ) ?;
261
- Ok ( Config :: new ( shell, cwd, homedir) )
264
+ Config :: new ( shell, cwd, homedir)
262
265
}
263
266
264
267
/// Gets the user's Cargo home directory (OS-dependent).
265
268
pub fn home ( & self ) -> & Filesystem {
266
- & self . home_path
269
+ & self . dirs . data_dir
267
270
}
268
271
269
272
/// Gets the Cargo Git directory (`<cargo_home>/git`).
270
273
pub fn git_path ( & self ) -> Filesystem {
271
- self . home_path . join ( "git" )
274
+ self . dirs . data_dir . join ( "git" )
272
275
}
273
276
274
277
/// Gets the Cargo registry index directory (`<cargo_home>/registry/index`).
275
278
pub fn registry_index_path ( & self ) -> Filesystem {
276
- self . home_path . join ( "registry" ) . join ( "index" )
279
+ self . dirs . data_dir . join ( "registry" ) . join ( "index" )
277
280
}
278
281
279
282
/// Gets the Cargo registry cache directory (`<cargo_home>/registry/path`).
280
283
pub fn registry_cache_path ( & self ) -> Filesystem {
281
- self . home_path . join ( "registry" ) . join ( "cache" )
284
+ self . dirs . cache_dir . clone ( )
282
285
}
283
286
284
287
/// Gets the Cargo registry source directory (`<cargo_home>/registry/src`).
285
288
pub fn registry_source_path ( & self ) -> Filesystem {
286
- self . home_path . join ( "registry" ) . join ( "src" )
289
+ self . dirs . data_dir . join ( "registry" ) . join ( "src" )
287
290
}
288
291
289
292
/// Gets the default Cargo registry.
@@ -781,7 +784,7 @@ impl Config {
781
784
// This definition path is ignored, this is just a temporary container
782
785
// representing the entire file.
783
786
let mut cfg = CV :: Table ( HashMap :: new ( ) , Definition :: Path ( PathBuf :: from ( "." ) ) ) ;
784
- let home = self . home_path . clone ( ) . into_path_unlocked ( ) ;
787
+ let home = self . dirs . home_dir . clone ( ) . into_path_unlocked ( ) ;
785
788
786
789
self . walk_tree ( path, & home, |path| {
787
790
let value = self . load_file ( path) ?;
@@ -1040,7 +1043,7 @@ impl Config {
1040
1043
1041
1044
/// Loads credentials config from the credentials file, if present.
1042
1045
pub fn load_credentials ( & mut self ) -> CargoResult < ( ) > {
1043
- let home_path = self . home_path . clone ( ) . into_path_unlocked ( ) ;
1046
+ let home_path = self . dirs . data_dir . clone ( ) . into_path_unlocked ( ) ;
1044
1047
let credentials = match self . get_file_path ( & home_path, "credentials" , true ) ? {
1045
1048
Some ( credentials) => credentials,
1046
1049
None => return Ok ( ( ) ) ,
@@ -1197,7 +1200,7 @@ impl Config {
1197
1200
"package cache lock is not currently held, Cargo forgot to call \
1198
1201
`acquire_package_cache_lock` before we got to this stack frame",
1199
1202
) ;
1200
- assert ! ( ret. starts_with( self . home_path . as_path_unlocked( ) ) ) ;
1203
+ assert ! ( ret. starts_with( self . dirs . cache_dir . as_path_unlocked( ) ) ) ;
1201
1204
ret
1202
1205
}
1203
1206
@@ -1234,11 +1237,11 @@ impl Config {
1234
1237
// someone else on the system we should synchronize with them,
1235
1238
// but if we can't even do that then we did our best and we just
1236
1239
// keep on chugging elsewhere.
1237
- match self . home_path . open_rw ( path, self , desc) {
1240
+ match self . dirs . data_dir . open_rw ( path, self , desc) {
1238
1241
Ok ( lock) => * slot = Some ( ( Some ( lock) , 1 ) ) ,
1239
1242
Err ( e) => {
1240
1243
if maybe_readonly ( & e) {
1241
- let lock = self . home_path . open_ro ( path, self , desc) . ok ( ) ;
1244
+ let lock = self . dirs . data_dir . open_ro ( path, self , desc) . ok ( ) ;
1242
1245
* slot = Some ( ( lock, 1 ) ) ;
1243
1246
return Ok ( PackageCacheLock ( self ) ) ;
1244
1247
}
@@ -1556,7 +1559,7 @@ pub fn save_credentials(cfg: &Config, token: String, registry: Option<String>) -
1556
1559
// If 'credentials.toml' exists, we should write to that, otherwise
1557
1560
// use the legacy 'credentials'. There's no need to print the warning
1558
1561
// here, because it would already be printed at load time.
1559
- let home_path = cfg. home_path . clone ( ) . into_path_unlocked ( ) ;
1562
+ let home_path = cfg. dirs . data_dir . clone ( ) . into_path_unlocked ( ) ;
1560
1563
let filename = match cfg. get_file_path ( & home_path, "credentials" , false ) ? {
1561
1564
Some ( path) => match path. file_name ( ) {
1562
1565
Some ( filename) => Path :: new ( filename) . to_owned ( ) ,
@@ -1566,8 +1569,9 @@ pub fn save_credentials(cfg: &Config, token: String, registry: Option<String>) -
1566
1569
} ;
1567
1570
1568
1571
let mut file = {
1569
- cfg. home_path . create_dir ( ) ?;
1570
- cfg. home_path
1572
+ cfg. dirs . data_dir . create_dir ( ) ?;
1573
+ cfg. dirs
1574
+ . data_dir
1571
1575
. open_rw ( filename, cfg, "credentials' config file" ) ?
1572
1576
} ;
1573
1577
0 commit comments