Skip to content

Commit 330c6dc

Browse files
committed
refactor(index): pass pkg name to cache manager instead of relative paths
1 parent be1bee1 commit 330c6dc

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
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

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

556552
let mut cached_summaries = None;
557553
let mut index_version = None;
558-
if let Some(contents) = cache_manager.get(&cache_path) {
554+
if let Some(contents) = cache_manager.get(name) {
559555
match Summaries::parse_cache(contents) {
560556
Ok((s, v)) => {
561557
cached_summaries = Some(s);
562558
index_version = Some(v);
563559
}
564560
Err(e) => {
565-
tracing::debug!("failed to parse {:?} cache: {}", relative, e);
561+
tracing::debug!("failed to parse {name:?} cache: {e}");
566562
}
567563
}
568564
}
@@ -575,7 +571,7 @@ impl Summaries {
575571
return Poll::Ready(Ok(cached_summaries));
576572
}
577573
LoadResponse::NotFound => {
578-
cache_manager.invalidate(&cache_path);
574+
cache_manager.invalidate(name);
579575
return Poll::Ready(Ok(None));
580576
}
581577
LoadResponse::Data {
@@ -624,7 +620,7 @@ impl Summaries {
624620
// Once we have our `cache_bytes` which represents the `Summaries` we're
625621
// about to return, write that back out to disk so future Cargo
626622
// invocations can use it.
627-
cache_manager.put(&cache_path, &cache_bytes);
623+
cache_manager.put(name, &cache_bytes);
628624

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

0 commit comments

Comments
 (0)