[PATCH 05/10] mount: fsprobe: make fsprobe_get_devname functions more generic

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

 



The blkid supports NAME=value parsing, but use the library for
this simple task is overkill. (The libblkid requires initialized
blkid cache all time, for all calls.)

This patch makes the fsprobe_get_devname_for_mounting() and
fsprobe_get_devname() generic for all fsprobe implementations.

Signed-off-by: Karel Zak <kzak@xxxxxxxxxx>
---
 mount/Makefile.am     |    2 +-
 mount/fsprobe.c       |   61 +++++++++++++++++++++++++++++++++++++++++++++++++
 mount/fsprobe_blkid.c |   11 ---------
 mount/sundries.c      |   38 ++++++++++++++++++++++++++++++
 mount/sundries.h      |    2 +
 mount/swapon.c        |    1 +
 6 files changed, 103 insertions(+), 12 deletions(-)

diff --git a/mount/Makefile.am b/mount/Makefile.am
index b5b21b5..e61261f 100644
--- a/mount/Makefile.am
+++ b/mount/Makefile.am
@@ -23,7 +23,7 @@ umount_LDADD = $(top_srcdir)/lib/libenv.a
 umount_CFLAGS = $(SUID_CFLAGS)
 
 swapon_SOURCES = swapon.c xmalloc.c \
-	swap_constants.h realpath.c
+	swap_constants.h realpath.c fsprobe.c sundries.c
 
 losetup_SOURCES = lomount.c loop.h lomount.h
 losetup_CFLAGS = -DMAIN
diff --git a/mount/fsprobe.c b/mount/fsprobe.c
index 4b57802..2629d0d 100644
--- a/mount/fsprobe.c
+++ b/mount/fsprobe.c
@@ -162,3 +162,64 @@ fsprobe_procfsloop_mount(	int (*mount_fn)(struct mountargs *),
 	}
 	return 1;
 }
+
+const char *
+fsprobe_get_devname_for_mounting(const char *spec)
+{
+	char *name, *value;
+
+	if (!spec)
+		return NULL;
+
+	if (parse_spec(spec, &name, &value) != 0)
+		return NULL;				/* parse error */
+
+	if (name) {
+		const char *nspec = NULL;
+
+		if (!strcmp(name,"LABEL"))
+			nspec = fsprobe_get_devname_by_label(value);
+		else if (!strcmp(name,"UUID"))
+			nspec = fsprobe_get_devname_by_uuid(value);
+
+		if (nspec && verbose > 1)
+			printf(_("mount: going to mount %s by %s\n"), spec, name);
+
+		free((void *) name);
+		return nspec;
+	}
+
+	/* no LABEL, no UUID, .. probably a path */
+	if (verbose > 1)
+		printf(_("mount: no LABEL=, no UUID=, going to mount %s by path\n"), spec);
+
+	return canonicalize(spec);
+}
+
+/* like fsprobe_get_devname_for_mounting(), but without verbose messages */
+const char *
+fsprobe_get_devname(const char *spec)
+{
+	char *name, *value;
+
+	if (!spec)
+		return NULL;
+
+	if (parse_spec(spec, &name, &value) != 0)
+		return NULL;				/* parse error */
+
+	if (name) {
+		const char *nspec = NULL;
+
+		if (!strcmp(name,"LABEL"))
+			nspec = fsprobe_get_devname_by_label(value);
+		else if (!strcmp(name,"UUID"))
+			nspec = fsprobe_get_devname_by_uuid(value);
+
+		free((void *) name);
+		return nspec;
+	}
+
+	return canonicalize(spec);
+}
+
diff --git a/mount/fsprobe_blkid.c b/mount/fsprobe_blkid.c
index d25b973..2dc734e 100644
--- a/mount/fsprobe_blkid.c
+++ b/mount/fsprobe_blkid.c
@@ -25,11 +25,6 @@ fsprobe_get_uuid_by_devname(const char *devname) {
 }
 
 const char *
-fsprobe_get_devname(const char *spec) {
-	return blkid_get_devname(blkid, spec, 0);
-}
-
-const char *
 fsprobe_get_devname_by_uuid(const char *uuid) {
 	return blkid_get_devname(blkid, "UUID", uuid);
 }
@@ -39,12 +34,6 @@ fsprobe_get_devname_by_label(const char *label) {
 	return blkid_get_devname(blkid, "LABEL", label);
 }
 
-/* Also when no UUID= or LABEL= occur? No verbose? No warnings? */
-const char *
-fsprobe_get_devname_for_mounting(const char *spec) {
-	return blkid_get_devname(blkid, spec, 0);
-}
-
 int
 fsprobe_known_fstype(const char *fstype)
 {
diff --git a/mount/sundries.c b/mount/sundries.c
index cdfbb42..45404c5 100644
--- a/mount/sundries.c
+++ b/mount/sundries.c
@@ -247,3 +247,41 @@ canonicalize (const char *path) {
 
 	return xstrdup(path);
 }
+
+
+/*
+ * Parses NAME=value, returns -1 on parse error, 0 success. The success is also
+ * when the 'spec' doesn't contain name=value pair (because the spec could be
+ * a devname too). In particular case the pointer 'name' is set to NULL.
+
+ * The result is a new allocated string (the 'name' pointer).
+ */
+int
+parse_spec(const char *spec, char **name, char **value)
+{
+	char *vl, *tk, *cp;
+
+	*name = NULL;
+	*value = NULL;
+
+	if (!(cp = strchr(spec, '=')))
+		return 0;				/* no name= */
+
+	tk = xstrdup(spec);
+	vl = tk + (cp - spec);
+	*vl++ = '\0';
+
+	if (*vl == '"' || *vl == '\'') {
+		if (!(cp = strrchr(vl+1, *vl))) {
+			free(tk);
+			return -1;			/* parse error */
+		}
+		vl++;
+		*cp = '\0';
+	}
+
+	*name = tk;
+	*value = vl;
+	return 0;
+}
+
diff --git a/mount/sundries.h b/mount/sundries.h
index 0851145..85ccd85 100644
--- a/mount/sundries.h
+++ b/mount/sundries.h
@@ -35,6 +35,8 @@ char *xstrndup (const char *s, int n);
 char *xstrconcat3 (char *, const char *, const char *);
 char *xstrconcat4 (char *, const char *, const char *, const char *);
 
+int parse_spec(const char *spec, char **name, char **value);
+
 void die (int errcode, const char *fmt, ...);
 
 /* exit status - bits below are ORed */
diff --git a/mount/swapon.c b/mount/swapon.c
index 025a6b5..3936790 100644
--- a/mount/swapon.c
+++ b/mount/swapon.c
@@ -30,6 +30,7 @@
 int all = 0;
 int verbose = 0;
 int priority = -1;	/* non-prioritized swap by default */
+int mount_quiet = 0;
 
 /* If true, don't complain if the device/file doesn't exist */
 int ifexists = 0;
-- 
1.5.0.6

-
To unsubscribe from this list: send the line "unsubscribe util-linux-ng" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux