9b64e0294bcd823a4b5149d35d868cb4d9dfd69a
[gnulib.git] / lib / fchdir.c
1 /* fchdir replacement.
2    Copyright (C) 2006-2009 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>.  */
16
17 #include <config.h>
18
19 /* Specification.  */
20 #include <unistd.h>
21
22 #include <assert.h>
23 #include <dirent.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <stdbool.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31
32 #include "canonicalize.h"
33
34 #ifndef REPLACE_OPEN_DIRECTORY
35 # define REPLACE_OPEN_DIRECTORY 0
36 #endif
37
38 /* This replacement assumes that a directory is not renamed while opened
39    through a file descriptor.
40
41    FIXME: On mingw, this would be possible to enforce if we were to
42    also open a HANDLE to each directory currently visited by a file
43    descriptor, since mingw refuses to rename any in-use file system
44    object.  */
45
46 /* Array of file descriptors opened.  If it points to a directory, it stores
47    info about this directory.  */
48 typedef struct
49 {
50   char *name;       /* Absolute name of the directory, or NULL.  */
51   /* FIXME - add a DIR* member to make dirfd possible on mingw?  */
52 } dir_info_t;
53 static dir_info_t *dirs;
54 static size_t dirs_allocated;
55
56 /* Try to ensure dirs has enough room for a slot at index fd.  Return
57    false and set errno to ENOMEM on allocation failure.  */
58 static bool
59 ensure_dirs_slot (size_t fd)
60 {
61   if (fd >= dirs_allocated)
62     {
63       size_t new_allocated;
64       dir_info_t *new_dirs;
65
66       new_allocated = 2 * dirs_allocated + 1;
67       if (new_allocated <= fd)
68         new_allocated = fd + 1;
69       new_dirs =
70         (dirs != NULL
71          ? (dir_info_t *) realloc (dirs, new_allocated * sizeof *dirs)
72          : (dir_info_t *) malloc (new_allocated * sizeof *dirs));
73       if (new_dirs == NULL)
74         return false;
75       memset (new_dirs + dirs_allocated, 0,
76               (new_allocated - dirs_allocated) * sizeof *dirs);
77       dirs = new_dirs;
78       dirs_allocated = new_allocated;
79     }
80   return true;
81 }
82
83 /* Hook into the gnulib replacements for open() and close() to keep track
84    of the open file descriptors.  */
85
86 /* Close FD, cleaning up any fd to name mapping if fd was visiting a
87    directory.  */
88 void
89 _gl_unregister_fd (int fd)
90 {
91   if (fd >= 0 && fd < dirs_allocated)
92     {
93       free (dirs[fd].name);
94       dirs[fd].name = NULL;
95     }
96 }
97
98 /* Mark FD as visiting FILENAME.  FD must be non-negative, and refer
99    to an open file descriptor.  If REPLACE_OPEN_DIRECTORY is non-zero,
100    this should only be called if FD is visiting a directory.  Close FD
101    and return -1 if there is insufficient memory to track the
102    directory name; otherwise return FD.  */
103 int
104 _gl_register_fd (int fd, const char *filename)
105 {
106   struct stat statbuf;
107
108   assert (0 <= fd);
109   if (REPLACE_OPEN_DIRECTORY
110       || (fstat (fd, &statbuf) == 0 && S_ISDIR (statbuf.st_mode)))
111     {
112       if (!ensure_dirs_slot (fd)
113           || (dirs[fd].name = canonicalize_file_name (filename)) == NULL)
114         {
115           int saved_errno = errno;
116           close (fd);
117           errno = saved_errno;
118           return -1;
119         }
120     }
121   return fd;
122 }
123
124 /* Mark NEWFD as a duplicate of OLDFD; useful from dup, dup2, dup3,
125    and fcntl.  Both arguments must be valid and distinct file
126    descriptors.  Close NEWFD and return -1 if OLDFD is tracking a
127    directory, but there is insufficient memory to track the same
128    directory in NEWFD; otherwise return NEWFD.
129
130    FIXME: Need to implement rpl_fcntl in gnulib, and have it call
131    this.  */
132 int
133 _gl_register_dup (int oldfd, int newfd)
134 {
135   assert (0 <= oldfd && 0 <= newfd && oldfd != newfd);
136   if (oldfd < dirs_allocated && dirs[oldfd].name)
137     {
138       /* Duplicated a directory; must ensure newfd is allocated.  */
139       if (!ensure_dirs_slot (newfd)
140           || (dirs[newfd].name = strdup (dirs[oldfd].name)) == NULL)
141         {
142           int saved_errno = errno;
143           close (newfd);
144           errno = saved_errno;
145           newfd = -1;
146         }
147     }
148   else if (newfd < dirs_allocated)
149     {
150       /* Duplicated a non-directory; ensure newfd is cleared.  */
151       free (dirs[newfd].name);
152       dirs[newfd].name = NULL;
153     }
154   return newfd;
155 }
156
157 /* Return stat information about FD in STATBUF.  Needed when
158    rpl_open() used a dummy file to work around an open() that can't
159    normally visit directories.  */
160 #if REPLACE_OPEN_DIRECTORY
161 int
162 rpl_fstat (int fd, struct stat *statbuf)
163 {
164   if (0 <= fd && fd < dirs_allocated && dirs[fd].name != NULL)
165     return stat (dirs[fd].name, statbuf);
166   return fstat (fd, statbuf);
167 }
168 #endif
169
170 /* Override opendir() and closedir(), to keep track of the open file
171    descriptors.  Needed because there is a function dirfd().  */
172
173 int
174 rpl_closedir (DIR *dp)
175 #undef closedir
176 {
177   int fd = dirfd (dp);
178   int retval = closedir (dp);
179
180   if (retval >= 0)
181     _gl_unregister_fd (fd);
182   return retval;
183 }
184
185 DIR *
186 rpl_opendir (const char *filename)
187 #undef opendir
188 {
189   DIR *dp;
190
191   dp = opendir (filename);
192   if (dp != NULL)
193     {
194       int fd = dirfd (dp);
195       if (0 <= fd && _gl_register_fd (fd, filename) != fd)
196         {
197           int saved_errno = errno;
198           closedir (dp);
199           errno = saved_errno;
200           return NULL;
201         }
202     }
203   return dp;
204 }
205
206 /* Override dup(), to keep track of open file descriptors.  */
207
208 int
209 rpl_dup (int oldfd)
210 #undef dup
211 {
212   int newfd = dup (oldfd);
213
214   if (0 <= newfd)
215     newfd = _gl_register_dup (oldfd, newfd);
216   return newfd;
217 }
218
219
220 /* Implement fchdir() in terms of chdir().  */
221
222 int
223 fchdir (int fd)
224 {
225   if (fd >= 0 && fd < dirs_allocated && dirs[fd].name != NULL)
226     return chdir (dirs[fd].name);
227   /* At this point, fd is either invalid, or open but not a directory.
228      If dup2 fails, errno is correctly EBADF.  */
229   if (0 <= fd)
230     {
231       if (dup2 (fd, fd) == fd)
232         errno = ENOTDIR;
233     }
234   else
235     errno = EBADF;
236   return -1;
237 }