]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
lib: Add statvfs2fsusage
authorVolker Lendecke <vl@samba.org>
Fri, 23 Jan 2026 17:34:57 +0000 (18:34 +0100)
committerAnoop C S <anoopcs@samba.org>
Sun, 15 Feb 2026 10:42:34 +0000 (10:42 +0000)
Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Anoop C S <anoopcs@samba.org>
lib/util/statvfs.c
lib/util/statvfs.h

index 91a7152c66a7e814dfefdb29274294d864baff76..40149c3407b6d74b818ca2c03b1f8debdf75adef 100644 (file)
@@ -255,3 +255,32 @@ int sys_fstatvfs(int fd, struct vfs_statvfs_struct *statbuf)
 #endif /* EOPNOTSUPP */
 #endif /* LINUX */
 }
+
+/*
+ * Return the number of TOSIZE-byte blocks used by BLOCKS
+ * FROMSIZE-byte blocks, rounding away from zero.
+ */
+static uint64_t adjust_blocks(uint64_t blocks, uint64_t fromsize)
+{
+       if (fromsize == 512) {
+               return blocks;
+       }
+
+       if (fromsize > 512) {
+               return blocks * (fromsize / 512);
+       }
+
+       if (fromsize == 0) {
+               fromsize = 512;
+       }
+
+       return (blocks + 1) / (512 / fromsize);
+}
+
+void statvfs2fsusage(const struct vfs_statvfs_struct *statbuf,
+                    uint64_t *dfree,
+                    uint64_t *dsize)
+{
+       *dfree = adjust_blocks(statbuf->UserBlocksAvail, statbuf->BlockSize);
+       *dsize = adjust_blocks(statbuf->TotalBlocks, statbuf->BlockSize);
+}
index 3309f83dbda9ed4bed227ae5d9f1ced9527c4bb7..301cbc25873c61eba8ffe0755a15fa5347fe8daa 100644 (file)
@@ -54,5 +54,8 @@ struct vfs_statvfs_struct {
 
 int sys_statvfs(const char *path, struct vfs_statvfs_struct *statbuf);
 int sys_fstatvfs(int fd, struct vfs_statvfs_struct *statbuf);
+void statvfs2fsusage(const struct vfs_statvfs_struct *statbuf,
+                    uint64_t *dfree,
+                    uint64_t *dsize);
 
 #endif