2 * cache.c - allocation/initialization/free routines for cache
4 * Copyright (C) 2001 Andreas Dilger
5 * Copyright (C) 2003 Theodore Ts'o
8 * This file may be redistributed under the terms of the
9 * GNU Lesser General Public License.
16 int blkid_debug_mask = 0;
18 int blkid_get_cache(blkid_cache *ret_cache, const char *filename)
22 #ifdef CONFIG_BLKID_DEBUG
23 if (!(blkid_debug_mask & DEBUG_INIT)) {
24 char *dstr = getenv("BLKID_DEBUG");
27 blkid_debug_mask = strtoul(dstr, 0, 0);
28 blkid_debug_mask |= DEBUG_INIT;
32 DBG(DEBUG_CACHE, printf("creating blkid cache (using %s)\n",
33 filename ? filename : "default cache"));
35 if (!(cache = (blkid_cache) calloc(1, sizeof(struct blkid_struct_cache))))
36 return -BLKID_ERR_MEM;
38 INIT_LIST_HEAD(&cache->bic_devs);
39 INIT_LIST_HEAD(&cache->bic_tags);
41 if (!filename || !strlen(filename))
42 filename = BLKID_CACHE_FILE;
43 cache->bic_filename = blkid_strdup(filename);
45 blkid_read_cache(cache);
51 void blkid_put_cache(blkid_cache cache)
56 (void) blkid_flush_cache(cache);
58 DBG(DEBUG_CACHE, printf("freeing cache struct\n"));
60 /* DEB_DUMP_CACHE(cache); */
62 while (!list_empty(&cache->bic_devs)) {
63 blkid_dev dev = list_entry(cache->bic_devs.next,
64 struct blkid_struct_dev,
69 while (!list_empty(&cache->bic_tags)) {
70 blkid_tag tag = list_entry(cache->bic_tags.next,
71 struct blkid_struct_tag,
74 while (!list_empty(&tag->bit_names)) {
75 blkid_tag bad = list_entry(tag->bit_names.next,
76 struct blkid_struct_tag,
79 DBG(DEBUG_CACHE, printf("warning: unfreed tag %s=%s\n",
80 bad->bit_name, bad->bit_val));
85 if (cache->bic_filename)
86 free(cache->bic_filename);
92 int main(int argc, char** argv)
94 blkid_cache cache = NULL;
97 blkid_debug_mask = DEBUG_ALL;
99 fprintf(stderr, "Usage: %s [filename] \n", argv[0]);
103 if ((ret = blkid_get_cache(&cache, argv[1])) < 0) {
104 fprintf(stderr, "error %d parsing cache file %s\n", ret,
105 argv[1] ? argv[1] : BLKID_CACHE_FILE);
108 if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
109 fprintf(stderr, "%s: error creating cache (%d)\n",
113 if ((ret = blkid_probe_all(cache) < 0))
114 fprintf(stderr, "error probing devices\n");
116 blkid_put_cache(cache);