real_path_internal(): add comment explaining use of cwd
[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
5b8e6f85
DP
14/* We allow "recursive" symbolic links. Only within reason, though. */
15#define MAXDEPTH 5
16
e2a57aac 17/*
038e55fe
MH
18 * Return the real path (i.e., absolute path, with symlinks resolved
19 * and extra slashes removed) equivalent to the specified path. (If
20 * you want an absolute path but don't mind links, use
21 * absolute_path().) The return value is a pointer to a static
22 * buffer.
23 *
24 * The input and all intermediate paths must be shorter than MAX_PATH.
25 * The directory part of path (i.e., everything up to the last
26 * dir_sep) must denote a valid, existing directory, but the last
27 * component need not exist. If die_on_error is set, then die with an
28 * informative error message if there is a problem. Otherwise, return
29 * NULL on errors (without generating any output).
e2a57aac
CMN
30 *
31 * If path is our buffer, then return path, as it's already what the
32 * user wants.
33 */
038e55fe 34static const char *real_path_internal(const char *path, int die_on_error)
5b8e6f85
DP
35{
36 static char bufs[2][PATH_MAX + 1], *buf = bufs[0], *next_buf = bufs[1];
038e55fe 37 char *retval = NULL;
d6052abc
MH
38
39 /*
40 * If we have to temporarily chdir(), store the original CWD
41 * here so that we can chdir() back to it at the end of the
42 * function:
43 */
5b8e6f85 44 char cwd[1024] = "";
d6052abc 45
1630726e 46 int buf_index = 1;
5b8e6f85
DP
47
48 int depth = MAXDEPTH;
49 char *last_elem = NULL;
50 struct stat st;
51
1d679de5
CMN
52 /* We've already done it */
53 if (path == buf || path == next_buf)
54 return path;
55
038e55fe
MH
56 if (!*path) {
57 if (die_on_error)
58 die("The empty string is not a valid path");
59 else
60 goto error_out;
61 }
3efe5d1d 62
038e55fe
MH
63 if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX) {
64 if (die_on_error)
65 die("Too long path: %.*s", 60, path);
66 else
67 goto error_out;
68 }
5b8e6f85
DP
69
70 while (depth--) {
90b4a71c 71 if (!is_directory(buf)) {
d1c69255 72 char *last_slash = find_last_dir_sep(buf);
5b8e6f85 73 if (last_slash) {
5b8e6f85 74 last_elem = xstrdup(last_slash + 1);
f4c21e89 75 last_slash[1] = '\0';
5b8e6f85
DP
76 } else {
77 last_elem = xstrdup(buf);
78 *buf = '\0';
79 }
80 }
81
82 if (*buf) {
038e55fe
MH
83 if (!*cwd && !getcwd(cwd, sizeof(cwd))) {
84 if (die_on_error)
85 die_errno("Could not get current working directory");
86 else
87 goto error_out;
88 }
5b8e6f85 89
038e55fe
MH
90 if (chdir(buf)) {
91 if (die_on_error)
92 die_errno("Could not switch to '%s'", buf);
93 else
94 goto error_out;
95 }
96 }
97 if (!getcwd(buf, PATH_MAX)) {
98 if (die_on_error)
99 die_errno("Could not get current working directory");
100 else
101 goto error_out;
5b8e6f85 102 }
5b8e6f85
DP
103
104 if (last_elem) {
1630726e 105 size_t len = strlen(buf);
038e55fe
MH
106 if (len + strlen(last_elem) + 2 > PATH_MAX) {
107 if (die_on_error)
108 die("Too long path name: '%s/%s'",
109 buf, last_elem);
110 else
111 goto error_out;
112 }
d1c69255 113 if (len && !is_dir_sep(buf[len-1]))
ed0cb46e
NTND
114 buf[len++] = '/';
115 strcpy(buf + len, last_elem);
5b8e6f85
DP
116 free(last_elem);
117 last_elem = NULL;
118 }
119
120 if (!lstat(buf, &st) && S_ISLNK(st.st_mode)) {
1630726e 121 ssize_t len = readlink(buf, next_buf, PATH_MAX);
038e55fe
MH
122 if (len < 0) {
123 if (die_on_error)
124 die_errno("Invalid symlink '%s'", buf);
125 else
126 goto error_out;
127 }
128 if (PATH_MAX <= len) {
129 if (die_on_error)
130 die("symbolic link too long: %s", buf);
131 else
132 goto error_out;
133 }
5b8e6f85
DP
134 next_buf[len] = '\0';
135 buf = next_buf;
136 buf_index = 1 - buf_index;
137 next_buf = bufs[buf_index];
138 } else
139 break;
140 }
141
038e55fe
MH
142 retval = buf;
143error_out:
144 free(last_elem);
5b8e6f85 145 if (*cwd && chdir(cwd))
0721c314 146 die_errno ("Could not change back to '%s'", cwd);
5b8e6f85 147
038e55fe
MH
148 return retval;
149}
150
151const char *real_path(const char *path)
152{
153 return real_path_internal(path, 1);
5b8e6f85 154}
10c4c881
JS
155
156static const char *get_pwd_cwd(void)
157{
158 static char cwd[PATH_MAX + 1];
159 char *pwd;
160 struct stat cwd_stat, pwd_stat;
161 if (getcwd(cwd, PATH_MAX) == NULL)
162 return NULL;
163 pwd = getenv("PWD");
164 if (pwd && strcmp(pwd, cwd)) {
165 stat(cwd, &cwd_stat);
7d092adc
JS
166 if ((cwd_stat.st_dev || cwd_stat.st_ino) &&
167 !stat(pwd, &pwd_stat) &&
10c4c881
JS
168 pwd_stat.st_dev == cwd_stat.st_dev &&
169 pwd_stat.st_ino == cwd_stat.st_ino) {
170 strlcpy(cwd, pwd, PATH_MAX);
171 }
172 }
173 return cwd;
174}
175
e2a57aac
CMN
176/*
177 * Use this to get an absolute path from a relative one. If you want
178 * to resolve links, you should use real_path.
179 *
180 * If the path is already absolute, then return path. As the user is
181 * never meant to free the return value, we're safe.
182 */
183const char *absolute_path(const char *path)
10c4c881
JS
184{
185 static char buf[PATH_MAX + 1];
186
a0601dc1
MH
187 if (!*path) {
188 die("The empty string is not a valid path");
189 } else if (is_absolute_path(path)) {
10c4c881
JS
190 if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX)
191 die("Too long path: %.*s", 60, path);
192 } else {
b248e950
ES
193 size_t len;
194 const char *fmt;
10c4c881
JS
195 const char *cwd = get_pwd_cwd();
196 if (!cwd)
0721c314 197 die_errno("Cannot determine the current working directory");
b248e950
ES
198 len = strlen(cwd);
199 fmt = (len > 0 && is_dir_sep(cwd[len-1])) ? "%s%s" : "%s/%s";
200 if (snprintf(buf, PATH_MAX, fmt, cwd, path) >= PATH_MAX)
10c4c881
JS
201 die("Too long path: %.*s", 60, path);
202 }
203 return buf;
204}
06876284
DI
205
206/*
207 * Unlike prefix_path, this should be used if the named file does
208 * not have to interact with index entry; i.e. name of a random file
209 * on the filesystem.
210 */
211const char *prefix_filename(const char *pfx, int pfx_len, const char *arg)
212{
213 static char path[PATH_MAX];
214#ifndef WIN32
215 if (!pfx_len || is_absolute_path(arg))
216 return arg;
217 memcpy(path, pfx, pfx_len);
218 strcpy(path + pfx_len, arg);
219#else
220 char *p;
221 /* don't add prefix to absolute paths, but still replace '\' by '/' */
222 if (is_absolute_path(arg))
223 pfx_len = 0;
224 else if (pfx_len)
225 memcpy(path, pfx, pfx_len);
226 strcpy(path + pfx_len, arg);
227 for (p = path + pfx_len; *p; p++)
228 if (*p == '\\')
229 *p = '/';
230#endif
231 return path;
232}