blob: 40d999a1bb926efa663c3e44d1ec88511da33842 [file] [log] [blame]
Mike Frysinger38a33f92005-05-09 22:13:22 +00001/*
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/*
26 * Find a tagname (e.g. LABEL or UUID) on a specific device.
27 */
28char *blkid_get_tag_value(blkid_cache cache, const char *tagname,
29 const char *devname)
30{
31 blkid_tag found;
32 blkid_dev dev;
33 blkid_cache c = cache;
34 char *ret = NULL;
35
36 DBG(DEBUG_RESOLVE, printf("looking for %s on %s\n", tagname, devname));
37
38 if (!devname)
39 return NULL;
40
41 if (!cache) {
42 if (blkid_get_cache(&c, NULL) < 0)
43 return NULL;
44 }
45
46 if ((dev = blkid_get_dev(c, devname, BLKID_DEV_NORMAL)) &&
47 (found = blkid_find_tag_dev(dev, tagname)))
48 ret = blkid_strdup(found->bit_val);
49
50 if (!cache)
51 blkid_put_cache(c);
52
53 return ret;
54}
55
56/*
57 * Locate a device name from a token (NAME=value string), or (name, value)
58 * pair. In the case of a token, value is ignored. If the "token" is not
59 * of the form "NAME=value" and there is no value given, then it is assumed
60 * to be the actual devname and a copy is returned.
61 */
62char *blkid_get_devname(blkid_cache cache, const char *token,
63 const char *value)
64{
65 blkid_dev dev;
66 blkid_cache c = cache;
67 char *t = 0, *v = 0;
68 char *ret = NULL;
69
70 if (!token)
71 return NULL;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000072
Mike Frysinger38a33f92005-05-09 22:13:22 +000073 if (!cache) {
74 if (blkid_get_cache(&c, NULL) < 0)
75 return NULL;
76 }
77
78 DBG(DEBUG_RESOLVE,
79 printf("looking for %s%s%s %s\n", token, value ? "=" : "",
80 value ? value : "", cache ? "in cache" : "from disk"));
81
82 if (!value) {
83 if (!strchr(token, '='))
84 return blkid_strdup(token);
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_dev_devname(dev));
97
98errout:
Rob Landleye7c43b62006-03-01 16:39:45 +000099 free(t);
100 free(v);
Mike Frysinger38a33f92005-05-09 22:13:22 +0000101 if (!cache) {
102 blkid_put_cache(c);
103 }
104 return (ret);
105}
106
107#ifdef TEST_PROGRAM
108int main(int argc, char **argv)
109{
110 char *value;
111 blkid_cache cache;
112
113 blkid_debug_mask = DEBUG_ALL;
114 if (argc != 2 && argc != 3) {
115 fprintf(stderr, "Usage:\t%s tagname=value\n"
116 "\t%s tagname devname\n"
117 "Find which device holds a given token or\n"
118 "Find what the value of a tag is in a device\n",
119 argv[0], argv[0]);
120 exit(1);
121 }
"Vladimir N. Oleynik"6c35c7c2005-10-12 15:34:25 +0000122 if (blkid_get_cache(&cache, bb_dev_null) < 0) {
Mike Frysinger38a33f92005-05-09 22:13:22 +0000123 fprintf(stderr, "Couldn't get blkid cache\n");
124 exit(1);
125 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000126
Mike Frysinger38a33f92005-05-09 22:13:22 +0000127 if (argv[2]) {
128 value = blkid_get_tag_value(cache, argv[1], argv[2]);
129 printf("%s has tag %s=%s\n", argv[2], argv[1],
130 value ? value : "<missing>");
131 } else {
132 value = blkid_get_devname(cache, argv[1], NULL);
133 printf("%s has tag %s\n", value ? value : "<none>", argv[1]);
134 }
135 blkid_put_cache(cache);
136 return value ? 0 : 1;
137}
138#endif