blob: 0e2b4e1bc0acd0ffca434b853f40c99411bb1404 [file] [log] [blame]
Mike Frysinger38a33f92005-05-09 22:13:22 +00001/*
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
18blkid_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
31void 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 */
55extern 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
77struct blkid_struct_dev_iterate {
78 int magic;
79 blkid_cache cache;
80 struct list_head *p;
81};
82
83extern blkid_dev_iterate blkid_dev_iterate_begin(blkid_cache cache)
84{
85 blkid_dev_iterate iter;
86
Mike Frysinger7fde8de2005-06-11 22:37:25 +000087 iter = xmalloc(sizeof(struct blkid_struct_dev_iterate));
88 iter->magic = DEV_ITERATE_MAGIC;
89 iter->cache = cache;
90 iter->p = cache->bic_devs.next;
Mike Frysinger38a33f92005-05-09 22:13:22 +000091 return (iter);
92}
93
94/*
95 * Return 0 on success, -1 on error
96 */
97extern int blkid_dev_next(blkid_dev_iterate iter,
98 blkid_dev *dev)
99{
100 *dev = 0;
101 if (!iter || iter->magic != DEV_ITERATE_MAGIC ||
102 iter->p == &iter->cache->bic_devs)
103 return -1;
104 *dev = list_entry(iter->p, struct blkid_struct_dev, bid_devs);
105 iter->p = iter->p->next;
106 return 0;
107}
108
109extern void blkid_dev_iterate_end(blkid_dev_iterate iter)
110{
111 if (!iter || iter->magic != DEV_ITERATE_MAGIC)
112 return;
113 iter->magic = 0;
114 free(iter);
115}
116