add ext4,vfat and tar.bz2
[u-tools.git] / u-tools / apps / tar / gnu / chdir-long.c
bloba7b40f092ca65c4f8ef9baa574c1f3349a6599de
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* provide a chdir function that tries not to fail due to ENAMETOOLONG
4 Copyright (C) 2004-2011 Free Software Foundation, Inc.
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.
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.
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/>. */
19 /* written by Jim Meyering */
21 #include <config.h>
23 #include "chdir-long.h"
25 #include <assert.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <stdlib.h>
29 #include <stdbool.h>
30 #include <string.h>
31 #include <stdio.h>
33 #ifndef PATH_MAX
34 # error "compile this file only if your system defines PATH_MAX"
35 #endif
37 /* The results of openat() in this file are not leaked to any
38 single-threaded code that could use stdio.
39 FIXME - if the kernel ever adds support for multi-thread safety for
40 avoiding standard fds, then we should use openat_safer. */
42 struct cd_buf
44 int fd;
47 static inline void
48 cdb_init (struct cd_buf *cdb)
50 cdb->fd = AT_FDCWD;
53 static inline int
54 cdb_fchdir (struct cd_buf const *cdb)
56 return fchdir (cdb->fd);
59 static inline void
60 cdb_free (struct cd_buf const *cdb)
62 if (0 <= cdb->fd)
64 bool close_fail = close (cdb->fd);
65 assert (! close_fail);
69 /* Given a file descriptor of an open directory (or AT_FDCWD), CDB->fd,
70 try to open the CDB->fd-relative directory, DIR. If the open succeeds,
71 update CDB->fd with the resulting descriptor, close the incoming file
72 descriptor, and return zero. Upon failure, return -1 and set errno. */
73 static int
74 cdb_advance_fd (struct cd_buf *cdb, char const *dir)
76 int new_fd = openat (cdb->fd, dir,
77 O_SEARCH | O_DIRECTORY | O_NOCTTY | O_NONBLOCK);
78 if (new_fd < 0)
79 return -1;
81 cdb_free (cdb);
82 cdb->fd = new_fd;
84 return 0;
87 /* Return a pointer to the first non-slash in S. */
88 static inline char *
89 find_non_slash (char const *s)
91 size_t n_slash = strspn (s, "/");
92 return (char *) s + n_slash;
95 /* This is a function much like chdir, but without the PATH_MAX limitation
96 on the length of the directory name. A significant difference is that
97 it must be able to modify (albeit only temporarily) the directory
98 name. It handles an arbitrarily long directory name by operating
99 on manageable portions of the name. On systems without the openat
100 syscall, this means changing the working directory to more and more
101 `distant' points along the long directory name and then restoring
102 the working directory. If any of those attempts to save or restore
103 the working directory fails, this function exits nonzero.
105 Note that this function may still fail with errno == ENAMETOOLONG, but
106 only if the specified directory name contains a component that is long
107 enough to provoke such a failure all by itself (e.g. if the component
108 has length PATH_MAX or greater on systems that define PATH_MAX). */
111 chdir_long (char *dir)
113 int e = chdir (dir);
114 if (e == 0 || errno != ENAMETOOLONG)
115 return e;
118 size_t len = strlen (dir);
119 char *dir_end = dir + len;
120 struct cd_buf cdb;
121 size_t n_leading_slash;
123 cdb_init (&cdb);
125 /* If DIR is the empty string, then the chdir above
126 must have failed and set errno to ENOENT. */
127 assert (0 < len);
128 assert (PATH_MAX <= len);
130 /* Count leading slashes. */
131 n_leading_slash = strspn (dir, "/");
133 /* Handle any leading slashes as well as any name that matches
134 the regular expression, m!^//hostname[/]*! . Handling this
135 prefix separately usually results in a single additional
136 cdb_advance_fd call, but it's worthwhile, since it makes the
137 code in the following loop cleaner. */
138 if (n_leading_slash == 2)
140 int err;
141 /* Find next slash.
142 We already know that dir[2] is neither a slash nor '\0'. */
143 char *slash = memchr (dir + 3, '/', dir_end - (dir + 3));
144 if (slash == NULL)
146 errno = ENAMETOOLONG;
147 return -1;
149 *slash = '\0';
150 err = cdb_advance_fd (&cdb, dir);
151 *slash = '/';
152 if (err != 0)
153 goto Fail;
154 dir = find_non_slash (slash + 1);
156 else if (n_leading_slash)
158 if (cdb_advance_fd (&cdb, "/") != 0)
159 goto Fail;
160 dir += n_leading_slash;
163 assert (*dir != '/');
164 assert (dir <= dir_end);
166 while (PATH_MAX <= dir_end - dir)
168 int err;
169 /* Find a slash that is PATH_MAX or fewer bytes away from dir.
170 I.e. see if there is a slash that will give us a name of
171 length PATH_MAX-1 or less. */
172 char *slash = memrchr (dir, '/', PATH_MAX);
173 if (slash == NULL)
175 errno = ENAMETOOLONG;
176 return -1;
179 *slash = '\0';
180 assert (slash - dir < PATH_MAX);
181 err = cdb_advance_fd (&cdb, dir);
182 *slash = '/';
183 if (err != 0)
184 goto Fail;
186 dir = find_non_slash (slash + 1);
189 if (dir < dir_end)
191 if (cdb_advance_fd (&cdb, dir) != 0)
192 goto Fail;
195 if (cdb_fchdir (&cdb) != 0)
196 goto Fail;
198 cdb_free (&cdb);
199 return 0;
201 Fail:
203 int saved_errno = errno;
204 cdb_free (&cdb);
205 errno = saved_errno;
206 return -1;
211 #if TEST_CHDIR
213 # include "closeout.h"
214 # include "error.h"
216 char *program_name;
219 main (int argc, char *argv[])
221 char *line = NULL;
222 size_t n = 0;
223 int len;
225 program_name = argv[0];
226 atexit (close_stdout);
228 len = getline (&line, &n, stdin);
229 if (len < 0)
231 int saved_errno = errno;
232 if (feof (stdin))
233 exit (0);
235 error (EXIT_FAILURE, saved_errno,
236 "reading standard input");
238 else if (len == 0)
239 exit (0);
241 if (line[len-1] == '\n')
242 line[len-1] = '\0';
244 if (chdir_long (line) != 0)
245 error (EXIT_FAILURE, errno,
246 "chdir_long failed: %s", line);
248 if (argc <= 1)
250 /* Using `pwd' here makes sense only if it is a robust implementation,
251 like the one in coreutils after the 2004-04-19 changes. */
252 char const *cmd = "pwd";
253 execlp (cmd, (char *) NULL);
254 error (EXIT_FAILURE, errno, "%s", cmd);
257 fclose (stdin);
258 fclose (stderr);
260 exit (EXIT_SUCCESS);
262 #endif
265 Local Variables:
266 compile-command: "gcc -DTEST_CHDIR=1 -g -O -W -Wall chdir-long.c libcoreutils.a"
267 End: