Skip to content

Commit e97cd02

Browse files
committed
refactor(index): pass pkg name to cache manager instead of relative paths
1 parent 32db09c commit e97cd02

File tree

2 files changed

+38
-26
lines changed

2 files changed

+38
-26
lines changed

src/cargo/sources/registry/index/cache.rs

+30-14
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,11 @@
6767
6868
use std::fs;
6969
use std::io;
70-
use std::path::Path;
70+
use std::path::PathBuf;
7171
use std::str;
7272

7373
use anyhow::bail;
74+
use cargo_util::registry::make_dep_path;
7475
use semver::Version;
7576

7677
use crate::util::cache_lock::CacheLockMode;
@@ -221,45 +222,60 @@ impl<'a> SummariesCache<'a> {
221222

222223
/// Manages the on-disk index caches.
223224
pub struct CacheManager<'gctx> {
225+
/// The root path where caches are located.
226+
cache_root: Filesystem,
224227
/// [`GlobalContext`] reference for convenience.
225228
pub gctx: &'gctx GlobalContext,
226229
}
227230

228231
impl<'gctx> CacheManager<'gctx> {
229232
/// 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 }
232237
}
233238

234239
/// 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) {
237243
Ok(contents) => Some(contents),
238244
Err(e) => {
239-
tracing::debug!(?key, "cache missing: {e}");
245+
tracing::debug!(?cache_path, "cache missing: {e}");
240246
None
241247
}
242248
}
243249
}
244250

245251
/// 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());
249256
self.gctx
250257
.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}");
253260
}
254261
}
255262
}
256263

257264
/// 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) {
260268
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}");
262270
}
263271
}
264272
}
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+
}
265281
}

src/cargo/sources/registry/index/mod.rs

+8-12
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ impl<'gctx> RegistryIndex<'gctx> {
304304
path: path.clone(),
305305
summaries_cache: HashMap::new(),
306306
gctx,
307-
cache_manager: CacheManager::new(gctx),
307+
cache_manager: CacheManager::new(path.join(".cache"), gctx),
308308
}
309309
}
310310

@@ -542,24 +542,20 @@ impl Summaries {
542542
cache_manager: &CacheManager<'_>,
543543
) -> Poll<CargoResult<Option<Summaries>>> {
544544
// This is the file we're loading from cache or the index data.
545-
// See module comment in `registry/mod.rs` for why this is structured
546-
// the way it is.
547-
let relative = make_dep_path(&name.to_lowercase(), false);
548-
// First up, attempt to load the cache. This could fail for all manner
549-
// of reasons, but consider all of them non-fatal and just log their
550-
// occurrence in case anyone is debugging anything.
551-
let cache_path = root.join(".cache").join(&relative);
545+
// See module comment in `registry/mod.rs` for why this is structured the way it is.
546+
let name = &name.to_lowercase();
547+
let relative = make_dep_path(&name, false);
552548

553549
let mut cached_summaries = None;
554550
let mut index_version = None;
555-
if let Some(contents) = cache_manager.get(&cache_path) {
551+
if let Some(contents) = cache_manager.get(name) {
556552
match Summaries::parse_cache(contents) {
557553
Ok((s, v)) => {
558554
cached_summaries = Some(s);
559555
index_version = Some(v);
560556
}
561557
Err(e) => {
562-
tracing::debug!("failed to parse {:?} cache: {}", relative, e);
558+
tracing::debug!("failed to parse {name:?} cache: {e}");
563559
}
564560
}
565561
}
@@ -574,7 +570,7 @@ impl Summaries {
574570
return Poll::Ready(Ok(cached_summaries));
575571
}
576572
LoadResponse::NotFound => {
577-
cache_manager.invalidate(&cache_path);
573+
cache_manager.invalidate(name);
578574
return Poll::Ready(Ok(None));
579575
}
580576
LoadResponse::Data {
@@ -623,7 +619,7 @@ impl Summaries {
623619
// Once we have our `cache_bytes` which represents the `Summaries` we're
624620
// about to return, write that back out to disk so future Cargo
625621
// invocations can use it.
626-
cache_manager.put(&cache_path, &cache_bytes);
622+
cache_manager.put(name, &cache_bytes);
627623

628624
// If we've got debug assertions enabled read back in the cached values
629625
// and assert they match the expected result.

0 commit comments

Comments
 (0)