cache.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * cache.c - allocation/initialization/free routines for cache
  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 <unistd.h>
  15. #include "blkidP.h"
  16. int blkid_debug_mask = 0;
  17. int blkid_get_cache(blkid_cache *ret_cache, const char *filename)
  18. {
  19. blkid_cache cache;
  20. #ifdef CONFIG_BLKID_DEBUG
  21. if (!(blkid_debug_mask & DEBUG_INIT)) {
  22. char *dstr = getenv("BLKID_DEBUG");
  23. if (dstr)
  24. blkid_debug_mask = strtoul(dstr, 0, 0);
  25. blkid_debug_mask |= DEBUG_INIT;
  26. }
  27. #endif
  28. DBG(DEBUG_CACHE, printf("creating blkid cache (using %s)\n",
  29. filename ? filename : "default cache"));
  30. if (!(cache = (blkid_cache) calloc(1, sizeof(struct blkid_struct_cache))))
  31. return -BLKID_ERR_MEM;
  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. /* DEB_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. if (cache->bic_filename)
  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, "/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