Add fd.c and ds.h: functions for computing free disk space.
authorAndre Noll <maan@systemlinux.org>
Sun, 16 Mar 2008 21:49:50 +0000 (22:49 +0100)
committerAndre Noll <maan@systemlinux.org>
Sun, 16 Mar 2008 21:49:50 +0000 (22:49 +0100)
Makefile
df.c [new file with mode: 0644]
df.h [new file with mode: 0644]
dss.c

index c9e16ac..e0eeb48 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-dss_objects := dss.o cmdline.o string.o fd.o exec.o signal.o daemon.o
+dss_objects := dss.o cmdline.o string.o fd.o exec.o signal.o daemon.o df.o
 all: dss
 man: dss.1
 
diff --git a/df.c b/df.c
new file mode 100644 (file)
index 0000000..c0c42f8
--- /dev/null
+++ b/df.c
@@ -0,0 +1,35 @@
+#include <sys/statvfs.h>
+#include <stdio.h>
+#include <assert.h>
+#include <string.h>
+#include <sys/types.h>
+#include <errno.h>
+
+#include "gcc-compat.h"
+#include "error.h"
+#include "string.h"
+#include "df.h"
+
+int get_disk_space(const char *path, struct disk_space *result)
+{
+       /* using floats allows to not care about integer overflows */
+       float total_blocks, available_blocks, blocksize;
+       float total_inodes, available_inodes;
+
+       struct statvfs vfs;
+       int ret = statvfs(path, &vfs);
+       if (ret < 0)
+               return -ERRNO_TO_DSS_ERROR(errno);
+
+       available_blocks = vfs.f_bavail;
+       blocksize = vfs.f_bsize;
+       total_blocks = vfs.f_blocks;
+       total_inodes = vfs.f_files;
+       available_inodes = vfs.f_ffree;
+
+       result->total_mb = total_blocks * blocksize / 1024.0 / 1024.0;
+       result->free_mb =  available_blocks * blocksize / 1024.0 / 1024.0;
+       result->percent_free = 100.0 * available_blocks / total_blocks + 0.5;
+       result->percent_free_inodes = 100.0 * available_inodes / total_inodes + 0.5;
+       return 1;
+}
diff --git a/df.h b/df.h
new file mode 100644 (file)
index 0000000..d89531f
--- /dev/null
+++ b/df.h
@@ -0,0 +1,9 @@
+struct disk_space {
+       unsigned total_mb;
+       unsigned free_mb;
+       unsigned percent_free;
+       unsigned percent_free_inodes;
+};
+
+int get_disk_space(const char *path, struct disk_space *result);
+
diff --git a/dss.c b/dss.c
index 3612d4f..1fa6d56 100644 (file)
--- a/dss.c
+++ b/dss.c
@@ -24,6 +24,7 @@
 #include "exec.h"
 #include "daemon.h"
 #include "signal.h"
+#include "df.h"
 
 
 struct gengetopt_args_info conf;
@@ -472,12 +473,24 @@ int com_run(void)
        return 42;
 }
 
+void log_disk_space(struct disk_space *ds)
+{
+       DSS_INFO_LOG("free: %uM/%uM (%u%%), %u%% inodes unused\n",
+               ds->free_mb, ds->total_mb, ds->percent_free,
+               ds->percent_free_inodes);
+}
+
 int com_prune(void)
 {
        int ret;
        struct snapshot_list sl;
        pid_t pid;
+       struct disk_space ds;
 
+       ret = get_disk_space(".", &ds);
+       if (ret < 0)
+               return ret;
+       log_disk_space(&ds);
        for (;;) {
                get_snapshot_list(&sl);
                ret = remove_old_snapshot(&sl, &pid);