]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libmount: add mnt_fs_streq_target()
authorKarel Zak <kzak@redhat.com>
Fri, 2 Mar 2012 14:53:55 +0000 (15:53 +0100)
committerKarel Zak <kzak@redhat.com>
Tue, 13 Mar 2012 11:56:08 +0000 (12:56 +0100)
Note that in v2.21.x the function mnt_fs_streq_ are not exported by
libmount API.

Signed-off-by: Karel Zak <kzak@redhat.com>
libmount/src/fs.c
libmount/src/mountP.h
libmount/src/tab.c
libmount/src/tab_parse.c
sys-utils/mount.c

index ba57dabb34dbd4213415f89219fa9d61a81d1eae..19650cbf020ce62de5ddec0b9d017d87c3cfadcb 100644 (file)
@@ -350,22 +350,40 @@ int mnt_fs_set_source(struct libmnt_fs *fs, const char *source)
        return rc;
 }
 
+/*
+ * Compares @fs source path with @path. The tailing slash is ignored.
+ * See also mnt_fs_match_source().
+ *
+ * Returns: 1 if @fs source path equal to @path, otherwise 0.
+ */
 int mnt_fs_streq_srcpath(struct libmnt_fs *fs, const char *path)
 {
-       const char *p = mnt_fs_get_srcpath(fs);
+       const char *p;
 
-       if (p == NULL && path == NULL)
-               return 1;
-       if (p == NULL || path == NULL)
+       if (!fs)
                return 0;
 
-       if (mnt_fs_is_pseudofs(fs))
-               /* don't think about pseudo-fs source as about path */
-               return strcmp(p, path) == 0;
+       p = mnt_fs_get_srcpath(fs);
+
+       if (!mnt_fs_is_pseudofs(fs))
+               return streq_except_trailing_slash(p, path);
 
-       return streq_except_trailing_slash(p, path);
+       if (!p && !path)
+               return 1;
+
+       return p && path && strcmp(p, path) == 0;
 }
 
+/*
+ * Compares @fs target path with @path. The tailing slash is ignored.
+ * See also mnt_fs_match_target().
+ *
+ * Returns: 1 if @fs target path equal to @path, otherwise 0.
+ */
+int mnt_fs_streq_target(struct libmnt_fs *fs, const char *path)
+{
+       return fs && streq_except_trailing_slash(mnt_fs_get_target(fs), path);
+}
 
 /**
  * mnt_fs_get_tag:
@@ -1128,7 +1146,7 @@ int mnt_fs_match_target(struct libmnt_fs *fs, const char *target,
                return 0;
 
        /* 1) native paths */
-       rc = !strcmp(target, fs->target);
+       rc = mnt_fs_streq_target(fs, target);
 
        if (!rc && cache) {
                /* 2) - canonicalized and non-canonicalized */
index f62acbd1011d3db37903497ecee0d90995e40333..a7b5c0d5d15c9f058c085990ba5d78b30af98179 100644 (file)
@@ -366,7 +366,11 @@ extern int mnt_optstr_fix_user(char **optstr);
 extern struct libmnt_fs *mnt_copy_mtab_fs(const struct libmnt_fs *fs);
 extern int __mnt_fs_set_source_ptr(struct libmnt_fs *fs, char *source);
 extern int __mnt_fs_set_fstype_ptr(struct libmnt_fs *fs, char *fstype);
+
+/* exported in v2.22 */
 extern int mnt_fs_streq_srcpath(struct libmnt_fs *fs, const char *path);
+extern int mnt_fs_streq_target(struct libmnt_fs *fs, const char *path);
+
 
 /* context.c */
 extern int mnt_context_prepare_srcpath(struct libmnt_context *cxt);
index d2a954bbabba10fab4ca981a819e16a092e2aad1..9992c9cf17f80501a90825507998e177f7dc9758 100644 (file)
@@ -438,7 +438,7 @@ struct libmnt_fs *mnt_table_find_target(struct libmnt_table *tb, const char *pat
        /* native @target */
        mnt_reset_iter(&itr, direction);
        while(mnt_table_next_fs(tb, &itr, &fs) == 0) {
-               if (fs->target && streq_except_trailing_slash(fs->target, path))
+               if (mnt_fs_streq_target(fs, path))
                        return fs;
        }
        if (!tb->cache || !(cn = mnt_resolve_path(path, tb->cache)))
@@ -447,7 +447,7 @@ struct libmnt_fs *mnt_table_find_target(struct libmnt_table *tb, const char *pat
        /* canonicalized paths in struct libmnt_table */
        mnt_reset_iter(&itr, direction);
        while(mnt_table_next_fs(tb, &itr, &fs) == 0) {
-               if (fs->target && strcmp(fs->target, cn) == 0)
+               if (mnt_fs_streq_target(fs, cn))
                        return fs;
        }
 
@@ -461,7 +461,8 @@ struct libmnt_fs *mnt_table_find_target(struct libmnt_table *tb, const char *pat
                       continue;
 
                p = mnt_resolve_path(fs->target, tb->cache);
-               if (strcmp(cn, p) == 0)
+               /* both canonicalized, strcmp() is fine here */
+               if (p && strcmp(cn, p) == 0)
                        return fs;
        }
        return NULL;
@@ -544,7 +545,9 @@ struct libmnt_fs *mnt_table_find_srcpath(struct libmnt_table *tb, const char *pa
                                 if (mnt_fs_get_tag(fs, &t, &v))
                                         continue;
                                 x = mnt_resolve_tag(t, v, tb->cache);
-                                if (x && streq_except_trailing_slash(x, cn))
+
+                                /* both canonicalized, strcmp() is fine here */
+                                if (x && strcmp(x, cn) == 0)
                                         return fs;
                         }
                }
@@ -559,7 +562,9 @@ struct libmnt_fs *mnt_table_find_srcpath(struct libmnt_table *tb, const char *pa
                        p = mnt_fs_get_srcpath(fs);
                        if (p)
                                p = mnt_resolve_path(p, tb->cache);
-                       if (p && streq_except_trailing_slash(cn, p))
+
+                       /* both canonicalized, strcmp() is fine here */
+                       if (p && strcmp(p, cn) == 0)
                                return fs;
                }
        }
@@ -853,14 +858,11 @@ int mnt_table_is_fs_mounted(struct libmnt_table *tb, struct libmnt_fs *fstab_fs)
                mnt_reset_iter(&itr, MNT_ITER_FORWARD);
 
                while(mnt_table_next_fs(tb, &itr, &fs) == 0) {
-                       const char *t = mnt_fs_get_target(fs),
-                                  *r = mnt_fs_get_root(fs);
+                       const char *r = mnt_fs_get_root(fs);
 
-                       if (t && r
-                           && mnt_fs_get_srcpath(fs)
+                       if (r && strcmp(r, root) == 0
                            && mnt_fs_streq_srcpath(fs, src)
-                           && streq_except_trailing_slash(t, tgt)
-                           && strcmp(r, root) == 0)
+                           && mnt_fs_streq_target(fs, tgt))
                                break;
                }
                if (fs)
index 4d581c31e5d0908bbf4f0b41b8d25b2082d03bd2..28c8536afec2017897c03928ab13e91e2f010d38 100644 (file)
@@ -728,21 +728,14 @@ static struct libmnt_fs *mnt_table_merge_user_fs(struct libmnt_table *tb, struct
        mnt_reset_iter(&itr, MNT_ITER_BACKWARD);
 
        while(mnt_table_next_fs(tb, &itr, &fs) == 0) {
-               const char *s = mnt_fs_get_srcpath(fs),
-                          *t = mnt_fs_get_target(fs),
-                          *r = mnt_fs_get_root(fs);
+               const char *r = mnt_fs_get_root(fs);
 
                if (fs->flags & MNT_FS_MERGED)
                        continue;
 
-               /*
-                * Note that kernel can add tailing slash to the network
-                * filesystem source path
-                */
-               if (s && t && r &&
-                   strcmp(t, target) == 0 &&
-                   mnt_fs_streq_srcpath(fs, src) &&
-                   strcmp(r, root) == 0)
+               if (r && strcmp(r, root) == 0
+                   && mnt_fs_streq_target(fs, target)
+                   && mnt_fs_streq_srcpath(fs, src))
                        break;
        }
 
index 6a490b9e84405ad059d376432cb542725089f670..12ec372efcfeae4a1e2cd7bfc2f05c7499abdfca 100644 (file)
@@ -301,6 +301,25 @@ static void selinux_warning(struct libmnt_context *cxt, const char *tgt)
 # define selinux_warning(_x, _y)
 #endif
 
+/* temporary in mount(8) for v2.21.x releases, in v2.22 will be in libmount
+ */
+static int mnt_fs_streq_srcpath(struct libmnt_fs *fs, const char *path)
+{
+       const char *p;
+
+       if (!fs)
+               return 0;
+
+       p = mnt_fs_get_srcpath(fs);
+
+       if (!mnt_fs_is_pseudofs(fs))
+               return streq_except_trailing_slash(p, path);
+
+       if (!p && !path)
+               return 1;
+
+       return p && path && strcmp(p, path) == 0;
+}
 
 /*
  * rc = 0 success
@@ -423,7 +442,7 @@ try_readonly:
                                const char *s = mnt_fs_get_srcpath(fs),
                                           *t = mnt_fs_get_target(fs);
 
-                               if (t && s && mnt_fs_streq_strpath(fs, src))
+                               if (t && s && mnt_fs_streq_srcpath(fs, src))
                                        fprintf(stderr, _(
                                                "       %s is already mounted on %s\n"), s, t);
                        }