1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
use std::collections::btree_map::Entry;
use std::collections::{BTreeMap, BTreeSet};
use std::env;
use std::ffi::OsString;
use std::fs::{self, File};
use std::io::prelude::*;
use std::path::{Path, PathBuf};
use toml;
use core::{SourceId, Source, Package, Registry, Dependency, PackageIdSpec};
use core::PackageId;
use ops::{self, CompileFilter};
use sources::{GitSource, PathSource, RegistrySource};
use util::{CargoResult, ChainError, Config, human, internal};
#[derive(RustcDecodable, RustcEncodable)]
enum CrateListing {
V1(CrateListingV1),
}
#[derive(RustcDecodable, RustcEncodable)]
struct CrateListingV1 {
v1: BTreeMap<PackageId, BTreeSet<String>>,
}
struct Transaction {
bins: Vec<PathBuf>,
}
impl Drop for Transaction {
fn drop(&mut self) {
for bin in self.bins.iter() {
let _ = fs::remove_file(bin);
}
}
}
pub fn install(root: Option<&str>,
krate: Option<&str>,
source_id: &SourceId,
vers: Option<&str>,
opts: &ops::CompileOptions) -> CargoResult<()> {
let config = opts.config;
let root = try!(resolve_root(root, config));
let (pkg, source) = if source_id.is_git() {
try!(select_pkg(GitSource::new(source_id, config), source_id,
krate, vers, &mut |git| git.read_packages()))
} else if source_id.is_path() {
let path = source_id.url().to_file_path().ok()
.expect("path sources must have a valid path");
try!(select_pkg(PathSource::new(&path, source_id, config),
source_id, krate, vers,
&mut |path| path.read_packages()))
} else {
try!(select_pkg(RegistrySource::new(source_id, config),
source_id, krate, vers,
&mut |_| Err(human("must specify a crate to install from \
crates.io, or use --path or --git to \
specify alternate source"))))
};
let mut list = try!(read_crate_list(&root));
let dst = root.join("bin");
try!(check_overwrites(&dst, &pkg, &opts.filter, &list));
let target_dir = config.cwd().join("target-install");
config.set_target_dir(&target_dir);
let compile = try!(ops::compile_pkg(&pkg, Some(source), opts).chain_error(|| {
human(format!("failed to compile `{}`, intermediate artifacts can be \
found at `{}`", pkg, target_dir.display()))
}));
let mut t = Transaction { bins: Vec::new() };
try!(fs::create_dir_all(&dst));
for bin in compile.binaries.iter() {
let dst = dst.join(bin.file_name().unwrap());
try!(config.shell().status("Installing", dst.display()));
try!(fs::copy(&bin, &dst).chain_error(|| {
human(format!("failed to copy `{}` to `{}`", bin.display(),
dst.display()))
}));
t.bins.push(dst);
}
try!(fs::remove_dir_all(&target_dir));
list.v1.entry(pkg.package_id().clone()).or_insert_with(|| {
BTreeSet::new()
}).extend(t.bins.iter().map(|t| {
t.file_name().unwrap().to_string_lossy().into_owned()
}));
try!(write_crate_list(&root, list));
t.bins.truncate(0);
let path = env::var_os("PATH").unwrap_or(OsString::new());
for path in env::split_paths(&path) {
if path == dst {
return Ok(())
}
}
try!(config.shell().warn(&format!("be sure to add `{}` to your PATH to be \
able to run the installed binaries",
dst.display())));
Ok(())
}
fn select_pkg<'a, T>(mut source: T,
source_id: &SourceId,
name: Option<&str>,
vers: Option<&str>,
list_all: &mut FnMut(&mut T) -> CargoResult<Vec<Package>>)
-> CargoResult<(Package, Box<Source + 'a>)>
where T: Source + 'a
{
try!(source.update());
match name {
Some(name) => {
let dep = try!(Dependency::parse(name, vers, source_id));
let deps = try!(source.query(&dep));
match deps.iter().map(|p| p.package_id()).max() {
Some(pkgid) => {
try!(source.download(&[pkgid.clone()]));
Ok((try!(source.get(&[pkgid.clone()])).remove(0),
Box::new(source)))
}
None => {
let vers_info = vers.map(|v| format!(" with version `{}`", v))
.unwrap_or(String::new());
Err(human(format!("could not find `{}` in `{}`{}", name,
source_id, vers_info)))
}
}
}
None => {
let candidates = try!(list_all(&mut source));
let binaries = candidates.iter().filter(|cand| {
cand.targets().iter().filter(|t| t.is_bin()).count() > 0
});
let examples = candidates.iter().filter(|cand| {
cand.targets().iter().filter(|t| t.is_example()).count() > 0
});
let pkg = match try!(one(binaries, |v| multi_err("binaries", v))) {
Some(p) => p,
None => {
match try!(one(examples, |v| multi_err("examples", v))) {
Some(p) => p,
None => bail!("no packages found with binaries or \
examples"),
}
}
};
return Ok((pkg.clone(), Box::new(source)));
#[allow(deprecated)]
fn multi_err(kind: &str, mut pkgs: Vec<&Package>) -> String {
pkgs.sort_by(|a, b| a.name().cmp(b.name()));
format!("multiple packages with {} found: {}", kind,
pkgs.iter().map(|p| p.name()).collect::<Vec<_>>()
.connect(", "))
}
}
}
}
fn one<I, F>(mut i: I, f: F) -> CargoResult<Option<I::Item>>
where I: Iterator,
F: FnOnce(Vec<I::Item>) -> String
{
match (i.next(), i.next()) {
(Some(i1), Some(i2)) => {
let mut v = vec![i1, i2];
v.extend(i);
Err(human(f(v)))
}
(Some(i), None) => Ok(Some(i)),
(None, _) => Ok(None)
}
}
fn check_overwrites(dst: &Path,
pkg: &Package,
filter: &ops::CompileFilter,
prev: &CrateListingV1) -> CargoResult<()> {
let check = |name| {
let name = format!("{}{}", name, env::consts::EXE_SUFFIX);
if fs::metadata(dst.join(&name)).is_err() {
return Ok(())
}
let mut msg = format!("binary `{}` already exists in destination", name);
if let Some((p, _)) = prev.v1.iter().find(|&(_, v)| v.contains(&name)) {
msg.push_str(&format!(" as part of `{}`", p));
}
Err(human(msg))
};
match *filter {
CompileFilter::Everything => {
if pkg.targets().iter().filter(|t| t.is_bin()).next().is_none() {
bail!("specified package has no binaries")
}
for target in pkg.targets().iter().filter(|t| t.is_bin()) {
try!(check(target.name()));
}
}
CompileFilter::Only { bins, examples, .. } => {
for bin in bins.iter().chain(examples) {
try!(check(bin));
}
}
}
Ok(())
}
fn read_crate_list(path: &Path) -> CargoResult<CrateListingV1> {
let metadata = path.join(".crates.toml");
let mut f = match File::open(&metadata) {
Ok(f) => f,
Err(..) => return Ok(CrateListingV1 { v1: BTreeMap::new() }),
};
(|| -> CargoResult<_> {
let mut contents = String::new();
try!(f.read_to_string(&mut contents));
let listing = try!(toml::decode_str(&contents).chain_error(|| {
internal("invalid TOML found for metadata")
}));
match listing {
CrateListing::V1(v1) => Ok(v1),
}
}).chain_error(|| {
human(format!("failed to parse crate metadata at `{}`",
metadata.display()))
})
}
fn write_crate_list(path: &Path, listing: CrateListingV1) -> CargoResult<()> {
let metadata = path.join(".crates.toml");
(|| -> CargoResult<_> {
let mut f = try!(File::create(&metadata));
let data = toml::encode_str::<CrateListing>(&CrateListing::V1(listing));
try!(f.write_all(data.as_bytes()));
Ok(())
}).chain_error(|| {
human(format!("failed to write crate metadata at `{}`",
metadata.display()))
})
}
pub fn install_list(dst: Option<&str>, config: &Config) -> CargoResult<()> {
let dst = try!(resolve_root(dst, config));
let list = try!(read_crate_list(&dst));
let mut shell = config.shell();
let out = shell.out();
for (k, v) in list.v1.iter() {
try!(writeln!(out, "{}:", k));
for bin in v {
try!(writeln!(out, " {}", bin));
}
}
Ok(())
}
pub fn uninstall(root: Option<&str>,
spec: &str,
bins: &[String],
config: &Config) -> CargoResult<()> {
let root = try!(resolve_root(root, config));
let mut metadata = try!(read_crate_list(&root));
let mut to_remove = Vec::new();
{
let result = try!(PackageIdSpec::query_str(spec, metadata.v1.keys()))
.clone();
let mut installed = match metadata.v1.entry(result.clone()) {
Entry::Occupied(e) => e,
Entry::Vacant(..) => panic!("entry not found: {}", result),
};
let dst = root.join("bin");
for bin in installed.get() {
let bin = dst.join(bin);
if fs::metadata(&bin).is_err() {
bail!("corrupt metadata, `{}` does not exist when it should",
bin.display())
}
}
let bins = bins.iter().map(|s| {
if s.ends_with(env::consts::EXE_SUFFIX) {
s.to_string()
} else {
format!("{}{}", s, env::consts::EXE_SUFFIX)
}
}).collect::<Vec<_>>();
for bin in bins.iter() {
if !installed.get().contains(bin) {
bail!("binary `{}` not installed as part of `{}`", bin, result)
}
}
if bins.len() == 0 {
to_remove.extend(installed.get().iter().map(|b| dst.join(b)));
installed.get_mut().clear();
} else {
for bin in bins.iter() {
to_remove.push(dst.join(bin));
installed.get_mut().remove(bin);
}
}
if installed.get().len() == 0 {
installed.remove();
}
}
try!(write_crate_list(&root, metadata));
for bin in to_remove {
try!(config.shell().status("Removing", bin.display()));
try!(fs::remove_file(bin));
}
Ok(())
}
fn resolve_root(flag: Option<&str>, config: &Config) -> CargoResult<PathBuf> {
let config_root = try!(config.get_string("install.root"));
Ok(flag.map(PathBuf::from).or_else(|| {
env::var_os("CARGO_INSTALL_ROOT").map(PathBuf::from)
}).or_else(|| {
config_root.clone().map(|(v, _)| PathBuf::from(v))
}).unwrap_or_else(|| {
config.home().to_owned()
}))
}