Home | History | Annotate | Download | only in blkid
      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 
     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 
     40 	if (!cache) {
     41 		if (blkid_get_cache(&c, NULL) < 0)
     42 			return NULL;
     43 	}
     44 
     45 	if ((dev = blkid_get_dev(c, devname, BLKID_DEV_NORMAL)) &&
     46 	    (found = blkid_find_tag_dev(dev, tagname)))
     47 		ret = blkid_strdup(found->bit_val);
     48 
     49 	if (!cache)
     50 		blkid_put_cache(c);
     51 
     52 	return ret;
     53 }
     54 
     55 /*
     56  * Locate a device name from a token (NAME=value string), or (name, value)
     57  * pair.  In the case of a token, value is ignored.  If the "token" is not
     58  * of the form "NAME=value" and there is no value given, then it is assumed
     59  * to be the actual devname and a copy is returned.
     60  */
     61 char *blkid_get_devname(blkid_cache cache, const char *token,
     62 			const char *value)
     63 {
     64 	blkid_dev dev;
     65 	blkid_cache c = cache;
     66 	char *t = 0, *v = 0;
     67 	char *ret = NULL;
     68 
     69 	if (!token)
     70 		return NULL;
     71 
     72 	if (!cache) {
     73 		if (blkid_get_cache(&c, NULL) < 0)
     74 			return NULL;
     75 	}
     76 
     77 	DBG(DEBUG_RESOLVE,
     78 	    printf("looking for %s%s%s %s\n", token, value ? "=" : "",
     79 		   value ? value : "", cache ? "in cache" : "from disk"));
     80 
     81 	if (!value) {
     82 		if (!strchr(token, '=')) {
     83 			ret = blkid_strdup(token);
     84 			goto out;
     85 		}
     86 		blkid_parse_tag_string(token, &t, &v);
     87 		if (!t || !v)
     88 			goto out;
     89 		token = t;
     90 		value = v;
     91 	}
     92 
     93 	dev = blkid_find_dev_with_tag(c, token, value);
     94 	if (!dev)
     95 		goto out;
     96 
     97 	ret = blkid_strdup(blkid_dev_devname(dev));
     98 
     99 out:
    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, "/dev/null") < 0) {
    124 		fprintf(stderr, "Couldn't get blkid cache\n");
    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
    140