2 * resolve.c - resolve names and tags into specific devices
4 * Copyright (C) 2001, 2003 Theodore Ts'o.
5 * Copyright (C) 2001 Andreas Dilger
8 * This file may be redistributed under the terms of the
9 * GNU Lesser General Public License.
21 #include <sys/types.h>
26 * Find a tagname (e.g. LABEL or UUID) on a specific device.
28 char *blkid_get_tag_value(blkid_cache cache, const char *tagname,
33 blkid_cache c = cache;
36 DBG(DEBUG_RESOLVE, printf("looking for %s on %s\n", tagname, devname));
42 if (blkid_get_cache(&c, NULL) < 0)
46 if ((dev = blkid_get_dev(c, devname, BLKID_DEV_NORMAL)) &&
47 (found = blkid_find_tag_dev(dev, tagname)))
48 ret = blkid_strdup(found->bit_val);
57 * Locate a device name from a token (NAME=value string), or (name, value)
58 * pair. In the case of a token, value is ignored. If the "token" is not
59 * of the form "NAME=value" and there is no value given, then it is assumed
60 * to be the actual devname and a copy is returned.
62 char *blkid_get_devname(blkid_cache cache, const char *token,
66 blkid_cache c = cache;
74 if (blkid_get_cache(&c, NULL) < 0)
79 printf("looking for %s%s%s %s\n", token, value ? "=" : "",
80 value ? value : "", cache ? "in cache" : "from disk"));
83 if (!strchr(token, '=')) {
84 ret = blkid_strdup(token);
87 blkid_parse_tag_string(token, &t, &v);
94 dev = blkid_find_dev_with_tag(c, token, value);
98 ret = blkid_strdup(blkid_dev_devname(dev));
110 int main(int argc, char **argv)
115 blkid_debug_mask = DEBUG_ALL;
116 if (argc != 2 && argc != 3) {
117 fprintf(stderr, "Usage:\t%s tagname=value\n"
118 "\t%s tagname devname\n"
119 "Find which device holds a given token or\n"
120 "Find what the value of a tag is in a device\n",
124 if (blkid_get_cache(&cache, "/dev/null") < 0) {
125 fprintf(stderr, "Couldn't get blkid cache\n");
130 value = blkid_get_tag_value(cache, argv[1], argv[2]);
131 printf("%s has tag %s=%s\n", argv[2], argv[1],
132 value ? value : "<missing>");
134 value = blkid_get_devname(cache, argv[1], NULL);
135 printf("%s has tag %s\n", value ? value : "<none>", argv[1]);
137 blkid_put_cache(cache);
138 return value ? 0 : 1;