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 /* 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 /* Hook into the gnulib replacements for open() and close() to keep track
78 of the open file descriptors. */
81 _gl_unregister_fd (int fd)
83 if (fd >= 0 && fd < dirs_allocated)
85 if (dirs[fd].name != NULL)
88 dirs[fd].saved_errno = ENOTDIR;
93 _gl_register_fd (int fd, const char *filename)
97 ensure_dirs_slot (fd);
98 if (fd < dirs_allocated
99 && fstat (fd, &statbuf) >= 0 && S_ISDIR (statbuf.st_mode))
101 dirs[fd].name = canonicalize_file_name (filename);
102 if (dirs[fd].name == NULL)
103 dirs[fd].saved_errno = errno;
107 /* Override opendir() and closedir(), to keep track of the open file
108 descriptors. Needed because there is a function dirfd(). */
111 rpl_closedir (DIR *dp)
115 int retval = closedir (dp);
118 _gl_unregister_fd (fd);
123 rpl_opendir (const char *filename)
128 dp = opendir (filename);
133 _gl_register_fd (fd, filename);
138 /* Override dup() and dup2(), to keep track of open file descriptors. */
144 int newfd = dup (oldfd);
146 if (oldfd >= 0 && newfd >= 0)
148 ensure_dirs_slot (newfd);
149 if (newfd < dirs_allocated)
151 if (oldfd < dirs_allocated)
153 if (dirs[oldfd].name != NULL)
155 dirs[newfd].name = strdup (dirs[oldfd].name);
156 if (dirs[newfd].name == NULL)
157 dirs[newfd].saved_errno = ENOMEM;
161 dirs[newfd].name = NULL;
162 dirs[newfd].saved_errno = dirs[oldfd].saved_errno;
167 dirs[newfd].name = NULL;
168 dirs[newfd].saved_errno = ENOMEM;
176 rpl_dup2 (int oldfd, int newfd)
179 int retval = dup2 (oldfd, newfd);
181 /* Inline mingw replacement from dup2.c. */
186 if (retval >= 0 && oldfd >= 0 && newfd >= 0 && newfd != oldfd)
188 ensure_dirs_slot (newfd);
189 if (newfd < dirs_allocated)
191 if (oldfd < dirs_allocated)
193 if (dirs[oldfd].name != NULL)
195 dirs[newfd].name = strdup (dirs[oldfd].name);
196 if (dirs[newfd].name == NULL)
197 dirs[newfd].saved_errno = ENOMEM;
201 dirs[newfd].name = NULL;
202 dirs[newfd].saved_errno = dirs[oldfd].saved_errno;
207 dirs[newfd].name = NULL;
208 dirs[newfd].saved_errno = ENOMEM;
215 /* Implement fchdir() in terms of chdir(). */
222 if (fd < dirs_allocated)
224 if (dirs[fd].name != NULL)
225 return chdir (dirs[fd].name);
228 errno = dirs[fd].saved_errno;