openat: don’t close (-1)
[gnulib.git] / lib / readlinkat.c
blobf4d64c0d135299127bc4329071193eea867cccf3
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 */
19 #include <config.h>
21 /* Specification. */
22 #include <unistd.h>
24 #include <errno.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/stat.h>
29 #if HAVE_READLINKAT
31 # undef fstatat
32 # undef readlinkat
34 ssize_t
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(). */
46 struct stat st;
47 if (fstatat (fd, file, &st, 0) == 0 || errno == EOVERFLOW)
48 errno = EINVAL;
49 return -1;
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. */
60 char stackbuf[4032];
61 r = readlinkat (fd, file, stackbuf, sizeof stackbuf);
62 if (r < 0)
64 if (errno == ERANGE)
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);
73 else
75 if (bufsize < r)
76 r = bufsize;
77 memcpy (buf, stackbuf, r);
80 # endif
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] == '\\')
87 errno = EINVAL;
88 return -1;
90 # endif
92 return r;
95 #else
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"
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
123 #endif