From: Karel Zak Date: Thu, 17 Mar 2011 11:51:06 +0000 (+0100) Subject: libblkid: add blkid_evaluate_spec() X-Git-Tag: v2.20-rc1~434 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=5298f72839cb44e2ae9d4407375674ba50f9b1c0;p=thirdparty%2Futil-linux.git libblkid: add blkid_evaluate_spec() 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 --- diff --git a/lib/fsprobe.c b/lib/fsprobe.c index 2d7be0e5bb..71d15f798e 100644 --- a/lib/fsprobe.c +++ b/lib/fsprobe.c @@ -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 diff --git a/shlibs/blkid/src/blkid.h.in b/shlibs/blkid/src/blkid.h.in index 46cbeb23b1..05527bed88 100644 --- a/shlibs/blkid/src/blkid.h.in +++ b/shlibs/blkid/src/blkid.h.in @@ -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); diff --git a/shlibs/blkid/src/blkid.sym b/shlibs/blkid/src/blkid.sym index 9df2ba3d33..e758ff1a60 100644 --- a/shlibs/blkid/src/blkid.sym +++ b/shlibs/blkid/src/blkid.sym @@ -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; + diff --git a/shlibs/blkid/src/evaluate.c b/shlibs/blkid/src/evaluate.c index 0b1cd51f9e..88c6830e89 100644 --- a/shlibs/blkid/src/evaluate.c +++ b/shlibs/blkid/src/evaluate.c @@ -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/ 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 \n", argv[0]); + if (argc < 2) { + fprintf(stderr, "usage: %s | \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)