Whamcloud - gitweb
Add dynamic debugging capabilities to the blkid library,
[tools/e2fsprogs.git] / lib / blkid / dev.c
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         DEB_DUMP_DEV(DEBUG_DEV, dev);
39
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,
44                                            bit_tags);
45                 blkid_free_tag(tag);
46         }
47         if (dev->bid_name)
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 /*
61  * dev iteration routines for the public libblkid interface.
62  *
63  * These routines do not expose the list.h implementation, which are a
64  * contamination of the namespace, and which force us to reveal far, far
65  * too much of our internal implemenation.  I'm not convinced I want
66  * to keep list.h in the long term, anyway.  It's fine for kernel
67  * programming, but performance is not the #1 priority for this
68  * library, and I really don't like the tradeoff of type-safety for
69  * performance for this application.  [tytso:20030125.2007EST]
70  */
71
72 /*
73  * This series of functions iterate over all devices in a blkid cache
74  */
75 #define DEV_ITERATE_MAGIC       0x01a5284c
76         
77 struct blkid_struct_dev_iterate {
78         int                     magic;
79         blkid_cache             cache;
80         struct list_head        *p;
81 };
82
83 extern blkid_dev_iterate blkid_dev_iterate_begin(blkid_cache cache)
84 {
85         blkid_dev_iterate       iter;
86
87         iter = malloc(sizeof(struct blkid_struct_dev_iterate));
88         if (iter) {
89                 iter->magic = DEV_ITERATE_MAGIC;
90                 iter->cache = cache;
91                 iter->p = cache->bic_devs.next;
92         }
93         return (iter);
94 }
95
96 /*
97  * Return 0 on success, -1 on error
98  */
99 extern int blkid_dev_next(blkid_dev_iterate iter,
100                           blkid_dev *dev)
101 {
102         *dev = 0;
103         if (!iter || iter->magic != DEV_ITERATE_MAGIC ||
104             iter->p == &iter->cache->bic_devs)
105                 return -1;
106         *dev = list_entry(iter->p, struct blkid_struct_dev, bid_devs);
107         iter->p = iter->p->next;
108         return 0;
109 }
110
111 extern void blkid_dev_iterate_end(blkid_dev_iterate iter)
112 {
113         if (!iter || iter->magic != DEV_ITERATE_MAGIC)
114                 return;
115         iter->magic = 0;
116         free(iter);
117 }
118