New module 'close'.
[pspp] / lib / fchdir.c
1 /* fchdir replacement.
2    Copyright (C) 2006-2008 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 <errno.h>
23 #include <fcntl.h>
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <dirent.h>
30
31 #include "canonicalize.h"
32 #include "dirfd.h"
33
34 /* This replacement assumes that a directory is not renamed while opened
35    through a file descriptor.  */
36
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.  */
39 typedef struct
40 {
41   char *name;       /* Absolute name of the directory, or NULL.  */
42   int saved_errno;  /* If name == NULL: The error code describing the failure
43                        reason.  */
44 } dir_info_t;
45 static dir_info_t *dirs;
46 static size_t dirs_allocated;
47
48 /* Try to ensure dirs has enough room for a slot at index fd.  */
49 static void
50 ensure_dirs_slot (size_t fd)
51 {
52   if (fd >= dirs_allocated)
53     {
54       size_t new_allocated;
55       dir_info_t *new_dirs;
56       size_t i;
57
58       new_allocated = 2 * dirs_allocated + 1;
59       if (new_allocated <= fd)
60         new_allocated = fd + 1;
61       new_dirs =
62         (dirs != NULL
63          ? (dir_info_t *) realloc (dirs, new_allocated * sizeof (dir_info_t))
64          : (dir_info_t *) malloc (new_allocated * sizeof (dir_info_t)));
65       if (new_dirs != NULL)
66         {
67           for (i = dirs_allocated; i < new_allocated; i++)
68             {
69               new_dirs[i].name = NULL;
70               new_dirs[i].saved_errno = ENOTDIR;
71             }
72           dirs = new_dirs;
73           dirs_allocated = new_allocated;
74         }
75     }
76 }
77
78 /* Hook into the gnulib replacements for open() and close() to keep track
79    of the open file descriptors.  */
80
81 void
82 _gl_unregister_fd (int fd)
83 {
84   if (fd >= 0 && fd < dirs_allocated)
85     {
86       if (dirs[fd].name != NULL)
87         free (dirs[fd].name);
88       dirs[fd].name = NULL;
89       dirs[fd].saved_errno = ENOTDIR;
90     }
91 }
92
93 void
94 _gl_register_fd (int fd, const char *filename)
95 {
96   struct stat statbuf;
97
98   ensure_dirs_slot (fd);
99   if (fd < dirs_allocated
100       && fstat (fd, &statbuf) >= 0 && S_ISDIR (statbuf.st_mode))
101     {
102       dirs[fd].name = canonicalize_file_name (filename);
103       if (dirs[fd].name == NULL)
104         dirs[fd].saved_errno = errno;
105     }
106 }
107
108 /* Override opendir() and closedir(), to keep track of the open file
109    descriptors.  Needed because there is a function dirfd().  */
110
111 int
112 rpl_closedir (DIR *dp)
113 #undef closedir
114 {
115   int fd = dirfd (dp);
116   int retval = closedir (dp);
117
118   if (retval >= 0)
119     _gl_unregister_fd (fd);
120   return retval;
121 }
122
123 DIR *
124 rpl_opendir (const char *filename)
125 #undef opendir
126 {
127   DIR *dp;
128
129   dp = opendir (filename);
130   if (dp != NULL)
131     {
132       int fd = dirfd (dp);
133       if (fd >= 0)
134         _gl_register_fd (fd, filename);
135     }
136   return dp;
137 }
138
139 /* Override dup() and dup2(), to keep track of open file descriptors.  */
140
141 int
142 rpl_dup (int oldfd)
143 #undef dup
144 {
145   int newfd = dup (oldfd);
146
147   if (oldfd >= 0 && newfd >= 0)
148     {
149       ensure_dirs_slot (newfd);
150       if (newfd < dirs_allocated)
151         {
152           if (oldfd < dirs_allocated)
153             {
154               if (dirs[oldfd].name != NULL)
155                 {
156                   dirs[newfd].name = strdup (dirs[oldfd].name);
157                   if (dirs[newfd].name == NULL)
158                     dirs[newfd].saved_errno = ENOMEM;
159                 }
160               else
161                 {
162                   dirs[newfd].name = NULL;
163                   dirs[newfd].saved_errno = dirs[oldfd].saved_errno;
164                 }
165             }
166           else
167             {
168               dirs[newfd].name = NULL;
169               dirs[newfd].saved_errno = ENOMEM;
170             }
171         }
172     }
173   return newfd;
174 }
175
176 int
177 rpl_dup2 (int oldfd, int newfd)
178 #undef dup2
179 {
180   int retval = dup2 (oldfd, newfd);
181
182   if (retval >= 0 && oldfd >= 0 && newfd >= 0 && newfd != oldfd)
183     {
184       ensure_dirs_slot (newfd);
185       if (newfd < dirs_allocated)
186         {
187           if (oldfd < dirs_allocated)
188             {
189               if (dirs[oldfd].name != NULL)
190                 {
191                   dirs[newfd].name = strdup (dirs[oldfd].name);
192                   if (dirs[newfd].name == NULL)
193                     dirs[newfd].saved_errno = ENOMEM;
194                 }
195               else
196                 {
197                   dirs[newfd].name = NULL;
198                   dirs[newfd].saved_errno = dirs[oldfd].saved_errno;
199                 }
200             }
201           else
202             {
203               dirs[newfd].name = NULL;
204               dirs[newfd].saved_errno = ENOMEM;
205             }
206         }
207     }
208   return retval;
209 }
210
211 /* Implement fchdir() in terms of chdir().  */
212
213 int
214 fchdir (int fd)
215 {
216   if (fd >= 0)
217     {
218       if (fd < dirs_allocated)
219         {
220           if (dirs[fd].name != NULL)
221             return chdir (dirs[fd].name);
222           else
223             {
224               errno = dirs[fd].saved_errno;
225               return -1;
226             }
227         }
228       else
229         {
230           errno = ENOMEM;
231           return -1;
232         }
233     }
234   else
235     {
236       errno = EBADF;
237       return -1;
238     }
239 }