From: Volker Lendecke Date: Fri, 23 Jan 2026 17:34:57 +0000 (+0100) Subject: lib: Add statvfs2fsusage X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=85685fbd7e3f5fda2577fbbabee8e2caa4977e75;p=thirdparty%2Fsamba.git lib: Add statvfs2fsusage Signed-off-by: Volker Lendecke Reviewed-by: Anoop C S --- diff --git a/lib/util/statvfs.c b/lib/util/statvfs.c index 91a7152c66a..40149c3407b 100644 --- a/lib/util/statvfs.c +++ b/lib/util/statvfs.c @@ -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); +} diff --git a/lib/util/statvfs.h b/lib/util/statvfs.h index 3309f83dbda..301cbc25873 100644 --- a/lib/util/statvfs.h +++ b/lib/util/statvfs.h @@ -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