🌐 AI搜索 & 代理 主页
blob: 5edb4e78162ca6646ef42ad2c8abc36872cd75a5 [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{
René Scharfe2fdb9ce2014-07-28 18:28:3036 static struct strbuf sb = STRBUF_INIT;
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 */
René Scharfe251277a2014-07-28 18:27:3444 struct strbuf cwd = STRBUF_INIT;
Michael Haggertyd6052ab2012-10-28 16:16:2145
Dmitry Potapov5b8e6f82008-06-27 20:46:4246 int depth = MAXDEPTH;
47 char *last_elem = NULL;
48 struct stat st;
49
Carlos Martín Nieto1d679de2011-03-16 16:06:1750 /* We've already done it */
René Scharfe2fdb9ce2014-07-28 18:28:3051 if (path == sb.buf)
Carlos Martín Nieto1d679de2011-03-16 16:06:1752 return path;
53
Michael Haggerty038e55f2012-10-28 16:16:2054 if (!*path) {
55 if (die_on_error)
56 die("The empty string is not a valid path");
57 else
58 goto error_out;
59 }
Michael Haggerty3efe5d12012-09-06 22:41:0160
René Scharfe2fdb9ce2014-07-28 18:28:3061 strbuf_reset(&sb);
62 strbuf_addstr(&sb, path);
Dmitry Potapov5b8e6f82008-06-27 20:46:4263
64 while (depth--) {
René Scharfe2fdb9ce2014-07-28 18:28:3065 if (!is_directory(sb.buf)) {
66 char *last_slash = find_last_dir_sep(sb.buf);
Dmitry Potapov5b8e6f82008-06-27 20:46:4267 if (last_slash) {
Dmitry Potapov5b8e6f82008-06-27 20:46:4268 last_elem = xstrdup(last_slash + 1);
René Scharfe2fdb9ce2014-07-28 18:28:3069 strbuf_setlen(&sb, last_slash - sb.buf + 1);
Dmitry Potapov5b8e6f82008-06-27 20:46:4270 } else {
René Scharfe2fdb9ce2014-07-28 18:28:3071 last_elem = xmemdupz(sb.buf, sb.len);
72 strbuf_reset(&sb);
Dmitry Potapov5b8e6f82008-06-27 20:46:4273 }
74 }
75
René Scharfe2fdb9ce2014-07-28 18:28:3076 if (sb.len) {
René Scharfe251277a2014-07-28 18:27:3477 if (!cwd.len && strbuf_getcwd(&cwd)) {
Michael Haggerty038e55f2012-10-28 16:16:2078 if (die_on_error)
79 die_errno("Could not get current working directory");
80 else
81 goto error_out;
82 }
Dmitry Potapov5b8e6f82008-06-27 20:46:4283
René Scharfe2fdb9ce2014-07-28 18:28:3084 if (chdir(sb.buf)) {
Michael Haggerty038e55f2012-10-28 16:16:2085 if (die_on_error)
René Scharfe2fdb9ce2014-07-28 18:28:3086 die_errno("Could not switch to '%s'",
87 sb.buf);
Michael Haggerty038e55f2012-10-28 16:16:2088 else
89 goto error_out;
90 }
Dmitry Potapov5b8e6f82008-06-27 20:46:4291 }
René Scharfe2fdb9ce2014-07-28 18:28:3092 if (strbuf_getcwd(&sb)) {
Michael Haggerty038e55f2012-10-28 16:16:2093 if (die_on_error)
94 die_errno("Could not get current working directory");
95 else
96 goto error_out;
97 }
Dmitry Potapov5b8e6f82008-06-27 20:46:4298
99 if (last_elem) {
René Scharfe2fdb9ce2014-07-28 18:28:30100 if (sb.len && !is_dir_sep(sb.buf[sb.len - 1]))
101 strbuf_addch(&sb, '/');
102 strbuf_addstr(&sb, last_elem);
Dmitry Potapov5b8e6f82008-06-27 20:46:42103 free(last_elem);
104 last_elem = NULL;
105 }
106
René Scharfe2fdb9ce2014-07-28 18:28:30107 if (!lstat(sb.buf, &st) && S_ISLNK(st.st_mode)) {
108 struct strbuf next_sb = STRBUF_INIT;
109 ssize_t len = strbuf_readlink(&next_sb, sb.buf, 0);
Michael Haggerty038e55f2012-10-28 16:16:20110 if (len < 0) {
111 if (die_on_error)
René Scharfe2fdb9ce2014-07-28 18:28:30112 die_errno("Invalid symlink '%s'",
113 sb.buf);
Michael Haggerty038e55f2012-10-28 16:16:20114 else
115 goto error_out;
116 }
René Scharfe2fdb9ce2014-07-28 18:28:30117 strbuf_swap(&sb, &next_sb);
118 strbuf_release(&next_sb);
Dmitry Potapov5b8e6f82008-06-27 20:46:42119 } else
120 break;
121 }
122
René Scharfe2fdb9ce2014-07-28 18:28:30123 retval = sb.buf;
Michael Haggerty038e55f2012-10-28 16:16:20124error_out:
125 free(last_elem);
René Scharfe251277a2014-07-28 18:27:34126 if (cwd.len && chdir(cwd.buf))
127 die_errno("Could not change back to '%s'", cwd.buf);
128 strbuf_release(&cwd);
Dmitry Potapov5b8e6f82008-06-27 20:46:42129
Michael Haggerty038e55f2012-10-28 16:16:20130 return retval;
131}
132
133const char *real_path(const char *path)
134{
135 return real_path_internal(path, 1);
Dmitry Potapov5b8e6f82008-06-27 20:46:42136}
Johannes Sixt10c4c882008-07-21 19:19:55137
Michael Haggertye3e46cd2012-10-28 16:16:22138const char *real_path_if_valid(const char *path)
139{
140 return real_path_internal(path, 0);
141}
142
Carlos Martín Nietoe2a57aa2011-03-17 11:26:46143/*
144 * Use this to get an absolute path from a relative one. If you want
145 * to resolve links, you should use real_path.
Carlos Martín Nietoe2a57aa2011-03-17 11:26:46146 */
147const char *absolute_path(const char *path)
Johannes Sixt10c4c882008-07-21 19:19:55148{
René Scharfe679eebe2014-07-28 18:33:55149 static struct strbuf sb = STRBUF_INIT;
150 strbuf_reset(&sb);
151 strbuf_add_absolute_path(&sb, path);
152 return sb.buf;
Johannes Sixt10c4c882008-07-21 19:19:55153}
Dmitry Ivankov06876282011-08-11 09:15:38154
155/*
156 * Unlike prefix_path, this should be used if the named file does
157 * not have to interact with index entry; i.e. name of a random file
158 * on the filesystem.
159 */
160const char *prefix_filename(const char *pfx, int pfx_len, const char *arg)
161{
Antoine Pelissefc2b6212013-12-14 11:31:16162 static struct strbuf path = STRBUF_INIT;
Jonathan Nieder380395d2013-05-02 19:26:08163#ifndef GIT_WINDOWS_NATIVE
Dmitry Ivankov06876282011-08-11 09:15:38164 if (!pfx_len || is_absolute_path(arg))
165 return arg;
Antoine Pelissefc2b6212013-12-14 11:31:16166 strbuf_reset(&path);
167 strbuf_add(&path, pfx, pfx_len);
168 strbuf_addstr(&path, arg);
Dmitry Ivankov06876282011-08-11 09:15:38169#else
170 char *p;
171 /* don't add prefix to absolute paths, but still replace '\' by '/' */
Antoine Pelissefc2b6212013-12-14 11:31:16172 strbuf_reset(&path);
Dmitry Ivankov06876282011-08-11 09:15:38173 if (is_absolute_path(arg))
174 pfx_len = 0;
175 else if (pfx_len)
Antoine Pelissefc2b6212013-12-14 11:31:16176 strbuf_add(&path, pfx, pfx_len);
177 strbuf_addstr(&path, arg);
178 for (p = path.buf + pfx_len; *p; p++)
Dmitry Ivankov06876282011-08-11 09:15:38179 if (*p == '\\')
180 *p = '/';
181#endif
Antoine Pelissefc2b6212013-12-14 11:31:16182 return path.buf;
Dmitry Ivankov06876282011-08-11 09:15:38183}