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 2, or (at your option)
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, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
26 #include <sys/types.h>
30 #include "canonicalize.h"
33 /* This replacement assumes that a directory is not renamed while opened
34 through a file descriptor. */
36 /* Array of file descriptors opened. If it points to a directory, it stores
37 info about this directory; otherwise it stores an errno value of ENOTDIR. */
40 char *name; /* Absolute name of the directory, or NULL. */
41 int saved_errno; /* If name == NULL: The error code describing the failure
44 static dir_info_t *dirs;
45 static size_t dirs_allocated;
47 /* Try to ensure dirs has enough room for a slot at index fd. */
49 ensure_dirs_slot (size_t fd)
51 if (fd >= dirs_allocated)
57 new_allocated = 2 * dirs_allocated + 1;
58 if (new_allocated <= fd)
59 new_allocated = fd + 1;
62 ? (dir_info_t *) realloc (dirs, new_allocated * sizeof (dir_info_t))
63 : (dir_info_t *) malloc (new_allocated * sizeof (dir_info_t)));
66 for (i = dirs_allocated; i < new_allocated; i++)
68 new_dirs[i].name = NULL;
69 new_dirs[i].saved_errno = ENOTDIR;
72 dirs_allocated = new_allocated;
77 /* Override open() and close(), to keep track of the open file descriptors. */
83 int retval = close (fd);
85 if (retval >= 0 && fd >= 0 && fd < dirs_allocated)
87 if (dirs[fd].name != NULL)
90 dirs[fd].saved_errno = ENOTDIR;
96 open (const char *filename, int flags, ...)
107 va_start (arg, flags);
109 /* If mode_t is narrower than int, use the promoted type (int),
110 not mode_t. Use sizeof to guess whether mode_t is narrower;
111 we don't know of any practical counterexamples. */
112 mode = (sizeof (mode_t) < sizeof (int)
114 : va_arg (arg, mode_t));
118 fd = open (filename, flags, mode);
121 ensure_dirs_slot (fd);
122 if (fd < dirs_allocated
123 && fstat (fd, &statbuf) >= 0 && S_ISDIR (statbuf.st_mode))
125 dirs[fd].name = canonicalize_file_name (filename);
126 if (dirs[fd].name == NULL)
127 dirs[fd].saved_errno = errno;
133 /* Override opendir() and closedir(), to keep track of the open file
134 descriptors. Needed because there is a function dirfd(). */
141 int retval = closedir (dp);
143 if (retval >= 0 && fd >= 0 && fd < dirs_allocated)
145 if (dirs[fd].name != NULL)
146 free (dirs[fd].name);
147 dirs[fd].name = NULL;
148 dirs[fd].saved_errno = ENOTDIR;
154 opendir (const char *filename)
159 dp = opendir (filename);
165 ensure_dirs_slot (fd);
166 if (fd < dirs_allocated)
168 dirs[fd].name = canonicalize_file_name (filename);
169 if (dirs[fd].name == NULL)
170 dirs[fd].saved_errno = errno;
177 /* Override dup() and dup2(), to keep track of open file descriptors. */
183 int newfd = dup (oldfd);
185 if (oldfd >= 0 && newfd >= 0)
187 ensure_dirs_slot (newfd);
188 if (newfd < dirs_allocated)
190 if (oldfd < dirs_allocated)
192 if (dirs[oldfd].name != NULL)
194 dirs[newfd].name = strdup (dirs[oldfd].name);
195 if (dirs[newfd].name == NULL)
196 dirs[newfd].saved_errno = ENOMEM;
200 dirs[newfd].name = NULL;
201 dirs[newfd].saved_errno = dirs[oldfd].saved_errno;
206 dirs[newfd].name = NULL;
207 dirs[newfd].saved_errno = ENOMEM;
215 dup2 (int oldfd, int newfd)
218 int retval = dup2 (oldfd, newfd);
220 if (retval >= 0 && oldfd >= 0 && newfd >= 0 && newfd != oldfd)
222 ensure_dirs_slot (newfd);
223 if (newfd < dirs_allocated)
225 if (oldfd < dirs_allocated)
227 if (dirs[oldfd].name != NULL)
229 dirs[newfd].name = strdup (dirs[oldfd].name);
230 if (dirs[newfd].name == NULL)
231 dirs[newfd].saved_errno = ENOMEM;
235 dirs[newfd].name = NULL;
236 dirs[newfd].saved_errno = dirs[oldfd].saved_errno;
241 dirs[newfd].name = NULL;
242 dirs[newfd].saved_errno = ENOMEM;
249 /* Implement fchdir() in terms of chdir(). */
256 if (fd < dirs_allocated)
258 if (dirs[fd].name != NULL)
259 return chdir (dirs[fd].name);
262 errno = dirs[fd].saved_errno;