2 * collectd - src/utils_fbhash.c
3 * Copyright (C) 2009 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
30 #include "utils_fbhash.h"
31 #include "utils_avltree.h"
45 static void fbh_free_tree (c_avl_tree_t *tree) /* {{{ */
57 status = c_avl_pick (tree, (void *) &key, (void *) &value);
66 } /* }}} void fbh_free_tree */
68 static int fbh_read_file (fbhash_t *h) /* {{{ */
76 fh = fopen (h->filename, "r");
80 memset (&fl, 0, sizeof (fl));
82 fl.l_whence = SEEK_SET;
84 fl.l_len = 0; /* == entire file */
85 /* TODO: Lock file? -> fcntl */
87 status = fcntl (fileno (fh), F_SETLK, &fl);
94 tree = c_avl_create ((int (*) (const void *, const void *)) strcmp);
101 /* Read `fh' into `tree' */
102 while (fgets (buffer, sizeof (buffer), fh) != NULL) /* {{{ */
111 buffer[sizeof (buffer) - 1] = 0;
112 len = strlen (buffer);
114 /* Remove trailing newline characters. */
116 && ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
122 /* Seek first non-space character */
124 while ((*key != 0) && isspace ((int) *key))
127 /* Skip empty lines and comments */
128 if ((key[0] == 0) || (key[0] == '#'))
131 /* Seek first colon */
132 value = strchr (key, ':');
136 /* Null-terminate `key'. */
140 /* Skip leading whitespace */
141 while ((*value != 0) && isspace ((int) *value))
144 /* Skip lines without value */
148 key_copy = strdup (key);
149 value_copy = strdup (value);
151 if ((key_copy == NULL) || (value_copy == NULL))
158 status = c_avl_insert (tree, key_copy, value_copy);
166 DEBUG ("utils_fbhash: fbh_read_file: key = %s; value = %s;",
168 } /* }}} while (fgets) */
172 fbh_free_tree (h->tree);
176 } /* }}} int fbh_read_file */
178 static int fbh_check_file (fbhash_t *h) /* {{{ */
183 memset (&statbuf, 0, sizeof (statbuf));
185 status = stat (h->filename, &statbuf);
189 if (h->mtime >= statbuf.st_mtime)
192 status = fbh_read_file (h);
194 h->mtime = statbuf.st_mtime;
197 } /* }}} int fbh_check_file */
202 fbhash_t *fbh_create (const char *file) /* {{{ */
210 h = calloc (1, sizeof (*h));
214 h->filename = strdup (file);
215 if (h->filename == NULL)
222 pthread_mutex_init (&h->lock, /* attr = */ NULL);
224 status = fbh_check_file (h);
233 } /* }}} fbhash_t *fbh_create */
235 void fbh_destroy (fbhash_t *h) /* {{{ */
240 pthread_mutex_destroy (&h->lock);
242 fbh_free_tree (h->tree);
243 } /* }}} void fbh_destroy */
245 char *fbh_get (fbhash_t *h, const char *key) /* {{{ */
251 if ((h == NULL) || (key == NULL))
257 pthread_mutex_lock (&h->lock);
259 /* TODO: Checking this every time may be a bit much..? */
262 status = c_avl_get (h->tree, key, (void *) &value);
265 assert (value != NULL);
266 value_copy = strdup (value);
269 pthread_mutex_unlock (&h->lock);
272 } /* }}} char *fbh_get */
274 /* vim: set sw=2 sts=2 et fdm=marker : */