From: Karel Zak Date: Tue, 3 Mar 2020 09:55:28 +0000 (+0100) Subject: libmount: move "already mounted" code to separate function X-Git-Tag: v2.36-rc1~201 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=acd229fc2afe0c402cca3b21e0490be71dec2725;p=thirdparty%2Futil-linux.git libmount: move "already mounted" code to separate function Signed-off-by: Karel Zak --- diff --git a/libmount/src/context_mount.c b/libmount/src/context_mount.c index 088d1328e1..0c4b7657bb 100644 --- a/libmount/src/context_mount.c +++ b/libmount/src/context_mount.c @@ -1119,6 +1119,36 @@ int mnt_context_do_mount(struct libmnt_context *cxt) return res; } +/* + * Returns mountinfo FS entry of context source patch if the source is already + * mounted. This function is used for "already mounted" message or to get FS of + * re-used loop device. + */ +static struct libmnt_fs *get_already_mounted_source(struct libmnt_context *cxt) +{ + const char *src; + struct libmnt_table *tb; + + assert(cxt); + + src = mnt_fs_get_srcpath(cxt->fs); + + if (src && mnt_context_get_mtab(cxt, &tb) == 0) { + struct libmnt_iter itr; + struct libmnt_fs *fs; + + mnt_reset_iter(&itr, MNT_ITER_FORWARD); + while (mnt_table_next_fs(tb, &itr, &fs) == 0) { + const char *s = mnt_fs_get_srcpath(fs), + *t = mnt_fs_get_target(fs); + + if (t && s && mnt_fs_streq_srcpath(fs, src)) + return fs; + } + } + return NULL; +} + /** * mnt_context_finalize_mount: * @cxt: context @@ -1721,34 +1751,22 @@ int mnt_context_get_mount_excode( break; case EBUSY: - { - struct libmnt_table *tb; - if (!buf) break; if (mflags & MS_REMOUNT) { snprintf(buf, bufsz, _("mount point is busy")); break; } - if (src && mnt_context_get_mtab(cxt, &tb) == 0) { - struct libmnt_iter itr; - struct libmnt_fs *fs; - - mnt_reset_iter(&itr, MNT_ITER_FORWARD); - while (mnt_table_next_fs(tb, &itr, &fs) == 0) { - const char *s = mnt_fs_get_srcpath(fs), - *t = mnt_fs_get_target(fs); - - if (t && s && mnt_fs_streq_srcpath(fs, src)) { - snprintf(buf, bufsz, _("%s already mounted on %s"), s, t); - break; - } - } + if (src) { + struct libmnt_fs *fs = get_already_mounted_source(cxt); + + if (fs && mnt_fs_get_target(fs)) + snprintf(buf, bufsz, _("%s already mounted on %s"), + src, mnt_fs_get_target(fs)); } if (!*buf) snprintf(buf, bufsz, _("%s already mounted or mount point busy"), src); break; - } case ENOENT: if (tgt && lstat(tgt, &st)) { if (buf)