summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: dfabd77)
raw | patch | inline | side by side (parent: dfabd77)
author | Florian Forster <ff@octo.it> | |
Mon, 14 Jun 2010 21:46:37 +0000 (23:46 +0200) | ||
committer | Florian Forster <octo@leeloo.lan.home.verplant.org> | |
Mon, 14 Jun 2010 21:46:37 +0000 (23:46 +0200) |
graph_config.c | [new file with mode: 0644] | patch | blob |
graph_config.h | [new file with mode: 0644] | patch | blob |
diff --git a/graph_config.c b/graph_config.c
--- /dev/null
+++ b/graph_config.c
@@ -0,0 +1,116 @@
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <strings.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <errno.h>
+
+#include "graph_config.h"
+#include "graph_list.h"
+#include "oconfig.h"
+#include "common.h"
+
+#define CONFIG_FILE "/usr/lib/cgi-bin/octo/collection.conf"
+
+time_t last_read_mtime = 0;
+
+static int dispatch_config (const oconfig_item_t *ci) /* {{{ */
+{
+ int i;
+
+ for (i = 0; i < ci->children_num; i++)
+ {
+ oconfig_item_t *child;
+
+ child = ci->children + i;
+ if (strcasecmp ("Graph", child->key) == 0)
+ graph_config_add (child);
+ else
+ {
+ DEBUG ("Unknown config option: %s", child->key);
+ }
+ }
+
+ return (0);
+} /* }}} int dispatch_config */
+
+static int internal_read_config (void) /* {{{ */
+{
+ oconfig_item_t *ci;
+
+ ci = oconfig_parse_file (CONFIG_FILE);
+ if (ci == NULL)
+ return (-1);
+
+ dispatch_config (ci);
+
+ oconfig_free (ci);
+
+ graph_config_submit ();
+
+ return (0);
+} /* }}} int internal_read_config */
+
+static time_t get_config_mtime (void) /* {{{ */
+{
+ struct stat statbuf;
+ int status;
+
+ memset (&statbuf, 0, sizeof (statbuf));
+ status = stat (CONFIG_FILE, &statbuf);
+ if (status != 0)
+ return (0);
+
+ return (statbuf.st_mtime);
+} /* }}} time_t get_config_mtime */
+
+int graph_read_config (void) /* {{{ */
+{
+ time_t mtime;
+
+ mtime = get_config_mtime ();
+
+ if (mtime <= last_read_mtime)
+ return (0);
+
+ internal_read_config ();
+
+ last_read_mtime = mtime;
+
+ return (0);
+} /* }}} int graph_read_config */
+
+int graph_config_get_string (const oconfig_item_t *ci, /* {{{ */
+ char **ret_str)
+{
+ char *tmp;
+
+ if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
+ return (EINVAL);
+
+ tmp = strdup (ci->values[0].value.string);
+ if (tmp == NULL)
+ return (ENOMEM);
+
+ free (*ret_str);
+ *ret_str = tmp;
+
+ return (0);
+} /* }}} int graph_config_get_string */
+
+int graph_config_get_bool (const oconfig_item_t *ci, /* {{{ */
+ _Bool *ret_bool)
+{
+ if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
+ return (EINVAL);
+
+ if (ci->values[0].value.boolean)
+ *ret_bool = 1;
+ else
+ *ret_bool = 0;
+
+ return (0);
+} /* }}} int graph_config_get_bool */
+
+/* vim: set sw=2 sts=2 et fdm=marker : */
diff --git a/graph_config.h b/graph_config.h
--- /dev/null
+++ b/graph_config.h
@@ -0,0 +1,12 @@
+#ifndef GRAPH_CONFIG_H
+#define GRAPH_CONFIG_H 1
+
+#include "oconfig.h"
+
+int graph_read_config (void);
+
+int graph_config_get_string (const oconfig_item_t *ci, char **ret_str);
+int graph_config_get_bool (const oconfig_item_t *ci, _Bool *ret_bool);
+
+/* vim: set sw=2 sts=2 et fdm=marker : */
+#endif /* GRAPH_CONFIG_H */