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