Skip to main content

uv_cache/
lib.rs

1use std::fmt::{Display, Formatter};
2use std::io;
3use std::io::Write;
4use std::ops::Deref;
5use std::path::{Path, PathBuf};
6use std::str::FromStr;
7use std::sync::Arc;
8
9use rustc_hash::FxHashMap;
10use tracing::{debug, trace, warn};
11
12use uv_cache_info::Timestamp;
13use uv_fs::{LockedFile, LockedFileError, LockedFileMode, Simplified, cachedir, directories};
14use uv_normalize::PackageName;
15use uv_pypi_types::ResolutionMetadata;
16
17pub use crate::by_timestamp::CachedByTimestamp;
18#[cfg(feature = "clap")]
19pub use crate::cli::CacheArgs;
20use crate::removal::Remover;
21pub use crate::removal::{Removal, RemovalAccounting};
22pub use crate::wheel::WheelCache;
23use crate::wheel::WheelCacheKind;
24pub use archive::ArchiveId;
25
26mod archive;
27mod by_timestamp;
28#[cfg(feature = "clap")]
29mod cli;
30mod removal;
31mod wheel;
32
33/// The version of the archive bucket.
34///
35/// Must be kept in-sync with the version in [`CacheBucket::to_str`].
36pub const ARCHIVE_VERSION: u8 = 0;
37
38/// Error locking a cache entry or shard
39#[derive(Debug, thiserror::Error)]
40pub enum Error {
41    #[error(transparent)]
42    Io(#[from] io::Error),
43    #[error("Failed to initialize cache at `{}`", _0.user_display())]
44    Init(PathBuf, #[source] io::Error),
45    #[error("Could not make the path absolute")]
46    Absolute(#[source] io::Error),
47    #[error("Could not acquire lock")]
48    Acquire(#[from] LockedFileError),
49}
50
51/// A [`CacheEntry`] which may or may not exist yet.
52#[derive(Debug, Clone)]
53pub struct CacheEntry(PathBuf);
54
55impl CacheEntry {
56    /// Create a new [`CacheEntry`] from a directory and a file name.
57    pub fn new(dir: impl Into<PathBuf>, file: impl AsRef<Path>) -> Self {
58        Self(dir.into().join(file))
59    }
60
61    /// Create a new [`CacheEntry`] from a path.
62    pub fn from_path(path: impl Into<PathBuf>) -> Self {
63        Self(path.into())
64    }
65
66    /// Return the cache entry's parent directory.
67    pub fn shard(&self) -> CacheShard {
68        CacheShard(self.dir().to_path_buf())
69    }
70
71    /// Convert the [`CacheEntry`] into a [`PathBuf`].
72    #[inline]
73    pub fn into_path_buf(self) -> PathBuf {
74        self.0
75    }
76
77    /// Return the path to the [`CacheEntry`].
78    #[inline]
79    pub fn path(&self) -> &Path {
80        &self.0
81    }
82
83    /// Return the cache entry's parent directory.
84    #[inline]
85    pub fn dir(&self) -> &Path {
86        self.0.parent().expect("Cache entry has no parent")
87    }
88
89    /// Create a new [`CacheEntry`] with the given file name.
90    #[must_use]
91    pub fn with_file(&self, file: impl AsRef<Path>) -> Self {
92        Self(self.dir().join(file))
93    }
94
95    /// Acquire the [`CacheEntry`] as an exclusive lock.
96    pub async fn lock(&self) -> Result<LockedFile, Error> {
97        fs_err::create_dir_all(self.dir())?;
98        Ok(LockedFile::acquire(
99            self.path(),
100            LockedFileMode::Exclusive,
101            self.path().display(),
102        )
103        .await?)
104    }
105}
106
107impl AsRef<Path> for CacheEntry {
108    fn as_ref(&self) -> &Path {
109        &self.0
110    }
111}
112
113/// A subdirectory within the cache.
114#[derive(Debug, Clone)]
115pub struct CacheShard(PathBuf);
116
117impl CacheShard {
118    /// Return a [`CacheEntry`] within this shard.
119    pub fn entry(&self, file: impl AsRef<Path>) -> CacheEntry {
120        CacheEntry::new(&self.0, file)
121    }
122
123    /// Return a [`CacheShard`] within this shard.
124    #[must_use]
125    pub fn shard(&self, dir: impl AsRef<Path>) -> Self {
126        Self(self.0.join(dir.as_ref()))
127    }
128
129    /// Acquire the cache entry as an exclusive lock.
130    pub async fn lock(&self) -> Result<LockedFile, Error> {
131        fs_err::create_dir_all(self.as_ref())?;
132        Ok(LockedFile::acquire(
133            self.join(".lock"),
134            LockedFileMode::Exclusive,
135            self.display(),
136        )
137        .await?)
138    }
139
140    /// Return the [`CacheShard`] as a [`PathBuf`].
141    pub fn into_path_buf(self) -> PathBuf {
142        self.0
143    }
144}
145
146impl AsRef<Path> for CacheShard {
147    fn as_ref(&self) -> &Path {
148        &self.0
149    }
150}
151
152impl Deref for CacheShard {
153    type Target = Path;
154
155    fn deref(&self) -> &Self::Target {
156        &self.0
157    }
158}
159
160/// The main cache abstraction.
161///
162/// While the cache is active, it holds a read (shared) lock that prevents cache cleaning
163#[derive(Debug, Clone)]
164pub struct Cache {
165    /// The cache directory.
166    root: PathBuf,
167    /// The refresh strategy to use when reading from the cache.
168    refresh: Refresh,
169    /// A temporary cache directory, if the user requested `--no-cache`.
170    ///
171    /// Included to ensure that the temporary directory exists for the length of the operation, but
172    /// is dropped at the end as appropriate.
173    temp_dir: Option<Arc<tempfile::TempDir>>,
174    /// Ensure that `uv cache` operations don't remove items from the cache that are used by another
175    /// uv process.
176    lock_file: Option<Arc<LockedFile>>,
177    /// The storage accounting used when removing cache entries.
178    removal_accounting: RemovalAccounting,
179}
180
181impl Cache {
182    /// A persistent cache directory at `root`.
183    pub fn from_path(root: impl Into<PathBuf>) -> Self {
184        Self {
185            root: root.into(),
186            refresh: Refresh::None(Timestamp::now()),
187            temp_dir: None,
188            lock_file: None,
189            removal_accounting: RemovalAccounting::Coarse,
190        }
191    }
192
193    /// Create a temporary cache directory.
194    pub fn temp() -> Result<Self, io::Error> {
195        let temp_dir = tempfile::tempdir()?;
196        Ok(Self {
197            root: temp_dir.path().to_path_buf(),
198            refresh: Refresh::None(Timestamp::now()),
199            temp_dir: Some(Arc::new(temp_dir)),
200            lock_file: None,
201            removal_accounting: RemovalAccounting::Coarse,
202        })
203    }
204
205    /// Set the [`Refresh`] policy for the cache.
206    #[must_use]
207    pub fn with_refresh(self, refresh: Refresh) -> Self {
208        Self { refresh, ..self }
209    }
210
211    /// Set the storage accounting used when removing cache entries.
212    ///
213    /// Falls back to [`RemovalAccounting::Coarse`] when fine-grained accounting is unsupported.
214    #[must_use]
215    pub fn with_removal_accounting(self, removal_accounting: RemovalAccounting) -> Self {
216        let removal_accounting = match removal_accounting {
217            RemovalAccounting::Fine if !uv_fs::supports_fine_grained_accounting() => {
218                RemovalAccounting::Coarse
219            }
220            removal_accounting => removal_accounting,
221        };
222        Self {
223            removal_accounting,
224            ..self
225        }
226    }
227
228    /// Create an empty removal summary using the cache's configured accounting.
229    pub fn removal(&self) -> Removal {
230        Removal::new(self.removal_accounting)
231    }
232
233    /// Acquire a lock that allows removing entries from the cache.
234    pub async fn with_exclusive_lock(self) -> Result<Self, LockedFileError> {
235        let Self {
236            root,
237            refresh,
238            temp_dir,
239            lock_file,
240            removal_accounting,
241        } = self;
242
243        // Release the existing lock, avoid deadlocks from a cloned cache.
244        if let Some(lock_file) = lock_file {
245            drop(
246                Arc::try_unwrap(lock_file).expect(
247                    "cloning the cache before acquiring an exclusive lock causes a deadlock",
248                ),
249            );
250        }
251        let lock_file = LockedFile::acquire(
252            root.join(".lock"),
253            LockedFileMode::Exclusive,
254            root.simplified_display(),
255        )
256        .await?;
257
258        Ok(Self {
259            root,
260            refresh,
261            temp_dir,
262            lock_file: Some(Arc::new(lock_file)),
263            removal_accounting,
264        })
265    }
266
267    /// Acquire a lock that allows removing entries from the cache, if available.
268    ///
269    /// If the lock is not immediately available, returns [`Err`] with self.
270    pub fn with_exclusive_lock_no_wait(self) -> Result<Self, Self> {
271        let Self {
272            root,
273            refresh,
274            temp_dir,
275            lock_file,
276            removal_accounting,
277        } = self;
278
279        match LockedFile::acquire_no_wait(
280            root.join(".lock"),
281            LockedFileMode::Exclusive,
282            root.simplified_display(),
283        ) {
284            Some(lock_file) => Ok(Self {
285                root,
286                refresh,
287                temp_dir,
288                lock_file: Some(Arc::new(lock_file)),
289                removal_accounting,
290            }),
291            None => Err(Self {
292                root,
293                refresh,
294                temp_dir,
295                lock_file,
296                removal_accounting,
297            }),
298        }
299    }
300
301    /// Return the root of the cache.
302    pub fn root(&self) -> &Path {
303        &self.root
304    }
305
306    /// The folder for a specific cache bucket
307    pub fn bucket(&self, cache_bucket: CacheBucket) -> PathBuf {
308        self.root.join(cache_bucket.to_str())
309    }
310
311    /// Compute an entry in the cache.
312    pub fn shard(&self, cache_bucket: CacheBucket, dir: impl AsRef<Path>) -> CacheShard {
313        CacheShard(self.bucket(cache_bucket).join(dir.as_ref()))
314    }
315
316    /// Compute an entry in the cache.
317    pub fn entry(
318        &self,
319        cache_bucket: CacheBucket,
320        dir: impl AsRef<Path>,
321        file: impl AsRef<Path>,
322    ) -> CacheEntry {
323        CacheEntry::new(self.bucket(cache_bucket).join(dir), file)
324    }
325
326    /// Return the path to an archive in the cache.
327    pub fn archive(&self, id: &ArchiveId) -> PathBuf {
328        self.bucket(CacheBucket::Archive).join(id)
329    }
330
331    /// Create a temporary directory to be used as a Python virtual environment.
332    pub fn venv_dir(&self) -> io::Result<tempfile::TempDir> {
333        fs_err::create_dir_all(self.bucket(CacheBucket::Builds))?;
334        tempfile::tempdir_in(self.bucket(CacheBucket::Builds))
335    }
336
337    /// Create a temporary directory to be used for executing PEP 517 source distribution builds.
338    pub fn build_dir(&self) -> io::Result<tempfile::TempDir> {
339        fs_err::create_dir_all(self.bucket(CacheBucket::Builds))?;
340        tempfile::tempdir_in(self.bucket(CacheBucket::Builds))
341    }
342
343    /// Returns `true` if a cache entry must be revalidated given the [`Refresh`] policy.
344    pub fn must_revalidate_package(&self, package: &PackageName) -> bool {
345        match &self.refresh {
346            Refresh::None(_) => false,
347            Refresh::All(_) => true,
348            Refresh::Packages(packages, _, _) => packages.contains(package),
349        }
350    }
351
352    /// Returns `true` if a cache entry must be revalidated given the [`Refresh`] policy.
353    pub fn must_revalidate_path(&self, path: &Path) -> bool {
354        match &self.refresh {
355            Refresh::None(_) => false,
356            Refresh::All(_) => true,
357            Refresh::Packages(_, paths, _) => paths
358                .iter()
359                .any(|target| same_file::is_same_file(path, target).unwrap_or(false)),
360        }
361    }
362
363    /// Returns the [`Freshness`] for a cache entry, validating it against the [`Refresh`] policy.
364    ///
365    /// A cache entry is considered fresh if it was created after the cache itself was
366    /// initialized, or if the [`Refresh`] policy does not require revalidation.
367    pub fn freshness(
368        &self,
369        entry: &CacheEntry,
370        package: Option<&PackageName>,
371        path: Option<&Path>,
372    ) -> io::Result<Freshness> {
373        // Grab the cutoff timestamp, if it's relevant.
374        let timestamp = match &self.refresh {
375            Refresh::None(_) => return Ok(Freshness::Fresh),
376            Refresh::All(timestamp) => timestamp,
377            Refresh::Packages(packages, paths, timestamp) => {
378                if package.is_none_or(|package| packages.contains(package))
379                    || path.is_some_and(|path| {
380                        paths
381                            .iter()
382                            .any(|target| same_file::is_same_file(path, target).unwrap_or(false))
383                    })
384                {
385                    timestamp
386                } else {
387                    return Ok(Freshness::Fresh);
388                }
389            }
390        };
391
392        match fs_err::metadata(entry.path()) {
393            Ok(metadata) => {
394                if Timestamp::from_metadata(&metadata) >= *timestamp {
395                    Ok(Freshness::Fresh)
396                } else {
397                    Ok(Freshness::Stale)
398                }
399            }
400            Err(err) if err.kind() == io::ErrorKind::NotFound => Ok(Freshness::Missing),
401            Err(err) => Err(err),
402        }
403    }
404
405    /// Persist a temporary directory to the artifact store, returning its unique ID.
406    pub async fn persist(
407        &self,
408        temp_dir: impl AsRef<Path>,
409        path: impl AsRef<Path>,
410    ) -> io::Result<ArchiveId> {
411        // Create a unique ID for the artifact.
412        // TODO(charlie): Support content-addressed persistence via SHAs.
413        let id = ArchiveId::new();
414
415        // Move the temporary directory into the directory store.
416        let archive_entry = self.entry(CacheBucket::Archive, "", &id);
417        fs_err::create_dir_all(archive_entry.dir())?;
418        uv_fs::rename_with_retry(temp_dir.as_ref(), archive_entry.path()).await?;
419
420        // Create a symlink to the directory store.
421        fs_err::create_dir_all(path.as_ref().parent().expect("Cache entry to have parent"))?;
422        self.create_link(&id, path.as_ref())?;
423
424        Ok(id)
425    }
426
427    /// Returns `true` if the [`Cache`] is temporary.
428    pub fn is_temporary(&self) -> bool {
429        self.temp_dir.is_some()
430    }
431
432    /// Populate the cache scaffold.
433    fn create_base_files(root: &PathBuf) -> io::Result<()> {
434        // Create the cache directory, if it doesn't exist.
435        fs_err::create_dir_all(root)?;
436
437        // Add the CACHEDIR.TAG.
438        cachedir::ensure_tag(root)?;
439
440        // Add the .gitignore.
441        match fs_err::OpenOptions::new()
442            .write(true)
443            .create_new(true)
444            .open(root.join(".gitignore"))
445        {
446            Ok(mut file) => file.write_all(b"*")?,
447            Err(err) if err.kind() == io::ErrorKind::AlreadyExists => (),
448            Err(err) => return Err(err),
449        }
450
451        // Add an empty .gitignore to the build bucket, to ensure that the cache's own .gitignore
452        // doesn't interfere with source distribution builds. Build backends (like hatchling) will
453        // traverse upwards to look for .gitignore files.
454        fs_err::create_dir_all(root.join(CacheBucket::SourceDistributions.to_str()))?;
455        match fs_err::OpenOptions::new()
456            .write(true)
457            .create_new(true)
458            .open(
459                root.join(CacheBucket::SourceDistributions.to_str())
460                    .join(".gitignore"),
461            ) {
462            Ok(_) => {}
463            Err(err) if err.kind() == io::ErrorKind::AlreadyExists => (),
464            Err(err) => return Err(err),
465        }
466
467        // Add a phony .git, if it doesn't exist, to ensure that the cache isn't considered to be
468        // part of a Git repository. (Some packages will include Git metadata (like a hash) in the
469        // built version if they're in a Git repository, but the cache should be viewed as an
470        // isolated store.).
471        // We have to put this below the gitignore. Otherwise, if the build backend uses the rust
472        // ignore crate it will walk up to the top level .gitignore and ignore its python source
473        // files.
474        let phony_git = root
475            .join(CacheBucket::SourceDistributions.to_str())
476            .join(".git");
477        match fs_err::OpenOptions::new()
478            .create(true)
479            .write(true)
480            .open(&phony_git)
481        {
482            Ok(_) => {}
483            // Handle read-only caches including sandboxed environments.
484            Err(err) if err.kind() == io::ErrorKind::ReadOnlyFilesystem => {
485                if !phony_git.exists() {
486                    return Err(err);
487                }
488            }
489            Err(err) => return Err(err),
490        }
491
492        Ok(())
493    }
494
495    /// Initialize the [`Cache`].
496    pub async fn init(self) -> Result<Self, Error> {
497        let root = &self.root;
498
499        Self::create_base_files(root).map_err(|err| Error::Init(root.clone(), err))?;
500
501        // Block cache removal operations from interfering.
502        let lock_file = match LockedFile::acquire(
503            root.join(".lock"),
504            LockedFileMode::Shared,
505            root.simplified_display(),
506        )
507        .await
508        {
509            Ok(lock_file) => Some(Arc::new(lock_file)),
510            Err(err)
511                if err
512                    .as_io_error()
513                    .is_some_and(|err| err.kind() == io::ErrorKind::Unsupported) =>
514            {
515                warn!(
516                    "Shared locking is not supported by the current platform or filesystem, \
517                        reduced parallel process safety with `uv cache clean` and `uv cache prune`."
518                );
519                None
520            }
521            Err(err) => return Err(err.into()),
522        };
523
524        Ok(Self {
525            root: std::path::absolute(root).map_err(Error::Absolute)?,
526            lock_file,
527            ..self
528        })
529    }
530
531    /// Initialize the [`Cache`], assuming that there are no other uv processes running.
532    pub fn init_no_wait(self) -> Result<Option<Self>, Error> {
533        let root = &self.root;
534
535        Self::create_base_files(root).map_err(|err| Error::Init(root.clone(), err))?;
536
537        // Block cache removal operations from interfering.
538        let Some(lock_file) = LockedFile::acquire_no_wait(
539            root.join(".lock"),
540            LockedFileMode::Shared,
541            root.simplified_display(),
542        ) else {
543            return Ok(None);
544        };
545        Ok(Some(Self {
546            root: std::path::absolute(root).map_err(Error::Absolute)?,
547            lock_file: Some(Arc::new(lock_file)),
548            ..self
549        }))
550    }
551
552    /// Clear the cache, removing all entries.
553    pub fn clear(self, reporter: Box<dyn CleanReporter>) -> Result<Removal, io::Error> {
554        // Remove everything but `.lock`, Windows does not allow removal of a locked file
555        let mut removal = Remover::new(reporter)
556            .with_removal_accounting(self.removal_accounting)
557            .rm_rf(&self.root, true)?;
558        let Self {
559            root, lock_file, ..
560        } = self;
561
562        // Remove the `.lock` file, unlocking it first
563        if let Some(lock) = lock_file {
564            drop(lock);
565            fs_err::remove_file(root.join(".lock"))?;
566        }
567        removal.num_files += 1;
568
569        // Remove the root directory
570        match fs_err::remove_dir(root) {
571            Ok(()) => {
572                removal.num_dirs += 1;
573            }
574            // On Windows, when `--force` is used, the `.lock` file can exist and be unremovable,
575            // so we make this non-fatal
576            Err(err) if err.kind() == io::ErrorKind::DirectoryNotEmpty => {
577                trace!("Failed to remove root cache directory: not empty");
578            }
579            Err(err) => return Err(err),
580        }
581
582        Ok(removal)
583    }
584
585    /// Remove a package from the cache.
586    ///
587    /// Returns the number of entries removed from the cache.
588    pub fn remove(&self, name: &PackageName) -> io::Result<Removal> {
589        // Collect the set of referenced archives.
590        let references = self.find_archive_references()?;
591
592        // Remove any entries for the package from the cache.
593        let mut summary = self.removal();
594        for bucket in CacheBucket::iter() {
595            summary += bucket.remove(self, name)?;
596        }
597
598        if references.is_empty() {
599            return Ok(summary);
600        }
601
602        // Only remove targets in the archive bucket. Cache entries may contain unexpected links
603        // to paths outside the cache.
604        let archive_root = fs_err::canonicalize(&self.root)?.join(CacheBucket::Archive.to_str());
605
606        // Remove any archives that are no longer referenced.
607        for (target, references) in references {
608            if target.starts_with(&archive_root) && references.iter().all(|path| !path.exists()) {
609                debug!("Removing dangling cache entry: {}", target.display());
610                summary += self.remove_path(target)?;
611            }
612        }
613
614        Ok(summary)
615    }
616
617    /// Prune dangling cache entries and cached environments.
618    pub fn prune(&self, ci: bool) -> Result<Removal, io::Error> {
619        let mut summary = self.removal();
620
621        // First, remove any top-level directories that are unused. These typically represent
622        // outdated cache buckets (e.g., `wheels-v0`, when latest is `wheels-v1`).
623        for entry in fs_err::read_dir(&self.root)? {
624            let entry = entry?;
625            let metadata = entry.metadata()?;
626
627            if entry.file_name() == "CACHEDIR.TAG"
628                || entry.file_name() == ".gitignore"
629                || entry.file_name() == ".git"
630                || entry.file_name() == ".lock"
631            {
632                continue;
633            }
634
635            if metadata.is_dir() {
636                // If the directory is not a cache bucket, remove it.
637                if CacheBucket::iter().all(|bucket| entry.file_name() != bucket.to_str()) {
638                    let path = entry.path();
639                    debug!("Removing dangling cache bucket: {}", path.display());
640                    summary += self.remove_path(path)?;
641                }
642            } else {
643                // If the file is not a marker file, remove it.
644                let path = entry.path();
645                debug!("Removing dangling cache bucket: {}", path.display());
646                summary += self.remove_path(path)?;
647            }
648        }
649
650        // Second, remove all cached environments. Centralized project environments can be
651        // referenced by `.venv` links, but are recreated when next needed.
652        match fs_err::read_dir(self.bucket(CacheBucket::Environments)) {
653            Ok(entries) => {
654                for entry in entries {
655                    let entry = entry?;
656                    let path = entry.path();
657                    debug!("Removing cached environment: {}", path.display());
658                    summary += self.remove_path(path)?;
659                }
660            }
661            Err(err) if err.kind() == io::ErrorKind::NotFound => (),
662            Err(err) => return Err(err),
663        }
664
665        // Third, if enabled, remove all unzipped wheels, leaving only the wheel archives.
666        if ci {
667            // Remove the entire pre-built wheel cache, since every entry is an unzipped wheel.
668            match fs_err::read_dir(self.bucket(CacheBucket::Wheels)) {
669                Ok(entries) => {
670                    for entry in entries {
671                        let entry = entry?;
672                        let path = entry.path();
673                        if path.is_dir() {
674                            debug!("Removing unzipped wheel entry: {}", path.display());
675                            summary += self.remove_path(path)?;
676                        }
677                    }
678                }
679                Err(err) if err.kind() == io::ErrorKind::NotFound => (),
680                Err(err) => return Err(err),
681            }
682
683            let source_distributions = self.bucket(CacheBucket::SourceDistributions);
684            if source_distributions.try_exists()? {
685                for entry in walkdir::WalkDir::new(source_distributions) {
686                    let entry = entry?;
687
688                    // If the directory contains a `metadata.msgpack`, then it's a built wheel revision.
689                    if !entry.file_type().is_dir() {
690                        continue;
691                    }
692
693                    if !entry.path().join("metadata.msgpack").exists() {
694                        continue;
695                    }
696
697                    // Remove everything except the built wheel archive and the metadata.
698                    for entry in fs_err::read_dir(entry.path())? {
699                        let entry = entry?;
700                        let path = entry.path();
701
702                        // Retain the resolved metadata (`metadata.msgpack`).
703                        if path
704                            .file_name()
705                            .is_some_and(|file_name| file_name == "metadata.msgpack")
706                        {
707                            continue;
708                        }
709
710                        // Retain any built wheel archives.
711                        if path
712                            .extension()
713                            .is_some_and(|ext| ext.eq_ignore_ascii_case("whl"))
714                        {
715                            continue;
716                        }
717
718                        debug!("Removing unzipped built wheel entry: {}", path.display());
719                        summary += self.remove_path(path)?;
720                    }
721                }
722            }
723        }
724
725        // Fourth, remove any unused archives (by searching for archives that are not symlinked).
726        let references = self.find_archive_references()?;
727
728        match fs_err::read_dir(self.bucket(CacheBucket::Archive)) {
729            Ok(entries) => {
730                for entry in entries {
731                    let entry = entry?;
732                    let path = entry.path();
733                    let target = fs_err::canonicalize(&path)?;
734                    if !references.contains_key(&target) {
735                        debug!("Removing dangling cache archive: {}", path.display());
736                        summary += self.remove_path(path)?;
737                    }
738                }
739            }
740            Err(err) if err.kind() == io::ErrorKind::NotFound => (),
741            Err(err) => return Err(err),
742        }
743
744        Ok(summary)
745    }
746
747    /// Remove a cache path using the cache's configured storage accounting.
748    pub fn remove_path(&self, path: impl AsRef<Path>) -> io::Result<Removal> {
749        Remover::default()
750            .with_removal_accounting(self.removal_accounting)
751            .rm_rf(path, false)
752    }
753
754    /// Find all references to entries in the archive bucket.
755    ///
756    /// Archive entries are often referenced by symlinks in other cache buckets. This method
757    /// searches for all such references.
758    ///
759    /// Returns a map from archive path to paths that reference it.
760    fn find_archive_references(&self) -> Result<FxHashMap<PathBuf, Vec<PathBuf>>, io::Error> {
761        let mut references = FxHashMap::<PathBuf, Vec<PathBuf>>::default();
762        for bucket in [CacheBucket::SourceDistributions, CacheBucket::Wheels] {
763            let bucket_path = self.bucket(bucket);
764            if bucket_path.is_dir() {
765                let walker = walkdir::WalkDir::new(&bucket_path).into_iter();
766                for entry in walker.filter_entry(|entry| {
767                    !(
768                        // As an optimization, ignore any `.lock`, `.whl`, `.msgpack`, `.rev`, or
769                        // `.http` files, along with the `src` directory, which represents the
770                        // unpacked source distribution.
771                        entry.file_name() == "src"
772                            || entry.file_name() == ".lock"
773                            || entry.file_name() == ".gitignore"
774                            || entry.path().extension().is_some_and(|ext| {
775                                ext.eq_ignore_ascii_case("lock")
776                                    || ext.eq_ignore_ascii_case("whl")
777                                    || ext.eq_ignore_ascii_case("http")
778                                    || ext.eq_ignore_ascii_case("rev")
779                                    || ext.eq_ignore_ascii_case("msgpack")
780                            })
781                    )
782                }) {
783                    let entry = entry?;
784
785                    // On Unix, archive references use symlinks.
786                    if cfg!(unix) {
787                        if !entry.file_type().is_symlink() {
788                            continue;
789                        }
790                    }
791
792                    // On Windows, archive references are files containing structured data.
793                    if cfg!(windows) {
794                        if !entry.file_type().is_file() {
795                            continue;
796                        }
797                    }
798
799                    if let Ok(target) = self.resolve_link(entry.path()) {
800                        references
801                            .entry(target)
802                            .or_default()
803                            .push(entry.path().to_path_buf());
804                    }
805                }
806            }
807        }
808        Ok(references)
809    }
810
811    /// Create a link to a directory in the archive bucket.
812    ///
813    /// On Windows, we write structured data ([`Link`]) to a file containing the archive ID and
814    /// version. On Unix, we create a symlink to the target directory.
815    #[cfg(windows)]
816    #[expect(clippy::unused_self)]
817    fn create_link(&self, id: &ArchiveId, dst: impl AsRef<Path>) -> io::Result<()> {
818        // Serialize the link.
819        let link = Link::new(id.clone());
820        let contents = link.to_string();
821
822        // First, attempt to create a file at the location, but fail if it already exists.
823        match fs_err::OpenOptions::new()
824            .write(true)
825            .create_new(true)
826            .open(dst.as_ref())
827        {
828            Ok(mut file) => {
829                // Write the target path to the file.
830                file.write_all(contents.as_bytes())?;
831                Ok(())
832            }
833            Err(err) if err.kind() == io::ErrorKind::AlreadyExists => {
834                // Write to a temporary file, then move it into place.
835                let temp_dir = tempfile::tempdir_in(dst.as_ref().parent().unwrap())?;
836                let temp_file = temp_dir.path().join("link");
837                fs_err::write(&temp_file, contents.as_bytes())?;
838
839                // Move the symlink into the target location.
840                fs_err::rename(&temp_file, dst.as_ref())?;
841
842                Ok(())
843            }
844            Err(err) => Err(err),
845        }
846    }
847
848    /// Resolve an archive link, returning the fully-resolved path.
849    ///
850    /// Returns an error if the link target does not exist.
851    #[cfg(windows)]
852    pub fn resolve_link(&self, path: impl AsRef<Path>) -> io::Result<PathBuf> {
853        // Deserialize the link.
854        let contents = fs_err::read_to_string(path.as_ref())?;
855        let link = Link::from_str(&contents)?;
856
857        // Ignore stale links.
858        if link.version != ARCHIVE_VERSION {
859            return Err(io::Error::new(
860                io::ErrorKind::NotFound,
861                "The link target does not exist.",
862            ));
863        }
864
865        // Reconstruct the path.
866        let path = self.archive(&link.id);
867        path.canonicalize()
868    }
869
870    /// Create a link to a directory in the archive bucket.
871    ///
872    /// On Windows, we write structured data ([`Link`]) to a file containing the archive ID and
873    /// version. On Unix, we create a symlink to the target directory.
874    #[cfg(unix)]
875    fn create_link(&self, id: &ArchiveId, dst: impl AsRef<Path>) -> io::Result<()> {
876        let dst = dst.as_ref();
877        let dst_parent = dst.parent().expect("Cache entry to have parent");
878        // Construct the relative link target.
879        let src = uv_fs::relative_to(self.archive(id), dst_parent)?;
880
881        // Attempt to create the symlink directly.
882        match fs_err::os::unix::fs::symlink(&src, dst) {
883            Ok(()) => Ok(()),
884            Err(err) if err.kind() == io::ErrorKind::AlreadyExists => {
885                // Create a symlink, using a temporary file to ensure atomicity.
886                let temp_dir = tempfile::tempdir_in(dst_parent)?;
887                let temp_file = temp_dir.path().join("link");
888                fs_err::os::unix::fs::symlink(&src, &temp_file)?;
889
890                // Move the symlink into the target location.
891                fs_err::rename(&temp_file, dst)?;
892
893                Ok(())
894            }
895            Err(err) => Err(err),
896        }
897    }
898
899    /// Resolve an archive link, returning the fully-resolved path.
900    ///
901    /// Returns an error if the link target does not exist.
902    #[cfg(unix)]
903    pub fn resolve_link(&self, path: impl AsRef<Path>) -> io::Result<PathBuf> {
904        path.as_ref().canonicalize()
905    }
906}
907
908/// An archive (unzipped wheel) that exists in the local cache.
909#[derive(Debug, Clone)]
910#[allow(unused)]
911struct Link {
912    /// The unique ID of the entry in the archive bucket.
913    id: ArchiveId,
914    /// The version of the archive bucket.
915    version: u8,
916}
917
918#[allow(unused)]
919impl Link {
920    /// Create a new [`Archive`] with the given ID and hashes.
921    fn new(id: ArchiveId) -> Self {
922        Self {
923            id,
924            version: ARCHIVE_VERSION,
925        }
926    }
927}
928
929impl Display for Link {
930    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
931        write!(f, "archive-v{}/{}", self.version, self.id)
932    }
933}
934
935impl FromStr for Link {
936    type Err = io::Error;
937
938    fn from_str(s: &str) -> Result<Self, Self::Err> {
939        let mut parts = s.splitn(2, '/');
940        let version = parts
941            .next()
942            .filter(|s| !s.is_empty())
943            .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "missing version"))?;
944        let id = parts
945            .next()
946            .filter(|s| !s.is_empty())
947            .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "missing ID"))?;
948
949        // Parse the archive version from `archive-v{version}/{id}`.
950        let version = version
951            .strip_prefix("archive-v")
952            .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "missing version prefix"))?;
953        let version = u8::from_str(version).map_err(|err| {
954            io::Error::new(
955                io::ErrorKind::InvalidData,
956                format!("failed to parse version: {err}"),
957            )
958        })?;
959
960        // Parse the ID from `archive-v{version}/{id}`.
961        let id = ArchiveId::from_str(id).map_err(|err| {
962            io::Error::new(
963                io::ErrorKind::InvalidData,
964                format!("failed to parse ID: {err}"),
965            )
966        })?;
967
968        Ok(Self { id, version })
969    }
970}
971
972pub trait CleanReporter: Send + Sync {
973    /// Called after one file or directory is removed.
974    fn on_clean(&self);
975
976    /// Called after all files and directories are removed.
977    fn on_complete(&self);
978}
979
980/// The different kinds of data in the cache are stored in different bucket, which in our case
981/// are subdirectories of the cache root.
982#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
983pub enum CacheBucket {
984    /// Wheels (excluding built wheels), alongside their metadata and cache policy.
985    ///
986    /// There are three kinds from cache entries: Wheel metadata and policy as `MsgPack` files, the
987    /// wheels themselves, and the unzipped wheel archives. If a wheel file is over an in-memory
988    /// size threshold, we first download the zip file into the cache, then unzip it into a
989    /// directory with the same name (exclusive of the `.whl` extension).
990    ///
991    /// Cache structure:
992    ///  * `wheel-metadata-v0/pypi/foo/{foo-1.0.0-py3-none-any.msgpack, foo-1.0.0-py3-none-any.whl}`
993    ///  * `wheel-metadata-v0/<digest(index-url)>/foo/{foo-1.0.0-py3-none-any.msgpack, foo-1.0.0-py3-none-any.whl}`
994    ///  * `wheel-metadata-v0/url/<digest(url)>/foo/{foo-1.0.0-py3-none-any.msgpack, foo-1.0.0-py3-none-any.whl}`
995    ///
996    /// See `uv_client::RegistryClient::wheel_metadata` for information on how wheel metadata
997    /// is fetched.
998    ///
999    /// # Example
1000    ///
1001    /// Consider the following `requirements.in`:
1002    /// ```text
1003    /// # pypi wheel
1004    /// pandas
1005    /// # url wheel
1006    /// flask @ https://files.pythonhosted.org/packages/36/42/015c23096649b908c809c69388a805a571a3bea44362fe87e33fc3afa01f/flask-3.0.0-py3-none-any.whl
1007    /// ```
1008    ///
1009    /// When we run `pip compile`, it will only fetch and cache the metadata (and cache policy), it
1010    /// doesn't need the actual wheels yet:
1011    /// ```text
1012    /// wheel-v0
1013    /// ├── pypi
1014    /// │   ...
1015    /// │   ├── pandas
1016    /// │   │   └── pandas-2.1.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.msgpack
1017    /// │   ...
1018    /// └── url
1019    ///     └── 4b8be67c801a7ecb
1020    ///         └── flask
1021    ///             └── flask-3.0.0-py3-none-any.msgpack
1022    /// ```
1023    ///
1024    /// We get the following `requirement.txt` from `pip compile`:
1025    ///
1026    /// ```text
1027    /// [...]
1028    /// flask @ https://files.pythonhosted.org/packages/36/42/015c23096649b908c809c69388a805a571a3bea44362fe87e33fc3afa01f/flask-3.0.0-py3-none-any.whl
1029    /// [...]
1030    /// pandas==2.1.3
1031    /// [...]
1032    /// ```
1033    ///
1034    /// If we run `pip sync` on `requirements.txt` on a different machine, it also fetches the
1035    /// wheels:
1036    ///
1037    /// TODO(konstin): This is still wrong, we need to store the cache policy too!
1038    /// ```text
1039    /// wheel-v0
1040    /// ├── pypi
1041    /// │   ...
1042    /// │   ├── pandas
1043    /// │   │   ├── pandas-2.1.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
1044    /// │   │   ├── pandas-2.1.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64
1045    /// │   ...
1046    /// └── url
1047    ///     └── 4b8be67c801a7ecb
1048    ///         └── flask
1049    ///             └── flask-3.0.0-py3-none-any.whl
1050    ///                 ├── flask
1051    ///                 │   └── ...
1052    ///                 └── flask-3.0.0.dist-info
1053    ///                     └── ...
1054    /// ```
1055    ///
1056    /// If we run first `pip compile` and then `pip sync` on the same machine, we get both:
1057    ///
1058    /// ```text
1059    /// wheels-v0
1060    /// ├── pypi
1061    /// │   ├── ...
1062    /// │   ├── pandas
1063    /// │   │   ├── pandas-2.1.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.msgpack
1064    /// │   │   ├── pandas-2.1.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
1065    /// │   │   └── pandas-2.1.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64
1066    /// │   │       ├── pandas
1067    /// │   │       │   ├── ...
1068    /// │   │       ├── pandas-2.1.3.dist-info
1069    /// │   │       │   ├── ...
1070    /// │   │       └── pandas.libs
1071    /// │   ├── ...
1072    /// └── url
1073    ///     └── 4b8be67c801a7ecb
1074    ///         └── flask
1075    ///             ├── flask-3.0.0-py3-none-any.msgpack
1076    ///             ├── flask-3.0.0-py3-none-any.msgpack
1077    ///             └── flask-3.0.0-py3-none-any
1078    ///                 ├── flask
1079    ///                 │   └── ...
1080    ///                 └── flask-3.0.0.dist-info
1081    ///                     └── ...
1082    Wheels,
1083    /// Source distributions, wheels built from source distributions, their extracted metadata, and the
1084    /// cache policy of the source distribution.
1085    ///
1086    /// The structure is similar of that of the `Wheel` bucket, except we have an additional layer
1087    /// for the source distribution filename and the metadata is at the source distribution-level,
1088    /// not at the wheel level.
1089    ///
1090    /// TODO(konstin): The cache policy should be on the source distribution level, the metadata we
1091    /// can put next to the wheels as in the `Wheels` bucket.
1092    ///
1093    /// The unzipped source distribution is stored in a directory matching the source distribution
1094    /// archive name.
1095    ///
1096    /// Source distributions are built into zipped wheel files (as PEP 517 specifies) and unzipped
1097    /// lazily before installing. So when resolving, we only build the wheel and store the archive
1098    /// file in the cache, when installing, we unpack it under the same name (exclusive of the
1099    /// `.whl` extension). You may find a mix of wheel archive zip files and unzipped wheel
1100    /// directories in the cache.
1101    ///
1102    /// Cache structure:
1103    ///  * `built-wheels-v0/pypi/foo/34a17436ed1e9669/{manifest.msgpack, metadata.msgpack, foo-1.0.0.zip, foo-1.0.0-py3-none-any.whl, ...other wheels}`
1104    ///  * `built-wheels-v0/<digest(index-url)>/foo/foo-1.0.0.zip/{manifest.msgpack, metadata.msgpack, foo-1.0.0-py3-none-any.whl, ...other wheels}`
1105    ///  * `built-wheels-v0/url/<digest(url)>/foo/foo-1.0.0.zip/{manifest.msgpack, metadata.msgpack, foo-1.0.0-py3-none-any.whl, ...other wheels}`
1106    ///  * `built-wheels-v0/git/<digest(url)>/<git sha>/foo/foo-1.0.0.zip/{metadata.msgpack, foo-1.0.0-py3-none-any.whl, ...other wheels}`
1107    ///
1108    /// But the url filename does not need to be a valid source dist filename
1109    /// (<https://github.com/search?q=path%3A**%2Frequirements.txt+master.zip&type=code>),
1110    /// so it could also be the following and we have to take any string as filename:
1111    ///  * `built-wheels-v0/url/<sha256(url)>/master.zip/metadata.msgpack`
1112    ///
1113    /// # Example
1114    ///
1115    /// The following requirements:
1116    /// ```text
1117    /// # git source dist
1118    /// pydantic-extra-types @ git+https://github.com/pydantic/pydantic-extra-types.git
1119    /// # pypi source dist
1120    /// django_allauth==0.51.0
1121    /// # url source dist
1122    /// werkzeug @ https://files.pythonhosted.org/packages/0d/cc/ff1904eb5eb4b455e442834dabf9427331ac0fa02853bf83db817a7dd53d/werkzeug-3.0.1.tar.gz
1123    /// ```
1124    ///
1125    /// ...may be cached as:
1126    /// ```text
1127    /// built-wheels-v4/
1128    /// ├── git
1129    /// │   └── 2122faf3e081fb7a
1130    /// │       └── 7a2d650a4a7b4d04
1131    /// │           ├── metadata.msgpack
1132    /// │           └── pydantic_extra_types-2.9.0-py3-none-any.whl
1133    /// ├── pypi
1134    /// │   └── django-allauth
1135    /// │       └── 0.51.0
1136    /// │           ├── 0gH-_fwv8tdJ7JwwjJsUc
1137    /// │           │   ├── django-allauth-0.51.0.tar.gz
1138    /// │           │   │   └── [UNZIPPED CONTENTS]
1139    /// │           │   ├── django_allauth-0.51.0-py3-none-any.whl
1140    /// │           │   └── metadata.msgpack
1141    /// │           └── revision.http
1142    /// └── url
1143    ///     └── 6781bd6440ae72c2
1144    ///         ├── APYY01rbIfpAo_ij9sCY6
1145    ///         │   ├── metadata.msgpack
1146    ///         │   ├── werkzeug-3.0.1-py3-none-any.whl
1147    ///         │   └── werkzeug-3.0.1.tar.gz
1148    ///         │       └── [UNZIPPED CONTENTS]
1149    ///         └── revision.http
1150    /// ```
1151    ///
1152    /// Structurally, the `manifest.msgpack` is empty, and only contains the caching information
1153    /// needed to invalidate the cache. The `metadata.msgpack` contains the metadata of the source
1154    /// distribution.
1155    SourceDistributions,
1156    /// Flat index responses, a format very similar to the simple metadata API.
1157    ///
1158    /// Cache structure:
1159    ///  * `flat-index-v0/index/<digest(flat_index_url)>.msgpack`
1160    ///
1161    /// The response is stored as `Vec<File>`.
1162    FlatIndex,
1163    /// Git repositories.
1164    Git,
1165    /// Information about an interpreter at a path.
1166    ///
1167    /// To avoid caching pyenv shims, bash scripts which may redirect to a new python version
1168    /// without the shim itself changing, we only cache when the path equals `sys.executable`, i.e.
1169    /// the path we're running is the python executable itself and not a shim.
1170    ///
1171    /// Cache structure: `interpreter-v0/<digest(path)>.msgpack`
1172    ///
1173    /// # Example
1174    ///
1175    /// The contents of each of the `MsgPack` files has a timestamp field in unix time, the [PEP 508]
1176    /// markers and some information from the `sys`/`sysconfig` modules.
1177    ///
1178    /// ```json
1179    /// {
1180    ///   "timestamp": 1698047994491,
1181    ///   "data": {
1182    ///     "markers": {
1183    ///       "implementation_name": "cpython",
1184    ///       "implementation_version": "3.12.0",
1185    ///       "os_name": "posix",
1186    ///       "platform_machine": "x86_64",
1187    ///       "platform_python_implementation": "CPython",
1188    ///       "platform_release": "6.5.0-13-generic",
1189    ///       "platform_system": "Linux",
1190    ///       "platform_version": "#13-Ubuntu SMP PREEMPT_DYNAMIC Fri Nov  3 12:16:05 UTC 2023",
1191    ///       "python_full_version": "3.12.0",
1192    ///       "python_version": "3.12",
1193    ///       "sys_platform": "linux"
1194    ///     },
1195    ///     "base_exec_prefix": "/home/ferris/.pyenv/versions/3.12.0",
1196    ///     "base_prefix": "/home/ferris/.pyenv/versions/3.12.0",
1197    ///     "sys_executable": "/home/ferris/projects/uv/.venv/bin/python"
1198    ///   }
1199    /// }
1200    /// ```
1201    ///
1202    /// [PEP 508]: https://peps.python.org/pep-0508/#environment-markers
1203    Interpreter,
1204    /// Index responses through the simple metadata API.
1205    ///
1206    /// Cache structure:
1207    ///  * `simple-v0/pypi/<package_name>.rkyv`
1208    ///  * `simple-v0/<digest(index_url)>/<package_name>.rkyv`
1209    ///
1210    /// The response is parsed into `uv_client::SimpleDetailMetadata` before storage.
1211    Simple,
1212    /// A cache of unzipped wheels, stored as directories. This is used internally within the cache.
1213    /// When other buckets need to store directories, they should persist them to
1214    /// [`CacheBucket::Archive`], and then symlink them into the appropriate bucket. This ensures
1215    /// that cache entries can be atomically replaced and removed, as storing directories in the
1216    /// other buckets directly would make atomic operations impossible.
1217    Archive,
1218    /// Ephemeral virtual environments used to execute PEP 517 builds and other operations.
1219    Builds,
1220    /// Reusable virtual environments for Python tools and projects.
1221    Environments,
1222    /// Cached Python downloads
1223    Python,
1224    /// Downloaded tool binaries (e.g., Ruff).
1225    Binaries,
1226    /// Cached vulnerability data from [OSV](https://osv.dev/).
1227    ///
1228    /// Cache structure:
1229    ///  * `osv-v0/vulnerability/<vuln_id>.msgpack` — cached full vulnerability records
1230    Osv,
1231}
1232
1233impl CacheBucket {
1234    fn to_str(self) -> &'static str {
1235        match self {
1236            // Note that when bumping this, you'll also need to bump it
1237            // in `crates/uv/tests/build/cache_prune.rs`.
1238            Self::SourceDistributions => "sdists-v9",
1239            // Note that when bumping this, you'll also need to bump it
1240            // in `crates/uv/tests/lock/lock.rs`.
1241            Self::FlatIndex => "flat-index-v4",
1242            Self::Git => "git-v0",
1243            Self::Interpreter => "interpreter-v4",
1244            // Note that when bumping this, you'll also need to bump it
1245            // in `crates/uv/tests/build/cache_clean.rs`.
1246            Self::Simple => "simple-v24",
1247            // Note that when bumping this, you'll also need to bump it
1248            // in `crates/uv/tests/build/cache_prune.rs`.
1249            Self::Wheels => "wheels-v6",
1250            // Note that when bumping this, you'll also need to bump
1251            // `ARCHIVE_VERSION` in `crates/uv-cache/src/lib.rs`.
1252            Self::Archive => "archive-v0",
1253            Self::Builds => "builds-v0",
1254            Self::Environments => "environments-v2",
1255            Self::Python => "python-v0",
1256            Self::Binaries => "binaries-v0",
1257            Self::Osv => "osv-v0",
1258        }
1259    }
1260
1261    /// Remove a package from the cache bucket.
1262    ///
1263    /// Returns the number of entries removed from the cache.
1264    fn remove(self, cache: &Cache, name: &PackageName) -> Result<Removal, io::Error> {
1265        /// Returns `true` if the [`Path`] represents a built wheel for the given package.
1266        fn is_match(path: &Path, name: &PackageName) -> bool {
1267            let Ok(metadata) = fs_err::read(path.join("metadata.msgpack")) else {
1268                return false;
1269            };
1270            let Ok(metadata) = rmp_serde::from_slice::<ResolutionMetadata>(&metadata) else {
1271                return false;
1272            };
1273            metadata.name == *name
1274        }
1275
1276        let mut summary = cache.removal();
1277        match self {
1278            Self::Wheels => {
1279                // For `pypi` wheels, we expect a directory per package (indexed by name).
1280                let root = cache.bucket(self).join(WheelCacheKind::Pypi);
1281                summary += cache.remove_path(root.join(name.to_string()))?;
1282
1283                // For alternate indices, we expect a directory for every index (under an `index`
1284                // subdirectory), followed by a directory per package (indexed by name).
1285                let root = cache.bucket(self).join(WheelCacheKind::Index);
1286                for directory in directories(root)? {
1287                    summary += cache.remove_path(directory.join(name.to_string()))?;
1288                }
1289
1290                // For direct URLs, we expect a directory for every URL, followed by a
1291                // directory per package (indexed by name).
1292                let root = cache.bucket(self).join(WheelCacheKind::Url);
1293                for directory in directories(root)? {
1294                    summary += cache.remove_path(directory.join(name.to_string()))?;
1295                }
1296            }
1297            Self::SourceDistributions => {
1298                // For `pypi` wheels, we expect a directory per package (indexed by name).
1299                let root = cache.bucket(self).join(WheelCacheKind::Pypi);
1300                summary += cache.remove_path(root.join(name.to_string()))?;
1301
1302                // For alternate indices, we expect a directory for every index (under an `index`
1303                // subdirectory), followed by a directory per package (indexed by name).
1304                let root = cache.bucket(self).join(WheelCacheKind::Index);
1305                for directory in directories(root)? {
1306                    summary += cache.remove_path(directory.join(name.to_string()))?;
1307                }
1308
1309                // For direct URLs, we expect a directory for every URL, followed by a
1310                // directory per version. To determine whether the URL is relevant, we need to
1311                // search for a wheel matching the package name.
1312                let root = cache.bucket(self).join(WheelCacheKind::Url);
1313                for url in directories(root)? {
1314                    if directories(&url)?.any(|version| is_match(&version, name)) {
1315                        summary += cache.remove_path(url)?;
1316                    }
1317                }
1318
1319                // For local dependencies, we expect a directory for every path, followed by a
1320                // directory per version. To determine whether the path is relevant, we need to
1321                // search for a wheel matching the package name.
1322                let root = cache.bucket(self).join(WheelCacheKind::Path);
1323                for path in directories(root)? {
1324                    if directories(&path)?.any(|version| is_match(&version, name)) {
1325                        summary += cache.remove_path(path)?;
1326                    }
1327                }
1328
1329                // For Git dependencies, we expect a directory for every repository, followed by a
1330                // directory for every SHA. To determine whether the SHA is relevant, we need to
1331                // search for a wheel matching the package name.
1332                let root = cache.bucket(self).join(WheelCacheKind::Git);
1333                for repository in directories(root)? {
1334                    for sha in directories(repository)? {
1335                        if is_match(&sha, name) {
1336                            summary += cache.remove_path(sha)?;
1337                        }
1338                    }
1339                }
1340            }
1341            Self::Simple => {
1342                // For `pypi` wheels, we expect a rkyv file per package, indexed by name.
1343                let root = cache.bucket(self).join(WheelCacheKind::Pypi);
1344                summary += cache.remove_path(root.join(format!("{name}.rkyv")))?;
1345
1346                // For alternate indices, we expect a directory for every index (under an `index`
1347                // subdirectory), followed by a directory per package (indexed by name).
1348                let root = cache.bucket(self).join(WheelCacheKind::Index);
1349                for directory in directories(root)? {
1350                    summary += cache.remove_path(directory.join(format!("{name}.rkyv")))?;
1351                }
1352            }
1353            Self::FlatIndex => {
1354                // We can't know if the flat index includes a package, so we just remove the entire
1355                // cache entry.
1356                let root = cache.bucket(self);
1357                summary += cache.remove_path(root)?;
1358            }
1359            Self::Git
1360            | Self::Interpreter
1361            | Self::Archive
1362            | Self::Builds
1363            | Self::Environments
1364            | Self::Python
1365            | Self::Binaries
1366            | Self::Osv => {
1367                // Nothing to do.
1368            }
1369        }
1370        Ok(summary)
1371    }
1372
1373    /// Return an iterator over all cache buckets.
1374    fn iter() -> impl Iterator<Item = Self> {
1375        [
1376            Self::Wheels,
1377            Self::SourceDistributions,
1378            Self::FlatIndex,
1379            Self::Git,
1380            Self::Interpreter,
1381            Self::Simple,
1382            Self::Archive,
1383            Self::Builds,
1384            Self::Environments,
1385            Self::Python,
1386            Self::Binaries,
1387            Self::Osv,
1388        ]
1389        .iter()
1390        .copied()
1391    }
1392}
1393
1394impl Display for CacheBucket {
1395    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
1396        f.write_str(self.to_str())
1397    }
1398}
1399
1400#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1401pub enum Freshness {
1402    /// The cache entry is fresh according to the [`Refresh`] policy.
1403    Fresh,
1404    /// The cache entry is stale according to the [`Refresh`] policy.
1405    Stale,
1406    /// The cache entry does not exist.
1407    Missing,
1408}
1409
1410impl Freshness {
1411    pub const fn is_fresh(self) -> bool {
1412        matches!(self, Self::Fresh)
1413    }
1414}
1415
1416/// A refresh policy for cache entries.
1417#[derive(Debug, Clone)]
1418pub enum Refresh {
1419    /// Don't refresh any entries.
1420    None(Timestamp),
1421    /// Refresh entries linked to the given packages, if created before the given timestamp.
1422    Packages(Vec<PackageName>, Vec<Box<Path>>, Timestamp),
1423    /// Refresh all entries created before the given timestamp.
1424    All(Timestamp),
1425}
1426
1427impl Refresh {
1428    /// Determine the refresh strategy to use based on the command-line arguments.
1429    pub fn from_args(refresh: Option<bool>, refresh_package: Vec<PackageName>) -> Self {
1430        let timestamp = Timestamp::now();
1431        match refresh {
1432            Some(true) => Self::All(timestamp),
1433            Some(false) => Self::None(timestamp),
1434            None => {
1435                if refresh_package.is_empty() {
1436                    Self::None(timestamp)
1437                } else {
1438                    Self::Packages(refresh_package, vec![], timestamp)
1439                }
1440            }
1441        }
1442    }
1443
1444    /// Combine two [`Refresh`] policies, taking the "max" of the two policies.
1445    #[must_use]
1446    pub fn combine(self, other: Self) -> Self {
1447        match (self, other) {
1448            // If the policy is `None`, return the existing refresh policy.
1449            // Take the `max` of the two timestamps.
1450            (Self::None(t1), Self::None(t2)) => Self::None(t1.max(t2)),
1451            (Self::None(t1), Self::All(t2)) => Self::All(t1.max(t2)),
1452            (Self::None(t1), Self::Packages(packages, paths, t2)) => {
1453                Self::Packages(packages, paths, t1.max(t2))
1454            }
1455
1456            // If the policy is `All`, refresh all packages.
1457            (Self::All(t1), Self::None(t2) | Self::All(t2) | Self::Packages(.., t2)) => {
1458                Self::All(t1.max(t2))
1459            }
1460
1461            // If the policy is `Packages`, take the "max" of the two policies.
1462            (Self::Packages(packages, paths, t1), Self::None(t2)) => {
1463                Self::Packages(packages, paths, t1.max(t2))
1464            }
1465            (Self::Packages(.., t1), Self::All(t2)) => Self::All(t1.max(t2)),
1466            (Self::Packages(packages1, paths1, t1), Self::Packages(packages2, paths2, t2)) => {
1467                Self::Packages(
1468                    packages1.into_iter().chain(packages2).collect(),
1469                    paths1.into_iter().chain(paths2).collect(),
1470                    t1.max(t2),
1471                )
1472            }
1473        }
1474    }
1475}
1476
1477#[cfg(test)]
1478mod tests {
1479    use std::str::FromStr;
1480
1481    use crate::ArchiveId;
1482
1483    use super::Link;
1484
1485    #[test]
1486    fn test_link_round_trip() {
1487        let id = ArchiveId::new();
1488        let link = Link::new(id);
1489        let s = link.to_string();
1490        let parsed = Link::from_str(&s).unwrap();
1491        assert_eq!(link.id, parsed.id);
1492        assert_eq!(link.version, parsed.version);
1493    }
1494
1495    #[test]
1496    fn test_link_deserialize() {
1497        assert!(Link::from_str("archive-v0/foo").is_ok());
1498        assert!(Link::from_str("archive/foo").is_err());
1499        assert!(Link::from_str("v1/foo").is_err());
1500        assert!(Link::from_str("archive-v0/").is_err());
1501    }
1502
1503    #[test]
1504    #[cfg(unix)]
1505    fn prune_does_not_follow_environment_symlinks() {
1506        use super::{Cache, CacheBucket};
1507
1508        let cache_root = tempfile::tempdir().unwrap();
1509        let victim_root = tempfile::tempdir().unwrap();
1510        let environments = cache_root.path().join(CacheBucket::Environments.to_str());
1511        let victim_dir = victim_root.path().join("victim-dir");
1512
1513        fs_err::create_dir_all(&environments).unwrap();
1514        fs_err::create_dir_all(&victim_dir).unwrap();
1515        fs_err::write(victim_dir.join("payload.txt"), "payload").unwrap();
1516        fs_err::os::unix::fs::symlink(&victim_dir, environments.join("escape")).unwrap();
1517
1518        let summary = Cache::from_path(cache_root.path()).prune(false).unwrap();
1519
1520        assert_eq!(summary.num_files, 1);
1521        assert_eq!(summary.num_dirs, 0);
1522        assert!(victim_dir.is_dir());
1523        assert!(victim_dir.join("payload.txt").is_file());
1524        assert!(fs_err::symlink_metadata(environments.join("escape")).is_err());
1525    }
1526
1527    #[test]
1528    #[cfg(unix)]
1529    fn prune_ci_does_not_follow_wheel_symlinks() {
1530        use super::{Cache, CacheBucket};
1531
1532        let cache_root = tempfile::tempdir().unwrap();
1533        let victim_root = tempfile::tempdir().unwrap();
1534        let wheels = cache_root.path().join(CacheBucket::Wheels.to_str());
1535        let source_distributions = cache_root
1536            .path()
1537            .join(CacheBucket::SourceDistributions.to_str());
1538        let victim_dir = victim_root.path().join("victim-dir");
1539        let symlink = wheels.join("escape");
1540
1541        fs_err::create_dir_all(&wheels).unwrap();
1542        fs_err::create_dir_all(&source_distributions).unwrap();
1543        fs_err::create_dir_all(&victim_dir).unwrap();
1544        fs_err::write(victim_dir.join("payload.txt"), "payload").unwrap();
1545        fs_err::os::unix::fs::symlink(&victim_dir, &symlink).unwrap();
1546
1547        let summary = Cache::from_path(cache_root.path()).prune(true).unwrap();
1548
1549        assert_eq!(summary.num_files, 1);
1550        assert_eq!(summary.num_dirs, 0);
1551        assert!(victim_dir.is_dir());
1552        assert!(victim_dir.join("payload.txt").is_file());
1553        assert!(fs_err::symlink_metadata(symlink).is_err());
1554    }
1555
1556    #[test]
1557    #[cfg(unix)]
1558    fn prune_does_not_follow_archive_symlinks() {
1559        use super::{Cache, CacheBucket};
1560
1561        let cache_root = tempfile::tempdir().unwrap();
1562        let victim_root = tempfile::tempdir().unwrap();
1563        let archives = cache_root.path().join(CacheBucket::Archive.to_str());
1564        let victim_dir = victim_root.path().join("victim-dir");
1565        let symlink = archives.join("escape");
1566
1567        fs_err::create_dir_all(&archives).unwrap();
1568        fs_err::create_dir_all(&victim_dir).unwrap();
1569        fs_err::write(victim_dir.join("payload.txt"), "payload").unwrap();
1570        fs_err::os::unix::fs::symlink(&victim_dir, &symlink).unwrap();
1571
1572        let summary = Cache::from_path(cache_root.path()).prune(false).unwrap();
1573
1574        assert_eq!(summary.num_files, 1);
1575        assert_eq!(summary.num_dirs, 0);
1576        assert!(victim_dir.is_dir());
1577        assert!(victim_dir.join("payload.txt").is_file());
1578        assert!(fs_err::symlink_metadata(symlink).is_err());
1579    }
1580}