Changes for kernel and Busybox
[tomato.git] / release / src / router / busybox / e2fsprogs / blkid / cache.c
blobd1d29146b3505a9e0d92df1eb758077e61baa8cf
1 /* vi: set sw=4 ts=4: */
2 /*
3 * cache.c - allocation/initialization/free routines for cache
5 * Copyright (C) 2001 Andreas Dilger
6 * Copyright (C) 2003 Theodore Ts'o
8 * %Begin-Header%
9 * This file may be redistributed under the terms of the
10 * GNU Lesser General Public License.
11 * %End-Header%
14 #include <stdlib.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include "blkidP.h"
19 int blkid_debug_mask = 0;
21 int blkid_get_cache(blkid_cache *ret_cache, const char *filename)
23 blkid_cache cache;
25 #ifdef CONFIG_BLKID_DEBUG
26 if (!(blkid_debug_mask & DEBUG_INIT)) {
27 char *dstr = getenv("BLKID_DEBUG");
29 if (dstr)
30 blkid_debug_mask = strtoul(dstr, 0, 0);
31 blkid_debug_mask |= DEBUG_INIT;
33 #endif
35 DBG(DEBUG_CACHE, printf("creating blkid cache (using %s)\n",
36 filename ? filename : "default cache"));
38 cache = xzalloc(sizeof(struct blkid_struct_cache));
40 INIT_LIST_HEAD(&cache->bic_devs);
41 INIT_LIST_HEAD(&cache->bic_tags);
43 if (filename && !strlen(filename))
44 filename = 0;
45 if (!filename && (getuid() == geteuid()))
46 filename = getenv("BLKID_FILE");
47 if (!filename)
48 filename = BLKID_CACHE_FILE;
49 cache->bic_filename = blkid_strdup(filename);
51 blkid_read_cache(cache);
53 *ret_cache = cache;
54 return 0;
57 void blkid_put_cache(blkid_cache cache)
59 if (!cache)
60 return;
62 (void) blkid_flush_cache(cache);
64 DBG(DEBUG_CACHE, printf("freeing cache struct\n"));
66 /* DBG(DEBUG_CACHE, blkid_debug_dump_cache(cache)); */
68 while (!list_empty(&cache->bic_devs)) {
69 blkid_dev dev = list_entry(cache->bic_devs.next,
70 struct blkid_struct_dev,
71 bid_devs);
72 blkid_free_dev(dev);
75 while (!list_empty(&cache->bic_tags)) {
76 blkid_tag tag = list_entry(cache->bic_tags.next,
77 struct blkid_struct_tag,
78 bit_tags);
80 while (!list_empty(&tag->bit_names)) {
81 blkid_tag bad = list_entry(tag->bit_names.next,
82 struct blkid_struct_tag,
83 bit_names);
85 DBG(DEBUG_CACHE, printf("warning: unfreed tag %s=%s\n",
86 bad->bit_name, bad->bit_val));
87 blkid_free_tag(bad);
89 blkid_free_tag(tag);
91 free(cache->bic_filename);
93 free(cache);
96 #ifdef TEST_PROGRAM
97 int main(int argc, char** argv)
99 blkid_cache cache = NULL;
100 int ret;
102 blkid_debug_mask = DEBUG_ALL;
103 if ((argc > 2)) {
104 fprintf(stderr, "Usage: %s [filename]\n", argv[0]);
105 exit(1);
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);
113 if ((ret = blkid_get_cache(&cache, bb_dev_null)) != 0) {
114 fprintf(stderr, "%s: error creating cache (%d)\n",
115 argv[0], ret);
116 exit(1);
118 if ((ret = blkid_probe_all(cache) < 0))
119 fprintf(stderr, "error probing devices\n");
121 blkid_put_cache(cache);
123 return ret;
125 #endif