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
use std::collections::HashMap;
use std::fmt::{self, Debug, Formatter};
use std::fs::File;
use std::io::Read;
use std::path::{Path, PathBuf};
use hex::ToHex;
use serde_json;
use core::{Package, PackageId, Summary, SourceId, Source, Dependency, Registry};
use sources::PathSource;
use util::{Config, Sha256};
use util::errors::{CargoResult, CargoResultExt};
use util::paths;
pub struct DirectorySource<'cfg> {
source_id: SourceId,
root: PathBuf,
packages: HashMap<PackageId, (Package, Checksum)>,
config: &'cfg Config,
}
#[derive(Deserialize)]
struct Checksum {
package: Option<String>,
files: HashMap<String, String>,
}
impl<'cfg> DirectorySource<'cfg> {
pub fn new(path: &Path, id: &SourceId, config: &'cfg Config)
-> DirectorySource<'cfg> {
DirectorySource {
source_id: id.clone(),
root: path.to_path_buf(),
config: config,
packages: HashMap::new(),
}
}
}
impl<'cfg> Debug for DirectorySource<'cfg> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "DirectorySource {{ root: {:?} }}", self.root)
}
}
impl<'cfg> Registry for DirectorySource<'cfg> {
fn query(&mut self,
dep: &Dependency,
f: &mut FnMut(Summary)) -> CargoResult<()> {
let packages = self.packages.values().map(|p| &p.0);
let matches = packages.filter(|pkg| dep.matches(pkg.summary()));
for summary in matches.map(|pkg| pkg.summary().clone()) {
f(summary);
}
Ok(())
}
fn supports_checksums(&self) -> bool {
true
}
fn requires_precise(&self) -> bool {
true
}
}
impl<'cfg> Source for DirectorySource<'cfg> {
fn source_id(&self) -> &SourceId {
&self.source_id
}
fn update(&mut self) -> CargoResult<()> {
self.packages.clear();
let entries = self.root.read_dir().chain_err(|| {
format!("failed to read root of directory source: {}",
self.root.display())
})?;
for entry in entries {
let entry = entry?;
let path = entry.path();
if let Some(s) = path.file_name().and_then(|s| s.to_str()) {
if s.starts_with('.') {
continue
}
}
let mut only_dotfile = true;
for entry in path.read_dir()?.filter_map(|e| e.ok()) {
if let Some(s) = entry.file_name().to_str() {
if s.starts_with('.') {
continue
}
}
only_dotfile = false;
}
if only_dotfile {
continue
}
let mut src = PathSource::new(&path, &self.source_id, self.config);
src.update()?;
let pkg = src.root_package()?;
let cksum_file = path.join(".cargo-checksum.json");
let cksum = paths::read(&path.join(cksum_file)).chain_err(|| {
format!("failed to load checksum `.cargo-checksum.json` \
of {} v{}",
pkg.package_id().name(),
pkg.package_id().version())
})?;
let cksum: Checksum = serde_json::from_str(&cksum).chain_err(|| {
format!("failed to decode `.cargo-checksum.json` of \
{} v{}",
pkg.package_id().name(),
pkg.package_id().version())
})?;
let mut manifest = pkg.manifest().clone();
let mut summary = manifest.summary().clone();
if let Some(ref package) = cksum.package {
summary = summary.set_checksum(package.clone());
}
manifest.set_summary(summary);
let pkg = Package::new(manifest, pkg.manifest_path());
self.packages.insert(pkg.package_id().clone(), (pkg, cksum));
}
Ok(())
}
fn download(&mut self, id: &PackageId) -> CargoResult<Package> {
self.packages.get(id).map(|p| &p.0).cloned().ok_or_else(|| {
format_err!("failed to find package with id: {}", id)
})
}
fn fingerprint(&self, pkg: &Package) -> CargoResult<String> {
Ok(pkg.package_id().version().to_string())
}
fn verify(&self, id: &PackageId) -> CargoResult<()> {
let (pkg, cksum) = match self.packages.get(id) {
Some(&(ref pkg, ref cksum)) => (pkg, cksum),
None => bail!("failed to find entry for `{}` in directory source",
id),
};
let mut buf = [0; 16 * 1024];
for (file, cksum) in cksum.files.iter() {
let mut h = Sha256::new();
let file = pkg.root().join(file);
(|| -> CargoResult<()> {
let mut f = File::open(&file)?;
loop {
match f.read(&mut buf)? {
0 => return Ok(()),
n => h.update(&buf[..n]),
}
}
})().chain_err(|| {
format!("failed to calculate checksum of: {}",
file.display())
})?;
let actual = h.finish().to_hex();
if &*actual != cksum {
bail!("\
the listed checksum of `{}` has changed:\n\
expected: {}\n\
actual: {}\n\
\n\
directory sources are not intended to be edited, if \
modifications are required then it is recommended \
that [replace] is used with a forked copy of the \
source\
", file.display(), cksum, actual);
}
}
Ok(())
}
}