$OpenBSD: patch-server_gam_fs_c,v 1.1.1.1 2009/12/04 08:07:20 ajacoutot Exp $

From FreeBSD: get list of mounted filesystems.
(no support for /etc/mtab)

--- server/gam_fs.c.orig	Wed Nov 12 21:45:28 2008
+++ server/gam_fs.c	Sat Nov 28 15:25:00 2009
@@ -7,6 +7,10 @@
 #include <string.h>
 #include <errno.h>
 #include <glib.h>
+#ifdef __OpenBSD__
+#include <sys/param.h>
+#include <sys/mount.h>
+#endif
 #include "gam_error.h"
 #include "gam_fs.h"
 
@@ -21,9 +25,13 @@ typedef struct _gam_fs_properties {
 typedef struct _gam_fs {
 	char *path;
 	char *fsname;
+	guint64 flags;
 } gam_fs;
 
 static gboolean initialized = FALSE;
+#ifdef __OpenBSD__
+static gboolean initializing = FALSE;
+#endif
 static GList *filesystems = NULL;
 static GList *fs_props = NULL;
 static struct stat mtab_sbuf;
@@ -110,6 +118,7 @@ gam_fs_filesystem_sort_cb (gconstpointer a, gconstpoin
 	return strlen(fsb->path) - strlen (fsa->path);
 }
 
+#ifdef __linux__
 static void
 gam_fs_scan_mtab (void)
 {
@@ -165,10 +174,41 @@ gam_fs_scan_mtab (void)
 	gam_fs_free_filesystems ();
 	filesystems = g_list_sort (new_filesystems, gam_fs_filesystem_sort_cb);
 }
+#endif
 
+#ifdef __OpenBSD__
+static void
+gam_fs_getmntinfo (void)
+{
+	struct statfs *stat;
+	GList *new_filesystems = NULL;
+	gam_fs *fs = NULL;
+        int i, n;
+
+	n = getmntinfo(&stat, MNT_NOWAIT);
+	if (n == -1)
+		return;
+
+	for (i = 0; i < n; i++)
+	{
+		fs = g_new0 (gam_fs, 1);
+		fs->path = g_strdup (stat[i].f_mntonname);
+		fs->fsname = g_strdup (stat[i].f_fstypename);
+		fs->flags = stat[i].f_flags;
+
+		new_filesystems = g_list_prepend (new_filesystems, fs);
+	}
+
+        /* Replace the old file systems list with the new one */
+        gam_fs_free_filesystems ();
+        filesystems = g_list_sort (new_filesystems, gam_fs_filesystem_sort_cb);
+}
+#endif
+
 void
 gam_fs_init (void)
 {
+#if defined(__linux__)
 	if (initialized == FALSE)
 	{
 		initialized = TRUE;
@@ -199,6 +239,54 @@ gam_fs_init (void)
 
 		mtab_sbuf = sbuf;
 	}
+#elif defined(__OpenBSD__)
+	if (initialized == FALSE && initializing == FALSE)
+	{
+		GList *iterator = NULL;
+		GHashTable *fs_hash = NULL;
+		gam_fs *fs = NULL;
+
+		initialized = TRUE;
+		initializing = TRUE;
+
+		gam_fs_getmntinfo ();
+
+		iterator = filesystems;
+		fs_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
+
+		while (iterator) {
+			fs = iterator->data;
+
+			if (!g_hash_table_lookup (fs_hash, fs->fsname)) {
+				if (fs->flags & MNT_LOCAL)
+					gam_fs_set (fs->fsname, GFS_MT_DEFAULT, 0);
+				else
+					gam_fs_set (fs->fsname, GFS_MT_POLL, 5);
+
+				g_hash_table_insert (fs_hash, g_strdup (fs->fsname), GINT_TO_POINTER (1));
+			}
+
+			iterator = g_list_next (iterator);
+		}
+
+		g_hash_table_destroy (fs_hash);
+		initializing = FALSE;
+	} else if (initializing == FALSE) {
+		struct stat sbuf;
+
+		if (stat ("/etc/fstab", &sbuf) != 0) {
+			GAM_DEBUG(DEBUG_INFO, "Could not stat /etc/fstab\n");
+			return;
+		}
+
+		if (sbuf.st_mtime != mtab_sbuf.st_mtime) {
+			GAM_DEBUG(DEBUG_INFO, "Updating list of mounted filesystems\n");
+			gam_fs_getmntinfo ();
+		}
+
+		mtab_sbuf = sbuf;
+	}
+#endif
 }
 
 gam_fs_mon_type
