use std::collections::HashMap;
use std::ffi::OsString;
use std::os::unix::ffi::OsStringExt;
use std::os::unix::fs::MetadataExt;
use std::path::PathBuf;
use std::ptr::null_mut;
use super::DiskInner;
use super::inner::refresh_disk;
use super::io::refresh_disk_io;
use crate::sys::utils::{c_buf_to_utf8_str, get_sys_value_str_by_name};
use crate::{Disk, DiskRefreshKind};
fn get_disks_mapping() -> HashMap<String, String> {
let mut disk_mapping = HashMap::new();
let Some(mapping) = get_sys_value_str_by_name(b"kern.geom.conftxt\0") else {
return disk_mapping;
};
let mut last_id = String::new();
for line in mapping.lines() {
let mut parts = line.split_whitespace();
let Some(kind) = parts.next() else { continue };
#[allow(clippy::collapsible_if)]
if kind == "0" {
if let Some("DISK") = parts.next()
&& let Some(id) = parts.next()
{
last_id.clear();
last_id.push_str(id);
}
} else if kind == "2" && !last_id.is_empty() {
if let Some("LABEL") = parts.next()
&& let Some(path) = parts.next()
{
disk_mapping.insert(format!("/dev/{path}"), last_id.clone());
}
}
}
disk_mapping
}
pub unsafe fn get_all_list(
container: &mut Vec<Disk>,
remove_not_listed_disks: bool,
refresh_kind: DiskRefreshKind,
) {
let mut fs_infos: *mut libc::statfs = null_mut();
let count = unsafe { libc::getmntinfo(&mut fs_infos, libc::MNT_WAIT) };
if count < 1 {
return;
}
let disk_mapping = get_disks_mapping();
let fs_infos: &[libc::statfs] =
unsafe { std::slice::from_raw_parts(fs_infos as _, count as _) };
for fs_info in fs_infos {
if fs_info.f_mntfromname[0] == 0 || fs_info.f_mntonname[0] == 0 {
continue;
}
let fs_type: Vec<u8> = {
let len = fs_info
.f_fstypename
.iter()
.position(|x| *x == 0)
.unwrap_or(fs_info.f_fstypename.len());
fs_info.f_fstypename[..len]
.iter()
.map(|c| *c as u8)
.collect()
};
match &fs_type[..] {
b"autofs" | b"devfs" | b"linprocfs" | b"procfs" | b"fdescfs" | b"tmpfs"
| b"linsysfs" => {
sinf_debug!(
"Memory filesystem `{:?}`, ignoring it.",
c_buf_to_utf8_str(&fs_info.f_fstypename),
);
continue;
}
_ => {}
}
let mount_point = match c_buf_to_utf8_str(&fs_info.f_mntonname) {
Some(m) => m,
None => {
sinf_debug!("Cannot get disk mount point, ignoring it.");
continue;
}
};
if mount_point == "/boot/efi" {
continue;
}
let name = if &fs_type[..] == b"zfs" {
OsString::from(c_buf_to_utf8_str(&fs_info.f_mntfromname).unwrap_or("")) } else if mount_point == "/" {
OsString::from("root")
} else {
OsString::from(mount_point)
};
if let Some(disk) = container.iter_mut().find(|d| {
d.inner.name == name
&& d.inner
.file_system
.as_encoded_bytes()
.iter()
.zip(fs_type.iter())
.all(|(a, b)| a == b)
}) {
refresh_disk(&mut disk.inner, refresh_kind.without_io_usage());
disk.inner.updated = true;
} else {
let dev_mount_point = c_buf_to_utf8_str(&fs_info.f_mntfromname).unwrap_or("");
let is_removable = if refresh_kind.storage() {
[b"USB", b"usb"].iter().any(|b| *b == &fs_type[..])
|| fs_type.starts_with(b"/dev/cd")
} else {
false
};
let device_id = std::fs::metadata(mount_point)
.ok()
.map(|metadata| metadata.dev());
let mut disk = DiskInner {
name,
c_mount_point: fs_info.f_mntonname.to_vec(),
mount_point: PathBuf::from(mount_point),
device_id,
dev_id: disk_mapping.get(dev_mount_point).map(ToString::to_string),
total_space: 0,
available_space: 0,
file_system: OsString::from_vec(fs_type),
is_removable,
is_read_only: false,
read_bytes: 0,
old_read_bytes: 0,
written_bytes: 0,
old_written_bytes: 0,
updated: true,
};
refresh_disk(&mut disk, refresh_kind.without_io_usage());
container.push(Disk { inner: disk });
}
}
if remove_not_listed_disks {
container.retain_mut(|disk| {
if !disk.inner.updated {
return false;
}
disk.inner.updated = false;
true
});
} else {
for c in container.iter_mut() {
c.inner.updated = false;
}
}
if refresh_kind.io_usage() {
unsafe {
refresh_disk_io(container.as_mut_slice());
}
}
}