Make cache clearing robust to directories without read permissions (#3524)

## Summary

If you run the script included in the linked issue, then `uv cache
clean`, we hit permissions errors on certain directories created by
`setuptools`. The permissions on those directories look like:

```
❯ sudo ls -l /Users/crmarsh/Library/Caches/uv/built-wheels-v3/pypi/opentracing/2.4.0/M-fYsaHAaQQvedmPMUl9D/opentracing-2.4.0.tar.gz/build/bdist.macosx-14.2-arm64/wheel/opentracing
Password:
total 0
drwxr-xr-x  3 crmarsh  staff  96 May 11 12:51 harness
```

This PR adds logic to make those directories readable by the current
user.

Closes https://github.com/astral-sh/uv/issues/3515.
This commit is contained in:
Charlie Marsh 2024-05-11 13:02:25 -04:00 committed by GitHub
parent df43dc9e70
commit 3b728c16cc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 92 additions and 29 deletions

View File

@ -35,36 +35,53 @@ impl Removal {
Err(err) => return Err(err),
};
let mut rm_file = |path: &Path, meta: Result<std::fs::Metadata, walkdir::Error>| {
if let Ok(meta) = meta {
self.total_bytes += meta.len();
}
remove_file(path)?;
Ok(())
};
if !metadata.is_dir() {
self.num_files += 1;
return rm_file(path, Ok(metadata));
// Remove the file.
self.total_bytes += metadata.len();
remove_file(path)?;
return Ok(());
}
for entry in walkdir::WalkDir::new(path).contents_first(true) {
// If we hit a directory that lacks read permissions, try to make it readable.
if let Err(ref err) = entry {
if err
.io_error()
.is_some_and(|err| err.kind() == io::ErrorKind::PermissionDenied)
{
if let Some(dir) = err.path() {
if set_readable(dir).unwrap_or(false) {
// Retry the operation; if we _just_ `self.rm_rf(dir)` and continue,
// `walkdir` may give us duplicate entries for the directory.
return self.rm_rf(path);
}
}
}
}
let entry = entry?;
if cfg!(windows) && entry.file_type().is_symlink() {
// In this branch, we try to handle junction removal.
self.num_files += 1;
fs_err::remove_dir(entry.path())?;
remove_dir(entry.path())?;
} else if entry.file_type().is_dir() {
self.num_dirs += 1;
// The contents should have been removed by now, but sometimes a race condition is
// hit where other files have been added by the OS. Fall back to `remove_dir_all`,
// which will remove the directory robustly across platforms.
fs_err::remove_dir_all(entry.path())?;
remove_dir_all(entry.path())?;
} else {
self.num_files += 1;
rm_file(entry.path(), entry.metadata())?;
// Remove the file.
if let Ok(meta) = entry.metadata() {
self.total_bytes += meta.len();
}
remove_file(entry.path())?;
}
}
@ -80,9 +97,22 @@ impl std::ops::AddAssign for Removal {
}
}
/// Like [`fs_err::remove_file`], but attempts to change the permissions to force the file to be
/// deleted (if it is readonly).
fn remove_file(path: &Path) -> io::Result<()> {
/// If the directory isn't readable by the current user, change the permissions to make it readable.
#[cfg_attr(windows, allow(unused_variables, clippy::unnecessary_wraps))]
fn set_readable(path: &Path) -> io::Result<bool> {
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let mut perms = path.metadata()?.permissions();
if perms.mode() & 0o500 == 0 {
perms.set_mode(perms.mode() | 0o500);
fs_err::set_permissions(path, perms)?;
return Ok(true);
}
}
Ok(false)
}
/// If the file is readonly, change the permissions to make it _not_ readonly.
fn set_not_readonly(path: &Path) -> io::Result<bool> {
let mut perms = path.metadata()?.permissions();
@ -99,6 +129,9 @@ fn remove_file(path: &Path) -> io::Result<()> {
Ok(true)
}
/// Like [`fs_err::remove_file`], but attempts to change the permissions to force the file to be
/// deleted (if it is readonly).
fn remove_file(path: &Path) -> io::Result<()> {
match fs_err::remove_file(path) {
Ok(()) => Ok(()),
Err(err)
@ -110,3 +143,33 @@ fn remove_file(path: &Path) -> io::Result<()> {
Err(err) => Err(err),
}
}
/// Like [`fs_err::remove_dir`], but attempts to change the permissions to force the directory to
/// be deleted (if it is readonly).
fn remove_dir(path: &Path) -> io::Result<()> {
match fs_err::remove_dir(path) {
Ok(()) => Ok(()),
Err(err)
if err.kind() == io::ErrorKind::PermissionDenied
&& set_readable(path).unwrap_or(false) =>
{
fs_err::remove_dir(path)
}
Err(err) => Err(err),
}
}
/// Like [`fs_err::remove_dir_all`], but attempts to change the permissions to force the directory
/// to be deleted (if it is readonly).
fn remove_dir_all(path: &Path) -> io::Result<()> {
match fs_err::remove_dir_all(path) {
Ok(()) => Ok(()),
Err(err)
if err.kind() == io::ErrorKind::PermissionDenied
&& set_readable(path).unwrap_or(false) =>
{
fs_err::remove_dir_all(path)
}
Err(err) => Err(err),
}
}