#[cfg(target_os = "macos")]
use std::os::unix::ffi::OsStringExt;
use std::{ffi::CStr, mem::MaybeUninit, os::unix::ffi::OsStrExt, path::Path};
use super::{ffi, last_errno, os_error};
#[cfg(target_os = "macos")]
use crate::disk::DiskKind;
use crate::{
Error, Pressure, Result,
disk::{Disk, DiskRaw, ListOptions, Space},
};
const PATH_MAX: usize = 1024;
fn c_path<'a>(path: &Path, buf: &'a mut [u8; PATH_MAX]) -> Result<&'a CStr> {
let bytes = path.as_os_str().as_bytes();
if bytes.contains(&0) {
return Err(Error::InvalidInput {
what: "disk path",
reason: "it contains a NUL byte",
});
}
if bytes.len() >= buf.len() {
return Err(Error::InvalidInput {
what: "disk path",
reason: "it is longer than the 1 023-byte path limit (MAXPATHLEN)",
});
}
buf[..bytes.len()].copy_from_slice(bytes);
buf[bytes.len()] = 0;
CStr::from_bytes_with_nul(&buf[..=bytes.len()]).map_err(|_| Error::InvalidInput {
what: "disk path",
reason: "it contains a NUL byte",
})
}
fn statfs(path: &CStr) -> core::result::Result<ffi::Statfs, i32> {
let mut buf = MaybeUninit::<ffi::Statfs>::zeroed();
let rc = unsafe { ffi::statfs(path.as_ptr(), buf.as_mut_ptr()) };
if rc != 0 {
return Err(last_errno());
}
Ok(unsafe { buf.assume_init() })
}
fn space_of(st: &ffi::Statfs) -> Space {
let block = u64::from(st.bsize);
Space::new(
st.blocks.saturating_mul(block),
st.bfree.saturating_mul(block),
st.bavail.saturating_mul(block),
)
}
pub(crate) fn disk_space(path: &Path) -> Result<Space> {
let mut buf = [0u8; PATH_MAX];
let path = c_path(path, &mut buf)?;
let st = statfs(path).map_err(|code| os_error("disk space", "statfs", code))?;
Ok(space_of(&st))
}
#[cfg(not(target_os = "macos"))]
pub(crate) fn disks(_options: &ListOptions) -> Result<Vec<Disk>> {
Err(super::unsupported("disk list"))
}
#[cfg(target_os = "macos")]
pub(crate) fn disks(options: &ListOptions) -> Result<Vec<Disk>> {
let table = mount_table(options.max_mounts)?;
let mut out = Vec::new();
for st in &table {
let Some(class) = classify(st, options) else {
continue;
};
let mount_point = std::path::PathBuf::from(std::ffi::OsString::from_vec(
super::c_array_bytes(&st.mntonname),
));
let space = Some(fresh_space(&mount_point).unwrap_or_else(|| space_of(st)));
out.push(Disk {
device: crate::parse::lossy(&super::c_array_bytes(&st.mntfromname), PATH_MAX),
mount_point,
label: None,
filesystem: crate::parse::lossy(&super::c_array_bytes(&st.fstypename), 16),
kind: class,
space,
read_only: st.flags & ffi::MNT_RDONLY != 0,
removable: st.flags & ffi::MNT_REMOVABLE != 0,
});
}
Ok(out)
}
#[cfg(target_os = "macos")]
fn fresh_space(mount_point: &Path) -> Option<Space> {
let mut buf = [0u8; PATH_MAX];
let path = c_path(mount_point, &mut buf).ok()?;
statfs(path).ok().map(|st| space_of(&st))
}
#[cfg(target_os = "macos")]
fn classify(st: &ffi::Statfs, options: &ListOptions) -> Option<DiskKind> {
let fstype = super::c_array_bytes(&st.fstypename);
let pseudo = matches!(fstype.as_slice(), b"devfs" | b"autofs");
if pseudo {
return options.include_pseudo.then_some(DiskKind::Virtual);
}
let network = st.flags & ffi::MNT_LOCAL == 0
|| matches!(
fstype.as_slice(),
b"nfs" | b"smbfs" | b"afpfs" | b"webdav" | b"cifs" | b"ftp"
);
if network {
return options.include_network.then_some(DiskKind::Network);
}
if st.flags & ffi::MNT_DONTBROWSE != 0 && !options.include_duplicates {
return None;
}
Some(if st.flags & ffi::MNT_REMOVABLE != 0 {
DiskKind::Removable
} else {
DiskKind::Unknown
})
}
#[cfg(target_os = "macos")]
fn mount_table(max: usize) -> Result<Vec<ffi::Statfs>> {
const WHAT: &str = "disk list";
let too_large = || Error::TooLarge {
what: "mounted filesystems",
limit: max as u64,
};
let mut table: Vec<ffi::Statfs> = Vec::new();
for _ in 0..3 {
let n = unsafe { ffi::getfsstat(core::ptr::null_mut(), 0, ffi::MNT_NOWAIT) };
let n = usize::try_from(n).map_err(|_| os_error(WHAT, "getfsstat", last_errno()))?;
if n > max {
return Err(too_large());
}
let room = n.saturating_add(8).min(max.saturating_add(1));
let bytes = room
.checked_mul(core::mem::size_of::<ffi::Statfs>())
.and_then(|b| core::ffi::c_int::try_from(b).ok())
.ok_or_else(too_large)?;
table.clear();
table.reserve_exact(room);
let got = unsafe { ffi::getfsstat(table.as_mut_ptr(), bytes, ffi::MNT_NOWAIT) };
let got = usize::try_from(got)
.map_err(|_| os_error(WHAT, "getfsstat", last_errno()))?
.min(room);
unsafe { table.set_len(got) };
if got > max {
return Err(too_large());
}
if got < room {
break;
}
}
Ok(table)
}
#[cfg(not(target_os = "macos"))]
pub(crate) fn disk_io_totals() -> Result<DiskRaw> {
Err(super::unsupported("disk I/O"))
}
#[cfg(not(target_os = "macos"))]
pub(crate) fn disk_io_counters() -> Result<Vec<(String, DiskRaw)>> {
Err(super::unsupported("disk I/O"))
}
#[cfg(target_os = "macos")]
pub(crate) fn disk_io_totals() -> Result<DiskRaw> {
let keys = iokit::Keys::new()?;
let mut total = DiskRaw::default();
iokit::for_each_driver(|driver, _| {
if let Some(raw) = iokit::statistics(driver, &keys) {
total.add(&raw);
}
Ok(())
})?;
Ok(total)
}
#[cfg(target_os = "macos")]
pub(crate) fn disk_io_counters() -> Result<Vec<(String, DiskRaw)>> {
let keys = iokit::Keys::new()?;
let mut out = Vec::new();
iokit::for_each_driver(|driver, n| {
if let Some(raw) = iokit::statistics(driver, &keys) {
let name = iokit::bsd_name(driver, &keys).unwrap_or_else(|| format!("disk{n}"));
out.push((name, raw));
}
Ok(())
})?;
Ok(out)
}
pub(crate) fn disk_pressure() -> Result<Option<Pressure>> {
Ok(None)
}
#[cfg(target_os = "macos")]
mod iokit {
use core::{
ffi::{c_char, c_void},
time::Duration,
};
use super::super::{ffi, kern_error};
use crate::{Error, Result, disk::DiskRaw};
const WHAT: &str = "disk I/O";
const MAX_DRIVERS: usize = 4_096;
pub(super) struct Cf(ffi::CfRef);
impl Cf {
fn new(object: ffi::CfRef) -> Option<Cf> {
(!object.is_null()).then_some(Cf(object))
}
fn string(text: &str) -> Result<Cf> {
let len = ffi::CfIndex::try_from(text.len()).unwrap_or(0);
let object = unsafe {
ffi::CFStringCreateWithBytes(
core::ptr::null(),
text.as_ptr(),
len,
ffi::CF_STRING_ENCODING_UTF8,
0,
)
};
Cf::new(object).ok_or_else(|| kern_error(WHAT, "CFStringCreateWithBytes", 0))
}
fn is(&self, type_id: ffi::CfTypeId) -> bool {
unsafe { ffi::CFGetTypeID(self.0) == type_id }
}
}
impl Drop for Cf {
fn drop(&mut self) {
unsafe { ffi::CFRelease(self.0) };
}
}
pub(super) struct Io(ffi::IoObject);
impl Drop for Io {
fn drop(&mut self) {
if self.0 != 0 {
unsafe { ffi::IOObjectRelease(self.0) };
}
}
}
pub(super) struct Keys {
statistics: Cf,
bytes_read: Cf,
bytes_written: Cf,
reads: Cf,
writes: Cf,
read_time: Cf,
write_time: Cf,
bsd_name: Cf,
}
impl Keys {
pub(super) fn new() -> Result<Keys> {
Ok(Keys {
statistics: Cf::string("Statistics")?,
bytes_read: Cf::string("Bytes (Read)")?,
bytes_written: Cf::string("Bytes (Write)")?,
reads: Cf::string("Operations (Read)")?,
writes: Cf::string("Operations (Write)")?,
read_time: Cf::string("Total Time (Read)")?,
write_time: Cf::string("Total Time (Write)")?,
bsd_name: Cf::string("BSD Name")?,
})
}
}
pub(super) fn for_each_driver(
mut f: impl FnMut(ffi::IoObject, usize) -> Result<()>,
) -> Result<()> {
let matching = unsafe { ffi::IOServiceMatching(c"IOBlockStorageDriver".as_ptr()) };
if matching.is_null() {
return Err(kern_error(WHAT, "IOServiceMatching", 0));
}
let mut iterator: ffi::IoObject = 0;
let kr = unsafe {
ffi::IOServiceGetMatchingServices(
ffi::IO_MAIN_PORT_DEFAULT,
matching,
&raw mut iterator,
)
};
if kr != ffi::KERN_SUCCESS {
return Err(kern_error(WHAT, "IOServiceGetMatchingServices", kr));
}
let iterator = Io(iterator);
for n in 0..=MAX_DRIVERS {
let driver = Io(unsafe { ffi::IOIteratorNext(iterator.0) });
if driver.0 == 0 {
return Ok(());
}
if n == MAX_DRIVERS {
break;
}
f(driver.0, n)?;
}
Err(Error::TooLarge {
what: "block storage drivers",
limit: MAX_DRIVERS as u64,
})
}
pub(super) fn statistics(driver: ffi::IoObject, keys: &Keys) -> Option<DiskRaw> {
let stats = Cf::new(unsafe {
ffi::IORegistryEntryCreateCFProperty(driver, keys.statistics.0, core::ptr::null(), 0)
})?;
if !stats.is(ffi::CFDictionaryGetTypeID()) {
return None;
}
let read_time = number(&stats, &keys.read_time);
let write_time = number(&stats, &keys.write_time);
Some(DiskRaw {
read: number(&stats, &keys.bytes_read),
written: number(&stats, &keys.bytes_written),
reads: number(&stats, &keys.reads),
writes: number(&stats, &keys.writes),
busy: None,
op_time: Some(Duration::from_nanos(read_time.saturating_add(write_time))),
disks: 1,
})
}
fn number(dictionary: &Cf, key: &Cf) -> u64 {
let value = unsafe { ffi::CFDictionaryGetValue(dictionary.0, key.0) };
if value.is_null() {
return 0;
}
let type_id = unsafe { ffi::CFGetTypeID(value) };
if type_id != ffi::CFNumberGetTypeID() {
return 0;
}
let mut n: i64 = 0;
let ok = unsafe {
ffi::CFNumberGetValue(
value,
ffi::CF_NUMBER_SINT64_TYPE,
(&raw mut n).cast::<c_void>(),
)
};
if ok == 0 {
return 0;
}
u64::try_from(n).unwrap_or(0)
}
pub(super) fn bsd_name(driver: ffi::IoObject, keys: &Keys) -> Option<String> {
let mut child: ffi::IoObject = 0;
let kr = unsafe {
ffi::IORegistryEntryGetChildEntry(driver, c"IOService".as_ptr(), &raw mut child)
};
let media = Io(child);
if kr != ffi::KERN_SUCCESS || media.0 == 0 {
return None;
}
let name = Cf::new(unsafe {
ffi::IORegistryEntryCreateCFProperty(media.0, keys.bsd_name.0, core::ptr::null(), 0)
})?;
if !name.is(ffi::CFStringGetTypeID()) {
return None;
}
let mut buf = [0 as c_char; 64];
let ok = unsafe {
ffi::CFStringGetCString(
name.0,
buf.as_mut_ptr(),
buf.len() as ffi::CfIndex,
ffi::CF_STRING_ENCODING_UTF8,
)
};
if ok == 0 {
return None;
}
let bytes = super::super::c_array_bytes(&buf);
let text = crate::parse::lossy(&bytes, 64);
(!text.is_empty()).then_some(text)
}
}
#[cfg(test)]
mod tests {
use std::path::PathBuf;
use super::*;
#[test]
fn test_disk_apple_c_path_bounds_and_nul() {
let mut buf = [0u8; PATH_MAX];
assert_eq!(c_path(Path::new("/tmp"), &mut buf).unwrap(), c"/tmp");
let long = PathBuf::from("a".repeat(PATH_MAX));
assert!(matches!(
c_path(&long, &mut buf),
Err(Error::InvalidInput { .. })
));
let nul = PathBuf::from(std::ffi::OsStr::from_bytes(b"/a\0b"));
assert!(matches!(
c_path(&nul, &mut buf),
Err(Error::InvalidInput { .. })
));
}
#[test]
fn test_disk_apple_space_multiplies_blocks() {
let mut st: ffi::Statfs = unsafe { MaybeUninit::zeroed().assume_init() };
st.bsize = 4096;
st.blocks = 1000;
st.bfree = 400;
st.bavail = 300;
let s = space_of(&st);
assert_eq!(
(s.total.raw(), s.free.raw(), s.available.raw()),
(4_096_000, 1_638_400, 1_228_800)
);
st.blocks = u64::MAX;
assert_eq!(space_of(&st).total.raw(), u64::MAX);
}
#[cfg(target_os = "macos")]
fn mount(fstype: &[u8], flags: u32) -> ffi::Statfs {
let mut st: ffi::Statfs = unsafe { MaybeUninit::zeroed().assume_init() };
for (dst, &src) in st.fstypename.iter_mut().zip(fstype) {
*dst = src.cast_signed();
}
st.flags = flags;
st
}
#[cfg(target_os = "macos")]
#[test]
fn test_disk_apple_classify_by_type_and_flags() {
use ffi::{MNT_DONTBROWSE, MNT_LOCAL, MNT_REMOVABLE};
let default = ListOptions::default();
let all = ListOptions {
include_network: true,
include_pseudo: true,
include_duplicates: true,
..ListOptions::default()
};
let root = mount(b"apfs", MNT_LOCAL);
assert_eq!(classify(&root, &default), Some(DiskKind::Unknown));
let usb = mount(b"msdos", MNT_LOCAL | MNT_REMOVABLE);
assert_eq!(classify(&usb, &default), Some(DiskKind::Removable));
let data = mount(b"apfs", MNT_LOCAL | MNT_DONTBROWSE);
assert_eq!(classify(&data, &default), None);
assert_eq!(classify(&data, &all), Some(DiskKind::Unknown));
let nfs = mount(b"nfs", 0);
assert_eq!(classify(&nfs, &default), None);
assert_eq!(classify(&nfs, &all), Some(DiskKind::Network));
let smb = mount(b"smbfs", MNT_LOCAL);
assert_eq!(classify(&smb, &all), Some(DiskKind::Network));
let auto = mount(b"autofs", MNT_DONTBROWSE);
assert_eq!(classify(&auto, &default), None);
assert_eq!(classify(&auto, &all), Some(DiskKind::Virtual));
let dev = mount(b"devfs", MNT_LOCAL | MNT_DONTBROWSE);
assert_eq!(classify(&dev, &all), Some(DiskKind::Virtual));
}
#[cfg(target_os = "macos")]
#[test]
fn test_disk_apple_live_list_and_io() {
let list = disks(&ListOptions::default()).unwrap();
assert!(list.iter().any(|d| d.mount_point == Path::new("/")));
for d in &list {
assert!(!d.filesystem.is_empty() && !d.device.is_empty(), "{d:?}");
assert_ne!(d.kind, DiskKind::Network);
}
let tight = ListOptions {
max_mounts: 0,
..ListOptions::default()
};
assert!(matches!(disks(&tight), Err(Error::TooLarge { .. })));
let total = disk_io_totals().unwrap();
let each = disk_io_counters().unwrap();
let sum: u64 = each.iter().map(|(_, r)| r.read).sum();
assert!(sum <= disk_io_totals().unwrap().read);
assert!(total.read <= disk_io_totals().unwrap().read);
for (name, _) in &each {
assert!(name.starts_with("disk"), "{name}");
}
}
#[test]
fn test_disk_apple_live_space() {
let s = disk_space(Path::new("/")).unwrap();
assert!(s.total.raw() > 0 && s.available <= s.free && s.free <= s.total);
assert!(matches!(
disk_space(Path::new("/definitely/not/here/sys-lib")),
Err(Error::NotFound { .. })
));
assert_eq!(disk_pressure().unwrap(), None);
}
}