blob: bb0cc914a06d4fa4ddaffdafb3090cd400bcf57e [file] [log] [blame]
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001/* vi: set sw=4 ts=4: */
2/*
3 * dev.c - allocation/initialization/free routines for dev
4 *
5 * Copyright (C) 2001 Andreas Dilger
6 * Copyright (C) 2003 Theodore Ts'o
7 *
8 * %Begin-Header%
9 * This file may be redistributed under the terms of the
10 * GNU Lesser General Public License.
11 * %End-Header%
12 */
13
14#include <stdlib.h>
15#include <string.h>
16
17#include "blkidP.h"
18
19blkid_dev blkid_new_dev(void)
20{
21 blkid_dev dev;
22
Denis Vlasenkod02db892008-03-17 09:05:21 +000023 dev = xzalloc(sizeof(struct blkid_struct_dev));
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000024
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 DBG(DEBUG_DEV, blkid_debug_dump_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 */
55const char *blkid_dev_devname(blkid_dev dev)
56{
57 return dev->bid_name;
58}
59
60#ifdef CONFIG_BLKID_DEBUG
61void blkid_debug_dump_dev(blkid_dev dev)
62{
63 struct list_head *p;
64
65 if (!dev) {
66 printf(" dev: NULL\n");
67 return;
68 }
69
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);
75
76 list_for_each(p, &dev->bid_tags) {
77 blkid_tag tag = list_entry(p, struct blkid_struct_tag, bit_tags);
78 if (tag)
79 printf(" tag: %s=\"%s\"\n", tag->bit_name,
80 tag->bit_val);
81 else
82 printf(" tag: NULL\n");
83 }
Denis Vlasenko4daad902007-09-27 10:20:47 +000084 bb_putchar('\n');
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000085}
86#endif
87
88/*
89 * dev iteration routines for the public libblkid interface.
90 *
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]
98 */
99
100/*
101 * This series of functions iterate over all devices in a blkid cache
102 */
103#define DEV_ITERATE_MAGIC 0x01a5284c
104
105struct blkid_struct_dev_iterate {
106 int magic;
107 blkid_cache cache;
108 struct list_head *p;
109};
110
111blkid_dev_iterate blkid_dev_iterate_begin(blkid_cache cache)
112{
113 blkid_dev_iterate iter;
114
115 iter = xmalloc(sizeof(struct blkid_struct_dev_iterate));
116 iter->magic = DEV_ITERATE_MAGIC;
117 iter->cache = cache;
118 iter->p = cache->bic_devs.next;
119 return iter;
120}
121
122/*
123 * Return 0 on success, -1 on error
124 */
125extern int blkid_dev_next(blkid_dev_iterate iter,
126 blkid_dev *dev)
127{
128 *dev = 0;
129 if (!iter || iter->magic != DEV_ITERATE_MAGIC ||
130 iter->p == &iter->cache->bic_devs)
131 return -1;
132 *dev = list_entry(iter->p, struct blkid_struct_dev, bid_devs);
133 iter->p = iter->p->next;
134 return 0;
135}
136
137void blkid_dev_iterate_end(blkid_dev_iterate iter)
138{
139 if (!iter || iter->magic != DEV_ITERATE_MAGIC)
140 return;
141 iter->magic = 0;
142 free(iter);
143}
144
145#ifdef TEST_PROGRAM
146#ifdef HAVE_GETOPT_H
147#include <getopt.h>
148#else
149extern char *optarg;
150extern int optind;
151#endif
152
153void usage(char *prog)
154{
155 fprintf(stderr, "Usage: %s [-f blkid_file] [-m debug_mask]\n", prog);
156 fprintf(stderr, "\tList all devices and exit\n", prog);
157 exit(1);
158}
159
160int main(int argc, char **argv)
161{
162 blkid_dev_iterate iter;
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000163 blkid_cache cache = NULL;
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000164 blkid_dev dev;
165 int c, ret;
166 char *tmp;
167 char *file = NULL;
168 char *search_type = NULL;
169 char *search_value = NULL;
170
171 while ((c = getopt (argc, argv, "m:f:")) != EOF)
172 switch (c) {
173 case 'f':
174 file = optarg;
175 break;
176 case 'm':
177 blkid_debug_mask = strtoul (optarg, &tmp, 0);
178 if (*tmp) {
179 fprintf(stderr, "Invalid debug mask: %d\n",
180 optarg);
181 exit(1);
182 }
183 break;
184 case '?':
185 usage(argv[0]);
186 }
187 if (argc >= optind+2) {
188 search_type = argv[optind];
189 search_value = argv[optind+1];
190 optind += 2;
191 }
192 if (argc != optind)
193 usage(argv[0]);
194
195 if ((ret = blkid_get_cache(&cache, file)) != 0) {
196 fprintf(stderr, "%s: error creating cache (%d)\n",
197 argv[0], ret);
198 exit(1);
199 }
200
201 iter = blkid_dev_iterate_begin(cache);
202 if (search_type)
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));
206 }
207 blkid_dev_iterate_end(iter);
208
209
210 blkid_put_cache(cache);
211 return 0;
212}
213#endif