source: MondoRescue/branches/2.2.5/mindi-busybox/e2fsprogs/old_e2fsprogs/blkid/dev.c@ 1765

Last change on this file since 1765 was 1765, checked in by Bruno Cornec, 16 years ago

Update to busybox 1.7.2

  • Property svn:eol-style set to native
File size: 4.5 KB
Line 
1/* 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
23 if (!(dev = (blkid_dev) calloc(1, sizeof(struct blkid_struct_dev))))
24 return NULL;
25
26 INIT_LIST_HEAD(&dev->bid_devs);
27 INIT_LIST_HEAD(&dev->bid_tags);
28
29 return dev;
30}
31
32void blkid_free_dev(blkid_dev dev)
33{
34 if (!dev)
35 return;
36
37 DBG(DEBUG_DEV,
38 printf(" freeing dev %s (%s)\n", dev->bid_name, dev->bid_type));
39 DBG(DEBUG_DEV, blkid_debug_dump_dev(dev));
40
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,
45 bit_tags);
46 blkid_free_tag(tag);
47 }
48 if (dev->bid_name)
49 free(dev->bid_name);
50 free(dev);
51}
52
53/*
54 * Given a blkid device, return its name
55 */
56const char *blkid_dev_devname(blkid_dev dev)
57{
58 return dev->bid_name;
59}
60
61#ifdef CONFIG_BLKID_DEBUG
62void blkid_debug_dump_dev(blkid_dev dev)
63{
64 struct list_head *p;
65
66 if (!dev) {
67 printf(" dev: NULL\n");
68 return;
69 }
70
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);
76
77 list_for_each(p, &dev->bid_tags) {
78 blkid_tag tag = list_entry(p, struct blkid_struct_tag, bit_tags);
79 if (tag)
80 printf(" tag: %s=\"%s\"\n", tag->bit_name,
81 tag->bit_val);
82 else
83 printf(" tag: NULL\n");
84 }
85 puts("");
86}
87#endif
88
89/*
90 * dev iteration routines for the public libblkid interface.
91 *
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]
99 */
100
101/*
102 * This series of functions iterate over all devices in a blkid cache
103 */
104#define DEV_ITERATE_MAGIC 0x01a5284c
105
106struct blkid_struct_dev_iterate {
107 int magic;
108 blkid_cache cache;
109 struct list_head *p;
110};
111
112blkid_dev_iterate blkid_dev_iterate_begin(blkid_cache cache)
113{
114 blkid_dev_iterate iter;
115
116 iter = xmalloc(sizeof(struct blkid_struct_dev_iterate));
117 iter->magic = DEV_ITERATE_MAGIC;
118 iter->cache = cache;
119 iter->p = cache->bic_devs.next;
120 return iter;
121}
122
123/*
124 * Return 0 on success, -1 on error
125 */
126extern int blkid_dev_next(blkid_dev_iterate iter,
127 blkid_dev *dev)
128{
129 *dev = 0;
130 if (!iter || iter->magic != DEV_ITERATE_MAGIC ||
131 iter->p == &iter->cache->bic_devs)
132 return -1;
133 *dev = list_entry(iter->p, struct blkid_struct_dev, bid_devs);
134 iter->p = iter->p->next;
135 return 0;
136}
137
138void blkid_dev_iterate_end(blkid_dev_iterate iter)
139{
140 if (!iter || iter->magic != DEV_ITERATE_MAGIC)
141 return;
142 iter->magic = 0;
143 free(iter);
144}
145
146#ifdef TEST_PROGRAM
147#ifdef HAVE_GETOPT_H
148#include <getopt.h>
149#else
150extern char *optarg;
151extern int optind;
152#endif
153
154void usage(char *prog)
155{
156 fprintf(stderr, "Usage: %s [-f blkid_file] [-m debug_mask]\n", prog);
157 fprintf(stderr, "\tList all devices and exit\n", prog);
158 exit(1);
159}
160
161int main(int argc, char **argv)
162{
163 blkid_dev_iterate iter;
164 blkid_cache cache = NULL;
165 blkid_dev dev;
166 int c, ret;
167 char *tmp;
168 char *file = NULL;
169 char *search_type = NULL;
170 char *search_value = NULL;
171
172 while ((c = getopt (argc, argv, "m:f:")) != EOF)
173 switch (c) {
174 case 'f':
175 file = optarg;
176 break;
177 case 'm':
178 blkid_debug_mask = strtoul (optarg, &tmp, 0);
179 if (*tmp) {
180 fprintf(stderr, "Invalid debug mask: %d\n",
181 optarg);
182 exit(1);
183 }
184 break;
185 case '?':
186 usage(argv[0]);
187 }
188 if (argc >= optind+2) {
189 search_type = argv[optind];
190 search_value = argv[optind+1];
191 optind += 2;
192 }
193 if (argc != optind)
194 usage(argv[0]);
195
196 if ((ret = blkid_get_cache(&cache, file)) != 0) {
197 fprintf(stderr, "%s: error creating cache (%d)\n",
198 argv[0], ret);
199 exit(1);
200 }
201
202 iter = blkid_dev_iterate_begin(cache);
203 if (search_type)
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));
207 }
208 blkid_dev_iterate_end(iter);
209
210
211 blkid_put_cache(cache);
212 return 0;
213}
214#endif
Note: See TracBrowser for help on using the repository browser.