@@ -53,6 +53,27 @@ impl Drop for Transaction {
53
53
}
54
54
}
55
55
56
+ #[ derive( Clone ) ]
57
+ struct CargoInstallDirs {
58
+ config_dir : PathBuf ,
59
+ bin_dir : PathBuf ,
60
+ }
61
+
62
+ impl CargoInstallDirs {
63
+ fn from_root ( root : PathBuf ) -> CargoInstallDirs {
64
+ CargoInstallDirs {
65
+ bin_dir : root. join ( "bin" ) ,
66
+ config_dir : root,
67
+ }
68
+ }
69
+ fn from_config ( config : & Config ) -> CargoInstallDirs {
70
+ CargoInstallDirs {
71
+ config_dir : config. config_path ( ) . into_path_unlocked ( ) ,
72
+ bin_dir : config. bin_path ( ) ,
73
+ }
74
+ }
75
+ }
76
+
56
77
pub fn install (
57
78
root : Option < & str > ,
58
79
krates : Vec < & str > ,
@@ -61,7 +82,7 @@ pub fn install(
61
82
opts : & ops:: CompileOptions ,
62
83
force : bool ,
63
84
) -> CargoResult < ( ) > {
64
- let root = resolve_root ( root, opts. config ) ?;
85
+ let root = resolve_install_dirs ( root, opts. config ) ?;
65
86
let map = SourceConfigMap :: new ( opts. config ) ?;
66
87
67
88
let ( installed_anything, scheduled_error) = if krates. len ( ) <= 1 {
@@ -122,7 +143,7 @@ pub fn install(
122
143
if installed_anything {
123
144
// Print a warning that if this directory isn't in PATH that they won't be
124
145
// able to run these commands.
125
- let dst = metadata ( opts. config , & root) ?. parent ( ) . join ( "bin" ) ;
146
+ let dst = metadata ( opts. config , & Filesystem :: new ( root. config_dir ) ) ?. parent ( ) . join ( "bin" ) ;
126
147
let path = env:: var_os ( "PATH" ) . unwrap_or_default ( ) ;
127
148
for path in env:: split_paths ( & path) {
128
149
if path == dst {
@@ -145,7 +166,7 @@ pub fn install(
145
166
}
146
167
147
168
fn install_one (
148
- root : & Filesystem ,
169
+ dirs : & CargoInstallDirs ,
149
170
map : & SourceConfigMap ,
150
171
krate : Option < & str > ,
151
172
source_id : & SourceId ,
@@ -235,9 +256,9 @@ fn install_one(
235
256
// We have to check this again afterwards, but may as well avoid building
236
257
// anything if we're gonna throw it away anyway.
237
258
{
238
- let metadata = metadata ( config, root ) ?;
259
+ let metadata = metadata ( config, & Filesystem :: new ( dirs . config_dir . clone ( ) ) ) ?;
239
260
let list = read_crate_list ( & metadata) ?;
240
- let dst = metadata . parent ( ) . join ( "bin" ) ;
261
+ let dst = config . bin_path ( ) ;
241
262
check_overwrites ( & dst, pkg, & opts. filter , & list, force) ?;
242
263
}
243
264
@@ -274,7 +295,7 @@ fn install_one(
274
295
) ;
275
296
}
276
297
277
- let metadata = metadata ( config, root ) ?;
298
+ let metadata = metadata ( config, & Filesystem :: new ( dirs . config_dir . clone ( ) ) ) ?;
278
299
let mut list = read_crate_list ( & metadata) ?;
279
300
let dst = metadata. parent ( ) . join ( "bin" ) ;
280
301
let duplicates = check_overwrites ( & dst, pkg, & opts. filter , & list, force) ?;
@@ -653,8 +674,8 @@ fn write_crate_list(file: &FileLock, listing: CrateListingV1) -> CargoResult<()>
653
674
}
654
675
655
676
pub fn install_list ( dst : Option < & str > , config : & Config ) -> CargoResult < ( ) > {
656
- let dst = resolve_root ( dst, config) ?;
657
- let dst = metadata ( config, & dst) ?;
677
+ let dst = resolve_install_dirs ( dst, config) ?;
678
+ let dst = metadata ( config, & Filesystem :: new ( dst. config_dir ) ) ?;
658
679
let list = read_crate_list ( & dst) ?;
659
680
for ( k, v) in list. v1 . iter ( ) {
660
681
println ! ( "{}:" , k) ;
@@ -675,7 +696,7 @@ pub fn uninstall(
675
696
bail ! ( "A binary can only be associated with a single installed package, specifying multiple specs with --bin is redundant." ) ;
676
697
}
677
698
678
- let root = resolve_root ( root, config) ?;
699
+ let root = resolve_install_dirs ( root, config) ?;
679
700
let scheduled_error = if specs. len ( ) == 1 {
680
701
uninstall_one ( & root, specs[ 0 ] , bins, config) ?;
681
702
false
@@ -721,13 +742,13 @@ pub fn uninstall(
721
742
Ok ( ( ) )
722
743
}
723
744
724
- pub fn uninstall_one (
725
- root : & Filesystem ,
745
+ fn uninstall_one (
746
+ dirs : & CargoInstallDirs ,
726
747
spec : & str ,
727
748
bins : & [ String ] ,
728
749
config : & Config ,
729
750
) -> CargoResult < ( ) > {
730
- let crate_metadata = metadata ( config, root ) ?;
751
+ let crate_metadata = metadata ( config, & Filesystem :: new ( dirs . config_dir . clone ( ) ) ) ?;
731
752
let mut metadata = read_crate_list ( & crate_metadata) ?;
732
753
let mut to_remove = Vec :: new ( ) ;
733
754
{
@@ -736,7 +757,7 @@ pub fn uninstall_one(
736
757
Entry :: Occupied ( e) => e,
737
758
Entry :: Vacant ( ..) => panic ! ( "entry not found: {}" , result) ,
738
759
} ;
739
- let dst = crate_metadata . parent ( ) . join ( "bin" ) ;
760
+ let dst = & dirs . bin_dir ;
740
761
for bin in installed. get ( ) {
741
762
let bin = dst. join ( bin) ;
742
763
if fs:: metadata ( & bin) . is_err ( ) {
@@ -785,15 +806,22 @@ pub fn uninstall_one(
785
806
Ok ( ( ) )
786
807
}
787
808
809
+ /// Return a file lock for the .crates.toml file at the given root.
810
+ /// The config argument is only used for logging to the shell.
788
811
fn metadata ( config : & Config , root : & Filesystem ) -> CargoResult < FileLock > {
789
812
root. open_rw ( Path :: new ( ".crates.toml" ) , config, "crate metadata" )
790
813
}
791
814
792
- fn resolve_root ( flag : Option < & str > , config : & Config ) -> CargoResult < Filesystem > {
815
+ // Determine cargo directories by first checking whether an argument was given
816
+ // on the command line, if not checking whether the environment variable
817
+ // CARGO_INSTALL_ROOT is set, and if not using the paths of the configuration.
818
+ fn resolve_install_dirs (
819
+ root : Option < & str > ,
820
+ config : & Config ,
821
+ ) -> CargoResult < CargoInstallDirs > {
793
822
let config_root = config. get_path ( "install.root" ) ?;
794
- Ok ( flag . map ( PathBuf :: from)
823
+ Ok ( root . map ( PathBuf :: from)
795
824
. or_else ( || env:: var_os ( "CARGO_INSTALL_ROOT" ) . map ( PathBuf :: from) )
796
- . or_else ( move || config_root. map ( |v| v. val ) )
797
- . map ( Filesystem :: new)
798
- . unwrap_or_else ( || config. home ( ) . clone ( ) ) )
825
+ . or_else ( move || config_root. map ( |v| v. val ) ) . map ( CargoInstallDirs :: from_root)
826
+ . unwrap_or_else ( || CargoInstallDirs :: from_config ( config) ) )
799
827
}
0 commit comments