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 dev = xzalloc(sizeof(struct blkid_struct_dev));
25 INIT_LIST_HEAD(&dev->bid_devs);
26 INIT_LIST_HEAD(&dev->bid_tags);
31 void blkid_free_dev(blkid_dev dev)
37 printf(" freeing dev %s (%s)\n", dev->bid_name, dev->bid_type));
38 DBG(DEBUG_DEV, blkid_debug_dump_dev(dev));
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,
53 * Given a blkid device, return its name
55 const char *blkid_dev_devname(blkid_dev dev)
60 #ifdef CONFIG_BLKID_DEBUG
61 void blkid_debug_dump_dev(blkid_dev dev)
66 printf(" dev: NULL\n");
70 printf(" dev: name = %s\n", dev->bid_name);
71 printf(" dev: DEVNO=\"0x%0llx\"\n", dev->bid_devno);
72 printf(" dev: TIME=\"%lu\"\n", dev->bid_time);
73 printf(" dev: PRI=\"%d\"\n", dev->bid_pri);
74 printf(" dev: flags = 0x%08X\n", dev->bid_flags);
76 list_for_each(p, &dev->bid_tags) {
77 blkid_tag tag = list_entry(p, struct blkid_struct_tag, bit_tags);
79 printf(" tag: %s=\"%s\"\n", tag->bit_name,
82 printf(" tag: NULL\n");
89 * dev iteration routines for the public libblkid interface.
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]
101 * This series of functions iterate over all devices in a blkid cache
103 #define DEV_ITERATE_MAGIC 0x01a5284c
105 struct blkid_struct_dev_iterate {
111 blkid_dev_iterate blkid_dev_iterate_begin(blkid_cache cache)
113 blkid_dev_iterate iter;
115 iter = xmalloc(sizeof(struct blkid_struct_dev_iterate));
116 iter->magic = DEV_ITERATE_MAGIC;
118 iter->p = cache->bic_devs.next;
123 * Return 0 on success, -1 on error
125 extern int blkid_dev_next(blkid_dev_iterate iter,
129 if (!iter || iter->magic != DEV_ITERATE_MAGIC ||
130 iter->p == &iter->cache->bic_devs)
132 *dev = list_entry(iter->p, struct blkid_struct_dev, bid_devs);
133 iter->p = iter->p->next;
137 void blkid_dev_iterate_end(blkid_dev_iterate iter)
139 if (!iter || iter->magic != DEV_ITERATE_MAGIC)
153 void usage(char *prog)
155 fprintf(stderr, "Usage: %s [-f blkid_file] [-m debug_mask]\n", prog);
156 fprintf(stderr, "\tList all devices and exit\n", prog);
160 int main(int argc, char **argv)
162 blkid_dev_iterate iter;
163 blkid_cache cache = NULL;
168 char *search_type = NULL;
169 char *search_value = NULL;
171 while ((c = getopt (argc, argv, "m:f:")) != EOF)
177 blkid_debug_mask = strtoul (optarg, &tmp, 0);
179 fprintf(stderr, "Invalid debug mask: %d\n",
187 if (argc >= optind+2) {
188 search_type = argv[optind];
189 search_value = argv[optind+1];
195 if ((ret = blkid_get_cache(&cache, file)) != 0) {
196 fprintf(stderr, "%s: error creating cache (%d)\n",
201 iter = blkid_dev_iterate_begin(cache);
203 blkid_dev_set_search(iter, search_type, search_value);
204 while (blkid_dev_next(iter, &dev) == 0) {
205 printf("Device: %s\n", blkid_dev_devname(dev));
207 blkid_dev_iterate_end(iter);
210 blkid_put_cache(cache);