Home | History | Annotate | Download | only in blkid
      1 /*
      2  * dev.c - allocation/initialization/free routines for dev
      3  *
      4  * Copyright (C) 2001 Andreas Dilger
      5  * Copyright (C) 2003 Theodore Ts'o
      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 <stdlib.h>
     14 #include <string.h>
     15 
     16 #include "blkidP.h"
     17 
     18 blkid_dev blkid_new_dev(void)
     19 {
     20 	blkid_dev dev;
     21 
     22 	if (!(dev = (blkid_dev) calloc(1, sizeof(struct blkid_struct_dev))))
     23 		return NULL;
     24 
     25 	INIT_LIST_HEAD(&dev->bid_devs);
     26 	INIT_LIST_HEAD(&dev->bid_tags);
     27 
     28 	return dev;
     29 }
     30 
     31 void blkid_free_dev(blkid_dev dev)
     32 {
     33 	if (!dev)
     34 		return;
     35 
     36 	DBG(DEBUG_DEV,
     37 	    printf("  freeing dev %s (%s)\n", dev->bid_name, dev->bid_type));
     38 	DBG(DEBUG_DEV, blkid_debug_dump_dev(dev));
     39 
     40 	list_del(&dev->bid_devs);
     41 	while (!list_empty(&dev->bid_tags)) {
     42 		blkid_tag tag = list_entry(dev->bid_tags.next,
     43 					   struct blkid_struct_tag,
     44 					   bit_tags);
     45 		blkid_free_tag(tag);
     46 	}
     47 	if (dev->bid_name)
     48 		free(dev->bid_name);
     49 	free(dev);
     50 }
     51 
     52 /*
     53  * Given a blkid device, return its name
     54  */
     55 extern const char *blkid_dev_devname(blkid_dev dev)
     56 {
     57 	return dev->bid_name;
     58 }
     59 
     60 #ifdef CONFIG_BLKID_DEBUG
     61 void blkid_debug_dump_dev(blkid_dev dev)
     62 {
     63 	struct list_head *p;
     64 
     65 	if (!dev) {
     66 		printf("  dev: NULL\n");
     67 		return;
     68 	}
     69 
     70 	printf("  dev: name = %s\n", dev->bid_name);
     71 	printf("  dev: DEVNO=\"0x%0llx\"\n", (long long)dev->bid_devno);
     72 	printf("  dev: TIME=\"%ld\"\n", (long)dev->bid_time);
     73 	printf("  dev: PRI=\"%d\"\n", dev->bid_pri);
     74 	printf("  dev: flags = 0x%08X\n", dev->bid_flags);
     75 
     76 	list_for_each(p, &dev->bid_tags) {
     77 		blkid_tag tag = list_entry(p, struct blkid_struct_tag, bit_tags);
     78 		if (tag)
     79 			printf("    tag: %s=\"%s\"\n", tag->bit_name,
     80 			       tag->bit_val);
     81 		else
     82 			printf("    tag: NULL\n");
     83 	}
     84 	printf("\n");
     85 }
     86 #endif
     87 
     88 /*
     89  * dev iteration routines for the public libblkid interface.
     90  *
     91  * These routines do not expose the list.h implementation, which are a
     92  * contamination of the namespace, and which force us to reveal far, far
     93  * too much of our internal implemenation.  I'm not convinced I want
     94  * to keep list.h in the long term, anyway.  It's fine for kernel
     95  * programming, but performance is not the #1 priority for this
     96  * library, and I really don't like the tradeoff of type-safety for
     97  * performance for this application.  [tytso:20030125.2007EST]
     98  */
     99 
    100 /*
    101  * This series of functions iterate over all devices in a blkid cache
    102  */
    103 #define DEV_ITERATE_MAGIC	0x01a5284c
    104 
    105 struct blkid_struct_dev_iterate {
    106 	int			magic;
    107 	blkid_cache		cache;
    108 	char			*search_type;
    109 	char			*search_value;
    110 	struct list_head	*p;
    111 };
    112 
    113 extern blkid_dev_iterate blkid_dev_iterate_begin(blkid_cache cache)
    114 {
    115 	blkid_dev_iterate	iter;
    116 
    117 	iter = malloc(sizeof(struct blkid_struct_dev_iterate));
    118 	if (iter) {
    119 		iter->magic = DEV_ITERATE_MAGIC;
    120 		iter->cache = cache;
    121 		iter->p	= cache->bic_devs.next;
    122 		iter->search_type = 0;
    123 		iter->search_value = 0;
    124 	}
    125 	return (iter);
    126 }
    127 
    128 extern int blkid_dev_set_search(blkid_dev_iterate iter,
    129 				 char *search_type, char *search_value)
    130 {
    131 	char *new_type, *new_value;
    132 
    133 	if (!iter || iter->magic != DEV_ITERATE_MAGIC || !search_type ||
    134 	    !search_value)
    135 		return -1;
    136 	new_type = malloc(strlen(search_type)+1);
    137 	new_value = malloc(strlen(search_value)+1);
    138 	if (!new_type || !new_value) {
    139 		if (new_type)
    140 			free(new_type);
    141 		if (new_value)
    142 			free(new_value);
    143 		return -1;
    144 	}
    145 	strcpy(new_type, search_type);
    146 	strcpy(new_value, search_value);
    147 	if (iter->search_type)
    148 		free(iter->search_type);
    149 	if (iter->search_value)
    150 		free(iter->search_value);
    151 	iter->search_type = new_type;
    152 	iter->search_value = new_value;
    153 	return 0;
    154 }
    155 
    156 /*
    157  * Return 0 on success, -1 on error
    158  */
    159 extern int blkid_dev_next(blkid_dev_iterate iter,
    160 			  blkid_dev *ret_dev)
    161 {
    162 	blkid_dev		dev;
    163 
    164 	*ret_dev = 0;
    165 	if (!iter || iter->magic != DEV_ITERATE_MAGIC)
    166 		return -1;
    167 	while (iter->p != &iter->cache->bic_devs) {
    168 		dev = list_entry(iter->p, struct blkid_struct_dev, bid_devs);
    169 		iter->p = iter->p->next;
    170 		if (iter->search_type &&
    171 		    !blkid_dev_has_tag(dev, iter->search_type,
    172 				       iter->search_value))
    173 			continue;
    174 		*ret_dev = dev;
    175 		return 0;
    176 	}
    177 	return -1;
    178 }
    179 
    180 extern void blkid_dev_iterate_end(blkid_dev_iterate iter)
    181 {
    182 	if (!iter || iter->magic != DEV_ITERATE_MAGIC)
    183 		return;
    184 	iter->magic = 0;
    185 	free(iter);
    186 }
    187 
    188 #ifdef TEST_PROGRAM
    189 #ifdef HAVE_GETOPT_H
    190 #include <getopt.h>
    191 #else
    192 extern char *optarg;
    193 extern int optind;
    194 #endif
    195 
    196 void usage(char *prog)
    197 {
    198 	fprintf(stderr, "Usage: %s [-f blkid_file] [-m debug_mask]\n", prog);
    199 	fprintf(stderr, "\tList all devices and exit\n");
    200 	exit(1);
    201 }
    202 
    203 int main(int argc, char **argv)
    204 {
    205 	blkid_dev_iterate	iter;
    206 	blkid_cache 		cache = NULL;
    207 	blkid_dev		dev;
    208 	int			c, ret;
    209 	char			*tmp;
    210 	char			*file = NULL;
    211 	char			*search_type = NULL;
    212 	char			*search_value = NULL;
    213 
    214 	while ((c = getopt (argc, argv, "m:f:")) != EOF)
    215 		switch (c) {
    216 		case 'f':
    217 			file = optarg;
    218 			break;
    219 		case 'm':
    220 			blkid_debug_mask = strtoul (optarg, &tmp, 0);
    221 			if (*tmp) {
    222 				fprintf(stderr, "Invalid debug mask: %s\n",
    223 					optarg);
    224 				exit(1);
    225 			}
    226 			break;
    227 		case '?':
    228 			usage(argv[0]);
    229 		}
    230 	if (argc >= optind+2) {
    231 		search_type = argv[optind];
    232 		search_value = argv[optind+1];
    233 		optind += 2;
    234 	}
    235 	if (argc != optind)
    236 		usage(argv[0]);
    237 
    238 	if ((ret = blkid_get_cache(&cache, file)) != 0) {
    239 		fprintf(stderr, "%s: error creating cache (%d)\n",
    240 			argv[0], ret);
    241 		exit(1);
    242 	}
    243 
    244 	iter = blkid_dev_iterate_begin(cache);
    245 	if (search_type)
    246 		blkid_dev_set_search(iter, search_type, search_value);
    247 	while (blkid_dev_next(iter, &dev) == 0) {
    248 		printf("Device: %s\n", blkid_dev_devname(dev));
    249 	}
    250 	blkid_dev_iterate_end(iter);
    251 
    252 
    253 	blkid_put_cache(cache);
    254 	return (0);
    255 }
    256 #endif
    257