2 Copyright (C) 2006-2008 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 /* Hook into the gnulib replacements for open() and close() to keep track
79 of the open file descriptors. */
82 _gl_unregister_fd (int fd)
84 if (fd >= 0 && fd < dirs_allocated)
86 if (dirs[fd].name != NULL)
89 dirs[fd].saved_errno = ENOTDIR;
94 _gl_register_fd (int fd, const char *filename)
98 ensure_dirs_slot (fd);
99 if (fd < dirs_allocated
100 && fstat (fd, &statbuf) >= 0 && S_ISDIR (statbuf.st_mode))
102 dirs[fd].name = canonicalize_file_name (filename);
103 if (dirs[fd].name == NULL)
104 dirs[fd].saved_errno = errno;
108 /* Override opendir() and closedir(), to keep track of the open file
109 descriptors. Needed because there is a function dirfd(). */
112 rpl_closedir (DIR *dp)
116 int retval = closedir (dp);
119 _gl_unregister_fd (fd);
124 rpl_opendir (const char *filename)
129 dp = opendir (filename);
134 _gl_register_fd (fd, filename);
139 /* Override dup() and dup2(), to keep track of open file descriptors. */
145 int newfd = dup (oldfd);
147 if (oldfd >= 0 && newfd >= 0)
149 ensure_dirs_slot (newfd);
150 if (newfd < dirs_allocated)
152 if (oldfd < dirs_allocated)
154 if (dirs[oldfd].name != NULL)
156 dirs[newfd].name = strdup (dirs[oldfd].name);
157 if (dirs[newfd].name == NULL)
158 dirs[newfd].saved_errno = ENOMEM;
162 dirs[newfd].name = NULL;
163 dirs[newfd].saved_errno = dirs[oldfd].saved_errno;
168 dirs[newfd].name = NULL;
169 dirs[newfd].saved_errno = ENOMEM;
177 rpl_dup2 (int oldfd, int newfd)
180 int retval = dup2 (oldfd, newfd);
182 if (retval >= 0 && oldfd >= 0 && newfd >= 0 && newfd != oldfd)
184 ensure_dirs_slot (newfd);
185 if (newfd < dirs_allocated)
187 if (oldfd < dirs_allocated)
189 if (dirs[oldfd].name != NULL)
191 dirs[newfd].name = strdup (dirs[oldfd].name);
192 if (dirs[newfd].name == NULL)
193 dirs[newfd].saved_errno = ENOMEM;
197 dirs[newfd].name = NULL;
198 dirs[newfd].saved_errno = dirs[oldfd].saved_errno;
203 dirs[newfd].name = NULL;
204 dirs[newfd].saved_errno = ENOMEM;
211 /* Implement fchdir() in terms of chdir(). */
218 if (fd < dirs_allocated)
220 if (dirs[fd].name != NULL)
221 return chdir (dirs[fd].name);
224 errno = dirs[fd].saved_errno;