diskbyname.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 <stdio.h>
  34. #include <errno.h>
  35. #include <string.h>
  36. #include <strings.h>
  37. #include "error.h"
  38. #include "platform.h"
  39. /*
  40. * checkdisk(spath, dpath, maxlen)
  41. *
  42. * Determine that <spath> exists, and return the device name that was referenced
  43. * (/dev/sdx) when <spath> is a link in <dpath>, observing <dpath>'s <maxlen>.
  44. */
  45. static size_t
  46. checkdisk(const char *spath, char *dpath, size_t maxlen)
  47. {
  48. char diskname[MAX_PATH_LEN];
  49. char *r;
  50. struct stat buffer;
  51. size_t result;
  52. bzero(diskname, MAX_PATH_LEN);
  53. bzero(&buffer, sizeof(struct stat));
  54. if (lstat(spath, &buffer)) {
  55. return 0;
  56. }
  57. /* Walk one link, if it is there */
  58. if (S_ISLNK(buffer.st_mode)) {
  59. if ((r = realpath(spath, NULL))) {
  60. strlcpy(diskname, r, sizeof(diskname));
  61. free(r);
  62. }
  63. bzero(&buffer, sizeof(struct stat));
  64. if (lstat(diskname, &buffer)) {
  65. fatal("diskbyname: '%.200s' = '%.200s' cannot be examined: %.200s", spath, diskname, strerror(errno));
  66. }
  67. } else
  68. strlcpy(diskname, spath, sizeof(diskname));
  69. /*
  70. * No more links from here; also note the lack of further checks on the
  71. * stat structure. For linux we should now be looking at a block device,
  72. * for FreeBSD this should be a character device.
  73. */
  74. if (!S_ISLNK(buffer.st_mode)) {
  75. result = strlcpy(dpath, diskname, maxlen);
  76. return result;
  77. }
  78. return 0;
  79. }
  80. /*
  81. * diskbyname(spath, dpath, maxlen)
  82. *
  83. * Resolve a logical disk name <spath> to it's block device name
  84. * <dpath>. <dpath> is preallocated and can hold <maxlen> characters. <spath>
  85. * can refer to a disk via 1) an absolute path or 2) a diskname relative to
  86. * /dev in various forms defined in platform specific DISK_PATHS.
  87. */
  88. size_t
  89. diskbyname(const char *spath, char *dpath, size_t maxlen)
  90. {
  91. char diskname[MAX_PATH_LEN];
  92. size_t size;
  93. #ifdef DISK_PATHS
  94. char *l[] = DISK_PATHS;
  95. #else
  96. char *l[] = { "/dev/%s", NULL };
  97. #endif
  98. int i;
  99. if (spath == NULL)
  100. return 0;
  101. if (strchr(spath, '/') == spath)
  102. return checkdisk(spath, dpath, maxlen);
  103. for (i = 0; l[i] != NULL; i++) {
  104. bzero(diskname, sizeof(diskname));
  105. snprintf(diskname, sizeof(diskname), l[i], spath);
  106. if ((size = checkdisk(diskname, dpath, maxlen)))
  107. return size;
  108. }
  109. return 0;
  110. }