]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - libblkid/src/resolve.c
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.
20 #include <sys/types.h>
25 * Find a tagname (e.g. LABEL or UUID) on a specific device.
27 char *blkid_get_tag_value(blkid_cache cache
, const char *tagname
,
32 blkid_cache c
= cache
;
35 DBG(TAG
, ul_debug("looking for %s on %s", tagname
, devname
));
39 if (!cache
&& blkid_get_cache(&c
, NULL
) < 0)
42 if ((dev
= blkid_get_dev(c
, devname
, BLKID_DEV_NORMAL
)) &&
43 (found
= blkid_find_tag_dev(dev
, tagname
)))
44 ret
= found
->bit_val
? strdup(found
->bit_val
) : NULL
;
53 * Locate a device name from a token (NAME=value string), or (name, value)
54 * pair. In the case of a token, value is ignored. If the "token" is not
55 * of the form "NAME=value" and there is no value given, then it is assumed
56 * to be the actual devname and a copy is returned.
58 char *blkid_get_devname(blkid_cache cache
, const char *token
,
62 blkid_cache c
= cache
;
63 char *t
= NULL
, *v
= NULL
;
68 if (!cache
&& blkid_get_cache(&c
, NULL
) < 0)
71 DBG(TAG
, ul_debug("looking for %s%s%s %s", token
, value
? "=" : "",
72 value
? value
: "", cache
? "in cache" : "from disk"));
75 if (!strchr(token
, '=')) {
79 if (blkid_parse_tag_string(token
, &t
, &v
) != 0 || !t
|| !v
)
85 dev
= blkid_find_dev_with_tag(c
, token
, value
);
89 ret
= dev
->bid_name
? strdup(dev
->bid_name
) : NULL
;
99 int main(int argc
, char **argv
)
104 blkid_init_debug(BLKID_DEBUG_ALL
);
105 if (argc
!= 2 && argc
!= 3) {
106 fprintf(stderr
, "Usage:\t%s tagname=value\n"
107 "\t%s tagname devname\n"
108 "Find which device holds a given token or\n"
109 "Find what the value of a tag is in a device\n",
113 if (blkid_get_cache(&cache
, "/dev/null") < 0) {
114 fprintf(stderr
, "Couldn't get blkid cache\n");
119 value
= blkid_get_tag_value(cache
, argv
[1], argv
[2]);
120 printf("%s has tag %s=%s\n", argv
[2], argv
[1],
121 value
? value
: "<missing>");
123 value
= blkid_get_devname(cache
, argv
[1], NULL
);
124 printf("%s has tag %s\n", value
? value
: "<none>", argv
[1]);
126 blkid_put_cache(cache
);
127 return value
? 0 : 1;