Merge branch 'master' into f/riemann/rcc
[collectd.git] / src / filecount.c
index f071379..270e5d0 100644 (file)
  *
  * Authors:
  *   Alessandro Iurlano <alessandro.iurlano at gmail.com>
- *   Florian octo Forster <octo at verplant.org>
+ *   Florian octo Forster <octo at collectd.org>
  **/
 
 #include "collectd.h"
 #include "common.h"
-#include "plugin.h"       
+#include "plugin.h"
 
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <dirent.h>
 #include <fnmatch.h>
 
+#define FC_RECURSIVE 1
+#define FC_HIDDEN 2
+
 struct fc_directory_conf_s
 {
   char *path;
   char *instance;
 
+  int options;
+
   /* Data counters */
   uint64_t files_num;
   uint64_t files_size;
@@ -62,7 +67,6 @@ static void fc_submit_dir (const fc_directory_conf_t *dir)
 
   vl.values = values;
   vl.values_len = STATIC_ARRAY_SIZE (values);
-  vl.time = time (NULL);
   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
   sstrncpy (vl.plugin, "filecount", sizeof (vl.plugin));
   sstrncpy (vl.plugin_instance, dir->instance, sizeof (vl.plugin_instance));
@@ -307,6 +311,25 @@ static int fc_config_add_dir_size (fc_directory_conf_t *dir,
   return (0);
 } /* int fc_config_add_dir_size */
 
+static int fc_config_add_dir_option (fc_directory_conf_t *dir,
+    oconfig_item_t *ci, int bit)
+{
+  if ((ci->values_num != 1)
+      || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
+  {
+    WARNING ("filecount plugin: The `Recursive' config options needs exactly "
+        "one boolean argument.");
+    return (-1);
+  }
+
+  if (ci->values[0].value.boolean)
+    dir->options |= bit;
+  else
+    dir->options &= ~bit;
+
+  return (0);
+} /* int fc_config_add_dir_option */
+
 static int fc_config_add_dir (oconfig_item_t *ci)
 {
   fc_directory_conf_t *dir;
@@ -321,23 +344,25 @@ static int fc_config_add_dir (oconfig_item_t *ci)
   }
 
   /* Initialize `dir' */
-  dir = (fc_directory_conf_t *) malloc (sizeof (*dir));
+  dir = calloc (1, sizeof (*dir));
   if (dir == NULL)
   {
-    ERROR ("filecount plugin: malloc failed.");
+    ERROR ("filecount plugin: calloc failed.");
     return (-1);
   }
-  memset (dir, 0, sizeof (*dir));
 
   dir->path = strdup (ci->values[0].value.string);
   if (dir->path == NULL)
   {
     ERROR ("filecount plugin: strdup failed.");
+    sfree (dir);
     return (-1);
   }
 
   fc_config_set_instance (dir, dir->path);
 
+  dir->options = FC_RECURSIVE;
+
   dir->name = NULL;
   dir->mtime = 0;
   dir->size = 0;
@@ -355,6 +380,10 @@ static int fc_config_add_dir (oconfig_item_t *ci)
       status = fc_config_add_dir_mtime (dir, option);
     else if (strcasecmp ("Size", option->key) == 0)
       status = fc_config_add_dir_size (dir, option);
+    else if (strcasecmp ("Recursive", option->key) == 0)
+      status = fc_config_add_dir_option (dir, option, FC_RECURSIVE);
+    else if (strcasecmp ("IncludeHidden", option->key) == 0)
+      status = fc_config_add_dir_option (dir, option, FC_HIDDEN);
     else
     {
       WARNING ("filecount plugin: fc_config_add_dir: "
@@ -447,9 +476,10 @@ static int fc_read_dir_callback (const char *dirname, const char *filename,
     return (-1);
   }
 
-  if (S_ISDIR (statbuf.st_mode))
+  if (S_ISDIR (statbuf.st_mode) && (dir->options & FC_RECURSIVE))
   {
-    status = walk_directory (abs_path, fc_read_dir_callback, dir);
+    status = walk_directory (abs_path, fc_read_dir_callback, dir,
+        /* include hidden = */ (dir->options & FC_HIDDEN) ? 1 : 0);
     return (status);
   }
   else if (!S_ISREG (statbuf.st_mode))
@@ -512,7 +542,8 @@ static int fc_read_dir (fc_directory_conf_t *dir)
   if (dir->mtime != 0)
     dir->now = time (NULL);
 
-  status = walk_directory (dir->path, fc_read_dir_callback, dir);
+  status = walk_directory (dir->path, fc_read_dir_callback, dir,
+      /* include hidden */ (dir->options & FC_HIDDEN) ? 1 : 0);
   if (status != 0)
   {
     WARNING ("filecount plugin: walk_directory (%s) failed.", dir->path);