xreadlink.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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(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 *target, c, *last_slash;
  125. size_t i;
  126. target = xmalloc_readlink(path);
  127. if (target) {
  128. /*
  129. * $ ln -s /bin/qwe symlink # note: /bin is a link to /usr/bin
  130. * $ readlink -f symlink
  131. * /usr/bin/qwe
  132. * $ realpath symlink
  133. * /usr/bin/qwe
  134. */
  135. if (target[0] != '/') {
  136. /*
  137. * $ ln -s target_does_not_exist symlink
  138. * $ readlink -f symlink
  139. * /CURDIR/target_does_not_exist
  140. * $ realpath symlink
  141. * /CURDIR/target_does_not_exist
  142. */
  143. char *cwd = xrealloc_getcwd_or_warn(NULL);
  144. char *tmp = concat_path_file(cwd, target);
  145. free(cwd);
  146. free(target);
  147. target = tmp;
  148. }
  149. buf = xmalloc_realpath_coreutils(target);
  150. free(target);
  151. return buf;
  152. }
  153. /* ignore leading and trailing slashes */
  154. while (path[0] == '/' && path[1] == '/')
  155. ++path;
  156. i = strlen(path) - 1;
  157. while (i > 0 && path[i] == '/')
  158. i--;
  159. c = path[i + 1];
  160. path[i + 1] = '\0';
  161. last_slash = strrchr(path, '/');
  162. if (last_slash == path)
  163. buf = xstrdup(path);
  164. else if (last_slash) {
  165. *last_slash = '\0';
  166. buf = xmalloc_realpath(path);
  167. *last_slash++ = '/';
  168. if (buf) {
  169. unsigned len = strlen(buf);
  170. buf = xrealloc(buf, len + strlen(last_slash) + 2);
  171. buf[len++] = '/';
  172. strcpy(buf + len, last_slash);
  173. }
  174. }
  175. path[i + 1] = c;
  176. }
  177. return buf;
  178. }