From a27a2222a526bfc8fde95d4f59e9e93bce322bfd Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Tue, 15 Jun 2010 10:41:29 +0200 Subject: [PATCH] filesystem.[ch]: Move filesystem accessing functions into a separate module. --- Makefile | 4 +- common.c | 133 ----------------------- common.h | 13 --- filesystem.c | 299 +++++++++++++++++++++++++++++++++++++++++++++++++++ filesystem.h | 11 ++ graph_list.c | 116 +------------------- 6 files changed, 318 insertions(+), 258 deletions(-) create mode 100644 filesystem.c create mode 100644 filesystem.h diff --git a/Makefile b/Makefile index 65544e6..0b22fd7 100644 --- a/Makefile +++ b/Makefile @@ -11,6 +11,8 @@ clean: common.o: common.c common.h +filesystem.o: filesystem.c filesystem.h + graph_config.o: graph_config.c graph_config.h graph_def.o: graph_def.c graph_def.h @@ -44,7 +46,7 @@ parser.o: parser.c test: test.c utils_params.o test.fcgi: LDLIBS = -lfcgi -lrrd -test.fcgi: test.fcgi.c common.o graph_config.o graph_def.o graph_ident.o graph_instance.o graph_list.o utils_array.o utils_params.o action_graph.o action_list_graphs.o scanner.o parser.o oconfig.o +test.fcgi: test.fcgi.c common.o filesystem.o graph_config.o graph_def.o graph_ident.o graph_instance.o graph_list.o utils_array.o utils_params.o action_graph.o action_list_graphs.o scanner.o parser.o oconfig.o .PHONY: clean diff --git a/common.c b/common.c index 15fbe6a..2b6b5eb 100644 --- a/common.c +++ b/common.c @@ -19,139 +19,6 @@ #include #include -static int foreach_rrd_file (const char *dir, /* {{{ */ - int (*callback) (const char *, void *), - void *user_data) -{ - DIR *dh; - struct dirent *entry; - int status; - - if (callback == NULL) - return (EINVAL); - - dh = opendir (dir); - if (dh == NULL) - return (errno); - - while ((entry = readdir (dh)) != NULL) - { - struct stat statbuf; - char abspath[PATH_MAX + 1]; - size_t d_name_len; - - if (entry->d_name[0] == '.') - continue; - - d_name_len = strlen (entry->d_name); - if (d_name_len <= 4) - continue; - - if (strcasecmp (".rrd", entry->d_name + (d_name_len - 4)) != 0) - continue; - - snprintf (abspath, sizeof (abspath), "%s/%s", dir, entry->d_name); - abspath[sizeof (abspath) - 1] = 0; - - memset (&statbuf, 0, sizeof (statbuf)); - - status = stat (abspath, &statbuf); - if (status != 0) - continue; - - if (!S_ISREG (statbuf.st_mode)) - continue; - - entry->d_name[d_name_len - 4] = 0; - - status = (*callback) (entry->d_name, user_data); - if (status != 0) - break; - } /* while (readdir) */ - - closedir (dh); - return (status); -} /* }}} int foreach_rrd_file */ - -static int foreach_dir (const char *dir, /* {{{ */ - int (*callback) (const char *, void *), - void *user_data) -{ - DIR *dh; - struct dirent *entry; - int status; - - if (callback == NULL) - return (EINVAL); - - dh = opendir (dir); - if (dh == NULL) - return (errno); - - while ((entry = readdir (dh)) != NULL) - { - struct stat statbuf; - char abspath[PATH_MAX + 1]; - - if (entry->d_name[0] == '.') - continue; - - snprintf (abspath, sizeof (abspath), "%s/%s", dir, entry->d_name); - abspath[sizeof (abspath) - 1] = 0; - - memset (&statbuf, 0, sizeof (statbuf)); - - status = stat (abspath, &statbuf); - if (status != 0) - continue; - - if (!S_ISDIR (statbuf.st_mode)) - continue; - - status = (*callback) (entry->d_name, user_data); - if (status != 0) - break; - } /* while (readdir) */ - - closedir (dh); - return (status); -} /* }}} int foreach_dir */ - -int foreach_type (const char *host, const char *plugin, /* {{{ */ - callback_type_t callback, void *user_data) -{ - char abspath[PATH_MAX + 1]; - - if ((host == NULL) || (plugin == NULL)) - return (EINVAL); - - snprintf (abspath, sizeof (abspath), "%s/%s/%s", DATA_DIR, host, plugin); - abspath[sizeof (abspath) - 1] = 0; - - return (foreach_rrd_file (abspath, callback, user_data)); -} /* }}} int foreach_type */ - -int foreach_plugin (const char *host, /* {{{ */ - callback_plugin_t callback, - void *user_data) -{ - char abspath[PATH_MAX + 1]; - - if (host == NULL) - return (EINVAL); - - snprintf (abspath, sizeof (abspath), "%s/%s", DATA_DIR, host); - abspath[sizeof (abspath) - 1] = 0; - - return (foreach_dir (abspath, callback, user_data)); -} /* }}} int foreach_plugin */ - -int foreach_host (callback_host_t callback, /* {{{ */ - void *user_data) -{ - return (foreach_dir (DATA_DIR, callback, user_data)); -} /* }}} int foreach_host */ - size_t c_strlcat (char *dst, const char *src, size_t size) /* {{{ */ { size_t retval; diff --git a/common.h b/common.h index f70d385..2b5662f 100644 --- a/common.h +++ b/common.h @@ -4,14 +4,6 @@ #include #include -#define DATA_DIR "/var/lib/collectd/rrd" - -#include "graph_list.h" - -typedef int (*callback_type_t) (const char *type, void *user_data); -typedef int (*callback_plugin_t) (const char *plugin, void *user_data); -typedef int (*callback_host_t) (const char *host, void *user_data); - int print_debug (const char *format, ...) __attribute__((format(printf,1,2))); #if 0 @@ -20,11 +12,6 @@ int print_debug (const char *format, ...) # define DEBUG(...) /**/ #endif -int foreach_type (const char *host, const char *plugin, - callback_type_t, void *user_data); -int foreach_plugin (const char *host, callback_plugin_t, void *user_data); -int foreach_host (callback_host_t, void *user_data); - size_t c_strlcat (char *dst, const char *src, size_t size); #define strlcat c_strlcat diff --git a/filesystem.c b/filesystem.c new file mode 100644 index 0000000..ba57249 --- /dev/null +++ b/filesystem.c @@ -0,0 +1,299 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define DATA_DIR "/var/lib/collectd/rrd" + +#include "filesystem.h" + +struct fs_scan_dir_data_s /* {{{ */ +{ + fs_ident_cb_t callback; + void *user_data; + + char *host; + char *plugin; + char *plugin_instance; + char *type; + char *type_instance; +}; /* }}} */ +typedef struct fs_scan_dir_data_s fs_scan_dir_data_t; + +typedef int (*callback_type_t) (const char *type, void *user_data); +typedef int (*callback_plugin_t) (const char *plugin, void *user_data); +typedef int (*callback_host_t) (const char *host, void *user_data); + +/* + * Directory and file walking functions + */ +static int foreach_rrd_file (const char *dir, /* {{{ */ + int (*callback) (const char *, void *), + void *user_data) +{ + DIR *dh; + struct dirent *entry; + int status; + + if (callback == NULL) + return (EINVAL); + + dh = opendir (dir); + if (dh == NULL) + return (errno); + + while ((entry = readdir (dh)) != NULL) + { + struct stat statbuf; + char abspath[PATH_MAX + 1]; + size_t d_name_len; + + if (entry->d_name[0] == '.') + continue; + + d_name_len = strlen (entry->d_name); + if (d_name_len <= 4) + continue; + + if (strcasecmp (".rrd", entry->d_name + (d_name_len - 4)) != 0) + continue; + + snprintf (abspath, sizeof (abspath), "%s/%s", dir, entry->d_name); + abspath[sizeof (abspath) - 1] = 0; + + memset (&statbuf, 0, sizeof (statbuf)); + + status = stat (abspath, &statbuf); + if (status != 0) + continue; + + if (!S_ISREG (statbuf.st_mode)) + continue; + + entry->d_name[d_name_len - 4] = 0; + + status = (*callback) (entry->d_name, user_data); + if (status != 0) + break; + } /* while (readdir) */ + + closedir (dh); + return (status); +} /* }}} int foreach_rrd_file */ + +static int foreach_dir (const char *dir, /* {{{ */ + int (*callback) (const char *, void *), + void *user_data) +{ + DIR *dh; + struct dirent *entry; + int status; + + if (callback == NULL) + return (EINVAL); + + dh = opendir (dir); + if (dh == NULL) + return (errno); + + while ((entry = readdir (dh)) != NULL) + { + struct stat statbuf; + char abspath[PATH_MAX + 1]; + + if (entry->d_name[0] == '.') + continue; + + snprintf (abspath, sizeof (abspath), "%s/%s", dir, entry->d_name); + abspath[sizeof (abspath) - 1] = 0; + + memset (&statbuf, 0, sizeof (statbuf)); + + status = stat (abspath, &statbuf); + if (status != 0) + continue; + + if (!S_ISDIR (statbuf.st_mode)) + continue; + + status = (*callback) (entry->d_name, user_data); + if (status != 0) + break; + } /* while (readdir) */ + + closedir (dh); + return (status); +} /* }}} int foreach_dir */ + +static int foreach_type (const char *host, const char *plugin, /* {{{ */ + callback_type_t callback, void *user_data) +{ + char abspath[PATH_MAX + 1]; + + if ((host == NULL) || (plugin == NULL)) + return (EINVAL); + + snprintf (abspath, sizeof (abspath), "%s/%s/%s", DATA_DIR, host, plugin); + abspath[sizeof (abspath) - 1] = 0; + + return (foreach_rrd_file (abspath, callback, user_data)); +} /* }}} int foreach_type */ + +static int foreach_plugin (const char *host, /* {{{ */ + callback_plugin_t callback, + void *user_data) +{ + char abspath[PATH_MAX + 1]; + + if (host == NULL) + return (EINVAL); + + snprintf (abspath, sizeof (abspath), "%s/%s", DATA_DIR, host); + abspath[sizeof (abspath) - 1] = 0; + + return (foreach_dir (abspath, callback, user_data)); +} /* }}} int foreach_plugin */ + +static int foreach_host (callback_host_t callback, /* {{{ */ + void *user_data) +{ + return (foreach_dir (DATA_DIR, callback, user_data)); +} /* }}} int foreach_host */ + +/* + * Functions building "fs_scan_dir_data_t" and calling the user-supplied + * callback eventually. + */ +static int scan_type (const char *type, void *user_data) /* {{{ */ +{ + fs_scan_dir_data_t *data = user_data; + graph_ident_t *ident; + int status; + + if ((type == NULL) || (data == NULL)) + return (EINVAL); + + if ((data->type != NULL) || (data->type_instance != NULL)) + return (EINVAL); + + data->type = strdup (type); + if (data->type == NULL) + return (ENOMEM); + + data->type_instance = strchr (data->type, '-'); + if (data->type_instance != NULL) + { + *data->type_instance = 0; + data->type_instance++; + } + else + { + data->type_instance = data->type + strlen (data->type); + } + + ident = ident_create (data->host, + data->plugin, data->plugin_instance, + data->type, data->type_instance); + if (ident == NULL) + { + status = -1; + } + else + { + status = (*data->callback) (ident, data->user_data); + ident_destroy (ident); + } + + free (data->type); + data->type = NULL; + data->type_instance = NULL; + + return (status); +} /* }}} int scan_type */ + +static int scan_plugin (const char *plugin, void *user_data) /* {{{ */ +{ + fs_scan_dir_data_t *data = user_data; + int status; + + if ((plugin == NULL) || (data == NULL)) + return (EINVAL); + + if ((data->plugin != NULL) || (data->plugin_instance != NULL)) + return (EINVAL); + + data->plugin = strdup (plugin); + if (data->plugin == NULL) + return (ENOMEM); + + data->plugin_instance = strchr (data->plugin, '-'); + if (data->plugin_instance != NULL) + { + *data->plugin_instance = 0; + data->plugin_instance++; + } + else + { + data->plugin_instance = data->plugin + strlen (data->plugin); + } + + status = foreach_type (data->host, plugin, scan_type, data); + + free (data->plugin); + data->plugin = NULL; + data->plugin_instance = NULL; + + return (status); +} /* }}} int scan_plugin */ + +static int scan_host (const char *host, void *user_data) /* {{{ */ +{ + fs_scan_dir_data_t *data = user_data; + int status; + + if ((host == NULL) || (data == NULL)) + return (EINVAL); + + if (data->host != NULL) + return (EINVAL); + + data->host = strdup (host); + if (data->host == NULL) + return (ENOMEM); + + status = foreach_plugin (host, scan_plugin, data); + + free (data->host); + data->host = NULL; + + return (status); +} /* }}} int scan_host */ + +/* + * Public function + */ +int fs_scan (fs_ident_cb_t callback, void *user_data) /* {{{ */ +{ + fs_scan_dir_data_t data; + + memset (&data, 0, sizeof (data)); + data.callback = callback; + data.user_data = user_data; + + data.host = NULL; + data.plugin = NULL; + data.plugin_instance = NULL; + data.type = NULL; + data.type_instance = NULL; + + foreach_host (scan_host, &data); + + return (0); +} /* }}} int fs_scan */ + +/* vim: set sw=2 sts=2 et fdm=marker : */ diff --git a/filesystem.h b/filesystem.h new file mode 100644 index 0000000..49798a0 --- /dev/null +++ b/filesystem.h @@ -0,0 +1,11 @@ +#ifndef FILESYSTEM_G +#define FILESYSTEM_G 1 + +#include "graph_ident.h" + +typedef int (*fs_ident_cb_t) (const graph_ident_t *ident, void *user_data); + +int fs_scan (fs_ident_cb_t callback, void *user_data); + +#endif /* FILESYSTEM_G */ +/* vim: set sw=2 sts=2 et fdm=marker : */ diff --git a/graph_list.c b/graph_list.c index 5b37f2d..a5df4cb 100644 --- a/graph_list.c +++ b/graph_list.c @@ -12,6 +12,7 @@ #include "graph_def.h" #include "graph_config.h" #include "common.h" +#include "filesystem.h" #include "utils_params.h" #include @@ -183,7 +184,8 @@ static void graph_destroy (graph_config_t *cfg) /* {{{ */ graph_destroy (next); } /* }}} void graph_destroy */ -static int register_file (const graph_ident_t *file) /* {{{ */ +static int gl_register_file (const graph_ident_t *file, /* {{{ */ + __attribute__((unused)) void *user_data) { graph_config_t *cfg; int num_graphs = 0; @@ -214,115 +216,7 @@ static int register_file (const graph_ident_t *file) /* {{{ */ } return (0); -} /* }}} int register_file */ - -static int callback_type (const char *type, void *user_data) /* {{{ */ -{ - gl_ident_stage_t *gl; - graph_ident_t *ident; - int status; - - if ((type == NULL) || (user_data == NULL)) - return (EINVAL); - - gl = user_data; - if ((gl->type != NULL) || (gl->type_instance != NULL)) - return (EINVAL); - - gl->type = strdup (type); - if (gl->type == NULL) - return (ENOMEM); - - gl->type_instance = strchr (gl->type, '-'); - if (gl->type_instance != NULL) - { - *gl->type_instance = 0; - gl->type_instance++; - } - else - { - gl->type_instance = gl->type + strlen (gl->type); - } - - ident = ident_create (gl->host, - gl->plugin, gl->plugin_instance, - gl->type, gl->type_instance); - if (ident == 0) - { - status = -1; - } - else - { - status = register_file (ident); - ident_destroy (ident); - } - - free (gl->type); - gl->type = NULL; - gl->type_instance = NULL; - - return (status); -} /* }}} int callback_type */ - -static int callback_plugin (const char *plugin, void *user_data) /* {{{ */ -{ - gl_ident_stage_t *gl; - int status; - - if ((plugin == NULL) || (user_data == NULL)) - return (EINVAL); - - gl = user_data; - if ((gl->plugin != NULL) || (gl->plugin_instance != NULL)) - return (EINVAL); - - gl->plugin = strdup (plugin); - if (gl->plugin == NULL) - return (ENOMEM); - - gl->plugin_instance = strchr (gl->plugin, '-'); - if (gl->plugin_instance != NULL) - { - *gl->plugin_instance = 0; - gl->plugin_instance++; - } - else - { - gl->plugin_instance = gl->plugin + strlen (gl->plugin); - } - - status = foreach_type (gl->host, plugin, callback_type, gl); - - free (gl->plugin); - gl->plugin = NULL; - gl->plugin_instance = NULL; - - return (status); -} /* }}} int callback_plugin */ - -static int callback_host (const char *host, void *user_data) /* {{{ */ -{ - gl_ident_stage_t *gl; - int status; - - if ((host == NULL) || (user_data == NULL)) - return (EINVAL); - - gl = user_data; - if (gl->host != NULL) - return (EINVAL); - - gl->host = strdup (host); - if (gl->host == NULL) - return (ENOMEM); - - status = foreach_plugin (host, callback_plugin, gl); - - free (gl->host); - gl->host = NULL; - - return (status); -} /* }}} int callback_host */ +} /* }}} int gl_register_file */ static const char *get_part_from_param (const char *prim_key, /* {{{ */ const char *sec_key) @@ -631,7 +525,7 @@ int gl_update (void) /* {{{ */ gl.type_instance = NULL; gl_clear_instances (); - status = foreach_host (callback_host, &gl); + status = fs_scan (/* callback = */ gl_register_file, /* user data = */ NULL); gl_last_update = now; -- 2.30.2