bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 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 | { |
| 30 | blkid_tag found; |
| 31 | blkid_dev dev; |
| 32 | blkid_cache c = cache; |
| 33 | char *ret = NULL; |
| 34 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 35 | DBG(TAG, ul_debug("looking for %s on %s", tagname, devname)); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 36 | |
| 37 | if (!devname) |
| 38 | return NULL; |
| 39 | if (!cache && blkid_get_cache(&c, NULL) < 0) |
| 40 | return NULL; |
| 41 | |
| 42 | if ((dev = blkid_get_dev(c, devname, BLKID_DEV_NORMAL)) && |
| 43 | (found = blkid_find_tag_dev(dev, tagname))) |
| 44 | ret = found->bit_val ? strdup(found->bit_val) : NULL; |
| 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 | */ |
| 58 | char *blkid_get_devname(blkid_cache cache, const char *token, |
| 59 | const char *value) |
| 60 | { |
| 61 | blkid_dev dev; |
| 62 | blkid_cache c = cache; |
| 63 | char *t = 0, *v = 0; |
| 64 | char *ret = NULL; |
| 65 | |
| 66 | if (!token) |
| 67 | return NULL; |
| 68 | if (!cache && blkid_get_cache(&c, NULL) < 0) |
| 69 | return NULL; |
| 70 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 71 | DBG(TAG, ul_debug("looking for %s%s%s %s", token, value ? "=" : "", |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 72 | value ? value : "", cache ? "in cache" : "from disk")); |
| 73 | |
| 74 | if (!value) { |
| 75 | if (!strchr(token, '=')) { |
| 76 | ret = strdup(token); |
| 77 | goto out; |
| 78 | } |
| 79 | blkid_parse_tag_string(token, &t, &v); |
| 80 | if (!t || !v) |
| 81 | goto out; |
| 82 | token = t; |
| 83 | value = v; |
| 84 | } |
| 85 | |
| 86 | dev = blkid_find_dev_with_tag(c, token, value); |
| 87 | if (!dev) |
| 88 | goto out; |
| 89 | |
| 90 | ret = dev->bid_name ? strdup(dev->bid_name) : NULL; |
| 91 | out: |
| 92 | free(t); |
| 93 | free(v); |
| 94 | if (!cache) |
| 95 | blkid_put_cache(c); |
| 96 | return ret; |
| 97 | } |
| 98 | |
| 99 | #ifdef TEST_PROGRAM |
| 100 | int main(int argc, char **argv) |
| 101 | { |
| 102 | char *value; |
| 103 | blkid_cache cache; |
| 104 | |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 105 | blkid_init_debug(BLKID_DEBUG_ALL); |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 106 | if (argc != 2 && argc != 3) { |
| 107 | fprintf(stderr, "Usage:\t%s tagname=value\n" |
| 108 | "\t%s tagname devname\n" |
| 109 | "Find which device holds a given token or\n" |
| 110 | "Find what the value of a tag is in a device\n", |
| 111 | argv[0], argv[0]); |
| 112 | exit(1); |
| 113 | } |
| 114 | if (blkid_get_cache(&cache, "/dev/null") < 0) { |
| 115 | fprintf(stderr, "Couldn't get blkid cache\n"); |
| 116 | exit(1); |
| 117 | } |
| 118 | |
| 119 | if (argv[2]) { |
| 120 | value = blkid_get_tag_value(cache, argv[1], argv[2]); |
| 121 | printf("%s has tag %s=%s\n", argv[2], argv[1], |
| 122 | value ? value : "<missing>"); |
| 123 | } else { |
| 124 | value = blkid_get_devname(cache, argv[1], NULL); |
| 125 | printf("%s has tag %s\n", value ? value : "<none>", argv[1]); |
| 126 | } |
| 127 | blkid_put_cache(cache); |
| 128 | return value ? 0 : 1; |
| 129 | } |
| 130 | #endif |