Whamcloud - gitweb
Merge branch 'maint' into next
[tools/e2fsprogs.git] / lib / blkid / resolve.c
index fca1e16..3bc37b0 100644 (file)
@@ -10,6 +10,7 @@
  * %End-Header%
  */
 
+#include "config.h"
 #include <stdio.h>
 #if HAVE_UNISTD_H
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include "blkidP.h"
-#include "probe.h"
-
-#ifdef DEBUG_RESOLVE
-#define DBG(x) x
-#else
-#define DBG(x)
-#endif
-
 
 /*
  * Find a tagname (e.g. LABEL or UUID) on a specific device.
  */
-char *blkid_get_tagname_devname(blkid_cache cache, const char *tagname,
-                               const char *devname)
+char *blkid_get_tag_value(blkid_cache cache, const char *tagname,
+                         const char *devname)
 {
        blkid_tag found;
        blkid_dev dev;
+       blkid_cache c = cache;
        char *ret = NULL;
 
-       DBG(printf("looking for %s on %s\n", tagname, devname));
+       DBG(DEBUG_RESOLVE, printf("looking for %s on %s\n", tagname, devname));
 
        if (!devname)
                return NULL;
 
-       if ((dev = blkid_get_devname(cache, devname, BLKID_DEV_NORMAL)) &&
+       if (!cache) {
+               if (blkid_get_cache(&c, NULL) < 0)
+                       return NULL;
+       }
+
+       if ((dev = blkid_get_dev(c, devname, BLKID_DEV_NORMAL)) &&
            (found = blkid_find_tag_dev(dev, tagname)))
                ret = blkid_strdup(found->bit_val);
 
        if (!cache)
-               blkid_free_dev(dev);
+               blkid_put_cache(c);
 
        return ret;
 }
@@ -60,8 +59,8 @@ char *blkid_get_tagname_devname(blkid_cache cache, const char *tagname,
  * of the form "NAME=value" and there is no value given, then it is assumed
  * to be the actual devname and a copy is returned.
  */
-char *blkid_get_token(blkid_cache cache, const char *token,
-                     const char *value)
+char *blkid_get_devname(blkid_cache cache, const char *token,
+                       const char *value)
 {
        blkid_dev dev;
        blkid_cache c = cache;
@@ -70,36 +69,37 @@ char *blkid_get_token(blkid_cache cache, const char *token,
 
        if (!token)
                return NULL;
-       
-       DBG(printf("looking for %s%c%s %s\n", token, value ? '=' : ' ',
-                  value ? value : "", cache ? "in cache" : "from disk"));
 
        if (!cache) {
                if (blkid_get_cache(&c, NULL) < 0)
-                       c = blkid_new_cache();
-               if (!c)
                        return NULL;
        }
 
+       DBG(DEBUG_RESOLVE,
+           printf("looking for %s%s%s %s\n", token, value ? "=" : "",
+                  value ? value : "", cache ? "in cache" : "from disk"));
+
        if (!value) {
+               if (!strchr(token, '=')) {
+                       ret = blkid_strdup(token);
+                       goto out;
+               }
                blkid_parse_tag_string(token, &t, &v);
                if (!t || !v)
-                       goto errout;
+                       goto out;
                token = t;
                value = v;
        }
 
        dev = blkid_find_dev_with_tag(c, token, value);
        if (!dev)
-               goto errout;
+               goto out;
 
-       ret = blkid_strdup(blkid_devname_name(dev));
+       ret = blkid_strdup(blkid_dev_devname(dev));
 
-errout:
-       if (t)
-               free(t);
-       if (v)
-               free(v);
+out:
+       free(t);
+       free(v);
        if (!cache) {
                blkid_put_cache(c);
        }
@@ -112,6 +112,7 @@ int main(int argc, char **argv)
        char *value;
        blkid_cache cache;
 
+       blkid_debug_mask = DEBUG_ALL;
        if (argc != 2 && argc != 3) {
                fprintf(stderr, "Usage:\t%s tagname=value\n"
                        "\t%s tagname devname\n"
@@ -120,17 +121,17 @@ int main(int argc, char **argv)
                        argv[0], argv[0]);
                exit(1);
        }
-       if (blkid_get_cache(&cache, 0) < 0) {
+       if (blkid_get_cache(&cache, "/dev/null") < 0) {
                fprintf(stderr, "Couldn't get blkid cache\n");
                exit(1);
        }
-       
+
        if (argv[2]) {
-               value = blkid_get_tagname_devname(cache, argv[1], argv[2]);
+               value = blkid_get_tag_value(cache, argv[1], argv[2]);
                printf("%s has tag %s=%s\n", argv[2], argv[1],
                       value ? value : "<missing>");
        } else {
-               value = blkid_get_token(cache, argv[1], NULL);
+               value = blkid_get_devname(cache, argv[1], NULL);
                printf("%s has tag %s\n", value ? value : "<none>", argv[1]);
        }
        blkid_put_cache(cache);