Mike Frysinger | 38a33f9 | 2005-05-09 22:13:22 +0000 | [diff] [blame] | 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 <unistd.h> |
| 16 | #include "blkidP.h" |
| 17 | |
| 18 | int blkid_debug_mask = 0; |
| 19 | |
| 20 | int blkid_get_cache(blkid_cache *ret_cache, const char *filename) |
| 21 | { |
| 22 | blkid_cache cache; |
| 23 | |
| 24 | #ifdef CONFIG_BLKID_DEBUG |
| 25 | if (!(blkid_debug_mask & DEBUG_INIT)) { |
| 26 | char *dstr = getenv("BLKID_DEBUG"); |
| 27 | |
| 28 | if (dstr) |
| 29 | blkid_debug_mask = strtoul(dstr, 0, 0); |
| 30 | blkid_debug_mask |= DEBUG_INIT; |
| 31 | } |
| 32 | #endif |
| 33 | |
| 34 | DBG(DEBUG_CACHE, printf("creating blkid cache (using %s)\n", |
| 35 | filename ? filename : "default cache")); |
| 36 | |
| 37 | if (!(cache = (blkid_cache) calloc(1, sizeof(struct blkid_struct_cache)))) |
| 38 | return -BLKID_ERR_MEM; |
| 39 | |
| 40 | INIT_LIST_HEAD(&cache->bic_devs); |
| 41 | INIT_LIST_HEAD(&cache->bic_tags); |
| 42 | |
| 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); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 50 | |
Mike Frysinger | 38a33f9 | 2005-05-09 22:13:22 +0000 | [diff] [blame] | 51 | blkid_read_cache(cache); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 52 | |
Mike Frysinger | 38a33f9 | 2005-05-09 22:13:22 +0000 | [diff] [blame] | 53 | *ret_cache = cache; |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | void blkid_put_cache(blkid_cache cache) |
| 58 | { |
| 59 | if (!cache) |
| 60 | return; |
| 61 | |
| 62 | (void) blkid_flush_cache(cache); |
| 63 | |
| 64 | DBG(DEBUG_CACHE, printf("freeing cache struct\n")); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 65 | |
Mike Frysinger | 38a33f9 | 2005-05-09 22:13:22 +0000 | [diff] [blame] | 66 | /* DEB_DUMP_CACHE(cache); */ |
| 67 | |
| 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); |
| 73 | } |
| 74 | |
| 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); |
| 79 | |
| 80 | while (!list_empty(&tag->bit_names)) { |
| 81 | blkid_tag bad = list_entry(tag->bit_names.next, |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 82 | struct blkid_struct_tag, |
Mike Frysinger | 38a33f9 | 2005-05-09 22:13:22 +0000 | [diff] [blame] | 83 | bit_names); |
| 84 | |
| 85 | DBG(DEBUG_CACHE, printf("warning: unfreed tag %s=%s\n", |
| 86 | bad->bit_name, bad->bit_val)); |
| 87 | blkid_free_tag(bad); |
| 88 | } |
| 89 | blkid_free_tag(tag); |
| 90 | } |
Rob Landley | e7c43b6 | 2006-03-01 16:39:45 +0000 | [diff] [blame] | 91 | free(cache->bic_filename); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 92 | |
Mike Frysinger | 38a33f9 | 2005-05-09 22:13:22 +0000 | [diff] [blame] | 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 | } |
"Vladimir N. Oleynik" | 6c35c7c | 2005-10-12 15:34:25 +0000 | [diff] [blame] | 113 | if ((ret = blkid_get_cache(&cache, bb_dev_null)) != 0) { |
Mike Frysinger | 38a33f9 | 2005-05-09 22:13:22 +0000 | [diff] [blame] | 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 |