"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. |
Denys Vlasenko | c504938 | 2014-03-16 20:53:40 +0100 | [diff] [blame] | 4 | * Returns a NULL on failure. |
Denis Vlasenko | db12d1d | 2008-12-07 00:52:58 +0000 | [diff] [blame] | 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 | */ |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 8 | #include "libbb.h" |
Mark Whitley | 8a63326 | 2001-04-30 18:17:00 +0000 | [diff] [blame] | 9 | |
Denys Vlasenko | c504938 | 2014-03-16 20:53:40 +0100 | [diff] [blame] | 10 | /* Some systems (eg Hurd) do not have MAXSYMLINKS definition, |
Michael Tokarev | 9f4f601 | 2013-12-09 14:45:01 +0400 | [diff] [blame] | 11 | * set it to some reasonable value if it isn't defined */ |
| 12 | #ifndef MAXSYMLINKS |
| 13 | # define MAXSYMLINKS 20 |
| 14 | #endif |
| 15 | |
Mark Whitley | 8a63326 | 2001-04-30 18:17:00 +0000 | [diff] [blame] | 16 | /* |
| 17 | * 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] | 18 | * yourself. |
Mark Whitley | 8a63326 | 2001-04-30 18:17:00 +0000 | [diff] [blame] | 19 | */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 20 | char* FAST_FUNC xmalloc_readlink(const char *path) |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 21 | { |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 22 | enum { GROWBY = 80 }; /* how large we will grow strings by */ |
Mark Whitley | 8a63326 | 2001-04-30 18:17:00 +0000 | [diff] [blame] | 23 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 24 | char *buf = NULL; |
Mark Whitley | 8a63326 | 2001-04-30 18:17:00 +0000 | [diff] [blame] | 25 | int bufsize = 0, readsize = 0; |
| 26 | |
| 27 | do { |
Denis Vlasenko | b68979a | 2007-11-02 23:31:10 +0000 | [diff] [blame] | 28 | bufsize += GROWBY; |
| 29 | buf = xrealloc(buf, bufsize); |
Denis Vlasenko | beffd43 | 2007-09-05 11:30:34 +0000 | [diff] [blame] | 30 | readsize = readlink(path, buf, bufsize); |
Eric Andersen | 28355a3 | 2001-05-07 17:48:28 +0000 | [diff] [blame] | 31 | if (readsize == -1) { |
Glenn L McGrath | 18bbd9b | 2004-08-11 03:50:30 +0000 | [diff] [blame] | 32 | free(buf); |
| 33 | return NULL; |
Eric Andersen | 28355a3 | 2001-05-07 17:48:28 +0000 | [diff] [blame] | 34 | } |
Denis Vlasenko | beffd43 | 2007-09-05 11:30:34 +0000 | [diff] [blame] | 35 | } while (bufsize < readsize + 1); |
Mark Whitley | 8a63326 | 2001-04-30 18:17:00 +0000 | [diff] [blame] | 36 | |
| 37 | buf[readsize] = '\0'; |
| 38 | |
| 39 | return buf; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 40 | } |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 41 | |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 42 | /* |
Bernhard Reutner-Fischer | 9bd8d0c | 2007-11-08 21:11:43 +0000 | [diff] [blame] | 43 | * This routine is not the same as realpath(), which |
| 44 | * canonicalizes the given path completely. This routine only |
| 45 | * follows trailing symlinks until a real file is reached and |
| 46 | * returns its name. If the path ends in a dangling link or if |
| 47 | * the target doesn't exist, the path is returned in any case. |
| 48 | * Intermediate symlinks in the path are not expanded -- only |
Paul Fox | 599bbfb | 2007-11-08 20:00:36 +0000 | [diff] [blame] | 49 | * those at the tail. |
Bernhard Reutner-Fischer | 9bd8d0c | 2007-11-08 21:11:43 +0000 | [diff] [blame] | 50 | * A malloced char* is returned, which must be freed by the caller. |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 51 | */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 52 | char* FAST_FUNC xmalloc_follow_symlinks(const char *path) |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 53 | { |
Denis Vlasenko | abbd363 | 2007-11-08 17:40:23 +0000 | [diff] [blame] | 54 | char *buf; |
| 55 | char *lpc; |
| 56 | char *linkpath; |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 57 | int bufsize; |
Denis Vlasenko | abbd363 | 2007-11-08 17:40:23 +0000 | [diff] [blame] | 58 | int looping = MAXSYMLINKS + 1; |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 59 | |
Paul Fox | 599bbfb | 2007-11-08 20:00:36 +0000 | [diff] [blame] | 60 | buf = xstrdup(path); |
Denis Vlasenko | abbd363 | 2007-11-08 17:40:23 +0000 | [diff] [blame] | 61 | goto jump_in; |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 62 | |
Denis Vlasenko | abbd363 | 2007-11-08 17:40:23 +0000 | [diff] [blame] | 63 | while (1) { |
Paul Fox | 599bbfb | 2007-11-08 20:00:36 +0000 | [diff] [blame] | 64 | linkpath = xmalloc_readlink(buf); |
| 65 | if (!linkpath) { |
| 66 | /* not a symlink, or doesn't exist */ |
| 67 | if (errno == EINVAL || errno == ENOENT) |
| 68 | return buf; |
Bernhard Reutner-Fischer | 9bd8d0c | 2007-11-08 21:11:43 +0000 | [diff] [blame] | 69 | goto free_buf_ret_null; |
| 70 | } |
Paul Fox | 599bbfb | 2007-11-08 20:00:36 +0000 | [diff] [blame] | 71 | |
Denis Vlasenko | abbd363 | 2007-11-08 17:40:23 +0000 | [diff] [blame] | 72 | if (!--looping) { |
| 73 | free(linkpath); |
Denis Vlasenko | d031b20 | 2007-11-10 01:28:19 +0000 | [diff] [blame] | 74 | free_buf_ret_null: |
Denis Vlasenko | abbd363 | 2007-11-08 17:40:23 +0000 | [diff] [blame] | 75 | free(buf); |
| 76 | return NULL; |
| 77 | } |
Paul Fox | 599bbfb | 2007-11-08 20:00:36 +0000 | [diff] [blame] | 78 | |
| 79 | if (*linkpath != '/') { |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 80 | bufsize += strlen(linkpath); |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 81 | buf = xrealloc(buf, bufsize); |
| 82 | lpc = bb_get_last_path_component_strip(buf); |
| 83 | strcpy(lpc, linkpath); |
| 84 | free(linkpath); |
Denis Vlasenko | abbd363 | 2007-11-08 17:40:23 +0000 | [diff] [blame] | 85 | } else { |
| 86 | free(buf); |
Denis Vlasenko | abbd363 | 2007-11-08 17:40:23 +0000 | [diff] [blame] | 87 | buf = linkpath; |
Paul Fox | 599bbfb | 2007-11-08 20:00:36 +0000 | [diff] [blame] | 88 | jump_in: |
Denis Vlasenko | abbd363 | 2007-11-08 17:40:23 +0000 | [diff] [blame] | 89 | bufsize = strlen(buf) + 1; |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 90 | } |
| 91 | } |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 94 | char* FAST_FUNC xmalloc_readlink_or_warn(const char *path) |
Denis Vlasenko | beffd43 | 2007-09-05 11:30:34 +0000 | [diff] [blame] | 95 | { |
| 96 | char *buf = xmalloc_readlink(path); |
| 97 | if (!buf) { |
| 98 | /* EINVAL => "file: Invalid argument" => puzzled user */ |
Denis Vlasenko | 3a014b8 | 2009-03-21 19:11:23 +0000 | [diff] [blame] | 99 | const char *errmsg = "not a symlink"; |
| 100 | int err = errno; |
| 101 | if (err != EINVAL) |
| 102 | errmsg = strerror(err); |
| 103 | bb_error_msg("%s: cannot read link: %s", path, errmsg); |
Denis Vlasenko | beffd43 | 2007-09-05 11:30:34 +0000 | [diff] [blame] | 104 | } |
| 105 | return buf; |
| 106 | } |
| 107 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 108 | char* FAST_FUNC xmalloc_realpath(const char *path) |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 109 | { |
Denys Vlasenko | c504938 | 2014-03-16 20:53:40 +0100 | [diff] [blame] | 110 | /* NB: uclibc also defines __GLIBC__ |
| 111 | * Therefore the test "if glibc, or uclibc >= 0.9.31" looks a bit weird: |
| 112 | */ |
| 113 | #if defined(__GLIBC__) && \ |
| 114 | (!defined(__UCLIBC__) || UCLIBC_VERSION >= KERNEL_VERSION(0, 9, 31)) |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 115 | /* glibc provides a non-standard extension */ |
Jérémie Koenig | fbedacf | 2010-03-26 19:08:53 +0100 | [diff] [blame] | 116 | /* new: POSIX.1-2008 specifies this behavior as well */ |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 117 | return realpath(path, NULL); |
| 118 | #else |
| 119 | char buf[PATH_MAX+1]; |
| 120 | |
Jérémie Koenig | fbedacf | 2010-03-26 19:08:53 +0100 | [diff] [blame] | 121 | /* on error returns NULL (xstrdup(NULL) == NULL) */ |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 122 | return xstrdup(realpath(path, buf)); |
| 123 | #endif |
| 124 | } |
Denys Vlasenko | 7471621 | 2018-05-24 17:29:14 +0200 | [diff] [blame] | 125 | |
| 126 | char* FAST_FUNC xmalloc_realpath_coreutils(const char *path) |
| 127 | { |
| 128 | char *buf; |
| 129 | |
| 130 | errno = 0; |
| 131 | buf = xmalloc_realpath(path); |
| 132 | /* |
| 133 | * There is one case when "readlink -f" and |
| 134 | * "realpath" from coreutils succeed, |
| 135 | * even though file does not exist, such as: |
| 136 | * /tmp/file_does_not_exist |
| 137 | * (the directory must exist). |
| 138 | */ |
| 139 | if (!buf && errno == ENOENT) { |
| 140 | char *last_slash = strrchr(path, '/'); |
| 141 | if (last_slash) { |
| 142 | *last_slash++ = '\0'; |
| 143 | buf = xmalloc_realpath(path); |
| 144 | if (buf) { |
| 145 | unsigned len = strlen(buf); |
Denys Vlasenko | bf7f103 | 2018-05-26 17:42:00 +0200 | [diff] [blame] | 146 | buf = xrealloc(buf, len + strlen(last_slash) + 2); |
Denys Vlasenko | 7471621 | 2018-05-24 17:29:14 +0200 | [diff] [blame] | 147 | buf[len++] = '/'; |
| 148 | strcpy(buf + len, last_slash); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | return buf; |
| 154 | } |