Skip to content

Commit

Permalink
Change Disk::file_system() to return &OsStr
Browse files Browse the repository at this point in the history
  • Loading branch information
chklauser committed Oct 20, 2023
1 parent 5cd2676 commit 24b2d37
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
5 changes: 1 addition & 4 deletions src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>(),
self.file_system(),
self.kind(),
if self.is_removable() { "yes" } else { "no" },
self.mount_point(),
Expand Down
22 changes: 16 additions & 6 deletions src/unix/apple/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>,
pub(crate) file_system: OsString,
pub(crate) mount_point: PathBuf,
volume_url: RetainedCFURL,
pub(crate) total_space: u64,
Expand All @@ -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
}

Expand Down Expand Up @@ -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]
.into_iter()
.map(|c| *c as u8)
.collect(),
)
};

Some(Disk {
inner: DiskInner {
Expand Down
29 changes: 17 additions & 12 deletions src/unix/freebsd/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,7 +14,7 @@ pub(crate) struct DiskInner {
mount_point: PathBuf,
total_space: u64,
available_space: u64,
file_system: Vec<u8>,
file_system: OsString,
is_removable: bool,
}

Expand All @@ -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
}

Expand Down Expand Up @@ -106,14 +107,18 @@ pub unsafe fn get_all_list(container: &mut Vec<Disk>) {
// 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<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"fdesckfs" | b"tmpfs"
| b"linsysfs" => {
sysinfo_debug!(
Expand Down Expand Up @@ -145,7 +150,7 @@ pub unsafe fn get_all_list(container: &mut Vec<Disk>) {

// 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 _;

Expand All @@ -156,7 +161,7 @@ pub unsafe fn get_all_list(container: &mut Vec<Disk>) {
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,
},
});
Expand Down
8 changes: 4 additions & 4 deletions src/unix/linux/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ macro_rules! cast {
pub(crate) struct DiskInner {
type_: DiskKind,
device_name: OsString,
file_system: Vec<u8>,
file_system: OsString,
mount_point: PathBuf,
total_space: u64,
available_space: u64,
Expand All @@ -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
}

Expand Down Expand Up @@ -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<Disk> {
let mount_point_cpath = to_cpath(mount_point);
Expand Down Expand Up @@ -273,7 +273,7 @@ fn get_all_list(container: &mut Vec<Disk>, content: &str) {
new_disk(
fs_spec.as_ref(),
Path::new(&fs_file),
fs_vfstype.as_bytes(),
fs_vfstype.as_ref(),
&removable_entries,
)
})
Expand Down
4 changes: 2 additions & 2 deletions src/unknown/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 9 additions & 9 deletions src/windows/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>,
file_system: OsString,
mount_point: Vec<u16>,
s_mount_point: OsString,
total_space: u64,
Expand All @@ -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
}

Expand Down Expand Up @@ -343,13 +343,8 @@ pub(crate) unsafe fn get_list() -> Vec<Disk> {
}
};

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::<Vec<_>>();
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 {
Expand All @@ -368,3 +363,8 @@ pub(crate) unsafe fn get_list() -> Vec<Disk> {
})
.collect::<Vec<_>>()
}

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])
}

0 comments on commit 24b2d37

Please sign in to comment.