[patch] Re: [Utopia] GVM - fstab symlink patch



On Fri, Aug 13, 2004 at 02:39:17PM -0500, Jerry Haltom wrote:
> http://bugs.gnome.org/show_bug.cgi?id=149977
> 
> This is like, my first actual piece of working C code. I'd appreciate
> any public ridicule that can be provided. ;)

I've had a look at your patch for inclusion in g-v-m's debian package. Imho the
usage of GError in this case is somewhat overkill. And the way readlink is
used a little troublesome. So i rewrote it for the biggest part.

The patch like it's included in g-v-m 0.9.9's debian package is attached.

  Sjoerd
-- 
Life may have no meaning, or, even worse, it may have a meaning of which
you disapprove.
Index: src/manager.c
===================================================================
RCS file: /cvs/gnome/gnome-volume-manager/src/manager.c,v
retrieving revision 1.28
diff -u -r1.28 manager.c
--- src/manager.c	13 Aug 2004 20:58:23 -0000	1.28
+++ src/manager.c	15 Aug 2004 15:19:42 -0000
@@ -22,6 +22,8 @@
 #include <dbus/dbus-glib.h>
 #include <libhal.h>
 
+#include <mntent.h>
+
 #include "gvm.h"
 
 #ifdef ENABLE_NLS
@@ -377,6 +379,61 @@
 }
 
 /*
+ * gvm_get_fstab_path - scans the fstab file for an entry matching the
+ * specified device name, even if this entry is a symbolic link that eventually
+ * resolves to the real device
+ */
+static gchar *
+gvm_get_fstab_path (gchar *device) {
+	struct mntent *mnt; /* pointer to the next mntent record */
+ 	gchar *result = NULL; /* result of realpath, NULL if error */
+	gchar *dest; /* destination of a symlink */
+	gchar *tmp, *tmp2;
+ 	FILE *fstab = setmntent("/etc/fstab" /* get this some other way? */, "r");
+
+ 	if (fstab == NULL) {
+    	warn("failed to open /etc/fstab");
+     	return NULL;
+ 	}
+    
+ 	/* loop over each fstab entry */
+ 	while ((mnt = getmntent (fstab)) != NULL) {
+		if (mnt->mnt_fsname[0] != '/' &&
+				!g_file_test(mnt->mnt_fsname, G_FILE_TEST_EXISTS)) {
+			continue;
+		}
+		if (!g_file_test(mnt->mnt_fsname, G_FILE_TEST_IS_SYMLINK) && 
+				!strcmp(mnt->mnt_fsname, device)) {
+			result = g_strdup(mnt->mnt_fsname);
+			break;
+		}
+		/* symlink check if it links to the real device file */
+		dest = g_file_read_link(mnt->mnt_fsname, NULL);
+		if (dest == NULL) {
+			continue;
+		}
+		if (dest[0] != '/') {
+			/* not an absolute path, need to make it to one */
+			tmp = g_path_get_dirname(mnt->mnt_fsname);
+			tmp2 = dest; 
+			dest = g_build_filename(tmp, tmp2, NULL);
+			g_free(tmp); g_free(tmp2);
+		}
+		if (!strcmp(dest, device)) {
+			result = g_strdup(mnt->mnt_fsname);
+			g_free(dest);
+			break;
+		} 
+		g_free(dest);
+	}
+	endmntent (fstab);
+	if (result == NULL) {
+	  warn("%s not found in /etc/fstab", device);
+	}
+	return result;
+}
+
+/*
  * gvm_device_mount - use BIN_MOUNT to mount the given device node.
  *
  * Note that this requires that the given device node is in /etc/fstab.  This
@@ -385,17 +442,27 @@
 static void
 gvm_device_mount (char *device)
 {
+	char *fstab_device;
 	char *argv[3];
 	GError *error = NULL;
 
+    /* resolve the system device path to the one in the fstab */
+	fstab_device = gvm_get_fstab_path(device);
+	if (fstab_device == NULL) {
+		return;
+	}
+    
 	argv[0] = BIN_MOUNT;
-	argv[1] = device;
+	argv[1] = fstab_device;
 	argv[2] = NULL;
+    
+	g_spawn_async (g_get_home_dir (), argv, NULL, 0, NULL, NULL, NULL, &error);
+	g_free(fstab_device);
 
-	g_spawn_async (g_get_home_dir (), argv, NULL, 0, NULL,
-		       NULL, NULL, &error);
-	if (error)
-		warn ("failed to exec " BIN_MOUNT ": %s\n", error->message);
+	if (error) {
+		g_warning ("failed to exec " BIN_MOUNT ": %s\n", error->message);
+        g_error_free (error);
+	}
 }
 
 /*


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]