Changes for kernel and Busybox
[tomato.git] / release / src / router / busybox / e2fsprogs / blkid / resolve.c
blob295ca61bffff3aac6e4f20d73d08757862471137
1 /* vi: set sw=4 ts=4: */
2 /*
3 * resolve.c - resolve names and tags into specific devices
5 * Copyright (C) 2001, 2003 Theodore Ts'o.
6 * Copyright (C) 2001 Andreas Dilger
8 * %Begin-Header%
9 * This file may be redistributed under the terms of the
10 * GNU Lesser General Public License.
11 * %End-Header%
14 #include <stdio.h>
15 #ifdef HAVE_UNISTD_H
16 #include <unistd.h>
17 #endif
18 #include <stdlib.h>
19 #include <fcntl.h>
20 #include <string.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include "blkidP.h"
24 #include "probe.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,
30 const char *devname)
32 blkid_tag found;
33 blkid_dev dev;
34 blkid_cache c = cache;
35 char *ret = NULL;
37 DBG(DEBUG_RESOLVE, printf("looking for %s on %s\n", tagname, devname));
39 if (!devname)
40 return NULL;
42 if (!cache) {
43 if (blkid_get_cache(&c, NULL) < 0)
44 return NULL;
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);
51 if (!cache)
52 blkid_put_cache(c);
54 return ret;
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,
64 const char *value)
66 blkid_dev dev;
67 blkid_cache c = cache;
68 char *t = NULL, *v = NULL;
69 char *ret = NULL;
71 if (!token)
72 return NULL;
74 if (!cache) {
75 if (blkid_get_cache(&c, NULL) < 0)
76 return NULL;
79 DBG(DEBUG_RESOLVE,
80 printf("looking for %s%s%s %s\n", token, value ? "=" : "",
81 value ? value : "", cache ? "in cache" : "from disk"));
83 if (!value) {
84 if (!strchr(token, '='))
85 return blkid_strdup(token);
86 blkid_parse_tag_string(token, &t, &v);
87 if (!t || !v)
88 goto errout;
89 token = t;
90 value = v;
93 dev = blkid_find_dev_with_tag(c, token, value);
94 if (!dev)
95 goto errout;
97 ret = blkid_strdup(blkid_dev_devname(dev));
99 errout:
100 free(t);
101 free(v);
102 if (!cache) {
103 blkid_put_cache(c);
105 return ret;
108 #ifdef TEST_PROGRAM
109 int main(int argc, char **argv)
111 char *value;
112 blkid_cache cache;
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",
120 argv[0], argv[0]);
121 exit(1);
123 if (blkid_get_cache(&cache, bb_dev_null) < 0) {
124 fprintf(stderr, "Can't get blkid cache\n");
125 exit(1);
128 if (argv[2]) {
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>");
132 } else {
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;
139 #endif