]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - libblkid/src/dev.c
2 * dev.c - allocation/initialization/free routines for dev
4 * Copyright (C) 2001 Andreas Dilger
5 * Copyright (C) 2003 Theodore Ts'o
8 * This file may be redistributed under the terms of the
9 * GNU Lesser General Public License.
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.
25 * @title: Search and iterate
26 * @short_description: search devices and iterate over devices in the cache.
28 * Note that high-level probing API provides information about superblocks
29 * (filesystems/raids) only. For partitions and topology is necessary to use
33 blkid_dev
blkid_new_dev(void)
37 if (!(dev
= calloc(1, sizeof(struct blkid_struct_dev
))))
40 DBG(DEV
, ul_debugobj(dev
, "alloc"));
41 INIT_LIST_HEAD(&dev
->bid_devs
);
42 INIT_LIST_HEAD(&dev
->bid_tags
);
47 void blkid_free_dev(blkid_dev dev
)
52 DBG(DEV
, ul_debugobj(dev
, "freeing (%s)", dev
->bid_name
));
54 list_del(&dev
->bid_devs
);
55 while (!list_empty(&dev
->bid_tags
)) {
56 blkid_tag tag
= list_entry(dev
->bid_tags
.next
,
57 struct blkid_struct_tag
,
67 * Given a blkid device, return its name. The function returns the name
68 * previously used for blkid_get_dev(). This name does not have to be canonical
69 * (real path) name, but for example symlink.
71 const char *blkid_dev_devname(blkid_dev dev
)
76 return dev
->bid_xname
;
80 void blkid_debug_dump_dev(blkid_dev dev
)
85 printf(" dev: NULL\n");
89 fprintf(stderr
, " dev: name = %s\n", dev
->bid_name
);
90 fprintf(stderr
, " dev: DEVNO=\"0x%0llx\"\n", (long long)dev
->bid_devno
);
91 fprintf(stderr
, " dev: TIME=\"%ld.%ld\"\n", (long)dev
->bid_time
, (long)dev
->bid_utime
);
92 fprintf(stderr
, " dev: PRI=\"%d\"\n", dev
->bid_pri
);
93 fprintf(stderr
, " dev: flags = 0x%08X\n", dev
->bid_flags
);
95 list_for_each(p
, &dev
->bid_tags
) {
96 blkid_tag tag
= list_entry(p
, struct blkid_struct_tag
, bit_tags
);
98 fprintf(stderr
, " tag: %s=\"%s\"\n", tag
->bit_name
,
101 fprintf(stderr
, " tag: NULL\n");
106 * dev iteration routines for the public libblkid interface.
108 * These routines do not expose the list.h implementation, which are a
109 * contamination of the namespace, and which force us to reveal far, far
110 * too much of our internal implementation. I'm not convinced I want
111 * to keep list.h in the long term, anyway. It's fine for kernel
112 * programming, but performance is not the #1 priority for this
113 * library, and I really don't like the trade-off of type-safety for
114 * performance for this application. [tytso:20030125.2007EST]
118 * This series of functions iterate over all devices in a blkid cache
120 #define DEV_ITERATE_MAGIC 0x01a5284c
122 struct blkid_struct_dev_iterate
{
130 blkid_dev_iterate
blkid_dev_iterate_begin(blkid_cache cache
)
132 blkid_dev_iterate iter
;
139 iter
= malloc(sizeof(struct blkid_struct_dev_iterate
));
141 iter
->magic
= DEV_ITERATE_MAGIC
;
143 iter
->p
= cache
->bic_devs
.next
;
144 iter
->search_type
= NULL
;
145 iter
->search_value
= NULL
;
150 int blkid_dev_set_search(blkid_dev_iterate iter
,
151 const char *search_type
, const char *search_value
)
153 char *new_type
, *new_value
;
155 if (!iter
|| iter
->magic
!= DEV_ITERATE_MAGIC
|| !search_type
||
158 new_type
= malloc(strlen(search_type
)+1);
159 new_value
= malloc(strlen(search_value
)+1);
160 if (!new_type
|| !new_value
) {
165 strcpy(new_type
, search_type
);
166 strcpy(new_value
, search_value
);
167 free(iter
->search_type
);
168 free(iter
->search_value
);
169 iter
->search_type
= new_type
;
170 iter
->search_value
= new_value
;
175 * Return 0 on success, -1 on error
177 int blkid_dev_next(blkid_dev_iterate iter
,
182 if (!ret_dev
|| !iter
|| iter
->magic
!= DEV_ITERATE_MAGIC
)
185 while (iter
->p
!= &iter
->cache
->bic_devs
) {
186 dev
= list_entry(iter
->p
, struct blkid_struct_dev
, bid_devs
);
187 iter
->p
= iter
->p
->next
;
188 if (iter
->search_type
&&
189 !blkid_dev_has_tag(dev
, iter
->search_type
,
198 void blkid_dev_iterate_end(blkid_dev_iterate iter
)
200 if (!iter
|| iter
->magic
!= DEV_ITERATE_MAGIC
)
203 free(iter
->search_type
);
204 free(iter
->search_value
);
216 static void __attribute__((__noreturn__
)) usage(char *prog
)
218 fprintf(stderr
, "Usage: %s [-f blkid_file] [-m debug_mask]\n", prog
);
219 fprintf(stderr
, "\tList all devices and exit\n");
223 int main(int argc
, char **argv
)
225 blkid_dev_iterate iter
;
226 blkid_cache cache
= NULL
;
231 char *search_type
= NULL
;
232 char *search_value
= NULL
;
234 while ((c
= getopt (argc
, argv
, "m:f:")) != EOF
)
241 int mask
= strtoul (optarg
, &tmp
, 0);
243 fprintf(stderr
, "Invalid debug mask: %s\n",
247 blkid_init_debug(mask
);
253 if (argc
>= optind
+2) {
254 search_type
= argv
[optind
];
255 search_value
= argv
[optind
+1];
261 if ((ret
= blkid_get_cache(&cache
, file
)) != 0) {
262 fprintf(stderr
, "%s: error creating cache (%d)\n",
267 iter
= blkid_dev_iterate_begin(cache
);
269 blkid_dev_set_search(iter
, search_type
, search_value
);
270 while (blkid_dev_next(iter
, &dev
) == 0) {
271 printf("Device: %s\n", blkid_dev_devname(dev
));
273 blkid_dev_iterate_end(iter
);
276 blkid_put_cache(cache
);