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)
98 dirs[fd].saved_errno = ENOTDIR;
102 /* Mark FD as visiting FILENAME. FD must be positive, and refer to an
103 open file descriptor. If REPLACE_OPEN_DIRECTORY is non-zero, this
104 should only be called if FD is visiting a directory. */
106 _gl_register_fd (int fd, const char *filename)
110 ensure_dirs_slot (fd);
111 if (fd < dirs_allocated
112 && (REPLACE_OPEN_DIRECTORY
113 || (fstat (fd, &statbuf) >= 0 && S_ISDIR (statbuf.st_mode))))
115 dirs[fd].name = canonicalize_file_name (filename);
116 if (dirs[fd].name == NULL)
117 dirs[fd].saved_errno = errno;
121 /* Return stat information about FD in STATBUF. Needed when
122 rpl_open() used a dummy file to work around an open() that can't
123 normally visit directories. */
124 #if REPLACE_OPEN_DIRECTORY
126 rpl_fstat (int fd, struct stat *statbuf)
128 if (0 <= fd && fd < dirs_allocated && dirs[fd].name != NULL)
129 return stat (dirs[fd].name, statbuf);
130 return fstat (fd, statbuf);
134 /* Override opendir() and closedir(), to keep track of the open file
135 descriptors. Needed because there is a function dirfd(). */
138 rpl_closedir (DIR *dp)
142 int retval = closedir (dp);
145 _gl_unregister_fd (fd);
150 rpl_opendir (const char *filename)
155 dp = opendir (filename);
160 _gl_register_fd (fd, filename);
165 /* Override dup() and dup2(), to keep track of open file descriptors. */
171 int newfd = dup (oldfd);
173 if (oldfd >= 0 && newfd >= 0)
175 ensure_dirs_slot (newfd);
176 if (newfd < dirs_allocated)
178 if (oldfd < dirs_allocated)
180 if (dirs[oldfd].name != NULL)
182 dirs[newfd].name = strdup (dirs[oldfd].name);
183 if (dirs[newfd].name == NULL)
184 dirs[newfd].saved_errno = ENOMEM;
188 dirs[newfd].name = NULL;
189 dirs[newfd].saved_errno = dirs[oldfd].saved_errno;
194 dirs[newfd].name = NULL;
195 dirs[newfd].saved_errno = ENOMEM;
202 /* Our <unistd.h> replacement overrides dup2 twice; be sure to pick
206 # define dup2 rpl_dup2
210 rpl_dup2_fchdir (int oldfd, int newfd)
212 int retval = dup2 (oldfd, newfd);
214 if (retval >= 0 && newfd != oldfd)
216 ensure_dirs_slot (newfd);
217 if (newfd < dirs_allocated)
219 if (oldfd < dirs_allocated)
221 if (dirs[oldfd].name != NULL)
223 dirs[newfd].name = strdup (dirs[oldfd].name);
224 if (dirs[newfd].name == NULL)
225 dirs[newfd].saved_errno = ENOMEM;
229 dirs[newfd].name = NULL;
230 dirs[newfd].saved_errno = dirs[oldfd].saved_errno;
235 dirs[newfd].name = NULL;
236 dirs[newfd].saved_errno = ENOMEM;
243 /* Implement fchdir() in terms of chdir(). */
248 if (fd >= 0 && fd < dirs_allocated && dirs[fd].name != NULL)
249 return chdir (dirs[fd].name);
250 /* At this point, fd is either invalid, or open but not a directory.
251 If dup2 fails, errno is correctly EBADF. */
254 if (dup2 (fd, fd) == fd)