Collect capacity for every Windows fixed drive letter
Rust development slice / linux (push) Failing after 11s
Rust development slice / linux (push) Failing after 11s
This commit is contained in:
@@ -300,18 +300,36 @@ pub fn snapshot() -> Result<Value> {
|
|||||||
snapshot_with_boot(None)
|
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<Value> {
|
||||||
|
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<DateTime<Utc>>) -> Result<Value> {
|
pub fn snapshot_with_boot(booted_at: Option<DateTime<Utc>>) -> Result<Value> {
|
||||||
let started = Utc::now();
|
let started = Utc::now();
|
||||||
let hostname = computer_name(ComputerNameDnsHostname)?;
|
let hostname = computer_name(ComputerNameDnsHostname)?;
|
||||||
let volume = wide(std::env::var("SystemDrive").context("SYSTEM_DRIVE")? + "\\");
|
let system = std::env::var("SystemDrive").context("SYSTEM_DRIVE")?.to_ascii_uppercase();
|
||||||
let mut available = 0;
|
let (available, total) = disk_space(&wide(format!("{system}\\"))).context("VOLUME_QUERY")?;
|
||||||
let mut total = 0;
|
let volumes = fixed_volumes(&system, available, total);
|
||||||
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 (os, mut os_data) = os_inventory(registry_string)?;
|
let (os, mut os_data) = os_inventory(registry_string)?;
|
||||||
os_data["installed_at"] = json!(install_date());
|
os_data["installed_at"] = json!(install_date());
|
||||||
os_data["booted_at"] = json!(booted_at);
|
os_data["booted_at"] = json!(booted_at);
|
||||||
@@ -320,7 +338,7 @@ pub fn snapshot_with_boot(booted_at: Option<DateTime<Utc>>) -> Result<Value> {
|
|||||||
"collection_started_at":started,"collection_completed_at":completed,
|
"collection_started_at":started,"collection_completed_at":completed,
|
||||||
"observations":[
|
"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.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}
|
{"scope":"windows.inodes","source_id":"windows.win32","observed_at":completed,"quality":"unsupported","completeness":"unknown","truncated":false,"error_code":"NOT_APPLICABLE","data":null}
|
||||||
]});
|
]});
|
||||||
snapshot["observations"]
|
snapshot["observations"]
|
||||||
|
|||||||
@@ -3230,9 +3230,11 @@ pub fn main() -> Result<()> {
|
|||||||
let runtime = tokio::runtime::Runtime::new()?;
|
let runtime = tokio::runtime::Runtime::new()?;
|
||||||
let (boot, booted_at) = runtime.block_on(native::boot_identity())?;
|
let (boot, booted_at) = runtime.block_on(native::boot_identity())?;
|
||||||
let inventory = native::snapshot_with_boot(Some(booted_at))?;
|
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!(
|
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(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user