Skip to content

Commit

Permalink
add volume_serial_number on disk for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
siminx committed Apr 14, 2024
1 parent 9705c68 commit 6c3484d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/common.rs
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/debug.rs
Expand Up @@ -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(),
Expand Down
10 changes: 9 additions & 1 deletion src/windows/disk.rs
Expand Up @@ -122,6 +122,7 @@ pub(crate) struct DiskInner {
file_system: OsString,
mount_point: Vec<u16>,
s_mount_point: OsString,
volume_serial_number: String,
total_space: u64,
available_space: u64,
is_removable: bool,
Expand All @@ -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
}
Expand Down Expand Up @@ -239,10 +244,11 @@ pub(crate) unsafe fn get_list() -> Vec<Disk> {
}
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),
Expand Down Expand Up @@ -312,6 +318,7 @@ pub(crate) unsafe fn get_list() -> Vec<Disk> {

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 {
Expand All @@ -321,6 +328,7 @@ pub(crate) unsafe fn get_list() -> Vec<Disk> {
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,
Expand Down

0 comments on commit 6c3484d

Please sign in to comment.