resolve.c revision 50b380b4d4ab668bad45033e3a8aaf93c7f42844
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#if 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#include "probe.h" 24 25#ifdef DEBUG_RESOLVE 26#define DBG(x) x 27#else 28#define DBG(x) 29#endif 30 31 32/* 33 * Find a tagname (e.g. LABEL or UUID) on a specific device. 34 */ 35char *blkid_get_tagname_devname(blkid_cache cache, const char *tagname, 36 const char *devname) 37{ 38 blkid_tag found; 39 blkid_dev dev; 40 char *ret = NULL; 41 42 DBG(printf("looking for %s on %s\n", tagname, devname)); 43 44 if (!devname) 45 return NULL; 46 47 if (!cache) 48 DBG(printf("no cache given, direct device probe\n")); 49 50 if ((dev = blkid_get_devname(cache, devname, BLKID_DEV_NORMAL)) && 51 (found = blkid_find_tag_dev(dev, tagname))) 52 ret = blkid_strdup(found->bit_val); 53 54 if (!cache) 55 blkid_free_dev(dev); 56 57 return ret; 58} 59 60/* 61 * Locate a device name from a token (NAME=value string), or (name, value) 62 * pair. In the case of a token, value is ignored. If the "token" is not 63 * of the form "NAME=value" and there is no value given, then it is assumed 64 * to be the actual devname and a copy is returned. 65 */ 66char *blkid_get_token(blkid_cache cache, const char *token, 67 const char *value) 68{ 69 blkid_dev dev; 70 blkid_cache c = cache; 71 char *t = 0, *v = 0; 72 char *ret = NULL; 73 74 if (!token) 75 return NULL; 76 77 DBG(printf("looking for %s%c%s %s\n", token, value ? '=' : ' ', 78 value ? value : "", cache ? "in cache" : "from disk")); 79 80 if (!cache) { 81 if (blkid_get_cache(&c, NULL) < 0) 82 c = blkid_new_cache(); 83 if (!c) 84 return NULL; 85 } 86 87 if (!value) { 88 blkid_parse_tag_string(token, &t, &v); 89 if (!t || !v) 90 goto errout; 91 token = t; 92 value = v; 93 } 94 95 dev = blkid_find_dev_with_tag(c, token, value); 96 if (!dev) 97 goto errout; 98 99 ret = blkid_strdup(blkid_devname_name(dev)); 100 101errout: 102 if (t) 103 free(t); 104 if (v) 105 free(v); 106 if (!cache) { 107 blkid_put_cache(c); 108 } 109 return (ret); 110} 111 112#ifdef TEST_PROGRAM 113int main(int argc, char **argv) 114{ 115 char *value; 116 117 if (argc != 2 && argc != 3) { 118 fprintf(stderr, "Usage:\t%s tagname=value\n" 119 "\t%s tagname devname\n" 120 "Find which device holds a given token or\n" 121 "Find what the value of a tag is in a device\n", 122 argv[0], argv[0]); 123 exit(1); 124 } 125 if (argv[2]) { 126 value = blkid_get_tagname_devname(NULL, argv[1], argv[2]); 127 printf("%s has tag %s=%s\n", argv[2], argv[1], 128 value ? value : "<missing>"); 129 } else { 130 value = blkid_get_token(NULL, argv[1], NULL); 131 printf("%s has tag %s\n", value ? value : "<none>", argv[1]); 132 } 133 return value ? 0 : 1; 134} 135#endif 136