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:32:34 -05:00
parent 38d9c74db5
commit 9ee1231e5b
4 changed files with 54 additions and 16 deletions

3
.gitignore vendored
View file

@ -3,4 +3,5 @@
/pki /pki
**/*.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,22 +817,55 @@ 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) {
#[cfg(unix)] use winapi::um::fileapi::GetDiskFreeSpaceExA;
let mount_avail = { use winapi::um::winnt::ULARGE_INTEGER;
use nix::sys::statvfs::statvfs;
|path: &Path| match statvfs(path) { let mount_avail = |path: &Path| -> Option<(u64, u64)> {
Ok(x) => { let mut path = path.to_path_buf();
let avail = x.blocks_available() as u64 * x.fragment_size() as u64; path.push(""); // Ensure trailing slash
let total = x.blocks() as u64 * x.fragment_size() as u64;
Some((x.filesystem_id(), avail, total)) let mut a: ULARGE_INTEGER = Default::default();
} let mut total: ULARGE_INTEGER = Default::default();
Err(_) => None, 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))
}; };
#[cfg(windows)] self.meta_disk_avail = mount_avail(meta_dir);
let mount_avail = |_path: &Path| None::<(u64, _, _)>; self.data_disk_avail = match data_dir {
DataDirEnum::Single(path_buf) => mount_avail(path_buf),
DataDirEnum::Multiple(dirs) => dirs
.into_iter()
.filter_map(|dir| mount_avail(&dir.path))
.reduce(|(a1, b1), (a2, b2)| (a1 + a2, b1 + b2)),
};
}
#[cfg(unix)]
fn update_disk_usage(&mut self, meta_dir: &Path, data_dir: &DataDirEnum) {
use nix::sys::statvfs::statvfs;
let mount_avail = |path: &Path| match statvfs(path) {
Ok(x) => {
let avail = x.blocks_available() as u64 * x.fragment_size() as u64;
let total = x.blocks() as u64 * x.fragment_size() as u64;
Some((x.filesystem_id(), avail, total))
}
Err(_) => None,
};
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 {