1 /* vi: set sw=4 ts=4: */
3 * resolve.c - resolve names and tags into specific devices
5 * Copyright (C) 2001, 2003 Theodore Ts'o.
6 * Copyright (C) 2001 Andreas Dilger
9 * This file may be redistributed under the terms of the
10 * GNU Lesser General Public License.
21 #include <sys/types.h>
27 * Find a tagname (e.g. LABEL or UUID) on a specific device.
29 char *blkid_get_tag_value(blkid_cache cache
, const char *tagname
,
34 blkid_cache c
= cache
;
37 DBG(DEBUG_RESOLVE
, printf("looking for %s on %s\n", tagname
, devname
));
43 if (blkid_get_cache(&c
, NULL
) < 0)
47 if ((dev
= blkid_get_dev(c
, devname
, BLKID_DEV_NORMAL
)) &&
48 (found
= blkid_find_tag_dev(dev
, tagname
)))
49 ret
= blkid_strdup(found
->bit_val
);
58 * Locate a device name from a token (NAME=value string), or (name, value)
59 * pair. In the case of a token, value is ignored. If the "token" is not
60 * of the form "NAME=value" and there is no value given, then it is assumed
61 * to be the actual devname and a copy is returned.
63 char *blkid_get_devname(blkid_cache cache
, const char *token
,
67 blkid_cache c
= cache
;
68 char *t
= NULL
, *v
= NULL
;
75 if (blkid_get_cache(&c
, NULL
) < 0)
80 printf("looking for %s%s%s %s\n", token
, value
? "=" : "",
81 value
? value
: "", cache
? "in cache" : "from disk"));
84 if (!strchr(token
, '='))
85 return blkid_strdup(token
);
86 blkid_parse_tag_string(token
, &t
, &v
);
93 dev
= blkid_find_dev_with_tag(c
, token
, value
);
97 ret
= blkid_strdup(blkid_dev_devname(dev
));
109 int main(int argc
, char **argv
)
114 blkid_debug_mask
= DEBUG_ALL
;
115 if (argc
!= 2 && argc
!= 3) {
116 fprintf(stderr
, "Usage:\t%s tagname=value\n"
117 "\t%s tagname devname\n"
118 "Find which device holds a given token or\n"
119 "Find what the value of a tag is in a device\n",
123 if (blkid_get_cache(&cache
, bb_dev_null
) < 0) {
124 fprintf(stderr
, "Can't get blkid cache\n");
129 value
= blkid_get_tag_value(cache
, argv
[1], argv
[2]);
130 printf("%s has tag %s=%s\n", argv
[2], argv
[1],
131 value
? value
: "<missing>");
133 value
= blkid_get_devname(cache
, argv
[1], NULL
);
134 printf("%s has tag %s\n", value
? value
: "<none>", argv
[1]);
136 blkid_put_cache(cache
);
137 return value
? 0 : 1;