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
use std::fs::File;
use std::io::{BufReader, Write, BufWriter};
use std::iter::Iterator;
use std::path::{PathBuf, StripPrefixError};
use walkdir::{IntoIter, WalkDir};
use failure::Fail;
use crate::ReadSeek;
pub type SourceId = usize;
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum TrustLevel
{
UntrustedSource,
TrustedSource
}
pub struct SourceManager
{
sources : Vec<Box<dyn Source>>
}
impl SourceManager
{
pub fn new() -> SourceManager
{
SourceManager
{
sources : Vec::new()
}
}
pub fn add_source(&mut self, source : Box<dyn Source>) -> SourceId
{
self.sources.push(source);
self.sources.len()
}
pub fn source(&mut self, id : SourceId) -> Option<&mut Box<dyn Source>>
{
if id == 0 || id - 1 >= self.sources.len()
{
return None
}
Some(&mut self.sources[id - 1])
}
pub fn get_package_source(&mut self, package_name : &str) -> Option<&mut Box<dyn Source>>
{
for source in self.sources.iter_mut().rev()
{
if source.has_package(package_name)
{
return Some(source);
}
}
None
}
pub fn clear(&mut self)
{
self.sources.clear();
}
}
#[derive(Debug, Fail)]
pub enum PackageError
{
#[fail(display = "Failed to access package source")]
Generic,
#[fail(display = "{}", _0)]
IoError(#[cause] std::io::Error),
#[fail(display = "Package item does not belong to the given prefix: {}", _0)]
PrefixMismatch(StripPrefixError),
#[fail(display = "Operation not supported")]
NotSupported
}
impl From<std::io::Error> for PackageError
{
fn from(error : std::io::Error) -> PackageError
{
PackageError::IoError(error)
}
}
pub trait Source
{
fn get_uri(&self) -> &str;
fn has_package(&self, package_name : &str) -> bool;
fn list_packages(&mut self) -> Vec<String>;
fn read_file(&mut self, full_pathname : &str) -> Result<Box<dyn ReadSeek>, PackageError>;
#[allow(unused_variables)]
fn write_file(&mut self, full_pathname : &str) -> Result<Box<dyn Write>, PackageError> { Err(PackageError::NotSupported) }
fn iter_entries(&mut self, package_name : &str, type_folder : &str) -> Box<dyn Iterator<Item = Result<String, PackageError>>>;
fn trust_level(&self, package_name : &str) -> TrustLevel;
}
struct FilesystemIter
{
basepath : PathBuf,
walkdir : IntoIter
}
impl FilesystemIter
{
fn new<P : Into<PathBuf>>(basepath : P) -> FilesystemIter
{
let path = basepath.into();
FilesystemIter
{
basepath : path.clone(),
walkdir : WalkDir::new(path).into_iter(),
}
}
}
impl Iterator for FilesystemIter
{
type Item = Result<String, PackageError>;
fn next(&mut self) -> Option<Result<String, PackageError>>
{
while let Some(dir_next) = self.walkdir.next()
{
let dir_entry = try_or_else!(dir_next,
|error : walkdir::Error|
{
if let Some(io_error) = error.io_error()
{
if io_error.kind() == std::io::ErrorKind::NotFound
{
return None;
}
}
Some(Err(PackageError::IoError(error.into())))
});
if !dir_entry.file_type().is_file()
{
continue;
}
let path = dir_entry.path();
let filepath = try_or_else!(path.strip_prefix(self.basepath.clone()),
|error| Some(Err(PackageError::PrefixMismatch(error))));
let mut pathname = String::new();
let mut first_part = true;
for part in filepath.iter()
{
if !first_part
{
pathname += "/";
}
pathname += &part.to_string_lossy();
first_part = false;
}
return Some(Ok(pathname));
}
None
}
}
pub struct FilesystemSource
{
basedir : String,
package_list : Option<Vec<String>>,
trust : TrustLevel
}
impl FilesystemSource
{
pub fn new(directory : &str, trust : TrustLevel) -> FilesystemSource
{
FilesystemSource
{
basedir : directory.to_string(),
package_list : None,
trust
}
}
}
impl Source for FilesystemSource
{
fn get_uri(&self) -> &str
{
&self.basedir
}
fn has_package(&self, package_name : &str) -> bool
{
let mut path = PathBuf::from(&self.basedir);
path.push(package_name);
path.exists()
}
fn list_packages(&mut self) -> Vec<String>
{
if let Some(package_list) = &self.package_list
{
return package_list.clone();
}
let mut package_list : Vec<String> = Vec::new();
let path = PathBuf::from(&self.basedir);
let walkdir = WalkDir::new(path).min_depth(1).max_depth(1).into_iter();
for dir_entry in walkdir
{
if let Ok(entry) = dir_entry
{
if !entry.file_type().is_dir()
{
continue;
}
package_list.push(entry.file_name().to_string_lossy().to_string());
}
}
package_list.sort();
self.package_list = Some(package_list.clone());
package_list
}
fn read_file(&mut self, full_pathname : &str) -> Result<Box<dyn ReadSeek>, PackageError>
{
let mut path = PathBuf::from(&self.basedir);
path.push(full_pathname);
let file = BufReader::new(try_or_else!(File::open(path),
|error| Err(PackageError::IoError(error))));
Ok(Box::new(file))
}
fn write_file(&mut self, full_pathname : &str) -> Result<Box<dyn Write>, PackageError>
{
let mut path = PathBuf::from(&self.basedir);
path.push(full_pathname);
let mut folder_path = path.clone();
if !full_pathname.ends_with("/")
{
folder_path.pop();
}
try_or_else!(std::fs::create_dir_all(folder_path),
|error| Err(PackageError::IoError(error)));
let file = BufWriter::new(try_or_else!(File::create(path),
|error| Err(PackageError::IoError(error))));
Ok(Box::new(file))
}
fn iter_entries(&mut self, package_name : &str, type_folder : &str) -> Box<dyn Iterator<Item = Result<String, PackageError>>>
{
let mut path = PathBuf::from(&self.basedir);
path.push(package_name);
path.push(type_folder);
Box::new(FilesystemIter::new(path))
}
fn trust_level(&self, _package_name: &str) -> TrustLevel
{
self.trust
}
}