careadlinkat.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* Read symbolic links into a buffer without size limitation, relative to fd.
  2. Copyright (C) 2001, 2003-2004, 2007, 2009-2011 Free Software Foundation,
  3. 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.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. /* Written by Paul Eggert, Bruno Haible, and Jim Meyering. */
  15. #include <config.h>
  16. #include "careadlinkat.h"
  17. #include <errno.h>
  18. #include <limits.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. /* Define this independently so that stdint.h is not a prerequisite. */
  23. #ifndef SIZE_MAX
  24. # define SIZE_MAX ((size_t) -1)
  25. #endif
  26. #ifndef SSIZE_MAX
  27. # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
  28. #endif
  29. #include "allocator.h"
  30. /* Get the symbolic link value of FILENAME and put it into BUFFER, with
  31. size BUFFER_SIZE. This function acts like readlink but has
  32. readlinkat's signature. */
  33. ssize_t
  34. careadlinkatcwd (int fd, char const *filename, char *buffer,
  35. size_t buffer_size)
  36. {
  37. /* FD must be AT_FDCWD here, otherwise the caller is using this
  38. function in contexts for which it was not meant for. */
  39. if (fd != AT_FDCWD)
  40. abort ();
  41. return readlink (filename, buffer, buffer_size);
  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. If the link is small enough to fit into BUFFER put it there.
  48. BUFFER's size is BUFFER_SIZE, and BUFFER can be null
  49. if BUFFER_SIZE is zero.
  50. If the link is not small, put it into a dynamically allocated
  51. buffer managed by ALLOC. It is the caller's responsibility to free
  52. the returned value if it is nonnull and is not BUFFER. A null
  53. ALLOC stands for the standard allocator.
  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.
  58. If successful, return the buffer address; otherwise return NULL and
  59. set errno. */
  60. char *
  61. careadlinkat (int fd, char const *filename,
  62. char *buffer, size_t buffer_size,
  63. struct allocator const *alloc,
  64. ssize_t (*preadlinkat) (int, char const *, char *, size_t))
  65. {
  66. char *buf;
  67. size_t buf_size;
  68. size_t buf_size_max =
  69. SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;
  70. char stack_buf[1024];
  71. if (! alloc)
  72. alloc = &stdlib_allocator;
  73. if (! buffer_size)
  74. {
  75. /* Allocate the initial buffer on the stack. This way, in the
  76. common case of a symlink of small size, we get away with a
  77. single small malloc() instead of a big malloc() followed by a
  78. shrinking realloc(). */
  79. buffer = stack_buf;
  80. buffer_size = sizeof stack_buf;
  81. }
  82. buf = buffer;
  83. buf_size = buffer_size;
  84. do
  85. {
  86. /* Attempt to read the link into the current buffer. */
  87. ssize_t link_length = preadlinkat (fd, filename, buf, buf_size);
  88. size_t link_size;
  89. if (link_length < 0)
  90. {
  91. /* On AIX 5L v5.3 and HP-UX 11i v2 04/09, readlink returns -1
  92. with errno == ERANGE if the buffer is too small. */
  93. int readlinkat_errno = errno;
  94. if (readlinkat_errno != ERANGE)
  95. {
  96. if (buf != buffer)
  97. {
  98. alloc->free (buf);
  99. errno = readlinkat_errno;
  100. }
  101. return NULL;
  102. }
  103. }
  104. link_size = link_length;
  105. if (link_size < buf_size)
  106. {
  107. buf[link_size++] = '\0';
  108. if (buf == stack_buf)
  109. {
  110. char *b = (char *) alloc->allocate (link_size);
  111. buf_size = link_size;
  112. if (! b)
  113. break;
  114. memcpy (b, buf, link_size);
  115. buf = b;
  116. }
  117. else if (link_size < buf_size && buf != buffer && alloc->reallocate)
  118. {
  119. /* Shrink BUF before returning it. */
  120. char *b = (char *) alloc->reallocate (buf, link_size);
  121. if (b)
  122. buf = b;
  123. }
  124. return buf;
  125. }
  126. if (buf != buffer)
  127. alloc->free (buf);
  128. if (buf_size <= buf_size_max / 2)
  129. buf_size *= 2;
  130. else if (buf_size < buf_size_max)
  131. buf_size = buf_size_max;
  132. else if (buf_size_max < SIZE_MAX)
  133. {
  134. errno = ENAMETOOLONG;
  135. return NULL;
  136. }
  137. else
  138. break;
  139. buf = (char *) alloc->allocate (buf_size);
  140. }
  141. while (buf);
  142. if (alloc->die)
  143. alloc->die (buf_size);
  144. errno = ENOMEM;
  145. return NULL;
  146. }