Merge branch 'refs/heads/t/ls-incomplete-duration'
[dss.git] / df.c
1 /*
2  * Copyright (C) 2008-2010 Andre Noll <maan@tuebingen.mpg.de>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6 #include <sys/statvfs.h>
7 #include <stdio.h>
8 #include <assert.h>
9 #include <string.h>
10 #include <sys/types.h>
11 #include <errno.h>
12
13 #include "gcc-compat.h"
14 #include "log.h"
15 #include "err.h"
16 #include "str.h"
17 #include "df.h"
18
19 int get_disk_space(const char *path, struct disk_space *result)
20 {
21         /* With floats we don't need to care about integer overflows. */
22         float total_blocks, available_blocks, blocksize;
23         float total_inodes, available_inodes;
24
25         struct statvfs vfs;
26         int ret = statvfs(path, &vfs);
27         if (ret < 0)
28                 return -ERRNO_TO_DSS_ERROR(errno);
29
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;
35
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;
40         return 1;
41 }
42
43 void log_disk_space(struct disk_space *ds)
44 {
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));
48 }