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 | |
| 35 | DBG(DEBUG_RESOLVE, printf("looking for %s on %s\n", tagname, devname)); |
| 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 | |
| 71 | DBG(DEBUG_RESOLVE, |
| 72 | printf("looking for %s%s%s %s\n", token, value ? "=" : "", |
| 73 | value ? value : "", cache ? "in cache" : "from disk")); |
| 74 | |
| 75 | if (!value) { |
| 76 | if (!strchr(token, '=')) { |
| 77 | ret = strdup(token); |
| 78 | goto out; |
| 79 | } |
| 80 | blkid_parse_tag_string(token, &t, &v); |
| 81 | if (!t || !v) |
| 82 | goto out; |
| 83 | token = t; |
| 84 | value = v; |
| 85 | } |
| 86 | |
| 87 | dev = blkid_find_dev_with_tag(c, token, value); |
| 88 | if (!dev) |
| 89 | goto out; |
| 90 | |
| 91 | ret = dev->bid_name ? strdup(dev->bid_name) : NULL; |
| 92 | out: |
| 93 | free(t); |
| 94 | free(v); |
| 95 | if (!cache) |
| 96 | blkid_put_cache(c); |
| 97 | return ret; |
| 98 | } |
| 99 | |
| 100 | #ifdef TEST_PROGRAM |
| 101 | int main(int argc, char **argv) |
| 102 | { |
| 103 | char *value; |
| 104 | blkid_cache cache; |
| 105 | |
| 106 | blkid_init_debug(DEBUG_ALL); |
| 107 | if (argc != 2 && argc != 3) { |
| 108 | fprintf(stderr, "Usage:\t%s tagname=value\n" |
| 109 | "\t%s tagname devname\n" |
| 110 | "Find which device holds a given token or\n" |
| 111 | "Find what the value of a tag is in a device\n", |
| 112 | argv[0], argv[0]); |
| 113 | exit(1); |
| 114 | } |
| 115 | if (blkid_get_cache(&cache, "/dev/null") < 0) { |
| 116 | fprintf(stderr, "Couldn't get blkid cache\n"); |
| 117 | exit(1); |
| 118 | } |
| 119 | |
| 120 | if (argv[2]) { |
| 121 | value = blkid_get_tag_value(cache, argv[1], argv[2]); |
| 122 | printf("%s has tag %s=%s\n", argv[2], argv[1], |
| 123 | value ? value : "<missing>"); |
| 124 | } else { |
| 125 | value = blkid_get_devname(cache, argv[1], NULL); |
| 126 | printf("%s has tag %s\n", value ? value : "<none>", argv[1]); |
| 127 | } |
| 128 | blkid_put_cache(cache); |
| 129 | return value ? 0 : 1; |
| 130 | } |
| 131 | #endif |