From 6c3484d195845a428b419f421d4ccb5da18f375b Mon Sep 17 00:00:00 2001 From: siminx Date: Sun, 14 Apr 2024 21:54:56 +0800 Subject: [PATCH] add `volume_serial_number` on `disk` for Windows --- src/common.rs | 14 ++++++++++++++ src/debug.rs | 3 ++- src/windows/disk.rs | 10 +++++++++- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/common.rs b/src/common.rs index ba987610b..4e850bb54 100644 --- a/src/common.rs +++ b/src/common.rs @@ -2419,6 +2419,20 @@ impl Disk { self.inner.mount_point() } + /// Returns the volume serial number. + /// + /// ```no_run + /// use sysinfo::Disks; + /// + /// let disks = Disks::new_with_refreshed_list(); + /// for disk in disks.list() { + /// println!("[{:?}] {:?}", disk.name(), disk.volume_serial_number()); + /// } + /// ``` + pub fn volume_serial_number(&self) -> &String { + self.inner.volume_serial_number() + } + /// Returns the total disk size, in bytes. /// /// ```no_run diff --git a/src/debug.rs b/src/debug.rs index c9f502306..90f37f63d 100644 --- a/src/debug.rs +++ b/src/debug.rs @@ -37,11 +37,12 @@ impl fmt::Debug for Disk { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { write!( fmt, - "Disk({:?})[FS: {:?}][Type: {:?}][removable: {}] mounted on {:?}: {}/{} B", + "Disk({:?})[FS: {:?}][Type: {:?}][removable: {}] [VSN: {}] mounted on {:?}: {}/{} B", self.name(), self.file_system(), self.kind(), if self.is_removable() { "yes" } else { "no" }, + self.volume_serial_number(), self.mount_point(), self.available_space(), self.total_space(), diff --git a/src/windows/disk.rs b/src/windows/disk.rs index 16c7eafdb..56d96d52d 100644 --- a/src/windows/disk.rs +++ b/src/windows/disk.rs @@ -122,6 +122,7 @@ pub(crate) struct DiskInner { file_system: OsString, mount_point: Vec, s_mount_point: OsString, + volume_serial_number: String, total_space: u64, available_space: u64, is_removable: bool, @@ -144,6 +145,10 @@ impl DiskInner { self.s_mount_point.as_ref() } + pub(crate) fn volume_serial_number(&self) -> &String { + &self.volume_serial_number + } + pub(crate) fn total_space(&self) -> u64 { self.total_space } @@ -239,10 +244,11 @@ pub(crate) unsafe fn get_list() -> Vec { } let mut name = [0u16; MAX_PATH as usize + 1]; let mut file_system = [0u16; 32]; + let mut volume_serial_number = 0u32; let volume_info_res = GetVolumeInformationW( raw_volume_name, Some(&mut name), - None, + Some(&mut volume_serial_number), None, None, Some(&mut file_system), @@ -312,6 +318,7 @@ pub(crate) unsafe fn get_list() -> Vec { let name = os_string_from_zero_terminated(&name); let file_system = os_string_from_zero_terminated(&file_system); + let volume_serial_number = format!("{:0>8}", format!("{:X}", volume_serial_number)); mount_paths .into_iter() .map(move |mount_path| Disk { @@ -321,6 +328,7 @@ pub(crate) unsafe fn get_list() -> Vec { file_system: file_system.clone(), s_mount_point: OsString::from_wide(&mount_path[..mount_path.len() - 1]), mount_point: mount_path, + volume_serial_number: volume_serial_number.clone(), total_space, available_space, is_removable,