2 Copyright (C) 2006, 2007 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/>. */
27 #include <sys/types.h>
31 #include "canonicalize.h"
34 /* This replacement assumes that a directory is not renamed while opened
35 through a file descriptor. */
37 /* Array of file descriptors opened. If it points to a directory, it stores
38 info about this directory; otherwise it stores an errno value of ENOTDIR. */
41 char *name; /* Absolute name of the directory, or NULL. */
42 int saved_errno; /* If name == NULL: The error code describing the failure
45 static dir_info_t *dirs;
46 static size_t dirs_allocated;
48 /* Try to ensure dirs has enough room for a slot at index fd. */
50 ensure_dirs_slot (size_t fd)
52 if (fd >= dirs_allocated)
58 new_allocated = 2 * dirs_allocated + 1;
59 if (new_allocated <= fd)
60 new_allocated = fd + 1;
63 ? (dir_info_t *) realloc (dirs, new_allocated * sizeof (dir_info_t))
64 : (dir_info_t *) malloc (new_allocated * sizeof (dir_info_t)));
67 for (i = dirs_allocated; i < new_allocated; i++)
69 new_dirs[i].name = NULL;
70 new_dirs[i].saved_errno = ENOTDIR;
73 dirs_allocated = new_allocated;
78 /* Override open() and close(), to keep track of the open file descriptors. */
84 int retval = close (fd);
86 if (retval >= 0 && fd >= 0 && fd < dirs_allocated)
88 if (dirs[fd].name != NULL)
91 dirs[fd].saved_errno = ENOTDIR;
97 rpl_open (const char *filename, int flags, ...)
108 va_start (arg, flags);
110 /* If mode_t is narrower than int, use the promoted type (int),
111 not mode_t. Use sizeof to guess whether mode_t is narrower;
112 we don't know of any practical counterexamples. */
113 mode = (sizeof (mode_t) < sizeof (int)
115 : va_arg (arg, mode_t));
119 #if defined GNULIB_OPEN && ((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__)
120 if (strcmp (filename, "/dev/null") == 0)
123 fd = open (filename, flags, mode);
126 ensure_dirs_slot (fd);
127 if (fd < dirs_allocated
128 && fstat (fd, &statbuf) >= 0 && S_ISDIR (statbuf.st_mode))
130 dirs[fd].name = canonicalize_file_name (filename);
131 if (dirs[fd].name == NULL)
132 dirs[fd].saved_errno = errno;
138 /* Override opendir() and closedir(), to keep track of the open file
139 descriptors. Needed because there is a function dirfd(). */
142 rpl_closedir (DIR *dp)
146 int retval = closedir (dp);
148 if (retval >= 0 && fd >= 0 && fd < dirs_allocated)
150 if (dirs[fd].name != NULL)
151 free (dirs[fd].name);
152 dirs[fd].name = NULL;
153 dirs[fd].saved_errno = ENOTDIR;
159 rpl_opendir (const char *filename)
164 dp = opendir (filename);
170 ensure_dirs_slot (fd);
171 if (fd < dirs_allocated)
173 dirs[fd].name = canonicalize_file_name (filename);
174 if (dirs[fd].name == NULL)
175 dirs[fd].saved_errno = errno;
182 /* Override dup() and dup2(), to keep track of open file descriptors. */
188 int newfd = dup (oldfd);
190 if (oldfd >= 0 && newfd >= 0)
192 ensure_dirs_slot (newfd);
193 if (newfd < dirs_allocated)
195 if (oldfd < dirs_allocated)
197 if (dirs[oldfd].name != NULL)
199 dirs[newfd].name = strdup (dirs[oldfd].name);
200 if (dirs[newfd].name == NULL)
201 dirs[newfd].saved_errno = ENOMEM;
205 dirs[newfd].name = NULL;
206 dirs[newfd].saved_errno = dirs[oldfd].saved_errno;
211 dirs[newfd].name = NULL;
212 dirs[newfd].saved_errno = ENOMEM;
220 rpl_dup2 (int oldfd, int newfd)
223 int retval = dup2 (oldfd, newfd);
225 if (retval >= 0 && oldfd >= 0 && newfd >= 0 && newfd != oldfd)
227 ensure_dirs_slot (newfd);
228 if (newfd < dirs_allocated)
230 if (oldfd < dirs_allocated)
232 if (dirs[oldfd].name != NULL)
234 dirs[newfd].name = strdup (dirs[oldfd].name);
235 if (dirs[newfd].name == NULL)
236 dirs[newfd].saved_errno = ENOMEM;
240 dirs[newfd].name = NULL;
241 dirs[newfd].saved_errno = dirs[oldfd].saved_errno;
246 dirs[newfd].name = NULL;
247 dirs[newfd].saved_errno = ENOMEM;
254 /* Implement fchdir() in terms of chdir(). */
261 if (fd < dirs_allocated)
263 if (dirs[fd].name != NULL)
264 return chdir (dirs[fd].name);
267 errno = dirs[fd].saved_errno;