2 Copyright (C) 2006-2009 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 <http://www.gnu.org/licenses/>. */
28 #include <sys/types.h>
31 #include "canonicalize.h"
33 #ifndef REPLACE_OPEN_DIRECTORY
34 # define REPLACE_OPEN_DIRECTORY 0
37 /* This replacement assumes that a directory is not renamed while opened
38 through a file descriptor.
40 FIXME: On mingw, this would be possible to enforce if we were to
41 also open a HANDLE to each directory currently visited by a file
42 descriptor, since mingw refuses to rename any in-use file system
45 /* Array of file descriptors opened. If it points to a directory, it stores
46 info about this directory; otherwise it stores an errno value of ENOTDIR. */
49 char *name; /* Absolute name of the directory, or NULL. */
50 int saved_errno; /* If name == NULL: The error code describing the failure
53 static dir_info_t *dirs;
54 static size_t dirs_allocated;
56 /* Try to ensure dirs has enough room for a slot at index fd. */
58 ensure_dirs_slot (size_t fd)
60 if (fd >= dirs_allocated)
66 new_allocated = 2 * dirs_allocated + 1;
67 if (new_allocated <= fd)
68 new_allocated = fd + 1;
71 ? (dir_info_t *) realloc (dirs, new_allocated * sizeof (dir_info_t))
72 : (dir_info_t *) malloc (new_allocated * sizeof (dir_info_t)));
75 for (i = dirs_allocated; i < new_allocated; i++)
77 new_dirs[i].name = NULL;
78 new_dirs[i].saved_errno = ENOTDIR;
81 dirs_allocated = new_allocated;
86 /* Hook into the gnulib replacements for open() and close() to keep track
87 of the open file descriptors. */
89 /* Close FD, cleaning up any fd to name mapping if fd was visiting a
92 _gl_unregister_fd (int fd)
94 if (fd >= 0 && fd < dirs_allocated)
96 if (dirs[fd].name != NULL)
99 dirs[fd].saved_errno = ENOTDIR;
103 /* Mark FD as visiting FILENAME. FD must be positive, and refer to an
104 open file descriptor. If REPLACE_OPEN_DIRECTORY is non-zero, this
105 should only be called if FD is visiting a directory. */
107 _gl_register_fd (int fd, const char *filename)
111 ensure_dirs_slot (fd);
112 if (fd < dirs_allocated
113 && (REPLACE_OPEN_DIRECTORY
114 || (fstat (fd, &statbuf) >= 0 && S_ISDIR (statbuf.st_mode))))
116 dirs[fd].name = canonicalize_file_name (filename);
117 if (dirs[fd].name == NULL)
118 dirs[fd].saved_errno = errno;
122 /* Return stat information about FD in STATBUF. Needed when
123 rpl_open() used a dummy file to work around an open() that can't
124 normally visit directories. */
125 #if REPLACE_OPEN_DIRECTORY
127 rpl_fstat (int fd, struct stat *statbuf)
129 if (0 <= fd && fd <= dirs_allocated && dirs[fd].name != NULL)
130 return stat (dirs[fd].name, statbuf);
131 return fstat (fd, statbuf);
135 /* Override opendir() and closedir(), to keep track of the open file
136 descriptors. Needed because there is a function dirfd(). */
139 rpl_closedir (DIR *dp)
143 int retval = closedir (dp);
146 _gl_unregister_fd (fd);
151 rpl_opendir (const char *filename)
156 dp = opendir (filename);
161 _gl_register_fd (fd, filename);
166 /* Override dup() and dup2(), to keep track of open file descriptors. */
172 int newfd = dup (oldfd);
174 if (oldfd >= 0 && newfd >= 0)
176 ensure_dirs_slot (newfd);
177 if (newfd < dirs_allocated)
179 if (oldfd < dirs_allocated)
181 if (dirs[oldfd].name != NULL)
183 dirs[newfd].name = strdup (dirs[oldfd].name);
184 if (dirs[newfd].name == NULL)
185 dirs[newfd].saved_errno = ENOMEM;
189 dirs[newfd].name = NULL;
190 dirs[newfd].saved_errno = dirs[oldfd].saved_errno;
195 dirs[newfd].name = NULL;
196 dirs[newfd].saved_errno = ENOMEM;
203 /* Our <unistd.h> replacement overrides dup2 twice; be sure to pick
207 # define dup2 rpl_dup2
211 rpl_dup2_fchdir (int oldfd, int newfd)
213 int retval = dup2 (oldfd, newfd);
215 if (retval >= 0 && newfd != oldfd)
217 ensure_dirs_slot (newfd);
218 if (newfd < dirs_allocated)
220 if (oldfd < dirs_allocated)
222 if (dirs[oldfd].name != NULL)
224 dirs[newfd].name = strdup (dirs[oldfd].name);
225 if (dirs[newfd].name == NULL)
226 dirs[newfd].saved_errno = ENOMEM;
230 dirs[newfd].name = NULL;
231 dirs[newfd].saved_errno = dirs[oldfd].saved_errno;
236 dirs[newfd].name = NULL;
237 dirs[newfd].saved_errno = ENOMEM;
244 /* Implement fchdir() in terms of chdir(). */
249 if (fd >= 0 && fd < dirs_allocated && dirs[fd].name != NULL)
250 return chdir (dirs[fd].name);
251 /* At this point, fd is either invalid, or open but not a directory.
252 If dup2 fails, errno is correctly EBADF. */
255 if (dup2 (fd, fd) == fd)