resolve.c 3.0 KB

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