2006-07-02 19:47:05 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
2001-04-30 18:17:00 +00:00
|
|
|
/*
|
2008-12-07 00:52:58 +00:00
|
|
|
* xreadlink.c - safe implementation of readlink.
|
2014-03-16 19:53:40 +00:00
|
|
|
* Returns a NULL on failure.
|
2008-12-07 00:52:58 +00:00
|
|
|
*
|
2010-08-16 18:14:46 +00:00
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
2001-04-30 18:17:00 +00:00
|
|
|
*/
|
|
|
|
|
2007-01-04 17:59:59 +00:00
|
|
|
#include "libbb.h"
|
2001-04-30 18:17:00 +00:00
|
|
|
|
2014-03-16 19:53:40 +00:00
|
|
|
/* Some systems (eg Hurd) do not have MAXSYMLINKS definition,
|
2013-12-09 10:45:01 +00:00
|
|
|
* set it to some reasonable value if it isn't defined */
|
|
|
|
#ifndef MAXSYMLINKS
|
|
|
|
# define MAXSYMLINKS 20
|
|
|
|
#endif
|
|
|
|
|
2001-04-30 18:17:00 +00:00
|
|
|
/*
|
|
|
|
* NOTE: This function returns a malloced char* that you will have to free
|
2007-11-08 21:11:43 +00:00
|
|
|
* yourself.
|
2001-04-30 18:17:00 +00:00
|
|
|
*/
|
2008-06-27 02:52:20 +00:00
|
|
|
char* FAST_FUNC xmalloc_readlink(const char *path)
|
2006-01-25 00:08:53 +00:00
|
|
|
{
|
2006-03-10 19:22:06 +00:00
|
|
|
enum { GROWBY = 80 }; /* how large we will grow strings by */
|
2001-04-30 18:17:00 +00:00
|
|
|
|
2004-03-15 08:29:22 +00:00
|
|
|
char *buf = NULL;
|
2001-04-30 18:17:00 +00:00
|
|
|
int bufsize = 0, readsize = 0;
|
|
|
|
|
|
|
|
do {
|
2007-11-02 23:31:10 +00:00
|
|
|
bufsize += GROWBY;
|
|
|
|
buf = xrealloc(buf, bufsize);
|
2007-09-05 11:30:34 +00:00
|
|
|
readsize = readlink(path, buf, bufsize);
|
2001-05-07 17:48:28 +00:00
|
|
|
if (readsize == -1) {
|
2004-08-11 03:50:30 +00:00
|
|
|
free(buf);
|
|
|
|
return NULL;
|
2001-05-07 17:48:28 +00:00
|
|
|
}
|
2007-09-05 11:30:34 +00:00
|
|
|
} while (bufsize < readsize + 1);
|
2001-04-30 18:17:00 +00:00
|
|
|
|
|
|
|
buf[readsize] = '\0';
|
|
|
|
|
|
|
|
return buf;
|
2004-03-15 08:29:22 +00:00
|
|
|
}
|
2007-01-04 17:59:59 +00:00
|
|
|
|
2007-11-08 01:11:41 +00:00
|
|
|
/*
|
2007-11-08 21:11:43 +00:00
|
|
|
* This routine is not the same as realpath(), which
|
|
|
|
* canonicalizes the given path completely. This routine only
|
|
|
|
* follows trailing symlinks until a real file is reached and
|
|
|
|
* returns its name. If the path ends in a dangling link or if
|
|
|
|
* the target doesn't exist, the path is returned in any case.
|
|
|
|
* Intermediate symlinks in the path are not expanded -- only
|
2007-11-08 20:00:36 +00:00
|
|
|
* those at the tail.
|
2007-11-08 21:11:43 +00:00
|
|
|
* A malloced char* is returned, which must be freed by the caller.
|
2007-11-08 01:11:41 +00:00
|
|
|
*/
|
2008-06-27 02:52:20 +00:00
|
|
|
char* FAST_FUNC xmalloc_follow_symlinks(const char *path)
|
2007-11-08 01:11:41 +00:00
|
|
|
{
|
2007-11-08 17:40:23 +00:00
|
|
|
char *buf;
|
|
|
|
char *lpc;
|
|
|
|
char *linkpath;
|
2007-11-08 01:11:41 +00:00
|
|
|
int bufsize;
|
2007-11-08 17:40:23 +00:00
|
|
|
int looping = MAXSYMLINKS + 1;
|
2007-11-08 01:11:41 +00:00
|
|
|
|
2007-11-08 20:00:36 +00:00
|
|
|
buf = xstrdup(path);
|
2007-11-08 17:40:23 +00:00
|
|
|
goto jump_in;
|
2007-11-08 01:11:41 +00:00
|
|
|
|
2007-11-08 17:40:23 +00:00
|
|
|
while (1) {
|
2007-11-08 01:11:41 +00:00
|
|
|
linkpath = xmalloc_readlink(buf);
|
|
|
|
if (!linkpath) {
|
2007-11-08 20:00:36 +00:00
|
|
|
/* not a symlink, or doesn't exist */
|
|
|
|
if (errno == EINVAL || errno == ENOENT)
|
2007-11-08 01:11:41 +00:00
|
|
|
return buf;
|
2007-11-08 21:11:43 +00:00
|
|
|
goto free_buf_ret_null;
|
|
|
|
}
|
2007-11-08 20:00:36 +00:00
|
|
|
|
|
|
|
if (!--looping) {
|
|
|
|
free(linkpath);
|
2007-11-10 01:28:19 +00:00
|
|
|
free_buf_ret_null:
|
2007-11-08 20:00:36 +00:00
|
|
|
free(buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*linkpath != '/') {
|
2007-11-08 01:11:41 +00:00
|
|
|
bufsize += strlen(linkpath);
|
|
|
|
buf = xrealloc(buf, bufsize);
|
|
|
|
lpc = bb_get_last_path_component_strip(buf);
|
|
|
|
strcpy(lpc, linkpath);
|
|
|
|
free(linkpath);
|
2007-11-08 17:40:23 +00:00
|
|
|
} else {
|
|
|
|
free(buf);
|
|
|
|
buf = linkpath;
|
2007-11-08 20:00:36 +00:00
|
|
|
jump_in:
|
2007-11-08 17:40:23 +00:00
|
|
|
bufsize = strlen(buf) + 1;
|
2007-11-08 01:11:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
char* FAST_FUNC xmalloc_readlink_or_warn(const char *path)
|
2007-09-05 11:30:34 +00:00
|
|
|
{
|
|
|
|
char *buf = xmalloc_readlink(path);
|
|
|
|
if (!buf) {
|
|
|
|
/* EINVAL => "file: Invalid argument" => puzzled user */
|
2009-03-21 19:11:23 +00:00
|
|
|
const char *errmsg = "not a symlink";
|
|
|
|
int err = errno;
|
|
|
|
if (err != EINVAL)
|
|
|
|
errmsg = strerror(err);
|
|
|
|
bb_error_msg("%s: cannot read link: %s", path, errmsg);
|
2007-09-05 11:30:34 +00:00
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
char* FAST_FUNC xmalloc_realpath(const char *path)
|
2007-01-04 17:59:59 +00:00
|
|
|
{
|
2014-03-16 19:53:40 +00:00
|
|
|
/* NB: uclibc also defines __GLIBC__
|
|
|
|
* Therefore the test "if glibc, or uclibc >= 0.9.31" looks a bit weird:
|
|
|
|
*/
|
|
|
|
#if defined(__GLIBC__) && \
|
|
|
|
(!defined(__UCLIBC__) || UCLIBC_VERSION >= KERNEL_VERSION(0, 9, 31))
|
2007-01-04 17:59:59 +00:00
|
|
|
/* glibc provides a non-standard extension */
|
2010-03-26 18:08:53 +00:00
|
|
|
/* new: POSIX.1-2008 specifies this behavior as well */
|
2007-01-04 17:59:59 +00:00
|
|
|
return realpath(path, NULL);
|
|
|
|
#else
|
|
|
|
char buf[PATH_MAX+1];
|
|
|
|
|
2010-03-26 18:08:53 +00:00
|
|
|
/* on error returns NULL (xstrdup(NULL) == NULL) */
|
2007-01-04 17:59:59 +00:00
|
|
|
return xstrdup(realpath(path, buf));
|
|
|
|
#endif
|
|
|
|
}
|