diff --git a/crates/agent-windows/src/native.rs b/crates/agent-windows/src/native.rs index 8cec18d..02a24e7 100644 --- a/crates/agent-windows/src/native.rs +++ b/crates/agent-windows/src/native.rs @@ -300,18 +300,36 @@ pub fn snapshot() -> Result { snapshot_with_boot(None) } +fn disk_space(root: &[u16]) -> Option<(u64, u64)> { + let (mut available, mut total) = (0, 0); + (unsafe { GetDiskFreeSpaceExW(root.as_ptr(), &mut available, &mut total, ptr::null_mut()) } != 0 + && total > 0 && available <= total).then_some((available, total)) +} + +fn fixed_volumes(system: &str, available: u64, total: u64) -> Vec { + use windows_sys::Win32::Storage::FileSystem::{GetDriveTypeW, GetLogicalDrives}; + const DRIVE_FIXED: u32 = 3; + let mask = unsafe { GetLogicalDrives() }; + let mut rows = vec![json!({"name":system,"free_bytes":available,"total_bytes":total})]; + for letter in b'A'..=b'Z' { + if mask & (1 << (letter - b'A')) == 0 { continue; } + let name = format!("{}:", char::from(letter)); + if name.eq_ignore_ascii_case(system) { continue; } + let root = wide(format!("{name}\\")); + if unsafe { GetDriveTypeW(root.as_ptr()) } != DRIVE_FIXED { continue; } + // Retain an identified but unreadable volume without claiming zero usage. + let sizes = disk_space(&root); + rows.push(json!({"name":name,"free_bytes":sizes.map(|s| s.0),"total_bytes":sizes.map(|s| s.1)})); + } + rows +} + pub fn snapshot_with_boot(booted_at: Option>) -> Result { let started = Utc::now(); let hostname = computer_name(ComputerNameDnsHostname)?; - let volume = wide(std::env::var("SystemDrive").context("SYSTEM_DRIVE")? + "\\"); - let mut available = 0; - let mut total = 0; - let mut free = 0; - ensure!( - unsafe { GetDiskFreeSpaceExW(volume.as_ptr(), &mut available, &mut total, &mut free) } != 0, - "VOLUME_QUERY" - ); - ensure!(total > 0 && available <= total, "VOLUME_VALUES"); + let system = std::env::var("SystemDrive").context("SYSTEM_DRIVE")?.to_ascii_uppercase(); + let (available, total) = disk_space(&wide(format!("{system}\\"))).context("VOLUME_QUERY")?; + let volumes = fixed_volumes(&system, available, total); let (os, mut os_data) = os_inventory(registry_string)?; os_data["installed_at"] = json!(install_date()); os_data["booted_at"] = json!(booted_at); @@ -320,7 +338,7 @@ pub fn snapshot_with_boot(booted_at: Option>) -> Result { "collection_started_at":started,"collection_completed_at":completed, "observations":[ {"scope":"windows.os","source_id":"windows.registry:CurrentVersion;GetComputerNameExW;Kernel-General/12","observed_at":completed,"quality":"valid","completeness":"partial","truncated":false,"error_code":null,"data":os_data}, - {"scope":"windows.volumes","source_id":"windows.win32:GetDiskFreeSpaceExW","observed_at":completed,"quality":"valid","completeness":"partial","truncated":false,"error_code":null,"data":{"system_volume_total_bytes":total,"system_volume_available_bytes":available}}, + {"scope":"windows.volumes","source_id":"windows.win32:GetLogicalDrives;GetDriveTypeW;GetDiskFreeSpaceExW","observed_at":completed,"quality":"valid","completeness":"partial","truncated":false,"error_code":null,"data":{"system_volume_total_bytes":total,"system_volume_available_bytes":available,"drives":volumes}}, {"scope":"windows.inodes","source_id":"windows.win32","observed_at":completed,"quality":"unsupported","completeness":"unknown","truncated":false,"error_code":"NOT_APPLICABLE","data":null} ]}); snapshot["observations"] diff --git a/crates/agent-windows/src/runtime.rs b/crates/agent-windows/src/runtime.rs index e353814..6e4d197 100644 --- a/crates/agent-windows/src/runtime.rs +++ b/crates/agent-windows/src/runtime.rs @@ -3230,9 +3230,11 @@ pub fn main() -> Result<()> { let runtime = tokio::runtime::Runtime::new()?; let (boot, booted_at) = runtime.block_on(native::boot_identity())?; let inventory = native::snapshot_with_boot(Some(booted_at))?; + let drives = inventory["observations"].as_array().and_then(|observations| + observations.iter().find(|o| o["scope"] == "windows.volumes")).map(|o| &o["data"]["drives"]); println!( "{}", - serde_json::json!({"platform":"windows","architecture":std::env::consts::ARCH,"hostname_available":!inventory["hostname"].as_str().unwrap_or("").is_empty(),"system_volume_available":inventory["root_available_bytes"].as_u64().is_some(),"inodes":null,"boot_id":boot,"capabilities":CAPABILITIES,"jobs_supported":false}) + serde_json::json!({"platform":"windows","architecture":std::env::consts::ARCH,"hostname_available":!inventory["hostname"].as_str().unwrap_or("").is_empty(),"system_volume_available":inventory["root_available_bytes"].as_u64().is_some(),"fixed_volumes":drives,"inodes":null,"boot_id":boot,"capabilities":CAPABILITIES,"jobs_supported":false}) ); return Ok(()); }