3
0

dev.c 4.5 KB

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