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