diff --git a/src/common.rs b/src/common.rs index f3485271e..e65cc54c3 100644 --- a/src/common.rs +++ b/src/common.rs @@ -1922,7 +1922,7 @@ impl Disk { /// println!("[{:?}] {:?}", disk.name(), disk.file_system()); /// } /// ``` - pub fn file_system(&self) -> &[u8] { + pub fn file_system(&self) -> &OsStr { self.inner.file_system() } diff --git a/src/debug.rs b/src/debug.rs index 0cd0c6fb6..98976a021 100644 --- a/src/debug.rs +++ b/src/debug.rs @@ -39,10 +39,7 @@ impl fmt::Debug for Disk { fmt, "Disk({:?})[FS: {:?}][Type: {:?}][removable: {}] mounted on {:?}: {}/{} B", self.name(), - self.file_system() - .iter() - .map(|c| *c as char) - .collect::>(), + self.file_system(), self.kind(), if self.is_removable() { "yes" } else { "no" }, self.mount_point(), diff --git a/src/unix/apple/disk.rs b/src/unix/apple/disk.rs index 2722a15b9..bc0d84af4 100644 --- a/src/unix/apple/disk.rs +++ b/src/unix/apple/disk.rs @@ -15,14 +15,14 @@ use core_foundation_sys::string::{self as cfs, CFStringRef}; use libc::c_void; use std::ffi::{CStr, OsStr, OsString}; -use std::os::unix::ffi::OsStrExt; +use std::os::unix::ffi::{OsStrExt, OsStringExt}; use std::path::{Path, PathBuf}; use std::ptr; pub(crate) struct DiskInner { pub(crate) type_: DiskKind, pub(crate) name: OsString, - pub(crate) file_system: Vec, + pub(crate) file_system: OsString, pub(crate) mount_point: PathBuf, volume_url: RetainedCFURL, pub(crate) total_space: u64, @@ -39,7 +39,7 @@ impl DiskInner { &self.name } - pub(crate) fn file_system(&self) -> &[u8] { + pub(crate) fn file_system(&self) -> &OsStr { &self.file_system } @@ -400,9 +400,19 @@ unsafe fn new_disk( let available_space = get_available_volume_space(disk_props); - let file_system = IntoIterator::into_iter(c_disk.f_fstypename) - .filter_map(|b| if b != 0 { Some(b as u8) } else { None }) - .collect(); + let file_system = { + let len = c_disk + .f_fstypename + .iter() + .position(|&b| b == 0) + .unwrap_or(c_disk.f_fstypename.len()); + OsString::from_vec( + c_disk.f_fstypename[..len] + .iter() + .map(|c| *c as u8) + .collect(), + ) + }; Some(Disk { inner: DiskInner { diff --git a/src/unix/freebsd/disk.rs b/src/unix/freebsd/disk.rs index cacba4c74..a42bd7ca1 100644 --- a/src/unix/freebsd/disk.rs +++ b/src/unix/freebsd/disk.rs @@ -3,6 +3,7 @@ use crate::{Disk, DiskKind}; use std::ffi::{OsStr, OsString}; +use std::os::unix::ffi::OsStringExt; use std::path::{Path, PathBuf}; use super::utils::c_buf_to_str; @@ -13,7 +14,7 @@ pub(crate) struct DiskInner { mount_point: PathBuf, total_space: u64, available_space: u64, - file_system: Vec, + file_system: OsString, is_removable: bool, } @@ -26,7 +27,7 @@ impl DiskInner { &self.name } - pub(crate) fn file_system(&self) -> &[u8] { + pub(crate) fn file_system(&self) -> &OsStr { &self.file_system } @@ -106,14 +107,18 @@ pub unsafe fn get_all_list(container: &mut Vec) { // If we have missing information, no need to look any further... continue; } - let fs_type: &[libc::c_char] = - if let Some(pos) = fs_info.f_fstypename.iter().position(|x| *x == 0) { - &fs_info.f_fstypename[..pos] - } else { - &fs_info.f_fstypename - }; - let fs_type: &[u8] = std::slice::from_raw_parts(fs_type.as_ptr() as _, fs_type.len()); - match fs_type { + let fs_type: Vec = { + 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"fdesckfs" | b"tmpfs" | b"linsysfs" => { sysinfo_debug!( @@ -145,7 +150,7 @@ pub unsafe fn get_all_list(container: &mut Vec) { // USB keys and CDs are removable. let is_removable = - [b"USB", b"usb"].iter().any(|b| b == &fs_type) || fs_type.starts_with(b"/dev/cd"); + [b"USB", b"usb"].iter().any(|b| *b == &fs_type[..]) || fs_type.starts_with(b"/dev/cd"); let f_frsize: u64 = vfs.f_frsize as _; @@ -156,7 +161,7 @@ pub unsafe fn get_all_list(container: &mut Vec) { mount_point: PathBuf::from(mount_point), total_space: vfs.f_blocks.saturating_mul(f_frsize), available_space: vfs.f_favail.saturating_mul(f_frsize), - file_system: fs_type.to_vec(), + file_system: OsString::from_vec(fs_type), is_removable, }, }); diff --git a/src/unix/linux/disk.rs b/src/unix/linux/disk.rs index f2f0f9ab0..35665076e 100644 --- a/src/unix/linux/disk.rs +++ b/src/unix/linux/disk.rs @@ -19,7 +19,7 @@ macro_rules! cast { pub(crate) struct DiskInner { type_: DiskKind, device_name: OsString, - file_system: Vec, + file_system: OsString, mount_point: PathBuf, total_space: u64, available_space: u64, @@ -35,7 +35,7 @@ impl DiskInner { &self.device_name } - pub(crate) fn file_system(&self) -> &[u8] { + pub(crate) fn file_system(&self) -> &OsStr { &self.file_system } @@ -96,7 +96,7 @@ impl crate::DisksInner { fn new_disk( device_name: &OsStr, mount_point: &Path, - file_system: &[u8], + file_system: &OsStr, removable_entries: &[PathBuf], ) -> Option { let mount_point_cpath = to_cpath(mount_point); @@ -273,7 +273,7 @@ fn get_all_list(container: &mut Vec, content: &str) { new_disk( fs_spec.as_ref(), Path::new(&fs_file), - fs_vfstype.as_bytes(), + fs_vfstype.as_ref(), &removable_entries, ) }) diff --git a/src/unknown/disk.rs b/src/unknown/disk.rs index f4cec7c17..727d33657 100644 --- a/src/unknown/disk.rs +++ b/src/unknown/disk.rs @@ -15,8 +15,8 @@ impl DiskInner { unreachable!() } - pub(crate) fn file_system(&self) -> &[u8] { - &[] + pub(crate) fn file_system(&self) -> &OsStr { + Default::default() } pub(crate) fn mount_point(&self) -> &Path { diff --git a/src/windows/disk.rs b/src/windows/disk.rs index 641fc2db1..18f772d86 100644 --- a/src/windows/disk.rs +++ b/src/windows/disk.rs @@ -133,7 +133,7 @@ pub(crate) unsafe fn get_volume_path_names_for_volume_name( pub(crate) struct DiskInner { type_: DiskKind, name: OsString, - file_system: Vec, + file_system: OsString, mount_point: Vec, s_mount_point: OsString, total_space: u64, @@ -150,7 +150,7 @@ impl DiskInner { &self.name } - pub(crate) fn file_system(&self) -> &[u8] { + pub(crate) fn file_system(&self) -> &OsStr { &self.file_system } @@ -343,13 +343,8 @@ pub(crate) unsafe fn get_list() -> Vec { } }; - let name_len = name.iter().position(|&x| x == 0).unwrap_or(name.len()); - let name = OsString::from_wide(&name[..name_len]); - let file_system = file_system - .iter() - .take_while(|c| **c != 0) - .map(|c| *c as u8) - .collect::>(); + let name = os_string_from_zero_terminated(&name); + let file_system = os_string_from_zero_terminated(&file_system); mount_paths .into_iter() .map(move |mount_path| Disk { @@ -368,3 +363,8 @@ pub(crate) unsafe fn get_list() -> Vec { }) .collect::>() } + +fn os_string_from_zero_terminated(name: &[u16]) -> OsString { + let len = name.iter().position(|&x| x == 0).unwrap_or(name.len()); + OsString::from_wide(&name[..len]) +}