]> git.ipfire.org Git - thirdparty/util-linux.git/blame - libblkid/src/resolve.c
Merge branch 'topic/irq'
[thirdparty/util-linux.git] / libblkid / src / resolve.c
CommitLineData
a0948ffe
KZ
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>
fbc333fe 14#ifdef HAVE_UNISTD_H
a0948ffe
KZ
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
24/*
25 * Find a tagname (e.g. LABEL or UUID) on a specific device.
26 */
27char *blkid_get_tag_value(blkid_cache cache, const char *tagname,
28 const char *devname)
29{
30 blkid_tag found;
31 blkid_dev dev;
32 blkid_cache c = cache;
33 char *ret = NULL;
34
b7da851e 35 DBG(TAG, ul_debug("looking for %s on %s", tagname, devname));
a0948ffe
KZ
36
37 if (!devname)
38 return NULL;
e3436956
KZ
39 if (!cache && blkid_get_cache(&c, NULL) < 0)
40 return NULL;
a0948ffe
KZ
41
42 if ((dev = blkid_get_dev(c, devname, BLKID_DEV_NORMAL)) &&
43 (found = blkid_find_tag_dev(dev, tagname)))
e0a9b8cf 44 ret = found->bit_val ? strdup(found->bit_val) : NULL;
a0948ffe
KZ
45
46 if (!cache)
47 blkid_put_cache(c);
48
49 return ret;
50}
51
52/*
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.
57 */
58char *blkid_get_devname(blkid_cache cache, const char *token,
59 const char *value)
60{
61 blkid_dev dev;
62 blkid_cache c = cache;
87918040 63 char *t = NULL, *v = NULL;
a0948ffe
KZ
64 char *ret = NULL;
65
66 if (!token)
67 return NULL;
e3436956
KZ
68 if (!cache && blkid_get_cache(&c, NULL) < 0)
69 return NULL;
a0948ffe 70
b7da851e 71 DBG(TAG, ul_debug("looking for %s%s%s %s", token, value ? "=" : "",
a0948ffe
KZ
72 value ? value : "", cache ? "in cache" : "from disk"));
73
74 if (!value) {
75 if (!strchr(token, '=')) {
e0a9b8cf 76 ret = strdup(token);
a0948ffe
KZ
77 goto out;
78 }
e12e917b 79 if (blkid_parse_tag_string(token, &t, &v) != 0 || !t || !v)
a0948ffe
KZ
80 goto out;
81 token = t;
82 value = v;
83 }
84
85 dev = blkid_find_dev_with_tag(c, token, value);
86 if (!dev)
87 goto out;
88
e0a9b8cf 89 ret = dev->bid_name ? strdup(dev->bid_name) : NULL;
a0948ffe 90out:
0e4ed1aa
JM
91 free(t);
92 free(v);
e0a9b8cf 93 if (!cache)
a0948ffe 94 blkid_put_cache(c);
e0a9b8cf 95 return ret;
a0948ffe
KZ
96}
97
98#ifdef TEST_PROGRAM
99int main(int argc, char **argv)
100{
101 char *value;
102 blkid_cache cache;
103
0540ea54 104 blkid_init_debug(BLKID_DEBUG_ALL);
a0948ffe
KZ
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",
110 argv[0], argv[0]);
111 exit(1);
112 }
113 if (blkid_get_cache(&cache, "/dev/null") < 0) {
114 fprintf(stderr, "Couldn't get blkid cache\n");
115 exit(1);
116 }
117
118 if (argv[2]) {
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>");
122 } else {
123 value = blkid_get_devname(cache, argv[1], NULL);
124 printf("%s has tag %s\n", value ? value : "<none>", argv[1]);
125 }
126 blkid_put_cache(cache);
127 return value ? 0 : 1;
128}
129#endif