Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(utils): add disk_space helpers for free-bytes lookup

Richie McIlroy2bc7a5ab4518
diff --git a/crates/utils/Cargo.toml b/crates/utils/Cargo.toml
index 66c180a55..f2c93a91c 100644
--- a/crates/utils/Cargo.toml
+++ b/crates/utils/Cargo.toml
@@ -5,6 +5,7 @@ edition = "2024"
[target.'cfg(unix)'.dependencies]
nix = { version = "0.29.0", features = ["fs"] }
+libc = "0.2"
[target.'cfg(windows)'.dependencies]
windows = { version = "0.58.0", features = [
diff --git a/crates/utils/src/disk_space.rs b/crates/utils/src/disk_space.rs
new file mode 100644
index 000000000..e8cbec742
--- /dev/null
+++ b/crates/utils/src/disk_space.rs
@@ -0,0 +1,131 @@
+use std::io;
+use std::path::Path;
+
+pub const LOW_DISK_WARN_BYTES: u64 = 200 * 1024 * 1024;
+pub const LOW_DISK_STOP_BYTES: u64 = 50 * 1024 * 1024;
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum DiskSpaceStatus {
+ Ok,
+ Low,
+ Exhausted,
+}
+
+impl DiskSpaceStatus {
+ pub fn from_bytes(bytes_free: u64) -> Self {
+ if bytes_free <= LOW_DISK_STOP_BYTES {
+ DiskSpaceStatus::Exhausted
+ } else if bytes_free <= LOW_DISK_WARN_BYTES {
+ DiskSpaceStatus::Low
+ } else {
+ DiskSpaceStatus::Ok
+ }
+ }
+}
+
+#[cfg(unix)]
+pub fn free_bytes_for_path(path: &Path) -> io::Result<u64> {
+ use std::ffi::CString;
+ use std::os::unix::ffi::OsStrExt;
+
+ let probe_path = resolve_existing_ancestor(path);
+ let c_path = CString::new(probe_path.as_os_str().as_bytes())
+ .map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?;
+
+ let mut stat: libc::statvfs = unsafe { std::mem::zeroed() };
+ let rc = unsafe { libc::statvfs(c_path.as_ptr(), &mut stat) };
+ if rc != 0 {
+ return Err(io::Error::last_os_error());
+ }
+
+ let bavail = stat.f_bavail as u64;
+ let frsize = if stat.f_frsize == 0 {
+ stat.f_bsize as u64
+ } else {
+ stat.f_frsize as u64
+ };
+ Ok(bavail.saturating_mul(frsize))
+}
+
+#[cfg(windows)]
+pub fn free_bytes_for_path(path: &Path) -> io::Result<u64> {
+ use std::os::windows::ffi::OsStrExt;
+ use windows::Win32::Storage::FileSystem::GetDiskFreeSpaceExW;
+ use windows::core::PCWSTR;
+
+ let probe_path = resolve_existing_ancestor(path);
+ let mut wide: Vec<u16> = probe_path.as_os_str().encode_wide().collect();
+ if wide.last().copied() != Some(0) {
+ wide.push(0);
+ }
+
+ let mut free_bytes_available_to_caller: u64 = 0;
+ let mut total_number_of_bytes: u64 = 0;
+ let mut total_number_of_free_bytes: u64 = 0;
+
+ unsafe {
+ GetDiskFreeSpaceExW(
+ PCWSTR(wide.as_ptr()),
+ Some(&mut free_bytes_available_to_caller),
+ Some(&mut total_number_of_bytes),
+ Some(&mut total_number_of_free_bytes),
+ )
+ .map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))?;
+ }
+
+ Ok(free_bytes_available_to_caller)
+}
+
+fn resolve_existing_ancestor(path: &Path) -> std::path::PathBuf {
+ let mut candidate: std::path::PathBuf = path.to_path_buf();
+ loop {
+ if candidate.exists() {
+ return candidate;
+ }
+ if !candidate.pop() {
+ return std::env::temp_dir();
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn status_from_bytes() {
+ assert_eq!(DiskSpaceStatus::from_bytes(u64::MAX), DiskSpaceStatus::Ok);
+ assert_eq!(
+ DiskSpaceStatus::from_bytes(LOW_DISK_WARN_BYTES + 1),
+ DiskSpaceStatus::Ok
+ );
+ assert_eq!(
+ DiskSpaceStatus::from_bytes(LOW_DISK_WARN_BYTES),
+ DiskSpaceStatus::Low
+ );
+ assert_eq!(
+ DiskSpaceStatus::from_bytes(LOW_DISK_STOP_BYTES + 1),
+ DiskSpaceStatus::Low
+ );
+ assert_eq!(
+ DiskSpaceStatus::from_bytes(LOW_DISK_STOP_BYTES),
+ DiskSpaceStatus::Exhausted
+ );
+ assert_eq!(DiskSpaceStatus::from_bytes(0), DiskSpaceStatus::Exhausted);
+ }
+
+ #[test]
+ fn free_bytes_on_temp_dir_is_positive() {
+ let dir = tempfile::tempdir().unwrap();
+ let bytes = free_bytes_for_path(dir.path()).expect("statvfs should succeed on temp dir");
+ assert!(bytes > 0);
+ }
+
+ #[test]
+ fn free_bytes_on_missing_path_walks_up_to_existing_ancestor() {
+ let dir = tempfile::tempdir().unwrap();
+ let nested = dir.path().join("nope").join("also-nope").join("nada");
+ let bytes = free_bytes_for_path(&nested).expect("should fall back to existing ancestor");
+ assert!(bytes > 0);
+ }
+}
diff --git a/crates/utils/src/lib.rs b/crates/utils/src/lib.rs
index 39eca69fa..b99740fe3 100644
--- a/crates/utils/src/lib.rs
+++ b/crates/utils/src/lib.rs
@@ -9,6 +9,8 @@ use std::{
use aho_corasick::{AhoCorasickBuilder, MatchKind};
use tracing::Instrument;
+pub mod disk_space;
+
/// Wrapper around tokio::spawn that inherits the current tracing subscriber and span.
pub fn spawn_actor<F>(future: F) -> tokio::task::JoinHandle<F::Output>
where