2 * Copyright (C) 2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
6 #include <sys/statvfs.h>
10 #include <sys/types.h>
13 #include "gcc-compat.h"
19 int get_disk_space(const char *path, struct disk_space *result)
21 /* using floats allows to not care about integer overflows */
22 float total_blocks, available_blocks, blocksize;
23 float total_inodes, available_inodes;
26 int ret = statvfs(path, &vfs);
28 return -ERRNO_TO_DSS_ERROR(errno);
30 available_blocks = vfs.f_bavail;
31 blocksize = vfs.f_bsize;
32 total_blocks = vfs.f_blocks;
33 total_inodes = vfs.f_files;
34 available_inodes = vfs.f_ffree;
36 result->total_mb = total_blocks * blocksize / 1024.0 / 1024.0;
37 result->free_mb = available_blocks * blocksize / 1024.0 / 1024.0;
38 result->percent_free = 100.0 * available_blocks / total_blocks + 0.5;
39 result->percent_free_inodes = 100.0 * available_inodes / total_inodes + 0.5;
43 void log_disk_space(struct disk_space *ds)
45 DSS_INFO_LOG("free: %uM/%uM (%u%%), %u%% inodes unused\n",
46 ds->free_mb, ds->total_mb, ds->percent_free,
47 ds->percent_free_inodes);