abspath.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. char *real_pathdup(const char *path, int die_on_error)
  176. {
  177. struct strbuf realpath = STRBUF_INIT;
  178. char *retval = NULL;
  179. if (strbuf_realpath(&realpath, path, die_on_error))
  180. retval = strbuf_detach(&realpath, NULL);
  181. strbuf_release(&realpath);
  182. return retval;
  183. }
  184. /*
  185. * Use this to get an absolute path from a relative one. If you want
  186. * to resolve links, you should use strbuf_realpath.
  187. */
  188. const char *absolute_path(const char *path)
  189. {
  190. static struct strbuf sb = STRBUF_INIT;
  191. strbuf_reset(&sb);
  192. strbuf_add_absolute_path(&sb, path);
  193. return sb.buf;
  194. }
  195. char *absolute_pathdup(const char *path)
  196. {
  197. struct strbuf sb = STRBUF_INIT;
  198. strbuf_add_absolute_path(&sb, path);
  199. return strbuf_detach(&sb, NULL);
  200. }
  201. char *prefix_filename(const char *pfx, const char *arg)
  202. {
  203. struct strbuf path = STRBUF_INIT;
  204. size_t pfx_len = pfx ? strlen(pfx) : 0;
  205. if (!pfx_len)
  206. ; /* nothing to prefix */
  207. else if (is_absolute_path(arg))
  208. pfx_len = 0;
  209. else
  210. strbuf_add(&path, pfx, pfx_len);
  211. strbuf_addstr(&path, arg);
  212. #ifdef GIT_WINDOWS_NATIVE
  213. convert_slashes(path.buf + pfx_len);
  214. #endif
  215. return strbuf_detach(&path, NULL);
  216. }