| 1 |
/* Read a symlink relative to an open directory. |
| 2 |
Copyright (C) 2009-2026 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This program is free software: you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU General Public License as published by |
| 6 |
the Free Software Foundation, either version 3 of the License, or |
| 7 |
(at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License |
| 15 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 16 |
|
| 17 |
/* written by Eric Blake */ |
| 18 |
|
| 19 |
#include <config.h> |
| 20 |
|
| 21 |
/* Specification. */ |
| 22 |
#include <unistd.h> |
| 23 |
|
| 24 |
#include <errno.h> |
| 25 |
#include <stdlib.h> |
| 26 |
#include <string.h> |
| 27 |
#include <sys/stat.h> |
| 28 |
|
| 29 |
#if HAVE_READLINKAT |
| 30 |
|
| 31 |
# undef fstatat |
| 32 |
# undef readlinkat |
| 33 |
|
| 34 |
ssize_t |
| 35 |
rpl_readlinkat (int fd, char const *file, char *buf, size_t bufsize) |
| 36 |
{ |
| 37 |
# if READLINK_TRAILING_SLASH_BUG |
| 38 |
size_t file_len = strlen (file); |
| 39 |
if (file_len && file[file_len - 1] == '/') |
| 40 |
{ |
| 41 |
/* Even if FILE without the slash is a symlink to a directory, |
| 42 |
both lstat() and stat() must resolve the trailing slash to |
| 43 |
the directory rather than the symlink. We can therefore |
| 44 |
safely use fstatat(..., 0) to distinguish between EINVAL and |
| 45 |
ENOTDIR/ENOENT, avoiding extra overhead of rpl_fstatat(). */ |
| 46 |
struct stat st; |
| 47 |
if (fstatat (fd, file, &st, 0) == 0 || errno == EOVERFLOW) |
| 48 |
errno = EINVAL; |
| 49 |
return -1; |
| 50 |
} |
| 51 |
# endif /* READLINK_TRAILING_SLASH_BUG */ |
| 52 |
|
| 53 |
ssize_t r = readlinkat (fd, file, buf, bufsize); |
| 54 |
|
| 55 |
# if READLINK_TRUNCATE_BUG |
| 56 |
if (r < 0 && errno == ERANGE) |
| 57 |
{ |
| 58 |
/* Try again with a bigger buffer. This is just for test cases; |
| 59 |
real code invariably discards short reads. */ |
| 60 |
char stackbuf[4032]; |
| 61 |
r = readlinkat (fd, file, stackbuf, sizeof stackbuf); |
| 62 |
if (r < 0) |
| 63 |
{ |
| 64 |
if (errno == ERANGE) |
| 65 |
{ |
| 66 |
/* Clear the buffer, which is good enough for real code. |
| 67 |
Thankfully, no test cases try short reads of enormous |
| 68 |
symlinks and what would be the point anyway? */ |
| 69 |
r = bufsize; |
| 70 |
memset (buf, 0, r); |
| 71 |
} |
| 72 |
} |
| 73 |
else |
| 74 |
{ |
| 75 |
if (bufsize < r) |
| 76 |
r = bufsize; |
| 77 |
memcpy (buf, stackbuf, r); |
| 78 |
} |
| 79 |
} |
| 80 |
# endif |
| 81 |
|
| 82 |
# if defined __CYGWIN__ |
| 83 |
/* On Cygwin 3.3.6, readlinkat(AT_FDCWD,"/dev/null") returns "\\Device\\Null", |
| 84 |
which is unusable. Better fail with EINVAL. */ |
| 85 |
if (r > 0 && strncmp (file, "/dev/", 5) == 0 && buf[0] == '\\') |
| 86 |
{ |
| 87 |
errno = EINVAL; |
| 88 |
return -1; |
| 89 |
} |
| 90 |
# endif |
| 91 |
|
| 92 |
return r; |
| 93 |
} |
| 94 |
|
| 95 |
#else |
| 96 |
|
| 97 |
/* Gnulib provides a readlink stub for mingw; use it for distinction |
| 98 |
between EINVAL and ENOENT, rather than always failing with ENOSYS. */ |
| 99 |
|
| 100 |
/* POSIX 2008 says that unlike readlink, readlinkat returns 0 for |
| 101 |
success instead of the buffer length. But this would render |
| 102 |
readlinkat worthless since readlink does not guarantee a |
| 103 |
NUL-terminated buffer. Assume this was a bug in POSIX. */ |
| 104 |
|
| 105 |
/* Read the contents of symlink FILE into buffer BUF of size BUFSIZE, in the |
| 106 |
directory open on descriptor FD. If possible, do it without changing |
| 107 |
the working directory. Otherwise, resort to using save_cwd/fchdir, |
| 108 |
then readlink/restore_cwd. If either the save_cwd or the restore_cwd |
| 109 |
fails, then give a diagnostic and exit nonzero. */ |
| 110 |
|
| 111 |
# define AT_FUNC_NAME readlinkat |
| 112 |
# define AT_FUNC_F1 readlink |
| 113 |
# define AT_FUNC_POST_FILE_PARAM_DECLS , char *buf, size_t bufsize |
| 114 |
# define AT_FUNC_POST_FILE_ARGS , buf, bufsize |
| 115 |
# define AT_FUNC_RESULT ssize_t |
| 116 |
# include "at-func.c" |
| 117 |
# undef AT_FUNC_NAME |
| 118 |
# undef AT_FUNC_F1 |
| 119 |
# undef AT_FUNC_POST_FILE_PARAM_DECLS |
| 120 |
# undef AT_FUNC_POST_FILE_ARGS |
| 121 |
# undef AT_FUNC_RESULT |
| 122 |
|
| 123 |
#endif |