]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libblkid: add blkid_evaluate_spec()
authorKarel Zak <kzak@redhat.com>
Thu, 17 Mar 2011 11:51:06 +0000 (12:51 +0100)
committerKarel Zak <kzak@redhat.com>
Thu, 17 Mar 2011 11:51:06 +0000 (12:51 +0100)
The function blkid_evaluate_tag() is useful for tags only (e.g.
LABEL=foo). But we also need to address devices by tags OR paths in
many utils.

The function blkid_evaluate_spec() support this functionality without
extra care about the way how device is addressed. The tags as well as
paths are converted to the standardized device path.

Signed-off-by: Karel Zak <kzak@redhat.com>
lib/fsprobe.c
shlibs/blkid/src/blkid.h.in
shlibs/blkid/src/blkid.sym
shlibs/blkid/src/evaluate.c

index 2d7be0e5bbc2fd717d2e9c83e99fbab14ddb4d52..71d15f798ee842cd4211d6bbb1ca25669fe6ef51 100644 (file)
@@ -53,26 +53,7 @@ fsprobe_parse_spec(const char *spec, char **name, char **value)
 char *
 fsprobe_get_devname_by_spec(const char *spec)
 {
-       char *name, *value;
-
-       if (!spec)
-               return NULL;
-       if (fsprobe_parse_spec(spec, &name, &value) != 0)
-               return NULL;                            /* parse error */
-       if (name) {
-               char *nspec = NULL;
-
-               if (!strcmp(name,"LABEL"))
-                       nspec = fsprobe_get_devname_by_label(value);
-               else if (!strcmp(name,"UUID"))
-                       nspec = fsprobe_get_devname_by_uuid(value);
-
-               free(name);
-               free(value);
-               return nspec;
-       }
-
-       return canonicalize_path(spec);
+       return blkid_evaluate_spec(spec, blcache);
 }
 
 int
index 46cbeb23b10fa031d302e4b7544bd39bf6733edf..05527bed88aa9dc5f8a2dbaac88f87b726483f0e 100644 (file)
@@ -186,6 +186,7 @@ extern int blkid_safe_string(const char *str, char *str_safe, size_t len);
 extern int blkid_send_uevent(const char *devname, const char *action);
 extern char *blkid_evaluate_tag(const char *token, const char *value,
                                blkid_cache *cache);
+extern char *blkid_evaluate_spec(const char *spec, blkid_cache *cache);
 
 /* probe.c */
 extern blkid_probe blkid_new_probe(void);
index 9df2ba3d33365384944e15f335fe1a584ff40a36..e758ff1a60158e2f1f3394e074b397221e641ba0 100644 (file)
@@ -39,7 +39,7 @@ local:
 
 
 /*
- * version(s) since util-linux 2.15
+ * symbols since util-linux 2.15
  */
 BLKID_2.15 {
 global:
@@ -67,7 +67,7 @@ global:
 } BLKID_1.0;
 
 /*
- * version(s) since util-linux 2.17
+ * symbols since util-linux 2.17
  */
 BLKID_2.17 {
 global:
@@ -117,7 +117,7 @@ global:
 } BLKID_2.15;
 
 /*
- * version(s) since util-linux 2.18
+ * symbols since util-linux 2.18
  */
 BLKID_2.18 {
 global:
@@ -130,3 +130,12 @@ global:
        blkid_probe_get_wholedisk_devno;
        blkid_probe_is_wholedisk;
 } BLKID_2.17;
+
+/*
+ * symbols since util-linux 2.20
+ */
+BLKID_2.20 {
+global:
+       blkid_evaluate_spec;
+} BLKID_2.18;
+
index 0b1cd51f9eab9ac02bb95e3bbc70c9289be1abab..88c6830e894fe22d36bf4e146210259473c8dd2a 100644 (file)
@@ -202,8 +202,8 @@ static char *evaluate_by_scan(const char *token, const char *value,
 
 /**
  * blkid_evaluate_tag:
- * @token: token name (e.g "LABEL" or "UUID")
- * @value: token data
+ * @token: token name (e.g "LABEL" or "UUID") or unparsed tag (e.g. "LABEL=foo")
+ * @value: token data (e.g. "foo")
  * @cache: pointer to cache (or NULL when you don't want to re-use the cache)
  *
  * Returns: allocated string with a device name.
@@ -259,20 +259,52 @@ out:
        return ret;
 }
 
+/**
+ * blkid_evaluate_spec:
+ * @spec: unparsed tag (e.g. "LABEL=foo") or path (e.g. /dev/dm-0)
+ * @cache: pointer to cache (or NULL when you don't want to re-use the cache)
+ *
+ * All returned paths are canonicalized, device-mapper paths are converted
+ * to the /dev/mapper/<name> format.
+ *
+ * Returns: allocated string with a device name.
+ */
+char *blkid_evaluate_spec(const char *spec, blkid_cache *cache)
+{
+       char *t = NULL, *v = NULL, *res;
+
+       if (!spec)
+               return NULL;
+
+       if (strchr(spec, '=') &&
+           blkid_parse_tag_string(spec, &t, &v) != 0)  /* parse error */
+               return NULL;
+
+       if (v)
+               res = blkid_evaluate_tag(t, v, cache);
+       else
+               res = canonicalize_path(spec);
+
+       free(t);
+       free(v);
+       return res;
+}
+
+
 #ifdef TEST_PROGRAM
 int main(int argc, char *argv[])
 {
        blkid_cache cache = NULL;
        char *res;
 
-       if (argc < 3) {
-               fprintf(stderr, "usage: %s <token> <value>\n", argv[0]);
+       if (argc < 2) {
+               fprintf(stderr, "usage: %s <tag> | <spec>\n", argv[0]);
                return EXIT_FAILURE;
        }
 
        blkid_init_debug(0);
 
-       res = blkid_evaluate_tag(argv[1], argv[2], &cache);
+       res = blkid_evaluate_spec(argv[1], &cache);
        if (res)
                printf("%s\n", res);
        if (cache)