Whamcloud - gitweb
bfa104e697f928b17a044f5a92e7dae03f61e449
[tools/e2fsprogs.git] / lib / blkid / cache.c
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
13 #include <stdlib.h>
14 #include "blkidP.h"
15
16 int blkid_debug_mask = 0;
17
18 int blkid_get_cache(blkid_cache *ret_cache, const char *filename)
19 {
20         blkid_cache cache;
21
22 #ifdef CONFIG_BLKID_DEBUG
23         if (!(blkid_debug_mask & DEBUG_INIT)) {
24                 char *dstr = getenv("BLKID_DEBUG");
25
26                 if (dstr)
27                         blkid_debug_mask = strtoul(dstr, 0, 0);
28                 blkid_debug_mask |= DEBUG_INIT;
29         }
30 #endif
31
32         DBG(DEBUG_CACHE, printf("creating blkid cache (using %s)\n",
33                                 filename ? filename : "default cache"));
34
35         if (!(cache = (blkid_cache) calloc(1, sizeof(struct blkid_struct_cache))))
36                 return -BLKID_ERR_MEM;
37
38         INIT_LIST_HEAD(&cache->bic_devs);
39         INIT_LIST_HEAD(&cache->bic_tags);
40
41         if (!filename || !strlen(filename))
42                 filename = BLKID_CACHE_FILE;
43         cache->bic_filename = blkid_strdup(filename);
44         
45         blkid_read_cache(cache);
46         
47         *ret_cache = cache;
48         return 0;
49 }
50
51 void blkid_put_cache(blkid_cache cache)
52 {
53         if (!cache)
54                 return;
55
56         (void) blkid_flush_cache(cache);
57
58         DBG(DEBUG_CACHE, printf("freeing cache struct\n"));
59         
60         /* DEB_DUMP_CACHE(cache); */
61
62         while (!list_empty(&cache->bic_devs)) {
63                 blkid_dev dev = list_entry(cache->bic_devs.next,
64                                            struct blkid_struct_dev,
65                                             bid_devs);
66                 blkid_free_dev(dev);
67         }
68
69         while (!list_empty(&cache->bic_tags)) {
70                 blkid_tag tag = list_entry(cache->bic_tags.next,
71                                            struct blkid_struct_tag,
72                                            bit_tags);
73
74                 while (!list_empty(&tag->bit_names)) {
75                         blkid_tag bad = list_entry(tag->bit_names.next,
76                                                    struct blkid_struct_tag, 
77                                                    bit_names);
78
79                         DBG(DEBUG_CACHE, printf("warning: unfreed tag %s=%s\n",
80                                                 bad->bit_name, bad->bit_val));
81                         blkid_free_tag(bad);
82                 }
83                 blkid_free_tag(tag);
84         }
85         if (cache->bic_filename)
86                 free(cache->bic_filename);
87         
88         free(cache);
89 }
90
91 #ifdef TEST_PROGRAM
92 int main(int argc, char** argv)
93 {
94         blkid_cache cache = NULL;
95         int ret;
96
97         blkid_debug_mask = DEBUG_ALL;
98         if ((argc > 2)) {
99                 fprintf(stderr, "Usage: %s [filename] \n", argv[0]);
100                 exit(1);
101         }
102
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);
106                 exit(1);
107         }
108         if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
109                 fprintf(stderr, "%s: error creating cache (%d)\n",
110                         argv[0], ret);
111                 exit(1);
112         }
113         if ((ret = blkid_probe_all(cache) < 0))
114                 fprintf(stderr, "error probing devices\n");
115
116         blkid_put_cache(cache);
117
118         return ret;
119 }
120 #endif