sinf 0.1.0

Library for obtaining in-depth system information at both the hardware and software levels
Documentation
use std::ffi::{CStr, OsStr};
use std::os::unix::ffi::OsStrExt;
use std::path::PathBuf;
use std::ptr;

use objc2_core_foundation::{
    CFURL, kCFAllocatorDefault, kCFURLVolumeAvailableCapacityForImportantUsageKey,
    kCFURLVolumeAvailableCapacityKey, kCFURLVolumeIsBrowsableKey, kCFURLVolumeIsEjectableKey,
    kCFURLVolumeIsInternalKey, kCFURLVolumeIsLocalKey, kCFURLVolumeIsRemovableKey,
    kCFURLVolumeNameKey, kCFURLVolumeTotalCapacityKey, kCFURLVolumeUUIDStringKey,
};

use crate::{Disk, DiskRefreshKind};

use super::new::new_disk;
use super::properties::{build_requested_properties, get_bool_value, get_disk_properties};

pub(super) unsafe fn get_list(container: &mut Vec<Disk>, refresh_kind: DiskRefreshKind) {
    unsafe {
        let raw_disks = {
            let count = libc::getfsstat(ptr::null_mut(), 0, libc::MNT_NOWAIT);
            if count < 1 {
                return;
            }
            let bufsize = count * std::mem::size_of::<libc::statfs>() as libc::c_int;
            let mut disks = Vec::with_capacity(count as _);
            let count = libc::getfsstat(disks.as_mut_ptr(), bufsize, libc::MNT_NOWAIT);

            if count < 1 {
                return;
            }

            disks.set_len(count as usize);

            disks
        };

        // At most 9 properties are queried currently.
        let mut properties = Vec::with_capacity(10);
        // "Mandatory" information.
        properties.push(kCFURLVolumeNameKey);
        properties.push(kCFURLVolumeIsBrowsableKey);
        properties.push(kCFURLVolumeUUIDStringKey);

        // Removability.
        properties.push(kCFURLVolumeIsEjectableKey);
        properties.push(kCFURLVolumeIsRemovableKey);
        properties.push(kCFURLVolumeIsInternalKey);

        if refresh_kind.storage() {
            properties.push(kCFURLVolumeTotalCapacityKey);
            properties.push(kCFURLVolumeAvailableCapacityForImportantUsageKey);
            properties.push(kCFURLVolumeAvailableCapacityKey);
        }

        if !refresh_kind.network() {
            properties.push(kCFURLVolumeIsLocalKey);
        }

        // Build the list of disk properties to fetch.
        let requested_properties = match build_requested_properties(&properties) {
            Some(properties) => properties,
            None => {
                sinf_debug!("failed to create volume key list");
                return;
            }
        };

        for c_disk in raw_disks {
            let volume_url = match CFURL::from_file_system_representation(
                kCFAllocatorDefault,
                c_disk.f_mntonname.as_ptr() as *const _,
                c_disk.f_mntonname.len() as _,
                false as _,
            ) {
                Some(url) => url,
                None => {
                    sinf_debug!("getfsstat returned incompatible paths");
                    continue;
                }
            };

            let prop_dict = match get_disk_properties(&volume_url, &requested_properties) {
                Some(props) => props,
                None => continue,
            };

            // Future note: `kCFURLVolumeIsBrowsableKey` differs from the `kCFURLEnumeratorSkipInvisibles` option of `CFURLEnumeratorOptions`.
            let browsable =
                get_bool_value(&prop_dict, kCFURLVolumeIsBrowsableKey).unwrap_or_default();

            // Do not return invisible "disks".
            if !browsable {
                continue;
            }

            // When `refresh_kind.network()` is true, `kCFURLVolumeIsLocalKey` is not added to `properties`, so `prop_dict` does not contain it and this is `None`.
            let local_or_network_enabled =
                get_bool_value(&prop_dict, kCFURLVolumeIsLocalKey).unwrap_or(true);

            if !local_or_network_enabled {
                continue;
            }

            let mount_point = PathBuf::from(OsStr::from_bytes(
                CStr::from_ptr(c_disk.f_mntonname.as_ptr()).to_bytes(),
            ));

            let uuid = OsStr::from_bytes(CStr::from_ptr(c_disk.f_mntonname.as_ptr()).to_bytes())
                .to_os_string();

            let disk = container.iter_mut().find(|d| d.inner.uuid == uuid);
            if let Some(disk) = new_disk(
                disk,
                mount_point,
                volume_url,
                c_disk,
                &prop_dict,
                refresh_kind,
                uuid,
            ) {
                container.push(disk);
            }
        }
    }
}