Whamcloud - gitweb
fca1e1683a0b606cf88ef49947518ab8151874f5
[tools/e2fsprogs.git] / lib / blkid / resolve.c
1 /*
2  * resolve.c - resolve names and tags into specific devices
3  *
4  * Copyright (C) 2001, 2003 Theodore Ts'o.
5  * Copyright (C) 2001 Andreas Dilger
6  *
7  * %Begin-Header%
8  * This file may be redistributed under the terms of the
9  * GNU Lesser General Public License.
10  * %End-Header%
11  */
12
13 #include <stdio.h>
14 #if HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17 #include <stdlib.h>
18 #include <fcntl.h>
19 #include <string.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include "blkidP.h"
23 #include "probe.h"
24
25 #ifdef DEBUG_RESOLVE
26 #define DBG(x)  x
27 #else
28 #define DBG(x)
29 #endif
30
31
32 /*
33  * Find a tagname (e.g. LABEL or UUID) on a specific device.
34  */
35 char *blkid_get_tagname_devname(blkid_cache cache, const char *tagname,
36                                 const char *devname)
37 {
38         blkid_tag found;
39         blkid_dev dev;
40         char *ret = NULL;
41
42         DBG(printf("looking for %s on %s\n", tagname, devname));
43
44         if (!devname)
45                 return NULL;
46
47         if ((dev = blkid_get_devname(cache, devname, BLKID_DEV_NORMAL)) &&
48             (found = blkid_find_tag_dev(dev, tagname)))
49                 ret = blkid_strdup(found->bit_val);
50
51         if (!cache)
52                 blkid_free_dev(dev);
53
54         return ret;
55 }
56
57 /*
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.
62  */
63 char *blkid_get_token(blkid_cache cache, const char *token,
64                       const char *value)
65 {
66         blkid_dev dev;
67         blkid_cache c = cache;
68         char *t = 0, *v = 0;
69         char *ret = NULL;
70
71         if (!token)
72                 return NULL;
73         
74         DBG(printf("looking for %s%c%s %s\n", token, value ? '=' : ' ',
75                    value ? value : "", cache ? "in cache" : "from disk"));
76
77         if (!cache) {
78                 if (blkid_get_cache(&c, NULL) < 0)
79                         c = blkid_new_cache();
80                 if (!c)
81                         return NULL;
82         }
83
84         if (!value) {
85                 blkid_parse_tag_string(token, &t, &v);
86                 if (!t || !v)
87                         goto errout;
88                 token = t;
89                 value = v;
90         }
91
92         dev = blkid_find_dev_with_tag(c, token, value);
93         if (!dev)
94                 goto errout;
95
96         ret = blkid_strdup(blkid_devname_name(dev));
97
98 errout:
99         if (t)
100                 free(t);
101         if (v)
102                 free(v);
103         if (!cache) {
104                 blkid_put_cache(c);
105         }
106         return (ret);
107 }
108
109 #ifdef TEST_PROGRAM
110 int main(int argc, char **argv)
111 {
112         char *value;
113         blkid_cache cache;
114
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);
122         }
123         if (blkid_get_cache(&cache, 0) < 0) {
124                 fprintf(stderr, "Couldn't get blkid cache\n");
125                 exit(1);
126         }
127         
128         if (argv[2]) {
129                 value = blkid_get_tagname_devname(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_token(cache, argv[1], NULL);
134                 printf("%s has tag %s\n", value ? value : "<none>", argv[1]);
135         }
136         blkid_put_cache(cache);
137         return value ? 0 : 1;
138 }
139 #endif