2008-06-27 20:46:42 +00:00
|
|
|
#include "cache.h"
|
|
|
|
|
2008-09-09 08:27:07 +00: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));
|
|
|
|
}
|
|
|
|
|
2016-12-12 18:16:52 +00:00
|
|
|
/* removes the last path component from 'path' except if 'path' is root */
|
|
|
|
static void strip_last_component(struct strbuf *path)
|
|
|
|
{
|
|
|
|
size_t offset = offset_1st_component(path->buf);
|
|
|
|
size_t len = path->len;
|
|
|
|
|
|
|
|
/* Find start of the last component */
|
|
|
|
while (offset < len && !is_dir_sep(path->buf[len - 1]))
|
|
|
|
len--;
|
|
|
|
/* Skip sequences of multiple path-separators */
|
|
|
|
while (offset < len && is_dir_sep(path->buf[len - 1]))
|
|
|
|
len--;
|
|
|
|
|
|
|
|
strbuf_setlen(path, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get (and remove) the next component in 'remaining' and place it in 'next' */
|
|
|
|
static void get_next_component(struct strbuf *next, struct strbuf *remaining)
|
|
|
|
{
|
|
|
|
char *start = NULL;
|
|
|
|
char *end = NULL;
|
|
|
|
|
|
|
|
strbuf_reset(next);
|
|
|
|
|
|
|
|
/* look for the next component */
|
|
|
|
/* Skip sequences of multiple path-separators */
|
|
|
|
for (start = remaining->buf; is_dir_sep(*start); start++)
|
|
|
|
; /* nothing */
|
|
|
|
/* Find end of the path component */
|
|
|
|
for (end = start; *end && !is_dir_sep(*end); end++)
|
|
|
|
; /* nothing */
|
|
|
|
|
|
|
|
strbuf_add(next, start, end - start);
|
|
|
|
/* remove the component from 'remaining' */
|
|
|
|
strbuf_remove(remaining, 0, end - remaining->buf);
|
|
|
|
}
|
|
|
|
|
2016-12-21 21:51:35 +00:00
|
|
|
/* copies root part from remaining to resolved, canonicalizing it on the way */
|
|
|
|
static void get_root_part(struct strbuf *resolved, struct strbuf *remaining)
|
|
|
|
{
|
|
|
|
int offset = offset_1st_component(remaining->buf);
|
|
|
|
|
|
|
|
strbuf_reset(resolved);
|
|
|
|
strbuf_add(resolved, remaining->buf, offset);
|
|
|
|
#ifdef GIT_WINDOWS_NATIVE
|
|
|
|
convert_slashes(resolved->buf);
|
|
|
|
#endif
|
|
|
|
strbuf_remove(remaining, 0, offset);
|
|
|
|
}
|
|
|
|
|
2008-06-27 20:46:42 +00:00
|
|
|
/* We allow "recursive" symbolic links. Only within reason, though. */
|
2017-01-09 18:50:23 +00:00
|
|
|
#ifndef MAXSYMLINKS
|
|
|
|
#define MAXSYMLINKS 32
|
|
|
|
#endif
|
2008-06-27 20:46:42 +00:00
|
|
|
|
2011-03-17 11:26:46 +00:00
|
|
|
/*
|
2012-10-28 16:16:20 +00: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
|
2016-12-12 18:16:53 +00:00
|
|
|
* absolute_path().) Places the resolved realpath in the provided strbuf.
|
2012-10-28 16:16:20 +00:00
|
|
|
*
|
|
|
|
* 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 11:26:46 +00:00
|
|
|
*/
|
2016-12-12 18:16:53 +00:00
|
|
|
char *strbuf_realpath(struct strbuf *resolved, const char *path,
|
|
|
|
int die_on_error)
|
2008-06-27 20:46:42 +00:00
|
|
|
{
|
2016-12-12 18:16:52 +00:00
|
|
|
struct strbuf remaining = STRBUF_INIT;
|
|
|
|
struct strbuf next = STRBUF_INIT;
|
|
|
|
struct strbuf symlink = STRBUF_INIT;
|
2012-10-28 16:16:20 +00:00
|
|
|
char *retval = NULL;
|
2016-12-12 18:16:52 +00:00
|
|
|
int num_symlinks = 0;
|
2008-06-27 20:46:42 +00:00
|
|
|
struct stat st;
|
|
|
|
|
2012-10-28 16:16:20 +00:00
|
|
|
if (!*path) {
|
|
|
|
if (die_on_error)
|
|
|
|
die("The empty string is not a valid path");
|
|
|
|
else
|
|
|
|
goto error_out;
|
|
|
|
}
|
2012-09-06 22:41:01 +00:00
|
|
|
|
2016-12-21 21:51:35 +00:00
|
|
|
strbuf_addstr(&remaining, path);
|
|
|
|
get_root_part(resolved, &remaining);
|
2008-06-27 20:46:42 +00:00
|
|
|
|
2016-12-21 21:51:35 +00:00
|
|
|
if (!resolved->len) {
|
2016-12-12 18:16:52 +00:00
|
|
|
/* relative path; can use CWD as the initial resolved path */
|
2016-12-12 18:16:53 +00:00
|
|
|
if (strbuf_getcwd(resolved)) {
|
2016-12-12 18:16:52 +00:00
|
|
|
if (die_on_error)
|
|
|
|
die_errno("unable to get current working directory");
|
|
|
|
else
|
|
|
|
goto error_out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Iterate over the remaining path components */
|
|
|
|
while (remaining.len > 0) {
|
|
|
|
get_next_component(&next, &remaining);
|
|
|
|
|
|
|
|
if (next.len == 0) {
|
|
|
|
continue; /* empty component */
|
|
|
|
} else if (next.len == 1 && !strcmp(next.buf, ".")) {
|
|
|
|
continue; /* '.' component */
|
|
|
|
} else if (next.len == 2 && !strcmp(next.buf, "..")) {
|
|
|
|
/* '..' component; strip the last path component */
|
2016-12-12 18:16:53 +00:00
|
|
|
strip_last_component(resolved);
|
2016-12-12 18:16:52 +00:00
|
|
|
continue;
|
2008-06-27 20:46:42 +00:00
|
|
|
}
|
|
|
|
|
2016-12-12 18:16:52 +00:00
|
|
|
/* append the next component and resolve resultant path */
|
2016-12-12 18:16:53 +00:00
|
|
|
if (!is_dir_sep(resolved->buf[resolved->len - 1]))
|
|
|
|
strbuf_addch(resolved, '/');
|
|
|
|
strbuf_addbuf(resolved, &next);
|
2016-12-12 18:16:52 +00:00
|
|
|
|
2016-12-12 18:16:53 +00:00
|
|
|
if (lstat(resolved->buf, &st)) {
|
2016-12-12 18:16:52 +00:00
|
|
|
/* error out unless this was the last component */
|
|
|
|
if (errno != ENOENT || remaining.len) {
|
2012-10-28 16:16:20 +00:00
|
|
|
if (die_on_error)
|
2016-12-12 18:16:52 +00:00
|
|
|
die_errno("Invalid path '%s'",
|
2016-12-12 18:16:53 +00:00
|
|
|
resolved->buf);
|
2012-10-28 16:16:20 +00:00
|
|
|
else
|
|
|
|
goto error_out;
|
|
|
|
}
|
2016-12-12 18:16:52 +00:00
|
|
|
} else if (S_ISLNK(st.st_mode)) {
|
|
|
|
ssize_t len;
|
|
|
|
strbuf_reset(&symlink);
|
2008-06-27 20:46:42 +00:00
|
|
|
|
2016-12-12 18:16:52 +00:00
|
|
|
if (num_symlinks++ > MAXSYMLINKS) {
|
2017-01-09 18:50:24 +00:00
|
|
|
errno = ELOOP;
|
|
|
|
|
2012-10-28 16:16:20 +00:00
|
|
|
if (die_on_error)
|
2016-12-12 18:16:52 +00:00
|
|
|
die("More than %d nested symlinks "
|
|
|
|
"on path '%s'", MAXSYMLINKS, path);
|
2012-10-28 16:16:20 +00:00
|
|
|
else
|
|
|
|
goto error_out;
|
|
|
|
}
|
2008-06-27 20:46:42 +00:00
|
|
|
|
2016-12-12 18:16:53 +00:00
|
|
|
len = strbuf_readlink(&symlink, resolved->buf,
|
2016-12-12 18:16:52 +00:00
|
|
|
st.st_size);
|
2012-10-28 16:16:20 +00:00
|
|
|
if (len < 0) {
|
|
|
|
if (die_on_error)
|
2014-07-28 18:28:30 +00:00
|
|
|
die_errno("Invalid symlink '%s'",
|
2016-12-12 18:16:53 +00:00
|
|
|
resolved->buf);
|
2012-10-28 16:16:20 +00:00
|
|
|
else
|
|
|
|
goto error_out;
|
|
|
|
}
|
2016-12-12 18:16:52 +00:00
|
|
|
|
|
|
|
if (is_absolute_path(symlink.buf)) {
|
|
|
|
/* absolute symlink; set resolved to root */
|
2016-12-21 21:51:35 +00:00
|
|
|
get_root_part(resolved, &symlink);
|
2016-12-12 18:16:52 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* relative symlink
|
|
|
|
* strip off the last component since it will
|
|
|
|
* be replaced with the contents of the symlink
|
|
|
|
*/
|
2016-12-12 18:16:53 +00:00
|
|
|
strip_last_component(resolved);
|
2016-12-12 18:16:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if there are still remaining components to resolve
|
|
|
|
* then append them to symlink
|
|
|
|
*/
|
|
|
|
if (remaining.len) {
|
|
|
|
strbuf_addch(&symlink, '/');
|
|
|
|
strbuf_addbuf(&symlink, &remaining);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* use the symlink as the remaining components that
|
|
|
|
* need to be resloved
|
|
|
|
*/
|
|
|
|
strbuf_swap(&symlink, &remaining);
|
|
|
|
}
|
2008-06-27 20:46:42 +00:00
|
|
|
}
|
|
|
|
|
2016-12-12 18:16:53 +00:00
|
|
|
retval = resolved->buf;
|
2016-12-12 18:16:52 +00:00
|
|
|
|
2012-10-28 16:16:20 +00:00
|
|
|
error_out:
|
2016-12-12 18:16:52 +00:00
|
|
|
strbuf_release(&remaining);
|
|
|
|
strbuf_release(&next);
|
|
|
|
strbuf_release(&symlink);
|
2008-06-27 20:46:42 +00:00
|
|
|
|
2016-12-12 18:16:53 +00:00
|
|
|
if (!retval)
|
|
|
|
strbuf_reset(resolved);
|
|
|
|
|
2012-10-28 16:16:20 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *real_path(const char *path)
|
|
|
|
{
|
2016-12-12 18:16:53 +00:00
|
|
|
static struct strbuf realpath = STRBUF_INIT;
|
|
|
|
return strbuf_realpath(&realpath, path, 1);
|
2008-06-27 20:46:42 +00:00
|
|
|
}
|
2008-07-21 19:19:55 +00:00
|
|
|
|
2012-10-28 16:16:22 +00:00
|
|
|
const char *real_path_if_valid(const char *path)
|
|
|
|
{
|
2016-12-12 18:16:53 +00:00
|
|
|
static struct strbuf realpath = STRBUF_INIT;
|
|
|
|
return strbuf_realpath(&realpath, path, 0);
|
2012-10-28 16:16:22 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 15:43:40 +00:00
|
|
|
char *real_pathdup(const char *path, int die_on_error)
|
2016-12-12 18:16:54 +00:00
|
|
|
{
|
|
|
|
struct strbuf realpath = STRBUF_INIT;
|
|
|
|
char *retval = NULL;
|
|
|
|
|
2017-03-08 15:43:40 +00:00
|
|
|
if (strbuf_realpath(&realpath, path, die_on_error))
|
2016-12-12 18:16:54 +00:00
|
|
|
retval = strbuf_detach(&realpath, NULL);
|
|
|
|
|
|
|
|
strbuf_release(&realpath);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2011-03-17 11:26:46 +00: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 19:19:55 +00:00
|
|
|
{
|
2014-07-28 18:33:55 +00:00
|
|
|
static struct strbuf sb = STRBUF_INIT;
|
|
|
|
strbuf_reset(&sb);
|
|
|
|
strbuf_add_absolute_path(&sb, path);
|
|
|
|
return sb.buf;
|
2008-07-21 19:19:55 +00:00
|
|
|
}
|
2011-08-11 09:15:38 +00:00
|
|
|
|
2017-01-26 17:47:45 +00:00
|
|
|
char *absolute_pathdup(const char *path)
|
|
|
|
{
|
|
|
|
struct strbuf sb = STRBUF_INIT;
|
|
|
|
strbuf_add_absolute_path(&sb, path);
|
|
|
|
return strbuf_detach(&sb, NULL);
|
|
|
|
}
|
|
|
|
|
2017-03-21 01:28:49 +00:00
|
|
|
char *prefix_filename(const char *pfx, const char *arg)
|
2011-08-11 09:15:38 +00:00
|
|
|
{
|
2017-03-21 01:28:49 +00:00
|
|
|
struct strbuf path = STRBUF_INIT;
|
2017-03-21 01:22:28 +00:00
|
|
|
size_t pfx_len = pfx ? strlen(pfx) : 0;
|
|
|
|
|
2017-03-21 01:30:41 +00:00
|
|
|
if (!pfx_len)
|
|
|
|
; /* nothing to prefix */
|
|
|
|
else if (is_absolute_path(arg))
|
2011-08-11 09:15:38 +00:00
|
|
|
pfx_len = 0;
|
2017-03-21 01:30:41 +00:00
|
|
|
else
|
2013-12-14 11:31:16 +00:00
|
|
|
strbuf_add(&path, pfx, pfx_len);
|
2017-03-21 01:30:41 +00:00
|
|
|
|
2013-12-14 11:31:16 +00:00
|
|
|
strbuf_addstr(&path, arg);
|
2017-03-21 01:30:41 +00:00
|
|
|
#ifdef GIT_WINDOWS_NATIVE
|
2016-04-02 19:03:14 +00:00
|
|
|
convert_slashes(path.buf + pfx_len);
|
2011-08-11 09:15:38 +00:00
|
|
|
#endif
|
2017-03-21 01:28:49 +00:00
|
|
|
return strbuf_detach(&path, NULL);
|
2011-08-11 09:15:38 +00:00
|
|
|
}
|