2 * resolve.c - resolve names and tags into specific devices
4 * Copyright (C) 2001 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.
19 #include <sys/types.h>
21 #include "blkid/blkid.h"
25 #define DEB_RESOLVE(fmt, arg...) printf("resolve: " fmt, ## arg)
27 #define DEB_RESOLVE(fmt, arg...) do {} while (0)
32 * Find a tagname (e.g. LABEL or UUID) on a specific device.
34 char *blkid_get_tagname_devname(blkid_cache *cache, const char *tagname,
37 blkid_tag *tag, *found;
41 DEB_RESOLVE("looking for %s on %s\n", tagname, devname);
46 if (blkid_create_tag(NULL, &tag, tagname, NULL, 0) < 0)
50 DEB_RESOLVE("no cache given, direct device probe\n");
52 if ((dev = blkid_get_devname(cache, devname)) &&
53 (found = blkid_find_tag_dev(dev, tag)))
54 ret = string_copy(found->bit_val);
63 * Locate a device name from a token (NAME=value string), or (name, value)
64 * pair. In the case of a token, value is ignored. If the "token" is not
65 * of the form "NAME=value" and there is no value given, then it is assumed
66 * to be the actual devname and a copy is returned.
68 * The string returned must be freed with string_free().
70 char *blkid_get_token(blkid_cache *cache, const char *token,
73 blkid_tag *tag = NULL, *found = NULL;
74 blkid_cache *c = cache;
77 DEB_RESOLVE("looking for %s%c%s %s\n", token, value ? '=' : ' ',
78 value ? value : "", cache ? "in cache" : "from disk");
80 if (!(tag = blkid_token_to_tag(token))) {
82 return string_copy(token);
83 if (blkid_create_tag(NULL,&tag,token,value,strlen(value)) < 0)
88 if (blkid_read_cache(&c, NULL) < 0)
89 c = blkid_new_cache();
94 if ((found = blkid_get_tag_cache(c, tag)) && found->bit_dev)
95 name = string_copy(found->bit_dev->bid_name);
98 blkid_save_cache(c, NULL);
108 int main(int argc, char **argv)
112 if (argc != 2 && argc != 3) {
113 fprintf(stderr, "Usage:\t%s tagname=value\n"
114 "\t%s tagname devname\n"
115 "Find which device holds a given token or\n"
116 "Find what the value of a tag is in a device\n",
121 value = blkid_get_tagname_devname(NULL, argv[1], argv[2]);
122 printf("%s has tag %s=%s\n", argv[2], argv[1],
123 value ? value : "<missing>");
125 value = blkid_get_token(NULL, argv[1], NULL);
126 printf("%s has tag %s\n", value ? value : "<none>", argv[1]);
128 return value ? 0 : 1;