cache.c revision d3f917989badf78d1f97654e46d60d1f3d25cd17
1/*
2 * cache.c - allocation/initialization/free routines for cache
3 *
4 * Copyright (C) 2001 Andreas Dilger
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the
8 * GNU Lesser General Public License.
9 * %End-Header%
10 */
11
12#include <stdlib.h>
13#include "blkid/blkid.h"
14
15#ifdef DEBUG_CACHE
16#include <stdio.h>
17#define DBG(x)	x
18#else
19#define DBG(x)
20#endif
21
22blkid_cache *blkid_new_cache(void)
23{
24	blkid_cache *cache;
25
26	if (!(cache = (blkid_cache *)calloc(1, sizeof(blkid_cache))))
27		return NULL;
28
29	INIT_LIST_HEAD(&cache->bic_devs);
30	INIT_LIST_HEAD(&cache->bic_tags);
31
32	return cache;
33}
34
35void blkid_free_cache(blkid_cache *cache)
36{
37	if (!cache)
38		return;
39
40	DBG(printf("freeing cache struct\n"));
41	/* DEB_DUMP_CACHE(cache); */
42
43	while (!list_empty(&cache->bic_devs)) {
44		blkid_dev *dev = list_entry(cache->bic_devs.next, blkid_dev,
45					    bid_devs);
46		blkid_free_dev(dev);
47	}
48
49	while (!list_empty(&cache->bic_tags)) {
50		blkid_tag *tag = list_entry(cache->bic_tags.next, blkid_tag,
51					    bit_tags);
52
53		while (!list_empty(&tag->bit_names)) {
54			blkid_tag *bad = list_entry(tag->bit_names.next,
55						    blkid_tag, bit_names);
56
57			DBG(printf("warning: unfreed tag %s=%s\n",
58				   bad->bit_name, bad->bit_val));
59			blkid_free_tag(bad);
60		}
61		blkid_free_tag(tag);
62	}
63	free(cache);
64}
65
66#ifdef TEST_PROGRAM
67int main(int argc, char** argv)
68{
69	blkid_cache *cache = NULL;
70	int ret;
71
72	if ((argc > 2)) {
73		fprintf(stderr, "Usage: %s [filename] \n", argv[0]);
74		exit(1);
75	}
76
77	if ((ret = blkid_read_cache(&cache, argv[1])) < 0)
78		fprintf(stderr, "error %d parsing cache file %s\n", ret,
79			argv[1] ? argv[1] : BLKID_CACHE_FILE);
80	else if ((ret = blkid_probe_all(&cache) < 0))
81		fprintf(stderr, "error probing devices\n");
82	else if ((ret = blkid_save_cache(cache, argv[1])) < 0)
83		fprintf(stderr, "error %d saving cache to %s\n", ret,
84			argv[1] ? argv[1] : BLKID_CACHE_FILE);
85
86	blkid_free_cache(cache);
87
88	return ret;
89}
90#endif
91