|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef _LIBC |
|
|
|
|
|
# define _GL_ARG_NONNULL(params) |
|
|
|
# include <libc-config.h> |
|
#endif |
|
|
|
|
|
#include <stdlib.h> |
|
|
|
#include <errno.h> |
|
#include <fcntl.h> |
|
#include <limits.h> |
|
#include <string.h> |
|
#include <sys/stat.h> |
|
#include <unistd.h> |
|
|
|
#include <eloop-threshold.h> |
|
#include <filename.h> |
|
#include <idx.h> |
|
#include <intprops.h> |
|
#include <scratch_buffer.h> |
|
|
|
#ifdef _LIBC |
|
# include <shlib-compat.h> |
|
# define GCC_LINT 1 |
|
# define _GL_ATTRIBUTE_PURE __attribute__ ((__pure__)) |
|
#else |
|
# define __canonicalize_file_name canonicalize_file_name |
|
# define __realpath realpath |
|
# define __strdup strdup |
|
# include "pathmax.h" |
|
# define __faccessat faccessat |
|
# if defined _WIN32 && !defined __CYGWIN__ |
|
# define __getcwd _getcwd |
|
# elif HAVE_GETCWD |
|
# if IN_RELOCWRAPPER |
|
|
|
|
|
|
|
# undef getcwd |
|
# endif |
|
# if defined VMS && !defined getcwd |
|
|
|
|
|
|
|
# define __getcwd(buf, max) getcwd (buf, max, 0) |
|
# else |
|
# define __getcwd getcwd |
|
# endif |
|
# else |
|
# define __getcwd(buf, max) getwd (buf) |
|
# endif |
|
# define __mempcpy mempcpy |
|
# define __pathconf pathconf |
|
# define __rawmemchr rawmemchr |
|
# define __readlink readlink |
|
# if IN_RELOCWRAPPER |
|
|
|
|
|
|
|
# undef memmove |
|
# endif |
|
#endif |
|
|
|
|
|
#if defined GCC_LINT || defined lint |
|
# define IF_LINT(Code) Code |
|
#else |
|
# define IF_LINT(Code) |
|
#endif |
|
|
|
#ifndef DOUBLE_SLASH_IS_DISTINCT_ROOT |
|
# define DOUBLE_SLASH_IS_DISTINCT_ROOT false |
|
#endif |
|
|
|
#if defined _LIBC || !FUNC_REALPATH_WORKS |
|
|
|
|
|
|
|
static bool |
|
file_accessible (char const *file) |
|
{ |
|
# if defined _LIBC || HAVE_FACCESSAT |
|
return __faccessat (AT_FDCWD, file, F_OK, AT_EACCESS) == 0; |
|
# else |
|
struct stat st; |
|
return stat (file, &st) == 0 || errno == EOVERFLOW; |
|
# endif |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static bool _GL_ATTRIBUTE_PURE |
|
suffix_requires_dir_check (char const *end) |
|
{ |
|
|
|
while (ISSLASH (*end)) |
|
{ |
|
|
|
do |
|
end++; |
|
while (ISSLASH (*end)); |
|
|
|
switch (*end++) |
|
{ |
|
default: return false; |
|
case '\0': return true; |
|
case '.': break; |
|
} |
|
|
|
if (!*end || (*end == '.' && (!end[1] || ISSLASH (end[1])))) |
|
return true; |
|
} |
|
|
|
return false; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
# if defined _LIBC || defined LSTAT_FOLLOWS_SLASHED_SYMLINK |
|
static char const dir_suffix[] = "/"; |
|
# else |
|
static char const dir_suffix[] = "/./"; |
|
# endif |
|
|
|
|
|
|
|
|
|
|
|
static bool |
|
dir_check (char *dir, char *dirend) |
|
{ |
|
strcpy (dirend, dir_suffix); |
|
return file_accessible (dir); |
|
} |
|
|
|
static idx_t |
|
get_path_max (void) |
|
{ |
|
# ifdef PATH_MAX |
|
long int path_max = PATH_MAX; |
|
# else |
|
|
|
|
|
|
|
|
|
|
|
int err = errno; |
|
long int path_max = __pathconf ("/", _PC_PATH_MAX); |
|
__set_errno (err); |
|
# endif |
|
return path_max < 0 ? 1024 : path_max <= IDX_MAX ? path_max : IDX_MAX; |
|
} |
|
|
|
|
|
struct realpath_bufs |
|
{ |
|
struct scratch_buffer rname; |
|
struct scratch_buffer extra; |
|
struct scratch_buffer link; |
|
}; |
|
|
|
static char * |
|
realpath_stk (const char *name, char *resolved, struct realpath_bufs *bufs) |
|
{ |
|
char *dest; |
|
char const *start; |
|
char const *end; |
|
int num_links = 0; |
|
|
|
if (name == NULL) |
|
{ |
|
|
|
|
|
|
|
|
|
__set_errno (EINVAL); |
|
return NULL; |
|
} |
|
|
|
if (name[0] == '\0') |
|
{ |
|
|
|
|
|
__set_errno (ENOENT); |
|
return NULL; |
|
} |
|
|
|
char *rname = bufs->rname.data; |
|
bool end_in_extra_buffer = false; |
|
bool failed = true; |
|
|
|
|
|
|
|
idx_t prefix_len = FILE_SYSTEM_PREFIX_LEN (name); |
|
|
|
if (!IS_ABSOLUTE_FILE_NAME (name)) |
|
{ |
|
while (!__getcwd (bufs->rname.data, bufs->rname.length)) |
|
{ |
|
if (errno != ERANGE) |
|
{ |
|
dest = rname; |
|
goto error; |
|
} |
|
if (!scratch_buffer_grow (&bufs->rname)) |
|
return NULL; |
|
rname = bufs->rname.data; |
|
} |
|
dest = __rawmemchr (rname, '\0'); |
|
start = name; |
|
prefix_len = FILE_SYSTEM_PREFIX_LEN (rname); |
|
} |
|
else |
|
{ |
|
dest = __mempcpy (rname, name, prefix_len); |
|
*dest++ = '/'; |
|
if (DOUBLE_SLASH_IS_DISTINCT_ROOT) |
|
{ |
|
if (prefix_len == 0 |
|
&& ISSLASH (name[1]) && !ISSLASH (name[2])) |
|
*dest++ = '/'; |
|
*dest = '\0'; |
|
} |
|
start = name + prefix_len; |
|
} |
|
|
|
for ( ; *start; start = end) |
|
{ |
|
|
|
while (ISSLASH (*start)) |
|
++start; |
|
|
|
|
|
for (end = start; *end && !ISSLASH (*end); ++end) |
|
; |
|
|
|
|
|
|
|
idx_t startlen = end - start; |
|
|
|
if (startlen == 0) |
|
break; |
|
else if (startlen == 1 && start[0] == '.') |
|
; |
|
else if (startlen == 2 && start[0] == '.' && start[1] == '.') |
|
{ |
|
|
|
if (dest > rname + prefix_len + 1) |
|
for (--dest; dest > rname && !ISSLASH (dest[-1]); --dest) |
|
continue; |
|
if (DOUBLE_SLASH_IS_DISTINCT_ROOT |
|
&& dest == rname + 1 && !prefix_len |
|
&& ISSLASH (*dest) && !ISSLASH (dest[1])) |
|
dest++; |
|
} |
|
else |
|
{ |
|
if (!ISSLASH (dest[-1])) |
|
*dest++ = '/'; |
|
|
|
while (rname + bufs->rname.length - dest |
|
< startlen + sizeof dir_suffix) |
|
{ |
|
idx_t dest_offset = dest - rname; |
|
if (!scratch_buffer_grow_preserve (&bufs->rname)) |
|
return NULL; |
|
rname = bufs->rname.data; |
|
dest = rname + dest_offset; |
|
} |
|
|
|
dest = __mempcpy (dest, start, startlen); |
|
*dest = '\0'; |
|
|
|
char *buf; |
|
ssize_t n; |
|
while (true) |
|
{ |
|
buf = bufs->link.data; |
|
idx_t bufsize = bufs->link.length; |
|
n = __readlink (rname, buf, bufsize - 1); |
|
if (n < bufsize - 1) |
|
break; |
|
if (!scratch_buffer_grow (&bufs->link)) |
|
return NULL; |
|
} |
|
if (0 <= n) |
|
{ |
|
if (++num_links > __eloop_threshold ()) |
|
{ |
|
__set_errno (ELOOP); |
|
goto error; |
|
} |
|
|
|
buf[n] = '\0'; |
|
|
|
char *extra_buf = bufs->extra.data; |
|
idx_t end_idx IF_LINT (= 0); |
|
if (end_in_extra_buffer) |
|
end_idx = end - extra_buf; |
|
size_t len = strlen (end); |
|
if (INT_ADD_OVERFLOW (len, n)) |
|
{ |
|
__set_errno (ENOMEM); |
|
return NULL; |
|
} |
|
while (bufs->extra.length <= len + n) |
|
{ |
|
if (!scratch_buffer_grow_preserve (&bufs->extra)) |
|
return NULL; |
|
extra_buf = bufs->extra.data; |
|
} |
|
if (end_in_extra_buffer) |
|
end = extra_buf + end_idx; |
|
|
|
|
|
memmove (&extra_buf[n], end, len + 1); |
|
name = end = memcpy (extra_buf, buf, n); |
|
end_in_extra_buffer = true; |
|
|
|
if (IS_ABSOLUTE_FILE_NAME (buf)) |
|
{ |
|
idx_t pfxlen = FILE_SYSTEM_PREFIX_LEN (buf); |
|
|
|
dest = __mempcpy (rname, buf, pfxlen); |
|
*dest++ = '/'; |
|
if (DOUBLE_SLASH_IS_DISTINCT_ROOT) |
|
{ |
|
if (ISSLASH (buf[1]) && !ISSLASH (buf[2]) && !pfxlen) |
|
*dest++ = '/'; |
|
*dest = '\0'; |
|
} |
|
|
|
prefix_len = pfxlen; |
|
} |
|
else |
|
{ |
|
|
|
|
|
if (dest > rname + prefix_len + 1) |
|
for (--dest; dest > rname && !ISSLASH (dest[-1]); --dest) |
|
continue; |
|
if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rname + 1 |
|
&& ISSLASH (*dest) && !ISSLASH (dest[1]) && !prefix_len) |
|
dest++; |
|
} |
|
} |
|
else if (! (suffix_requires_dir_check (end) |
|
? dir_check (rname, dest) |
|
: errno == EINVAL)) |
|
goto error; |
|
} |
|
} |
|
if (dest > rname + prefix_len + 1 && ISSLASH (dest[-1])) |
|
--dest; |
|
if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rname + 1 && !prefix_len |
|
&& ISSLASH (*dest) && !ISSLASH (dest[1])) |
|
dest++; |
|
failed = false; |
|
|
|
error: |
|
*dest++ = '\0'; |
|
if (resolved != NULL) |
|
{ |
|
|
|
|
|
if ((!failed || errno == ENOENT || errno == EACCES) |
|
&& dest - rname <= get_path_max ()) |
|
{ |
|
strcpy (resolved, rname); |
|
if (failed) |
|
return NULL; |
|
else |
|
return resolved; |
|
} |
|
if (!failed) |
|
__set_errno (ENAMETOOLONG); |
|
return NULL; |
|
} |
|
else |
|
{ |
|
if (failed) |
|
return NULL; |
|
else |
|
return __strdup (bufs->rname.data); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char * |
|
__realpath (const char *name, char *resolved) |
|
{ |
|
struct realpath_bufs bufs; |
|
scratch_buffer_init (&bufs.rname); |
|
scratch_buffer_init (&bufs.extra); |
|
scratch_buffer_init (&bufs.link); |
|
char *result = realpath_stk (name, resolved, &bufs); |
|
scratch_buffer_free (&bufs.link); |
|
scratch_buffer_free (&bufs.extra); |
|
scratch_buffer_free (&bufs.rname); |
|
return result; |
|
} |
|
libc_hidden_def (__realpath) |
|
versioned_symbol (libc, __realpath, realpath, GLIBC_2_3); |
|
|
|
#endif |
|
|
|
|
|
#if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_3) |
|
char * |
|
attribute_compat_text_section |
|
__old_realpath (const char *name, char *resolved) |
|
{ |
|
if (resolved == NULL) |
|
{ |
|
__set_errno (EINVAL); |
|
return NULL; |
|
} |
|
|
|
return __realpath (name, resolved); |
|
} |
|
compat_symbol (libc, __old_realpath, realpath, GLIBC_2_0); |
|
#endif |
|
|
|
|
|
char * |
|
__canonicalize_file_name (const char *name) |
|
{ |
|
return __realpath (name, NULL); |
|
} |
|
weak_alias (__canonicalize_file_name, canonicalize_file_name) |
|
|