* Applied 12 patches:
[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-2007 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  * $Id$ */
27
28 #include "conky.h"
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <sys/types.h>
34 #include <fcntl.h>
35
36 /* linux */
37 #ifdef HAVE_SYS_STATFS_H
38 #include <sys/statfs.h>
39 #endif
40
41 /* freebsd && netbsd */
42 #ifdef HAVE_SYS_PARAM_H
43 #include <sys/param.h>
44 #endif
45 #ifdef HAVE_SYS_MOUNT_H
46 #include <sys/mount.h>
47 #endif
48
49 #ifndef HAVE_STRUCT_STATFS_F_FSTYPENAME
50 #include <mntent.h>
51 #endif
52
53 #define MAX_FS_STATS 64
54
55 static struct fs_stat fs_stats_[MAX_FS_STATS];
56 struct fs_stat *fs_stats = fs_stats_;
57
58 static void update_fs_stat(struct fs_stat *fs);
59
60 static char* get_fs_type(const char* path);
61
62 void update_fs_stats()
63 {
64         unsigned i;
65
66         for (i = 0; i < MAX_FS_STATS; ++i) {
67                 if (fs_stats[i].path) {
68                         update_fs_stat(&fs_stats[i]);
69                 }
70         }
71 }
72
73 void clear_fs_stats()
74 {
75         unsigned i;
76
77         for (i = 0; i < MAX_FS_STATS; ++i) {
78                 if (fs_stats[i].path) {
79                         free(fs_stats[i].path);
80                         free(fs_stats[i].type);
81                         fs_stats[i].path = NULL;
82                 }
83         }
84 }
85
86 struct fs_stat *prepare_fs_stat(const char *s)
87 {
88         struct fs_stat *new = 0;
89         unsigned i;
90
91         /* lookup existing or get new */
92         for (i = 0; i < MAX_FS_STATS; ++i) {
93                 if (fs_stats[i].path) {
94                         if (strcmp(fs_stats[i].path, s) == 0) {
95                                 return &fs_stats[i];
96                         }
97                 } else {
98                         new = &fs_stats[i];
99                 }
100         }
101         /* new path */
102         if (!new) {
103                 ERR("too many fs stats");
104                 return 0;
105         }
106         new->path = strdup(s);
107         update_fs_stat(new);
108         return new;
109 }
110
111 static void update_fs_stat(struct fs_stat *fs)
112 {
113         struct statfs s;
114
115         if (statfs(fs->path, &s) == 0) {
116                 fs->size = (long long) s.f_blocks * s.f_bsize;
117                 /* bfree (root) or bavail (non-roots) ? */
118                 fs->avail = (long long) s.f_bavail * s.f_bsize;
119                 fs->free = (long long) s.f_bfree * s.f_bsize;
120                 fs->type = get_fs_type(fs->path);
121         } else {
122                 fs->size = 0;
123                 fs->avail = 0;
124                 fs->free = 0;
125                 fs->type = "unknown";
126                 ERR("statfs '%s': %s", fs->path, strerror(errno));
127         }
128 }
129
130 static char* get_fs_type(const char* path)
131 {
132
133 #ifdef HAVE_STRUCT_STATFS_F_FSTYPENAME
134
135         struct statfs s;
136         if(statfs(path, &s) == 0)
137                 return s.f_fstypename;
138         else
139                 ERR("statfs '%s': %s", path, strerror(errno));
140
141 #else
142
143         /* TODO: walk up the directory tree so it works on
144          * on paths that are not actually mount points. */
145
146         FILE* mtab = setmntent( "/etc/mtab", "r" );
147
148         if(mtab == NULL) {
149                 ERR("setmntent /etc/mtab: %s", strerror(errno));
150                 return "unknown";
151         }
152
153         struct mntent* me = getmntent(mtab);
154
155         // find our path in the mtab
156         while(getmntent(mtab) && strcmp(path,me->mnt_dir));
157
158         endmntent(mtab);
159
160         if(me)
161                 return strdup(me->mnt_type);
162
163 #endif // HAVE_STRUCT_STATFS_F_FSTYPENAME
164
165         return "unknown";
166
167 }