1 /* Read a symlink relative to an open directory.
2 Copyright (C) 2009-2024 Free Software Foundation, Inc.
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.
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.
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/>. */
17 /* written by Eric Blake */
35 rpl_readlinkat (int fd
, char const *file
, char *buf
, size_t bufsize
)
37 # if READLINK_TRAILING_SLASH_BUG
38 size_t file_len
= strlen (file
);
39 if (file_len
&& file
[file_len
- 1] == '/')
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(). */
47 if (fstatat (fd
, file
, &st
, 0) == 0 || errno
== EOVERFLOW
)
51 # endif /* READLINK_TRAILING_SLASH_BUG */
53 ssize_t r
= readlinkat (fd
, file
, buf
, bufsize
);
55 # if READLINK_TRUNCATE_BUG
56 if (r
< 0 && errno
== ERANGE
)
58 /* Try again with a bigger buffer. This is just for test cases;
59 real code invariably discards short reads. */
61 r
= readlinkat (fd
, file
, stackbuf
, sizeof stackbuf
);
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? */
77 memcpy (buf
, stackbuf
, r
);
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] == '\\')
97 /* Gnulib provides a readlink stub for mingw; use it for distinction
98 between EINVAL and ENOENT, rather than always failing with ENOSYS. */
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. */
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. */
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"
119 # undef AT_FUNC_POST_FILE_PARAM_DECLS
120 # undef AT_FUNC_POST_FILE_ARGS
121 # undef AT_FUNC_RESULT