canonicalize.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* Return the canonical absolute name of a given file.
  2. Copyright (C) 1996-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <assert.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <unistd.h>
  19. #include <limits.h>
  20. #include <sys/stat.h>
  21. #include <errno.h>
  22. #include <stddef.h>
  23. #include <eloop-threshold.h>
  24. #include <shlib-compat.h>
  25. /* Return the canonical absolute name of file NAME. A canonical name
  26. does not contain any `.', `..' components nor any repeated path
  27. separators ('/') or symlinks. All path components must exist. If
  28. RESOLVED is null, the result is malloc'd; otherwise, if the
  29. canonical name is PATH_MAX chars or more, returns null with `errno'
  30. set to ENAMETOOLONG; if the name fits in fewer than PATH_MAX chars,
  31. returns the name in RESOLVED. If the name cannot be resolved and
  32. RESOLVED is non-NULL, it contains the path of the first component
  33. that cannot be resolved. If the path can be resolved, RESOLVED
  34. holds the same value as the value returned. */
  35. char *
  36. __realpath (const char *name, char *resolved)
  37. {
  38. char *rpath, *dest, *extra_buf = NULL;
  39. const char *start, *end, *rpath_limit;
  40. long int path_max;
  41. int num_links = 0;
  42. if (name == NULL)
  43. {
  44. /* As per Single Unix Specification V2 we must return an error if
  45. either parameter is a null pointer. We extend this to allow
  46. the RESOLVED parameter to be NULL in case the we are expected to
  47. allocate the room for the return value. */
  48. __set_errno (EINVAL);
  49. return NULL;
  50. }
  51. if (name[0] == '\0')
  52. {
  53. /* As per Single Unix Specification V2 we must return an error if
  54. the name argument points to an empty string. */
  55. __set_errno (ENOENT);
  56. return NULL;
  57. }
  58. #ifdef PATH_MAX
  59. path_max = PATH_MAX;
  60. #else
  61. path_max = __pathconf (name, _PC_PATH_MAX);
  62. if (path_max <= 0)
  63. path_max = 1024;
  64. #endif
  65. if (resolved == NULL)
  66. {
  67. rpath = malloc (path_max);
  68. if (rpath == NULL)
  69. return NULL;
  70. }
  71. else
  72. rpath = resolved;
  73. rpath_limit = rpath + path_max;
  74. if (name[0] != '/')
  75. {
  76. if (!__getcwd (rpath, path_max))
  77. {
  78. rpath[0] = '\0';
  79. goto error;
  80. }
  81. dest = __rawmemchr (rpath, '\0');
  82. }
  83. else
  84. {
  85. rpath[0] = '/';
  86. dest = rpath + 1;
  87. }
  88. for (start = end = name; *start; start = end)
  89. {
  90. struct stat64 st;
  91. int n;
  92. /* Skip sequence of multiple path-separators. */
  93. while (*start == '/')
  94. ++start;
  95. /* Find end of path component. */
  96. for (end = start; *end && *end != '/'; ++end)
  97. /* Nothing. */;
  98. if (end - start == 0)
  99. break;
  100. else if (end - start == 1 && start[0] == '.')
  101. /* nothing */;
  102. else if (end - start == 2 && start[0] == '.' && start[1] == '.')
  103. {
  104. /* Back up to previous component, ignore if at root already. */
  105. if (dest > rpath + 1)
  106. while ((--dest)[-1] != '/');
  107. }
  108. else
  109. {
  110. size_t new_size;
  111. if (dest[-1] != '/')
  112. *dest++ = '/';
  113. if (dest + (end - start) >= rpath_limit)
  114. {
  115. ptrdiff_t dest_offset = dest - rpath;
  116. char *new_rpath;
  117. if (resolved)
  118. {
  119. __set_errno (ENAMETOOLONG);
  120. if (dest > rpath + 1)
  121. dest--;
  122. *dest = '\0';
  123. goto error;
  124. }
  125. new_size = rpath_limit - rpath;
  126. if (end - start + 1 > path_max)
  127. new_size += end - start + 1;
  128. else
  129. new_size += path_max;
  130. new_rpath = (char *) realloc (rpath, new_size);
  131. if (new_rpath == NULL)
  132. goto error;
  133. rpath = new_rpath;
  134. rpath_limit = rpath + new_size;
  135. dest = rpath + dest_offset;
  136. }
  137. dest = __mempcpy (dest, start, end - start);
  138. *dest = '\0';
  139. if (__lxstat64 (_STAT_VER, rpath, &st) < 0)
  140. goto error;
  141. if (S_ISLNK (st.st_mode))
  142. {
  143. char *buf = __alloca (path_max);
  144. size_t len;
  145. if (++num_links > __eloop_threshold ())
  146. {
  147. __set_errno (ELOOP);
  148. goto error;
  149. }
  150. n = __readlink (rpath, buf, path_max - 1);
  151. if (n < 0)
  152. goto error;
  153. buf[n] = '\0';
  154. if (!extra_buf)
  155. extra_buf = __alloca (path_max);
  156. len = strlen (end);
  157. if (path_max - n <= len)
  158. {
  159. __set_errno (ENAMETOOLONG);
  160. goto error;
  161. }
  162. /* Careful here, end may be a pointer into extra_buf... */
  163. memmove (&extra_buf[n], end, len + 1);
  164. name = end = memcpy (extra_buf, buf, n);
  165. if (buf[0] == '/')
  166. dest = rpath + 1; /* It's an absolute symlink */
  167. else
  168. /* Back up to previous component, ignore if at root already: */
  169. if (dest > rpath + 1)
  170. while ((--dest)[-1] != '/');
  171. }
  172. else if (!S_ISDIR (st.st_mode) && *end != '\0')
  173. {
  174. __set_errno (ENOTDIR);
  175. goto error;
  176. }
  177. }
  178. }
  179. if (dest > rpath + 1 && dest[-1] == '/')
  180. --dest;
  181. *dest = '\0';
  182. assert (resolved == NULL || resolved == rpath);
  183. return rpath;
  184. error:
  185. assert (resolved == NULL || resolved == rpath);
  186. if (resolved == NULL)
  187. free (rpath);
  188. return NULL;
  189. }
  190. libc_hidden_def (__realpath)
  191. versioned_symbol (libc, __realpath, realpath, GLIBC_2_3);
  192. #if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_3)
  193. char *
  194. attribute_compat_text_section
  195. __old_realpath (const char *name, char *resolved)
  196. {
  197. if (resolved == NULL)
  198. {
  199. __set_errno (EINVAL);
  200. return NULL;
  201. }
  202. return __realpath (name, resolved);
  203. }
  204. compat_symbol (libc, __old_realpath, realpath, GLIBC_2_0);
  205. #endif
  206. char *
  207. __canonicalize_file_name (const char *name)
  208. {
  209. return __realpath (name, NULL);
  210. }
  211. weak_alias (__canonicalize_file_name, canonicalize_file_name)