aboutsummaryrefslogtreecommitdiff
path: root/components/disk.c
diff options
context:
space:
mode:
authorkrolyxon <krolyxon@tutanota.com>2022-06-01 23:52:05 +0530
committerkrolyxon <krolyxon@tutanota.com>2022-06-01 23:52:05 +0530
commitde4cf2cf7e7b20594312ce6e7c5fceb6790a5401 (patch)
tree53d9bd0cd7c4ccb2c9ba8baeaa8754d9c5e403f2 /components/disk.c
first commit
Diffstat (limited to 'components/disk.c')
-rw-r--r--components/disk.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/components/disk.c b/components/disk.c
new file mode 100644
index 0000000..15a221b
--- /dev/null
+++ b/components/disk.c
@@ -0,0 +1,58 @@
+/* See LICENSE file for copyright and license details. */
+#include <stdio.h>
+#include <sys/statvfs.h>
+
+#include "../util.h"
+
+const char *
+disk_free(const char *path)
+{
+ struct statvfs fs;
+
+ if (statvfs(path, &fs) < 0) {
+ warn("statvfs '%s':", path);
+ return NULL;
+ }
+
+ return fmt_human(fs.f_frsize * fs.f_bavail, 1024);
+}
+
+const char *
+disk_perc(const char *path)
+{
+ struct statvfs fs;
+
+ if (statvfs(path, &fs) < 0) {
+ warn("statvfs '%s':", path);
+ return NULL;
+ }
+
+ return bprintf("%d", (int)(100 *
+ (1.0f - ((float)fs.f_bavail / (float)fs.f_blocks))));
+}
+
+const char *
+disk_total(const char *path)
+{
+ struct statvfs fs;
+
+ if (statvfs(path, &fs) < 0) {
+ warn("statvfs '%s':", path);
+ return NULL;
+ }
+
+ return fmt_human(fs.f_frsize * fs.f_blocks, 1024);
+}
+
+const char *
+disk_used(const char *path)
+{
+ struct statvfs fs;
+
+ if (statvfs(path, &fs) < 0) {
+ warn("statvfs '%s':", path);
+ return NULL;
+ }
+
+ return fmt_human(fs.f_frsize * (fs.f_blocks - fs.f_bfree), 1024);
+}