-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcrate_fs.rs
221 lines (191 loc) · 5.72 KB
/
crate_fs.rs
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
#![allow(clippy::module_name_repetitions)]
use circular_buffer::CircularBuffer;
use std::{
path::{Path, PathBuf},
sync::Mutex,
};
/// Top error type returned during any stage of analysis from compile to data import.
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("IO Error: {0}")]
IoError(#[from] std::io::Error),
#[error("{0}")]
IndexError(#[from] crates_index::Error),
#[error("CrateNotFound")]
CrateNotFound,
#[error("CrateFileNotFound")]
CrateFileNotFound,
#[error("CrateFileNotFound")]
ExtractionFailed,
#[error(
"Crate name contained invalid characters or did not match the NAME-VER format. Name: {0}"
)]
CrateNameError(String),
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct CrateEntry {
pub full_name: String,
}
impl CrateEntry {
pub fn new(full_name: String) -> Result<Self, Error> {
let (_, _) = full_name
.rsplit_once('-')
.ok_or(Error::CrateNameError(full_name.clone()))?;
// TODO: Semver check valid here
Ok(Self { full_name })
}
pub fn full_name(&self) -> &str {
&self.full_name
}
pub fn name(&self) -> &str {
self.full_name.rsplit_once('-').unwrap().0
}
pub fn version(&self) -> &str {
self.full_name.rsplit_once('-').unwrap().1
}
pub fn filename(&self) -> String {
format!("{}.crate", self.full_name())
}
}
impl<S> From<S> for CrateEntry
where
S: AsRef<str>,
{
fn from(rhv: S) -> CrateEntry {
Self::new(rhv.as_ref().to_string()).unwrap()
}
}
#[derive(Debug)]
pub struct CrateCache {
src_crate_file: PathBuf,
extracted_path: PathBuf,
no_delete: bool,
}
impl CrateCache {
pub fn new<P>(entry: &CrateEntry, crates_dir: P, sources_dir: P) -> Result<Self, Error>
where
P: AsRef<Path>,
{
let src_crate_file = crates_dir.as_ref().join(entry.filename());
let extracted_path = sources_dir.as_ref().join(entry.full_name()).clone();
if extracted_path.exists() {
return Ok(Self {
src_crate_file,
extracted_path,
no_delete: true,
});
}
log::trace!(
"Attempting extraction: {} -> {}",
src_crate_file.display(),
extracted_path.display()
);
let tar_gz = std::fs::File::open(&src_crate_file)?;
let tar = flate2::read::GzDecoder::new(tar_gz);
let mut archive = tar::Archive::new(tar);
archive.unpack(sources_dir.as_ref())?;
if !extracted_path.exists() {
return Err(Error::ExtractionFailed);
}
Ok(Self {
src_crate_file,
extracted_path,
no_delete: false,
})
}
pub fn path(&self) -> &Path {
&self.extracted_path
}
}
impl Drop for CrateCache {
fn drop(&mut self) {
log::trace!("dropping {:?}", self);
if !self.no_delete {
std::fs::remove_dir_all(&self.extracted_path).unwrap();
}
}
}
pub struct CrateFsConfig {
pub crates_path: PathBuf,
pub extract_path: PathBuf,
}
impl CrateFsConfig {
pub fn with_paths<P1, P2>(crates_path: P1, extract_path: P2) -> Self
where
P1: Into<PathBuf>,
P2: Into<PathBuf>,
{
let crates_path = crates_path.into();
let extract_path = extract_path.into();
// assert paths exist
assert!(crates_path.exists());
assert!(extract_path.exists());
Self {
crates_path,
extract_path,
}
}
}
pub struct CrateFs {
cache: Box<CircularBuffer<1024, (CrateEntry, CrateCache)>>,
index: crates_index::Index,
config: CrateFsConfig,
}
impl CrateFs {
pub fn new(config: CrateFsConfig) -> Result<Self, Error> {
let index = crates_index::Index::new_cargo_default()?;
Ok(Self {
cache: CircularBuffer::boxed(),
config,
index,
})
}
fn find_cache_index(&self, entry: &CrateEntry) -> Option<usize> {
self.cache.iter().enumerate().find_map(
|(i, (e, _))| {
if *e == *entry {
Some(i)
} else {
None
}
},
)
}
pub fn close<S: AsRef<str>>(&mut self, fullname: S) -> Result<(), Error> {
let entry = CrateEntry::new(fullname.as_ref().to_string())?;
self.cache
.remove(self.find_cache_index(&entry).ok_or(Error::CrateNotFound)?);
Ok(())
}
pub fn open<S: AsRef<str>>(&mut self, fullname: S) -> Result<&CrateCache, Error> {
let entry = CrateEntry::new(fullname.as_ref().to_string())?;
if let Some(index) = self.find_cache_index(&entry) {
Ok(&self.cache.get(index).ok_or(Error::CrateNotFound)?.1)
} else {
// Check that we have the crate file
let cratefile_path = self
.config
.crates_path
.join(format!("{}.crate", entry.full_name()));
if !cratefile_path.exists() {
return Err(Error::CrateFileNotFound);
}
// Check we have capcity, otherwise purge the front entry
let cache_entry =
CrateCache::new(&entry, &self.config.crates_path, &self.config.extract_path)?;
self.cache.push_back((entry, cache_entry));
Ok(&self.cache.back().ok_or(Error::CrateNotFound)?.1)
}
}
pub fn config(&self) -> &CrateFsConfig {
&self.config
}
}
#[cfg(test)]
mod tests {
use super::*;
fn init_logging() {
// capture log messages with test harness
let _ = env_logger::builder().is_test(true).try_init();
}
}