real_path: resolve symlinks by hand
The current implementation of real_path uses chdir() in order to resolve symlinks. Unfortunately this isn't thread-safe as chdir() affects a process as a whole and not just an individual thread. Instead perform the symlink resolution by hand so that the calls to chdir() can be removed, making real_path one step closer to being reentrant. Signed-off-by: Brandon Williams <bmwill@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>maint
parent
8d7a455ed5
commit
05b458c104
194
abspath.c
194
abspath.c
|
@ -11,8 +11,45 @@ int is_directory(const char *path)
|
||||||
return (!stat(path, &st) && S_ISDIR(st.st_mode));
|
return (!stat(path, &st) && S_ISDIR(st.st_mode));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 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);
|
||||||
|
}
|
||||||
|
|
||||||
/* We allow "recursive" symbolic links. Only within reason, though. */
|
/* We allow "recursive" symbolic links. Only within reason, though. */
|
||||||
#define MAXDEPTH 5
|
#define MAXSYMLINKS 5
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Return the real path (i.e., absolute path, with symlinks resolved
|
* Return the real path (i.e., absolute path, with symlinks resolved
|
||||||
|
@ -21,7 +58,6 @@ int is_directory(const char *path)
|
||||||
* absolute_path().) The return value is a pointer to a static
|
* absolute_path().) The return value is a pointer to a static
|
||||||
* buffer.
|
* 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
|
* The directory part of path (i.e., everything up to the last
|
||||||
* dir_sep) must denote a valid, existing directory, but 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
|
* component need not exist. If die_on_error is set, then die with an
|
||||||
|
@ -33,22 +69,16 @@ int is_directory(const char *path)
|
||||||
*/
|
*/
|
||||||
static const char *real_path_internal(const char *path, int die_on_error)
|
static const char *real_path_internal(const char *path, int die_on_error)
|
||||||
{
|
{
|
||||||
static struct strbuf sb = STRBUF_INIT;
|
static struct strbuf resolved = STRBUF_INIT;
|
||||||
|
struct strbuf remaining = STRBUF_INIT;
|
||||||
|
struct strbuf next = STRBUF_INIT;
|
||||||
|
struct strbuf symlink = STRBUF_INIT;
|
||||||
char *retval = NULL;
|
char *retval = NULL;
|
||||||
|
int num_symlinks = 0;
|
||||||
/*
|
|
||||||
* 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:
|
|
||||||
*/
|
|
||||||
struct strbuf cwd = STRBUF_INIT;
|
|
||||||
|
|
||||||
int depth = MAXDEPTH;
|
|
||||||
char *last_elem = NULL;
|
|
||||||
struct stat st;
|
struct stat st;
|
||||||
|
|
||||||
/* We've already done it */
|
/* We've already done it */
|
||||||
if (path == sb.buf)
|
if (path == resolved.buf)
|
||||||
return path;
|
return path;
|
||||||
|
|
||||||
if (!*path) {
|
if (!*path) {
|
||||||
|
@ -58,74 +88,112 @@ static const char *real_path_internal(const char *path, int die_on_error)
|
||||||
goto error_out;
|
goto error_out;
|
||||||
}
|
}
|
||||||
|
|
||||||
strbuf_reset(&sb);
|
strbuf_reset(&resolved);
|
||||||
strbuf_addstr(&sb, path);
|
|
||||||
|
|
||||||
while (depth--) {
|
if (is_absolute_path(path)) {
|
||||||
if (!is_directory(sb.buf)) {
|
/* absolute path; start with only root as being resolved */
|
||||||
char *last_slash = find_last_dir_sep(sb.buf);
|
int offset = offset_1st_component(path);
|
||||||
if (last_slash) {
|
strbuf_add(&resolved, path, offset);
|
||||||
last_elem = xstrdup(last_slash + 1);
|
strbuf_addstr(&remaining, path + offset);
|
||||||
strbuf_setlen(&sb, last_slash - sb.buf + 1);
|
|
||||||
} else {
|
} else {
|
||||||
last_elem = xmemdupz(sb.buf, sb.len);
|
/* relative path; can use CWD as the initial resolved path */
|
||||||
strbuf_reset(&sb);
|
if (strbuf_getcwd(&resolved)) {
|
||||||
|
if (die_on_error)
|
||||||
|
die_errno("unable to get current working directory");
|
||||||
|
else
|
||||||
|
goto error_out;
|
||||||
}
|
}
|
||||||
|
strbuf_addstr(&remaining, path);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sb.len) {
|
/* Iterate over the remaining path components */
|
||||||
if (!cwd.len && strbuf_getcwd(&cwd)) {
|
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 */
|
||||||
|
strip_last_component(&resolved);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* append the next component and resolve resultant path */
|
||||||
|
if (!is_dir_sep(resolved.buf[resolved.len - 1]))
|
||||||
|
strbuf_addch(&resolved, '/');
|
||||||
|
strbuf_addbuf(&resolved, &next);
|
||||||
|
|
||||||
|
if (lstat(resolved.buf, &st)) {
|
||||||
|
/* error out unless this was the last component */
|
||||||
|
if (errno != ENOENT || remaining.len) {
|
||||||
if (die_on_error)
|
if (die_on_error)
|
||||||
die_errno("Could not get current working directory");
|
die_errno("Invalid path '%s'",
|
||||||
|
resolved.buf);
|
||||||
|
else
|
||||||
|
goto error_out;
|
||||||
|
}
|
||||||
|
} else if (S_ISLNK(st.st_mode)) {
|
||||||
|
ssize_t len;
|
||||||
|
strbuf_reset(&symlink);
|
||||||
|
|
||||||
|
if (num_symlinks++ > MAXSYMLINKS) {
|
||||||
|
if (die_on_error)
|
||||||
|
die("More than %d nested symlinks "
|
||||||
|
"on path '%s'", MAXSYMLINKS, path);
|
||||||
else
|
else
|
||||||
goto error_out;
|
goto error_out;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (chdir(sb.buf)) {
|
len = strbuf_readlink(&symlink, resolved.buf,
|
||||||
if (die_on_error)
|
st.st_size);
|
||||||
die_errno("Could not switch to '%s'",
|
|
||||||
sb.buf);
|
|
||||||
else
|
|
||||||
goto error_out;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (strbuf_getcwd(&sb)) {
|
|
||||||
if (die_on_error)
|
|
||||||
die_errno("Could not get current working directory");
|
|
||||||
else
|
|
||||||
goto error_out;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (last_elem) {
|
|
||||||
if (sb.len && !is_dir_sep(sb.buf[sb.len - 1]))
|
|
||||||
strbuf_addch(&sb, '/');
|
|
||||||
strbuf_addstr(&sb, last_elem);
|
|
||||||
free(last_elem);
|
|
||||||
last_elem = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
if (len < 0) {
|
if (len < 0) {
|
||||||
if (die_on_error)
|
if (die_on_error)
|
||||||
die_errno("Invalid symlink '%s'",
|
die_errno("Invalid symlink '%s'",
|
||||||
sb.buf);
|
resolved.buf);
|
||||||
else
|
else
|
||||||
goto error_out;
|
goto error_out;
|
||||||
}
|
}
|
||||||
strbuf_swap(&sb, &next_sb);
|
|
||||||
strbuf_release(&next_sb);
|
if (is_absolute_path(symlink.buf)) {
|
||||||
} else
|
/* absolute symlink; set resolved to root */
|
||||||
break;
|
int offset = offset_1st_component(symlink.buf);
|
||||||
|
strbuf_reset(&resolved);
|
||||||
|
strbuf_add(&resolved, symlink.buf, offset);
|
||||||
|
strbuf_remove(&symlink, 0, offset);
|
||||||
|
} else {
|
||||||
|
/*
|
||||||
|
* relative symlink
|
||||||
|
* strip off the last component since it will
|
||||||
|
* be replaced with the contents of the symlink
|
||||||
|
*/
|
||||||
|
strip_last_component(&resolved);
|
||||||
}
|
}
|
||||||
|
|
||||||
retval = sb.buf;
|
/*
|
||||||
|
* 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
retval = resolved.buf;
|
||||||
|
|
||||||
error_out:
|
error_out:
|
||||||
free(last_elem);
|
strbuf_release(&remaining);
|
||||||
if (cwd.len && chdir(cwd.buf))
|
strbuf_release(&next);
|
||||||
die_errno("Could not change back to '%s'", cwd.buf);
|
strbuf_release(&symlink);
|
||||||
strbuf_release(&cwd);
|
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue