Removing old svn keywords.
[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-2008 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  */
27
28 #include "conky.h"
29 #include <unistd.h>
30 #include <errno.h>
31 #include <sys/types.h>
32 #include <fcntl.h>
33
34 /* linux */
35 #ifdef HAVE_SYS_STATFS_H
36 #include <sys/statfs.h>
37 #endif
38
39 /* freebsd && netbsd */
40 #ifdef HAVE_SYS_PARAM_H
41 #include <sys/param.h>
42 #endif
43 #ifdef HAVE_SYS_MOUNT_H
44 #include <sys/mount.h>
45 #endif
46
47 #if !defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) && !defined (__OpenBSD__) && !defined(__FreeBSD__)
48 #include <mntent.h>
49 #endif
50
51 #define MAX_FS_STATS 64
52
53 static struct fs_stat fs_stats_[MAX_FS_STATS];
54 struct fs_stat *fs_stats = fs_stats_;
55
56 static void update_fs_stat(struct fs_stat *fs);
57
58 void get_fs_type(const char *path, char *result);
59
60 void update_fs_stats(void)
61 {
62         unsigned i;
63
64         for (i = 0; i < MAX_FS_STATS; ++i) {
65                 if (fs_stats[i].set) {
66                         update_fs_stat(&fs_stats[i]);
67                 }
68         }
69 }
70
71 void clear_fs_stats(void)
72 {
73         unsigned i;
74         for (i = 0; i < MAX_FS_STATS; ++i) {
75                 memset(&fs_stats[i], 0, sizeof(struct fs_stat));
76         }
77 }
78
79 struct fs_stat *prepare_fs_stat(const char *s)
80 {
81         struct fs_stat *new = 0;
82         unsigned i;
83
84         /* lookup existing or get new */
85         for (i = 0; i < MAX_FS_STATS; ++i) {
86                 if (fs_stats[i].set) {
87                         if (strncmp(fs_stats[i].path, s, DEFAULT_TEXT_BUFFER_SIZE) == 0) {
88                                 return &fs_stats[i];
89                         }
90                 } else {
91                         new = &fs_stats[i];
92                 }
93         }
94         /* new path */
95         if (!new) {
96                 ERR("too many fs stats");
97                 return 0;
98         }
99         strncpy(new->path, s, DEFAULT_TEXT_BUFFER_SIZE);
100         new->set = 1;
101         update_fs_stat(new);
102         return new;
103 }
104
105 static void update_fs_stat(struct fs_stat *fs)
106 {
107         struct statfs s;
108
109         if (statfs(fs->path, &s) == 0) {
110                 fs->size = (long long)s.f_blocks * s.f_bsize;
111                 /* bfree (root) or bavail (non-roots) ? */
112                 fs->avail = (long long)s.f_bavail * s.f_bsize;
113                 fs->free = (long long)s.f_bfree * s.f_bsize;
114                 get_fs_type(fs->path, fs->type);
115         } else {
116                 fs->size = 0;
117                 fs->avail = 0;
118                 fs->free = 0;
119                 strncpy(fs->type, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
120                 ERR("statfs '%s': %s", fs->path, strerror(errno));
121         }
122 }
123
124 void get_fs_type(const char *path, char *result)
125 {
126
127 #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) || defined(__FreeBSD__) || defined (__OpenBSD__)
128
129         struct statfs s;
130         if (statfs(path, &s) == 0) {
131                 strncpy(result, s.f_fstypename, DEFAULT_TEXT_BUFFER_SIZE);
132         } else {
133                 ERR("statfs '%s': %s", path, strerror(errno));
134         }
135         return;
136
137 #else                           /* HAVE_STRUCT_STATFS_F_FSTYPENAME */
138
139         struct mntent *me;
140         FILE *mtab = setmntent("/etc/mtab", "r");
141         char *search_path;
142         int match;
143         char *slash;
144
145         if (mtab == NULL) {
146                 ERR("setmntent /etc/mtab: %s", strerror(errno));
147                 strncpy(result, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
148                 return;
149         }
150
151         me = getmntent(mtab);
152
153         // find our path in the mtab
154         search_path = strdup(path);
155         do {
156                 while ((match = strcmp(search_path, me->mnt_dir))
157                                 && getmntent(mtab));
158                 if (!match)
159                         break;
160                 fseek(mtab, 0, SEEK_SET);
161                 slash = strrchr(search_path, '/');
162                 if (slash == NULL)
163                         CRIT_ERR("invalid path '%s'", path);
164                 if (strlen(slash) == 1)         /* trailing slash */
165                         *(slash) = '\0';
166                 else if (strlen(slash) > 1)
167                         *(slash + 1) = '\0';
168                 else
169                         CRIT_ERR("found a crack in the matrix!");
170         } while (strlen(search_path) > 0);
171         free(search_path);
172
173         endmntent(mtab);
174
175         if (me && !match) {
176                 strncpy(result, me->mnt_type, DEFAULT_TEXT_BUFFER_SIZE);
177                 return;
178         }
179 #endif                          /* HAVE_STRUCT_STATFS_F_FSTYPENAME */
180
181         strncpy(result, "unknown", DEFAULT_TEXT_BUFFER_SIZE);
182
183 }