cache.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. if (!(cache = (blkid_cache) calloc(1, sizeof(struct blkid_struct_cache))))
  32. return -BLKID_ERR_MEM;
  33. INIT_LIST_HEAD(&cache->bic_devs);
  34. INIT_LIST_HEAD(&cache->bic_tags);
  35. if (filename && !strlen(filename))
  36. filename = 0;
  37. if (!filename && (getuid() == geteuid()))
  38. filename = getenv("BLKID_FILE");
  39. if (!filename)
  40. filename = BLKID_CACHE_FILE;
  41. cache->bic_filename = blkid_strdup(filename);
  42. blkid_read_cache(cache);
  43. *ret_cache = cache;
  44. return 0;
  45. }
  46. void blkid_put_cache(blkid_cache cache)
  47. {
  48. if (!cache)
  49. return;
  50. (void) blkid_flush_cache(cache);
  51. DBG(DEBUG_CACHE, printf("freeing cache struct\n"));
  52. /* DBG(DEBUG_CACHE, blkid_debug_dump_cache(cache)); */
  53. while (!list_empty(&cache->bic_devs)) {
  54. blkid_dev dev = list_entry(cache->bic_devs.next,
  55. struct blkid_struct_dev,
  56. bid_devs);
  57. blkid_free_dev(dev);
  58. }
  59. while (!list_empty(&cache->bic_tags)) {
  60. blkid_tag tag = list_entry(cache->bic_tags.next,
  61. struct blkid_struct_tag,
  62. bit_tags);
  63. while (!list_empty(&tag->bit_names)) {
  64. blkid_tag bad = list_entry(tag->bit_names.next,
  65. struct blkid_struct_tag,
  66. bit_names);
  67. DBG(DEBUG_CACHE, printf("warning: unfreed tag %s=%s\n",
  68. bad->bit_name, bad->bit_val));
  69. blkid_free_tag(bad);
  70. }
  71. blkid_free_tag(tag);
  72. }
  73. free(cache->bic_filename);
  74. free(cache);
  75. }
  76. #ifdef TEST_PROGRAM
  77. int main(int argc, char** argv)
  78. {
  79. blkid_cache cache = NULL;
  80. int ret;
  81. blkid_debug_mask = DEBUG_ALL;
  82. if ((argc > 2)) {
  83. fprintf(stderr, "Usage: %s [filename]\n", argv[0]);
  84. exit(1);
  85. }
  86. if ((ret = blkid_get_cache(&cache, argv[1])) < 0) {
  87. fprintf(stderr, "error %d parsing cache file %s\n", ret,
  88. argv[1] ? argv[1] : BLKID_CACHE_FILE);
  89. exit(1);
  90. }
  91. if ((ret = blkid_get_cache(&cache, bb_dev_null)) != 0) {
  92. fprintf(stderr, "%s: error creating cache (%d)\n",
  93. argv[0], ret);
  94. exit(1);
  95. }
  96. if ((ret = blkid_probe_all(cache) < 0))
  97. fprintf(stderr, "error probing devices\n");
  98. blkid_put_cache(cache);
  99. return ret;
  100. }
  101. #endif