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