blob: 6c2e268d099c54034ccd2505096103a940a86b0c [file] [log] [blame]
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05001/*
2 * resolve.c - resolve names and tags into specific devices
3 *
Theodore Ts'o50b380b2003-02-12 23:51:21 -05004 * Copyright (C) 2001, 2003 Theodore Ts'o.
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -05005 * 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
Theodore Ts'o76b07bb2003-01-27 01:09:24 -050017#include <stdlib.h>
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050018#include <fcntl.h>
19#include <string.h>
20#include <sys/types.h>
21#include <sys/stat.h>
Theodore Ts'o7a603aa2003-01-26 01:54:39 -050022#include "blkidP.h"
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050023
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050024/*
25 * Find a tagname (e.g. LABEL or UUID) on a specific device.
26 */
Theodore Ts'o1549a442003-02-27 19:33:36 -050027char *blkid_get_tag_value(blkid_cache cache, const char *tagname,
28 const char *devname)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050029{
Theodore Ts'o76b07bb2003-01-27 01:09:24 -050030 blkid_tag found;
Theodore Ts'o7a603aa2003-01-26 01:54:39 -050031 blkid_dev dev;
Theodore Ts'oed1b33e2003-03-01 19:29:01 -050032 blkid_cache c = cache;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050033 char *ret = NULL;
34
Theodore Ts'of0a22d02003-02-22 13:19:53 -050035 DBG(DEBUG_RESOLVE, printf("looking for %s on %s\n", tagname, devname));
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050036
37 if (!devname)
38 return NULL;
39
Theodore Ts'oed1b33e2003-03-01 19:29:01 -050040 if (!cache) {
41 if (blkid_get_cache(&c, NULL) < 0)
42 return NULL;
43 }
44
45 if ((dev = blkid_get_dev(c, devname, BLKID_DEV_NORMAL)) &&
Theodore Ts'o50b380b2003-02-12 23:51:21 -050046 (found = blkid_find_tag_dev(dev, tagname)))
47 ret = blkid_strdup(found->bit_val);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050048
49 if (!cache)
Theodore Ts'oed1b33e2003-03-01 19:29:01 -050050 blkid_put_cache(c);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050051
52 return ret;
53}
54
55/*
56 * Locate a device name from a token (NAME=value string), or (name, value)
57 * pair. In the case of a token, value is ignored. If the "token" is not
58 * of the form "NAME=value" and there is no value given, then it is assumed
59 * to be the actual devname and a copy is returned.
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050060 */
Theodore Ts'o98999c32003-02-16 00:47:07 -050061char *blkid_get_devname(blkid_cache cache, const char *token,
62 const char *value)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050063{
Theodore Ts'o76b07bb2003-01-27 01:09:24 -050064 blkid_dev dev;
Theodore Ts'o7a603aa2003-01-26 01:54:39 -050065 blkid_cache c = cache;
Theodore Ts'o76b07bb2003-01-27 01:09:24 -050066 char *t = 0, *v = 0;
67 char *ret = NULL;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050068
Theodore Ts'o76b07bb2003-01-27 01:09:24 -050069 if (!token)
70 return NULL;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040071
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050072 if (!cache) {
Theodore Ts'o50b380b2003-02-12 23:51:21 -050073 if (blkid_get_cache(&c, NULL) < 0)
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050074 return NULL;
75 }
76
Theodore Ts'oed1b33e2003-03-01 19:29:01 -050077 DBG(DEBUG_RESOLVE,
78 printf("looking for %s%s%s %s\n", token, value ? "=" : "",
79 value ? value : "", cache ? "in cache" : "from disk"));
80
Theodore Ts'o76b07bb2003-01-27 01:09:24 -050081 if (!value) {
Theodore Ts'o5f7fe7f2007-03-21 17:15:36 -040082 if (!strchr(token, '=')) {
83 ret = blkid_strdup(token);
84 goto out;
85 }
Theodore Ts'o76b07bb2003-01-27 01:09:24 -050086 blkid_parse_tag_string(token, &t, &v);
87 if (!t || !v)
Theodore Ts'o5f7fe7f2007-03-21 17:15:36 -040088 goto out;
Theodore Ts'o76b07bb2003-01-27 01:09:24 -050089 token = t;
90 value = v;
91 }
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -050092
Theodore Ts'o76b07bb2003-01-27 01:09:24 -050093 dev = blkid_find_dev_with_tag(c, token, value);
94 if (!dev)
Theodore Ts'o5f7fe7f2007-03-21 17:15:36 -040095 goto out;
Theodore Ts'o76b07bb2003-01-27 01:09:24 -050096
Theodore Ts'o98999c32003-02-16 00:47:07 -050097 ret = blkid_strdup(blkid_dev_devname(dev));
Theodore Ts'o76b07bb2003-01-27 01:09:24 -050098
Theodore Ts'o5f7fe7f2007-03-21 17:15:36 -040099out:
Jim Meyering45e338f2009-02-23 18:07:50 +0100100 free(t);
101 free(v);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500102 if (!cache) {
Theodore Ts'o50b380b2003-02-12 23:51:21 -0500103 blkid_put_cache(c);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500104 }
Theodore Ts'o76b07bb2003-01-27 01:09:24 -0500105 return (ret);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500106}
107
108#ifdef TEST_PROGRAM
109int main(int argc, char **argv)
110{
111 char *value;
Theodore Ts'oce72b862003-02-14 01:31:45 -0500112 blkid_cache cache;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500113
Theodore Ts'of0a22d02003-02-22 13:19:53 -0500114 blkid_debug_mask = DEBUG_ALL;
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500115 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 }
Theodore Ts'o79dd2342003-02-22 17:15:20 -0500123 if (blkid_get_cache(&cache, "/dev/null") < 0) {
Theodore Ts'oce72b862003-02-14 01:31:45 -0500124 fprintf(stderr, "Couldn't get blkid cache\n");
125 exit(1);
126 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400127
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500128 if (argv[2]) {
Theodore Ts'o28595222003-03-01 20:01:27 -0500129 value = blkid_get_tag_value(cache, argv[1], argv[2]);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500130 printf("%s has tag %s=%s\n", argv[2], argv[1],
131 value ? value : "<missing>");
132 } else {
Theodore Ts'o98999c32003-02-16 00:47:07 -0500133 value = blkid_get_devname(cache, argv[1], NULL);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500134 printf("%s has tag %s\n", value ? value : "<none>", argv[1]);
135 }
Theodore Ts'oce72b862003-02-14 01:31:45 -0500136 blkid_put_cache(cache);
Theodore Ts'oe12f2ae2003-01-23 16:45:16 -0500137 return value ? 0 : 1;
138}
139#endif