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