@@ -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.
1832pub 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 }
0 commit comments