11 #include "filesystem.h"
13 struct fs_scan_dir_data_s /* {{{ */
15 fs_ident_cb_t callback;
20 char *plugin_instance;
24 typedef struct fs_scan_dir_data_s fs_scan_dir_data_t;
26 typedef int (*callback_type_t) (const char *type, void *user_data);
27 typedef int (*callback_plugin_t) (const char *plugin, void *user_data);
28 typedef int (*callback_host_t) (const char *host, void *user_data);
31 * Directory and file walking functions
33 static int foreach_rrd_file (const char *dir, /* {{{ */
34 int (*callback) (const char *, void *),
48 while ((entry = readdir (dh)) != NULL)
51 char abspath[PATH_MAX + 1];
54 if (entry->d_name[0] == '.')
57 d_name_len = strlen (entry->d_name);
61 if (strcasecmp (".rrd", entry->d_name + (d_name_len - 4)) != 0)
64 snprintf (abspath, sizeof (abspath), "%s/%s", dir, entry->d_name);
65 abspath[sizeof (abspath) - 1] = 0;
67 memset (&statbuf, 0, sizeof (statbuf));
69 status = stat (abspath, &statbuf);
73 if (!S_ISREG (statbuf.st_mode))
76 entry->d_name[d_name_len - 4] = 0;
78 status = (*callback) (entry->d_name, user_data);
81 } /* while (readdir) */
85 } /* }}} int foreach_rrd_file */
87 static int foreach_dir (const char *dir, /* {{{ */
88 int (*callback) (const char *, void *),
102 while ((entry = readdir (dh)) != NULL)
105 char abspath[PATH_MAX + 1];
107 if (entry->d_name[0] == '.')
110 snprintf (abspath, sizeof (abspath), "%s/%s", dir, entry->d_name);
111 abspath[sizeof (abspath) - 1] = 0;
113 memset (&statbuf, 0, sizeof (statbuf));
115 status = stat (abspath, &statbuf);
119 if (!S_ISDIR (statbuf.st_mode))
122 status = (*callback) (entry->d_name, user_data);
125 } /* while (readdir) */
129 } /* }}} int foreach_dir */
131 static int foreach_type (const char *host, const char *plugin, /* {{{ */
132 callback_type_t callback, void *user_data)
134 char abspath[PATH_MAX + 1];
136 if ((host == NULL) || (plugin == NULL))
139 snprintf (abspath, sizeof (abspath), "%s/%s/%s", DATA_DIR, host, plugin);
140 abspath[sizeof (abspath) - 1] = 0;
142 return (foreach_rrd_file (abspath, callback, user_data));
143 } /* }}} int foreach_type */
145 static int foreach_plugin (const char *host, /* {{{ */
146 callback_plugin_t callback,
149 char abspath[PATH_MAX + 1];
154 snprintf (abspath, sizeof (abspath), "%s/%s", DATA_DIR, host);
155 abspath[sizeof (abspath) - 1] = 0;
157 return (foreach_dir (abspath, callback, user_data));
158 } /* }}} int foreach_plugin */
160 static int foreach_host (callback_host_t callback, /* {{{ */
163 return (foreach_dir (DATA_DIR, callback, user_data));
164 } /* }}} int foreach_host */
167 * Functions building "fs_scan_dir_data_t" and calling the user-supplied
168 * callback eventually.
170 static int scan_type (const char *type, void *user_data) /* {{{ */
172 fs_scan_dir_data_t *data = user_data;
173 graph_ident_t *ident;
176 if ((type == NULL) || (data == NULL))
179 if ((data->type != NULL) || (data->type_instance != NULL))
182 data->type = strdup (type);
183 if (data->type == NULL)
186 data->type_instance = strchr (data->type, '-');
187 if (data->type_instance != NULL)
189 *data->type_instance = 0;
190 data->type_instance++;
194 data->type_instance = data->type + strlen (data->type);
197 ident = ident_create (data->host,
198 data->plugin, data->plugin_instance,
199 data->type, data->type_instance);
206 status = (*data->callback) (ident, data->user_data);
207 ident_destroy (ident);
212 data->type_instance = NULL;
215 } /* }}} int scan_type */
217 static int scan_plugin (const char *plugin, void *user_data) /* {{{ */
219 fs_scan_dir_data_t *data = user_data;
222 if ((plugin == NULL) || (data == NULL))
225 if ((data->plugin != NULL) || (data->plugin_instance != NULL))
228 data->plugin = strdup (plugin);
229 if (data->plugin == NULL)
232 data->plugin_instance = strchr (data->plugin, '-');
233 if (data->plugin_instance != NULL)
235 *data->plugin_instance = 0;
236 data->plugin_instance++;
240 data->plugin_instance = data->plugin + strlen (data->plugin);
243 status = foreach_type (data->host, plugin, scan_type, data);
247 data->plugin_instance = NULL;
250 } /* }}} int scan_plugin */
252 static int scan_host (const char *host, void *user_data) /* {{{ */
254 fs_scan_dir_data_t *data = user_data;
257 if ((host == NULL) || (data == NULL))
260 if (data->host != NULL)
263 data->host = strdup (host);
264 if (data->host == NULL)
267 status = foreach_plugin (host, scan_plugin, data);
273 } /* }}} int scan_host */
278 int fs_scan (fs_ident_cb_t callback, void *user_data) /* {{{ */
280 fs_scan_dir_data_t data;
282 memset (&data, 0, sizeof (data));
283 data.callback = callback;
284 data.user_data = user_data;
288 data.plugin_instance = NULL;
290 data.type_instance = NULL;
292 foreach_host (scan_host, &data);
295 } /* }}} int fs_scan */
297 /* vim: set sw=2 sts=2 et fdm=marker : */