From 4edea20b86fb08209ffb38c77be2df1d3a373e82 Mon Sep 17 00:00:00 2001
From: Ton Voon <tonvoon@users.sourceforge.net>
Date: Wed, 12 Jul 2006 19:30:20 +0000
Subject: Moving parameter_list into utils_disk.h. Given list of mount points,
 can now work out best match or exact match.

git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1448 f882894a-f735-0410-b71e-b25c423dba1c

diff --git a/plugins/check_disk.c b/plugins/check_disk.c
index f0a679d..8e9c1fc 100644
--- a/plugins/check_disk.c
+++ b/plugins/check_disk.c
@@ -74,21 +74,6 @@ static int show_local_fs = 0;
    SunOs4.1.3, for one.  It is *not* necessary on Linux.  */
 /* static int require_sync = 0; */
 
-/* A filesystem type to display. */
-struct parameter_list
-{
-  char *name;
-  int found;
-  int found_len;
-  uintmax_t w_df;
-  uintmax_t c_df;
-  double w_dfp;
-  double c_dfp;
-  double w_idfp;
-  double c_idfp;
-  struct parameter_list *name_next;
-};
-
 /* Linked list of filesystem types to display.
    If `fs_select_list' is NULL, list all types.
    This table is generated dynamically from command-line options,
diff --git a/plugins/tests/.cvsignore b/plugins/tests/.cvsignore
index 94b5122..6dc692f 100644
--- a/plugins/tests/.cvsignore
+++ b/plugins/tests/.cvsignore
@@ -1,4 +1,5 @@
 Makefile
 Makefile.in
 test_utils
+test_disk
 .deps
diff --git a/plugins/tests/test_disk.c b/plugins/tests/test_disk.c
index 19cc3ac..8940236 100644
--- a/plugins/tests/test_disk.c
+++ b/plugins/tests/test_disk.c
@@ -27,16 +27,24 @@ main (int argc, char **argv)
 {
 	struct name_list *exclude_filesystem=NULL;
 	struct name_list *exclude_fstype=NULL;
+	struct name_list *dummy_mountlist = NULL;
+	struct name_list *temp_name;
+	struct parameter_list *paths = NULL;
+	struct parameter_list *p;
 
-	plan_tests(8);
+	struct mount_entry *dummy_mount_list;
+	struct mount_entry *me;
+	struct mount_entry **mtail = &dummy_mount_list;
 
-	ok( np_find_name(exclude_filesystem, "/var") == FALSE, "/var not in list");
-	np_add_name(&exclude_filesystem, "/var");
-	ok( np_find_name(exclude_filesystem, "/var") == TRUE, "is in list now");
+	plan_tests(17);
+
+	ok( np_find_name(exclude_filesystem, "/var/log") == FALSE, "/var/log not in list");
+	np_add_name(&exclude_filesystem, "/var/log");
+	ok( np_find_name(exclude_filesystem, "/var/log") == TRUE, "is in list now");
 	ok( np_find_name(exclude_filesystem, "/home") == FALSE, "/home not in list");
 	np_add_name(&exclude_filesystem, "/home");
 	ok( np_find_name(exclude_filesystem, "/home") == TRUE, "is in list now");
-	ok( np_find_name(exclude_filesystem, "/var") == TRUE, "/var still in list");
+	ok( np_find_name(exclude_filesystem, "/var/log") == TRUE, "/var/log still in list");
 
 	ok( np_find_name(exclude_fstype, "iso9660") == FALSE, "iso9660 not in list");
 	np_add_name(&exclude_fstype, "iso9660");
@@ -44,42 +52,73 @@ main (int argc, char **argv)
 
 	ok( np_find_name(exclude_filesystem, "iso9660") == FALSE, "Make sure no clashing in variables");
 
-
-	
-	
 	/*
-	range = parse_range_string("6");
-	ok( range != NULL, "'6' is valid range");
-	ok( range->start == 0, "Start correct");
-	ok( range->start_infinity == FALSE, "Not using negative infinity");
-	ok( range->end == 6, "End correct");
-	ok( range->end_infinity == FALSE, "Not using infinity");
-	free(range);
-
-	range = parse_range_string("-7:23");
-	ok( range != NULL, "'-7:23' is valid range");
-	ok( range->start == -7, "Start correct");
-	ok( range->start_infinity == FALSE, "Not using negative infinity");
-	ok( range->end == 23, "End correct");
-	ok( range->end_infinity == FALSE, "Not using infinity");
-	free(range);
-
-	range = parse_range_string(":5.75");
-	ok( range != NULL, "':5.75' is valid range");
-	ok( range->start == 0, "Start correct");
-	ok( range->start_infinity == FALSE, "Not using negative infinity");
-	ok( range->end == 5.75, "End correct");
-	ok( range->end_infinity == FALSE, "Not using infinity");
-	free(range);
-
-	range = parse_range_string("~:-95.99");
-	ok( range != NULL, "~:-95.99' is valid range");
-	ok( range->start_infinity == TRUE, "Using negative infinity");
-	ok( range->end == -95.99, "End correct (with rounding errors)");
-	ok( range->end_infinity == FALSE, "Not using infinity");
-	free(range);
+	for (temp_name = exclude_filesystem; temp_name; temp_name = temp_name->next) {
+		printf("Name: %s\n", temp_name->name);
+	}
 	*/
 
+	me = (struct mount_entry *) malloc(sizeof *me);
+	me->me_devname = strdup("/dev/c0t0d0s0");
+	me->me_mountdir = strdup("/");
+	*mtail = me;
+	mtail = &me->me_next;
+
+	me = (struct mount_entry *) malloc(sizeof *me);
+	me->me_devname = strdup("/dev/c1t0d1s0");
+	me->me_mountdir = strdup("/var");
+	*mtail = me;
+	mtail = &me->me_next;
+
+	me = (struct mount_entry *) malloc(sizeof *me);
+	me->me_devname = strdup("/dev/c2t0d0s0");
+	me->me_mountdir = strdup("/home");
+	*mtail = me;
+	mtail = &me->me_next;
+
+
+	np_add_parameter(&paths, "/home/groups");
+	np_add_parameter(&paths, "/var");
+	np_add_parameter(&paths, "/tmp");
+	np_add_parameter(&paths, "/home/tonvoon");
+	np_add_parameter(&paths, "/dev/c2t0d0s0");
+
+	np_set_best_match(paths, dummy_mount_list, FALSE);
+	for (p = paths; p; p = p->name_next) {
+		struct mount_entry *temp_me;
+		temp_me = p->best_match;
+		if (! strcmp(p->name, "/home/groups")) {
+			ok( temp_me && !strcmp(temp_me->me_mountdir, "/home"), "/home/groups got right best match: /home");
+		} else if (! strcmp(p->name, "/var")) {
+			ok( temp_me && !strcmp(temp_me->me_mountdir, "/var"), "/var got right best match: /var");
+		} else if (! strcmp(p->name, "/tmp")) {
+			ok( temp_me && !strcmp(temp_me->me_mountdir, "/"), "/tmp got right best match: /");
+		} else if (! strcmp(p->name, "/home/tonvoon")) {
+			ok( temp_me && !strcmp(temp_me->me_mountdir, "/home"), "/home/tonvoon got right best match: /home");
+		} else if (! strcmp(p->name, "/dev/c2t0d0s0")) {
+			ok( temp_me && !strcmp(temp_me->me_devname, "/dev/c2t0d0s0"), "/dev/c2t0d0s0 got right best match: /dev/c2t0d0s0");
+		}
+	}
+
+	paths = NULL;	/* Bad boy - should free, but this is a test suite */
+	np_add_parameter(&paths, "/home/groups");
+	np_add_parameter(&paths, "/var");
+	np_add_parameter(&paths, "/tmp");
+	np_add_parameter(&paths, "/home/tonvoon");
+
+	np_set_best_match(paths, dummy_mount_list, TRUE);
+	for (p = paths; p; p = p->name_next) {
+		if (! strcmp(p->name, "/home/groups")) {
+			ok( p->found == 0, "/home/groups correctly not found");
+		} else if (! strcmp(p->name, "/var")) {
+			ok( p->found == 1, "/var found");
+		} else if (! strcmp(p->name, "/tmp")) {
+			ok( p->found == 0, "/tmp correctly not found");
+		} else if (! strcmp(p->name, "/home/tonvoon")) {
+			ok( p->found == 0, "/home/tonvoon not found");
+		}
+	}
+
 	return exit_status();
 }
 
diff --git a/plugins/utils_disk.c b/plugins/utils_disk.c
index 6380df3..0a71d79 100644
--- a/plugins/utils_disk.c
+++ b/plugins/utils_disk.c
@@ -43,6 +43,60 @@ np_add_name (struct name_list **list, const char *name)
   *list = new_entry;
 }
 
+void
+np_add_parameter(struct parameter_list **list, const char *name)
+{
+  struct parameter_list *new_path;
+  new_path = (struct parameter_list *) malloc (sizeof *new_path);
+  new_path->name = (char *) name;
+  new_path->found = 0;
+  new_path->found_len = 0;
+  new_path->w_df = 0;
+  new_path->c_df = 0;
+  new_path->w_dfp = -1.0;
+  new_path->c_dfp = -1.0;
+  new_path->w_idfp = -1.0;
+  new_path->c_idfp = -1.0;
+  new_path->name_next = *list;
+  *list = new_path;
+}
+
+void
+np_set_best_match(struct parameter_list *desired, struct mount_entry *mount_list, int exact)
+{
+  struct parameter_list *d;
+  for (d = desired; d; d= d->name_next) {
+    struct mount_entry *me;
+    size_t name_len = strlen(d->name);
+    size_t best_match_len = 0;
+    struct mount_entry *best_match = NULL;
+
+    for (me = mount_list; me; me = me->me_next) {
+      size_t len = strlen (me->me_mountdir);
+      if ((exact == FALSE && (best_match_len <= len && len <= name_len && 
+        (len == 1 || strncmp (me->me_mountdir, d->name, len) == 0)))
+	|| (exact == TRUE && strcmp(me->me_mountdir, d->name)==0))
+      {
+        best_match = me;
+        best_match_len = len;
+      } else {
+        len = strlen (me->me_devname);
+        if ((exact == FALSE && (best_match_len <= len && len <= name_len &&
+          (len == 1 || strncmp (me->me_devname, d->name, len) == 0)))
+          || (exact == TRUE && strcmp(me->me_devname, d->name)==0))
+        {
+          best_match = me;
+          best_match_len = len;
+        }
+      }
+    }
+    if (best_match) {
+      d->best_match = best_match;
+      d->found = TRUE;
+    }
+  }
+}
+
 /* Returns TRUE if name is in list */
 int
 np_find_name (struct name_list *list, const char *name)
diff --git a/plugins/utils_disk.h b/plugins/utils_disk.h
index f173c91..c1919fe 100644
--- a/plugins/utils_disk.h
+++ b/plugins/utils_disk.h
@@ -1,5 +1,6 @@
 /* Header file for utils_disk */
 
+#include "mountlist.h"
 
 struct name_list
 {
@@ -7,6 +8,22 @@ struct name_list
   struct name_list *next;
 };
 
+struct parameter_list
+{
+  char *name;
+  int found;
+  int found_len;
+  uintmax_t w_df;
+  uintmax_t c_df;
+  double w_dfp;
+  double c_dfp;
+  double w_idfp;
+  double c_idfp;
+  struct mount_entry *best_match;
+  struct parameter_list *name_next;
+};
+
 void np_add_name (struct name_list **list, const char *name);
 int np_find_name (struct name_list *list, const char *name);
-
+void np_add_parameter(struct parameter_list **list, const char *name);
+int search_parameter_list (struct parameter_list *list, const char *name);
-- 
cgit v0.10-9-g596f

