Whamcloud - gitweb
Fix XFS superblock definition. Add support to
[tools/e2fsprogs.git] / lib / blkid / cache.c
index 0e1fc8e..bfa104e 100644 (file)
 #include <stdlib.h>
 #include "blkidP.h"
 
-#ifdef DEBUG_CACHE
-#include <stdio.h>
-#define DBG(x) x
-#else
-#define DBG(x)
-#endif
+int blkid_debug_mask = 0;
 
-blkid_cache blkid_new_cache(void)
+int blkid_get_cache(blkid_cache *ret_cache, const char *filename)
 {
        blkid_cache cache;
 
+#ifdef CONFIG_BLKID_DEBUG
+       if (!(blkid_debug_mask & DEBUG_INIT)) {
+               char *dstr = getenv("BLKID_DEBUG");
+
+               if (dstr)
+                       blkid_debug_mask = strtoul(dstr, 0, 0);
+               blkid_debug_mask |= DEBUG_INIT;
+       }
+#endif
+
+       DBG(DEBUG_CACHE, printf("creating blkid cache (using %s)\n",
+                               filename ? filename : "default cache"));
+
        if (!(cache = (blkid_cache) calloc(1, sizeof(struct blkid_struct_cache))))
-               return NULL;
+               return -BLKID_ERR_MEM;
 
        INIT_LIST_HEAD(&cache->bic_devs);
        INIT_LIST_HEAD(&cache->bic_tags);
 
-       return cache;
+       if (!filename || !strlen(filename))
+               filename = BLKID_CACHE_FILE;
+       cache->bic_filename = blkid_strdup(filename);
+       
+       blkid_read_cache(cache);
+       
+       *ret_cache = cache;
+       return 0;
 }
 
 void blkid_put_cache(blkid_cache cache)
@@ -40,7 +55,7 @@ void blkid_put_cache(blkid_cache cache)
 
        (void) blkid_flush_cache(cache);
 
-       DBG(printf("freeing cache struct\n"));
+       DBG(DEBUG_CACHE, printf("freeing cache struct\n"));
        
        /* DEB_DUMP_CACHE(cache); */
 
@@ -61,8 +76,8 @@ void blkid_put_cache(blkid_cache cache)
                                                   struct blkid_struct_tag, 
                                                   bit_names);
 
-                       DBG(printf("warning: unfreed tag %s=%s\n",
-                                  bad->bit_name, bad->bit_val));
+                       DBG(DEBUG_CACHE, printf("warning: unfreed tag %s=%s\n",
+                                               bad->bit_name, bad->bit_val));
                        blkid_free_tag(bad);
                }
                blkid_free_tag(tag);
@@ -79,6 +94,7 @@ int main(int argc, char** argv)
        blkid_cache cache = NULL;
        int ret;
 
+       blkid_debug_mask = DEBUG_ALL;
        if ((argc > 2)) {
                fprintf(stderr, "Usage: %s [filename] \n", argv[0]);
                exit(1);
@@ -89,8 +105,9 @@ int main(int argc, char** argv)
                        argv[1] ? argv[1] : BLKID_CACHE_FILE);
                exit(1);
        }
-       if ((cache = blkid_new_cache()) == NULL) {
-               fprintf(stderr, "%s: error creating cache\n", argv[0]);
+       if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
+               fprintf(stderr, "%s: error creating cache (%d)\n",
+                       argv[0], ret);
                exit(1);
        }
        if ((ret = blkid_probe_all(cache) < 0))