fchdir.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* fchdir replacement.
  2. Copyright (C) 2006-2021 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. #include <config.h>
  14. /* Specification. */
  15. #include <unistd.h>
  16. #include <dirent.h>
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <stdbool.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include "assure.h"
  25. #include "filename.h"
  26. #include "filenamecat.h"
  27. #ifndef REPLACE_OPEN_DIRECTORY
  28. # define REPLACE_OPEN_DIRECTORY 0
  29. #endif
  30. /* This replacement assumes that a directory is not renamed while opened
  31. through a file descriptor.
  32. FIXME: On mingw, this would be possible to enforce if we were to
  33. also open a HANDLE to each directory currently visited by a file
  34. descriptor, since mingw refuses to rename any in-use file system
  35. object. */
  36. /* Array of file descriptors opened. If REPLACE_OPEN_DIRECTORY or if it points
  37. to a directory, it stores info about this directory. */
  38. typedef struct
  39. {
  40. char *name; /* Absolute name of the directory, or NULL. */
  41. /* FIXME - add a DIR* member to make dirfd possible on mingw? */
  42. } dir_info_t;
  43. static dir_info_t *dirs;
  44. static size_t dirs_allocated;
  45. /* Try to ensure dirs has enough room for a slot at index fd; free any
  46. contents already in that slot. Return false and set errno to
  47. ENOMEM on allocation failure. */
  48. static bool
  49. ensure_dirs_slot (size_t fd)
  50. {
  51. if (fd < dirs_allocated)
  52. free (dirs[fd].name);
  53. else
  54. {
  55. size_t new_allocated;
  56. dir_info_t *new_dirs;
  57. new_allocated = 2 * dirs_allocated + 1;
  58. if (new_allocated <= fd)
  59. new_allocated = fd + 1;
  60. new_dirs =
  61. (dirs != NULL
  62. ? (dir_info_t *) realloc (dirs, new_allocated * sizeof *dirs)
  63. : (dir_info_t *) malloc (new_allocated * sizeof *dirs));
  64. if (new_dirs == NULL)
  65. return false;
  66. memset (new_dirs + dirs_allocated, 0,
  67. (new_allocated - dirs_allocated) * sizeof *dirs);
  68. dirs = new_dirs;
  69. dirs_allocated = new_allocated;
  70. }
  71. return true;
  72. }
  73. /* Return an absolute name of DIR in malloc'd storage.
  74. Upon failure, return NULL with errno set. */
  75. static char *
  76. get_name (char const *dir)
  77. {
  78. char *cwd;
  79. char *result;
  80. int saved_errno;
  81. if (IS_ABSOLUTE_FILE_NAME (dir))
  82. return strdup (dir);
  83. /* We often encounter "."; treat it as a special case. */
  84. cwd = getcwd (NULL, 0);
  85. if (!cwd || (dir[0] == '.' && dir[1] == '\0'))
  86. return cwd;
  87. result = mfile_name_concat (cwd, dir, NULL);
  88. saved_errno = errno;
  89. free (cwd);
  90. errno = saved_errno;
  91. return result;
  92. }
  93. /* Hook into the gnulib replacements for open() and close() to keep track
  94. of the open file descriptors. */
  95. /* Close FD, cleaning up any fd to name mapping if fd was visiting a
  96. directory. */
  97. void
  98. _gl_unregister_fd (int fd)
  99. {
  100. if (fd >= 0 && fd < dirs_allocated)
  101. {
  102. free (dirs[fd].name);
  103. dirs[fd].name = NULL;
  104. }
  105. }
  106. /* Mark FD as visiting FILENAME. FD must be non-negative, and refer
  107. to an open file descriptor. If REPLACE_OPEN_DIRECTORY is non-zero,
  108. this should only be called if FD is visiting a directory. Close FD
  109. and return -1 with errno set if there is insufficient memory to track
  110. the directory name; otherwise return FD. */
  111. int
  112. _gl_register_fd (int fd, const char *filename)
  113. {
  114. struct stat statbuf;
  115. assure (0 <= fd);
  116. if (REPLACE_OPEN_DIRECTORY
  117. || (fstat (fd, &statbuf) == 0 && S_ISDIR (statbuf.st_mode)))
  118. {
  119. if (!ensure_dirs_slot (fd)
  120. || (dirs[fd].name = get_name (filename)) == NULL)
  121. {
  122. int saved_errno = errno;
  123. close (fd);
  124. errno = saved_errno;
  125. return -1;
  126. }
  127. }
  128. return fd;
  129. }
  130. /* Mark NEWFD as a duplicate of OLDFD; useful from dup, dup2, dup3,
  131. and fcntl. Both arguments must be valid and distinct file
  132. descriptors. Close NEWFD and return -1 if OLDFD is tracking a
  133. directory, but there is insufficient memory to track the same
  134. directory in NEWFD; otherwise return NEWFD. */
  135. int
  136. _gl_register_dup (int oldfd, int newfd)
  137. {
  138. assure (0 <= oldfd && 0 <= newfd && oldfd != newfd);
  139. if (oldfd < dirs_allocated && dirs[oldfd].name)
  140. {
  141. /* Duplicated a directory; must ensure newfd is allocated. */
  142. if (!ensure_dirs_slot (newfd)
  143. || (dirs[newfd].name = strdup (dirs[oldfd].name)) == NULL)
  144. {
  145. int saved_errno = errno;
  146. close (newfd);
  147. errno = saved_errno;
  148. newfd = -1;
  149. }
  150. }
  151. else if (newfd < dirs_allocated)
  152. {
  153. /* Duplicated a non-directory; ensure newfd is cleared. */
  154. free (dirs[newfd].name);
  155. dirs[newfd].name = NULL;
  156. }
  157. return newfd;
  158. }
  159. /* If FD is currently visiting a directory, then return the name of
  160. that directory. Otherwise, return NULL and set errno. */
  161. const char *
  162. _gl_directory_name (int fd)
  163. {
  164. if (0 <= fd && fd < dirs_allocated && dirs[fd].name != NULL)
  165. return dirs[fd].name;
  166. /* At this point, fd is either invalid, or open but not a directory.
  167. If dup2 fails, errno is correctly EBADF. */
  168. if (0 <= fd)
  169. {
  170. if (dup2 (fd, fd) == fd)
  171. errno = ENOTDIR;
  172. }
  173. else
  174. errno = EBADF;
  175. return NULL;
  176. }
  177. /* Implement fchdir() in terms of chdir(). */
  178. int
  179. fchdir (int fd)
  180. {
  181. const char *name = _gl_directory_name (fd);
  182. return name ? chdir (name) : -1;
  183. }