"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Mark Whitley | 8a63326 | 2001-04-30 18:17:00 +0000 | [diff] [blame] | 2 | /* |
Denis Vlasenko | db12d1d | 2008-12-07 00:52:58 +0000 | [diff] [blame] | 3 | * xreadlink.c - safe implementation of readlink. |
| 4 | * Returns a NULL on failure... |
| 5 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 6 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Mark Whitley | 8a63326 | 2001-04-30 18:17:00 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 9 | #include "libbb.h" |
Mark Whitley | 8a63326 | 2001-04-30 18:17:00 +0000 | [diff] [blame] | 10 | |
| 11 | /* |
| 12 | * NOTE: This function returns a malloced char* that you will have to free |
Bernhard Reutner-Fischer | 9bd8d0c | 2007-11-08 21:11:43 +0000 | [diff] [blame] | 13 | * yourself. |
Mark Whitley | 8a63326 | 2001-04-30 18:17:00 +0000 | [diff] [blame] | 14 | */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 15 | char* FAST_FUNC xmalloc_readlink(const char *path) |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 16 | { |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 17 | enum { GROWBY = 80 }; /* how large we will grow strings by */ |
Mark Whitley | 8a63326 | 2001-04-30 18:17:00 +0000 | [diff] [blame] | 18 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 19 | char *buf = NULL; |
Mark Whitley | 8a63326 | 2001-04-30 18:17:00 +0000 | [diff] [blame] | 20 | int bufsize = 0, readsize = 0; |
| 21 | |
| 22 | do { |
Denis Vlasenko | b68979a | 2007-11-02 23:31:10 +0000 | [diff] [blame] | 23 | bufsize += GROWBY; |
| 24 | buf = xrealloc(buf, bufsize); |
Denis Vlasenko | beffd43 | 2007-09-05 11:30:34 +0000 | [diff] [blame] | 25 | readsize = readlink(path, buf, bufsize); |
Eric Andersen | 28355a3 | 2001-05-07 17:48:28 +0000 | [diff] [blame] | 26 | if (readsize == -1) { |
Glenn L McGrath | 18bbd9b | 2004-08-11 03:50:30 +0000 | [diff] [blame] | 27 | free(buf); |
| 28 | return NULL; |
Eric Andersen | 28355a3 | 2001-05-07 17:48:28 +0000 | [diff] [blame] | 29 | } |
Denis Vlasenko | beffd43 | 2007-09-05 11:30:34 +0000 | [diff] [blame] | 30 | } while (bufsize < readsize + 1); |
Mark Whitley | 8a63326 | 2001-04-30 18:17:00 +0000 | [diff] [blame] | 31 | |
| 32 | buf[readsize] = '\0'; |
| 33 | |
| 34 | return buf; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 35 | } |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 36 | |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 37 | /* |
Bernhard Reutner-Fischer | 9bd8d0c | 2007-11-08 21:11:43 +0000 | [diff] [blame] | 38 | * This routine is not the same as realpath(), which |
| 39 | * canonicalizes the given path completely. This routine only |
| 40 | * follows trailing symlinks until a real file is reached and |
| 41 | * returns its name. If the path ends in a dangling link or if |
| 42 | * the target doesn't exist, the path is returned in any case. |
| 43 | * Intermediate symlinks in the path are not expanded -- only |
Paul Fox | 599bbfb | 2007-11-08 20:00:36 +0000 | [diff] [blame] | 44 | * those at the tail. |
Bernhard Reutner-Fischer | 9bd8d0c | 2007-11-08 21:11:43 +0000 | [diff] [blame] | 45 | * A malloced char* is returned, which must be freed by the caller. |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 46 | */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 47 | char* FAST_FUNC xmalloc_follow_symlinks(const char *path) |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 48 | { |
Denis Vlasenko | abbd363 | 2007-11-08 17:40:23 +0000 | [diff] [blame] | 49 | char *buf; |
| 50 | char *lpc; |
| 51 | char *linkpath; |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 52 | int bufsize; |
Denis Vlasenko | abbd363 | 2007-11-08 17:40:23 +0000 | [diff] [blame] | 53 | int looping = MAXSYMLINKS + 1; |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 54 | |
Paul Fox | 599bbfb | 2007-11-08 20:00:36 +0000 | [diff] [blame] | 55 | buf = xstrdup(path); |
Denis Vlasenko | abbd363 | 2007-11-08 17:40:23 +0000 | [diff] [blame] | 56 | goto jump_in; |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 57 | |
Denis Vlasenko | abbd363 | 2007-11-08 17:40:23 +0000 | [diff] [blame] | 58 | while (1) { |
Paul Fox | 599bbfb | 2007-11-08 20:00:36 +0000 | [diff] [blame] | 59 | linkpath = xmalloc_readlink(buf); |
| 60 | if (!linkpath) { |
| 61 | /* not a symlink, or doesn't exist */ |
| 62 | if (errno == EINVAL || errno == ENOENT) |
| 63 | return buf; |
Bernhard Reutner-Fischer | 9bd8d0c | 2007-11-08 21:11:43 +0000 | [diff] [blame] | 64 | goto free_buf_ret_null; |
| 65 | } |
Paul Fox | 599bbfb | 2007-11-08 20:00:36 +0000 | [diff] [blame] | 66 | |
Denis Vlasenko | abbd363 | 2007-11-08 17:40:23 +0000 | [diff] [blame] | 67 | if (!--looping) { |
| 68 | free(linkpath); |
Denis Vlasenko | d031b20 | 2007-11-10 01:28:19 +0000 | [diff] [blame] | 69 | free_buf_ret_null: |
Denis Vlasenko | abbd363 | 2007-11-08 17:40:23 +0000 | [diff] [blame] | 70 | free(buf); |
| 71 | return NULL; |
| 72 | } |
Paul Fox | 599bbfb | 2007-11-08 20:00:36 +0000 | [diff] [blame] | 73 | |
| 74 | if (*linkpath != '/') { |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 75 | bufsize += strlen(linkpath); |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 76 | buf = xrealloc(buf, bufsize); |
| 77 | lpc = bb_get_last_path_component_strip(buf); |
| 78 | strcpy(lpc, linkpath); |
| 79 | free(linkpath); |
Denis Vlasenko | abbd363 | 2007-11-08 17:40:23 +0000 | [diff] [blame] | 80 | } else { |
| 81 | free(buf); |
Denis Vlasenko | abbd363 | 2007-11-08 17:40:23 +0000 | [diff] [blame] | 82 | buf = linkpath; |
Paul Fox | 599bbfb | 2007-11-08 20:00:36 +0000 | [diff] [blame] | 83 | jump_in: |
Denis Vlasenko | abbd363 | 2007-11-08 17:40:23 +0000 | [diff] [blame] | 84 | bufsize = strlen(buf) + 1; |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 85 | } |
| 86 | } |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 89 | char* FAST_FUNC xmalloc_readlink_or_warn(const char *path) |
Denis Vlasenko | beffd43 | 2007-09-05 11:30:34 +0000 | [diff] [blame] | 90 | { |
| 91 | char *buf = xmalloc_readlink(path); |
| 92 | if (!buf) { |
| 93 | /* EINVAL => "file: Invalid argument" => puzzled user */ |
Denis Vlasenko | 3a014b8 | 2009-03-21 19:11:23 +0000 | [diff] [blame] | 94 | const char *errmsg = "not a symlink"; |
| 95 | int err = errno; |
| 96 | if (err != EINVAL) |
| 97 | errmsg = strerror(err); |
| 98 | bb_error_msg("%s: cannot read link: %s", path, errmsg); |
Denis Vlasenko | beffd43 | 2007-09-05 11:30:34 +0000 | [diff] [blame] | 99 | } |
| 100 | return buf; |
| 101 | } |
| 102 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 103 | char* FAST_FUNC xmalloc_realpath(const char *path) |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 104 | { |
Mike Frysinger | af9e70b | 2013-03-12 11:14:24 -0400 | [diff] [blame] | 105 | #if defined(__GLIBC__) || \ |
| 106 | (defined(__UCLIBC__) && UCLIBC_VERSION >= KERNEL_VERSION(0, 9, 31)) |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 107 | /* glibc provides a non-standard extension */ |
Jérémie Koenig | fbedacf | 2010-03-26 19:08:53 +0100 | [diff] [blame] | 108 | /* new: POSIX.1-2008 specifies this behavior as well */ |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 109 | return realpath(path, NULL); |
| 110 | #else |
| 111 | char buf[PATH_MAX+1]; |
| 112 | |
Jérémie Koenig | fbedacf | 2010-03-26 19:08:53 +0100 | [diff] [blame] | 113 | /* on error returns NULL (xstrdup(NULL) == NULL) */ |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 114 | return xstrdup(realpath(path, buf)); |
| 115 | #endif |
| 116 | } |