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 libc::getmntinfo;
use super::DiskInner;
use super::inner::refresh_disk;
use super::io::get_dev_id;
use super::io::refresh_disk_io;
use crate::sys::utils::c_buf_to_utf8_str;
use crate::{Disk, DiskRefreshKind};
pub unsafe fn get_all_list(
container: &mut Vec<Disk>,
remove_not_listed_disks: bool,
refresh_kind: DiskRefreshKind,
) {
let mut mnt_buf: *mut libc::statvfs = null_mut();
let count = unsafe { getmntinfo(&mut mnt_buf, libc::MNT_WAIT) };
if count < 1 {
return;
}
let mnt_buf: &[libc::statvfs] = unsafe { std::slice::from_raw_parts(mnt_buf as _, count as _) };
for fs_info in mnt_buf {
if (fs_info.f_flag & libc::MNT_IGNORE as u64) != 0 {
continue;
}
if (fs_info.f_flag & libc::MNT_LOCAL as u64) == 0 {
continue;
}
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"fdesc" | b"tmpfs" | b"linsysfs"
| b"kernfs" | b"ptyfs" => {
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 = match c_buf_to_utf8_str(&fs_info.f_mntfromname) {
Some(device_name) => OsString::from(device_name),
None => {
sinf_debug!("Cannot get disk device name, ignoring it.");
continue;
}
};
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 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(),
device_id,
dev_id: get_dev_id(&fs_info.f_mntfromname),
mount_point: PathBuf::from(mount_point),
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);
}
}
}