From: Karel Zak Date: Fri, 12 Mar 2010 22:42:56 +0000 (+0100) Subject: libmount: add mnt_cache_find_tag_value() X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ba7232a1fe1398e7947d1af39fadc8d02b21bf69;p=util-linux libmount: add mnt_cache_find_tag_value() Signed-off-by: Karel Zak --- diff --git a/shlibs/mount/src/cache.c b/shlibs/mount/src/cache.c index b9d89f43..62d514d8 100644 --- a/shlibs/mount/src/cache.c +++ b/shlibs/mount/src/cache.c @@ -220,7 +220,7 @@ const char *mnt_cache_find_tag(mnt_cache *cache, * * Reads @devname LABEL and UUID to the @cache. * - * Returns: 1 if at least on tag was added, 0 no tag was added or + * Returns: 0 if at least on tag was added, 1 if no tag was added or * -1 in case of error. */ int mnt_cache_read_tags(mnt_cache *cache, const char *devname) @@ -289,7 +289,7 @@ int mnt_cache_read_tags(mnt_cache *cache, const char *devname) ntags++; } - return ntags ? 1 : 0; + return ntags ? 0 : 1; error: blkid_free_probe(pr); close(fd); @@ -317,6 +317,37 @@ int mnt_cache_device_has_tag(mnt_cache *cache, const char *devname, return 0; } +/** + * mnt_cache_find_tag_value: + * @cache: cache for results + * @devname: device name + * @token: tag name ("LABEL" or "UUID") + * + * Returns: LABEL or UUID for the @devname or NULL in case of error. + */ +char *mnt_cache_find_tag_value(mnt_cache *cache, + const char *devname, const char *token) +{ + int i; + + if (!cache || !devname || !token) + return NULL; + + if (mnt_cache_read_tags(cache, devname) != 0) + return NULL; + + for (i = 0; i < cache->nents; i++) { + struct mnt_cache_entry *e = &cache->ents[i]; + if (!(e->flag & MNT_CACHE_ISTAG)) + continue; + if (strcmp(e->real, devname) == 0 && /* dev name */ + strcmp(token, e->native) == 0) /* tag name */ + return e->native + strlen(token) + 1; /* tag value */ + } + + return NULL; +} + /** * mnt_resolve_path: * @path: "native" path diff --git a/shlibs/mount/src/fs.c b/shlibs/mount/src/fs.c index ae97b654..aa38998b 100644 --- a/shlibs/mount/src/fs.c +++ b/shlibs/mount/src/fs.c @@ -574,7 +574,7 @@ int mnt_fs_match_source(mnt_fs *fs, const char *source, mnt_cache *cache) return 0; /* read @source's tags to the cache */ - if (mnt_cache_read_tags(cache, cn) < 1) { + if (mnt_cache_read_tags(cache, cn) < 0) { if (errno == EACCES) { /* we don't have permissions to read TAGs from * @source, but can translate @fs tag to devname. diff --git a/shlibs/mount/src/mount.h.in b/shlibs/mount/src/mount.h.in index 4e918b83..7d13e706 100644 --- a/shlibs/mount/src/mount.h.in +++ b/shlibs/mount/src/mount.h.in @@ -123,6 +123,10 @@ extern const char *mnt_cache_find_tag(mnt_cache *cache, extern int mnt_cache_read_tags(mnt_cache *cache, const char *devname); extern int mnt_cache_device_has_tag(mnt_cache *cache, const char *devname, const char *token, const char *value); + +extern char *mnt_cache_find_tag_value(mnt_cache *cache, + const char *devname, const char *token); + extern char *mnt_resolve_path(const char *path, mnt_cache *cache); extern char *mnt_resolve_tag(const char *token, const char *value, mnt_cache *cache); extern char *mnt_resolve_spec(const char *spec, mnt_cache *cache); diff --git a/shlibs/mount/src/mount.sym b/shlibs/mount/src/mount.sym index 5df37796..358d7b73 100644 --- a/shlibs/mount/src/mount.sym +++ b/shlibs/mount/src/mount.sym @@ -8,6 +8,7 @@ global: mnt_cache_device_has_tag; mnt_cache_find_path; mnt_cache_find_tag; + mnt_cache_find_tag_value; mnt_cache_read_tags; mnt_fprintf_line; mnt_free_cache; diff --git a/shlibs/mount/src/tab.c b/shlibs/mount/src/tab.c index 98c56602..176a7d5a 100644 --- a/shlibs/mount/src/tab.c +++ b/shlibs/mount/src/tab.c @@ -519,9 +519,11 @@ mnt_fs *mnt_tab_find_srcpath(mnt_tab *tb, const char *path, int direction) /* evaluated tag */ if (ntags) { + int rc = mnt_cache_read_tags(tb->cache, cn); + mnt_reset_iter(&itr, direction); - if (mnt_cache_read_tags(tb->cache, cn) > 0) { + if (rc == 0) { /* @path's TAGs are in the cache */ while(mnt_tab_next_fs(tb, &itr, &fs) == 0) { const char *t, *v; @@ -532,7 +534,7 @@ mnt_fs *mnt_tab_find_srcpath(mnt_tab *tb, const char *path, int direction) if (mnt_cache_device_has_tag(tb->cache, cn, t, v)) return fs; } - } else if (errno == EACCES) { + } else if (rc < 0 && errno == EACCES) { /* @path is unaccessible, try evaluate all TAGs in @tb * by udev symlinks -- this could be expensive on systems * with huge fstab/mtab */