summaryrefslogtreecommitdiff
path: root/Utility/diskfree.c
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2012-03-22 17:09:54 -0400
committerGravatar Joey Hess <joey@kitenet.net>2012-03-22 17:32:47 -0400
commite38a839a80ae70eba13b6fd0e7ee08be8a62c513 (patch)
treec6f3faf1df29c0d9ddf7458554661ee4e50c9aed /Utility/diskfree.c
parentf1398b558316a936690a8f3b01493f498d15b659 (diff)
Rewrote free disk space checking code
Moving the portability handling into a small C library cleans up things a lot, avoiding the pain of unpacking structs from inside haskell code.
Diffstat (limited to 'Utility/diskfree.c')
-rw-r--r--Utility/diskfree.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/Utility/diskfree.c b/Utility/diskfree.c
new file mode 100644
index 000000000..9ac31a752
--- /dev/null
+++ b/Utility/diskfree.c
@@ -0,0 +1,61 @@
+/* disk free space checking, C mini-library
+ *
+ * Copyright 2012 Joey Hess <joey@kitenet.net>
+ *
+ * Licensed under the GNU GPL version 3 or higher.
+ */
+
+/* Include appropriate headers for the OS, and define what will be used to
+ * check the free space. */
+#if defined(__APPLE__)
+# include <sys/param.h>
+# include <sys/mount.h>
+# define STATSTRUCT statfs
+# define STATCALL statfs64
+#else
+#if defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
+# include <sys/param.h>
+# include <sys/mount.h>
+# define STATSTRUCT statfs
+# define STATCALL statfs
+#else
+#if defined (__linux__)
+# include <sys/statvfs.h>
+# define STATSTRUCT statvfs
+# define STATCALL statvfs
+#else
+# warning free space checking code not available for this OS
+# define UNKNOWN
+#endif
+#endif
+#endif
+
+#include <errno.h>
+
+/* Checks the amount of disk that is available to regular (non-root) users.
+ * (If there's an error, or this is not supported,
+ * returns 0 and sets errno to nonzero.)
+ */
+unsigned long long int diskfree(const char *path) {
+#ifdef UNKNOWN
+ errno = 1;
+ return 0;
+#else
+ unsigned long long int available, blocksize;
+ struct STATSTRUCT buf;
+
+ errno = 0;
+ if (STATCALL(path, &buf) != 0)
+ return 0; /* errno is set */
+
+ available = buf.f_bavail;
+ blocksize = buf.f_bsize;
+ return available * blocksize;
+#endif
+}
+
+/*
+main () {
+ printf("%lli\n", diskfree("."));
+}
+*/