lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * xreadlink.c - safe implementation of readlink. |
| 4 | * Returns a NULL on failure... |
| 5 | * |
| 6 | * Licensed under GPLv2, see file LICENSE in this source tree. |
| 7 | */ |
| 8 | |
| 9 | #include "libbb.h" |
| 10 | |
| 11 | /* |
| 12 | * NOTE: This function returns a malloced char* that you will have to free |
| 13 | * yourself. |
| 14 | */ |
| 15 | char* FAST_FUNC xmalloc_readlink(const char *path) |
| 16 | { |
| 17 | enum { GROWBY = 80 }; /* how large we will grow strings by */ |
| 18 | |
| 19 | char *buf = NULL; |
| 20 | int bufsize = 0, readsize = 0; |
| 21 | |
| 22 | do { |
| 23 | bufsize += GROWBY; |
| 24 | buf = xrealloc(buf, bufsize); |
| 25 | readsize = readlink(path, buf, bufsize); |
| 26 | if (readsize == -1) { |
| 27 | free(buf); |
| 28 | return NULL; |
| 29 | } |
| 30 | } while (bufsize < readsize + 1); |
| 31 | |
| 32 | buf[readsize] = '\0'; |
| 33 | |
| 34 | return buf; |
| 35 | } |
| 36 | |
| 37 | /* |
| 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 |
| 44 | * those at the tail. |
| 45 | * A malloced char* is returned, which must be freed by the caller. |
| 46 | */ |
| 47 | char* FAST_FUNC xmalloc_follow_symlinks(const char *path) |
| 48 | { |
| 49 | char *buf; |
| 50 | char *lpc; |
| 51 | char *linkpath; |
| 52 | int bufsize; |
| 53 | int looping = MAXSYMLINKS + 1; |
| 54 | |
| 55 | buf = xstrdup(path); |
| 56 | goto jump_in; |
| 57 | |
| 58 | while (1) { |
| 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; |
| 64 | goto free_buf_ret_null; |
| 65 | } |
| 66 | |
| 67 | if (!--looping) { |
| 68 | free(linkpath); |
| 69 | free_buf_ret_null: |
| 70 | free(buf); |
| 71 | return NULL; |
| 72 | } |
| 73 | |
| 74 | if (*linkpath != '/') { |
| 75 | bufsize += strlen(linkpath); |
| 76 | buf = xrealloc(buf, bufsize); |
| 77 | lpc = bb_get_last_path_component_strip(buf); |
| 78 | strcpy(lpc, linkpath); |
| 79 | free(linkpath); |
| 80 | } else { |
| 81 | free(buf); |
| 82 | buf = linkpath; |
| 83 | jump_in: |
| 84 | bufsize = strlen(buf) + 1; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | char* FAST_FUNC xmalloc_readlink_or_warn(const char *path) |
| 90 | { |
| 91 | char *buf = xmalloc_readlink(path); |
| 92 | if (!buf) { |
| 93 | /* EINVAL => "file: Invalid argument" => puzzled user */ |
| 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); |
| 99 | } |
| 100 | return buf; |
| 101 | } |
| 102 | |
| 103 | char* FAST_FUNC xmalloc_realpath(const char *path) |
| 104 | { |
| 105 | #if defined(__GLIBC__) && !defined(__UCLIBC__) |
| 106 | /* glibc provides a non-standard extension */ |
| 107 | /* new: POSIX.1-2008 specifies this behavior as well */ |
| 108 | return realpath(path, NULL); |
| 109 | #else |
| 110 | char buf[PATH_MAX+1]; |
| 111 | |
| 112 | /* on error returns NULL (xstrdup(NULL) == NULL) */ |
| 113 | return xstrdup(realpath(path, buf)); |
| 114 | #endif |
| 115 | } |