diskbyname.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2011 Willem Dijkstra
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * - Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following
  13. * disclaimer in the documentation and/or other materials provided
  14. * with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  19. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  20. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  22. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  26. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. */
  30. #include <sys/stat.h>
  31. #include <sys/types.h>
  32. #include <stdlib.h>
  33. #include <errno.h>
  34. #include <string.h>
  35. #include <strings.h>
  36. #include "error.h"
  37. #include "platform.h"
  38. /*
  39. * checkdisk(spath, dpath, maxlen)
  40. *
  41. * Determine that <spath> exists, and return the device name that was referenced
  42. * (/dev/sdx) when <spath> is a link in <dpath>, observing <dpath>'s <maxlen>.
  43. */
  44. static size_t
  45. checkdisk(const char *spath, char *dpath, size_t maxlen)
  46. {
  47. char diskname[MAX_PATH_LEN];
  48. char *r;
  49. struct stat buffer;
  50. size_t result;
  51. bzero(diskname, MAX_PATH_LEN);
  52. bzero(&buffer, sizeof(struct stat));
  53. if (lstat(spath, &buffer)) {
  54. return 0;
  55. }
  56. if (S_ISLNK(buffer.st_mode)) {
  57. if ((r = realpath(spath, NULL))) {
  58. strlcpy(diskname, r, sizeof(diskname));
  59. free(r);
  60. }
  61. bzero(&buffer, sizeof(struct stat));
  62. if (lstat(diskname, &buffer)) {
  63. fatal("diskbyname: '%.200s' = '%.200s' cannot be examined: %.200s", spath, diskname, strerror(errno));
  64. }
  65. } else
  66. strlcpy(diskname, spath, sizeof(diskname));
  67. if (S_ISBLK(buffer.st_mode) && !S_ISLNK(buffer.st_mode)) {
  68. result = strlcpy(dpath, diskname, maxlen);
  69. return result;
  70. }
  71. return 0;
  72. }
  73. /*
  74. * diskbyname(spath, dpath, maxlen)
  75. *
  76. * Resolve a logical disk name <spath> to it's block device name
  77. * <dpath>. <dpath> is preallocated and can hold <maxlen> characters. <spath>
  78. * can refer to a disk via 1) an absolute path or 2) a diskname relative to
  79. * /dev, or 3) a diskname relative to the /dev/disk/by-* directories.
  80. */
  81. size_t
  82. diskbyname(const char *spath, char *dpath, size_t maxlen)
  83. {
  84. char diskname[MAX_PATH_LEN];
  85. size_t size;
  86. char *l[] = {
  87. "/dev/%s",
  88. "/dev/disk/by-id/%s",
  89. "/dev/disk/by-label/%s",
  90. "/dev/disk/by-uuid/%s",
  91. "/dev/disk/by-path/%s",
  92. NULL
  93. };
  94. int i;
  95. if (spath == NULL)
  96. return 0;
  97. if (strchr(spath, '/'))
  98. return checkdisk(spath, dpath, maxlen);
  99. for (i = 0; l[i] != NULL; i++) {
  100. bzero(diskname, sizeof(diskname));
  101. snprintf(diskname, sizeof(diskname), l[i], spath);
  102. if ((size = checkdisk(diskname, dpath, maxlen)))
  103. return size;
  104. }
  105. return 0;
  106. }