/src/util-linux/libblkid/src/resolve.c
Line | Count | Source |
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 | | #ifdef 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 | | |
24 | | /* |
25 | | * Find a tagname (e.g. LABEL or UUID) on a specific device. |
26 | | */ |
27 | | char *blkid_get_tag_value(blkid_cache cache, const char *tagname, |
28 | | const char *devname) |
29 | 0 | { |
30 | 0 | blkid_tag found; |
31 | 0 | blkid_dev dev; |
32 | 0 | blkid_cache c = cache; |
33 | 0 | char *ret = NULL; |
34 | |
|
35 | 0 | DBG(TAG, ul_debug("looking for tag %s on %s device", tagname, devname)); |
36 | |
|
37 | 0 | if (!devname) |
38 | 0 | return NULL; |
39 | 0 | if (!cache && blkid_get_cache(&c, NULL) < 0) |
40 | 0 | return NULL; |
41 | | |
42 | 0 | if ((dev = blkid_get_dev(c, devname, BLKID_DEV_NORMAL)) && |
43 | 0 | (found = blkid_find_tag_dev(dev, tagname))) |
44 | 0 | ret = found->bit_val ? strdup(found->bit_val) : NULL; |
45 | |
|
46 | 0 | if (!cache) |
47 | 0 | blkid_put_cache(c); |
48 | |
|
49 | 0 | return ret; |
50 | 0 | } |
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 | | */ |
58 | | char *blkid_get_devname(blkid_cache cache, const char *token, |
59 | | const char *value) |
60 | 46 | { |
61 | 46 | blkid_dev dev; |
62 | 46 | blkid_cache c = cache; |
63 | 46 | char *t = NULL, *v = NULL; |
64 | 46 | char *ret = NULL; |
65 | | |
66 | 46 | if (!token) |
67 | 0 | return NULL; |
68 | 46 | if (!cache && blkid_get_cache(&c, NULL) < 0) |
69 | 0 | return NULL; |
70 | | |
71 | 46 | DBG(TAG, ul_debug("looking for %s%s%s %s", token, value ? "=" : "", |
72 | 46 | value ? value : "", cache ? "in cache" : "from disk")); |
73 | | |
74 | 46 | if (!value) { |
75 | 0 | if (!strchr(token, '=')) { |
76 | 0 | ret = strdup(token); |
77 | 0 | goto out; |
78 | 0 | } |
79 | 0 | if (blkid_parse_tag_string(token, &t, &v) != 0 || !t || !v) |
80 | 0 | goto out; |
81 | 0 | token = t; |
82 | 0 | value = v; |
83 | 0 | } |
84 | | |
85 | 46 | dev = blkid_find_dev_with_tag(c, token, value); |
86 | 46 | if (!dev) |
87 | 0 | goto out; |
88 | | |
89 | 46 | ret = dev->bid_name ? strdup(dev->bid_name) : NULL; |
90 | 46 | out: |
91 | 46 | free(t); |
92 | 46 | free(v); |
93 | 46 | if (!cache) |
94 | 0 | blkid_put_cache(c); |
95 | 46 | return ret; |
96 | 46 | } |
97 | | |
98 | | #ifdef TEST_PROGRAM |
99 | | int main(int argc, char **argv) |
100 | | { |
101 | | char *value; |
102 | | blkid_cache cache; |
103 | | |
104 | | blkid_init_debug(BLKID_DEBUG_ALL); |
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 |