blob: 39e06b58486e3e94e640929c27460e786533a2f8 [file] [log] [blame]
Dmitry Potapov5b8e6f82008-06-28 00:46:42 +04001#include "cache.h"
2
Junio C Hamano90b4a712008-09-09 01:27:07 -07003/*
4 * Do not use this for inspecting *tracked* content. When path is a
5 * symlink to a directory, we do not want to say it is a directory when
6 * dealing with tracked content in the working tree.
7 */
8int is_directory(const char *path)
9{
10 struct stat st;
11 return (!stat(path, &st) && S_ISDIR(st.st_mode));
12}
13
Brandon Williams05b458c2016-12-12 10:16:52 -080014/* removes the last path component from 'path' except if 'path' is root */
15static void strip_last_component(struct strbuf *path)
16{
17 size_t offset = offset_1st_component(path->buf);
18 size_t len = path->len;
19
20 /* Find start of the last component */
21 while (offset < len && !is_dir_sep(path->buf[len - 1]))
22 len--;
23 /* Skip sequences of multiple path-separators */
24 while (offset < len && is_dir_sep(path->buf[len - 1]))
25 len--;
26
27 strbuf_setlen(path, len);
28}
29
30/* get (and remove) the next component in 'remaining' and place it in 'next' */
31static void get_next_component(struct strbuf *next, struct strbuf *remaining)
32{
33 char *start = NULL;
34 char *end = NULL;
35
36 strbuf_reset(next);
37
38 /* look for the next component */
39 /* Skip sequences of multiple path-separators */
40 for (start = remaining->buf; is_dir_sep(*start); start++)
41 ; /* nothing */
42 /* Find end of the path component */
43 for (end = start; *end && !is_dir_sep(*end); end++)
44 ; /* nothing */
45
46 strbuf_add(next, start, end - start);
47 /* remove the component from 'remaining' */
48 strbuf_remove(remaining, 0, end - remaining->buf);
49}
50
Johannes Sixte9a379c2016-12-21 22:51:35 +010051/* copies root part from remaining to resolved, canonicalizing it on the way */
52static void get_root_part(struct strbuf *resolved, struct strbuf *remaining)
53{
54 int offset = offset_1st_component(remaining->buf);
55
56 strbuf_reset(resolved);
57 strbuf_add(resolved, remaining->buf, offset);
58#ifdef GIT_WINDOWS_NATIVE
59 convert_slashes(resolved->buf);
60#endif
61 strbuf_remove(remaining, 0, offset);
62}
63
Dmitry Potapov5b8e6f82008-06-28 00:46:42 +040064/* We allow "recursive" symbolic links. Only within reason, though. */
Brandon Williams7aeb81f2017-01-09 10:50:23 -080065#ifndef MAXSYMLINKS
66#define MAXSYMLINKS 32
67#endif
Dmitry Potapov5b8e6f82008-06-28 00:46:42 +040068
Carlos Martín Nietoe2a57aa2011-03-17 12:26:46 +010069/*
brian m. carlsonbe6e0da2020-12-13 00:25:28 +000070 * If set, any number of trailing components may be missing; otherwise, only one
71 * may be.
Carlos Martín Nietoe2a57aa2011-03-17 12:26:46 +010072 */
brian m. carlsonbe6e0da2020-12-13 00:25:28 +000073#define REALPATH_MANY_MISSING (1 << 0)
74/* Should we die if there's an error? */
75#define REALPATH_DIE_ON_ERROR (1 << 1)
76
77static char *strbuf_realpath_1(struct strbuf *resolved, const char *path,
78 int flags)
Dmitry Potapov5b8e6f82008-06-28 00:46:42 +040079{
Brandon Williams05b458c2016-12-12 10:16:52 -080080 struct strbuf remaining = STRBUF_INIT;
81 struct strbuf next = STRBUF_INIT;
82 struct strbuf symlink = STRBUF_INIT;
Michael Haggerty038e55f2012-10-28 17:16:20 +010083 char *retval = NULL;
Brandon Williams05b458c2016-12-12 10:16:52 -080084 int num_symlinks = 0;
Dmitry Potapov5b8e6f82008-06-28 00:46:42 +040085 struct stat st;
86
Michael Haggerty038e55f2012-10-28 17:16:20 +010087 if (!*path) {
brian m. carlsonbe6e0da2020-12-13 00:25:28 +000088 if (flags & REALPATH_DIE_ON_ERROR)
Michael Haggerty038e55f2012-10-28 17:16:20 +010089 die("The empty string is not a valid path");
90 else
91 goto error_out;
92 }
Michael Haggerty3efe5d12012-09-07 00:41:01 +020093
Johannes Sixte9a379c2016-12-21 22:51:35 +010094 strbuf_addstr(&remaining, path);
95 get_root_part(resolved, &remaining);
Dmitry Potapov5b8e6f82008-06-28 00:46:42 +040096
Johannes Sixte9a379c2016-12-21 22:51:35 +010097 if (!resolved->len) {
Brandon Williams05b458c2016-12-12 10:16:52 -080098 /* relative path; can use CWD as the initial resolved path */
Brandon Williamsa1ae4842016-12-12 10:16:53 -080099 if (strbuf_getcwd(resolved)) {
brian m. carlsonbe6e0da2020-12-13 00:25:28 +0000100 if (flags & REALPATH_DIE_ON_ERROR)
Brandon Williams05b458c2016-12-12 10:16:52 -0800101 die_errno("unable to get current working directory");
Michael Haggerty038e55f2012-10-28 17:16:20 +0100102 else
103 goto error_out;
104 }
Brandon Williams05b458c2016-12-12 10:16:52 -0800105 }
Dmitry Potapov5b8e6f82008-06-28 00:46:42 +0400106
Brandon Williams05b458c2016-12-12 10:16:52 -0800107 /* Iterate over the remaining path components */
108 while (remaining.len > 0) {
109 get_next_component(&next, &remaining);
110
111 if (next.len == 0) {
112 continue; /* empty component */
113 } else if (next.len == 1 && !strcmp(next.buf, ".")) {
114 continue; /* '.' component */
115 } else if (next.len == 2 && !strcmp(next.buf, "..")) {
116 /* '..' component; strip the last path component */
Brandon Williamsa1ae4842016-12-12 10:16:53 -0800117 strip_last_component(resolved);
Brandon Williams05b458c2016-12-12 10:16:52 -0800118 continue;
Dmitry Potapov5b8e6f82008-06-28 00:46:42 +0400119 }
120
Brandon Williams05b458c2016-12-12 10:16:52 -0800121 /* append the next component and resolve resultant path */
Brandon Williamsa1ae4842016-12-12 10:16:53 -0800122 if (!is_dir_sep(resolved->buf[resolved->len - 1]))
123 strbuf_addch(resolved, '/');
124 strbuf_addbuf(resolved, &next);
Brandon Williams05b458c2016-12-12 10:16:52 -0800125
Brandon Williamsa1ae4842016-12-12 10:16:53 -0800126 if (lstat(resolved->buf, &st)) {
Brandon Williams05b458c2016-12-12 10:16:52 -0800127 /* error out unless this was the last component */
brian m. carlsonbe6e0da2020-12-13 00:25:28 +0000128 if (errno != ENOENT ||
129 (!(flags & REALPATH_MANY_MISSING) && remaining.len)) {
130 if (flags & REALPATH_DIE_ON_ERROR)
Brandon Williams05b458c2016-12-12 10:16:52 -0800131 die_errno("Invalid path '%s'",
Brandon Williamsa1ae4842016-12-12 10:16:53 -0800132 resolved->buf);
Michael Haggerty038e55f2012-10-28 17:16:20 +0100133 else
134 goto error_out;
135 }
Brandon Williams05b458c2016-12-12 10:16:52 -0800136 } else if (S_ISLNK(st.st_mode)) {
137 ssize_t len;
138 strbuf_reset(&symlink);
139
140 if (num_symlinks++ > MAXSYMLINKS) {
Brandon Williams0b9864a2017-01-09 10:50:24 -0800141 errno = ELOOP;
142
brian m. carlsonbe6e0da2020-12-13 00:25:28 +0000143 if (flags & REALPATH_DIE_ON_ERROR)
Brandon Williams05b458c2016-12-12 10:16:52 -0800144 die("More than %d nested symlinks "
145 "on path '%s'", MAXSYMLINKS, path);
146 else
147 goto error_out;
148 }
149
Brandon Williamsa1ae4842016-12-12 10:16:53 -0800150 len = strbuf_readlink(&symlink, resolved->buf,
Brandon Williams05b458c2016-12-12 10:16:52 -0800151 st.st_size);
152 if (len < 0) {
brian m. carlsonbe6e0da2020-12-13 00:25:28 +0000153 if (flags & REALPATH_DIE_ON_ERROR)
Brandon Williams05b458c2016-12-12 10:16:52 -0800154 die_errno("Invalid symlink '%s'",
Brandon Williamsa1ae4842016-12-12 10:16:53 -0800155 resolved->buf);
Brandon Williams05b458c2016-12-12 10:16:52 -0800156 else
157 goto error_out;
158 }
159
160 if (is_absolute_path(symlink.buf)) {
161 /* absolute symlink; set resolved to root */
Johannes Sixte9a379c2016-12-21 22:51:35 +0100162 get_root_part(resolved, &symlink);
Brandon Williams05b458c2016-12-12 10:16:52 -0800163 } else {
164 /*
165 * relative symlink
166 * strip off the last component since it will
167 * be replaced with the contents of the symlink
168 */
Brandon Williamsa1ae4842016-12-12 10:16:53 -0800169 strip_last_component(resolved);
Brandon Williams05b458c2016-12-12 10:16:52 -0800170 }
171
172 /*
173 * if there are still remaining components to resolve
174 * then append them to symlink
175 */
176 if (remaining.len) {
177 strbuf_addch(&symlink, '/');
178 strbuf_addbuf(&symlink, &remaining);
179 }
180
181 /*
182 * use the symlink as the remaining components that
Ville Skyttä64127572017-06-25 13:20:41 +0300183 * need to be resolved
Brandon Williams05b458c2016-12-12 10:16:52 -0800184 */
185 strbuf_swap(&symlink, &remaining);
186 }
Dmitry Potapov5b8e6f82008-06-28 00:46:42 +0400187 }
188
Brandon Williamsa1ae4842016-12-12 10:16:53 -0800189 retval = resolved->buf;
Brandon Williams05b458c2016-12-12 10:16:52 -0800190
Michael Haggerty038e55f2012-10-28 17:16:20 +0100191error_out:
Brandon Williams05b458c2016-12-12 10:16:52 -0800192 strbuf_release(&remaining);
193 strbuf_release(&next);
194 strbuf_release(&symlink);
Dmitry Potapov5b8e6f82008-06-28 00:46:42 +0400195
Brandon Williamsa1ae4842016-12-12 10:16:53 -0800196 if (!retval)
197 strbuf_reset(resolved);
198
Michael Haggerty038e55f2012-10-28 17:16:20 +0100199 return retval;
200}
201
brian m. carlsonbe6e0da2020-12-13 00:25:28 +0000202/*
203 * Return the real path (i.e., absolute path, with symlinks resolved
204 * and extra slashes removed) equivalent to the specified path. (If
205 * you want an absolute path but don't mind links, use
206 * absolute_path().) Places the resolved realpath in the provided strbuf.
207 *
208 * The directory part of path (i.e., everything up to the last
209 * dir_sep) must denote a valid, existing directory, but the last
210 * component need not exist. If die_on_error is set, then die with an
211 * informative error message if there is a problem. Otherwise, return
212 * NULL on errors (without generating any output).
213 */
214char *strbuf_realpath(struct strbuf *resolved, const char *path,
215 int die_on_error)
216{
217 return strbuf_realpath_1(resolved, path,
218 die_on_error ? REALPATH_DIE_ON_ERROR : 0);
219}
220
221/*
222 * Just like strbuf_realpath, but allows an arbitrary number of path
223 * components to be missing.
224 */
225char *strbuf_realpath_forgiving(struct strbuf *resolved, const char *path,
226 int die_on_error)
227{
228 return strbuf_realpath_1(resolved, path,
229 ((die_on_error ? REALPATH_DIE_ON_ERROR : 0) |
230 REALPATH_MANY_MISSING));
231}
232
Johannes Schindelince83ead2017-03-08 16:43:40 +0100233char *real_pathdup(const char *path, int die_on_error)
Brandon Williams72417642016-12-12 10:16:54 -0800234{
235 struct strbuf realpath = STRBUF_INIT;
236 char *retval = NULL;
237
Johannes Schindelince83ead2017-03-08 16:43:40 +0100238 if (strbuf_realpath(&realpath, path, die_on_error))
Brandon Williams72417642016-12-12 10:16:54 -0800239 retval = strbuf_detach(&realpath, NULL);
240
241 strbuf_release(&realpath);
242
243 return retval;
244}
245
Carlos Martín Nietoe2a57aa2011-03-17 12:26:46 +0100246/*
247 * Use this to get an absolute path from a relative one. If you want
Alexandr Miloslavskiy3d7747e2020-03-10 13:11:22 +0000248 * to resolve links, you should use strbuf_realpath.
Carlos Martín Nietoe2a57aa2011-03-17 12:26:46 +0100249 */
250const char *absolute_path(const char *path)
Johannes Sixt10c4c882008-07-21 21:19:55 +0200251{
René Scharfe679eebe2014-07-28 20:33:55 +0200252 static struct strbuf sb = STRBUF_INIT;
253 strbuf_reset(&sb);
254 strbuf_add_absolute_path(&sb, path);
255 return sb.buf;
Johannes Sixt10c4c882008-07-21 21:19:55 +0200256}
Dmitry Ivankov06876282011-08-11 15:15:38 +0600257
René Scharfeb1edb402017-01-26 18:47:45 +0100258char *absolute_pathdup(const char *path)
259{
260 struct strbuf sb = STRBUF_INIT;
261 strbuf_add_absolute_path(&sb, path);
262 return strbuf_detach(&sb, NULL);
263}
264
Jeff Kinge4da43b2017-03-20 21:28:49 -0400265char *prefix_filename(const char *pfx, const char *arg)
Dmitry Ivankov06876282011-08-11 15:15:38 +0600266{
Jeff Kinge4da43b2017-03-20 21:28:49 -0400267 struct strbuf path = STRBUF_INIT;
Jeff King116fb642017-03-20 21:22:28 -0400268 size_t pfx_len = pfx ? strlen(pfx) : 0;
269
Jeff Kingaf10e8b2017-03-20 21:30:41 -0400270 if (!pfx_len)
271 ; /* nothing to prefix */
272 else if (is_absolute_path(arg))
Dmitry Ivankov06876282011-08-11 15:15:38 +0600273 pfx_len = 0;
Jeff Kingaf10e8b2017-03-20 21:30:41 -0400274 else
Antoine Pelissefc2b6212013-12-14 12:31:16 +0100275 strbuf_add(&path, pfx, pfx_len);
Jeff Kingaf10e8b2017-03-20 21:30:41 -0400276
Antoine Pelissefc2b6212013-12-14 12:31:16 +0100277 strbuf_addstr(&path, arg);
Jeff Kingaf10e8b2017-03-20 21:30:41 -0400278#ifdef GIT_WINDOWS_NATIVE
Johannes Sixt8e9b2082016-04-02 21:03:14 +0200279 convert_slashes(path.buf + pfx_len);
Dmitry Ivankov06876282011-08-11 15:15:38 +0600280#endif
Jeff Kinge4da43b2017-03-20 21:28:49 -0400281 return strbuf_detach(&path, NULL);
Dmitry Ivankov06876282011-08-11 15:15:38 +0600282}