|
67 | 67 |
|
68 | 68 | use std::fs;
|
69 | 69 | use std::io;
|
70 |
| -use std::path::Path; |
| 70 | +use std::path::PathBuf; |
71 | 71 | use std::str;
|
72 | 72 |
|
73 | 73 | use anyhow::bail;
|
| 74 | +use cargo_util::registry::make_dep_path; |
74 | 75 | use semver::Version;
|
75 | 76 |
|
76 | 77 | use crate::util::cache_lock::CacheLockMode;
|
@@ -221,45 +222,60 @@ impl<'a> SummariesCache<'a> {
|
221 | 222 |
|
222 | 223 | /// Manages the on-disk index caches.
|
223 | 224 | pub struct CacheManager<'gctx> {
|
| 225 | + /// The root path where caches are located. |
| 226 | + cache_root: Filesystem, |
224 | 227 | /// [`GlobalContext`] reference for convenience.
|
225 | 228 | gctx: &'gctx GlobalContext,
|
226 | 229 | }
|
227 | 230 |
|
228 | 231 | impl<'gctx> CacheManager<'gctx> {
|
229 | 232 | /// Creates a new instance of the on-disk index cache manager.
|
230 |
| - pub fn new(gctx: &'gctx GlobalContext) -> CacheManager<'gctx> { |
231 |
| - CacheManager { gctx } |
| 233 | + /// |
| 234 | + /// `root` --- The root path where caches are located. |
| 235 | + pub fn new(cache_root: Filesystem, gctx: &'gctx GlobalContext) -> CacheManager<'gctx> { |
| 236 | + CacheManager { cache_root, gctx } |
232 | 237 | }
|
233 | 238 |
|
234 | 239 | /// Gets the cache associated with the key.
|
235 |
| - pub fn get(&self, key: &Path) -> Option<Vec<u8>> { |
236 |
| - match fs::read(key) { |
| 240 | + pub fn get(&self, key: &str) -> Option<Vec<u8>> { |
| 241 | + let cache_path = &self.cache_path(key); |
| 242 | + match fs::read(cache_path) { |
237 | 243 | Ok(contents) => Some(contents),
|
238 | 244 | Err(e) => {
|
239 |
| - tracing::debug!(?key, "cache missing: {e}"); |
| 245 | + tracing::debug!(?cache_path, "cache missing: {e}"); |
240 | 246 | None
|
241 | 247 | }
|
242 | 248 | }
|
243 | 249 | }
|
244 | 250 |
|
245 | 251 | /// Associates the value with the key.
|
246 |
| - pub fn put(&self, key: &Path, value: &[u8]) { |
247 |
| - if fs::create_dir_all(key.parent().unwrap()).is_ok() { |
248 |
| - let path = Filesystem::new(key.into()); |
| 252 | + pub fn put(&self, key: &str, value: &[u8]) { |
| 253 | + let cache_path = &self.cache_path(key); |
| 254 | + if fs::create_dir_all(cache_path.parent().unwrap()).is_ok() { |
| 255 | + let path = Filesystem::new(cache_path.clone()); |
249 | 256 | self.gctx
|
250 | 257 | .assert_package_cache_locked(CacheLockMode::DownloadExclusive, &path);
|
251 |
| - if let Err(e) = fs::write(key, value) { |
252 |
| - tracing::info!(?key, "failed to write cache: {e}"); |
| 258 | + if let Err(e) = fs::write(cache_path, value) { |
| 259 | + tracing::info!(?cache_path, "failed to write cache: {e}"); |
253 | 260 | }
|
254 | 261 | }
|
255 | 262 | }
|
256 | 263 |
|
257 | 264 | /// Invalidates the cache associated with the key.
|
258 |
| - pub fn invalidate(&self, key: &Path) { |
259 |
| - if let Err(e) = fs::remove_file(key) { |
| 265 | + pub fn invalidate(&self, key: &str) { |
| 266 | + let cache_path = &self.cache_path(key); |
| 267 | + if let Err(e) = fs::remove_file(cache_path) { |
260 | 268 | if e.kind() != io::ErrorKind::NotFound {
|
261 |
| - tracing::debug!(?key, "failed to remove from cache: {e}"); |
| 269 | + tracing::debug!(?cache_path, "failed to remove from cache: {e}"); |
262 | 270 | }
|
263 | 271 | }
|
264 | 272 | }
|
| 273 | + |
| 274 | + fn cache_path(&self, key: &str) -> PathBuf { |
| 275 | + let relative = make_dep_path(key, false); |
| 276 | + // This is the file we're loading from cache or the index data. |
| 277 | + // See module comment in `registry/mod.rs` for why this is structured |
| 278 | + // the way it is. |
| 279 | + self.cache_root.join(relative).into_path_unlocked() |
| 280 | + } |
265 | 281 | }
|
0 commit comments