2 * dev.c - allocation/initialization/free routines for dev
4 * Copyright (C) 2001 Andreas Dilger
5 * Copyright (C) 2003 Theodore Ts'o
8 * This file may be redistributed under the terms of the
9 * GNU Lesser General Public License.
19 blkid_dev blkid_new_dev(void)
23 if (!(dev = (blkid_dev) calloc(1, sizeof(struct blkid_struct_dev))))
26 INIT_LIST_HEAD(&dev->bid_devs);
27 INIT_LIST_HEAD(&dev->bid_tags);
32 void blkid_free_dev(blkid_dev dev)
38 printf(" freeing dev %s (%s)\n", dev->bid_name, dev->bid_type ?
39 dev->bid_type : "(null)"));
40 DBG(DEBUG_DEV, blkid_debug_dump_dev(dev));
42 list_del(&dev->bid_devs);
43 while (!list_empty(&dev->bid_tags)) {
44 blkid_tag tag = list_entry(dev->bid_tags.next,
45 struct blkid_struct_tag,
54 * Given a blkid device, return its name
56 extern const char *blkid_dev_devname(blkid_dev dev)
61 #ifdef CONFIG_BLKID_DEBUG
62 void blkid_debug_dump_dev(blkid_dev dev)
67 printf(" dev: NULL\n");
71 printf(" dev: name = %s\n", dev->bid_name);
72 printf(" dev: DEVNO=\"0x%0llx\"\n", (long long)dev->bid_devno);
73 printf(" dev: TIME=\"%ld\"\n", (long)dev->bid_time);
74 printf(" dev: PRI=\"%d\"\n", dev->bid_pri);
75 printf(" dev: flags = 0x%08X\n", dev->bid_flags);
77 list_for_each(p, &dev->bid_tags) {
78 blkid_tag tag = list_entry(p, struct blkid_struct_tag, bit_tags);
80 printf(" tag: %s=\"%s\"\n", tag->bit_name,
83 printf(" tag: NULL\n");
90 * dev iteration routines for the public libblkid interface.
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]
102 * This series of functions iterate over all devices in a blkid cache
104 #define DEV_ITERATE_MAGIC 0x01a5284c
106 struct blkid_struct_dev_iterate {
114 extern blkid_dev_iterate blkid_dev_iterate_begin(blkid_cache cache)
116 blkid_dev_iterate iter;
118 iter = malloc(sizeof(struct blkid_struct_dev_iterate));
120 iter->magic = DEV_ITERATE_MAGIC;
122 iter->p = cache->bic_devs.next;
123 iter->search_type = 0;
124 iter->search_value = 0;
129 extern int blkid_dev_set_search(blkid_dev_iterate iter,
130 char *search_type, char *search_value)
132 char *new_type, *new_value;
134 if (!iter || iter->magic != DEV_ITERATE_MAGIC || !search_type ||
137 new_type = malloc(strlen(search_type)+1);
138 new_value = malloc(strlen(search_value)+1);
139 if (!new_type || !new_value) {
144 strcpy(new_type, search_type);
145 strcpy(new_value, search_value);
146 free(iter->search_type);
147 free(iter->search_value);
148 iter->search_type = new_type;
149 iter->search_value = new_value;
154 * Return 0 on success, -1 on error
156 extern int blkid_dev_next(blkid_dev_iterate iter,
162 if (!iter || iter->magic != DEV_ITERATE_MAGIC)
164 while (iter->p != &iter->cache->bic_devs) {
165 dev = list_entry(iter->p, struct blkid_struct_dev, bid_devs);
166 iter->p = iter->p->next;
167 if (iter->search_type &&
168 !blkid_dev_has_tag(dev, iter->search_type,
177 extern void blkid_dev_iterate_end(blkid_dev_iterate iter)
179 if (!iter || iter->magic != DEV_ITERATE_MAGIC)
193 void usage(char *prog)
195 fprintf(stderr, "Usage: %s [-f blkid_file] [-m debug_mask]\n", prog);
196 fprintf(stderr, "\tList all devices and exit\n");
200 int main(int argc, char **argv)
202 blkid_dev_iterate iter;
203 blkid_cache cache = NULL;
208 char *search_type = NULL;
209 char *search_value = NULL;
211 while ((c = getopt (argc, argv, "m:f:")) != EOF)
217 blkid_debug_mask = strtoul (optarg, &tmp, 0);
219 fprintf(stderr, "Invalid debug mask: %s\n",
227 if (argc >= optind+2) {
228 search_type = argv[optind];
229 search_value = argv[optind+1];
235 if ((ret = blkid_get_cache(&cache, file)) != 0) {
236 fprintf(stderr, "%s: error creating cache (%d)\n",
241 iter = blkid_dev_iterate_begin(cache);
243 blkid_dev_set_search(iter, search_type, search_value);
244 while (blkid_dev_next(iter, &dev) == 0) {
245 printf("Device: %s\n", blkid_dev_devname(dev));
247 blkid_dev_iterate_end(iter);
250 blkid_put_cache(cache);