[pbs-devel] [PATCH proxmox 2/3] proxmox-sys: add DiskUsage struct and helper

Dominik Csapak d.csapak at proxmox.com
Tue Dec 14 13:24:05 CET 2021


copied from proxmox-backup

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
 proxmox-sys/src/fs/mod.rs | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/proxmox-sys/src/fs/mod.rs b/proxmox-sys/src/fs/mod.rs
index 9fe333b..5935b1a 100644
--- a/proxmox-sys/src/fs/mod.rs
+++ b/proxmox-sys/src/fs/mod.rs
@@ -3,6 +3,7 @@ use std::fs::File;
 use std::path::Path;
 
 use anyhow::{bail, Error};
+use serde::Serialize;
 
 use std::os::unix::io::{AsRawFd, RawFd};
 use nix::unistd::{Gid, Uid};
@@ -102,3 +103,28 @@ impl CreateOptions {
     */
 }
 
+/// Basic disk usage information
+#[derive(Serialize)]
+pub struct DiskUsage {
+    pub total: u64,
+    pub used: u64,
+    pub available: u64,
+}
+
+/// Get disk usage information from path
+pub fn disk_usage(path: &std::path::Path) -> Result<DiskUsage, Error> {
+    let mut stat: libc::statfs64 = unsafe { std::mem::zeroed() };
+
+    use nix::NixPath;
+
+    let res = path.with_nix_path(|cstr| unsafe { libc::statfs64(cstr.as_ptr(), &mut stat) })?;
+    nix::errno::Errno::result(res)?;
+
+    let bsize = stat.f_bsize as u64;
+
+    Ok(DiskUsage{
+        total: stat.f_blocks*bsize,
+        used: (stat.f_blocks-stat.f_bfree)*bsize,
+        available: stat.f_bavail*bsize,
+    })
+}
-- 
2.30.2






More information about the pbs-devel mailing list