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.
8 int is_directory(const char *path
)
11 return (!stat(path
, &st
) && S_ISDIR(st
.st_mode
));
14 /* We allow "recursive" symbolic links. Only within reason, though. */
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
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).
31 * If path is our buffer, then return path, as it's already what the
34 static const char *real_path_internal(const char *path
, int die_on_error
)
36 static char bufs
[2][PATH_MAX
+ 1], *buf
= bufs
[0], *next_buf
= bufs
[1];
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
49 char *last_elem
= NULL
;
52 /* We've already done it */
53 if (path
== buf
|| path
== next_buf
)
58 die("The empty string is not a valid path");
63 if (strlcpy(buf
, path
, PATH_MAX
) >= PATH_MAX
) {
65 die("Too long path: %.*s", 60, path
);
71 if (!is_directory(buf
)) {
72 char *last_slash
= find_last_dir_sep(buf
);
74 last_elem
= xstrdup(last_slash
+ 1);
77 last_elem
= xstrdup(buf
);
83 if (!*cwd
&& !getcwd(cwd
, sizeof(cwd
))) {
85 die_errno("Could not get current working directory");
92 die_errno("Could not switch to '%s'", buf
);
97 if (!getcwd(buf
, PATH_MAX
)) {
99 die_errno("Could not get current working directory");
105 size_t len
= strlen(buf
);
106 if (len
+ strlen(last_elem
) + 2 > PATH_MAX
) {
108 die("Too long path name: '%s/%s'",
113 if (len
&& !is_dir_sep(buf
[len
- 1]))
115 strcpy(buf
+ len
, last_elem
);
120 if (!lstat(buf
, &st
) && S_ISLNK(st
.st_mode
)) {
121 ssize_t len
= readlink(buf
, next_buf
, PATH_MAX
);
124 die_errno("Invalid symlink '%s'", buf
);
128 if (PATH_MAX
<= len
) {
130 die("symbolic link too long: %s", buf
);
134 next_buf
[len
] = '\0';
136 buf_index
= 1 - buf_index
;
137 next_buf
= bufs
[buf_index
];
145 if (*cwd
&& chdir(cwd
))
146 die_errno("Could not change back to '%s'", cwd
);
151 const char *real_path(const char *path
)
153 return real_path_internal(path
, 1);
156 const char *real_path_if_valid(const char *path
)
158 return real_path_internal(path
, 0);
161 static const char *get_pwd_cwd(void)
163 static char cwd
[PATH_MAX
+ 1];
165 struct stat cwd_stat
, pwd_stat
;
166 if (getcwd(cwd
, PATH_MAX
) == NULL
)
169 if (pwd
&& strcmp(pwd
, cwd
)) {
170 stat(cwd
, &cwd_stat
);
171 if ((cwd_stat
.st_dev
|| cwd_stat
.st_ino
) &&
172 !stat(pwd
, &pwd_stat
) &&
173 pwd_stat
.st_dev
== cwd_stat
.st_dev
&&
174 pwd_stat
.st_ino
== cwd_stat
.st_ino
) {
175 strlcpy(cwd
, pwd
, PATH_MAX
);
182 * Use this to get an absolute path from a relative one. If you want
183 * to resolve links, you should use real_path.
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.
188 const char *absolute_path(const char *path
)
190 static char buf
[PATH_MAX
+ 1];
193 die("The empty string is not a valid path");
194 } else if (is_absolute_path(path
)) {
195 if (strlcpy(buf
, path
, PATH_MAX
) >= PATH_MAX
)
196 die("Too long path: %.*s", 60, path
);
200 const char *cwd
= get_pwd_cwd();
202 die_errno("Cannot determine the current working directory");
204 fmt
= (len
> 0 && is_dir_sep(cwd
[len
- 1])) ? "%s%s" : "%s/%s";
205 if (snprintf(buf
, PATH_MAX
, fmt
, cwd
, path
) >= PATH_MAX
)
206 die("Too long path: %.*s", 60, path
);
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
216 const char *prefix_filename(const char *pfx
, int pfx_len
, const char *arg
)
218 static char path
[PATH_MAX
];
219 #ifndef GIT_WINDOWS_NATIVE
220 if (!pfx_len
|| is_absolute_path(arg
))
222 memcpy(path
, pfx
, pfx_len
);
223 strcpy(path
+ pfx_len
, arg
);
226 /* don't add prefix to absolute paths, but still replace '\' by '/' */
227 if (is_absolute_path(arg
))
230 memcpy(path
, pfx
, pfx_len
);
231 strcpy(path
+ pfx_len
, arg
);
232 for (p
= path
+ pfx_len
; *p
; p
++)