paul@181 | 1 | /* |
paul@181 | 2 | * resolve.c - resolve names and tags into specific devices |
paul@181 | 3 | * |
paul@181 | 4 | * Copyright (C) 2001, 2003 Theodore Ts'o. |
paul@181 | 5 | * Copyright (C) 2001 Andreas Dilger |
paul@181 | 6 | * |
paul@181 | 7 | * %Begin-Header% |
paul@181 | 8 | * This file may be redistributed under the terms of the |
paul@181 | 9 | * GNU Lesser General Public License. |
paul@181 | 10 | * %End-Header% |
paul@181 | 11 | */ |
paul@181 | 12 | |
paul@181 | 13 | #include "config.h" |
paul@181 | 14 | #include <stdio.h> |
paul@181 | 15 | #if HAVE_UNISTD_H |
paul@181 | 16 | #include <unistd.h> |
paul@181 | 17 | #endif |
paul@181 | 18 | #include <stdlib.h> |
paul@181 | 19 | #include <fcntl.h> |
paul@181 | 20 | #include <string.h> |
paul@181 | 21 | #include <sys/types.h> |
paul@181 | 22 | #include <sys/stat.h> |
paul@181 | 23 | #include "blkidP.h" |
paul@181 | 24 | |
paul@181 | 25 | /* |
paul@181 | 26 | * Find a tagname (e.g. LABEL or UUID) on a specific device. |
paul@181 | 27 | */ |
paul@181 | 28 | char *blkid_get_tag_value(blkid_cache cache, const char *tagname, |
paul@181 | 29 | const char *devname) |
paul@181 | 30 | { |
paul@181 | 31 | blkid_tag found; |
paul@181 | 32 | blkid_dev dev; |
paul@181 | 33 | blkid_cache c = cache; |
paul@181 | 34 | char *ret = NULL; |
paul@181 | 35 | |
paul@181 | 36 | DBG(DEBUG_RESOLVE, printf("looking for %s on %s\n", tagname, devname)); |
paul@181 | 37 | |
paul@181 | 38 | if (!devname) |
paul@181 | 39 | return NULL; |
paul@181 | 40 | |
paul@181 | 41 | if (!cache) { |
paul@181 | 42 | if (blkid_get_cache(&c, NULL) < 0) |
paul@181 | 43 | return NULL; |
paul@181 | 44 | } |
paul@181 | 45 | |
paul@181 | 46 | if ((dev = blkid_get_dev(c, devname, BLKID_DEV_NORMAL)) && |
paul@181 | 47 | (found = blkid_find_tag_dev(dev, tagname))) |
paul@181 | 48 | ret = blkid_strdup(found->bit_val); |
paul@181 | 49 | |
paul@181 | 50 | if (!cache) |
paul@181 | 51 | blkid_put_cache(c); |
paul@181 | 52 | |
paul@181 | 53 | return ret; |
paul@181 | 54 | } |
paul@181 | 55 | |
paul@181 | 56 | /* |
paul@181 | 57 | * Locate a device name from a token (NAME=value string), or (name, value) |
paul@181 | 58 | * pair. In the case of a token, value is ignored. If the "token" is not |
paul@181 | 59 | * of the form "NAME=value" and there is no value given, then it is assumed |
paul@181 | 60 | * to be the actual devname and a copy is returned. |
paul@181 | 61 | */ |
paul@181 | 62 | char *blkid_get_devname(blkid_cache cache, const char *token, |
paul@181 | 63 | const char *value) |
paul@181 | 64 | { |
paul@181 | 65 | blkid_dev dev; |
paul@181 | 66 | blkid_cache c = cache; |
paul@181 | 67 | char *t = 0, *v = 0; |
paul@181 | 68 | char *ret = NULL; |
paul@181 | 69 | |
paul@181 | 70 | if (!token) |
paul@181 | 71 | return NULL; |
paul@181 | 72 | |
paul@181 | 73 | if (!cache) { |
paul@181 | 74 | if (blkid_get_cache(&c, NULL) < 0) |
paul@181 | 75 | return NULL; |
paul@181 | 76 | } |
paul@181 | 77 | |
paul@181 | 78 | DBG(DEBUG_RESOLVE, |
paul@181 | 79 | printf("looking for %s%s%s %s\n", token, value ? "=" : "", |
paul@181 | 80 | value ? value : "", cache ? "in cache" : "from disk")); |
paul@181 | 81 | |
paul@181 | 82 | if (!value) { |
paul@181 | 83 | if (!strchr(token, '=')) { |
paul@181 | 84 | ret = blkid_strdup(token); |
paul@181 | 85 | goto out; |
paul@181 | 86 | } |
paul@181 | 87 | blkid_parse_tag_string(token, &t, &v); |
paul@181 | 88 | if (!t || !v) |
paul@181 | 89 | goto out; |
paul@181 | 90 | token = t; |
paul@181 | 91 | value = v; |
paul@181 | 92 | } |
paul@181 | 93 | |
paul@181 | 94 | dev = blkid_find_dev_with_tag(c, token, value); |
paul@181 | 95 | if (!dev) |
paul@181 | 96 | goto out; |
paul@181 | 97 | |
paul@181 | 98 | ret = blkid_strdup(blkid_dev_devname(dev)); |
paul@181 | 99 | |
paul@181 | 100 | out: |
paul@181 | 101 | free(t); |
paul@181 | 102 | free(v); |
paul@181 | 103 | if (!cache) { |
paul@181 | 104 | blkid_put_cache(c); |
paul@181 | 105 | } |
paul@181 | 106 | return (ret); |
paul@181 | 107 | } |
paul@181 | 108 | |
paul@181 | 109 | #ifdef TEST_PROGRAM |
paul@181 | 110 | int main(int argc, char **argv) |
paul@181 | 111 | { |
paul@181 | 112 | char *value; |
paul@181 | 113 | blkid_cache cache; |
paul@181 | 114 | |
paul@181 | 115 | blkid_debug_mask = DEBUG_ALL; |
paul@181 | 116 | if (argc != 2 && argc != 3) { |
paul@181 | 117 | fprintf(stderr, "Usage:\t%s tagname=value\n" |
paul@181 | 118 | "\t%s tagname devname\n" |
paul@181 | 119 | "Find which device holds a given token or\n" |
paul@181 | 120 | "Find what the value of a tag is in a device\n", |
paul@181 | 121 | argv[0], argv[0]); |
paul@181 | 122 | exit(1); |
paul@181 | 123 | } |
paul@181 | 124 | if (blkid_get_cache(&cache, "/dev/null") < 0) { |
paul@181 | 125 | fprintf(stderr, "Couldn't get blkid cache\n"); |
paul@181 | 126 | exit(1); |
paul@181 | 127 | } |
paul@181 | 128 | |
paul@181 | 129 | if (argv[2]) { |
paul@181 | 130 | value = blkid_get_tag_value(cache, argv[1], argv[2]); |
paul@181 | 131 | printf("%s has tag %s=%s\n", argv[2], argv[1], |
paul@181 | 132 | value ? value : "<missing>"); |
paul@181 | 133 | } else { |
paul@181 | 134 | value = blkid_get_devname(cache, argv[1], NULL); |
paul@181 | 135 | printf("%s has tag %s\n", value ? value : "<none>", argv[1]); |
paul@181 | 136 | } |
paul@181 | 137 | blkid_put_cache(cache); |
paul@181 | 138 | return value ? 0 : 1; |
paul@181 | 139 | } |
paul@181 | 140 | #endif |