THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

271 lines
6.8KB

  1. #include "cache.h"
  2. /*
  3. * Do not use this for inspecting *tracked* content. When path is a
  4. * symlink to a directory, we do not want to say it is a directory when
  5. * dealing with tracked content in the working tree.
  6. */
  7. int is_directory(const char *path)
  8. {
  9. struct stat st;
  10. return (!stat(path, &st) && S_ISDIR(st.st_mode));
  11. }
  12. /* removes the last path component from 'path' except if 'path' is root */
  13. static void strip_last_component(struct strbuf *path)
  14. {
  15. size_t offset = offset_1st_component(path->buf);
  16. size_t len = path->len;
  17. /* Find start of the last component */
  18. while (offset < len && !is_dir_sep(path->buf[len - 1]))
  19. len--;
  20. /* Skip sequences of multiple path-separators */
  21. while (offset < len && is_dir_sep(path->buf[len - 1]))
  22. len--;
  23. strbuf_setlen(path, len);
  24. }
  25. /* get (and remove) the next component in 'remaining' and place it in 'next' */
  26. static void get_next_component(struct strbuf *next, struct strbuf *remaining)
  27. {
  28. char *start = NULL;
  29. char *end = NULL;
  30. strbuf_reset(next);
  31. /* look for the next component */
  32. /* Skip sequences of multiple path-separators */
  33. for (start = remaining->buf; is_dir_sep(*start); start++)
  34. ; /* nothing */
  35. /* Find end of the path component */
  36. for (end = start; *end && !is_dir_sep(*end); end++)
  37. ; /* nothing */
  38. strbuf_add(next, start, end - start);
  39. /* remove the component from 'remaining' */
  40. strbuf_remove(remaining, 0, end - remaining->buf);
  41. }
  42. /* copies root part from remaining to resolved, canonicalizing it on the way */
  43. static void get_root_part(struct strbuf *resolved, struct strbuf *remaining)
  44. {
  45. int offset = offset_1st_component(remaining->buf);
  46. strbuf_reset(resolved);
  47. strbuf_add(resolved, remaining->buf, offset);
  48. #ifdef GIT_WINDOWS_NATIVE
  49. convert_slashes(resolved->buf);
  50. #endif
  51. strbuf_remove(remaining, 0, offset);
  52. }
  53. /* We allow "recursive" symbolic links. Only within reason, though. */
  54. #ifndef MAXSYMLINKS
  55. #define MAXSYMLINKS 32
  56. #endif
  57. /*
  58. * Return the real path (i.e., absolute path, with symlinks resolved
  59. * and extra slashes removed) equivalent to the specified path. (If
  60. * you want an absolute path but don't mind links, use
  61. * absolute_path().) Places the resolved realpath in the provided strbuf.
  62. *
  63. * The directory part of path (i.e., everything up to the last
  64. * dir_sep) must denote a valid, existing directory, but the last
  65. * component need not exist. If die_on_error is set, then die with an
  66. * informative error message if there is a problem. Otherwise, return
  67. * NULL on errors (without generating any output).
  68. */
  69. char *strbuf_realpath(struct strbuf *resolved, const char *path,
  70. int die_on_error)
  71. {
  72. struct strbuf remaining = STRBUF_INIT;
  73. struct strbuf next = STRBUF_INIT;
  74. struct strbuf symlink = STRBUF_INIT;
  75. char *retval = NULL;
  76. int num_symlinks = 0;
  77. struct stat st;
  78. if (!*path) {
  79. if (die_on_error)
  80. die("The empty string is not a valid path");
  81. else
  82. goto error_out;
  83. }
  84. strbuf_addstr(&remaining, path);
  85. get_root_part(resolved, &remaining);
  86. if (!resolved->len) {
  87. /* relative path; can use CWD as the initial resolved path */
  88. if (strbuf_getcwd(resolved)) {
  89. if (die_on_error)
  90. die_errno("unable to get current working directory");
  91. else
  92. goto error_out;
  93. }
  94. }
  95. /* Iterate over the remaining path components */
  96. while (remaining.len > 0) {
  97. get_next_component(&next, &remaining);
  98. if (next.len == 0) {
  99. continue; /* empty component */
  100. } else if (next.len == 1 && !strcmp(next.buf, ".")) {
  101. continue; /* '.' component */
  102. } else if (next.len == 2 && !strcmp(next.buf, "..")) {
  103. /* '..' component; strip the last path component */
  104. strip_last_component(resolved);
  105. continue;
  106. }
  107. /* append the next component and resolve resultant path */
  108. if (!is_dir_sep(resolved->buf[resolved->len - 1]))
  109. strbuf_addch(resolved, '/');
  110. strbuf_addbuf(resolved, &next);
  111. if (lstat(resolved->buf, &st)) {
  112. /* error out unless this was the last component */
  113. if (errno != ENOENT || remaining.len) {
  114. if (die_on_error)
  115. die_errno("Invalid path '%s'",
  116. resolved->buf);
  117. else
  118. goto error_out;
  119. }
  120. } else if (S_ISLNK(st.st_mode)) {
  121. ssize_t len;
  122. strbuf_reset(&symlink);
  123. if (num_symlinks++ > MAXSYMLINKS) {
  124. errno = ELOOP;
  125. if (die_on_error)
  126. die("More than %d nested symlinks "
  127. "on path '%s'", MAXSYMLINKS, path);
  128. else
  129. goto error_out;
  130. }
  131. len = strbuf_readlink(&symlink, resolved->buf,
  132. st.st_size);
  133. if (len < 0) {
  134. if (die_on_error)
  135. die_errno("Invalid symlink '%s'",
  136. resolved->buf);
  137. else
  138. goto error_out;
  139. }
  140. if (is_absolute_path(symlink.buf)) {
  141. /* absolute symlink; set resolved to root */
  142. get_root_part(resolved, &symlink);
  143. } else {
  144. /*
  145. * relative symlink
  146. * strip off the last component since it will
  147. * be replaced with the contents of the symlink
  148. */
  149. strip_last_component(resolved);
  150. }
  151. /*
  152. * if there are still remaining components to resolve
  153. * then append them to symlink
  154. */
  155. if (remaining.len) {
  156. strbuf_addch(&symlink, '/');
  157. strbuf_addbuf(&symlink, &remaining);
  158. }
  159. /*
  160. * use the symlink as the remaining components that
  161. * need to be resolved
  162. */
  163. strbuf_swap(&symlink, &remaining);
  164. }
  165. }
  166. retval = resolved->buf;
  167. error_out:
  168. strbuf_release(&remaining);
  169. strbuf_release(&next);
  170. strbuf_release(&symlink);
  171. if (!retval)
  172. strbuf_reset(resolved);
  173. return retval;
  174. }
  175. /*
  176. * Resolve `path` into an absolute, cleaned-up path. The return value
  177. * comes from a shared buffer.
  178. */
  179. const char *real_path(const char *path)
  180. {
  181. static struct strbuf realpath = STRBUF_INIT;
  182. return strbuf_realpath(&realpath, path, 1);
  183. }
  184. const char *real_path_if_valid(const char *path)
  185. {
  186. static struct strbuf realpath = STRBUF_INIT;
  187. return strbuf_realpath(&realpath, path, 0);
  188. }
  189. char *real_pathdup(const char *path, int die_on_error)
  190. {
  191. struct strbuf realpath = STRBUF_INIT;
  192. char *retval = NULL;
  193. if (strbuf_realpath(&realpath, path, die_on_error))
  194. retval = strbuf_detach(&realpath, NULL);
  195. strbuf_release(&realpath);
  196. return retval;
  197. }
  198. /*
  199. * Use this to get an absolute path from a relative one. If you want
  200. * to resolve links, you should use real_path.
  201. */
  202. const char *absolute_path(const char *path)
  203. {
  204. static struct strbuf sb = STRBUF_INIT;
  205. strbuf_reset(&sb);
  206. strbuf_add_absolute_path(&sb, path);
  207. return sb.buf;
  208. }
  209. char *absolute_pathdup(const char *path)
  210. {
  211. struct strbuf sb = STRBUF_INIT;
  212. strbuf_add_absolute_path(&sb, path);
  213. return strbuf_detach(&sb, NULL);
  214. }
  215. char *prefix_filename(const char *pfx, const char *arg)
  216. {
  217. struct strbuf path = STRBUF_INIT;
  218. size_t pfx_len = pfx ? strlen(pfx) : 0;
  219. if (!pfx_len)
  220. ; /* nothing to prefix */
  221. else if (is_absolute_path(arg))
  222. pfx_len = 0;
  223. else
  224. strbuf_add(&path, pfx, pfx_len);
  225. strbuf_addstr(&path, arg);
  226. #ifdef GIT_WINDOWS_NATIVE
  227. convert_slashes(path.buf + pfx_len);
  228. #endif
  229. return strbuf_detach(&path, NULL);
  230. }