Fix building with xfsprogs 4.5.0
[collectd.git] / src / utils_ignorelist.c
index 1adfbc6..0828c55 100644 (file)
@@ -1,6 +1,7 @@
 /**
- * collectd - src/config_list.c
+ * collectd - src/utils_ignorelist.c
  * Copyright (C) 2006 Lubos Stanek <lubek at users.sourceforge.net>
+ * Copyright (C) 2008 Florian Forster <octo at verplant.org>
  *
  * This program is free software; you can redistribute it and/
  * or modify it under the terms of the GNU General Public Li-
  * ranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  * See the GNU General Public Licence for more details.
  *
- * You should have received a copy of the GNU General Public
- * Licence along with this program; if not, write to the Free
- * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
- * USA.
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  *
  * Authors:
  *   Lubos Stanek <lubek at users.sourceforge.net>
+ *   Florian Forster <octo at verplant.org>
  **/
 /**
  * ignorelist handles plugin's list of configured collectable
  *     return;
  **/
 
+#if HAVE_CONFIG_H
+# include "config.h"
+#endif
+
 #include "common.h"
-#include "utils_debug.h"
+#include "plugin.h"
 #include "utils_ignorelist.h"
 
 /*
@@ -67,7 +72,6 @@ typedef struct ignorelist_item_s ignorelist_item_t;
 struct ignorelist_s
 {
        int ignore;             /* ignore entries */
-       int num;                /* number of entries */
        ignorelist_item_t *head;        /* pointer to the first entry */
 };
 
@@ -81,69 +85,50 @@ static inline void ignorelist_append (ignorelist_t *il, ignorelist_item_t *item)
 
        item->next = il->head;
        il->head = item;
-
-       il->num++;
 }
 
 #if HAVE_REGEX_H
 static int ignorelist_append_regex(ignorelist_t *il, const char *entry)
 {
-       int rcompile;
-       regex_t *regtemp;
-       int errsize;
-       char *regerr = NULL;
-       ignorelist_item_t *new;
+       regex_t *re;
+       ignorelist_item_t *item;
+       int status;
 
        /* create buffer */
-       if ((regtemp = malloc(sizeof(regex_t))) == NULL)
+       re = malloc (sizeof (*re));
+       if (re == NULL)
        {
-               syslog (LOG_ERR, "cannot allocate new config entry");
-               return (1);
+               ERROR ("ignorelist_append_regex: malloc failed.");
+               return ENOMEM;
        }
-       memset (regtemp, '\0', sizeof(regex_t));
+       memset (re, 0, sizeof (*re));
 
        /* compile regex */
-       if ((rcompile = regcomp (regtemp, entry, REG_EXTENDED)) != 0)
+       status = regcomp (re, entry, REG_EXTENDED);
+       if (status != 0)
        {
-               /* prepare message buffer */
-               errsize = regerror(rcompile, regtemp, NULL, 0);
-               if (errsize)
-                       regerr = smalloc(errsize);
-               /* get error message */
-               if (regerror (rcompile, regtemp, regerr, errsize))
-               {
-                       fprintf (stderr, "Cannot compile regex %s: %i/%s",
-                                       entry, rcompile, regerr);
-                       syslog (LOG_ERR, "Cannot compile regex %s: %i/%s",
-                                       entry, rcompile, regerr);
-               }
-               else
-               {
-                       fprintf (stderr, "Cannot compile regex %s: %i",
-                                       entry, rcompile);
-                       syslog (LOG_ERR, "Cannot compile regex %s: %i",
-                                       entry, rcompile);
-               }
+               char errbuf[1024];
 
-               if (errsize)
-                       sfree (regerr);
-               regfree (regtemp);
-               return (1);
+               (void) regerror (status, re, errbuf, sizeof (errbuf));
+               ERROR ("ignorelist_append_regex: Compiling regular expression \"%s\" failed: %s", entry, errbuf);
+               sfree (re);
+               return status;
        }
-       DBG("regex compiled: %s - %i", entry, rcompile);
 
        /* create new entry */
-       if ((new = malloc(sizeof(ignorelist_item_t))) == NULL)
+       item = malloc (sizeof (*item));
+       if (item == NULL)
        {
-               syslog (LOG_ERR, "cannot allocate new config entry");
-               regfree (regtemp);
-               return (1);
+               ERROR ("ignorelist_append_regex: malloc failed.");
+               regfree (re);
+               sfree (re);
+               return ENOMEM;
        }
-       memset (new, '\0', sizeof(ignorelist_item_t));
-       new->rmatch = regtemp;
+       memset (item, 0, sizeof (*item));
+       item->rmatch = re;
 
        /* append new entry */
-       ignorelist_append (il, new);
+       ignorelist_append (il, item);
 
        return (0);
 } /* int ignorelist_append_regex(ignorelist_t *il, const char *entry) */
@@ -156,7 +141,7 @@ static int ignorelist_append_string(ignorelist_t *il, const char *entry)
        /* create new entry */
        if ((new = malloc(sizeof(ignorelist_item_t))) == NULL )
        {
-               syslog (LOG_ERR, "cannot allocate new entry");
+               ERROR ("cannot allocate new entry");
                return (1);
        }
        memset (new, '\0', sizeof(ignorelist_item_t));
@@ -214,13 +199,10 @@ ignorelist_t *ignorelist_create (int invert)
 {
        ignorelist_t *il;
 
-       /* smalloc exits if it failes */
-       il = (ignorelist_t *) smalloc (sizeof (ignorelist_t));
-       DBG("Ignorelist created 0x%p, default is %s",
-                       (void *) il,
-                       invert ? "collect" : "ignore");
-
-       memset (il, '\0', sizeof (ignorelist_t));
+       il = malloc (sizeof (*il));
+       if (il == NULL)
+               return NULL;
+       memset (il, 0, sizeof (*il));
 
        /*
         * ->ignore == 0  =>  collect
@@ -239,20 +221,17 @@ void ignorelist_free (ignorelist_t *il)
        ignorelist_item_t *this;
        ignorelist_item_t *next;
 
-       DBG ("(il = 0x%p)", (void *) il);
-
        if (il == NULL)
                return;
 
        for (this = il->head; this != NULL; this = next)
        {
-               DBG ("free - item = 0x%p, numlist %i", (void *) this, il->num);
                next = this->next;
-               il->num--;
 #if HAVE_REGEX_H
                if (this->rmatch != NULL)
                {
                        regfree (this->rmatch);
+                       sfree (this->rmatch);
                        this->rmatch = NULL;
                }
 #endif
@@ -263,11 +242,7 @@ void ignorelist_free (ignorelist_t *il)
                }
                sfree (this);
        }
-#if COLLECTD_DEBUG
-       if (il->num != 0)
-               DBG ("after free numlist: %i", il->num);
-#endif
-       il->num = 0;
+
        sfree (il);
        il = NULL;
 } /* void ignorelist_destroy (ignorelist_t *il) */
@@ -279,7 +254,7 @@ void ignorelist_set_invert (ignorelist_t *il, int invert)
 {
        if (il == NULL)
        {
-               DBG("ignore call with ignorelist_t == NULL");
+               DEBUG("ignore call with ignorelist_t == NULL");
                return;
        }
 
@@ -287,66 +262,50 @@ void ignorelist_set_invert (ignorelist_t *il, int invert)
 } /* void ignorelist_set_invert (ignorelist_t *il, int ignore) */
 
 /*
- * get number of entries in the ignorelist_t
- * return int number
- */
-int ignorelist_num (ignorelist_t *il)
-{
-       if (il == NULL)
-       {
-               DBG("get num called with ignorelist_t == NULL");
-               return (0);
-       }
-
-       return (il->num);
-} /* int ignorelist_num (ignorelist_t *il) */
-
-/*
  * append entry into ignorelist_t
  * return 1 for success
  */
 int ignorelist_add (ignorelist_t *il, const char *entry)
 {
-       int ret;
-       size_t entry_len;
-       char *entry_copy;
+       size_t len;
 
        if (il == NULL)
        {
-               DBG ("add called with ignorelist_t == NULL");
+               DEBUG ("add called with ignorelist_t == NULL");
                return (1);
        }
 
-       entry_len = strlen (entry);
+       len = strlen (entry);
 
        /* append nothing */
-       if (entry_len == 0)
+       if (len == 0)
        {
-               DBG("not appending: empty entry");
+               DEBUG("not appending: empty entry");
                return (1);
        }
 
 #if HAVE_REGEX_H
        /* regex string is enclosed in "/.../" */
-       if ((entry_len > 2) && (entry[0] == '/') && entry[entry_len - 1] == '/')
+       if ((len > 2) && (entry[0] == '/') && entry[len - 1] == '/')
        {
-               /* We need to copy `entry' since it's const */
-               entry_copy = smalloc (entry_len);
-               memset (entry_copy, '\0', entry_len);
-               strncpy (entry_copy, entry + 1, entry_len - 2);
-
-               DBG("I'm about to add regex entry: %s", entry_copy);
-               ret = ignorelist_append_regex(il, entry_copy);
-               sfree (entry_copy);
+               char *copy;
+               int status;
+
+               /* skip leading slash */
+               copy = strdup (entry + 1);
+               if (copy == NULL)
+                       return ENOMEM;
+
+               /* trim trailing slash */
+               copy[strlen (copy) - 1] = 0;
+
+               status = ignorelist_append_regex (il, copy);
+               sfree (copy);
+               return status;
        }
-       else
 #endif
-       {
-               DBG("to add entry: %s", entry);
-               ret = ignorelist_append_string(il, entry);
-       }
 
-       return (ret);
+       return ignorelist_append_string(il, entry);
 } /* int ignorelist_add (ignorelist_t *il, const char *entry) */
 
 /*
@@ -358,7 +317,10 @@ int ignorelist_match (ignorelist_t *il, const char *entry)
        ignorelist_item_t *traverse;
 
        /* if no entries, collect all */
-       if (ignorelist_num(il) == 0)
+       if ((il == NULL) || (il->head == NULL))
+               return (0);
+
+       if ((entry == NULL) || (strlen (entry) == 0))
                return (0);
 
        /* traverse list and check entries */