blob: 62dfc24d3499a0de7078b46d8489cc47bc29ac4f [file] [log] [blame]
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001/*
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/*
19 * NOTE: reference manual is not structured as code. The following section is a generic
20 * section for all high-level cache search+iterate routines.
21 */
22
23/**
24 * SECTION:search
25 * @title: Search and iterate
26 * @short_description: search devices and iterate over devices in the cache.
27 *
28 * Note that high-level probing API provides information about superblocks
29 * (filesystems/raids) only. For partitions and topology is necessary to use
30 * the low-level API.
31 */
32
33blkid_dev blkid_new_dev(void)
34{
35 blkid_dev dev;
36
37 if (!(dev = (blkid_dev) calloc(1, sizeof(struct blkid_struct_dev))))
38 return NULL;
39
40 INIT_LIST_HEAD(&dev->bid_devs);
41 INIT_LIST_HEAD(&dev->bid_tags);
42
43 return dev;
44}
45
46void blkid_free_dev(blkid_dev dev)
47{
48 if (!dev)
49 return;
50
51 DBG(DEBUG_DEV,
52 printf(" freeing dev %s (%s)\n", dev->bid_name, dev->bid_type ?
53 dev->bid_type : "(null)"));
54 DBG(DEBUG_DEV, blkid_debug_dump_dev(dev));
55
56 list_del(&dev->bid_devs);
57 while (!list_empty(&dev->bid_tags)) {
58 blkid_tag tag = list_entry(dev->bid_tags.next,
59 struct blkid_struct_tag,
60 bit_tags);
61 blkid_free_tag(tag);
62 }
63 free(dev->bid_name);
64 free(dev);
65}
66
67/*
68 * Given a blkid device, return its name
69 */
70extern const char *blkid_dev_devname(blkid_dev dev)
71{
72 return dev ? dev->bid_name : NULL;
73}
74
75#ifdef CONFIG_BLKID_DEBUG
76void blkid_debug_dump_dev(blkid_dev dev)
77{
78 struct list_head *p;
79
80 if (!dev) {
81 printf(" dev: NULL\n");
82 return;
83 }
84
85 printf(" dev: name = %s\n", dev->bid_name);
86 printf(" dev: DEVNO=\"0x%0llx\"\n", (long long)dev->bid_devno);
87 printf(" dev: TIME=\"%ld.%ld\"\n", (long)dev->bid_time, (long)dev->bid_utime);
88 printf(" dev: PRI=\"%d\"\n", dev->bid_pri);
89 printf(" dev: flags = 0x%08X\n", dev->bid_flags);
90
91 list_for_each(p, &dev->bid_tags) {
92 blkid_tag tag = list_entry(p, struct blkid_struct_tag, bit_tags);
93 if (tag)
94 printf(" tag: %s=\"%s\"\n", tag->bit_name,
95 tag->bit_val);
96 else
97 printf(" tag: NULL\n");
98 }
99 printf("\n");
100}
101#endif
102
103/*
104 * dev iteration routines for the public libblkid interface.
105 *
106 * These routines do not expose the list.h implementation, which are a
107 * contamination of the namespace, and which force us to reveal far, far
108 * too much of our internal implemenation. I'm not convinced I want
109 * to keep list.h in the long term, anyway. It's fine for kernel
110 * programming, but performance is not the #1 priority for this
111 * library, and I really don't like the tradeoff of type-safety for
112 * performance for this application. [tytso:20030125.2007EST]
113 */
114
115/*
116 * This series of functions iterate over all devices in a blkid cache
117 */
118#define DEV_ITERATE_MAGIC 0x01a5284c
119
120struct blkid_struct_dev_iterate {
121 int magic;
122 blkid_cache cache;
123 char *search_type;
124 char *search_value;
125 struct list_head *p;
126};
127
128extern blkid_dev_iterate blkid_dev_iterate_begin(blkid_cache cache)
129{
130 blkid_dev_iterate iter;
131
132 iter = malloc(sizeof(struct blkid_struct_dev_iterate));
133 if (iter) {
134 iter->magic = DEV_ITERATE_MAGIC;
135 iter->cache = cache;
136 iter->p = cache->bic_devs.next;
137 iter->search_type = 0;
138 iter->search_value = 0;
139 }
140 return iter;
141}
142
143extern int blkid_dev_set_search(blkid_dev_iterate iter,
144 char *search_type, char *search_value)
145{
146 char *new_type, *new_value;
147
148 if (!iter || iter->magic != DEV_ITERATE_MAGIC || !search_type ||
149 !search_value)
150 return -1;
151 new_type = malloc(strlen(search_type)+1);
152 new_value = malloc(strlen(search_value)+1);
153 if (!new_type || !new_value) {
154 free(new_type);
155 free(new_value);
156 return -1;
157 }
158 strcpy(new_type, search_type);
159 strcpy(new_value, search_value);
160 free(iter->search_type);
161 free(iter->search_value);
162 iter->search_type = new_type;
163 iter->search_value = new_value;
164 return 0;
165}
166
167/*
168 * Return 0 on success, -1 on error
169 */
170extern int blkid_dev_next(blkid_dev_iterate iter,
171 blkid_dev *ret_dev)
172{
173 blkid_dev dev;
174
175 if (!ret_dev || !iter || iter->magic != DEV_ITERATE_MAGIC)
176 return -1;
177 *ret_dev = 0;
178 while (iter->p != &iter->cache->bic_devs) {
179 dev = list_entry(iter->p, struct blkid_struct_dev, bid_devs);
180 iter->p = iter->p->next;
181 if (iter->search_type &&
182 !blkid_dev_has_tag(dev, iter->search_type,
183 iter->search_value))
184 continue;
185 *ret_dev = dev;
186 return 0;
187 }
188 return -1;
189}
190
191extern void blkid_dev_iterate_end(blkid_dev_iterate iter)
192{
193 if (!iter || iter->magic != DEV_ITERATE_MAGIC)
194 return;
195 iter->magic = 0;
196 free(iter->search_type);
197 free(iter->search_value);
198 free(iter);
199}
200
201#ifdef TEST_PROGRAM
202#ifdef HAVE_GETOPT_H
203#include <getopt.h>
204#else
205extern char *optarg;
206extern int optind;
207#endif
208
209void __attribute__((__noreturn__)) usage(char *prog)
210{
211 fprintf(stderr, "Usage: %s [-f blkid_file] [-m debug_mask]\n", prog);
212 fprintf(stderr, "\tList all devices and exit\n");
213 exit(1);
214}
215
216int main(int argc, char **argv)
217{
218 blkid_dev_iterate iter;
219 blkid_cache cache = NULL;
220 blkid_dev dev;
221 int c, ret;
222 char *tmp;
223 char *file = NULL;
224 char *search_type = NULL;
225 char *search_value = NULL;
226
227 while ((c = getopt (argc, argv, "m:f:")) != EOF)
228 switch (c) {
229 case 'f':
230 file = optarg;
231 break;
232 case 'm':
233 {
234 int mask = strtoul (optarg, &tmp, 0);
235 if (*tmp) {
236 fprintf(stderr, "Invalid debug mask: %s\n",
237 optarg);
238 exit(1);
239 }
240 blkid_init_debug(mask);
241 break;
242 }
243 case '?':
244 usage(argv[0]);
245 }
246 if (argc >= optind+2) {
247 search_type = argv[optind];
248 search_value = argv[optind+1];
249 optind += 2;
250 }
251 if (argc != optind)
252 usage(argv[0]);
253
254 if ((ret = blkid_get_cache(&cache, file)) != 0) {
255 fprintf(stderr, "%s: error creating cache (%d)\n",
256 argv[0], ret);
257 exit(1);
258 }
259
260 iter = blkid_dev_iterate_begin(cache);
261 if (search_type)
262 blkid_dev_set_search(iter, search_type, search_value);
263 while (blkid_dev_next(iter, &dev) == 0) {
264 printf("Device: %s\n", blkid_dev_devname(dev));
265 }
266 blkid_dev_iterate_end(iter);
267
268
269 blkid_put_cache(cache);
270 return (0);
271}
272#endif