"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Mike Frysinger | 38a33f9 | 2005-05-09 22:13:22 +0000 | [diff] [blame] | 2 | /* |
| 3 | * dev.c - allocation/initialization/free routines for dev |
| 4 | * |
| 5 | * Copyright (C) 2001 Andreas Dilger |
| 6 | * Copyright (C) 2003 Theodore Ts'o |
| 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 <stdlib.h> |
| 15 | #include <string.h> |
| 16 | |
| 17 | #include "blkidP.h" |
| 18 | |
| 19 | blkid_dev blkid_new_dev(void) |
| 20 | { |
| 21 | blkid_dev dev; |
| 22 | |
| 23 | if (!(dev = (blkid_dev) calloc(1, sizeof(struct blkid_struct_dev)))) |
| 24 | return NULL; |
| 25 | |
| 26 | INIT_LIST_HEAD(&dev->bid_devs); |
| 27 | INIT_LIST_HEAD(&dev->bid_tags); |
| 28 | |
| 29 | return dev; |
| 30 | } |
| 31 | |
| 32 | void blkid_free_dev(blkid_dev dev) |
| 33 | { |
| 34 | if (!dev) |
| 35 | return; |
| 36 | |
| 37 | DBG(DEBUG_DEV, |
| 38 | printf(" freeing dev %s (%s)\n", dev->bid_name, dev->bid_type)); |
Mike Frysinger | 874af85 | 2006-03-08 07:03:27 +0000 | [diff] [blame] | 39 | DBG(DEBUG_DEV, blkid_debug_dump_dev(dev)); |
Mike Frysinger | 38a33f9 | 2005-05-09 22:13:22 +0000 | [diff] [blame] | 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 | if (dev->bid_name) |
| 49 | free(dev->bid_name); |
| 50 | free(dev); |
| 51 | } |
| 52 | |
| 53 | /* |
| 54 | * Given a blkid device, return its name |
| 55 | */ |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 56 | const char *blkid_dev_devname(blkid_dev dev) |
Mike Frysinger | 38a33f9 | 2005-05-09 22:13:22 +0000 | [diff] [blame] | 57 | { |
| 58 | return dev->bid_name; |
| 59 | } |
| 60 | |
Mike Frysinger | 874af85 | 2006-03-08 07:03:27 +0000 | [diff] [blame] | 61 | #ifdef CONFIG_BLKID_DEBUG |
| 62 | void blkid_debug_dump_dev(blkid_dev dev) |
| 63 | { |
| 64 | struct list_head *p; |
| 65 | |
| 66 | if (!dev) { |
| 67 | printf(" dev: NULL\n"); |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | printf(" dev: name = %s\n", dev->bid_name); |
| 72 | printf(" dev: DEVNO=\"0x%0llx\"\n", dev->bid_devno); |
| 73 | printf(" dev: TIME=\"%lu\"\n", dev->bid_time); |
| 74 | printf(" dev: PRI=\"%d\"\n", dev->bid_pri); |
| 75 | printf(" dev: flags = 0x%08X\n", dev->bid_flags); |
| 76 | |
| 77 | list_for_each(p, &dev->bid_tags) { |
| 78 | blkid_tag tag = list_entry(p, struct blkid_struct_tag, bit_tags); |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 79 | if (tag) |
| 80 | printf(" tag: %s=\"%s\"\n", tag->bit_name, |
Mike Frysinger | 874af85 | 2006-03-08 07:03:27 +0000 | [diff] [blame] | 81 | tag->bit_val); |
| 82 | else |
| 83 | printf(" tag: NULL\n"); |
| 84 | } |
Denis Vlasenko | c6f188d | 2006-10-26 00:37:00 +0000 | [diff] [blame] | 85 | puts(""); |
Mike Frysinger | 874af85 | 2006-03-08 07:03:27 +0000 | [diff] [blame] | 86 | } |
| 87 | #endif |
| 88 | |
Mike Frysinger | 38a33f9 | 2005-05-09 22:13:22 +0000 | [diff] [blame] | 89 | /* |
| 90 | * dev iteration routines for the public libblkid interface. |
| 91 | * |
| 92 | * These routines do not expose the list.h implementation, which are a |
| 93 | * contamination of the namespace, and which force us to reveal far, far |
| 94 | * too much of our internal implemenation. I'm not convinced I want |
| 95 | * to keep list.h in the long term, anyway. It's fine for kernel |
| 96 | * programming, but performance is not the #1 priority for this |
| 97 | * library, and I really don't like the tradeoff of type-safety for |
| 98 | * performance for this application. [tytso:20030125.2007EST] |
| 99 | */ |
| 100 | |
| 101 | /* |
| 102 | * This series of functions iterate over all devices in a blkid cache |
| 103 | */ |
| 104 | #define DEV_ITERATE_MAGIC 0x01a5284c |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 105 | |
Mike Frysinger | 38a33f9 | 2005-05-09 22:13:22 +0000 | [diff] [blame] | 106 | struct blkid_struct_dev_iterate { |
| 107 | int magic; |
| 108 | blkid_cache cache; |
| 109 | struct list_head *p; |
| 110 | }; |
| 111 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 112 | blkid_dev_iterate blkid_dev_iterate_begin(blkid_cache cache) |
Mike Frysinger | 38a33f9 | 2005-05-09 22:13:22 +0000 | [diff] [blame] | 113 | { |
| 114 | blkid_dev_iterate iter; |
| 115 | |
Mike Frysinger | 7fde8de | 2005-06-11 22:37:25 +0000 | [diff] [blame] | 116 | iter = xmalloc(sizeof(struct blkid_struct_dev_iterate)); |
| 117 | iter->magic = DEV_ITERATE_MAGIC; |
| 118 | iter->cache = cache; |
| 119 | iter->p = cache->bic_devs.next; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame^] | 120 | return iter; |
Mike Frysinger | 38a33f9 | 2005-05-09 22:13:22 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /* |
| 124 | * Return 0 on success, -1 on error |
| 125 | */ |
| 126 | extern int blkid_dev_next(blkid_dev_iterate iter, |
| 127 | blkid_dev *dev) |
| 128 | { |
| 129 | *dev = 0; |
| 130 | if (!iter || iter->magic != DEV_ITERATE_MAGIC || |
| 131 | iter->p == &iter->cache->bic_devs) |
| 132 | return -1; |
| 133 | *dev = list_entry(iter->p, struct blkid_struct_dev, bid_devs); |
| 134 | iter->p = iter->p->next; |
| 135 | return 0; |
| 136 | } |
| 137 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 138 | void blkid_dev_iterate_end(blkid_dev_iterate iter) |
Mike Frysinger | 38a33f9 | 2005-05-09 22:13:22 +0000 | [diff] [blame] | 139 | { |
| 140 | if (!iter || iter->magic != DEV_ITERATE_MAGIC) |
| 141 | return; |
| 142 | iter->magic = 0; |
| 143 | free(iter); |
| 144 | } |
| 145 | |
Mike Frysinger | 874af85 | 2006-03-08 07:03:27 +0000 | [diff] [blame] | 146 | #ifdef TEST_PROGRAM |
| 147 | #ifdef HAVE_GETOPT_H |
| 148 | #include <getopt.h> |
| 149 | #else |
| 150 | extern char *optarg; |
| 151 | extern int optind; |
| 152 | #endif |
| 153 | |
| 154 | void usage(char *prog) |
| 155 | { |
| 156 | fprintf(stderr, "Usage: %s [-f blkid_file] [-m debug_mask]\n", prog); |
| 157 | fprintf(stderr, "\tList all devices and exit\n", prog); |
| 158 | exit(1); |
| 159 | } |
| 160 | |
| 161 | int main(int argc, char **argv) |
| 162 | { |
| 163 | blkid_dev_iterate iter; |
| 164 | blkid_cache cache = NULL; |
| 165 | blkid_dev dev; |
| 166 | int c, ret; |
| 167 | char *tmp; |
| 168 | char *file = NULL; |
| 169 | char *search_type = NULL; |
| 170 | char *search_value = NULL; |
| 171 | |
| 172 | while ((c = getopt (argc, argv, "m:f:")) != EOF) |
| 173 | switch (c) { |
| 174 | case 'f': |
| 175 | file = optarg; |
| 176 | break; |
| 177 | case 'm': |
| 178 | blkid_debug_mask = strtoul (optarg, &tmp, 0); |
| 179 | if (*tmp) { |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 180 | fprintf(stderr, "Invalid debug mask: %d\n", |
Mike Frysinger | 874af85 | 2006-03-08 07:03:27 +0000 | [diff] [blame] | 181 | optarg); |
| 182 | exit(1); |
| 183 | } |
| 184 | break; |
| 185 | case '?': |
| 186 | usage(argv[0]); |
| 187 | } |
| 188 | if (argc >= optind+2) { |
| 189 | search_type = argv[optind]; |
| 190 | search_value = argv[optind+1]; |
| 191 | optind += 2; |
| 192 | } |
| 193 | if (argc != optind) |
| 194 | usage(argv[0]); |
| 195 | |
| 196 | if ((ret = blkid_get_cache(&cache, file)) != 0) { |
| 197 | fprintf(stderr, "%s: error creating cache (%d)\n", |
| 198 | argv[0], ret); |
| 199 | exit(1); |
| 200 | } |
| 201 | |
| 202 | iter = blkid_dev_iterate_begin(cache); |
| 203 | if (search_type) |
| 204 | blkid_dev_set_search(iter, search_type, search_value); |
| 205 | while (blkid_dev_next(iter, &dev) == 0) { |
| 206 | printf("Device: %s\n", blkid_dev_devname(dev)); |
| 207 | } |
| 208 | blkid_dev_iterate_end(iter); |
| 209 | |
| 210 | |
| 211 | blkid_put_cache(cache); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 212 | return 0; |
Mike Frysinger | 874af85 | 2006-03-08 07:03:27 +0000 | [diff] [blame] | 213 | } |
| 214 | #endif |