3
0

dev.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. #include <stdlib.h>
  14. #include <string.h>
  15. #include "blkidP.h"
  16. blkid_dev blkid_new_dev(void)
  17. {
  18. blkid_dev dev;
  19. dev = xzalloc(sizeof(struct blkid_struct_dev));
  20. INIT_LIST_HEAD(&dev->bid_devs);
  21. INIT_LIST_HEAD(&dev->bid_tags);
  22. return dev;
  23. }
  24. void blkid_free_dev(blkid_dev dev)
  25. {
  26. if (!dev)
  27. return;
  28. DBG(DEBUG_DEV,
  29. printf(" freeing dev %s (%s)\n", dev->bid_name, dev->bid_type));
  30. DBG(DEBUG_DEV, blkid_debug_dump_dev(dev));
  31. list_del(&dev->bid_devs);
  32. while (!list_empty(&dev->bid_tags)) {
  33. blkid_tag tag = list_entry(dev->bid_tags.next,
  34. struct blkid_struct_tag,
  35. bit_tags);
  36. blkid_free_tag(tag);
  37. }
  38. if (dev->bid_name)
  39. free(dev->bid_name);
  40. free(dev);
  41. }
  42. /*
  43. * Given a blkid device, return its name
  44. */
  45. const char *blkid_dev_devname(blkid_dev dev)
  46. {
  47. return dev->bid_name;
  48. }
  49. #ifdef CONFIG_BLKID_DEBUG
  50. void blkid_debug_dump_dev(blkid_dev dev)
  51. {
  52. struct list_head *p;
  53. if (!dev) {
  54. printf(" dev: NULL\n");
  55. return;
  56. }
  57. printf(" dev: name = %s\n", dev->bid_name);
  58. printf(" dev: DEVNO=\"0x%0llx\"\n", dev->bid_devno);
  59. printf(" dev: TIME=\"%lu\"\n", dev->bid_time);
  60. printf(" dev: PRI=\"%d\"\n", dev->bid_pri);
  61. printf(" dev: flags = 0x%08X\n", dev->bid_flags);
  62. list_for_each(p, &dev->bid_tags) {
  63. blkid_tag tag = list_entry(p, struct blkid_struct_tag, bit_tags);
  64. if (tag)
  65. printf(" tag: %s=\"%s\"\n", tag->bit_name,
  66. tag->bit_val);
  67. else
  68. printf(" tag: NULL\n");
  69. }
  70. bb_putchar('\n');
  71. }
  72. #endif
  73. /*
  74. * dev iteration routines for the public libblkid interface.
  75. *
  76. * These routines do not expose the list.h implementation, which are a
  77. * contamination of the namespace, and which force us to reveal far, far
  78. * too much of our internal implemenation. I'm not convinced I want
  79. * to keep list.h in the long term, anyway. It's fine for kernel
  80. * programming, but performance is not the #1 priority for this
  81. * library, and I really don't like the tradeoff of type-safety for
  82. * performance for this application. [tytso:20030125.2007EST]
  83. */
  84. /*
  85. * This series of functions iterate over all devices in a blkid cache
  86. */
  87. #define DEV_ITERATE_MAGIC 0x01a5284c
  88. struct blkid_struct_dev_iterate {
  89. int magic;
  90. blkid_cache cache;
  91. struct list_head *p;
  92. };
  93. blkid_dev_iterate blkid_dev_iterate_begin(blkid_cache cache)
  94. {
  95. blkid_dev_iterate iter;
  96. iter = xmalloc(sizeof(struct blkid_struct_dev_iterate));
  97. iter->magic = DEV_ITERATE_MAGIC;
  98. iter->cache = cache;
  99. iter->p = cache->bic_devs.next;
  100. return iter;
  101. }
  102. /*
  103. * Return 0 on success, -1 on error
  104. */
  105. extern int blkid_dev_next(blkid_dev_iterate iter,
  106. blkid_dev *dev)
  107. {
  108. *dev = 0;
  109. if (!iter || iter->magic != DEV_ITERATE_MAGIC ||
  110. iter->p == &iter->cache->bic_devs)
  111. return -1;
  112. *dev = list_entry(iter->p, struct blkid_struct_dev, bid_devs);
  113. iter->p = iter->p->next;
  114. return 0;
  115. }
  116. void blkid_dev_iterate_end(blkid_dev_iterate iter)
  117. {
  118. if (!iter || iter->magic != DEV_ITERATE_MAGIC)
  119. return;
  120. iter->magic = 0;
  121. free(iter);
  122. }
  123. #ifdef TEST_PROGRAM
  124. #ifdef HAVE_GETOPT_H
  125. #include <getopt.h>
  126. #else
  127. extern char *optarg;
  128. extern int optind;
  129. #endif
  130. void usage(char *prog)
  131. {
  132. fprintf(stderr, "Usage: %s [-f blkid_file] [-m debug_mask]\n", prog);
  133. fprintf(stderr, "\tList all devices and exit\n", prog);
  134. exit(1);
  135. }
  136. int main(int argc, char **argv)
  137. {
  138. blkid_dev_iterate iter;
  139. blkid_cache cache = NULL;
  140. blkid_dev dev;
  141. int c, ret;
  142. char *tmp;
  143. char *file = NULL;
  144. char *search_type = NULL;
  145. char *search_value = NULL;
  146. while ((c = getopt (argc, argv, "m:f:")) != EOF)
  147. switch (c) {
  148. case 'f':
  149. file = optarg;
  150. break;
  151. case 'm':
  152. blkid_debug_mask = strtoul (optarg, &tmp, 0);
  153. if (*tmp) {
  154. fprintf(stderr, "Invalid debug mask: %d\n",
  155. optarg);
  156. exit(1);
  157. }
  158. break;
  159. case '?':
  160. usage(argv[0]);
  161. }
  162. if (argc >= optind+2) {
  163. search_type = argv[optind];
  164. search_value = argv[optind+1];
  165. optind += 2;
  166. }
  167. if (argc != optind)
  168. usage(argv[0]);
  169. if ((ret = blkid_get_cache(&cache, file)) != 0) {
  170. fprintf(stderr, "%s: error creating cache (%d)\n",
  171. argv[0], ret);
  172. exit(1);
  173. }
  174. iter = blkid_dev_iterate_begin(cache);
  175. if (search_type)
  176. blkid_dev_set_search(iter, search_type, search_value);
  177. while (blkid_dev_next(iter, &dev) == 0) {
  178. printf("Device: %s\n", blkid_dev_devname(dev));
  179. }
  180. blkid_dev_iterate_end(iter);
  181. blkid_put_cache(cache);
  182. return 0;
  183. }
  184. #endif