1 #include <sys/statvfs.h>
8 #include "gcc-compat.h"
13 int get_disk_space(const char *path, struct disk_space *result)
15 /* using floats allows to not care about integer overflows */
16 float total_blocks, available_blocks, blocksize;
17 float total_inodes, available_inodes;
20 int ret = statvfs(path, &vfs);
22 return -ERRNO_TO_DSS_ERROR(errno);
24 available_blocks = vfs.f_bavail;
25 blocksize = vfs.f_bsize;
26 total_blocks = vfs.f_blocks;
27 total_inodes = vfs.f_files;
28 available_inodes = vfs.f_ffree;
30 result->total_mb = total_blocks * blocksize / 1024.0 / 1024.0;
31 result->free_mb = available_blocks * blocksize / 1024.0 / 1024.0;
32 result->percent_free = 100.0 * available_blocks / total_blocks + 0.5;
33 result->percent_free_inodes = 100.0 * available_inodes / total_inodes + 0.5;