Whamcloud - gitweb
Integrate new blkid library.
[tools/e2fsprogs.git] / lib / blkid / resolve.c
1 /*
2  * resolve.c - resolve names and tags into specific devices
3  *
4  * Copyright (C) 2001 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 <fcntl.h>
18 #include <string.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include "blkid/blkid.h"
22 #include "probe.h"
23
24 #ifdef DEBUG_RESOLVE
25 #define DEB_RESOLVE(fmt, arg...) printf("resolve: " fmt, ## arg)
26 #else
27 #define DEB_RESOLVE(fmt, arg...) do {} while (0)
28 #endif
29
30
31 /*
32  * Find a tagname (e.g. LABEL or UUID) on a specific device.
33  */
34 char *blkid_get_tagname_devname(blkid_cache *cache, const char *tagname,
35                                 const char *devname)
36 {
37         blkid_tag *tag, *found;
38         blkid_dev *dev;
39         char *ret = NULL;
40
41         DEB_RESOLVE("looking for %s on %s\n", tagname, devname);
42
43         if (!devname)
44                 return NULL;
45
46         if (blkid_create_tag(NULL, &tag, tagname, NULL, 0) < 0)
47                 return NULL;
48
49         if (!cache)
50                 DEB_RESOLVE("no cache given, direct device probe\n");
51
52         if ((dev = blkid_get_devname(cache, devname)) &&
53             (found = blkid_find_tag_dev(dev, tag)))
54                 ret = string_copy(found->bit_val);
55
56         if (!cache)
57                 blkid_free_dev(dev);
58
59         return ret;
60 }
61
62 /*
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.
67  *
68  * The string returned must be freed with string_free().
69  */
70 char *blkid_get_token(blkid_cache *cache, const char *token,
71                       const char *value)
72 {
73         blkid_tag *tag = NULL, *found = NULL;
74         blkid_cache *c = cache;
75         char *name = NULL;
76
77         DEB_RESOLVE("looking for %s%c%s %s\n", token, value ? '=' : ' ',
78                     value ? value : "", cache ? "in cache" : "from disk");
79
80         if (!(tag = blkid_token_to_tag(token))) {
81                 if (!value)
82                         return string_copy(token);
83                 if (blkid_create_tag(NULL,&tag,token,value,strlen(value)) < 0)
84                         return NULL;
85         }
86
87         if (!cache) {
88                 if (blkid_read_cache(&c, NULL) < 0)
89                         c = blkid_new_cache();
90                 if (!c)
91                         return NULL;
92         }
93
94         if ((found = blkid_get_tag_cache(c, tag)) && found->bit_dev)
95                 name = string_copy(found->bit_dev->bid_name);
96
97         if (!cache) {
98                 blkid_save_cache(c, NULL);
99                 blkid_free_cache(c);
100         }
101
102
103         blkid_free_tag(tag);
104         return name;
105 }
106
107 #ifdef TEST_PROGRAM
108 int main(int argc, char **argv)
109 {
110         char *value;
111
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",
117                         argv[0], argv[0]);
118                 exit(1);
119         }
120         if (argv[2]) {
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>");
124         } else {
125                 value = blkid_get_token(NULL, argv[1], NULL);
126                 printf("%s has tag %s\n", value ? value : "<none>", argv[1]);
127         }
128         return value ? 0 : 1;
129 }
130 #endif