resolve.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * resolve.c - resolve names and tags into specific devices
  3. *
  4. * Copyright (C) 2001, 2003 Theodore Ts'o.
  5. * Copyright (C) 2001 Andreas Dilger
  6. *
  7. * %Begin-Header%
  8. * This file may be redistributed under the terms of the
  9. * GNU Lesser General Public License.
  10. * %End-Header%
  11. */
  12. #include <stdio.h>
  13. #if HAVE_UNISTD_H
  14. #include <unistd.h>
  15. #endif
  16. #include <stdlib.h>
  17. #include <fcntl.h>
  18. #include <string.h>
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include "blkidP.h"
  22. #include "probe.h"
  23. /*
  24. * Find a tagname (e.g. LABEL or UUID) on a specific device.
  25. */
  26. char *blkid_get_tag_value(blkid_cache cache, const char *tagname,
  27. const char *devname)
  28. {
  29. blkid_tag found;
  30. blkid_dev dev;
  31. blkid_cache c = cache;
  32. char *ret = NULL;
  33. DBG(DEBUG_RESOLVE, printf("looking for %s on %s\n", tagname, devname));
  34. if (!devname)
  35. return NULL;
  36. if (!cache) {
  37. if (blkid_get_cache(&c, NULL) < 0)
  38. return NULL;
  39. }
  40. if ((dev = blkid_get_dev(c, devname, BLKID_DEV_NORMAL)) &&
  41. (found = blkid_find_tag_dev(dev, tagname)))
  42. ret = blkid_strdup(found->bit_val);
  43. if (!cache)
  44. blkid_put_cache(c);
  45. return ret;
  46. }
  47. /*
  48. * Locate a device name from a token (NAME=value string), or (name, value)
  49. * pair. In the case of a token, value is ignored. If the "token" is not
  50. * of the form "NAME=value" and there is no value given, then it is assumed
  51. * to be the actual devname and a copy is returned.
  52. */
  53. char *blkid_get_devname(blkid_cache cache, const char *token,
  54. const char *value)
  55. {
  56. blkid_dev dev;
  57. blkid_cache c = cache;
  58. char *t = 0, *v = 0;
  59. char *ret = NULL;
  60. if (!token)
  61. return NULL;
  62. if (!cache) {
  63. if (blkid_get_cache(&c, NULL) < 0)
  64. return NULL;
  65. }
  66. DBG(DEBUG_RESOLVE,
  67. printf("looking for %s%s%s %s\n", token, value ? "=" : "",
  68. value ? value : "", cache ? "in cache" : "from disk"));
  69. if (!value) {
  70. if (!strchr(token, '='))
  71. return blkid_strdup(token);
  72. blkid_parse_tag_string(token, &t, &v);
  73. if (!t || !v)
  74. goto errout;
  75. token = t;
  76. value = v;
  77. }
  78. dev = blkid_find_dev_with_tag(c, token, value);
  79. if (!dev)
  80. goto errout;
  81. ret = blkid_strdup(blkid_dev_devname(dev));
  82. errout:
  83. if (t)
  84. free(t);
  85. if (v)
  86. free(v);
  87. if (!cache) {
  88. blkid_put_cache(c);
  89. }
  90. return (ret);
  91. }
  92. #ifdef TEST_PROGRAM
  93. int main(int argc, char **argv)
  94. {
  95. char *value;
  96. blkid_cache cache;
  97. blkid_debug_mask = DEBUG_ALL;
  98. if (argc != 2 && argc != 3) {
  99. fprintf(stderr, "Usage:\t%s tagname=value\n"
  100. "\t%s tagname devname\n"
  101. "Find which device holds a given token or\n"
  102. "Find what the value of a tag is in a device\n",
  103. argv[0], argv[0]);
  104. exit(1);
  105. }
  106. if (blkid_get_cache(&cache, "/dev/null") < 0) {
  107. fprintf(stderr, "Couldn't get blkid cache\n");
  108. exit(1);
  109. }
  110. if (argv[2]) {
  111. value = blkid_get_tag_value(cache, argv[1], argv[2]);
  112. printf("%s has tag %s=%s\n", argv[2], argv[1],
  113. value ? value : "<missing>");
  114. } else {
  115. value = blkid_get_devname(cache, argv[1], NULL);
  116. printf("%s has tag %s\n", value ? value : "<none>", argv[1]);
  117. }
  118. blkid_put_cache(cache);
  119. return value ? 0 : 1;
  120. }
  121. #endif