blob: 1202cde23dbc9b27e3bee5d3198a9cec3236a288 [file] [log] [blame]
Elijah Newren0b027f62023-03-21 06:25:58 +00001#include "git-compat-util.h"
2#include "abspath.h"
3#include "strbuf.h"
Dmitry Potapov5b8e6f82008-06-28 00:46:42 +04004
Junio C Hamano90b4a712008-09-09 01:27:07 -07005/*
6 * Do not use this for inspecting *tracked* content. When path is a
7 * symlink to a directory, we do not want to say it is a directory when
8 * dealing with tracked content in the working tree.
9 */
10int is_directory(const char *path)
11{
12 struct stat st;
13 return (!stat(path, &st) && S_ISDIR(st.st_mode));
14}
15
Brandon Williams05b458c2016-12-12 10:16:52 -080016/* removes the last path component from 'path' except if 'path' is root */
17static void strip_last_component(struct strbuf *path)
18{
19 size_t offset = offset_1st_component(path->buf);
20 size_t len = path->len;
21
22 /* Find start of the last component */
23 while (offset < len && !is_dir_sep(path->buf[len - 1]))
24 len--;
25 /* Skip sequences of multiple path-separators */
26 while (offset < len && is_dir_sep(path->buf[len - 1]))
27 len--;
28
29 strbuf_setlen(path, len);
30}
31
32/* get (and remove) the next component in 'remaining' and place it in 'next' */
33static void get_next_component(struct strbuf *next, struct strbuf *remaining)
34{
35 char *start = NULL;
36 char *end = NULL;
37
38 strbuf_reset(next);
39
40 /* look for the next component */
41 /* Skip sequences of multiple path-separators */
42 for (start = remaining->buf; is_dir_sep(*start); start++)
43 ; /* nothing */
44 /* Find end of the path component */
45 for (end = start; *end && !is_dir_sep(*end); end++)
46 ; /* nothing */
47
48 strbuf_add(next, start, end - start);
49 /* remove the component from 'remaining' */
50 strbuf_remove(remaining, 0, end - remaining->buf);
51}
52
Johannes Sixte9a379c2016-12-21 22:51:35 +010053/* copies root part from remaining to resolved, canonicalizing it on the way */
54static void get_root_part(struct strbuf *resolved, struct strbuf *remaining)
55{
56 int offset = offset_1st_component(remaining->buf);
57
58 strbuf_reset(resolved);
59 strbuf_add(resolved, remaining->buf, offset);
60#ifdef GIT_WINDOWS_NATIVE
61 convert_slashes(resolved->buf);
62#endif
63 strbuf_remove(remaining, 0, offset);
64}
65
Dmitry Potapov5b8e6f82008-06-28 00:46:42 +040066/* We allow "recursive" symbolic links. Only within reason, though. */
Brandon Williams7aeb81f2017-01-09 10:50:23 -080067#ifndef MAXSYMLINKS
68#define MAXSYMLINKS 32
69#endif
Dmitry Potapov5b8e6f82008-06-28 00:46:42 +040070
Carlos Martín Nietoe2a57aa2011-03-17 12:26:46 +010071/*
brian m. carlsonbe6e0da2020-12-13 00:25:28 +000072 * If set, any number of trailing components may be missing; otherwise, only one
73 * may be.
Carlos Martín Nietoe2a57aa2011-03-17 12:26:46 +010074 */
brian m. carlsonbe6e0da2020-12-13 00:25:28 +000075#define REALPATH_MANY_MISSING (1 << 0)
76/* Should we die if there's an error? */
77#define REALPATH_DIE_ON_ERROR (1 << 1)
78
79static char *strbuf_realpath_1(struct strbuf *resolved, const char *path,
80 int flags)
Dmitry Potapov5b8e6f82008-06-28 00:46:42 +040081{
Brandon Williams05b458c2016-12-12 10:16:52 -080082 struct strbuf remaining = STRBUF_INIT;
83 struct strbuf next = STRBUF_INIT;
84 struct strbuf symlink = STRBUF_INIT;
Michael Haggerty038e55f2012-10-28 17:16:20 +010085 char *retval = NULL;
Brandon Williams05b458c2016-12-12 10:16:52 -080086 int num_symlinks = 0;
Dmitry Potapov5b8e6f82008-06-28 00:46:42 +040087 struct stat st;
88
Michael Haggerty038e55f2012-10-28 17:16:20 +010089 if (!*path) {
brian m. carlsonbe6e0da2020-12-13 00:25:28 +000090 if (flags & REALPATH_DIE_ON_ERROR)
Michael Haggerty038e55f2012-10-28 17:16:20 +010091 die("The empty string is not a valid path");
92 else
93 goto error_out;
94 }
Michael Haggerty3efe5d12012-09-07 00:41:01 +020095
Johannes Sixte9a379c2016-12-21 22:51:35 +010096 strbuf_addstr(&remaining, path);
97 get_root_part(resolved, &remaining);
Dmitry Potapov5b8e6f82008-06-28 00:46:42 +040098
Johannes Sixte9a379c2016-12-21 22:51:35 +010099 if (!resolved->len) {
Brandon Williams05b458c2016-12-12 10:16:52 -0800100 /* relative path; can use CWD as the initial resolved path */
Brandon Williamsa1ae4842016-12-12 10:16:53 -0800101 if (strbuf_getcwd(resolved)) {
brian m. carlsonbe6e0da2020-12-13 00:25:28 +0000102 if (flags & REALPATH_DIE_ON_ERROR)
Brandon Williams05b458c2016-12-12 10:16:52 -0800103 die_errno("unable to get current working directory");
Michael Haggerty038e55f2012-10-28 17:16:20 +0100104 else
105 goto error_out;
106 }
Brandon Williams05b458c2016-12-12 10:16:52 -0800107 }
Dmitry Potapov5b8e6f82008-06-28 00:46:42 +0400108
Brandon Williams05b458c2016-12-12 10:16:52 -0800109 /* Iterate over the remaining path components */
110 while (remaining.len > 0) {
111 get_next_component(&next, &remaining);
112
113 if (next.len == 0) {
114 continue; /* empty component */
115 } else if (next.len == 1 && !strcmp(next.buf, ".")) {
116 continue; /* '.' component */
117 } else if (next.len == 2 && !strcmp(next.buf, "..")) {
118 /* '..' component; strip the last path component */
Brandon Williamsa1ae4842016-12-12 10:16:53 -0800119 strip_last_component(resolved);
Brandon Williams05b458c2016-12-12 10:16:52 -0800120 continue;
Dmitry Potapov5b8e6f82008-06-28 00:46:42 +0400121 }
122
Brandon Williams05b458c2016-12-12 10:16:52 -0800123 /* append the next component and resolve resultant path */
Brandon Williamsa1ae4842016-12-12 10:16:53 -0800124 if (!is_dir_sep(resolved->buf[resolved->len - 1]))
125 strbuf_addch(resolved, '/');
126 strbuf_addbuf(resolved, &next);
Brandon Williams05b458c2016-12-12 10:16:52 -0800127
Brandon Williamsa1ae4842016-12-12 10:16:53 -0800128 if (lstat(resolved->buf, &st)) {
Brandon Williams05b458c2016-12-12 10:16:52 -0800129 /* error out unless this was the last component */
brian m. carlsonbe6e0da2020-12-13 00:25:28 +0000130 if (errno != ENOENT ||
131 (!(flags & REALPATH_MANY_MISSING) && remaining.len)) {
132 if (flags & REALPATH_DIE_ON_ERROR)
Brandon Williams05b458c2016-12-12 10:16:52 -0800133 die_errno("Invalid path '%s'",
Brandon Williamsa1ae4842016-12-12 10:16:53 -0800134 resolved->buf);
Michael Haggerty038e55f2012-10-28 17:16:20 +0100135 else
136 goto error_out;
137 }
Brandon Williams05b458c2016-12-12 10:16:52 -0800138 } else if (S_ISLNK(st.st_mode)) {
139 ssize_t len;
140 strbuf_reset(&symlink);
141
142 if (num_symlinks++ > MAXSYMLINKS) {
Brandon Williams0b9864a2017-01-09 10:50:24 -0800143 errno = ELOOP;
144
brian m. carlsonbe6e0da2020-12-13 00:25:28 +0000145 if (flags & REALPATH_DIE_ON_ERROR)
Brandon Williams05b458c2016-12-12 10:16:52 -0800146 die("More than %d nested symlinks "
147 "on path '%s'", MAXSYMLINKS, path);
148 else
149 goto error_out;
150 }
151
Brandon Williamsa1ae4842016-12-12 10:16:53 -0800152 len = strbuf_readlink(&symlink, resolved->buf,
Brandon Williams05b458c2016-12-12 10:16:52 -0800153 st.st_size);
154 if (len < 0) {
brian m. carlsonbe6e0da2020-12-13 00:25:28 +0000155 if (flags & REALPATH_DIE_ON_ERROR)
Brandon Williams05b458c2016-12-12 10:16:52 -0800156 die_errno("Invalid symlink '%s'",
Brandon Williamsa1ae4842016-12-12 10:16:53 -0800157 resolved->buf);
Brandon Williams05b458c2016-12-12 10:16:52 -0800158 else
159 goto error_out;
160 }
161
162 if (is_absolute_path(symlink.buf)) {
163 /* absolute symlink; set resolved to root */
Johannes Sixte9a379c2016-12-21 22:51:35 +0100164 get_root_part(resolved, &symlink);
Brandon Williams05b458c2016-12-12 10:16:52 -0800165 } else {
166 /*
167 * relative symlink
168 * strip off the last component since it will
169 * be replaced with the contents of the symlink
170 */
Brandon Williamsa1ae4842016-12-12 10:16:53 -0800171 strip_last_component(resolved);
Brandon Williams05b458c2016-12-12 10:16:52 -0800172 }
173
174 /*
175 * if there are still remaining components to resolve
176 * then append them to symlink
177 */
178 if (remaining.len) {
179 strbuf_addch(&symlink, '/');
180 strbuf_addbuf(&symlink, &remaining);
181 }
182
183 /*
184 * use the symlink as the remaining components that
Ville Skyttä64127572017-06-25 13:20:41 +0300185 * need to be resolved
Brandon Williams05b458c2016-12-12 10:16:52 -0800186 */
187 strbuf_swap(&symlink, &remaining);
188 }
Dmitry Potapov5b8e6f82008-06-28 00:46:42 +0400189 }
190
Brandon Williamsa1ae4842016-12-12 10:16:53 -0800191 retval = resolved->buf;
Brandon Williams05b458c2016-12-12 10:16:52 -0800192
Michael Haggerty038e55f2012-10-28 17:16:20 +0100193error_out:
Brandon Williams05b458c2016-12-12 10:16:52 -0800194 strbuf_release(&remaining);
195 strbuf_release(&next);
196 strbuf_release(&symlink);
Dmitry Potapov5b8e6f82008-06-28 00:46:42 +0400197
Brandon Williamsa1ae4842016-12-12 10:16:53 -0800198 if (!retval)
199 strbuf_reset(resolved);
200
Michael Haggerty038e55f2012-10-28 17:16:20 +0100201 return retval;
202}
203
brian m. carlsonbe6e0da2020-12-13 00:25:28 +0000204/*
205 * Return the real path (i.e., absolute path, with symlinks resolved
206 * and extra slashes removed) equivalent to the specified path. (If
207 * you want an absolute path but don't mind links, use
208 * absolute_path().) Places the resolved realpath in the provided strbuf.
209 *
210 * The directory part of path (i.e., everything up to the last
211 * dir_sep) must denote a valid, existing directory, but the last
212 * component need not exist. If die_on_error is set, then die with an
213 * informative error message if there is a problem. Otherwise, return
214 * NULL on errors (without generating any output).
215 */
216char *strbuf_realpath(struct strbuf *resolved, const char *path,
217 int die_on_error)
218{
219 return strbuf_realpath_1(resolved, path,
220 die_on_error ? REALPATH_DIE_ON_ERROR : 0);
221}
222
223/*
224 * Just like strbuf_realpath, but allows an arbitrary number of path
225 * components to be missing.
226 */
227char *strbuf_realpath_forgiving(struct strbuf *resolved, const char *path,
228 int die_on_error)
229{
230 return strbuf_realpath_1(resolved, path,
231 ((die_on_error ? REALPATH_DIE_ON_ERROR : 0) |
232 REALPATH_MANY_MISSING));
233}
234
Johannes Schindelince83ead2017-03-08 16:43:40 +0100235char *real_pathdup(const char *path, int die_on_error)
Brandon Williams72417642016-12-12 10:16:54 -0800236{
237 struct strbuf realpath = STRBUF_INIT;
238 char *retval = NULL;
239
Johannes Schindelince83ead2017-03-08 16:43:40 +0100240 if (strbuf_realpath(&realpath, path, die_on_error))
Brandon Williams72417642016-12-12 10:16:54 -0800241 retval = strbuf_detach(&realpath, NULL);
242
243 strbuf_release(&realpath);
244
245 return retval;
246}
247
Carlos Martín Nietoe2a57aa2011-03-17 12:26:46 +0100248/*
249 * Use this to get an absolute path from a relative one. If you want
Alexandr Miloslavskiy3d7747e2020-03-10 13:11:22 +0000250 * to resolve links, you should use strbuf_realpath.
Carlos Martín Nietoe2a57aa2011-03-17 12:26:46 +0100251 */
252const char *absolute_path(const char *path)
Johannes Sixt10c4c882008-07-21 21:19:55 +0200253{
René Scharfe679eebe2014-07-28 20:33:55 +0200254 static struct strbuf sb = STRBUF_INIT;
255 strbuf_reset(&sb);
256 strbuf_add_absolute_path(&sb, path);
257 return sb.buf;
Johannes Sixt10c4c882008-07-21 21:19:55 +0200258}
Dmitry Ivankov06876282011-08-11 15:15:38 +0600259
René Scharfeb1edb402017-01-26 18:47:45 +0100260char *absolute_pathdup(const char *path)
261{
262 struct strbuf sb = STRBUF_INIT;
263 strbuf_add_absolute_path(&sb, path);
264 return strbuf_detach(&sb, NULL);
265}
266
Jeff Kinge4da43b2017-03-20 21:28:49 -0400267char *prefix_filename(const char *pfx, const char *arg)
Dmitry Ivankov06876282011-08-11 15:15:38 +0600268{
Jeff Kinge4da43b2017-03-20 21:28:49 -0400269 struct strbuf path = STRBUF_INIT;
Jeff King116fb642017-03-20 21:22:28 -0400270 size_t pfx_len = pfx ? strlen(pfx) : 0;
271
Jeff Kingaf10e8b2017-03-20 21:30:41 -0400272 if (!pfx_len)
273 ; /* nothing to prefix */
274 else if (is_absolute_path(arg))
Dmitry Ivankov06876282011-08-11 15:15:38 +0600275 pfx_len = 0;
Jeff Kingaf10e8b2017-03-20 21:30:41 -0400276 else
Antoine Pelissefc2b6212013-12-14 12:31:16 +0100277 strbuf_add(&path, pfx, pfx_len);
Jeff Kingaf10e8b2017-03-20 21:30:41 -0400278
Antoine Pelissefc2b6212013-12-14 12:31:16 +0100279 strbuf_addstr(&path, arg);
Jeff Kingaf10e8b2017-03-20 21:30:41 -0400280#ifdef GIT_WINDOWS_NATIVE
Johannes Sixt8e9b2082016-04-02 21:03:14 +0200281 convert_slashes(path.buf + pfx_len);
Dmitry Ivankov06876282011-08-11 15:15:38 +0600282#endif
Jeff Kinge4da43b2017-03-20 21:28:49 -0400283 return strbuf_detach(&path, NULL);
Dmitry Ivankov06876282011-08-11 15:15:38 +0600284}
Junio C Hamanoa8bfa992023-03-04 05:27:56 -0500285
286char *prefix_filename_except_for_dash(const char *pfx, const char *arg)
287{
288 if (!strcmp(arg, "-"))
289 return xstrdup(arg);
290 return prefix_filename(pfx, arg);
291}
Calvin Wan5d1344b2023-06-06 19:48:39 +0000292
293void strbuf_add_absolute_path(struct strbuf *sb, const char *path)
294{
295 if (!*path)
296 die("The empty string is not a valid path");
297 if (!is_absolute_path(path)) {
298 struct stat cwd_stat, pwd_stat;
299 size_t orig_len = sb->len;
300 char *cwd = xgetcwd();
301 char *pwd = getenv("PWD");
302 if (pwd && strcmp(pwd, cwd) &&
303 !stat(cwd, &cwd_stat) &&
304 (cwd_stat.st_dev || cwd_stat.st_ino) &&
305 !stat(pwd, &pwd_stat) &&
306 pwd_stat.st_dev == cwd_stat.st_dev &&
307 pwd_stat.st_ino == cwd_stat.st_ino)
308 strbuf_addstr(sb, pwd);
309 else
310 strbuf_addstr(sb, cwd);
311 if (sb->len > orig_len && !is_dir_sep(sb->buf[sb->len - 1]))
312 strbuf_addch(sb, '/');
313 free(cwd);
314 }
315 strbuf_addstr(sb, path);
316}
317
318void strbuf_add_real_path(struct strbuf *sb, const char *path)
319{
320 if (sb->len) {
321 struct strbuf resolved = STRBUF_INIT;
322 strbuf_realpath(&resolved, path, 1);
323 strbuf_addbuf(sb, &resolved);
324 strbuf_release(&resolved);
325 } else
326 strbuf_realpath(sb, path, 1);
327}