summaryrefslogtreecommitdiffstats
path: root/shlibs/mount/src/fs.c
diff options
context:
space:
mode:
authorKarel Zak2010-03-01 22:29:38 +0100
committerKarel Zak2010-06-03 15:20:11 +0200
commit3fca8422b154e047055ed938e81005ea6129d86a (patch)
tree05c23fe942f01df825d464546c299d66581488ec /shlibs/mount/src/fs.c
parentlibmount: add mnt_match_{fstype,options} functions (diff)
downloadkernel-qcow2-util-linux-3fca8422b154e047055ed938e81005ea6129d86a.tar.gz
kernel-qcow2-util-linux-3fca8422b154e047055ed938e81005ea6129d86a.tar.xz
kernel-qcow2-util-linux-3fca8422b154e047055ed938e81005ea6129d86a.zip
libmount: add fstab/mtab/mountinfo lookup routines
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'shlibs/mount/src/fs.c')
-rw-r--r--shlibs/mount/src/fs.c149
1 files changed, 149 insertions, 0 deletions
diff --git a/shlibs/mount/src/fs.c b/shlibs/mount/src/fs.c
index 0e12052d5..01c2d0277 100644
--- a/shlibs/mount/src/fs.c
+++ b/shlibs/mount/src/fs.c
@@ -11,6 +11,7 @@
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
+#include <errno.h>
#include <blkid/blkid.h>
#include "nls.h"
@@ -415,6 +416,154 @@ int mnt_fs_get_option(mnt_fs *fs, const char *name,
}
+/**
+ * mnt_fs_match_target:
+ * @fs: filesystem
+ * @target: mountpoint path
+ * @cache: tags/paths cache or NULL
+ *
+ * Possible are three attempts:
+ * 1) compare @target with @fs->target
+ * 2) realpath(@target) with @fs->target
+ * 3) realpath(@target) with realpath(@fs->target).
+ *
+ * The 2nd and 3rd attempts are not performed when @cache is NULL.
+ *
+ * Returns 1 if @fs target is equal to @target else 0.
+ */
+int mnt_fs_match_target(mnt_fs *fs, const char *target, mnt_cache *cache)
+{
+ int rc = 0;
+
+ if (!fs || !target || !fs->target)
+ return 0;
+
+ /* 1) native paths */
+ rc = !strcmp(target, fs->target);
+
+ if (!rc && cache) {
+ /* 2) - canonicalized and non-canonicalized */
+ char *cn = mnt_resolve_path(target, cache);
+ rc = (cn && strcmp(cn, fs->target) == 0);
+
+ /* 3) - canonicalized and canonicalized */
+ if (!rc && cn) {
+ char *tcn = mnt_resolve_path(fs->target, cache);
+ rc = (tcn && strcmp(cn, tcn) == 0);
+ }
+ }
+
+ return rc;
+}
+
+/**
+ * mnt_fs_match_source:
+ * @fs: filesystem
+ * @source: tag or path (device or so)
+ * @cache: tags/paths cache or NULL
+ *
+ * Possible are four attempts:
+ * 1) compare @source with @fs->source
+ * 2) compare realpath(@source) with @fs->source
+ * 3) compare realpath(@source) with realpath(@fs->source)
+ * 4) compare realpath(@source) with evaluated tag from @fs->source
+ *
+ * The 2nd, 3rd and 4th attempts are not performed when @cache is NULL. The
+ * 2nd and 3rd attempts are not performed if @fs->source is tag.
+ *
+ * Returns 1 if @fs source is equal to @source else 0.
+ */
+int mnt_fs_match_source(mnt_fs *fs, const char *source, mnt_cache *cache)
+{
+ int rc = 0;
+ char *cn;
+ const char *src, *t, *v;
+
+ if (!fs || !source || !fs->source)
+ return 0;
+
+ /* 1) native paths/tags */
+ rc = !strcmp(source, fs->source);
+ if (rc || !cache)
+ return rc;
+
+ if (fs->flags & (MNT_FS_NET | MNT_FS_PSEUDO))
+ return 0;
+
+ cn = mnt_resolve_spec(source, cache);
+ if (!cn)
+ return 0;
+
+ /* 2) canonicalized and native */
+ src = mnt_fs_get_srcpath(fs);
+ if (src)
+ rc = !strcmp(cn, src);
+
+ /* 3) canonicalized and canonicalized */
+ if (src && !rc) {
+ src = mnt_resolve_path(src, cache);
+ rc = !strcmp(cn, src);
+ }
+ if (src && !rc)
+ /* fs->source is path and does not match with @source */
+ return 0;
+
+ if (mnt_fs_get_tag(fs, &t, &v))
+ return 0;
+
+ /* read @source's tags to the cache */
+ if (mnt_cache_read_tags(cache, cn) < 1) {
+ if (errno == EACCES) {
+ /* we don't have permissions to read TAGs from
+ * @source, but can translate @fs tag to devname.
+ *
+ * (because libblkid uses udev symlinks and this is
+ * accessible for non-root uses)
+ */
+ char *x = mnt_resolve_tag(t, v, cache);
+ if (x && !strcmp(x, cn))
+ return 1;
+ }
+ return 0;
+ }
+
+ /* 4) has the @source a tag that matches with tag from @fs ? */
+ if (!mnt_cache_device_has_tag(cache, cn, t, v))
+ return 0;
+
+ return 1;
+}
+
+/**
+ * mnt_fs_match_fstype:
+ * @fs: filesystem
+ * @types: filesystem name or comma delimited list of filesystems
+ *
+ * For more details see mnt_match_fstype().
+ *
+ * Returns 1 if @fs type is matching to @types else 0. The function returns
+ * 0 when types is NULL.
+ */
+int mnt_fs_match_fstype(mnt_fs *fs, const char *types)
+{
+ return mnt_match_fstype(fs->fstype, types);
+}
+
+/**
+ * mnt_fs_match_options:
+ * @fs: filesystem
+ * @options: comma delimited list of options (and nooptions)
+ *
+ * For more details see mnt_match_options().
+ *
+ * Returns 1 if @fs type is matching to @options else 0. The function returns
+ * 0 when types is NULL.
+ */
+int mnt_fs_match_options(mnt_fs *fs, const char *options)
+{
+ return mnt_match_options(fs->optstr, options);
+}
+
/* Unfortunately the classical Unix /etc/mtab and /etc/fstab
do not handle directory names containing spaces.
Here we mangle them, replacing a space by \040.