xreadlink.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * xreadlink.c - safe implementation of readlink.
  4. * Returns a NULL on failure.
  5. *
  6. * Licensed under GPLv2, see file LICENSE in this source tree.
  7. */
  8. #include "libbb.h"
  9. /* Some systems (eg Hurd) do not have MAXSYMLINKS definition,
  10. * set it to some reasonable value if it isn't defined */
  11. #ifndef MAXSYMLINKS
  12. # define MAXSYMLINKS 20
  13. #endif
  14. /*
  15. * NOTE: This function returns a malloced char* that you will have to free
  16. * yourself.
  17. */
  18. char* FAST_FUNC xmalloc_readlink(const char *path)
  19. {
  20. enum { GROWBY = 80 }; /* how large we will grow strings by */
  21. char *buf = NULL;
  22. int bufsize = 0, readsize = 0;
  23. do {
  24. bufsize += GROWBY;
  25. buf = xrealloc(buf, bufsize);
  26. readsize = readlink(path, buf, bufsize);
  27. if (readsize == -1) {
  28. free(buf);
  29. return NULL;
  30. }
  31. } while (bufsize < readsize + 1);
  32. buf[readsize] = '\0';
  33. return buf;
  34. }
  35. /*
  36. * This routine is not the same as realpath(), which
  37. * canonicalizes the given path completely. This routine only
  38. * follows trailing symlinks until a real file is reached and
  39. * returns its name. If the path ends in a dangling link or if
  40. * the target doesn't exist, the path is returned in any case.
  41. * Intermediate symlinks in the path are not expanded -- only
  42. * those at the tail.
  43. * A malloced char* is returned, which must be freed by the caller.
  44. */
  45. char* FAST_FUNC xmalloc_follow_symlinks(const char *path)
  46. {
  47. char *buf;
  48. char *lpc;
  49. char *linkpath;
  50. int bufsize;
  51. int looping = MAXSYMLINKS + 1;
  52. buf = xstrdup(path);
  53. goto jump_in;
  54. while (1) {
  55. linkpath = xmalloc_readlink(buf);
  56. if (!linkpath) {
  57. /* not a symlink, or doesn't exist */
  58. if (errno == EINVAL || errno == ENOENT)
  59. return buf;
  60. goto free_buf_ret_null;
  61. }
  62. if (!--looping) {
  63. free(linkpath);
  64. free_buf_ret_null:
  65. free(buf);
  66. return NULL;
  67. }
  68. if (*linkpath != '/') {
  69. bufsize += strlen(linkpath);
  70. buf = xrealloc(buf, bufsize);
  71. lpc = bb_get_last_path_component_strip(buf);
  72. strcpy(lpc, linkpath);
  73. free(linkpath);
  74. } else {
  75. free(buf);
  76. buf = linkpath;
  77. jump_in:
  78. bufsize = strlen(buf) + 1;
  79. }
  80. }
  81. }
  82. char* FAST_FUNC xmalloc_readlink_or_warn(const char *path)
  83. {
  84. char *buf = xmalloc_readlink(path);
  85. if (!buf) {
  86. /* EINVAL => "file: Invalid argument" => puzzled user */
  87. const char *errmsg = "not a symlink";
  88. int err = errno;
  89. if (err != EINVAL)
  90. errmsg = strerror(err);
  91. bb_error_msg("%s: cannot read link: %s", path, errmsg);
  92. }
  93. return buf;
  94. }
  95. char* FAST_FUNC xmalloc_realpath(const char *path)
  96. {
  97. /* NB: uclibc also defines __GLIBC__
  98. * Therefore the test "if glibc, or uclibc >= 0.9.31" looks a bit weird:
  99. */
  100. #if defined(__GLIBC__) && \
  101. (!defined(__UCLIBC__) || UCLIBC_VERSION >= KERNEL_VERSION(0, 9, 31))
  102. /* glibc provides a non-standard extension */
  103. /* new: POSIX.1-2008 specifies this behavior as well */
  104. return realpath(path, NULL);
  105. #else
  106. char buf[PATH_MAX+1];
  107. /* on error returns NULL (xstrdup(NULL) == NULL) */
  108. return xstrdup(realpath(path, buf));
  109. #endif
  110. }
  111. char* FAST_FUNC xmalloc_realpath_coreutils(const char *path)
  112. {
  113. char *buf;
  114. errno = 0;
  115. buf = xmalloc_realpath(path);
  116. /*
  117. * There is one case when "readlink -f" and
  118. * "realpath" from coreutils succeed,
  119. * even though file does not exist, such as:
  120. * /tmp/file_does_not_exist
  121. * (the directory must exist).
  122. */
  123. if (!buf && errno == ENOENT) {
  124. char *last_slash = strrchr(path, '/');
  125. if (last_slash) {
  126. *last_slash++ = '\0';
  127. buf = xmalloc_realpath(path);
  128. if (buf) {
  129. unsigned len = strlen(buf);
  130. buf = xrealloc(buf, len + strlen(last_slash) + 2);
  131. buf[len++] = '/';
  132. strcpy(buf + len, last_slash);
  133. }
  134. }
  135. }
  136. return buf;
  137. }