Refactor: drive discovery into platform files, no inline cfg
- drive/unix.rs: find_drives() + resolve_device() for Linux/macOS - drive/windows.rs: find_drives() + resolve_device() + normalize_path() for Windows - drive/mod.rs: clean delegation, no cfg branches - scsi/windows.rs: SPTI transport only, no drive discovery
This commit is contained in:
@@ -194,84 +194,3 @@ impl ScsiTransport for SptiTransport {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Device path helpers ────────────────────────────────────────────────────
|
||||
|
||||
/// Normalize a device path to Windows \\.\X: format.
|
||||
///
|
||||
/// Accepts: "D:", "D:\\", "\\.\D:", "\\.\CdRom0"
|
||||
pub(crate) fn normalize_device_path(path: &str) -> String {
|
||||
// Already in \\.\X format
|
||||
if path.starts_with("\\\\.\\") {
|
||||
return path.to_string();
|
||||
}
|
||||
|
||||
// Single drive letter: "D:" or "D:\"
|
||||
let trimmed = path.trim_end_matches('\\');
|
||||
if trimmed.len() == 2 && trimmed.as_bytes()[1] == b':' {
|
||||
return format!("\\\\.\\{}", trimmed);
|
||||
}
|
||||
|
||||
// CdRomN format
|
||||
if path.to_lowercase().starts_with("cdrom") {
|
||||
return format!("\\\\.\\{}", path);
|
||||
}
|
||||
|
||||
// Fallback: wrap in \\.\
|
||||
format!("\\\\.\\{}", path)
|
||||
}
|
||||
|
||||
/// Find all optical drives on Windows.
|
||||
/// Scans drive letters A-Z and CdRom0-15.
|
||||
pub fn find_drives() -> Vec<(String, crate::identity::DriveId)> {
|
||||
let mut drives = Vec::new();
|
||||
|
||||
// Try CdRom0..CdRom15
|
||||
for i in 0..16 {
|
||||
let path = format!("\\\\.\\CdRom{}", i);
|
||||
if let Ok(mut transport) = SptiTransport::open(Path::new(&path)) {
|
||||
if let Ok(id) = crate::identity::DriveId::from_drive(&mut transport) {
|
||||
if !id.raw_inquiry.is_empty() && (id.raw_inquiry[0] & 0x1F) == 0x05 {
|
||||
drives.push((path, id));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Also try drive letters if CdRom didn't find anything
|
||||
if drives.is_empty() {
|
||||
for letter in b'D'..=b'Z' {
|
||||
let path = format!("{}:", letter as char);
|
||||
if let Ok(mut transport) = SptiTransport::open(Path::new(&path)) {
|
||||
if let Ok(id) = crate::identity::DriveId::from_drive(&mut transport) {
|
||||
if !id.raw_inquiry.is_empty() && (id.raw_inquiry[0] & 0x1F) == 0x05 {
|
||||
drives.push((path, id));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
drives
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn normalize_drive_letter() {
|
||||
assert_eq!(normalize_device_path("D:"), "\\\\.\\D:");
|
||||
assert_eq!(normalize_device_path("E:\\"), "\\\\.\\E:");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn normalize_already_prefixed() {
|
||||
assert_eq!(normalize_device_path("\\\\.\\D:"), "\\\\.\\D:");
|
||||
assert_eq!(normalize_device_path("\\\\.\\CdRom0"), "\\\\.\\CdRom0");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn normalize_cdrom() {
|
||||
assert_eq!(normalize_device_path("CdRom0"), "\\\\.\\CdRom0");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user