sinf 0.1.0

Library for obtaining in-depth system information at both the hardware and software levels
Documentation
mod autorelease;
#[cfg(target_os = "macos")]
mod bsd_name;
mod inner;
mod list;
mod new;
mod properties;

use std::ffi::OsString;
use std::path::PathBuf;

use objc2_core_foundation::{CFRetained, CFURL};
use objc2_io_kit as _;

use crate::{Disk, DiskKind, DiskRefreshKind, DisksInner, Error};

use self::autorelease::with_autorelease;
use self::list::get_list;
#[cfg(target_os = "macos")]
pub(crate) use self::properties::{get_int_value, get_str_value};

pub(crate) struct DiskInner {
    pub(crate) type_: DiskKind,
    pub(crate) id: Option<u64>,
    pub(crate) name: OsString,
    #[cfg(target_os = "macos")]
    bsd_name: Option<Vec<u8>>,
    pub(crate) file_system: OsString,
    pub(crate) mount_point: PathBuf,
    volume_url: CFRetained<CFURL>,
    pub(crate) total_space: u64,
    pub(crate) available_space: u64,
    pub(crate) is_removable: bool,
    pub(crate) is_read_only: bool,
    pub(crate) old_written_bytes: u64,
    pub(crate) old_read_bytes: u64,
    pub(crate) written_bytes: u64,
    pub(crate) read_bytes: u64,
    updated: bool,
    uuid: OsString,
}

#[cfg(test)]
impl Default for DiskInner {
    fn default() -> DiskInner {
        Self {
            type_: DiskKind::HDD,
            name: OsString::new(),
            id: None,
            #[cfg(target_os = "macos")]
            bsd_name: None,
            file_system: OsString::new(),
            mount_point: PathBuf::new(),
            volume_url: unsafe {
                let path = b"/\0";
                CFURL::from_file_system_representation(None, path.as_ptr(), 1, true)
                    .expect("failed to create a test CFURL")
            },
            total_space: 0,
            available_space: 0,
            is_removable: false,
            is_read_only: false,
            old_written_bytes: 0,
            old_read_bytes: 0,
            written_bytes: 0,
            read_bytes: 0,
            updated: false,
            uuid: OsString::new(),
        }
    }
}

// Required because the `volume_url` type (`CFURL` contains an `*const UnsafeCell`).
unsafe impl Send for DiskInner {}

impl DisksInner {
    pub(crate) fn new() -> Result<Self, Error> {
        Ok(Self {
            disks: Vec::with_capacity(2),
        })
    }

    pub(crate) fn refresh_specifics(
        &mut self,
        remove_not_listed_disks: bool,
        refresh_kind: DiskRefreshKind,
    ) {
        unsafe {
            // SAFETY: No Objective-C objects are kept around, because this code makes no direct Objective-C calls.
            with_autorelease(|| {
                get_list(&mut self.disks, refresh_kind);
            })
        }

        if remove_not_listed_disks {
            self.disks.retain_mut(|disk| {
                if !disk.inner.updated {
                    return false;
                }
                disk.inner.updated = false;
                true
            });
        } else {
            for c in self.disks.iter_mut() {
                c.inner.updated = false;
            }
        }
    }

    pub(crate) fn list(&self) -> &[Disk] {
        &self.disks
    }

    pub(crate) fn list_mut(&mut self) -> &mut [Disk] {
        &mut self.disks
    }
}