]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - lib/blkid/dev.c
Fix typos in code comments and developer docs
[thirdparty/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 "config.h"
14 #include <stdlib.h>
15 #include <string.h>
16
17 #include "blkidP.h"
18
19 blkid_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
32 void 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 dev->bid_type : "(null)"));
40 DBG(DEBUG_DEV, blkid_debug_dump_dev(dev));
41
42 list_del(&dev->bid_devs);
43 while (!list_empty(&dev->bid_tags)) {
44 blkid_tag tag = list_entry(dev->bid_tags.next,
45 struct blkid_struct_tag,
46 bit_tags);
47 blkid_free_tag(tag);
48 }
49 free(dev->bid_name);
50 free(dev);
51 }
52
53 /*
54 * Given a blkid device, return its name
55 */
56 extern const char *blkid_dev_devname(blkid_dev dev)
57 {
58 return dev->bid_name;
59 }
60
61 #ifdef CONFIG_BLKID_DEBUG
62 void 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", (long long)dev->bid_devno);
73 printf(" dev: TIME=\"%ld\"\n", (long)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 printf("\n");
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 implementation. 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
106 struct blkid_struct_dev_iterate {
107 int magic;
108 blkid_cache cache;
109 char *search_type;
110 char *search_value;
111 struct list_head *p;
112 };
113
114 extern blkid_dev_iterate blkid_dev_iterate_begin(blkid_cache cache)
115 {
116 blkid_dev_iterate iter;
117
118 iter = malloc(sizeof(struct blkid_struct_dev_iterate));
119 if (iter) {
120 iter->magic = DEV_ITERATE_MAGIC;
121 iter->cache = cache;
122 iter->p = cache->bic_devs.next;
123 iter->search_type = 0;
124 iter->search_value = 0;
125 }
126 return (iter);
127 }
128
129 extern int blkid_dev_set_search(blkid_dev_iterate iter,
130 char *search_type, char *search_value)
131 {
132 char *new_type, *new_value;
133
134 if (!iter || iter->magic != DEV_ITERATE_MAGIC || !search_type ||
135 !search_value)
136 return -1;
137 new_type = malloc(strlen(search_type)+1);
138 new_value = malloc(strlen(search_value)+1);
139 if (!new_type || !new_value) {
140 free(new_type);
141 free(new_value);
142 return -1;
143 }
144 strcpy(new_type, search_type);
145 strcpy(new_value, search_value);
146 free(iter->search_type);
147 free(iter->search_value);
148 iter->search_type = new_type;
149 iter->search_value = new_value;
150 return 0;
151 }
152
153 /*
154 * Return 0 on success, -1 on error
155 */
156 extern int blkid_dev_next(blkid_dev_iterate iter,
157 blkid_dev *ret_dev)
158 {
159 blkid_dev dev;
160
161 *ret_dev = 0;
162 if (!iter || iter->magic != DEV_ITERATE_MAGIC)
163 return -1;
164 while (iter->p != &iter->cache->bic_devs) {
165 dev = list_entry(iter->p, struct blkid_struct_dev, bid_devs);
166 iter->p = iter->p->next;
167 if (iter->search_type &&
168 !blkid_dev_has_tag(dev, iter->search_type,
169 iter->search_value))
170 continue;
171 *ret_dev = dev;
172 return 0;
173 }
174 return -1;
175 }
176
177 extern void blkid_dev_iterate_end(blkid_dev_iterate iter)
178 {
179 if (!iter || iter->magic != DEV_ITERATE_MAGIC)
180 return;
181 iter->magic = 0;
182 free(iter);
183 }
184
185 #ifdef TEST_PROGRAM
186 #ifdef HAVE_GETOPT_H
187 #include <getopt.h>
188 #else
189 extern char *optarg;
190 extern int optind;
191 #endif
192
193 void usage(char *prog)
194 {
195 fprintf(stderr, "Usage: %s [-f blkid_file] [-m debug_mask]\n", prog);
196 fprintf(stderr, "\tList all devices and exit\n");
197 exit(1);
198 }
199
200 int main(int argc, char **argv)
201 {
202 blkid_dev_iterate iter;
203 blkid_cache cache = NULL;
204 blkid_dev dev;
205 int c, ret;
206 char *tmp;
207 char *file = NULL;
208 char *search_type = NULL;
209 char *search_value = NULL;
210
211 while ((c = getopt (argc, argv, "m:f:")) != EOF)
212 switch (c) {
213 case 'f':
214 file = optarg;
215 break;
216 case 'm':
217 blkid_debug_mask = strtoul (optarg, &tmp, 0);
218 if (*tmp) {
219 fprintf(stderr, "Invalid debug mask: %s\n",
220 optarg);
221 exit(1);
222 }
223 break;
224 case '?':
225 usage(argv[0]);
226 }
227 if (argc >= optind+2) {
228 search_type = argv[optind];
229 search_value = argv[optind+1];
230 optind += 2;
231 }
232 if (argc != optind)
233 usage(argv[0]);
234
235 if ((ret = blkid_get_cache(&cache, file)) != 0) {
236 fprintf(stderr, "%s: error creating cache (%d)\n",
237 argv[0], ret);
238 exit(1);
239 }
240
241 iter = blkid_dev_iterate_begin(cache);
242 if (search_type)
243 blkid_dev_set_search(iter, search_type, search_value);
244 while (blkid_dev_next(iter, &dev) == 0) {
245 printf("Device: %s\n", blkid_dev_devname(dev));
246 }
247 blkid_dev_iterate_end(iter);
248
249
250 blkid_put_cache(cache);
251 return (0);
252 }
253 #endif