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