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.
|
|
|
|
* Returns a NULL on failure...
|
|
|
|
*
|
|
|
|
* Licensed under GPLv2, see file LICENSE in this tarball for details.
|
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
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* UNUSED */
|
|
|
|
#if 0
|
2008-06-27 02:52:20 +00:00
|
|
|
char* FAST_FUNC xmalloc_realpath(const char *path)
|
2007-01-04 17:59:59 +00:00
|
|
|
{
|
2007-01-24 22:02:01 +00:00
|
|
|
#if defined(__GLIBC__) && !defined(__UCLIBC__)
|
2007-01-04 17:59:59 +00:00
|
|
|
/* glibc provides a non-standard extension */
|
|
|
|
return realpath(path, NULL);
|
|
|
|
#else
|
|
|
|
char buf[PATH_MAX+1];
|
|
|
|
|
|
|
|
/* on error returns NULL (xstrdup(NULL) ==NULL) */
|
|
|
|
return xstrdup(realpath(path, buf));
|
|
|
|
#endif
|
|
|
|
}
|
2007-09-05 11:30:34 +00:00
|
|
|
#endif
|