From 65a567e837338052ab0714ee5eab925d74f4a6c1 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Mon, 14 May 2007 14:31:28 +0200 Subject: [PATCH] mount: fsprobe: make fsprobe_get_devname functions more generic 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 --- 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 ae67ed63..fb330133 100644 --- a/mount/Makefile.am +++ b/mount/Makefile.am @@ -22,7 +22,7 @@ umount_CFLAGS = $(SUID_CFLAGS) $(AM_CFLAGS) umount_LDFLAGS = $(SUID_LDFLAGS) $(AM_LDFLAGS) 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_CPPFLAGS = -DMAIN $(AM_CPPFLAGS) diff --git a/mount/fsprobe.c b/mount/fsprobe.c index 4b578026..2629d0d5 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 d25b9731..2dc734e5 100644 --- a/mount/fsprobe_blkid.c +++ b/mount/fsprobe_blkid.c @@ -24,11 +24,6 @@ fsprobe_get_uuid_by_devname(const char *devname) { return blkid_get_tag_value(blkid, "UUID", 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 cdfbb42e..45404c5c 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 08511456..85ccd85f 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 8c3c1bdf..a8e5cc10 100644 --- a/mount/swapon.c +++ b/mount/swapon.c @@ -34,6 +34,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; -- 2.39.5