cache.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * cache.c - allocation/initialization/free routines for cache
  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 <unistd.h>
  16. #include "blkidP.h"
  17. int blkid_debug_mask = 0;
  18. int blkid_get_cache(blkid_cache *ret_cache, const char *filename)
  19. {
  20. blkid_cache cache;
  21. #ifdef CONFIG_BLKID_DEBUG
  22. if (!(blkid_debug_mask & DEBUG_INIT)) {
  23. char *dstr = getenv("BLKID_DEBUG");
  24. if (dstr)
  25. blkid_debug_mask = strtoul(dstr, 0, 0);
  26. blkid_debug_mask |= DEBUG_INIT;
  27. }
  28. #endif
  29. DBG(DEBUG_CACHE, printf("creating blkid cache (using %s)\n",
  30. filename ? filename : "default cache"));
  31. cache = xzalloc(sizeof(struct blkid_struct_cache));
  32. INIT_LIST_HEAD(&cache->bic_devs);
  33. INIT_LIST_HEAD(&cache->bic_tags);
  34. if (filename && !strlen(filename))
  35. filename = 0;
  36. if (!filename && (getuid() == geteuid()))
  37. filename = getenv("BLKID_FILE");
  38. if (!filename)
  39. filename = BLKID_CACHE_FILE;
  40. cache->bic_filename = blkid_strdup(filename);
  41. blkid_read_cache(cache);
  42. *ret_cache = cache;
  43. return 0;
  44. }
  45. void blkid_put_cache(blkid_cache cache)
  46. {
  47. if (!cache)
  48. return;
  49. (void) blkid_flush_cache(cache);
  50. DBG(DEBUG_CACHE, printf("freeing cache struct\n"));
  51. /* DBG(DEBUG_CACHE, blkid_debug_dump_cache(cache)); */
  52. while (!list_empty(&cache->bic_devs)) {
  53. blkid_dev dev = list_entry(cache->bic_devs.next,
  54. struct blkid_struct_dev,
  55. bid_devs);
  56. blkid_free_dev(dev);
  57. }
  58. while (!list_empty(&cache->bic_tags)) {
  59. blkid_tag tag = list_entry(cache->bic_tags.next,
  60. struct blkid_struct_tag,
  61. bit_tags);
  62. while (!list_empty(&tag->bit_names)) {
  63. blkid_tag bad = list_entry(tag->bit_names.next,
  64. struct blkid_struct_tag,
  65. bit_names);
  66. DBG(DEBUG_CACHE, printf("warning: unfreed tag %s=%s\n",
  67. bad->bit_name, bad->bit_val));
  68. blkid_free_tag(bad);
  69. }
  70. blkid_free_tag(tag);
  71. }
  72. free(cache->bic_filename);
  73. free(cache);
  74. }
  75. #ifdef TEST_PROGRAM
  76. int main(int argc, char** argv)
  77. {
  78. blkid_cache cache = NULL;
  79. int ret;
  80. blkid_debug_mask = DEBUG_ALL;
  81. if ((argc > 2)) {
  82. fprintf(stderr, "Usage: %s [filename]\n", argv[0]);
  83. exit(1);
  84. }
  85. if ((ret = blkid_get_cache(&cache, argv[1])) < 0) {
  86. fprintf(stderr, "error %d parsing cache file %s\n", ret,
  87. argv[1] ? argv[1] : BLKID_CACHE_FILE);
  88. exit(1);
  89. }
  90. if ((ret = blkid_get_cache(&cache, bb_dev_null)) != 0) {
  91. fprintf(stderr, "%s: error creating cache (%d)\n",
  92. argv[0], ret);
  93. exit(1);
  94. }
  95. if ((ret = blkid_probe_all(cache) < 0))
  96. fprintf(stderr, "error probing devices\n");
  97. blkid_put_cache(cache);
  98. return ret;
  99. }
  100. #endif