]> git.sur5r.net Git - i3/i3status/blob - src/print_disk_info.c
fix: use SYSCONFDIR in error message
[i3/i3status] / src / print_disk_info.c
1 // vim:ts=4:sw=4:expandtab
2 #include <config.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <ctype.h>
7 #include <stdint.h>
8 #include <sys/stat.h>
9 #include <sys/statvfs.h>
10 #include <sys/types.h>
11 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
12 #include <sys/param.h>
13 #include <sys/mount.h>
14 #elif defined(__NetBSD__)
15 #else
16 #include <mntent.h>
17 #endif
18 #include <yajl/yajl_gen.h>
19 #include <yajl/yajl_version.h>
20
21 #include "i3status.h"
22
23 #define BINARY_BASE UINT64_C(1024)
24 #define DECIMAL_BASE UINT64_C(1000)
25
26 #define MAX_EXPONENT 4
27
28 static const char *const iec_symbols[MAX_EXPONENT + 1] = {"", "Ki", "Mi", "Gi", "Ti"};
29 static const char *const si_symbols[MAX_EXPONENT + 1] = {"", "k", "M", "G", "T"};
30 static const char *const custom_symbols[MAX_EXPONENT + 1] = {"", "K", "M", "G", "T"};
31
32 /*
33  * Formats bytes according to the given base and set of symbols.
34  *
35  */
36 static int format_bytes(char *outwalk, uint64_t bytes, uint64_t base, const char *const symbols[]) {
37     double size = bytes;
38     int exponent = 0;
39     while (size >= base && exponent < MAX_EXPONENT) {
40         size /= base;
41         exponent += 1;
42     }
43     return sprintf(outwalk, "%.1f %sB", size, symbols[exponent]);
44 }
45
46 /*
47  * Prints the given amount of bytes in a human readable manner.
48  *
49  */
50 static int print_bytes_human(char *outwalk, uint64_t bytes, const char *prefix_type) {
51     if (strcasecmp(prefix_type, "decimal") == 0) {
52         return format_bytes(outwalk, bytes, DECIMAL_BASE, si_symbols);
53     } else if (strcasecmp(prefix_type, "custom") == 0) {
54         return format_bytes(outwalk, bytes, BINARY_BASE, custom_symbols);
55     } else {
56         return format_bytes(outwalk, bytes, BINARY_BASE, iec_symbols);
57     }
58 }
59
60 /*
61  * Determines whether remaining bytes are below given threshold.
62  *
63  */
64 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
65 static bool below_threshold(struct statfs buf, const char *prefix_type, const char *threshold_type, const double low_threshold) {
66 #else
67 static bool below_threshold(struct statvfs buf, const char *prefix_type, const char *threshold_type, const double low_threshold) {
68 #endif
69     if (strcasecmp(threshold_type, "percentage_free") == 0) {
70         return 100.0 * (double)buf.f_bfree / (double)buf.f_blocks < low_threshold;
71     } else if (strcasecmp(threshold_type, "percentage_avail") == 0) {
72         return 100.0 * (double)buf.f_bavail / (double)buf.f_blocks < low_threshold;
73     } else if (strcasecmp(threshold_type, "bytes_free") == 0) {
74         return (double)buf.f_bsize * (double)buf.f_bfree < low_threshold;
75     } else if (strcasecmp(threshold_type, "bytes_avail") == 0) {
76         return (double)buf.f_bsize * (double)buf.f_bavail < low_threshold;
77     } else if (threshold_type[0] != '\0' && strncasecmp(threshold_type + 1, "bytes_", strlen("bytes_")) == 0) {
78         uint64_t base = strcasecmp(prefix_type, "decimal") == 0 ? DECIMAL_BASE : BINARY_BASE;
79         double factor = 1;
80
81         switch (threshold_type[0]) {
82             case 'T':
83             case 't':
84                 factor *= base;
85             case 'G':
86             case 'g':
87                 factor *= base;
88             case 'M':
89             case 'm':
90                 factor *= base;
91             case 'K':
92             case 'k':
93                 factor *= base;
94                 break;
95             default:
96                 return false;
97         }
98
99         if (strcasecmp(threshold_type + 1, "bytes_free") == 0) {
100             return (double)buf.f_bsize * (double)buf.f_bfree < low_threshold * factor;
101         } else if (strcasecmp(threshold_type + 1, "bytes_avail") == 0) {
102             return (double)buf.f_bsize * (double)buf.f_bavail < low_threshold * factor;
103         }
104     }
105
106     return false;
107 }
108
109 /*
110  * Does a statvfs and prints either free, used or total amounts of bytes in a
111  * human readable manner.
112  *
113  */
114 void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const char *format_below_threshold, const char *format_not_mounted, const char *prefix_type, const char *threshold_type, const double low_threshold) {
115     const char *selected_format = format;
116     const char *walk;
117     char *outwalk = buffer;
118     bool colorful_output = false;
119     bool mounted = false;
120
121     INSTANCE(path);
122
123 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
124     struct statfs buf;
125
126     if (statfs(path, &buf) == -1)
127         return;
128
129     mounted = true;
130 #elif defined(__NetBSD__)
131     struct statvfs buf;
132
133     if (statvfs(path, &buf) == -1)
134         return;
135
136     mounted = true;
137 #else
138     struct statvfs buf;
139
140     if (statvfs(path, &buf) == -1) {
141         /* If statvfs errors, e.g., due to the path not existing,
142          * we consider the device not mounted. */
143         mounted = false;
144     } else {
145         char *sanitized = sstrdup(path);
146         if (strlen(sanitized) > 1 && sanitized[strlen(sanitized) - 1] == '/')
147             sanitized[strlen(sanitized) - 1] = '\0';
148         FILE *mntentfile = setmntent("/etc/mtab", "r");
149         if (mntentfile == NULL) {
150             mntentfile = setmntent("/proc/mounts", "r");
151         }
152         if (mntentfile == NULL) {
153             fprintf(stderr, "i3status: files /etc/mtab and /proc/mounts aren't accessible\n");
154         } else {
155             struct mntent *m;
156
157             while ((m = getmntent(mntentfile)) != NULL) {
158                 if (strcmp(m->mnt_dir, sanitized) == 0) {
159                     mounted = true;
160                     break;
161                 }
162             }
163             endmntent(mntentfile);
164         }
165         free(sanitized);
166     }
167 #endif
168
169     if (!mounted) {
170         if (format_not_mounted == NULL)
171             format_not_mounted = "";
172         selected_format = format_not_mounted;
173     } else if (low_threshold > 0 && below_threshold(buf, prefix_type, threshold_type, low_threshold)) {
174         START_COLOR("color_bad");
175         colorful_output = true;
176         if (format_below_threshold != NULL)
177             selected_format = format_below_threshold;
178     }
179
180     for (walk = selected_format; *walk != '\0'; walk++) {
181         if (*walk != '%') {
182             *(outwalk++) = *walk;
183
184         } else if (BEGINS_WITH(walk + 1, "free")) {
185             outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bfree, prefix_type);
186             walk += strlen("free");
187
188         } else if (BEGINS_WITH(walk + 1, "used")) {
189             outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * ((uint64_t)buf.f_blocks - (uint64_t)buf.f_bfree), prefix_type);
190             walk += strlen("used");
191
192         } else if (BEGINS_WITH(walk + 1, "total")) {
193             outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_blocks, prefix_type);
194             walk += strlen("total");
195
196         } else if (BEGINS_WITH(walk + 1, "avail")) {
197             outwalk += print_bytes_human(outwalk, (uint64_t)buf.f_bsize * (uint64_t)buf.f_bavail, prefix_type);
198             walk += strlen("avail");
199
200         } else if (BEGINS_WITH(walk + 1, "percentage_free")) {
201             outwalk += sprintf(outwalk, "%.01f%s", 100.0 * (double)buf.f_bfree / (double)buf.f_blocks, pct_mark);
202             walk += strlen("percentage_free");
203
204         } else if (BEGINS_WITH(walk + 1, "percentage_used_of_avail")) {
205             outwalk += sprintf(outwalk, "%.01f%s", 100.0 * (double)(buf.f_blocks - buf.f_bavail) / (double)buf.f_blocks, pct_mark);
206             walk += strlen("percentage_used_of_avail");
207
208         } else if (BEGINS_WITH(walk + 1, "percentage_used")) {
209             outwalk += sprintf(outwalk, "%.01f%s", 100.0 * (double)(buf.f_blocks - buf.f_bfree) / (double)buf.f_blocks, pct_mark);
210             walk += strlen("percentage_used");
211
212         } else if (BEGINS_WITH(walk + 1, "percentage_avail")) {
213             outwalk += sprintf(outwalk, "%.01f%s", 100.0 * (double)buf.f_bavail / (double)buf.f_blocks, pct_mark);
214             walk += strlen("percentage_avail");
215
216         } else {
217             *(outwalk++) = '%';
218         }
219     }
220
221     if (colorful_output)
222         END_COLOR;
223
224     *outwalk = '\0';
225     OUTPUT_FULL_TEXT(buffer);
226 }