Add vim modelines.
[monky] / src / fs.c
1 /* Conky, a system monitor, based on torsmo
2  *
3  * Any original torsmo code is licensed under the BSD license
4  *
5  * All code written since the fork of torsmo is licensed under the GPL
6  *
7  * Please see COPYING for details
8  *
9  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
10  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
11  *      (see AUTHORS)
12  * All rights reserved.
13  *
14  * This program is free software: you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation, either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  * You should have received a copy of the GNU General Public License
24  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25  *
26  * vim: ts=4 sw=4 noet ai cindent syntax=c
27  *
28  */
29
30 #include "conky.h"
31 #include "logging.h"
32 #include "fs.h"
33 #include <unistd.h>
34 #include <errno.h>
35 #include <sys/types.h>
36 #include <fcntl.h>
37
38 /* linux */
39 #ifdef HAVE_SYS_STATFS_H
40 #include <sys/statfs.h>
41 #endif
42
43 /* freebsd && netbsd */
44 #ifdef HAVE_SYS_PARAM_H
45 #include <sys/param.h>
46 #endif
47 #ifdef HAVE_SYS_MOUNT_H
48 #include <sys/mount.h>
49 #endif
50
51 #if !defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) && !defined (__OpenBSD__) && !defined(__FreeBSD__)
52 #include <mntent.h>
53 #endif
54
55 #define MAX_FS_STATS 64
56
57 static struct fs_stat fs_stats_[MAX_FS_STATS];
58 struct fs_stat *fs_stats = fs_stats_;
59
60 static void update_fs_stat(struct fs_stat *fs);
61
62 void get_fs_type(const char *path, char *result);
63
64 void update_fs_stats(void)
65 {
66         unsigned i;
67
68         for (i = 0; i < MAX_FS_STATS; ++i) {
69                 if (fs_stats[i].set) {
70                         update_fs_stat(&fs_stats[i]);
71                 }
72         }
73 }
74
75 void clear_fs_stats(void)
76 {
77         unsigned i;
78         for (i = 0; i < MAX_FS_STATS; ++i) {
79                 memset(&fs_stats[i], 0, sizeof(struct fs_stat));
80         }
81 }
82
83 struct fs_stat *prepare_fs_stat(const char *s)
84 {
85         struct fs_stat *new = 0;
86         unsigned i;
87
88         /* lookup existing or get new */
89         for (i = 0; i < MAX_FS_STATS; ++i) {
90                 if (fs_stats[i].set) {
91                         if (strncmp(fs_stats[i].path, s, DEFAULT_TEXT_BUFFER_SIZE) == 0) {
92                                 return &fs_stats[i];
93                         }
94                 } else {
95                         new = &fs_stats[i];
96                 }
97         }
98         /* new path */
99         if (!new) {
100                 ERR("too many fs stats");
101                 return 0;
102         }
103         strncpy(new->path, s, DEFAULT_TEXT_BUFFER_SIZE);
104         new->set = 1;
105         update_fs_stat(new);
106         return new;
107 }
108
109 static void update_fs_stat(struct fs_stat *fs)
110 {
111         struct statfs s;
112
113         if (statfs(fs->path, &s) == 0) {
114                 fs->size = (long long)s.f_blocks * s.f_bsize;
115                 /* bfree (root) or bavail (non-roots) ? */
116                 fs->avail = (long long)s.f_bavail * s.f_bsize;
117                 fs->free = (long long)s.f_bfree * s.f_bsize;
118                 get_fs_type(fs->path, fs->type);
119         } else {
120                 fs->size = 0;
121                 fs->avail = 0;
122                 fs->free = 0;
123                 strncpy(fs->type, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
124                 ERR("statfs '%s': %s", fs->path, strerror(errno));
125         }
126 }
127
128 void get_fs_type(const char *path, char *result)
129 {
130
131 #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) || defined(__FreeBSD__) || defined (__OpenBSD__)
132
133         struct statfs s;
134         if (statfs(path, &s) == 0) {
135                 strncpy(result, s.f_fstypename, DEFAULT_TEXT_BUFFER_SIZE);
136         } else {
137                 ERR("statfs '%s': %s", path, strerror(errno));
138         }
139         return;
140
141 #else                           /* HAVE_STRUCT_STATFS_F_FSTYPENAME */
142
143         struct mntent *me;
144         FILE *mtab = setmntent("/etc/mtab", "r");
145         char *search_path;
146         int match;
147         char *slash;
148
149         if (mtab == NULL) {
150                 ERR("setmntent /etc/mtab: %s", strerror(errno));
151                 strncpy(result, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
152                 return;
153         }
154
155         me = getmntent(mtab);
156
157         // find our path in the mtab
158         search_path = strdup(path);
159         do {
160                 while ((match = strcmp(search_path, me->mnt_dir))
161                                 && getmntent(mtab));
162                 if (!match)
163                         break;
164                 fseek(mtab, 0, SEEK_SET);
165                 slash = strrchr(search_path, '/');
166                 if (slash == NULL)
167                         CRIT_ERR(NULL, NULL, "invalid path '%s'", path);
168                 if (strlen(slash) == 1)         /* trailing slash */
169                         *(slash) = '\0';
170                 else if (strlen(slash) > 1)
171                         *(slash + 1) = '\0';
172                 else
173                         CRIT_ERR(NULL, NULL, "found a crack in the matrix!");
174         } while (strlen(search_path) > 0);
175         free(search_path);
176
177         endmntent(mtab);
178
179         if (me && !match) {
180                 strncpy(result, me->mnt_type, DEFAULT_TEXT_BUFFER_SIZE);
181                 return;
182         }
183 #endif                          /* HAVE_STRUCT_STATFS_F_FSTYPENAME */
184
185         strncpy(result, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
186
187 }