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
use std::io::prelude::*;
use std::fs::{self, File};
use std::path::{self, Path, PathBuf};
use semver::VersionReq;
use tar::Archive;
use flate2::{GzBuilder, Compression};
use flate2::read::GzDecoder;
use core::{SourceId, Package, PackageId};
use core::dependency::Kind;
use sources::PathSource;
use util::{self, CargoResult, human, internal, ChainError, Config};
use ops;
pub fn package(manifest_path: &Path,
config: &Config,
verify: bool,
list: bool,
metadata: bool) -> CargoResult<Option<PathBuf>> {
let mut src = try!(PathSource::for_path(manifest_path.parent().unwrap(),
config));
let pkg = try!(src.root_package());
if metadata {
try!(check_metadata(&pkg, config));
}
try!(check_dependencies(&pkg, config));
if list {
let root = pkg.root();
let mut list: Vec<_> = try!(src.list_files(&pkg)).iter().map(|file| {
util::without_prefix(&file, &root).unwrap().to_path_buf()
}).collect();
list.sort();
for file in list.iter() {
println!("{}", file.display());
}
return Ok(None)
}
let filename = format!("{}-{}.crate", pkg.name(), pkg.version());
let dir = config.target_dir(&pkg).join("package");
let dst = dir.join(&filename);
if fs::metadata(&dst).is_ok() {
return Ok(Some(dst))
}
try!(config.shell().status("Packaging", pkg.package_id().to_string()));
let tmp_dst = dir.join(format!(".{}", filename));
let _ = fs::remove_file(&tmp_dst);
try!(tar(&pkg, &src, config, &tmp_dst, &filename).chain_error(|| {
human("failed to prepare local package for uploading")
}));
if verify {
try!(run_verify(config, &pkg, &tmp_dst).chain_error(|| {
human("failed to verify package tarball")
}))
}
try!(fs::rename(&tmp_dst, &dst).chain_error(|| {
human("failed to move temporary tarball into final location")
}));
Ok(Some(dst))
}
#[allow(deprecated)]
fn check_metadata(pkg: &Package, config: &Config) -> CargoResult<()> {
let md = pkg.manifest().metadata();
let mut missing = vec![];
macro_rules! lacking {
($( $($field: ident)||* ),*) => {{
$(
if $(md.$field.as_ref().map_or(true, |s| s.is_empty()))&&* {
$(missing.push(stringify!($field).replace("_", "-"));)*
}
)*
}}
}
lacking!(description, license || license_file, documentation || homepage || repository);
if !missing.is_empty() {
let mut things = missing[..missing.len() - 1].connect(", ");
if !things.is_empty() {
things.push_str(" or ");
}
things.push_str(&missing.last().unwrap());
try!(config.shell().warn(
&format!("warning: manifest has no {things}. \
See http://doc.crates.io/manifest.html#package-metadata for more info.",
things = things)))
}
Ok(())
}
#[allow(deprecated)]
fn check_dependencies(pkg: &Package, config: &Config) -> CargoResult<()> {
let wildcard = VersionReq::parse("*").unwrap();
let mut wildcard_deps = vec![];
for dep in pkg.dependencies() {
if dep.kind() != Kind::Development && dep.version_req() == &wildcard {
wildcard_deps.push(dep.name());
}
}
if !wildcard_deps.is_empty() {
let deps = wildcard_deps.connect(", ");
try!(config.shell().warn(
"warning: some dependencies have wildcard (\"*\") version constraints. \
On January 22nd, 2016, crates.io will begin rejecting packages with \
wildcard dependency constraints. See \
http://doc.crates.io/crates-io.html#using-crates.io-based-crates \
for information on version constraints."));
try!(config.shell().warn(
&format!("dependencies for these crates have wildcard constraints: {}", deps)));
}
Ok(())
}
fn tar(pkg: &Package,
src: &PathSource,
config: &Config,
dst: &Path,
filename: &str) -> CargoResult<()> {
if fs::metadata(&dst).is_ok() {
bail!("destination already exists: {}", dst.display())
}
try!(fs::create_dir_all(dst.parent().unwrap()));
let tmpfile = try!(File::create(dst));
let filename = Path::new(filename);
let encoder = GzBuilder::new().filename(try!(util::path2bytes(filename)))
.write(tmpfile, Compression::Best);
let ar = Archive::new(encoder);
let root = pkg.root();
for file in try!(src.list_files(pkg)).iter() {
if &**file == dst { continue }
let relative = util::without_prefix(&file, &root).unwrap();
try!(check_filename(relative));
let relative = try!(relative.to_str().chain_error(|| {
human(format!("non-utf8 path in source directory: {}",
relative.display()))
}));
let mut file = try!(File::open(file));
try!(config.shell().verbose(|shell| {
shell.status("Archiving", &relative)
}));
let path = format!("{}-{}{}{}", pkg.name(), pkg.version(),
path::MAIN_SEPARATOR, relative);
try!(ar.append_file(&path, &mut file).chain_error(|| {
internal(format!("could not archive source file `{}`", relative))
}));
}
try!(ar.finish());
Ok(())
}
fn run_verify(config: &Config, pkg: &Package, tar: &Path)
-> CargoResult<()> {
try!(config.shell().status("Verifying", pkg));
let f = try!(GzDecoder::new(try!(File::open(tar))));
let dst = pkg.root().join(&format!("target/package/{}-{}",
pkg.name(), pkg.version()));
if fs::metadata(&dst).is_ok() {
try!(fs::remove_dir_all(&dst));
}
let mut archive = Archive::new(f);
try!(archive.unpack(dst.parent().unwrap()));
let manifest_path = dst.join("Cargo.toml");
let registry = try!(SourceId::for_central(config));
let precise = Some("locked".to_string());
let new_src = try!(SourceId::for_path(&dst)).with_precise(precise);
let new_pkgid = try!(PackageId::new(pkg.name(), pkg.version(), &new_src));
let new_summary = pkg.summary().clone().map_dependencies(|d| {
if !d.source_id().is_path() { return d }
d.clone_inner().set_source_id(registry.clone()).into_dependency()
});
let mut new_manifest = pkg.manifest().clone();
new_manifest.set_summary(new_summary.override_id(new_pkgid));
let new_pkg = Package::new(new_manifest, &manifest_path);
try!(ops::compile_pkg(&new_pkg, None, &ops::CompileOptions {
config: config,
jobs: None,
target: None,
features: &[],
no_default_features: false,
spec: &[],
filter: ops::CompileFilter::Everything,
exec_engine: None,
release: false,
mode: ops::CompileMode::Build,
target_rustdoc_args: None,
target_rustc_args: None,
}));
Ok(())
}
fn check_filename(file: &Path) -> CargoResult<()> {
let name = match file.file_name() {
Some(name) => name,
None => return Ok(()),
};
let name = match name.to_str() {
Some(name) => name,
None => {
bail!("path does not have a unicode filename which may not unpack \
on all platforms: {}", file.display())
}
};
let bad_chars = ['/', '\\', '<', '>', ':', '"', '|', '?', '*'];
for c in bad_chars.iter().filter(|c| name.contains(**c)) {
bail!("cannot package a filename with a special character `{}`: {}",
c, file.display())
}
Ok(())
}