summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndre Noll <maan@systemlinux.org>2008-03-16 22:49:50 +0100
committerAndre Noll <maan@systemlinux.org>2008-03-16 22:49:50 +0100
commit985ee856a76b6cf415b2342dcfb44ac9192e5483 (patch)
treeecfdd2ab4c1fd04538273b92211f9076abe32639
parent2a2d1f8d52477af7e6e161d60c722b7a2f1a4651 (diff)
Add fd.c and ds.h: functions for computing free disk space.
-rw-r--r--Makefile2
-rw-r--r--df.c35
-rw-r--r--df.h9
-rw-r--r--dss.c13
4 files changed, 58 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index c9e16ac..e0eeb48 100644
--- 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
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
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
--- 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);