Skip to content

Commit dca33f5

Browse files
authored
Fall back to logical cache accounting on unsupported filesystems (#21133)
## Summary #20925 introduces more accurate space accounting under preview, but this will fail on unsupported filesystems. We already have code which works for these filesystems so this PR introduces a special error handling case for this situation and reverts to the old method. ## Test Plan New test cases added.
1 parent 3a76e49 commit dca33f5

5 files changed

Lines changed: 103 additions & 20 deletions

File tree

‎crates/uv-cache/src/removal.rs‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::io;
66
use std::path::Path;
77

88
use tracing::debug;
9+
use uv_fs::PhysicalSpaceError;
910

1011
use crate::CleanReporter;
1112

@@ -93,7 +94,15 @@ impl Removal {
9394
Ok(physical) => {
9495
self.physical_bytes = Some(physical_bytes.saturating_add(physical));
9596
}
96-
Err(error) => {
97+
Err(PhysicalSpaceError::UnsupportedFilesystem) => {
98+
debug!(
99+
"Physical space accounting is unsupported for {}; falling back to logical space",
100+
path.display()
101+
);
102+
self.physical_bytes = None;
103+
self.physical_bytes_incomplete = false;
104+
}
105+
Err(PhysicalSpaceError::UnmeasurableFile(error)) => {
97106
debug!(
98107
"Failed to measure physical space for {}: {error}",
99108
path.display()
@@ -233,7 +242,8 @@ impl std::ops::AddAssign for Removal {
233242
.physical_bytes
234243
.zip(other.physical_bytes)
235244
.map(|(left, right)| left.saturating_add(right));
236-
self.physical_bytes_incomplete |= other.physical_bytes_incomplete;
245+
self.physical_bytes_incomplete = self.physical_bytes.is_some()
246+
&& (self.physical_bytes_incomplete || other.physical_bytes_incomplete);
237247
}
238248
}
239249

‎crates/uv-fs/src/lib.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use tracing::{debug, warn};
1212
pub use crate::locked_file::*;
1313
pub use crate::path::*;
1414
pub use crate::read::ValidatedReader;
15-
pub use crate::space::{physical_space, supports_physical_space};
15+
pub use crate::space::{PhysicalSpaceError, physical_space, supports_physical_space};
1616

1717
pub mod cachedir;
1818
pub mod link;

‎crates/uv-fs/src/space.rs‎

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@ use linux_raw_sys::ioctl::{
1313
FIEMAP_EXTENT_DATA_INLINE, FIEMAP_EXTENT_DELALLOC, FIEMAP_EXTENT_ENCODED, FIEMAP_EXTENT_LAST,
1414
FIEMAP_EXTENT_NOT_ALIGNED, FIEMAP_EXTENT_SHARED, FIEMAP_EXTENT_UNKNOWN, FS_IOC_FIEMAP,
1515
};
16+
#[cfg(any(target_os = "linux", target_os = "macos", target_os = "ios"))]
17+
use rustix::io::Errno;
18+
use thiserror::Error;
19+
20+
/// An error encountered while measuring a file's physical storage.
21+
#[derive(Debug, Error)]
22+
pub enum PhysicalSpaceError {
23+
/// The filesystem cannot report exclusively owned physical storage.
24+
#[error("the filesystem does not support physical space accounting")]
25+
UnsupportedFilesystem,
26+
/// The file's physical storage could not be measured.
27+
#[error(transparent)]
28+
UnmeasurableFile(#[from] io::Error),
29+
}
1630

1731
/// Return whether the current platform can identify individual files' physical storage.
1832
pub const fn supports_physical_space() -> bool {
@@ -27,7 +41,10 @@ pub const fn supports_physical_space() -> bool {
2741
///
2842
/// The result excludes data retained by another hardlink, copy-on-write clone, or snapshot.
2943
/// Filesystem metadata is not included.
30-
pub fn physical_space(path: &Path, metadata: &std::fs::Metadata) -> io::Result<u64> {
44+
pub fn physical_space(
45+
path: &Path,
46+
metadata: &std::fs::Metadata,
47+
) -> Result<u64, PhysicalSpaceError> {
3148
if !metadata.is_file() {
3249
#[cfg(unix)]
3350
{
@@ -58,16 +75,13 @@ pub fn physical_space(path: &Path, metadata: &std::fs::Metadata) -> io::Result<u
5875
#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "ios")))]
5976
{
6077
let _ = path;
61-
Err(io::Error::new(
62-
io::ErrorKind::Unsupported,
63-
"per-file space measurement is unsupported on this platform",
64-
))
78+
Err(PhysicalSpaceError::UnsupportedFilesystem)
6579
}
6680
}
6781

6882
#[cfg(any(target_os = "macos", target_os = "ios"))]
6983
#[expect(unsafe_code)]
70-
fn apple_physical_space(path: &Path) -> io::Result<u64> {
84+
fn apple_physical_space(path: &Path) -> Result<u64, PhysicalSpaceError> {
7185
let path = CString::new(path.as_os_str().as_bytes())
7286
.map_err(|error| io::Error::new(io::ErrorKind::InvalidInput, error))?;
7387
let mut attributes = libc::attrlist {
@@ -94,25 +108,26 @@ fn apple_physical_space(path: &Path) -> io::Result<u64> {
94108
)
95109
};
96110
if result != 0 {
97-
return Err(io::Error::last_os_error());
111+
let error = io::Error::last_os_error();
112+
if Errno::from_io_error(&error) == Some(Errno::NOTSUP) {
113+
return Err(PhysicalSpaceError::UnsupportedFilesystem);
114+
}
115+
return Err(error.into());
98116
}
99117

100118
let returned_fork_attributes =
101119
u32::from_ne_bytes(response[20..24].try_into().map_err(io::Error::other)?);
102120
if returned_fork_attributes & libc::ATTR_CMNEXT_PRIVATESIZE == 0 {
103-
return Err(io::Error::new(
104-
io::ErrorKind::Unsupported,
105-
"the filesystem does not report private file sizes",
106-
));
121+
return Err(PhysicalSpaceError::UnsupportedFilesystem);
107122
}
108123

109124
let private_size = i64::from_ne_bytes(response[24..32].try_into().map_err(io::Error::other)?);
110-
u64::try_from(private_size).map_err(io::Error::other)
125+
Ok(u64::try_from(private_size).map_err(io::Error::other)?)
111126
}
112127

113128
#[cfg(target_os = "linux")]
114129
#[expect(unsafe_code)]
115-
fn linux_physical_space(path: &Path) -> io::Result<u64> {
130+
fn linux_physical_space(path: &Path) -> Result<u64, PhysicalSpaceError> {
116131
const MAX_EXTENTS: usize = 32;
117132

118133
#[derive(Default)]
@@ -167,16 +182,22 @@ fn linux_physical_space(path: &Path) -> io::Result<u64> {
167182
rustix::ioctl::Updater::<{ FS_IOC_FIEMAP as rustix::ioctl::Opcode }, _>::new(
168183
&mut request,
169184
),
170-
)?;
185+
)
171186
}
187+
.map_err(|error| match error {
188+
// Linux returns `ENOTTY` when the ioctl is unsupported for this file.
189+
Errno::NOTSUP | Errno::NOTTY => PhysicalSpaceError::UnsupportedFilesystem,
190+
error => PhysicalSpaceError::UnmeasurableFile(error.into()),
191+
})?;
172192

173193
let mapped_extents =
174194
usize::try_from(request.header.mapped_extents).map_err(io::Error::other)?;
175195
if mapped_extents > request.extents.len() {
176196
return Err(io::Error::new(
177197
io::ErrorKind::InvalidData,
178198
"the filesystem returned more extents than requested",
179-
));
199+
)
200+
.into());
180201
}
181202
if mapped_extents == 0 {
182203
return Ok(physical);
@@ -197,7 +218,8 @@ fn linux_physical_space(path: &Path) -> io::Result<u64> {
197218
return Err(io::Error::new(
198219
io::ErrorKind::Unsupported,
199220
"the filesystem cannot report the physical size of an extent",
200-
));
221+
)
222+
.into());
201223
}
202224

203225
physical = physical.saturating_add(extent.length);
@@ -213,7 +235,8 @@ fn linux_physical_space(path: &Path) -> io::Result<u64> {
213235
return Err(io::Error::new(
214236
io::ErrorKind::InvalidData,
215237
"the filesystem returned a non-advancing extent",
216-
));
238+
)
239+
.into());
217240
}
218241
start = next;
219242
}

‎crates/uv/tests/build/cache_clean.rs‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,32 @@ fn clean_all_hardlinked_file() -> Result<()> {
9595
Ok(())
9696
}
9797

98+
/// `cache clean` should fall back to logical space on unsupported filesystems.
99+
#[cfg(unix)]
100+
#[test]
101+
fn clean_all_physical_space_unsupported_fs() -> Result<()> {
102+
let Some(context) = uv_test::test_context!("3.12")
103+
.with_filtered_counts()
104+
.with_cache_on_alt_fs()?
105+
else {
106+
return Ok(());
107+
};
108+
109+
context
110+
.cache_dir
111+
.child("cached.bin")
112+
.write_binary(&vec![42; 1024 * 1024])?;
113+
114+
uv_snapshot!(context.filters(), context.clean().arg("--preview-features").arg("cache-physical-space"), @"
115+
exit_code: 0 (success)
116+
----- stderr -----
117+
Clearing cache at: [ALT_FS]/[CACHE_DIR]/
118+
Removed [N] files (1.0MiB)
119+
");
120+
121+
Ok(())
122+
}
123+
98124
/// `cache clean` should report physical space for copy-on-write clones in preview mode.
99125
#[cfg(unix)]
100126
#[test]

‎crates/uv/tests/build/cache_prune.rs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,30 @@ fn prune_hardlinked_file() -> Result<()> {
7474
Ok(())
7575
}
7676

77+
/// `cache prune` should fall back to logical space on unsupported filesystems.
78+
#[cfg(unix)]
79+
#[test]
80+
fn prune_physical_space_unsupported_fs() -> Result<()> {
81+
let Some(context) = uv_test::test_context!("3.12").with_cache_on_alt_fs()? else {
82+
return Ok(());
83+
};
84+
85+
let stale = context.cache_dir.child("stale-v0");
86+
stale.create_dir_all()?;
87+
stale
88+
.child("cached.bin")
89+
.write_binary(&vec![42; 1024 * 1024])?;
90+
91+
uv_snapshot!(context.filters(), context.prune().arg("--preview-features").arg("cache-physical-space"), @"
92+
exit_code: 0 (success)
93+
----- stderr -----
94+
Pruning cache at: [ALT_FS]/[CACHE_DIR]/
95+
Removed 1 file (1.0MiB)
96+
");
97+
98+
Ok(())
99+
}
100+
77101
/// `cache prune` should remove any stale top-level directories from the cache.
78102
#[test]
79103
fn prune_stale_directory() -> Result<()> {

0 commit comments

Comments
 (0)