1 /* vi: set sw=4 ts=4: */
3 * dev.c - allocation/initialization/free routines for dev
5 * Copyright (C) 2001 Andreas Dilger
6 * Copyright (C) 2003 Theodore Ts'o
9 * This file may be redistributed under the terms of the
10 * 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 DBG(DEBUG_DEV, blkid_debug_dump_dev(dev));
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,
54 * Given a blkid device, return its name
56 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", 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);
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 {
112 blkid_dev_iterate blkid_dev_iterate_begin(blkid_cache cache)
114 blkid_dev_iterate iter;
116 iter = xmalloc(sizeof(struct blkid_struct_dev_iterate));
117 iter->magic = DEV_ITERATE_MAGIC;
119 iter->p = cache->bic_devs.next;
124 * Return 0 on success, -1 on error
126 extern int blkid_dev_next(blkid_dev_iterate iter,
130 if (!iter || iter->magic != DEV_ITERATE_MAGIC ||
131 iter->p == &iter->cache->bic_devs)
133 *dev = list_entry(iter->p, struct blkid_struct_dev, bid_devs);
134 iter->p = iter->p->next;
138 void blkid_dev_iterate_end(blkid_dev_iterate iter)
140 if (!iter || iter->magic != DEV_ITERATE_MAGIC)
154 void usage(char *prog)
156 fprintf(stderr, "Usage: %s [-f blkid_file] [-m debug_mask]\n", prog);
157 fprintf(stderr, "\tList all devices and exit\n", prog);
161 int main(int argc, char **argv)
163 blkid_dev_iterate iter;
164 blkid_cache cache = NULL;
169 char *search_type = NULL;
170 char *search_value = NULL;
172 while ((c = getopt (argc, argv, "m:f:")) != EOF)
178 blkid_debug_mask = strtoul (optarg, &tmp, 0);
180 fprintf(stderr, "Invalid debug mask: %d\n",
188 if (argc >= optind+2) {
189 search_type = argv[optind];
190 search_value = argv[optind+1];
196 if ((ret = blkid_get_cache(&cache, file)) != 0) {
197 fprintf(stderr, "%s: error creating cache (%d)\n",
202 iter = blkid_dev_iterate_begin(cache);
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));
208 blkid_dev_iterate_end(iter);
211 blkid_put_cache(cache);