diskname.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (c) 2012 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. #include "diskname.h"
  40. void
  41. initdisknamectx(struct disknamectx *c, const char *spath, char *dpath, size_t dmaxlen)
  42. {
  43. bzero(c, sizeof(struct disknamectx));
  44. c->spath = spath;
  45. c->dpath = dpath;
  46. c->maxlen = dmaxlen;
  47. if (c->dpath != NULL)
  48. bzero(c->dpath, c->maxlen);
  49. }
  50. /*
  51. * diskbyname(spath, dpath, maxlen)
  52. *
  53. * Resolve a logical disk name <spath> to it's block device name
  54. * <dpath>. <dpath> is preallocated and can hold <maxlen> characters. <spath>
  55. * can refer to a disk via 1) an absolute path or 2) a diskname relative to
  56. * /dev in various forms.
  57. */
  58. char *
  59. nextdiskname(struct disknamectx *c)
  60. {
  61. char *r;
  62. struct stat buffer;
  63. #ifdef DISK_PATHS
  64. char *l[] = DISK_PATHS;
  65. #else
  66. char *l[] = { "/dev/%s", NULL };
  67. #endif
  68. if ((c->spath == NULL) || (c->dpath == NULL) || (l[c->i] == NULL))
  69. return NULL;
  70. bzero(c->dpath, c->maxlen);
  71. /* do not play with absolute paths, just return it once */
  72. if (c->spath[0] == '/') {
  73. if (c->i == 0) {
  74. strlcpy(c->dpath, c->spath, c->maxlen);
  75. c->i++;
  76. return c->dpath;
  77. }
  78. return NULL;
  79. }
  80. /* prepare the next pathname */
  81. snprintf(c->dpath, c->maxlen, l[c->i], c->spath);
  82. if ((c->link == 0) &&
  83. (lstat(c->dpath, &buffer) == 0)) {
  84. /* return the real path of a link, but only dereference once */
  85. if (S_ISLNK(buffer.st_mode)) {
  86. if ((r = realpath(c->dpath, NULL))) {
  87. strlcpy(c->dpath, r, c->maxlen);
  88. free(r);
  89. c->link = 1;
  90. return c->dpath;
  91. }
  92. }
  93. }
  94. c->link = 0;
  95. c->i++;
  96. return c->dpath;
  97. }