🌐 AI搜索 & 代理 主页
blob: ca33558a91c5259a793fc56b571fa683e16b134f [file] [log] [blame]
Dmitry Potapov5b8e6f82008-06-27 20:46:421#include "cache.h"
2
Junio C Hamano90b4a712008-09-09 08:27:073/*
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
Dmitry Potapov5b8e6f82008-06-27 20:46:4214/* We allow "recursive" symbolic links. Only within reason, though. */
15#define MAXDEPTH 5
16
Carlos Martín Nietoe2a57aa2011-03-17 11:26:4617/*
Michael Haggerty038e55f2012-10-28 16:16:2018 * 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).
Carlos Martín Nietoe2a57aa2011-03-17 11:26:4630 *
31 * If path is our buffer, then return path, as it's already what the
32 * user wants.
33 */
Michael Haggerty038e55f2012-10-28 16:16:2034static const char *real_path_internal(const char *path, int die_on_error)
Dmitry Potapov5b8e6f82008-06-27 20:46:4235{
36 static char bufs[2][PATH_MAX + 1], *buf = bufs[0], *next_buf = bufs[1];
Michael Haggerty038e55f2012-10-28 16:16:2037 char *retval = NULL;
Michael Haggertyd6052ab2012-10-28 16:16:2138
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 */
Dmitry Potapov5b8e6f82008-06-27 20:46:4244 char cwd[1024] = "";
Michael Haggertyd6052ab2012-10-28 16:16:2145
Brandon Casey16307262009-08-27 16:16:3346 int buf_index = 1;
Dmitry Potapov5b8e6f82008-06-27 20:46:4247
48 int depth = MAXDEPTH;
49 char *last_elem = NULL;
50 struct stat st;
51
Carlos Martín Nieto1d679de2011-03-16 16:06:1752 /* We've already done it */
53 if (path == buf || path == next_buf)
54 return path;
55
Michael Haggerty038e55f2012-10-28 16:16:2056 if (!*path) {
57 if (die_on_error)
58 die("The empty string is not a valid path");
59 else
60 goto error_out;
61 }
Michael Haggerty3efe5d12012-09-06 22:41:0162
Michael Haggerty038e55f2012-10-28 16:16:2063 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 }
Dmitry Potapov5b8e6f82008-06-27 20:46:4269
70 while (depth--) {
Junio C Hamano90b4a712008-09-09 08:27:0771 if (!is_directory(buf)) {
Theo Niessinkd1c69252011-05-27 16:00:3972 char *last_slash = find_last_dir_sep(buf);
Dmitry Potapov5b8e6f82008-06-27 20:46:4273 if (last_slash) {
Dmitry Potapov5b8e6f82008-06-27 20:46:4274 last_elem = xstrdup(last_slash + 1);
Michael Haggertyf4c21e82012-09-06 22:41:0375 last_slash[1] = '\0';
Dmitry Potapov5b8e6f82008-06-27 20:46:4276 } else {
77 last_elem = xstrdup(buf);
78 *buf = '\0';
79 }
80 }
81
82 if (*buf) {
Michael Haggerty038e55f2012-10-28 16:16:2083 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 }
Dmitry Potapov5b8e6f82008-06-27 20:46:4289
Michael Haggerty038e55f2012-10-28 16:16:2090 if (chdir(buf)) {
91 if (die_on_error)
92 die_errno("Could not switch to '%s'", buf);
93 else
94 goto error_out;
95 }
Dmitry Potapov5b8e6f82008-06-27 20:46:4296 }
Michael Haggerty038e55f2012-10-28 16:16:2097 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;
102 }
Dmitry Potapov5b8e6f82008-06-27 20:46:42103
104 if (last_elem) {
Brandon Casey16307262009-08-27 16:16:33105 size_t len = strlen(buf);
Michael Haggerty038e55f2012-10-28 16:16:20106 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 }
Junio C Hamanof1e835f2013-10-15 22:25:45113 if (len && !is_dir_sep(buf[len - 1]))
Nguyễn Thái Ngọc Duyed0cb462010-02-14 15:44:41114 buf[len++] = '/';
115 strcpy(buf + len, last_elem);
Dmitry Potapov5b8e6f82008-06-27 20:46:42116 free(last_elem);
117 last_elem = NULL;
118 }
119
120 if (!lstat(buf, &st) && S_ISLNK(st.st_mode)) {
Brandon Casey16307262009-08-27 16:16:33121 ssize_t len = readlink(buf, next_buf, PATH_MAX);
Michael Haggerty038e55f2012-10-28 16:16:20122 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 }
Dmitry Potapov5b8e6f82008-06-27 20:46:42134 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
Michael Haggerty038e55f2012-10-28 16:16:20142 retval = buf;
143error_out:
144 free(last_elem);
Dmitry Potapov5b8e6f82008-06-27 20:46:42145 if (*cwd && chdir(cwd))
Felipe Contrerase46c92e2013-12-08 05:56:56146 die_errno("Could not change back to '%s'", cwd);
Dmitry Potapov5b8e6f82008-06-27 20:46:42147
Michael Haggerty038e55f2012-10-28 16:16:20148 return retval;
149}
150
151const char *real_path(const char *path)
152{
153 return real_path_internal(path, 1);
Dmitry Potapov5b8e6f82008-06-27 20:46:42154}
Johannes Sixt10c4c882008-07-21 19:19:55155
Michael Haggertye3e46cd2012-10-28 16:16:22156const char *real_path_if_valid(const char *path)
157{
158 return real_path_internal(path, 0);
159}
160
Johannes Sixt10c4c882008-07-21 19:19:55161static const char *get_pwd_cwd(void)
162{
163 static char cwd[PATH_MAX + 1];
164 char *pwd;
165 struct stat cwd_stat, pwd_stat;
166 if (getcwd(cwd, PATH_MAX) == NULL)
167 return NULL;
168 pwd = getenv("PWD");
169 if (pwd && strcmp(pwd, cwd)) {
170 stat(cwd, &cwd_stat);
Johannes Schindelin7d092ad2011-07-09 17:38:08171 if ((cwd_stat.st_dev || cwd_stat.st_ino) &&
172 !stat(pwd, &pwd_stat) &&
Johannes Sixt10c4c882008-07-21 19:19:55173 pwd_stat.st_dev == cwd_stat.st_dev &&
174 pwd_stat.st_ino == cwd_stat.st_ino) {
175 strlcpy(cwd, pwd, PATH_MAX);
176 }
177 }
178 return cwd;
179}
180
Carlos Martín Nietoe2a57aa2011-03-17 11:26:46181/*
182 * Use this to get an absolute path from a relative one. If you want
183 * to resolve links, you should use real_path.
184 *
185 * If the path is already absolute, then return path. As the user is
186 * never meant to free the return value, we're safe.
187 */
188const char *absolute_path(const char *path)
Johannes Sixt10c4c882008-07-21 19:19:55189{
190 static char buf[PATH_MAX + 1];
191
Michael Haggertya0601dc2012-09-06 22:40:59192 if (!*path) {
193 die("The empty string is not a valid path");
194 } else if (is_absolute_path(path)) {
Johannes Sixt10c4c882008-07-21 19:19:55195 if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX)
196 die("Too long path: %.*s", 60, path);
197 } else {
Eric Sunshineb248e952010-07-07 01:48:47198 size_t len;
199 const char *fmt;
Johannes Sixt10c4c882008-07-21 19:19:55200 const char *cwd = get_pwd_cwd();
201 if (!cwd)
Thomas Rast0721c312009-06-27 15:58:47202 die_errno("Cannot determine the current working directory");
Eric Sunshineb248e952010-07-07 01:48:47203 len = strlen(cwd);
Junio C Hamanof1e835f2013-10-15 22:25:45204 fmt = (len > 0 && is_dir_sep(cwd[len - 1])) ? "%s%s" : "%s/%s";
Eric Sunshineb248e952010-07-07 01:48:47205 if (snprintf(buf, PATH_MAX, fmt, cwd, path) >= PATH_MAX)
Johannes Sixt10c4c882008-07-21 19:19:55206 die("Too long path: %.*s", 60, path);
207 }
208 return buf;
209}
Dmitry Ivankov06876282011-08-11 09:15:38210
211/*
212 * Unlike prefix_path, this should be used if the named file does
213 * not have to interact with index entry; i.e. name of a random file
214 * on the filesystem.
215 */
216const char *prefix_filename(const char *pfx, int pfx_len, const char *arg)
217{
Antoine Pelissefc2b6212013-12-14 11:31:16218 static struct strbuf path = STRBUF_INIT;
Jonathan Nieder380395d2013-05-02 19:26:08219#ifndef GIT_WINDOWS_NATIVE
Dmitry Ivankov06876282011-08-11 09:15:38220 if (!pfx_len || is_absolute_path(arg))
221 return arg;
Antoine Pelissefc2b6212013-12-14 11:31:16222 strbuf_reset(&path);
223 strbuf_add(&path, pfx, pfx_len);
224 strbuf_addstr(&path, arg);
Dmitry Ivankov06876282011-08-11 09:15:38225#else
226 char *p;
227 /* don't add prefix to absolute paths, but still replace '\' by '/' */
Antoine Pelissefc2b6212013-12-14 11:31:16228 strbuf_reset(&path);
Dmitry Ivankov06876282011-08-11 09:15:38229 if (is_absolute_path(arg))
230 pfx_len = 0;
231 else if (pfx_len)
Antoine Pelissefc2b6212013-12-14 11:31:16232 strbuf_add(&path, pfx, pfx_len);
233 strbuf_addstr(&path, arg);
234 for (p = path.buf + pfx_len; *p; p++)
Dmitry Ivankov06876282011-08-11 09:15:38235 if (*p == '\\')
236 *p = '/';
237#endif
Antoine Pelissefc2b6212013-12-14 11:31:16238 return path.buf;
Dmitry Ivankov06876282011-08-11 09:15:38239}