blob: b576df8b4672113cee2ba4b0defc21c597829ad3 [file] [log] [blame]
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001/*
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#ifdef HAVE_UNISTD_H
14#include <unistd.h>
15#endif
16#ifdef HAVE_ERRNO_H
17#include <errno.h>
18#endif
19#include <stdlib.h>
20#include <string.h>
21#ifdef HAVE_SYS_STAT_H
22#include <sys/stat.h>
23#endif
24#include "blkidP.h"
25#include "env.h"
26
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050027/**
28 * SECTION:cache
29 * @title: Cache
30 * @short_description: basic routines to work with libblkid cache
31 *
32 * Block device information is normally kept in a cache file blkid.tab and is
33 * verified to still be valid before being returned to the user (if the user has
34 * read permission on the raw block device, otherwise not). The cache file also
35 * allows unprivileged users (normally anyone other than root, or those not in the
36 * "disk" group) to locate devices by label/id. The standard location of the
37 * cache file can be overridden by the environment variable BLKID_FILE.
38 *
39 * In situations where one is getting information about a single known device, it
40 * does not impact performance whether the cache is used or not (unless you are
41 * not able to read the block device directly). If you are dealing with multiple
42 * devices, use of the cache is highly recommended (even if empty) as devices will
43 * be scanned at most one time and the on-disk cache will be updated if possible.
44 * There is rarely a reason not to use the cache.
45 *
46 * In some cases (modular kernels), block devices are not even visible until after
47 * they are accessed the first time, so it is critical that there is some way to
48 * locate these devices without enumerating only visible devices, so the use of
49 * the cache file is required in this situation.
50 */
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050051static const char *get_default_cache_filename(void)
52{
53 struct stat st;
54
55 if (stat(BLKID_RUNTIME_TOPDIR, &st) == 0 && S_ISDIR(st.st_mode))
56 return BLKID_CACHE_FILE; /* cache in /run */
57
58 return BLKID_CACHE_FILE_OLD; /* cache in /etc */
59}
60
61/* returns allocated path to cache */
62char *blkid_get_cache_filename(struct blkid_config *conf)
63{
64 char *filename;
65
66 filename = safe_getenv("BLKID_FILE");
67 if (filename)
68 filename = strdup(filename);
69 else if (conf)
70 filename = conf->cachefile ? strdup(conf->cachefile) : NULL;
71 else {
72 struct blkid_config *c = blkid_read_config(NULL);
73 if (!c)
74 filename = strdup(get_default_cache_filename());
75 else {
76 filename = c->cachefile; /* already allocated */
77 c->cachefile = NULL;
78 blkid_free_config(c);
79 }
80 }
81 return filename;
82}
83
84/**
85 * blkid_get_cache:
86 * @cache: pointer to return cache handler
87 * @filename: path to the cache file or NULL for the default path
88 *
89 * Allocates and initialize librray cache handler.
90 *
91 * Returns: 0 on success or number less than zero in case of error.
92 */
93int blkid_get_cache(blkid_cache *ret_cache, const char *filename)
94{
95 blkid_cache cache;
96
97 if (!ret_cache)
98 return -BLKID_ERR_PARAM;
99
100 blkid_init_debug(0);
101
bigbiff7b4c7a62015-01-01 19:44:14 -0500102 DBG(CACHE, ul_debug("creating blkid cache (using %s)",
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500103 filename ? filename : "default cache"));
104
105 if (!(cache = (blkid_cache) calloc(1, sizeof(struct blkid_struct_cache))))
106 return -BLKID_ERR_MEM;
107
108 INIT_LIST_HEAD(&cache->bic_devs);
109 INIT_LIST_HEAD(&cache->bic_tags);
110
111 if (filename && !*filename)
112 filename = NULL;
113 if (filename)
114 cache->bic_filename = strdup(filename);
115 else
116 cache->bic_filename = blkid_get_cache_filename(NULL);
117
118 blkid_read_cache(cache);
119 *ret_cache = cache;
120 return 0;
121}
122
123/**
124 * blkid_put_cache:
125 * @cache: cache handler
126 *
127 * Saves changes to cache file.
128 */
129void blkid_put_cache(blkid_cache cache)
130{
131 if (!cache)
132 return;
133
134 (void) blkid_flush_cache(cache);
135
bigbiff7b4c7a62015-01-01 19:44:14 -0500136 DBG(CACHE, ul_debug("freeing cache struct"));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500137
bigbiff7b4c7a62015-01-01 19:44:14 -0500138 /* DBG(CACHE, ul_debug_dump_cache(cache)); */
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500139
140 while (!list_empty(&cache->bic_devs)) {
141 blkid_dev dev = list_entry(cache->bic_devs.next,
142 struct blkid_struct_dev,
143 bid_devs);
144 blkid_free_dev(dev);
145 }
146
147 while (!list_empty(&cache->bic_tags)) {
148 blkid_tag tag = list_entry(cache->bic_tags.next,
149 struct blkid_struct_tag,
150 bit_tags);
151
152 while (!list_empty(&tag->bit_names)) {
153 blkid_tag bad = list_entry(tag->bit_names.next,
154 struct blkid_struct_tag,
155 bit_names);
156
bigbiff7b4c7a62015-01-01 19:44:14 -0500157 DBG(CACHE, ul_debug("warning: unfreed tag %s=%s",
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500158 bad->bit_name, bad->bit_val));
159 blkid_free_tag(bad);
160 }
161 blkid_free_tag(tag);
162 }
163
164 blkid_free_probe(cache->probe);
165
166 free(cache->bic_filename);
167 free(cache);
168}
169
170/**
171 * blkid_gc_cache:
172 * @cache: cache handler
173 *
174 * Removes garbage (non-existing devices) from the cache.
175 */
176void blkid_gc_cache(blkid_cache cache)
177{
178 struct list_head *p, *pnext;
179 struct stat st;
180
181 if (!cache)
182 return;
183
184 list_for_each_safe(p, pnext, &cache->bic_devs) {
185 blkid_dev dev = list_entry(p, struct blkid_struct_dev, bid_devs);
186 if (stat(dev->bid_name, &st) < 0) {
bigbiff7b4c7a62015-01-01 19:44:14 -0500187 DBG(CACHE, ul_debug("freeing %s", dev->bid_name));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500188 blkid_free_dev(dev);
189 cache->bic_flags |= BLKID_BIC_FL_CHANGED;
190 } else {
bigbiff7b4c7a62015-01-01 19:44:14 -0500191 DBG(CACHE, ul_debug("Device %s exists", dev->bid_name));
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500192 }
193 }
194}
195
196#ifdef TEST_PROGRAM
197int main(int argc, char** argv)
198{
199 blkid_cache cache = NULL;
200 int ret;
201
bigbiff7b4c7a62015-01-01 19:44:14 -0500202 blkid_init_debug(BLKID_DEBUG_ALL);
bigbiff bigbiffe60683a2013-02-22 20:55:50 -0500203
204 if ((argc > 2)) {
205 fprintf(stderr, "Usage: %s [filename] \n", argv[0]);
206 exit(1);
207 }
208
209 if ((ret = blkid_get_cache(&cache, argv[1])) < 0) {
210 fprintf(stderr, "error %d parsing cache file %s\n", ret,
211 argv[1] ? argv[1] : blkid_get_cache_filename(NULL));
212 exit(1);
213 }
214 if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
215 fprintf(stderr, "%s: error creating cache (%d)\n",
216 argv[0], ret);
217 exit(1);
218 }
219 if ((ret = blkid_probe_all(cache)) < 0)
220 fprintf(stderr, "error probing devices\n");
221
222 blkid_put_cache(cache);
223
224 return ret;
225}
226#endif