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