← Back to Pull requests

Implement network disk identification on Windows #1824

Open
jheanlee opened this pull request with 291 lines diff across 1 files
View on GitHub
jheanlee jheanlee commented on Nov 28, 2026 04:00 UTC
Author

Closes #1384.
Related stale PR #1745.

The original approach, FindFirstVolumeW enumerates volumes using volume GUID paths, which SMB does not support.
This PR uses GetLogicalDriveStringsW, which enumerates drives in the MS-DOS Device namespaces. So valid mapped drives with a drive letter will be returned, including network shares.

Tested on a virtual machine.

Diff: 291 lines (+116 / -175) across 1 files Reviewer: GuillaumeGomez Review duration: 4302m
All Vouch review confidence checks passed
Residual risk 0.05 = Change Risk (0.37) × (1 − Review Confidence (0.86)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.37 · 291 lines diff across 1 files
0.37
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.90 · 5 substantive comment(s)
0.90
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
85%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.340
Real PR Conversation Timeline
11 items
GuillaumeGomez GuillaumeGomez commented on code on Nov 30, 2026 12:45 UTC
Code Review
src/windows/disk.rs:1
@@ -305,28 +209,24 @@ pub(crate) unsafe fn get_list(
     remove_not_listed_disks: bool,
     refreshes: DiskRefreshKind,
 ) {
-    for volume_name in get_volume_guid_paths() {
-        let mount_paths = unsafe { get_volume_path_names_for_volume_name(&volume_name[..]) };
-        if mount_paths.is_empty() {
-            continue;
-        }
+    for mount_path in get_logical_drive_strings() {
+        let raw_mount_path = PCWSTR::from_raw(mount_path.as_ptr());
+        let drive_type = unsafe { GetDriveTypeW(raw_mount_path) };
 
-        let raw_volume_name = PCWSTR::from_raw(volume_name.as_ptr());
-        let drive_type = unsafe { GetDriveTypeW(raw_volume_name) };
-
-        // FIXME: Add conditional `DRIVE_REMOTE` depending on `refreshes.network()` when network disk identification is implemented.
-        if drive_type != DRIVE_FIXED && drive_type != DRIVE_REMOVABLE && drive_type != DRIVE_CDROM {
+        if !(matches!(drive_type, DRIVE_FIXED | DRIVE_REMOVABLE | DRIVE_CDROM)
+            || (drive_type == DRIVE_REMOTE && refreshes.network()))

Took me while a to parse it. What about:

        if !matches!(drive_type, DRIVE_FIXED | DRIVE_REMOVABLE | DRIVE_CDROM)
            || (drive_type == DRIVE_REMOTE && !refreshes.network())

GuillaumeGomez GuillaumeGomez commented on code on Nov 30, 2026 12:46 UTC
Code Review
src/windows/disk.rs:1
@@ -305,28 +209,24 @@ pub(crate) unsafe fn get_list(
     remove_not_listed_disks: bool,
     refreshes: DiskRefreshKind,
 ) {
-    for volume_name in get_volume_guid_paths() {
-        let mount_paths = unsafe { get_volume_path_names_for_volume_name(&volume_name[..]) };
-        if mount_paths.is_empty() {
-            continue;
-        }
+    for mount_path in get_logical_drive_strings() {
+        let raw_mount_path = PCWSTR::from_raw(mount_path.as_ptr());
+        let drive_type = unsafe { GetDriveTypeW(raw_mount_path) };
 
-        let raw_volume_name = PCWSTR::from_raw(volume_name.as_ptr());
-        let drive_type = unsafe { GetDriveTypeW(raw_volume_name) };
-
-        // FIXME: Add conditional `DRIVE_REMOTE` depending on `refreshes.network()` when network disk identification is implemented.
-        if drive_type != DRIVE_FIXED && drive_type != DRIVE_REMOVABLE && drive_type != DRIVE_CDROM {
+        if !(matches!(drive_type, DRIVE_FIXED | DRIVE_REMOVABLE | DRIVE_CDROM)
+            || (drive_type == DRIVE_REMOTE && refreshes.network()))

Or we could even use variables like:

        let is_local_drive = matches!(drive_type, DRIVE_FIXED | DRIVE_REMOVABLE | DRIVE_CDROM);
        let is remote_and_allowed = drive_type == DRIVE_REMOTE && refreshes.network();
        if !is_local_drive && !is_remote_and_allowed {
            continue;
        }

Although this second approach makes the DRIVE_REMOTE comparison all the time (which likely doesn't matter much).

GuillaumeGomez GuillaumeGomez commented on code on Nov 30, 2026 12:50 UTC
Code Review
src/windows/disk.rs:1
@@ -338,80 +238,87 @@ pub(crate) unsafe fn get_list(
         if !volume_info_res {
             sysinfo_debug!(
                 "Error: GetVolumeInformationW = {:?}",
-                Error::from_thread().code()
+                windows::core::Error::from_thread().code()
             );
             continue;
         }
         let is_read_only = (flags & FILE_READ_ONLY_VOLUME) != 0;
 
-        // The device path is the volume name without the trailing backslash.
-        let device_path = volume_name[..volume_name.len().saturating_sub(2)]
-            .iter()
-            .copied()
+        // The `device_path` that `CreateFileW` expects is `\\.\${local_path}` without trailing backslash.
+        let device_path = r"\\.\"
+            .encode_utf16()
+            .chain(
+                mount_path[..mount_path.len().saturating_sub(2)]
+                    .iter()
+                    .copied(),
+            )
             .chain([0])
             .collect::<Vec<_>>();
 
         let name = os_string_from_zero_terminated(&name);
         let file_system = os_string_from_zero_terminated(&file_system);
-        for mount_path in mount_paths {
-            if let Some(disk) = disks
-                .iter_mut()
-                .find(|d| d.inner.mount_point == mount_path && d.inner.file_system == file_system)
-            {
-                disk.refresh_specifics(refreshes);
-                disk.inner.updated = true;
-                continue;
-            }
 
-            //  FIXME: replace with `std::os::windows::fs::MetadataExt::volume_serial_number` when stable
-            //  https://github.com/rust-lang/rust/issues/63010
-            let volume_serial_number = unsafe {
-                if let Ok(handle) = CreateFileW(
-                    PCWSTR(mount_path.as_ptr()),
-                    0,
-                    FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
-                    None,
-                    OPEN_EXISTING,
-                    FILE_FLAG_BACKUP_SEMANTICS,
-                    None,
-                ) {
-                    let mut info: BY_HANDLE_FILE_INFORMATION = std::mem::zeroed();
-                    let volume_serial_number =
-                        if GetFileInformationByHandle(handle, &mut info).is_ok() {
-                            Some(info.dwVolumeSerialNumber)
-                        } else {
-                            None
-                        };
-                    let _ = CloseHandle(handle);
-                    volume_serial_number
-                } else {
-                    None
-                }
-            };
-
-            let mut disk = DiskInner {
-                type_: DiskKind::Unknown(-1),
-                name: name.clone(),
-                volume_serial_number,
-                file_system: file_system.clone(),
-                s_mount_point: OsString::from_wide(
-                    &mount_path[..mount_path.len().saturating_sub(1)],
-                ),
-                mount_point: mount_path,
-                total_space: 0,
-                available_space: 0,
-                is_removable,
-                is_read_only,
-                device_path: device_path.clone(),
-                old_read_bytes: 0,
-                old_written_bytes: 0,
-                read_bytes: 0,
-                written_bytes: 0,
-                updated: true,
-            };
+        if let Some(disk) = disks
+            .iter_mut()
+            .find(|d| d.inner.mount_point == mount_path && d.inner.file_system == file_system)
+        {
             disk.refresh_specifics(refreshes);
-            disks.push(Disk { inner: disk });
+            disk.inner.updated = true;
+            continue;
         }
+
+        //  FIXME: replace with `std::os::windows::fs::MetadataExt::volume_serial_number` when stable
+        //  https://github.com/rust-lang/rust/issues/63010
+        let volume_serial_number = unsafe {
+            if let Ok(handle) = CreateFileW(
+                PCWSTR(mount_path.as_ptr()),
+                0,
+                FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
+                None,
+                OPEN_EXISTING,
+                FILE_FLAG_BACKUP_SEMANTICS,
+                None,
+            ) {
+                let mut info: BY_HANDLE_FILE_INFORMATION = std::mem::zeroed();

Instead of using zeroed, use MaybeUninit::uninit instead.

GuillaumeGomez GuillaumeGomez commented on code on Nov 30, 2026 12:52 UTC
Code Review
src/windows/disk.rs:1
@@ -338,80 +238,87 @@ pub(crate) unsafe fn get_list(
         if !volume_info_res {
             sysinfo_debug!(
                 "Error: GetVolumeInformationW = {:?}",
-                Error::from_thread().code()
+                windows::core::Error::from_thread().code()
             );
             continue;
         }
         let is_read_only = (flags & FILE_READ_ONLY_VOLUME) != 0;
 
-        // The device path is the volume name without the trailing backslash.
-        let device_path = volume_name[..volume_name.len().saturating_sub(2)]
-            .iter()
-            .copied()
+        // The `device_path` that `CreateFileW` expects is `\\.\${local_path}` without trailing backslash.
+        let device_path = r"\\.\"
+            .encode_utf16()
+            .chain(
+                mount_path[..mount_path.len().saturating_sub(2)]
+                    .iter()
+                    .copied(),
+            )
             .chain([0])
             .collect::<Vec<_>>();
 
         let name = os_string_from_zero_terminated(&name);
         let file_system = os_string_from_zero_terminated(&file_system);
-        for mount_path in mount_paths {
-            if let Some(disk) = disks
-                .iter_mut()
-                .find(|d| d.inner.mount_point == mount_path && d.inner.file_system == file_system)
-            {
-                disk.refresh_specifics(refreshes);
-                disk.inner.updated = true;
-                continue;
-            }
 
-            //  FIXME: replace with `std::os::windows::fs::MetadataExt::volume_serial_number` when stable
-            //  https://github.com/rust-lang/rust/issues/63010
-            let volume_serial_number = unsafe {
-                if let Ok(handle) = CreateFileW(
-                    PCWSTR(mount_path.as_ptr()),
-                    0,
-                    FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
-                    None,
-                    OPEN_EXISTING,
-                    FILE_FLAG_BACKUP_SEMANTICS,
-                    None,
-                ) {
-                    let mut info: BY_HANDLE_FILE_INFORMATION = std::mem::zeroed();
-                    let volume_serial_number =
-                        if GetFileInformationByHandle(handle, &mut info).is_ok() {
-                            Some(info.dwVolumeSerialNumber)
-                        } else {
-                            None
-                        };
-                    let _ = CloseHandle(handle);
-                    volume_serial_number
-                } else {
-                    None
-                }
-            };
-
-            let mut disk = DiskInner {
-                type_: DiskKind::Unknown(-1),
-                name: name.clone(),
-                volume_serial_number,
-                file_system: file_system.clone(),
-                s_mount_point: OsString::from_wide(
-                    &mount_path[..mount_path.len().saturating_sub(1)],
-                ),
-                mount_point: mount_path,
-                total_space: 0,
-                available_space: 0,
-                is_removable,
-                is_read_only,
-                device_path: device_path.clone(),
-                old_read_bytes: 0,
-                old_written_bytes: 0,
-                read_bytes: 0,
-                written_bytes: 0,
-                updated: true,
-            };
+        if let Some(disk) = disks
+            .iter_mut()
+            .find(|d| d.inner.mount_point == mount_path && d.inner.file_system == file_system)
+        {
             disk.refresh_specifics(refreshes);
-            disks.push(Disk { inner: disk });
+            disk.inner.updated = true;
+            continue;
         }
+
+        //  FIXME: replace with `std::os::windows::fs::MetadataExt::volume_serial_number` when stable
+        //  https://github.com/rust-lang/rust/issues/63010
+        let volume_serial_number = unsafe {
+            if let Ok(handle) = CreateFileW(
+                PCWSTR(mount_path.as_ptr()),
+                0,
+                FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
+                None,
+                OPEN_EXISTING,
+                FILE_FLAG_BACKUP_SEMANTICS,
+                None,
+            ) {

            if let Some(handle) = CreateFileW(
                PCWSTR(mount_path.as_ptr()),
                0,
                FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
                None,
                OPEN_EXISTING,
                FILE_FLAG_BACKUP_SEMANTICS,
                None,
            ).ok().and_then(HandleWrapper::new) {

We have a HandleWrapper type which handles the CloseHandle for us, so let's put it into use.

GuillaumeGomez GuillaumeGomez commented on code on Nov 30, 2026 12:52 UTC
Code Review
src/windows/disk.rs:1
@@ -338,80 +238,87 @@ pub(crate) unsafe fn get_list(
         if !volume_info_res {
             sysinfo_debug!(
                 "Error: GetVolumeInformationW = {:?}",
-                Error::from_thread().code()
+                windows::core::Error::from_thread().code()
             );
             continue;
         }
         let is_read_only = (flags & FILE_READ_ONLY_VOLUME) != 0;
 
-        // The device path is the volume name without the trailing backslash.
-        let device_path = volume_name[..volume_name.len().saturating_sub(2)]
-            .iter()
-            .copied()
+        // The `device_path` that `CreateFileW` expects is `\\.\${local_path}` without trailing backslash.
+        let device_path = r"\\.\"
+            .encode_utf16()
+            .chain(
+                mount_path[..mount_path.len().saturating_sub(2)]
+                    .iter()
+                    .copied(),
+            )
             .chain([0])
             .collect::<Vec<_>>();
 
         let name = os_string_from_zero_terminated(&name);
         let file_system = os_string_from_zero_terminated(&file_system);
-        for mount_path in mount_paths {
-            if let Some(disk) = disks
-                .iter_mut()
-                .find(|d| d.inner.mount_point == mount_path && d.inner.file_system == file_system)
-            {
-                disk.refresh_specifics(refreshes);
-                disk.inner.updated = true;
-                continue;
-            }
 
-            //  FIXME: replace with `std::os::windows::fs::MetadataExt::volume_serial_number` when stable
-            //  https://github.com/rust-lang/rust/issues/63010
-            let volume_serial_number = unsafe {
-                if let Ok(handle) = CreateFileW(
-                    PCWSTR(mount_path.as_ptr()),
-                    0,
-                    FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
-                    None,
-                    OPEN_EXISTING,
-                    FILE_FLAG_BACKUP_SEMANTICS,
-                    None,
-                ) {
-                    let mut info: BY_HANDLE_FILE_INFORMATION = std::mem::zeroed();
-                    let volume_serial_number =
-                        if GetFileInformationByHandle(handle, &mut info).is_ok() {
-                            Some(info.dwVolumeSerialNumber)
-                        } else {
-                            None
-                        };
-                    let _ = CloseHandle(handle);
-                    volume_serial_number
-                } else {
-                    None
-                }
-            };
-
-            let mut disk = DiskInner {
-                type_: DiskKind::Unknown(-1),
-                name: name.clone(),
-                volume_serial_number,
-                file_system: file_system.clone(),
-                s_mount_point: OsString::from_wide(
-                    &mount_path[..mount_path.len().saturating_sub(1)],
-                ),
-                mount_point: mount_path,
-                total_space: 0,
-                available_space: 0,
-                is_removable,
-                is_read_only,
-                device_path: device_path.clone(),
-                old_read_bytes: 0,
-                old_written_bytes: 0,
-                read_bytes: 0,
-                written_bytes: 0,
-                updated: true,
-            };
+        if let Some(disk) = disks
+            .iter_mut()
+            .find(|d| d.inner.mount_point == mount_path && d.inner.file_system == file_system)
+        {
             disk.refresh_specifics(refreshes);
-            disks.push(Disk { inner: disk });
+            disk.inner.updated = true;
+            continue;
         }
+
+        //  FIXME: replace with `std::os::windows::fs::MetadataExt::volume_serial_number` when stable
+        //  https://github.com/rust-lang/rust/issues/63010
+        let volume_serial_number = unsafe {
+            if let Ok(handle) = CreateFileW(
+                PCWSTR(mount_path.as_ptr()),
+                0,
+                FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
+                None,
+                OPEN_EXISTING,
+                FILE_FLAG_BACKUP_SEMANTICS,
+                None,
+            ) {
+                let mut info: BY_HANDLE_FILE_INFORMATION = std::mem::zeroed();
+                let volume_serial_number = if GetFileInformationByHandle(handle, &mut info).is_ok()
+                {
+                    Some(info.dwVolumeSerialNumber)
+                } else {
+                    None
+                };
+                let _ = CloseHandle(handle);

Unneeded with HandleWrapper.

GuillaumeGomez GuillaumeGomez commented on code on Nov 30, 2026 12:56 UTC
Code Review
src/windows/disk.rs:1
@@ -429,6 +336,40 @@ pub(crate) unsafe fn get_list(
     }
 }
 
+fn get_logical_drive_strings() -> Vec<Vec<u16>> {
+    unsafe {
+        let required = GetLogicalDriveStringsW(None);
+
+        if required == 0 {
+            sysinfo_debug!(
+                "GetLogicalDriveStringsW failed: {:?}",
+                windows::core::Error::from_thread().code()
+            );
+            return Vec::new();
+        }
+
+        let mut buffer = vec![0u16; required as usize];
+
+        let len = GetLogicalDriveStringsW(Some(&mut buffer));
+
+        if len == 0 || len > required {

Good handling for len == 0, however for len > required, we should likely extend the buffer size (although it'd be weird that such a case would happen).

GuillaumeGomez GuillaumeGomez commented on code on Nov 30, 2026 12:57 UTC
Code Review
src/windows/disk.rs:1
@@ -429,6 +336,40 @@ pub(crate) unsafe fn get_list(
     }
 }
 
+fn get_logical_drive_strings() -> Vec<Vec<u16>> {
+    unsafe {
+        let required = GetLogicalDriveStringsW(None);
+
+        if required == 0 {
+            sysinfo_debug!(
+                "GetLogicalDriveStringsW failed: {:?}",
+                windows::core::Error::from_thread().code()
+            );
+            return Vec::new();
+        }
+
+        let mut buffer = vec![0u16; required as usize];

From the function docs:

If the function succeeds, the return value is the length, in characters, of the strings copied to the buffer, not including the terminating null character. Note that an ANSI-ASCII null character uses one byte, but a Unicode (UTF-16) null character uses two bytes.

I think it needs to be required as usze + 1.

GuillaumeGomez GuillaumeGomez requested changes on Nov 30, 2026 12:57 UTC
Changes Requested Reviewer

Good start, little things to be modified.

GuillaumeGomez GuillaumeGomez commented on code on Nov 30, 2026 20:48 UTC
Code Review
src/windows/disk.rs:1
@@ -430,6 +327,54 @@ pub(crate) unsafe fn get_list(
     }
 }
 
+fn get_logical_drive_strings() -> Vec<Vec<u16>> {
+    unsafe {
+        let required = GetLogicalDriveStringsW(None);
+
+        if required == 0 {
+            sysinfo_debug!(
+                "GetLogicalDriveStringsW failed: {:?}",
+                windows::core::Error::from_thread().code()
+            );
+            return Vec::new();
+        }
+
+        let mut retry_count = 0;
+        let mut buffer = vec![0u16; required as usize + 1];

Please add as a code comment what I explained for the + 1.

GuillaumeGomez GuillaumeGomez commented on code on Nov 30, 2026 20:48 UTC
Code Review
src/windows/disk.rs:1
@@ -430,6 +327,54 @@ pub(crate) unsafe fn get_list(
     }
 }
 
+fn get_logical_drive_strings() -> Vec<Vec<u16>> {
+    unsafe {
+        let required = GetLogicalDriveStringsW(None);
+
+        if required == 0 {
+            sysinfo_debug!(
+                "GetLogicalDriveStringsW failed: {:?}",
+                windows::core::Error::from_thread().code()
+            );
+            return Vec::new();
+        }
+
+        let mut retry_count = 0;
+        let mut buffer = vec![0u16; required as usize + 1];
+
+        loop {
+            let len = GetLogicalDriveStringsW(Some(&mut buffer));
+
+            if len == 0 {
+                sysinfo_debug!(
+                    "GetLogicalDriveStringsW failed: {:?}",
+                    windows::core::Error::from_thread().code()
+                );
+                return Vec::new();
+            }
+
+            if len > required {
+                if retry_count < 3 {

Would be better as a for _ in 0..3 loop.

Assignees
jheanlee jheanlee
Labels
risk: low confidence: 85.9% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 37.0% (MEDIUM) · Higher = larger blast radius
37.0%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 85.9% · Lower = rushed or shallow review
85.9%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 5.2% · Safe to merge
5.2%