determine windows free disk size
Some checks failed
ci/woodpecker/pr/debug Pipeline failed

This commit is contained in:
Michael Zhang 2024-10-21 10:33:44 -05:00
parent 38d9c74db5
commit 03e954560e
4 changed files with 57 additions and 16 deletions

1
.gitignore vendored
View file

@ -4,3 +4,4 @@
**/*.rs.bk **/*.rs.bk
*.swp *.swp
/.direnv /.direnv
Packet.lib

1
Cargo.lock generated
View file

@ -1544,6 +1544,7 @@ dependencies = [
"tokio", "tokio",
"tokio-stream", "tokio-stream",
"tracing", "tracing",
"winapi",
] ]
[[package]] [[package]]

View file

@ -51,7 +51,10 @@ tokio.workspace = true
tokio-stream.workspace = true tokio-stream.workspace = true
opentelemetry.workspace = true opentelemetry.workspace = true
[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3.9", features = ["fileapi", "impl-default"] }
[features] [features]
kubernetes-discovery = [ "kube", "k8s-openapi", "schemars" ] kubernetes-discovery = ["kube", "k8s-openapi", "schemars"]
consul-discovery = [ "reqwest", "err-derive" ] consul-discovery = ["reqwest", "err-derive"]
system-libs = [ "sodiumoxide/use-pkg-config" ] system-libs = ["sodiumoxide/use-pkg-config"]

View file

@ -817,23 +817,59 @@ impl NodeStatus {
} }
} }
#[cfg(windows)]
fn update_disk_usage(&mut self, meta_dir: &Path, data_dir: &DataDirEnum) { fn update_disk_usage(&mut self, meta_dir: &Path, data_dir: &DataDirEnum) {
use winapi::um::fileapi::GetDiskFreeSpaceExA;
use winapi::um::winnt::ULARGE_INTEGER;
let mount_avail = |path: &Path| -> Option<(u64, u64)> {
let mut path = path.to_path_buf();
path.push(""); // Ensure trailing slash
let mut a: ULARGE_INTEGER = Default::default();
let mut total: ULARGE_INTEGER = Default::default();
let mut free: ULARGE_INTEGER = Default::default();
let path_ptr = path.as_os_str().as_encoded_bytes().as_ptr();
let result = unsafe {
GetDiskFreeSpaceExA(path_ptr as *const i8, &mut a, &mut total, &mut free)
};
if result == 0 {
return None;
}
let free = unsafe { *free.QuadPart() };
let total = unsafe { *total.QuadPart() };
Some((free, total))
};
self.meta_disk_avail = mount_avail(meta_dir);
self.data_disk_avail = match data_dir {
DataDirEnum::Single(path_buf) => mount_avail(path_buf),
// TODO: THIS IS WRONG!! Does not take into account multiple dirs on the same partition
// Will have to deduplicate by the filesystem mount path
DataDirEnum::Multiple(dirs) => dirs
.into_iter()
.filter_map(|dir| mount_avail(&dir.path))
.reduce(|(a1, b1), (a2, b2)| (a1 + a2, b1 + b2)),
};
}
#[cfg(unix)] #[cfg(unix)]
let mount_avail = { fn update_disk_usage(&mut self, meta_dir: &Path, data_dir: &DataDirEnum) {
use nix::sys::statvfs::statvfs; use nix::sys::statvfs::statvfs;
|path: &Path| match statvfs(path) { let mount_avail = |path: &Path| match statvfs(path) {
Ok(x) => { Ok(x) => {
let avail = x.blocks_available() as u64 * x.fragment_size() as u64; let avail = x.blocks_available() as u64 * x.fragment_size() as u64;
let total = x.blocks() as u64 * x.fragment_size() as u64; let total = x.blocks() as u64 * x.fragment_size() as u64;
Some((x.filesystem_id(), avail, total)) Some((x.filesystem_id(), avail, total))
} }
Err(_) => None, Err(_) => None,
}
}; };
#[cfg(windows)]
let mount_avail = |_path: &Path| None::<(u64, _, _)>;
self.meta_disk_avail = mount_avail(meta_dir).map(|(_, a, t)| (a, t)); self.meta_disk_avail = mount_avail(meta_dir).map(|(_, a, t)| (a, t));
self.data_disk_avail = match data_dir { self.data_disk_avail = match data_dir {
DataDirEnum::Single(dir) => mount_avail(dir).map(|(_, a, t)| (a, t)), DataDirEnum::Single(dir) => mount_avail(dir).map(|(_, a, t)| (a, t)),