1 /* vi: set sw=4 ts=4: */
3 * cache.c - allocation/initialization/free routines for cache
5 * Copyright (C) 2001 Andreas Dilger
6 * Copyright (C) 2003 Theodore Ts'o
9 * This file may be redistributed under the terms of the
10 * GNU Lesser General Public License.
19 int blkid_debug_mask = 0;
21 int blkid_get_cache(blkid_cache *ret_cache, const char *filename)
25 #ifdef CONFIG_BLKID_DEBUG
26 if (!(blkid_debug_mask & DEBUG_INIT)) {
27 char *dstr = getenv("BLKID_DEBUG");
30 blkid_debug_mask = strtoul(dstr, 0, 0);
31 blkid_debug_mask |= DEBUG_INIT;
35 DBG(DEBUG_CACHE, printf("creating blkid cache (using %s)\n",
36 filename ? filename : "default cache"));
38 if (!(cache = (blkid_cache) calloc(1, sizeof(struct blkid_struct_cache))))
39 return -BLKID_ERR_MEM;
41 INIT_LIST_HEAD(&cache->bic_devs);
42 INIT_LIST_HEAD(&cache->bic_tags);
44 if (filename && !strlen(filename))
46 if (!filename && (getuid() == geteuid()))
47 filename = getenv("BLKID_FILE");
49 filename = BLKID_CACHE_FILE;
50 cache->bic_filename = blkid_strdup(filename);
52 blkid_read_cache(cache);
58 void blkid_put_cache(blkid_cache cache)
63 (void) blkid_flush_cache(cache);
65 DBG(DEBUG_CACHE, printf("freeing cache struct\n"));
67 /* DBG(DEBUG_CACHE, blkid_debug_dump_cache(cache)); */
69 while (!list_empty(&cache->bic_devs)) {
70 blkid_dev dev = list_entry(cache->bic_devs.next,
71 struct blkid_struct_dev,
76 while (!list_empty(&cache->bic_tags)) {
77 blkid_tag tag = list_entry(cache->bic_tags.next,
78 struct blkid_struct_tag,
81 while (!list_empty(&tag->bit_names)) {
82 blkid_tag bad = list_entry(tag->bit_names.next,
83 struct blkid_struct_tag,
86 DBG(DEBUG_CACHE, printf("warning: unfreed tag %s=%s\n",
87 bad->bit_name, bad->bit_val));
92 free(cache->bic_filename);
98 int main(int argc, char** argv)
100 blkid_cache cache = NULL;
103 blkid_debug_mask = DEBUG_ALL;
105 fprintf(stderr, "Usage: %s [filename]\n", argv[0]);
109 if ((ret = blkid_get_cache(&cache, argv[1])) < 0) {
110 fprintf(stderr, "error %d parsing cache file %s\n", ret,
111 argv[1] ? argv[1] : BLKID_CACHE_FILE);
114 if ((ret = blkid_get_cache(&cache, bb_dev_null)) != 0) {
115 fprintf(stderr, "%s: error creating cache (%d)\n",
119 if ((ret = blkid_probe_all(cache) < 0))
120 fprintf(stderr, "error probing devices\n");
122 blkid_put_cache(cache);