Whamcloud - gitweb
Add new two new functions to the blkid library: blkid_dev_set_search(), and
[tools/e2fsprogs.git] / lib / blkid / tag.c
index d3db41c..e3df150 100644 (file)
@@ -70,6 +70,22 @@ blkid_tag blkid_find_tag_dev(blkid_dev dev, const char *type)
        return NULL;
 }
 
+extern int blkid_dev_has_tag(blkid_dev dev, const char *type, 
+                            const char *value)
+{
+       blkid_tag               tag;
+
+       if (!dev || !type || !value)
+               return -1;
+
+       tag = blkid_find_tag_dev(dev, type);
+       if (!value)
+               return(tag != NULL);
+       if (!tag || strcmp(tag->bit_val, value))
+               return 0;
+       return 1;
+}
+
 /*
  * Find the desired tag type in the cache.
  * We return the head tag for this tag type.
@@ -108,7 +124,7 @@ int blkid_set_tag(blkid_dev dev, const char *name,
        if (!dev || !name)
                return -BLKID_ERR_PARAM;
 
-       if (!(val = blkid_strndup(value, vlength)))
+       if (!(val = blkid_strndup(value, vlength)) && value)
                return -BLKID_ERR_MEM;
        t = blkid_find_tag_dev(dev, name);
        if (!value) {
@@ -326,14 +342,102 @@ try_again:
                }
        }
        if (dev && !(dev->bid_flags & BLKID_BID_FL_VERIFIED)) {
-               dev = blkid_verify_devname(cache, dev);
+               dev = blkid_verify(cache, dev);
                if (dev && (dev->bid_flags & BLKID_BID_FL_VERIFIED))
                        goto try_again;
        }
 
        if (!dev && !(cache->bic_flags & BLKID_BIC_FL_PROBED)) {
-               blkid_probe_all(cache);
+               if (blkid_probe_all(cache) < 0)
+                       return NULL;
                goto try_again;
        }
        return dev;
 }
+
+#ifdef TEST_PROGRAM
+#ifdef HAVE_GETOPT_H
+#include <getopt.h>
+#else
+extern char *optarg;
+extern int optind;
+#endif
+
+void usage(char *prog)
+{
+       fprintf(stderr, "Usage: %s [-f blkid_file] [-m debug_mask] device "
+               "[type value]\n", 
+               prog);
+       fprintf(stderr, "\tList all tags for a device and exit\n", prog);
+       exit(1);
+}
+
+int main(int argc, char **argv)
+{
+       blkid_tag_iterate       iter;
+       blkid_cache             cache = NULL;
+       blkid_dev               dev;
+       int                     c, ret, found;
+       int                     flags = BLKID_DEV_FIND;
+       char                    *tmp;
+       char                    *file = NULL;
+       char                    *devname = NULL;
+       char                    *search_type = NULL;
+       char                    *search_value = NULL;
+       const char              *type, *value;
+
+       while ((c = getopt (argc, argv, "m:f:")) != EOF)
+               switch (c) {
+               case 'f':
+                       file = optarg;
+                       break;
+               case 'm':
+                       blkid_debug_mask = strtoul (optarg, &tmp, 0);
+                       if (*tmp) {
+                               fprintf(stderr, "Invalid debug mask: %d\n", 
+                                       optarg);
+                               exit(1);
+                       }
+                       break;
+               case '?':
+                       usage(argv[0]);
+               }
+       if (argc > optind)
+               devname = argv[optind++];
+       if (argc > optind)
+               search_type = argv[optind++];
+       if (argc > optind)
+               search_value = argv[optind++];
+       if (!devname || (argc != optind))
+               usage(argv[0]);
+
+       if ((ret = blkid_get_cache(&cache, file)) != 0) {
+               fprintf(stderr, "%s: error creating cache (%d)\n",
+                       argv[0], ret);
+               exit(1);
+       }
+
+       dev = blkid_get_dev(cache, devname, flags);
+       if (!dev) {
+               fprintf(stderr, "%s: Can not find device in blkid cache\n");
+               exit(1);
+       }
+       if (search_type) {
+               found = blkid_dev_has_tag(dev, search_type, search_value);
+               printf("Device %s: (%s, %s) %s\n", blkid_dev_devname(dev),
+                      search_type, search_value ? search_value : "NULL", 
+                      found ? "FOUND" : "NOT FOUND");
+               return(!found);
+       }
+       printf("Device %s...\n", blkid_dev_devname(dev));
+
+       iter = blkid_tag_iterate_begin(dev);
+       while (blkid_tag_next(iter, &type, &value) == 0) {
+               printf("\tTag %s has value %s\n", type, value);
+       }
+       blkid_tag_iterate_end(iter);
+
+       blkid_put_cache(cache);
+       return (0);
+}
+#endif