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 dev->bid_type : "(null)")); 39 DBG(DEBUG_DEV, blkid_debug_dump_dev(dev)); 40 41 list_del(&dev->bid_devs); 42 while (!list_empty(&dev->bid_tags)) { 43 blkid_tag tag = list_entry(dev->bid_tags.next, 44 struct blkid_struct_tag, 45 bit_tags); 46 blkid_free_tag(tag); 47 } 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 free(new_type); 140 free(new_value); 141 return -1; 142 } 143 strcpy(new_type, search_type); 144 strcpy(new_value, search_value); 145 free(iter->search_type); 146 free(iter->search_value); 147 iter->search_type = new_type; 148 iter->search_value = new_value; 149 return 0; 150 } 151 152 /* 153 * Return 0 on success, -1 on error 154 */ 155 extern int blkid_dev_next(blkid_dev_iterate iter, 156 blkid_dev *ret_dev) 157 { 158 blkid_dev dev; 159 160 *ret_dev = 0; 161 if (!iter || iter->magic != DEV_ITERATE_MAGIC) 162 return -1; 163 while (iter->p != &iter->cache->bic_devs) { 164 dev = list_entry(iter->p, struct blkid_struct_dev, bid_devs); 165 iter->p = iter->p->next; 166 if (iter->search_type && 167 !blkid_dev_has_tag(dev, iter->search_type, 168 iter->search_value)) 169 continue; 170 *ret_dev = dev; 171 return 0; 172 } 173 return -1; 174 } 175 176 extern void blkid_dev_iterate_end(blkid_dev_iterate iter) 177 { 178 if (!iter || iter->magic != DEV_ITERATE_MAGIC) 179 return; 180 iter->magic = 0; 181 free(iter); 182 } 183 184 #ifdef TEST_PROGRAM 185 #ifdef HAVE_GETOPT_H 186 #include <getopt.h> 187 #else 188 extern char *optarg; 189 extern int optind; 190 #endif 191 192 void usage(char *prog) 193 { 194 fprintf(stderr, "Usage: %s [-f blkid_file] [-m debug_mask]\n", prog); 195 fprintf(stderr, "\tList all devices and exit\n"); 196 exit(1); 197 } 198 199 int main(int argc, char **argv) 200 { 201 blkid_dev_iterate iter; 202 blkid_cache cache = NULL; 203 blkid_dev dev; 204 int c, ret; 205 char *tmp; 206 char *file = NULL; 207 char *search_type = NULL; 208 char *search_value = NULL; 209 210 while ((c = getopt (argc, argv, "m:f:")) != EOF) 211 switch (c) { 212 case 'f': 213 file = optarg; 214 break; 215 case 'm': 216 blkid_debug_mask = strtoul (optarg, &tmp, 0); 217 if (*tmp) { 218 fprintf(stderr, "Invalid debug mask: %s\n", 219 optarg); 220 exit(1); 221 } 222 break; 223 case '?': 224 usage(argv[0]); 225 } 226 if (argc >= optind+2) { 227 search_type = argv[optind]; 228 search_value = argv[optind+1]; 229 optind += 2; 230 } 231 if (argc != optind) 232 usage(argv[0]); 233 234 if ((ret = blkid_get_cache(&cache, file)) != 0) { 235 fprintf(stderr, "%s: error creating cache (%d)\n", 236 argv[0], ret); 237 exit(1); 238 } 239 240 iter = blkid_dev_iterate_begin(cache); 241 if (search_type) 242 blkid_dev_set_search(iter, search_type, search_value); 243 while (blkid_dev_next(iter, &dev) == 0) { 244 printf("Device: %s\n", blkid_dev_devname(dev)); 245 } 246 blkid_dev_iterate_end(iter); 247 248 249 blkid_put_cache(cache); 250 return (0); 251 } 252 #endif 253