Use fdopendir, fstatat and readlinkat, for efficiency.
[bpt/emacs.git] / lib / careadlinkat.c
CommitLineData
d1fdcab7
PE
1/* Read symbolic links into a buffer without size limitation, relative to fd.
2
ab422c4d 3 Copyright (C) 2001, 2003-2004, 2007, 2009-2013 Free Software Foundation,
d1fdcab7
PE
4 Inc.
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19/* Written by Paul Eggert, Bruno Haible, and Jim Meyering. */
20
21#include <config.h>
22
23#include "careadlinkat.h"
24
d1fdcab7
PE
25#include <errno.h>
26#include <limits.h>
d1fdcab7 27#include <string.h>
d1fdcab7 28
d1fdcab7
PE
29/* Define this independently so that stdint.h is not a prerequisite. */
30#ifndef SIZE_MAX
31# define SIZE_MAX ((size_t) -1)
32#endif
33
34#ifndef SSIZE_MAX
35# define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
36#endif
37
69d9a57d
PE
38#include "allocator.h"
39
d1fdcab7
PE
40/* Assuming the current directory is FD, get the symbolic link value
41 of FILENAME as a null-terminated string and put it into a buffer.
42 If FD is AT_FDCWD, FILENAME is interpreted relative to the current
43 working directory, as in openat.
44
45 If the link is small enough to fit into BUFFER put it there.
46 BUFFER's size is BUFFER_SIZE, and BUFFER can be null
47 if BUFFER_SIZE is zero.
48
49 If the link is not small, put it into a dynamically allocated
50 buffer managed by ALLOC. It is the caller's responsibility to free
51 the returned value if it is nonnull and is not BUFFER. A null
52 ALLOC stands for the standard allocator.
53
e5b28a7a
PE
54 The PREADLINKAT function specifies how to read links. It operates
55 like POSIX readlinkat()
56 <http://pubs.opengroup.org/onlinepubs/9699919799/functions/readlink.html>
57 but can assume that its first argument is the same as FD.
d1fdcab7
PE
58
59 If successful, return the buffer address; otherwise return NULL and
60 set errno. */
61
62char *
63careadlinkat (int fd, char const *filename,
64 char *buffer, size_t buffer_size,
65 struct allocator const *alloc,
66 ssize_t (*preadlinkat) (int, char const *, char *, size_t))
67{
68 char *buf;
69 size_t buf_size;
70 size_t buf_size_max =
71 SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;
72 char stack_buf[1024];
73
9e0e57c7 74 if (! alloc)
7ec98caf 75 alloc = &stdlib_allocator;
d1fdcab7
PE
76
77 if (! buffer_size)
78 {
79 /* Allocate the initial buffer on the stack. This way, in the
80 common case of a symlink of small size, we get away with a
81 single small malloc() instead of a big malloc() followed by a
82 shrinking realloc(). */
83 buffer = stack_buf;
84 buffer_size = sizeof stack_buf;
85 }
86
87 buf = buffer;
88 buf_size = buffer_size;
89
90 do
91 {
92 /* Attempt to read the link into the current buffer. */
93 ssize_t link_length = preadlinkat (fd, filename, buf, buf_size);
94 size_t link_size;
95 if (link_length < 0)
96 {
97 /* On AIX 5L v5.3 and HP-UX 11i v2 04/09, readlink returns -1
98 with errno == ERANGE if the buffer is too small. */
99 int readlinkat_errno = errno;
100 if (readlinkat_errno != ERANGE)
101 {
102 if (buf != buffer)
103 {
9e0e57c7 104 alloc->free (buf);
d1fdcab7
PE
105 errno = readlinkat_errno;
106 }
107 return NULL;
108 }
109 }
110
111 link_size = link_length;
112
113 if (link_size < buf_size)
114 {
115 buf[link_size++] = '\0';
116
117 if (buf == stack_buf)
118 {
7ec98caf 119 char *b = (char *) alloc->allocate (link_size);
f797625a 120 buf_size = link_size;
d1fdcab7
PE
121 if (! b)
122 break;
123 memcpy (b, buf, link_size);
124 buf = b;
125 }
7ec98caf 126 else if (link_size < buf_size && buf != buffer && alloc->reallocate)
d1fdcab7
PE
127 {
128 /* Shrink BUF before returning it. */
7ec98caf 129 char *b = (char *) alloc->reallocate (buf, link_size);
d1fdcab7
PE
130 if (b)
131 buf = b;
132 }
133
134 return buf;
135 }
136
137 if (buf != buffer)
9e0e57c7 138 alloc->free (buf);
d1fdcab7
PE
139
140 if (buf_size <= buf_size_max / 2)
141 buf_size *= 2;
142 else if (buf_size < buf_size_max)
143 buf_size = buf_size_max;
f797625a
PE
144 else if (buf_size_max < SIZE_MAX)
145 {
146 errno = ENAMETOOLONG;
147 return NULL;
148 }
d1fdcab7
PE
149 else
150 break;
7ec98caf 151 buf = (char *) alloc->allocate (buf_size);
d1fdcab7
PE
152 }
153 while (buf);
154
9e0e57c7 155 if (alloc->die)
f797625a 156 alloc->die (buf_size);
d1fdcab7
PE
157 errno = ENOMEM;
158 return NULL;
159}