diff --git a/crates/uv-cache/src/lib.rs b/crates/uv-cache/src/lib.rs index bd3f99aa7f13..d1787327050c 100644 --- a/crates/uv-cache/src/lib.rs +++ b/crates/uv-cache/src/lib.rs @@ -371,7 +371,22 @@ impl Cache { } } - // Second, remove any unused archives (by searching for archives that are not symlinked). + // Second, remove any cached environments. These are never referenced by symlinks, so we can + // remove them directly. + match fs::read_dir(self.bucket(CacheBucket::Environments)) { + Ok(entries) => { + for entry in entries { + let entry = entry?; + let path = fs_err::canonicalize(entry.path())?; + debug!("Removing dangling cache entry: {}", path.display()); + summary += rm_rf(path)?; + } + } + Err(err) if err.kind() == io::ErrorKind::NotFound => (), + Err(err) => return Err(err), + } + + // Third, remove any unused archives (by searching for archives that are not symlinked). // TODO(charlie): Remove any unused source distributions. This requires introspecting the // cache contents, e.g., reading and deserializing the manifests. let mut references = FxHashSet::default(); @@ -382,7 +397,9 @@ impl Cache { for entry in walkdir::WalkDir::new(bucket) { let entry = entry?; if entry.file_type().is_symlink() { - references.insert(entry.path().canonicalize()?); + if let Ok(target) = fs_err::canonicalize(entry.path()) { + references.insert(target); + } } } } @@ -392,7 +409,7 @@ impl Cache { Ok(entries) => { for entry in entries { let entry = entry?; - let path = entry.path().canonicalize()?; + let path = fs_err::canonicalize(entry.path())?; if !references.contains(&path) { debug!("Removing dangling cache entry: {}", path.display()); summary += rm_rf(path)?; @@ -403,21 +420,6 @@ impl Cache { Err(err) => return Err(err), } - // Third, remove any cached environments. These are never referenced by symlinks, so we can - // remove them directly. - match fs::read_dir(self.bucket(CacheBucket::Environments)) { - Ok(entries) => { - for entry in entries { - let entry = entry?; - let path = entry.path().canonicalize()?; - debug!("Removing dangling cache entry: {}", path.display()); - summary += rm_rf(path)?; - } - } - Err(err) if err.kind() == io::ErrorKind::NotFound => (), - Err(err) => return Err(err), - } - Ok(summary) } } diff --git a/crates/uv-cache/src/removal.rs b/crates/uv-cache/src/removal.rs index 99c35b3b812e..c54ce81b5aef 100644 --- a/crates/uv-cache/src/removal.rs +++ b/crates/uv-cache/src/removal.rs @@ -103,7 +103,7 @@ fn set_readable(path: &Path) -> io::Result { #[cfg(unix)] { use std::os::unix::fs::PermissionsExt; - let mut perms = path.metadata()?.permissions(); + let mut perms = fs_err::metadata(path)?.permissions(); if perms.mode() & 0o500 == 0 { perms.set_mode(perms.mode() | 0o500); fs_err::set_permissions(path, perms)?; @@ -115,7 +115,7 @@ fn set_readable(path: &Path) -> io::Result { /// If the file is readonly, change the permissions to make it _not_ readonly. fn set_not_readonly(path: &Path) -> io::Result { - let mut perms = path.metadata()?.permissions(); + let mut perms = fs_err::metadata(path)?.permissions(); if !perms.readonly() { return Ok(false); }