/** * 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) { char *cn; const char *src, *t, *v; if (!fs || !source || !fs->source) return 0; /* 1) native paths/tags */ if (!strcmp(source, fs->source)) return 1; if (!cache) return 0; 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 && !strcmp(cn, src)) return 1; /* 3) canonicalized and canonicalized */ if (src) { src = mnt_resolve_path(src, cache); if (src && !strcmp(cn, src)) return 1; } if (src || mnt_fs_get_tag(fs, &t, &v)) /* src path does not match and tag is not defined */ return 0; /* read @source's tags to the cache */ 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. * * (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 1; 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(struct libmnt_cache *cache, const char *devname, const char *token) { if (!cache || !devname || !token) return NULL; if (mnt_cache_read_tags(cache, devname) != 0) return NULL; return cache_find_tag_value(cache, devname, token); }
int test_read_tags(struct libmnt_test *ts, int argc, char *argv[]) { char line[BUFSIZ]; struct libmnt_cache *cache; int i; cache = mnt_new_cache(); if (!cache) return -ENOMEM; while(fgets(line, sizeof(line), stdin)) { size_t sz = strlen(line); if (line[sz - 1] == '\n') line[sz - 1] = '\0'; if (!strcmp(line, "quit")) break; if (*line == '/') { if (mnt_cache_read_tags(cache, line) < 0) fprintf(stderr, "%s: read tags failed\n", line); } else if (strchr(line, '=')) { char *tag, *val; const char *cn = NULL; if (!blkid_parse_tag_string(line, &tag, &val)) { cn = cache_find_tag(cache, tag, val); free(tag); free(val); } if (cn) printf("%s: %s\n", line, cn); else printf("%s: not cached\n", line); } } for (i = 0; i < cache->nents; i++) { struct mnt_cache_entry *e = &cache->ents[i]; if (!(e->flag & MNT_CACHE_ISTAG)) continue; printf("%15s : %5s : %s\n", e->value, e->key, e->key + strlen(e->key) + 1); } mnt_free_cache(cache); return 0; }
static int __mnt_cache_find_tag_value(struct libmnt_cache *cache, const char *devname, const char *token, char **data) { int rc = 0; if (!cache || !devname || !token || !data) return -EINVAL; rc = mnt_cache_read_tags(cache, devname); if (rc) return rc; *data = cache_find_tag_value(cache, devname, token); return *data ? 0 : -1; }
/** * 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(struct libmnt_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; }