3
0

dev.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #include <stdlib.h>
  13. #include <string.h>
  14. #include "blkidP.h"
  15. blkid_dev blkid_new_dev(void)
  16. {
  17. blkid_dev dev;
  18. if (!(dev = (blkid_dev) calloc(1, sizeof(struct blkid_struct_dev))))
  19. return NULL;
  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. DEB_DUMP_DEV(DEBUG_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. extern const char *blkid_dev_devname(blkid_dev dev)
  46. {
  47. return dev->bid_name;
  48. }
  49. /*
  50. * dev iteration routines for the public libblkid interface.
  51. *
  52. * These routines do not expose the list.h implementation, which are a
  53. * contamination of the namespace, and which force us to reveal far, far
  54. * too much of our internal implemenation. I'm not convinced I want
  55. * to keep list.h in the long term, anyway. It's fine for kernel
  56. * programming, but performance is not the #1 priority for this
  57. * library, and I really don't like the tradeoff of type-safety for
  58. * performance for this application. [tytso:20030125.2007EST]
  59. */
  60. /*
  61. * This series of functions iterate over all devices in a blkid cache
  62. */
  63. #define DEV_ITERATE_MAGIC 0x01a5284c
  64. struct blkid_struct_dev_iterate {
  65. int magic;
  66. blkid_cache cache;
  67. struct list_head *p;
  68. };
  69. extern blkid_dev_iterate blkid_dev_iterate_begin(blkid_cache cache)
  70. {
  71. blkid_dev_iterate iter;
  72. iter = xmalloc(sizeof(struct blkid_struct_dev_iterate));
  73. iter->magic = DEV_ITERATE_MAGIC;
  74. iter->cache = cache;
  75. iter->p = cache->bic_devs.next;
  76. return (iter);
  77. }
  78. /*
  79. * Return 0 on success, -1 on error
  80. */
  81. extern int blkid_dev_next(blkid_dev_iterate iter,
  82. blkid_dev *dev)
  83. {
  84. *dev = 0;
  85. if (!iter || iter->magic != DEV_ITERATE_MAGIC ||
  86. iter->p == &iter->cache->bic_devs)
  87. return -1;
  88. *dev = list_entry(iter->p, struct blkid_struct_dev, bid_devs);
  89. iter->p = iter->p->next;
  90. return 0;
  91. }
  92. extern void blkid_dev_iterate_end(blkid_dev_iterate iter)
  93. {
  94. if (!iter || iter->magic != DEV_ITERATE_MAGIC)
  95. return;
  96. iter->magic = 0;
  97. free(iter);
  98. }