blob: 98579853299427ff906434fe56288a73b9d19b21 [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/*
Michael Haggerty038e55f2012-10-28 17:16:20 +010070 * Return the real path (i.e., absolute path, with symlinks resolved
71 * and extra slashes removed) equivalent to the specified path. (If
72 * you want an absolute path but don't mind links, use
Brandon Williamsa1ae4842016-12-12 10:16:53 -080073 * absolute_path().) Places the resolved realpath in the provided strbuf.
Michael Haggerty038e55f2012-10-28 17:16:20 +010074 *
Michael Haggerty038e55f2012-10-28 17:16:20 +010075 * The directory part of path (i.e., everything up to the last
76 * dir_sep) must denote a valid, existing directory, but the last
77 * component need not exist. If die_on_error is set, then die with an
78 * informative error message if there is a problem. Otherwise, return
79 * NULL on errors (without generating any output).
Carlos Martín Nietoe2a57aa2011-03-17 12:26:46 +010080 */
Brandon Williamsa1ae4842016-12-12 10:16:53 -080081char *strbuf_realpath(struct strbuf *resolved, const char *path,
82 int die_on_error)
Dmitry Potapov5b8e6f82008-06-28 00:46:42 +040083{
Brandon Williams05b458c2016-12-12 10:16:52 -080084 struct strbuf remaining = STRBUF_INIT;
85 struct strbuf next = STRBUF_INIT;
86 struct strbuf symlink = STRBUF_INIT;
Michael Haggerty038e55f2012-10-28 17:16:20 +010087 char *retval = NULL;
Brandon Williams05b458c2016-12-12 10:16:52 -080088 int num_symlinks = 0;
Dmitry Potapov5b8e6f82008-06-28 00:46:42 +040089 struct stat st;
90
Michael Haggerty038e55f2012-10-28 17:16:20 +010091 if (!*path) {
92 if (die_on_error)
93 die("The empty string is not a valid path");
94 else
95 goto error_out;
96 }
Michael Haggerty3efe5d12012-09-07 00:41:01 +020097
Johannes Sixte9a379c2016-12-21 22:51:35 +010098 strbuf_addstr(&remaining, path);
99 get_root_part(resolved, &remaining);
Dmitry Potapov5b8e6f82008-06-28 00:46:42 +0400100
Johannes Sixte9a379c2016-12-21 22:51:35 +0100101 if (!resolved->len) {
Brandon Williams05b458c2016-12-12 10:16:52 -0800102 /* relative path; can use CWD as the initial resolved path */
Brandon Williamsa1ae4842016-12-12 10:16:53 -0800103 if (strbuf_getcwd(resolved)) {
Michael Haggerty038e55f2012-10-28 17:16:20 +0100104 if (die_on_error)
Brandon Williams05b458c2016-12-12 10:16:52 -0800105 die_errno("unable to get current working directory");
Michael Haggerty038e55f2012-10-28 17:16:20 +0100106 else
107 goto error_out;
108 }
Brandon Williams05b458c2016-12-12 10:16:52 -0800109 }
Dmitry Potapov5b8e6f82008-06-28 00:46:42 +0400110
Brandon Williams05b458c2016-12-12 10:16:52 -0800111 /* Iterate over the remaining path components */
112 while (remaining.len > 0) {
113 get_next_component(&next, &remaining);
114
115 if (next.len == 0) {
116 continue; /* empty component */
117 } else if (next.len == 1 && !strcmp(next.buf, ".")) {
118 continue; /* '.' component */
119 } else if (next.len == 2 && !strcmp(next.buf, "..")) {
120 /* '..' component; strip the last path component */
Brandon Williamsa1ae4842016-12-12 10:16:53 -0800121 strip_last_component(resolved);
Brandon Williams05b458c2016-12-12 10:16:52 -0800122 continue;
Dmitry Potapov5b8e6f82008-06-28 00:46:42 +0400123 }
124
Brandon Williams05b458c2016-12-12 10:16:52 -0800125 /* append the next component and resolve resultant path */
Brandon Williamsa1ae4842016-12-12 10:16:53 -0800126 if (!is_dir_sep(resolved->buf[resolved->len - 1]))
127 strbuf_addch(resolved, '/');
128 strbuf_addbuf(resolved, &next);
Brandon Williams05b458c2016-12-12 10:16:52 -0800129
Brandon Williamsa1ae4842016-12-12 10:16:53 -0800130 if (lstat(resolved->buf, &st)) {
Brandon Williams05b458c2016-12-12 10:16:52 -0800131 /* error out unless this was the last component */
132 if (errno != ENOENT || remaining.len) {
Michael Haggerty038e55f2012-10-28 17:16:20 +0100133 if (die_on_error)
Brandon Williams05b458c2016-12-12 10:16:52 -0800134 die_errno("Invalid path '%s'",
Brandon Williamsa1ae4842016-12-12 10:16:53 -0800135 resolved->buf);
Michael Haggerty038e55f2012-10-28 17:16:20 +0100136 else
137 goto error_out;
138 }
Brandon Williams05b458c2016-12-12 10:16:52 -0800139 } else if (S_ISLNK(st.st_mode)) {
140 ssize_t len;
141 strbuf_reset(&symlink);
142
143 if (num_symlinks++ > MAXSYMLINKS) {
Brandon Williams0b9864a2017-01-09 10:50:24 -0800144 errno = ELOOP;
145
Brandon Williams05b458c2016-12-12 10:16:52 -0800146 if (die_on_error)
147 die("More than %d nested symlinks "
148 "on path '%s'", MAXSYMLINKS, path);
149 else
150 goto error_out;
151 }
152
Brandon Williamsa1ae4842016-12-12 10:16:53 -0800153 len = strbuf_readlink(&symlink, resolved->buf,
Brandon Williams05b458c2016-12-12 10:16:52 -0800154 st.st_size);
155 if (len < 0) {
156 if (die_on_error)
157 die_errno("Invalid symlink '%s'",
Brandon Williamsa1ae4842016-12-12 10:16:53 -0800158 resolved->buf);
Brandon Williams05b458c2016-12-12 10:16:52 -0800159 else
160 goto error_out;
161 }
162
163 if (is_absolute_path(symlink.buf)) {
164 /* absolute symlink; set resolved to root */
Johannes Sixte9a379c2016-12-21 22:51:35 +0100165 get_root_part(resolved, &symlink);
Brandon Williams05b458c2016-12-12 10:16:52 -0800166 } else {
167 /*
168 * relative symlink
169 * strip off the last component since it will
170 * be replaced with the contents of the symlink
171 */
Brandon Williamsa1ae4842016-12-12 10:16:53 -0800172 strip_last_component(resolved);
Brandon Williams05b458c2016-12-12 10:16:52 -0800173 }
174
175 /*
176 * if there are still remaining components to resolve
177 * then append them to symlink
178 */
179 if (remaining.len) {
180 strbuf_addch(&symlink, '/');
181 strbuf_addbuf(&symlink, &remaining);
182 }
183
184 /*
185 * use the symlink as the remaining components that
Ville Skyttä64127572017-06-25 13:20:41 +0300186 * need to be resolved
Brandon Williams05b458c2016-12-12 10:16:52 -0800187 */
188 strbuf_swap(&symlink, &remaining);
189 }
Dmitry Potapov5b8e6f82008-06-28 00:46:42 +0400190 }
191
Brandon Williamsa1ae4842016-12-12 10:16:53 -0800192 retval = resolved->buf;
Brandon Williams05b458c2016-12-12 10:16:52 -0800193
Michael Haggerty038e55f2012-10-28 17:16:20 +0100194error_out:
Brandon Williams05b458c2016-12-12 10:16:52 -0800195 strbuf_release(&remaining);
196 strbuf_release(&next);
197 strbuf_release(&symlink);
Dmitry Potapov5b8e6f82008-06-28 00:46:42 +0400198
Brandon Williamsa1ae4842016-12-12 10:16:53 -0800199 if (!retval)
200 strbuf_reset(resolved);
201
Michael Haggerty038e55f2012-10-28 17:16:20 +0100202 return retval;
203}
204
Han-Wen Nienhuysd83d8462017-09-26 13:21:48 +0200205/*
206 * Resolve `path` into an absolute, cleaned-up path. The return value
207 * comes from a shared buffer.
208 */
Michael Haggerty038e55f2012-10-28 17:16:20 +0100209const char *real_path(const char *path)
210{
Brandon Williamsa1ae4842016-12-12 10:16:53 -0800211 static struct strbuf realpath = STRBUF_INIT;
212 return strbuf_realpath(&realpath, path, 1);
Dmitry Potapov5b8e6f82008-06-28 00:46:42 +0400213}
Johannes Sixt10c4c882008-07-21 21:19:55 +0200214
Michael Haggertye3e46cd2012-10-28 17:16:22 +0100215const char *real_path_if_valid(const char *path)
216{
Brandon Williamsa1ae4842016-12-12 10:16:53 -0800217 static struct strbuf realpath = STRBUF_INIT;
218 return strbuf_realpath(&realpath, path, 0);
Michael Haggertye3e46cd2012-10-28 17:16:22 +0100219}
220
Johannes Schindelince83ead2017-03-08 16:43:40 +0100221char *real_pathdup(const char *path, int die_on_error)
Brandon Williams72417642016-12-12 10:16:54 -0800222{
223 struct strbuf realpath = STRBUF_INIT;
224 char *retval = NULL;
225
Johannes Schindelince83ead2017-03-08 16:43:40 +0100226 if (strbuf_realpath(&realpath, path, die_on_error))
Brandon Williams72417642016-12-12 10:16:54 -0800227 retval = strbuf_detach(&realpath, NULL);
228
229 strbuf_release(&realpath);
230
231 return retval;
232}
233
Carlos Martín Nietoe2a57aa2011-03-17 12:26:46 +0100234/*
235 * Use this to get an absolute path from a relative one. If you want
236 * to resolve links, you should use real_path.
Carlos Martín Nietoe2a57aa2011-03-17 12:26:46 +0100237 */
238const char *absolute_path(const char *path)
Johannes Sixt10c4c882008-07-21 21:19:55 +0200239{
René Scharfe679eebe2014-07-28 20:33:55 +0200240 static struct strbuf sb = STRBUF_INIT;
241 strbuf_reset(&sb);
242 strbuf_add_absolute_path(&sb, path);
243 return sb.buf;
Johannes Sixt10c4c882008-07-21 21:19:55 +0200244}
Dmitry Ivankov06876282011-08-11 15:15:38 +0600245
René Scharfeb1edb402017-01-26 18:47:45 +0100246char *absolute_pathdup(const char *path)
247{
248 struct strbuf sb = STRBUF_INIT;
249 strbuf_add_absolute_path(&sb, path);
250 return strbuf_detach(&sb, NULL);
251}
252
Jeff Kinge4da43b2017-03-20 21:28:49 -0400253char *prefix_filename(const char *pfx, const char *arg)
Dmitry Ivankov06876282011-08-11 15:15:38 +0600254{
Jeff Kinge4da43b2017-03-20 21:28:49 -0400255 struct strbuf path = STRBUF_INIT;
Jeff King116fb642017-03-20 21:22:28 -0400256 size_t pfx_len = pfx ? strlen(pfx) : 0;
257
Jeff Kingaf10e8b2017-03-20 21:30:41 -0400258 if (!pfx_len)
259 ; /* nothing to prefix */
260 else if (is_absolute_path(arg))
Dmitry Ivankov06876282011-08-11 15:15:38 +0600261 pfx_len = 0;
Jeff Kingaf10e8b2017-03-20 21:30:41 -0400262 else
Antoine Pelissefc2b6212013-12-14 12:31:16 +0100263 strbuf_add(&path, pfx, pfx_len);
Jeff Kingaf10e8b2017-03-20 21:30:41 -0400264
Antoine Pelissefc2b6212013-12-14 12:31:16 +0100265 strbuf_addstr(&path, arg);
Jeff Kingaf10e8b2017-03-20 21:30:41 -0400266#ifdef GIT_WINDOWS_NATIVE
Johannes Sixt8e9b2082016-04-02 21:03:14 +0200267 convert_slashes(path.buf + pfx_len);
Dmitry Ivankov06876282011-08-11 15:15:38 +0600268#endif
Jeff Kinge4da43b2017-03-20 21:28:49 -0400269 return strbuf_detach(&path, NULL);
Dmitry Ivankov06876282011-08-11 15:15:38 +0600270}