dss.c: Add some more 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 "error.h"
10 #include "string.h"
11 #include "df.h"
12
13 int get_disk_space(const char *path, struct disk_space *result)
14 {
15 /* using floats allows to not care about integer overflows */
16 float total_blocks, available_blocks, blocksize;
17 float total_inodes, available_inodes;
18
19 struct statvfs vfs;
20 int ret = statvfs(path, &vfs);
21 if (ret < 0)
22 return -ERRNO_TO_DSS_ERROR(errno);
23
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;
29
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;
34 return 1;
35 }