fs_bar, fs_bar_free: convert to generic object payload
[monky] / src / fs.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  * vim: ts=4 sw=4 noet ai cindent syntax=c
3  *
4  * Conky, a system monitor, based on torsmo
5  *
6  * Any original torsmo code is licensed under the BSD license
7  *
8  * All code written since the fork of torsmo is licensed under the GPL
9  *
10  * Please see COPYING for details
11  *
12  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
14  *      (see AUTHORS)
15  * All rights reserved.
16  *
17  * This program is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  * You should have received a copy of the GNU General Public License
27  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28  *
29  */
30
31 #include "conky.h"
32 #include "logging.h"
33 #include "fs.h"
34 #include "specials.h"
35 #include "text_object.h"
36 #include <ctype.h>
37 #include <unistd.h>
38 #include <errno.h>
39 #include <sys/types.h>
40 #include <fcntl.h>
41
42 /* linux */
43 #ifdef HAVE_SYS_STATFS_H
44 #include <sys/statfs.h>
45 #endif
46
47 /* freebsd && netbsd */
48 #ifdef HAVE_SYS_PARAM_H
49 #include <sys/param.h>
50 #endif
51 #ifdef HAVE_SYS_MOUNT_H
52 #include <sys/mount.h>
53 #endif
54
55 #if !defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) && !defined (__OpenBSD__) && !defined(__FreeBSD__)
56 #include <mntent.h>
57 #endif
58
59 #define MAX_FS_STATS 64
60
61 struct fsbar {
62         struct fs_stat *fs;
63         int w, h;
64 };
65
66 static struct fs_stat fs_stats_[MAX_FS_STATS];
67 struct fs_stat *fs_stats = fs_stats_;
68
69 static void update_fs_stat(struct fs_stat *fs);
70
71 void get_fs_type(const char *path, char *result);
72
73 void update_fs_stats(void)
74 {
75         unsigned i;
76         static double last_fs_update = 0.0;
77
78         if (current_update_time - last_fs_update < 13)
79                 return;
80
81         for (i = 0; i < MAX_FS_STATS; ++i) {
82                 if (fs_stats[i].set) {
83                         update_fs_stat(&fs_stats[i]);
84                 }
85         }
86         last_fs_update = current_update_time;
87 }
88
89 void clear_fs_stats(void)
90 {
91         unsigned i;
92         for (i = 0; i < MAX_FS_STATS; ++i) {
93                 memset(&fs_stats[i], 0, sizeof(struct fs_stat));
94         }
95 }
96
97 struct fs_stat *prepare_fs_stat(const char *s)
98 {
99         struct fs_stat *new = 0;
100         unsigned i;
101
102         /* lookup existing or get new */
103         for (i = 0; i < MAX_FS_STATS; ++i) {
104                 if (fs_stats[i].set) {
105                         if (strncmp(fs_stats[i].path, s, DEFAULT_TEXT_BUFFER_SIZE) == 0) {
106                                 return &fs_stats[i];
107                         }
108                 } else {
109                         new = &fs_stats[i];
110                 }
111         }
112         /* new path */
113         if (!new) {
114                 NORM_ERR("too many fs stats");
115                 return 0;
116         }
117         strncpy(new->path, s, DEFAULT_TEXT_BUFFER_SIZE);
118         new->set = 1;
119         update_fs_stat(new);
120         return new;
121 }
122
123 static void update_fs_stat(struct fs_stat *fs)
124 {
125         struct statfs s;
126
127         if (statfs(fs->path, &s) == 0) {
128                 fs->size = (long long)s.f_blocks * s.f_bsize;
129                 /* bfree (root) or bavail (non-roots) ? */
130                 fs->avail = (long long)s.f_bavail * s.f_bsize;
131                 fs->free = (long long)s.f_bfree * s.f_bsize;
132                 get_fs_type(fs->path, fs->type);
133         } else {
134                 fs->size = 0;
135                 fs->avail = 0;
136                 fs->free = 0;
137                 strncpy(fs->type, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
138                 NORM_ERR("statfs '%s': %s", fs->path, strerror(errno));
139         }
140 }
141
142 void get_fs_type(const char *path, char *result)
143 {
144
145 #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) || defined(__FreeBSD__) || defined (__OpenBSD__)
146
147         struct statfs s;
148         if (statfs(path, &s) == 0) {
149                 strncpy(result, s.f_fstypename, DEFAULT_TEXT_BUFFER_SIZE);
150         } else {
151                 NORM_ERR("statfs '%s': %s", path, strerror(errno));
152         }
153         return;
154
155 #else                           /* HAVE_STRUCT_STATFS_F_FSTYPENAME */
156
157         struct mntent *me;
158         FILE *mtab = setmntent("/etc/mtab", "r");
159         char *search_path;
160         int match;
161         char *slash;
162
163         if (mtab == NULL) {
164                 NORM_ERR("setmntent /etc/mtab: %s", strerror(errno));
165                 strncpy(result, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
166                 return;
167         }
168
169         me = getmntent(mtab);
170
171         // find our path in the mtab
172         search_path = strdup(path);
173         do {
174                 while ((match = strcmp(search_path, me->mnt_dir))
175                                 && getmntent(mtab));
176                 if (!match)
177                         break;
178                 fseek(mtab, 0, SEEK_SET);
179                 slash = strrchr(search_path, '/');
180                 if (slash == NULL)
181                         CRIT_ERR(NULL, NULL, "invalid path '%s'", path);
182                 if (strlen(slash) == 1)         /* trailing slash */
183                         *(slash) = '\0';
184                 else if (strlen(slash) > 1)
185                         *(slash + 1) = '\0';
186                 else
187                         CRIT_ERR(NULL, NULL, "found a crack in the matrix!");
188         } while (strlen(search_path) > 0);
189         free(search_path);
190
191         endmntent(mtab);
192
193         if (me && !match) {
194                 strncpy(result, me->mnt_type, DEFAULT_TEXT_BUFFER_SIZE);
195                 return;
196         }
197 #endif                          /* HAVE_STRUCT_STATFS_F_FSTYPENAME */
198
199         strncpy(result, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
200
201 }
202
203 void init_fs_bar(struct text_object *obj, const char *arg)
204 {
205         struct fsbar *fb;
206
207         fb = malloc(sizeof(struct fsbar));
208         memset(fb, 0, sizeof(struct fsbar));
209
210         fb->w = default_bar_width;
211         fb->h = default_bar_height;
212
213         arg = scan_bar(arg, &fb->w, &fb->h);
214         if (arg) {
215                 while (isspace(*arg)) {
216                         arg++;
217                 }
218                 if (*arg == '\0') {
219                         arg = "/";
220                 }
221         } else {
222                 arg = "/";
223         }
224         fb->fs = prepare_fs_stat(arg);
225         obj->data.opaque = fb;
226 }
227
228 void print_fs_bar(struct text_object *obj, int be_free_bar, char *p, int p_max_size)
229 {
230         double val = 1.0;
231         struct fsbar *fb = obj->data.opaque;
232
233         if (!fb || !fb->fs)
234                 return;
235
236         if (fb->fs->size)
237                 val = (double)fb->fs->avail / (double)fb->fs->size;
238
239         if (!be_free_bar)
240                 val = 1.0 - val;
241
242 #ifdef X11
243                 if(output_methods & TO_X) {
244                         new_bar(p, fb->w, fb->h, (int)(255 * val));
245                 }else
246 #endif /* X11 */
247                 {
248                         if(!fb->w) fb->w = DEFAULT_BAR_WIDTH_NO_X;
249                         new_bar_in_shell(p, p_max_size, (int)(100 * val), fb->w);
250                 }
251 }