blob: 9bae6fb67d9bfbd677a792987e9d673fc0d93cc4 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger38a33f92005-05-09 22:13:22 +00002/*
3 * cache.c - allocation/initialization/free routines for cache
4 *
5 * Copyright (C) 2001 Andreas Dilger
6 * Copyright (C) 2003 Theodore Ts'o
7 *
8 * %Begin-Header%
9 * This file may be redistributed under the terms of the
10 * GNU Lesser General Public License.
11 * %End-Header%
12 */
13
14#include <stdlib.h>
15#include <string.h>
16#include <unistd.h>
17#include "blkidP.h"
18
19int blkid_debug_mask = 0;
20
21int blkid_get_cache(blkid_cache *ret_cache, const char *filename)
22{
23 blkid_cache cache;
24
25#ifdef CONFIG_BLKID_DEBUG
26 if (!(blkid_debug_mask & DEBUG_INIT)) {
27 char *dstr = getenv("BLKID_DEBUG");
28
29 if (dstr)
30 blkid_debug_mask = strtoul(dstr, 0, 0);
31 blkid_debug_mask |= DEBUG_INIT;
32 }
33#endif
34
35 DBG(DEBUG_CACHE, printf("creating blkid cache (using %s)\n",
36 filename ? filename : "default cache"));
37
38 if (!(cache = (blkid_cache) calloc(1, sizeof(struct blkid_struct_cache))))
39 return -BLKID_ERR_MEM;
40
41 INIT_LIST_HEAD(&cache->bic_devs);
42 INIT_LIST_HEAD(&cache->bic_tags);
43
44 if (filename && !strlen(filename))
45 filename = 0;
46 if (!filename && (getuid() == geteuid()))
47 filename = getenv("BLKID_FILE");
48 if (!filename)
49 filename = BLKID_CACHE_FILE;
50 cache->bic_filename = blkid_strdup(filename);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000051
Mike Frysinger38a33f92005-05-09 22:13:22 +000052 blkid_read_cache(cache);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000053
Mike Frysinger38a33f92005-05-09 22:13:22 +000054 *ret_cache = cache;
55 return 0;
56}
57
58void blkid_put_cache(blkid_cache cache)
59{
60 if (!cache)
61 return;
62
63 (void) blkid_flush_cache(cache);
64
65 DBG(DEBUG_CACHE, printf("freeing cache struct\n"));
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000066
Mike Frysinger874af852006-03-08 07:03:27 +000067 /* DBG(DEBUG_CACHE, blkid_debug_dump_cache(cache)); */
Mike Frysinger38a33f92005-05-09 22:13:22 +000068
69 while (!list_empty(&cache->bic_devs)) {
70 blkid_dev dev = list_entry(cache->bic_devs.next,
71 struct blkid_struct_dev,
72 bid_devs);
73 blkid_free_dev(dev);
74 }
75
76 while (!list_empty(&cache->bic_tags)) {
77 blkid_tag tag = list_entry(cache->bic_tags.next,
78 struct blkid_struct_tag,
79 bit_tags);
80
81 while (!list_empty(&tag->bit_names)) {
82 blkid_tag bad = list_entry(tag->bit_names.next,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000083 struct blkid_struct_tag,
Mike Frysinger38a33f92005-05-09 22:13:22 +000084 bit_names);
85
86 DBG(DEBUG_CACHE, printf("warning: unfreed tag %s=%s\n",
87 bad->bit_name, bad->bit_val));
88 blkid_free_tag(bad);
89 }
90 blkid_free_tag(tag);
91 }
Rob Landleye7c43b62006-03-01 16:39:45 +000092 free(cache->bic_filename);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000093
Mike Frysinger38a33f92005-05-09 22:13:22 +000094 free(cache);
95}
96
97#ifdef TEST_PROGRAM
98int main(int argc, char** argv)
99{
100 blkid_cache cache = NULL;
101 int ret;
102
103 blkid_debug_mask = DEBUG_ALL;
104 if ((argc > 2)) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000105 fprintf(stderr, "Usage: %s [filename]\n", argv[0]);
Mike Frysinger38a33f92005-05-09 22:13:22 +0000106 exit(1);
107 }
108
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);
112 exit(1);
113 }
"Vladimir N. Oleynik"6c35c7c2005-10-12 15:34:25 +0000114 if ((ret = blkid_get_cache(&cache, bb_dev_null)) != 0) {
Mike Frysinger38a33f92005-05-09 22:13:22 +0000115 fprintf(stderr, "%s: error creating cache (%d)\n",
116 argv[0], ret);
117 exit(1);
118 }
119 if ((ret = blkid_probe_all(cache) < 0))
120 fprintf(stderr, "error probing devices\n");
121
122 blkid_put_cache(cache);
123
124 return ret;
125}
126#endif