From: Karel Zak Date: Mon, 23 Jun 2014 10:42:33 +0000 (+0200) Subject: libmount: special treatment for auto in fstype pattern X-Git-Tag: v2.25-rc2~43 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b1f03df7983975919b2d2f6eaa3175212ce61bee;p=thirdparty%2Futil-linux.git libmount: special treatment for auto in fstype pattern Let's support mount -t ext2,auto /dev/sde /media/stick Reported-by: Andreas Henriksson Addresses: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=506695 Signed-off-by: Karel Zak --- diff --git a/libmount/src/context.c b/libmount/src/context.c index 65171eceb4..7d2b9e40fc 100644 --- a/libmount/src/context.c +++ b/libmount/src/context.c @@ -1601,6 +1601,40 @@ int mnt_context_prepare_target(struct libmnt_context *cxt) return 0; } +/* Guess type, but not set to cxt->fs, use free() for the result. It's no error + * when we're not able to guess a filesystem type. + */ +int mnt_context_guess_srcpath_fstype(struct libmnt_context *cxt, char **type) +{ + int rc = 0; + const char *dev = mnt_fs_get_srcpath(cxt->fs); + + *type = NULL; + + if (!dev) + goto done; + + if (access(dev, F_OK) == 0) { + struct libmnt_cache *cache = mnt_context_get_cache(cxt); + int ambi = 0; + + *type = mnt_get_fstype(dev, &ambi, cache); + if (cache && *type) + *type = strdup(*type); + if (ambi) + rc = -MNT_ERR_AMBIFS; + } else { + DBG(CXT, ul_debugobj(cxt, "access(%s) failed [%m]", dev)); + if (strchr(dev, ':') != NULL) + *type = strdup("nfs"); + else if (!strncmp(dev, "//", 2)) + *type = strdup("cifs"); + } + +done: + return rc; +} + /* * It's usually no error when we're not able to detect the filesystem type -- we * will try to use the types from /{etc,proc}/filesystems. @@ -1608,7 +1642,6 @@ int mnt_context_prepare_target(struct libmnt_context *cxt) int mnt_context_guess_fstype(struct libmnt_context *cxt) { char *type; - const char *dev; int rc = 0; assert(cxt); @@ -1635,30 +1668,11 @@ int mnt_context_guess_fstype(struct libmnt_context *cxt) if (cxt->fstype_pattern) goto done; - dev = mnt_fs_get_srcpath(cxt->fs); - if (!dev) - goto done; - - if (access(dev, F_OK) == 0) { - struct libmnt_cache *cache = mnt_context_get_cache(cxt); - int ambi = 0; - - type = mnt_get_fstype(dev, &ambi, cache); - if (type) { - rc = mnt_fs_set_fstype(cxt->fs, type); - if (!cache) - free(type); /* type is not cached */ - } - if (ambi) - rc = -MNT_ERR_AMBIFS; - } else { - DBG(CXT, ul_debugobj(cxt, "access(%s) failed [%m]", dev)); - if (strchr(dev, ':') != NULL) - rc = mnt_fs_set_fstype(cxt->fs, "nfs"); - else if (!strncmp(dev, "//", 2)) - rc = mnt_fs_set_fstype(cxt->fs, "cifs"); - } - + rc = mnt_context_guess_srcpath_fstype(cxt, &type); + if (rc == 0 && type) + __mnt_fs_set_fstype_ptr(cxt->fs, type); + else + free(type); done: DBG(CXT, ul_debugobj(cxt, "FS type: %s [rc=%d]", mnt_fs_get_fstype(cxt->fs), rc)); diff --git a/libmount/src/context_mount.c b/libmount/src/context_mount.c index dcfdabfd22..14ae65254c 100644 --- a/libmount/src/context_mount.c +++ b/libmount/src/context_mount.c @@ -773,18 +773,36 @@ static int do_mount_by_pattern(struct libmnt_context *cxt, const char *pattern) */ char *p, *p0; - DBG(CXT, ul_debugobj(cxt, "trying to mount by FS pattern list")); + DBG(CXT, ul_debugobj(cxt, "trying to mount by FS pattern list '%s'", pattern)); p0 = p = strdup(pattern); if (!p) return -ENOMEM; do { + char *autotype = NULL; char *end = strchr(p, ','); + if (end) *end = '\0'; - rc = do_mount(cxt, p); - p = end ? end + 1 : NULL; + DBG(CXT, ul_debugobj(cxt, "-->trying '%s'", p)); + + /* Let's support things like "udf,iso9660,auto" */ + if (strcmp(p, "auto") == 0) { + rc = mnt_context_guess_srcpath_fstype(cxt, &autotype); + if (rc) { + DBG(CXT, ul_debugobj(cxt, "failed to guess FS type")); + free(p0); + return rc; + } + p = autotype; + DBG(CXT, ul_debugobj(cxt, " --> '%s'", p)); + } + + if (p) + rc = do_mount(cxt, p); + p = end ? end + 1 : NULL; + free(autotype); } while (!mnt_context_get_status(cxt) && p); free(p0); diff --git a/libmount/src/mountP.h b/libmount/src/mountP.h index f2660e0574..f2d0a57681 100644 --- a/libmount/src/mountP.h +++ b/libmount/src/mountP.h @@ -378,6 +378,7 @@ extern const char *mnt_context_get_writable_tabpath(struct libmnt_context *cxt); extern int mnt_context_prepare_srcpath(struct libmnt_context *cxt); extern int mnt_context_prepare_target(struct libmnt_context *cxt); +extern int mnt_context_guess_srcpath_fstype(struct libmnt_context *cxt, char **type); extern int mnt_context_guess_fstype(struct libmnt_context *cxt); extern int mnt_context_prepare_helper(struct libmnt_context *cxt, const char *name, const char *type);