a39f1177d3bd7065aa9136e28fef4109b0efa358
[debian/tar] / gnu / fchdir.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* fchdir replacement.
4    Copyright (C) 2006-2014 Free Software Foundation, Inc.
5
6    This program is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 #include <config.h>
20
21 /* Specification.  */
22 #include <unistd.h>
23
24 #include <assert.h>
25 #include <dirent.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <stdbool.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33
34 #include "dosname.h"
35 #include "filenamecat.h"
36
37 #ifndef REPLACE_OPEN_DIRECTORY
38 # define REPLACE_OPEN_DIRECTORY 0
39 #endif
40
41 /* This replacement assumes that a directory is not renamed while opened
42    through a file descriptor.
43
44    FIXME: On mingw, this would be possible to enforce if we were to
45    also open a HANDLE to each directory currently visited by a file
46    descriptor, since mingw refuses to rename any in-use file system
47    object.  */
48
49 /* Array of file descriptors opened.  If REPLACE_OPEN_DIRECTORY or if it points
50    to a directory, it stores info about this directory.  */
51 typedef struct
52 {
53   char *name;       /* Absolute name of the directory, or NULL.  */
54   /* FIXME - add a DIR* member to make dirfd possible on mingw?  */
55 } dir_info_t;
56 static dir_info_t *dirs;
57 static size_t dirs_allocated;
58
59 /* Try to ensure dirs has enough room for a slot at index fd; free any
60    contents already in that slot.  Return false and set errno to
61    ENOMEM on allocation failure.  */
62 static bool
63 ensure_dirs_slot (size_t fd)
64 {
65   if (fd < dirs_allocated)
66     free (dirs[fd].name);
67   else
68     {
69       size_t new_allocated;
70       dir_info_t *new_dirs;
71
72       new_allocated = 2 * dirs_allocated + 1;
73       if (new_allocated <= fd)
74         new_allocated = fd + 1;
75       new_dirs =
76         (dirs != NULL
77          ? (dir_info_t *) realloc (dirs, new_allocated * sizeof *dirs)
78          : (dir_info_t *) malloc (new_allocated * sizeof *dirs));
79       if (new_dirs == NULL)
80         return false;
81       memset (new_dirs + dirs_allocated, 0,
82               (new_allocated - dirs_allocated) * sizeof *dirs);
83       dirs = new_dirs;
84       dirs_allocated = new_allocated;
85     }
86   return true;
87 }
88
89 /* Return an absolute name of DIR in malloc'd storage.  */
90 static char *
91 get_name (char const *dir)
92 {
93   char *cwd;
94   char *result;
95   int saved_errno;
96
97   if (IS_ABSOLUTE_FILE_NAME (dir))
98     return strdup (dir);
99
100   /* We often encounter "."; treat it as a special case.  */
101   cwd = getcwd (NULL, 0);
102   if (!cwd || (dir[0] == '.' && dir[1] == '\0'))
103     return cwd;
104
105   result = mfile_name_concat (cwd, dir, NULL);
106   saved_errno = errno;
107   free (cwd);
108   errno = saved_errno;
109   return result;
110 }
111
112 /* Hook into the gnulib replacements for open() and close() to keep track
113    of the open file descriptors.  */
114
115 /* Close FD, cleaning up any fd to name mapping if fd was visiting a
116    directory.  */
117 void
118 _gl_unregister_fd (int fd)
119 {
120   if (fd >= 0 && fd < dirs_allocated)
121     {
122       free (dirs[fd].name);
123       dirs[fd].name = NULL;
124     }
125 }
126
127 /* Mark FD as visiting FILENAME.  FD must be non-negative, and refer
128    to an open file descriptor.  If REPLACE_OPEN_DIRECTORY is non-zero,
129    this should only be called if FD is visiting a directory.  Close FD
130    and return -1 if there is insufficient memory to track the
131    directory name; otherwise return FD.  */
132 int
133 _gl_register_fd (int fd, const char *filename)
134 {
135   struct stat statbuf;
136
137   assert (0 <= fd);
138   if (REPLACE_OPEN_DIRECTORY
139       || (fstat (fd, &statbuf) == 0 && S_ISDIR (statbuf.st_mode)))
140     {
141       if (!ensure_dirs_slot (fd)
142           || (dirs[fd].name = get_name (filename)) == NULL)
143         {
144           int saved_errno = errno;
145           close (fd);
146           errno = saved_errno;
147           return -1;
148         }
149     }
150   return fd;
151 }
152
153 /* Mark NEWFD as a duplicate of OLDFD; useful from dup, dup2, dup3,
154    and fcntl.  Both arguments must be valid and distinct file
155    descriptors.  Close NEWFD and return -1 if OLDFD is tracking a
156    directory, but there is insufficient memory to track the same
157    directory in NEWFD; otherwise return NEWFD.  */
158 int
159 _gl_register_dup (int oldfd, int newfd)
160 {
161   assert (0 <= oldfd && 0 <= newfd && oldfd != newfd);
162   if (oldfd < dirs_allocated && dirs[oldfd].name)
163     {
164       /* Duplicated a directory; must ensure newfd is allocated.  */
165       if (!ensure_dirs_slot (newfd)
166           || (dirs[newfd].name = strdup (dirs[oldfd].name)) == NULL)
167         {
168           int saved_errno = errno;
169           close (newfd);
170           errno = saved_errno;
171           newfd = -1;
172         }
173     }
174   else if (newfd < dirs_allocated)
175     {
176       /* Duplicated a non-directory; ensure newfd is cleared.  */
177       free (dirs[newfd].name);
178       dirs[newfd].name = NULL;
179     }
180   return newfd;
181 }
182
183 /* If FD is currently visiting a directory, then return the name of
184    that directory.  Otherwise, return NULL and set errno.  */
185 const char *
186 _gl_directory_name (int fd)
187 {
188   if (0 <= fd && fd < dirs_allocated && dirs[fd].name != NULL)
189     return dirs[fd].name;
190   /* At this point, fd is either invalid, or open but not a directory.
191      If dup2 fails, errno is correctly EBADF.  */
192   if (0 <= fd)
193     {
194       if (dup2 (fd, fd) == fd)
195         errno = ENOTDIR;
196     }
197   else
198     errno = EBADF;
199   return NULL;
200 }
201
202
203 /* Implement fchdir() in terms of chdir().  */
204
205 int
206 fchdir (int fd)
207 {
208   const char *name = _gl_directory_name (fd);
209   return name ? chdir (name) : -1;
210 }