Add more source code documentation.
[dss.git] / df.c
1 #include <sys/statvfs.h>
2 #include <stdio.h>
3 #include <assert.h>
4 #include <string.h>
5 #include <sys/types.h>
6 #include <errno.h>
7
8 #include "gcc-compat.h"
9 #include "log.h"
10 #include "error.h"
11 #include "string.h"
12 #include "df.h"
13
14 int get_disk_space(const char *path, struct disk_space *result)
15 {
16 /* using floats allows to not care about integer overflows */
17 float total_blocks, available_blocks, blocksize;
18 float total_inodes, available_inodes;
19
20 struct statvfs vfs;
21 int ret = statvfs(path, &vfs);
22 if (ret < 0)
23 return -ERRNO_TO_DSS_ERROR(errno);
24
25 available_blocks = vfs.f_bavail;
26 blocksize = vfs.f_bsize;
27 total_blocks = vfs.f_blocks;
28 total_inodes = vfs.f_files;
29 available_inodes = vfs.f_ffree;
30
31 result->total_mb = total_blocks * blocksize / 1024.0 / 1024.0;
32 result->free_mb = available_blocks * blocksize / 1024.0 / 1024.0;
33 result->percent_free = 100.0 * available_blocks / total_blocks + 0.5;
34 result->percent_free_inodes = 100.0 * available_inodes / total_inodes + 0.5;
35 return 1;
36 }
37
38 void log_disk_space(struct disk_space *ds)
39 {
40 DSS_INFO_LOG("free: %uM/%uM (%u%%), %u%% inodes unused\n",
41 ds->free_mb, ds->total_mb, ds->percent_free,
42 ds->percent_free_inodes);
43 }