2008-06-28 00:46:42 +04:00
|
|
|
#include "cache.h"
|
|
|
|
|
2008-09-09 12:27:07 +04:00
|
|
|
/*
|
|
|
|
* Do not use this for inspecting *tracked* content. When path is a
|
|
|
|
* symlink to a directory, we do not want to say it is a directory when
|
|
|
|
* dealing with tracked content in the working tree.
|
|
|
|
*/
|
|
|
|
int is_directory(const char *path)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
return (!stat(path, &st) && S_ISDIR(st.st_mode));
|
|
|
|
}
|
|
|
|
|
2008-06-28 00:46:42 +04:00
|
|
|
/* We allow "recursive" symbolic links. Only within reason, though. */
|
|
|
|
#define MAXDEPTH 5
|
|
|
|
|
2011-03-17 14:26:46 +03:00
|
|
|
/*
|
2012-10-28 20:16:20 +04:00
|
|
|
* Return the real path (i.e., absolute path, with symlinks resolved
|
|
|
|
* and extra slashes removed) equivalent to the specified path. (If
|
|
|
|
* you want an absolute path but don't mind links, use
|
|
|
|
* absolute_path().) The return value is a pointer to a static
|
|
|
|
* buffer.
|
|
|
|
*
|
|
|
|
* The input and all intermediate paths must be shorter than MAX_PATH.
|
|
|
|
* The directory part of path (i.e., everything up to the last
|
|
|
|
* dir_sep) must denote a valid, existing directory, but the last
|
|
|
|
* component need not exist. If die_on_error is set, then die with an
|
|
|
|
* informative error message if there is a problem. Otherwise, return
|
|
|
|
* NULL on errors (without generating any output).
|
2011-03-17 14:26:46 +03:00
|
|
|
*
|
|
|
|
* If path is our buffer, then return path, as it's already what the
|
|
|
|
* user wants.
|
|
|
|
*/
|
2012-10-28 20:16:20 +04:00
|
|
|
static const char *real_path_internal(const char *path, int die_on_error)
|
2008-06-28 00:46:42 +04:00
|
|
|
{
|
2014-07-28 22:28:30 +04:00
|
|
|
static struct strbuf sb = STRBUF_INIT;
|
2012-10-28 20:16:20 +04:00
|
|
|
char *retval = NULL;
|
2012-10-28 20:16:21 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If we have to temporarily chdir(), store the original CWD
|
|
|
|
* here so that we can chdir() back to it at the end of the
|
|
|
|
* function:
|
|
|
|
*/
|
2014-07-28 22:27:34 +04:00
|
|
|
struct strbuf cwd = STRBUF_INIT;
|
2012-10-28 20:16:21 +04:00
|
|
|
|
2008-06-28 00:46:42 +04:00
|
|
|
int depth = MAXDEPTH;
|
|
|
|
char *last_elem = NULL;
|
|
|
|
struct stat st;
|
|
|
|
|
2011-03-16 19:06:17 +03:00
|
|
|
/* We've already done it */
|
2014-07-28 22:28:30 +04:00
|
|
|
if (path == sb.buf)
|
2011-03-16 19:06:17 +03:00
|
|
|
return path;
|
|
|
|
|
2012-10-28 20:16:20 +04:00
|
|
|
if (!*path) {
|
|
|
|
if (die_on_error)
|
|
|
|
die("The empty string is not a valid path");
|
|
|
|
else
|
|
|
|
goto error_out;
|
|
|
|
}
|
2012-09-07 02:41:01 +04:00
|
|
|
|
2014-07-28 22:28:30 +04:00
|
|
|
strbuf_reset(&sb);
|
|
|
|
strbuf_addstr(&sb, path);
|
2008-06-28 00:46:42 +04:00
|
|
|
|
|
|
|
while (depth--) {
|
2014-07-28 22:28:30 +04:00
|
|
|
if (!is_directory(sb.buf)) {
|
|
|
|
char *last_slash = find_last_dir_sep(sb.buf);
|
2008-06-28 00:46:42 +04:00
|
|
|
if (last_slash) {
|
|
|
|
last_elem = xstrdup(last_slash + 1);
|
2014-07-28 22:28:30 +04:00
|
|
|
strbuf_setlen(&sb, last_slash - sb.buf + 1);
|
2008-06-28 00:46:42 +04:00
|
|
|
} else {
|
2014-07-28 22:28:30 +04:00
|
|
|
last_elem = xmemdupz(sb.buf, sb.len);
|
|
|
|
strbuf_reset(&sb);
|
2008-06-28 00:46:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-28 22:28:30 +04:00
|
|
|
if (sb.len) {
|
2014-07-28 22:27:34 +04:00
|
|
|
if (!cwd.len && strbuf_getcwd(&cwd)) {
|
2012-10-28 20:16:20 +04:00
|
|
|
if (die_on_error)
|
|
|
|
die_errno("Could not get current working directory");
|
|
|
|
else
|
|
|
|
goto error_out;
|
|
|
|
}
|
2008-06-28 00:46:42 +04:00
|
|
|
|
2014-07-28 22:28:30 +04:00
|
|
|
if (chdir(sb.buf)) {
|
2012-10-28 20:16:20 +04:00
|
|
|
if (die_on_error)
|
2014-07-28 22:28:30 +04:00
|
|
|
die_errno("Could not switch to '%s'",
|
|
|
|
sb.buf);
|
2012-10-28 20:16:20 +04:00
|
|
|
else
|
|
|
|
goto error_out;
|
|
|
|
}
|
|
|
|
}
|
2014-07-28 22:28:30 +04:00
|
|
|
if (strbuf_getcwd(&sb)) {
|
2012-10-28 20:16:20 +04:00
|
|
|
if (die_on_error)
|
|
|
|
die_errno("Could not get current working directory");
|
|
|
|
else
|
|
|
|
goto error_out;
|
2008-06-28 00:46:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (last_elem) {
|
2014-07-28 22:28:30 +04:00
|
|
|
if (sb.len && !is_dir_sep(sb.buf[sb.len - 1]))
|
|
|
|
strbuf_addch(&sb, '/');
|
|
|
|
strbuf_addstr(&sb, last_elem);
|
2008-06-28 00:46:42 +04:00
|
|
|
free(last_elem);
|
|
|
|
last_elem = NULL;
|
|
|
|
}
|
|
|
|
|
2014-07-28 22:28:30 +04:00
|
|
|
if (!lstat(sb.buf, &st) && S_ISLNK(st.st_mode)) {
|
|
|
|
struct strbuf next_sb = STRBUF_INIT;
|
|
|
|
ssize_t len = strbuf_readlink(&next_sb, sb.buf, 0);
|
2012-10-28 20:16:20 +04:00
|
|
|
if (len < 0) {
|
|
|
|
if (die_on_error)
|
2014-07-28 22:28:30 +04:00
|
|
|
die_errno("Invalid symlink '%s'",
|
|
|
|
sb.buf);
|
2012-10-28 20:16:20 +04:00
|
|
|
else
|
|
|
|
goto error_out;
|
|
|
|
}
|
2014-07-28 22:28:30 +04:00
|
|
|
strbuf_swap(&sb, &next_sb);
|
|
|
|
strbuf_release(&next_sb);
|
2008-06-28 00:46:42 +04:00
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-07-28 22:28:30 +04:00
|
|
|
retval = sb.buf;
|
2012-10-28 20:16:20 +04:00
|
|
|
error_out:
|
|
|
|
free(last_elem);
|
2014-07-28 22:27:34 +04:00
|
|
|
if (cwd.len && chdir(cwd.buf))
|
|
|
|
die_errno("Could not change back to '%s'", cwd.buf);
|
|
|
|
strbuf_release(&cwd);
|
2008-06-28 00:46:42 +04:00
|
|
|
|
2012-10-28 20:16:20 +04:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *real_path(const char *path)
|
|
|
|
{
|
|
|
|
return real_path_internal(path, 1);
|
2008-06-28 00:46:42 +04:00
|
|
|
}
|
2008-07-21 23:19:55 +04:00
|
|
|
|
2012-10-28 20:16:22 +04:00
|
|
|
const char *real_path_if_valid(const char *path)
|
|
|
|
{
|
|
|
|
return real_path_internal(path, 0);
|
|
|
|
}
|
|
|
|
|
2011-03-17 14:26:46 +03:00
|
|
|
/*
|
|
|
|
* Use this to get an absolute path from a relative one. If you want
|
|
|
|
* to resolve links, you should use real_path.
|
|
|
|
*/
|
|
|
|
const char *absolute_path(const char *path)
|
2008-07-21 23:19:55 +04:00
|
|
|
{
|
2014-07-28 22:33:55 +04:00
|
|
|
static struct strbuf sb = STRBUF_INIT;
|
|
|
|
strbuf_reset(&sb);
|
|
|
|
strbuf_add_absolute_path(&sb, path);
|
|
|
|
return sb.buf;
|
2008-07-21 23:19:55 +04:00
|
|
|
}
|
2011-08-11 13:15:38 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Unlike prefix_path, this should be used if the named file does
|
|
|
|
* not have to interact with index entry; i.e. name of a random file
|
|
|
|
* on the filesystem.
|
|
|
|
*/
|
|
|
|
const char *prefix_filename(const char *pfx, int pfx_len, const char *arg)
|
|
|
|
{
|
2013-12-14 15:31:16 +04:00
|
|
|
static struct strbuf path = STRBUF_INIT;
|
2013-05-02 23:26:08 +04:00
|
|
|
#ifndef GIT_WINDOWS_NATIVE
|
2011-08-11 13:15:38 +04:00
|
|
|
if (!pfx_len || is_absolute_path(arg))
|
|
|
|
return arg;
|
2013-12-14 15:31:16 +04:00
|
|
|
strbuf_reset(&path);
|
|
|
|
strbuf_add(&path, pfx, pfx_len);
|
|
|
|
strbuf_addstr(&path, arg);
|
2011-08-11 13:15:38 +04:00
|
|
|
#else
|
|
|
|
/* don't add prefix to absolute paths, but still replace '\' by '/' */
|
2013-12-14 15:31:16 +04:00
|
|
|
strbuf_reset(&path);
|
2011-08-11 13:15:38 +04:00
|
|
|
if (is_absolute_path(arg))
|
|
|
|
pfx_len = 0;
|
|
|
|
else if (pfx_len)
|
2013-12-14 15:31:16 +04:00
|
|
|
strbuf_add(&path, pfx, pfx_len);
|
|
|
|
strbuf_addstr(&path, arg);
|
2016-04-02 22:03:14 +03:00
|
|
|
convert_slashes(path.buf + pfx_len);
|
2011-08-11 13:15:38 +04:00
|
|
|
#endif
|
2013-12-14 15:31:16 +04:00
|
|
|
return path.buf;
|
2011-08-11 13:15:38 +04:00
|
|
|
}
|