]> git.cworth.org Git - tar/blob - lib/fchdir.c
Imported Upstream version 1.20
[tar] / lib / fchdir.c
1 /* fchdir replacement.
2    Copyright (C) 2006, 2007 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 /* Override open() and close(), to keep track of the open file descriptors.  */
79
80 int
81 rpl_close (int fd)
82 #undef close
83 {
84   int retval = close (fd);
85
86   if (retval >= 0 && fd >= 0 && fd < dirs_allocated)
87     {
88       if (dirs[fd].name != NULL)
89         free (dirs[fd].name);
90       dirs[fd].name = NULL;
91       dirs[fd].saved_errno = ENOTDIR;
92     }
93   return retval;
94 }
95
96 int
97 rpl_open (const char *filename, int flags, ...)
98 #undef open
99 {
100   mode_t mode;
101   int fd;
102   struct stat statbuf;
103
104   mode = 0;
105   if (flags & O_CREAT)
106     {
107       va_list arg;
108       va_start (arg, flags);
109
110       /* If mode_t is narrower than int, use the promoted type (int),
111          not mode_t.  Use sizeof to guess whether mode_t is narrower;
112          we don't know of any practical counterexamples.  */
113       mode = (sizeof (mode_t) < sizeof (int)
114               ? va_arg (arg, int)
115               : va_arg (arg, mode_t));
116
117       va_end (arg);
118     }
119 #if defined GNULIB_OPEN && ((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__)
120   if (strcmp (filename, "/dev/null") == 0)
121     filename = "NUL";
122 #endif
123   fd = open (filename, flags, mode);
124   if (fd >= 0)
125     {
126       ensure_dirs_slot (fd);
127       if (fd < dirs_allocated
128           && fstat (fd, &statbuf) >= 0 && S_ISDIR (statbuf.st_mode))
129         {
130           dirs[fd].name = canonicalize_file_name (filename);
131           if (dirs[fd].name == NULL)
132             dirs[fd].saved_errno = errno;
133         }
134     }
135   return fd;
136 }
137
138 /* Override opendir() and closedir(), to keep track of the open file
139    descriptors.  Needed because there is a function dirfd().  */
140
141 int
142 rpl_closedir (DIR *dp)
143 #undef closedir
144 {
145   int fd = dirfd (dp);
146   int retval = closedir (dp);
147
148   if (retval >= 0 && fd >= 0 && fd < dirs_allocated)
149     {
150       if (dirs[fd].name != NULL)
151         free (dirs[fd].name);
152       dirs[fd].name = NULL;
153       dirs[fd].saved_errno = ENOTDIR;
154     }
155   return retval;
156 }
157
158 DIR *
159 rpl_opendir (const char *filename)
160 #undef opendir
161 {
162   DIR *dp;
163
164   dp = opendir (filename);
165   if (dp != NULL)
166     {
167       int fd = dirfd (dp);
168       if (fd >= 0)
169         {
170           ensure_dirs_slot (fd);
171           if (fd < dirs_allocated)
172             {
173               dirs[fd].name = canonicalize_file_name (filename);
174               if (dirs[fd].name == NULL)
175                 dirs[fd].saved_errno = errno;
176             }
177         }
178     }
179   return dp;
180 }
181
182 /* Override dup() and dup2(), to keep track of open file descriptors.  */
183
184 int
185 rpl_dup (int oldfd)
186 #undef dup
187 {
188   int newfd = dup (oldfd);
189
190   if (oldfd >= 0 && newfd >= 0)
191     {
192       ensure_dirs_slot (newfd);
193       if (newfd < dirs_allocated)
194         {
195           if (oldfd < dirs_allocated)
196             {
197               if (dirs[oldfd].name != NULL)
198                 {
199                   dirs[newfd].name = strdup (dirs[oldfd].name);
200                   if (dirs[newfd].name == NULL)
201                     dirs[newfd].saved_errno = ENOMEM;
202                 }
203               else
204                 {
205                   dirs[newfd].name = NULL;
206                   dirs[newfd].saved_errno = dirs[oldfd].saved_errno;
207                 }
208             }
209           else
210             {
211               dirs[newfd].name = NULL;
212               dirs[newfd].saved_errno = ENOMEM;
213             }
214         }
215     }
216   return newfd;
217 }
218
219 int
220 rpl_dup2 (int oldfd, int newfd)
221 #undef dup2
222 {
223   int retval = dup2 (oldfd, newfd);
224
225   if (retval >= 0 && oldfd >= 0 && newfd >= 0 && newfd != oldfd)
226     {
227       ensure_dirs_slot (newfd);
228       if (newfd < dirs_allocated)
229         {
230           if (oldfd < dirs_allocated)
231             {
232               if (dirs[oldfd].name != NULL)
233                 {
234                   dirs[newfd].name = strdup (dirs[oldfd].name);
235                   if (dirs[newfd].name == NULL)
236                     dirs[newfd].saved_errno = ENOMEM;
237                 }
238               else
239                 {
240                   dirs[newfd].name = NULL;
241                   dirs[newfd].saved_errno = dirs[oldfd].saved_errno;
242                 }
243             }
244           else
245             {
246               dirs[newfd].name = NULL;
247               dirs[newfd].saved_errno = ENOMEM;
248             }
249         }
250     }
251   return retval;
252 }
253
254 /* Implement fchdir() in terms of chdir().  */
255
256 int
257 fchdir (int fd)
258 {
259   if (fd >= 0)
260     {
261       if (fd < dirs_allocated)
262         {
263           if (dirs[fd].name != NULL)
264             return chdir (dirs[fd].name);
265           else
266             {
267               errno = dirs[fd].saved_errno;
268               return -1;
269             }
270         }
271       else
272         {
273           errno = ENOMEM;
274           return -1;
275         }
276     }
277   else
278     {
279       errno = EBADF;
280       return -1;
281     }
282 }