]> git.ipfire.org Git - thirdparty/git.git/blame - abspath.c
real_path: prevent redefinition of MAXSYMLINKS
[thirdparty/git.git] / abspath.c
CommitLineData
5b8e6f85
DP
1#include "cache.h"
2
90b4a71c
JH
3/*
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
05b458c1
BW
14/* 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
e9a379c3
JS
51/* 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
5b8e6f85 64/* We allow "recursive" symbolic links. Only within reason, though. */
7aeb81f1
BW
65#ifndef MAXSYMLINKS
66#define MAXSYMLINKS 32
67#endif
5b8e6f85 68
e2a57aac 69/*
038e55fe
MH
70 * 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
a1ae4841 73 * absolute_path().) Places the resolved realpath in the provided strbuf.
038e55fe 74 *
038e55fe
MH
75 * 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).
e2a57aac 80 */
a1ae4841
BW
81char *strbuf_realpath(struct strbuf *resolved, const char *path,
82 int die_on_error)
5b8e6f85 83{
05b458c1
BW
84 struct strbuf remaining = STRBUF_INIT;
85 struct strbuf next = STRBUF_INIT;
86 struct strbuf symlink = STRBUF_INIT;
038e55fe 87 char *retval = NULL;
05b458c1 88 int num_symlinks = 0;
5b8e6f85
DP
89 struct stat st;
90
038e55fe
MH
91 if (!*path) {
92 if (die_on_error)
93 die("The empty string is not a valid path");
94 else
95 goto error_out;
96 }
3efe5d1d 97
e9a379c3
JS
98 strbuf_addstr(&remaining, path);
99 get_root_part(resolved, &remaining);
5b8e6f85 100
e9a379c3 101 if (!resolved->len) {
05b458c1 102 /* relative path; can use CWD as the initial resolved path */
a1ae4841 103 if (strbuf_getcwd(resolved)) {
05b458c1
BW
104 if (die_on_error)
105 die_errno("unable to get current working directory");
106 else
107 goto error_out;
108 }
05b458c1
BW
109 }
110
111 /* 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 */
a1ae4841 121 strip_last_component(resolved);
05b458c1 122 continue;
5b8e6f85
DP
123 }
124
05b458c1 125 /* append the next component and resolve resultant path */
a1ae4841
BW
126 if (!is_dir_sep(resolved->buf[resolved->len - 1]))
127 strbuf_addch(resolved, '/');
128 strbuf_addbuf(resolved, &next);
05b458c1 129
a1ae4841 130 if (lstat(resolved->buf, &st)) {
05b458c1
BW
131 /* error out unless this was the last component */
132 if (errno != ENOENT || remaining.len) {
038e55fe 133 if (die_on_error)
05b458c1 134 die_errno("Invalid path '%s'",
a1ae4841 135 resolved->buf);
038e55fe
MH
136 else
137 goto error_out;
138 }
05b458c1
BW
139 } else if (S_ISLNK(st.st_mode)) {
140 ssize_t len;
141 strbuf_reset(&symlink);
5b8e6f85 142
05b458c1 143 if (num_symlinks++ > MAXSYMLINKS) {
038e55fe 144 if (die_on_error)
05b458c1
BW
145 die("More than %d nested symlinks "
146 "on path '%s'", MAXSYMLINKS, path);
038e55fe
MH
147 else
148 goto error_out;
149 }
5b8e6f85 150
a1ae4841 151 len = strbuf_readlink(&symlink, resolved->buf,
05b458c1 152 st.st_size);
038e55fe
MH
153 if (len < 0) {
154 if (die_on_error)
2fdb9ce0 155 die_errno("Invalid symlink '%s'",
a1ae4841 156 resolved->buf);
038e55fe
MH
157 else
158 goto error_out;
159 }
05b458c1
BW
160
161 if (is_absolute_path(symlink.buf)) {
162 /* absolute symlink; set resolved to root */
e9a379c3 163 get_root_part(resolved, &symlink);
05b458c1
BW
164 } else {
165 /*
166 * relative symlink
167 * strip off the last component since it will
168 * be replaced with the contents of the symlink
169 */
a1ae4841 170 strip_last_component(resolved);
05b458c1
BW
171 }
172
173 /*
174 * if there are still remaining components to resolve
175 * then append them to symlink
176 */
177 if (remaining.len) {
178 strbuf_addch(&symlink, '/');
179 strbuf_addbuf(&symlink, &remaining);
180 }
181
182 /*
183 * use the symlink as the remaining components that
184 * need to be resloved
185 */
186 strbuf_swap(&symlink, &remaining);
187 }
5b8e6f85
DP
188 }
189
a1ae4841 190 retval = resolved->buf;
05b458c1 191
038e55fe 192error_out:
05b458c1
BW
193 strbuf_release(&remaining);
194 strbuf_release(&next);
195 strbuf_release(&symlink);
5b8e6f85 196
a1ae4841
BW
197 if (!retval)
198 strbuf_reset(resolved);
199
038e55fe
MH
200 return retval;
201}
202
203const char *real_path(const char *path)
204{
a1ae4841
BW
205 static struct strbuf realpath = STRBUF_INIT;
206 return strbuf_realpath(&realpath, path, 1);
5b8e6f85 207}
10c4c881 208
e3e46cdb
MH
209const char *real_path_if_valid(const char *path)
210{
a1ae4841
BW
211 static struct strbuf realpath = STRBUF_INIT;
212 return strbuf_realpath(&realpath, path, 0);
e3e46cdb
MH
213}
214
72417640
BW
215char *real_pathdup(const char *path)
216{
217 struct strbuf realpath = STRBUF_INIT;
218 char *retval = NULL;
219
220 if (strbuf_realpath(&realpath, path, 0))
221 retval = strbuf_detach(&realpath, NULL);
222
223 strbuf_release(&realpath);
224
225 return retval;
226}
227
e2a57aac
CMN
228/*
229 * Use this to get an absolute path from a relative one. If you want
230 * to resolve links, you should use real_path.
e2a57aac
CMN
231 */
232const char *absolute_path(const char *path)
10c4c881 233{
679eebe2
RS
234 static struct strbuf sb = STRBUF_INIT;
235 strbuf_reset(&sb);
236 strbuf_add_absolute_path(&sb, path);
237 return sb.buf;
10c4c881 238}
06876284
DI
239
240/*
241 * Unlike prefix_path, this should be used if the named file does
242 * not have to interact with index entry; i.e. name of a random file
243 * on the filesystem.
244 */
245const char *prefix_filename(const char *pfx, int pfx_len, const char *arg)
246{
fc2b6214 247 static struct strbuf path = STRBUF_INIT;
380395d0 248#ifndef GIT_WINDOWS_NATIVE
06876284
DI
249 if (!pfx_len || is_absolute_path(arg))
250 return arg;
fc2b6214
AP
251 strbuf_reset(&path);
252 strbuf_add(&path, pfx, pfx_len);
253 strbuf_addstr(&path, arg);
06876284 254#else
06876284 255 /* don't add prefix to absolute paths, but still replace '\' by '/' */
fc2b6214 256 strbuf_reset(&path);
06876284
DI
257 if (is_absolute_path(arg))
258 pfx_len = 0;
259 else if (pfx_len)
fc2b6214
AP
260 strbuf_add(&path, pfx, pfx_len);
261 strbuf_addstr(&path, arg);
8e9b2080 262 convert_slashes(path.buf + pfx_len);
06876284 263#endif
fc2b6214 264 return path.buf;
06876284 265}