Import libarchive-2.5.5.
[dragonfly.git] / contrib / libarchive-1.3.1 / tar / tree.c
blob8803685ae09c5d6e5a0a9837cdfa6d955f1b747e
1 /*-
2 * Copyright (c) 2003-2004 Tim Kientzle
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 /*-
28 * This is a new directory-walking system that addresses a number
29 * of problems I've had with fts(3). In particular, it has no
30 * pathname-length limits (other than the size of 'int'), handles
31 * deep logical traversals, uses considerably less memory, and has
32 * an opaque interface (easier to modify in the future).
34 * Internally, it keeps a single list of "tree_entry" items that
35 * represent filesystem objects that require further attention.
36 * Non-directories are not kept in memory: they are pulled from
37 * readdir(), returned to the client, then freed as soon as possible.
38 * Any directory entry to be traversed gets pushed onto the stack.
40 * There is surprisingly little information that needs to be kept for
41 * each item on the stack. Just the name, depth (represented here as the
42 * string length of the parent directory's pathname), and some markers
43 * indicating how to get back to the parent (via chdir("..") for a
44 * regular dir or via fchdir(2) for a symlink).
46 #include "bsdtar_platform.h"
47 __FBSDID("$FreeBSD: src/usr.bin/tar/tree.c,v 1.4 2006/03/21 17:03:51 kientzle Exp $");
49 #include <sys/stat.h>
50 #include <dirent.h>
51 #include <errno.h>
52 #include <fcntl.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
57 #include "tree.h"
60 * TODO:
61 * 1) Loop checking.
62 * 3) Arbitrary logical traversals by closing/reopening intermediate fds.
65 struct tree_entry {
66 struct tree_entry *next;
67 struct tree_entry *parent;
68 char *name;
69 size_t dirname_length;
70 dev_t dev;
71 ino_t ino;
72 int fd;
73 int flags;
76 /* Definitions for tree_entry.flags bitmap. */
77 #define isDir 1 /* This entry is a regular directory. */
78 #define isDirLink 2 /* This entry is a symbolic link to a directory. */
79 #define needsPreVisit 4 /* This entry needs to be previsited. */
80 #define needsPostVisit 8 /* This entry needs to be postvisited. */
83 * Local data for this package.
85 struct tree {
86 struct tree_entry *stack;
87 struct tree_entry *current;
88 DIR *d;
89 int initialDirFd;
90 int flags;
91 int visit_type;
92 int tree_errno; /* Error code from last failed operation. */
94 char *buff;
95 const char *basename;
96 size_t buff_length;
97 size_t path_length;
98 size_t dirname_length;
100 int depth;
101 int openCount;
102 int maxOpenCount;
104 struct stat lst;
105 struct stat st;
108 /* Definitions for tree.flags bitmap. */
109 #define needsReturn 8 /* Marks first entry as not having been returned yet. */
110 #define hasStat 16 /* The st entry is set. */
111 #define hasLstat 32 /* The lst entry is set. */
114 #ifdef HAVE_DIRENT_D_NAMLEN
115 /* BSD extension; avoids need for a strlen() call. */
116 #define D_NAMELEN(dp) (dp)->d_namlen
117 #else
118 #define D_NAMELEN(dp) (strlen((dp)->d_name))
119 #endif
121 #if 0
122 #include <stdio.h>
123 void
124 tree_dump(struct tree *t, FILE *out)
126 struct tree_entry *te;
128 fprintf(out, "\tdepth: %d\n", t->depth);
129 fprintf(out, "\tbuff: %s\n", t->buff);
130 fprintf(out, "\tpwd: "); fflush(stdout); system("pwd");
131 fprintf(out, "\taccess: %s\n", t->basename);
132 fprintf(out, "\tstack:\n");
133 for (te = t->stack; te != NULL; te = te->next) {
134 fprintf(out, "\t\tte->name: %s%s%s\n", te->name,
135 te->flags & needsPreVisit ? "" : " *",
136 t->current == te ? " (current)" : "");
139 #endif
142 * Add a directory path to the current stack.
144 static void
145 tree_push(struct tree *t, const char *path)
147 struct tree_entry *te;
149 te = malloc(sizeof(*te));
150 memset(te, 0, sizeof(*te));
151 te->next = t->stack;
152 t->stack = te;
153 te->fd = -1;
154 te->name = strdup(path);
155 te->flags = needsPreVisit | needsPostVisit;
156 te->dirname_length = t->dirname_length;
160 * Append a name to the current path.
162 static void
163 tree_append(struct tree *t, const char *name, size_t name_length)
165 char *p;
167 if (t->buff != NULL)
168 t->buff[t->dirname_length] = '\0';
169 /* Strip trailing '/' from name, unless entire name is "/". */
170 while (name_length > 1 && name[name_length - 1] == '/')
171 name_length--;
173 /* Resize pathname buffer as needed. */
174 while (name_length + 1 + t->dirname_length >= t->buff_length) {
175 t->buff_length *= 2;
176 if (t->buff_length < 1024)
177 t->buff_length = 1024;
178 t->buff = realloc(t->buff, t->buff_length);
180 p = t->buff + t->dirname_length;
181 t->path_length = t->dirname_length + name_length;
182 /* Add a separating '/' if it's needed. */
183 if (t->dirname_length > 0 && p[-1] != '/') {
184 *p++ = '/';
185 t->path_length ++;
187 strncpy(p, name, name_length);
188 p[name_length] = '\0';
189 t->basename = p;
193 * Open a directory tree for traversal.
195 struct tree *
196 tree_open(const char *path)
198 struct tree *t;
200 t = malloc(sizeof(*t));
201 memset(t, 0, sizeof(*t));
202 tree_append(t, path, strlen(path));
203 t->initialDirFd = open(".", O_RDONLY);
205 * During most of the traversal, items are set up and then
206 * returned immediately from tree_next(). That doesn't work
207 * for the very first entry, so we set a flag for this special
208 * case.
210 t->flags = needsReturn;
211 return (t);
215 * We've finished a directory; ascend back to the parent.
217 static void
218 tree_ascend(struct tree *t)
220 struct tree_entry *te;
222 te = t->stack;
223 t->depth--;
224 if (te->flags & isDirLink) {
225 fchdir(te->fd);
226 close(te->fd);
227 t->openCount--;
228 } else {
229 chdir("..");
234 * Pop the working stack.
236 static void
237 tree_pop(struct tree *t)
239 struct tree_entry *te;
241 t->buff[t->dirname_length] = '\0';
242 if (t->stack == t->current && t->current != NULL)
243 t->current = t->current->parent;
244 te = t->stack;
245 t->stack = te->next;
246 t->dirname_length = te->dirname_length;
247 t->basename = t->buff + t->dirname_length;
248 /* Special case: starting dir doesn't skip leading '/'. */
249 if (t->dirname_length > 0)
250 t->basename++;
251 free(te->name);
252 free(te);
256 * Get the next item in the tree traversal.
259 tree_next(struct tree *t)
261 struct dirent *de = NULL;
263 /* Handle the startup case by returning the initial entry. */
264 if (t->flags & needsReturn) {
265 t->flags &= ~needsReturn;
266 return (t->visit_type = TREE_REGULAR);
269 while (t->stack != NULL) {
270 /* If there's an open dir, get the next entry from there. */
271 while (t->d != NULL) {
272 de = readdir(t->d);
273 if (de == NULL) {
274 closedir(t->d);
275 t->d = NULL;
276 } else if (de->d_name[0] == '.'
277 && de->d_name[1] == '\0') {
278 /* Skip '.' */
279 } else if (de->d_name[0] == '.'
280 && de->d_name[1] == '.'
281 && de->d_name[2] == '\0') {
282 /* Skip '..' */
283 } else {
285 * Append the path to the current path
286 * and return it.
288 tree_append(t, de->d_name, D_NAMELEN(de));
289 t->flags &= ~hasLstat;
290 t->flags &= ~hasStat;
291 return (t->visit_type = TREE_REGULAR);
295 /* If the current dir needs to be visited, set it up. */
296 if (t->stack->flags & needsPreVisit) {
297 t->current = t->stack;
298 tree_append(t, t->stack->name, strlen(t->stack->name));
299 t->stack->flags &= ~needsPreVisit;
300 /* If it is a link, set up fd for the ascent. */
301 if (t->stack->flags & isDirLink) {
302 t->stack->fd = open(".", O_RDONLY);
303 t->openCount++;
304 if (t->openCount > t->maxOpenCount)
305 t->maxOpenCount = t->openCount;
307 t->dirname_length = t->path_length;
308 if (chdir(t->stack->name) != 0) {
309 /* chdir() failed; return error */
310 tree_pop(t);
311 t->tree_errno = errno;
312 return (t->visit_type = TREE_ERROR_DIR);
314 t->depth++;
315 t->d = opendir(".");
316 if (t->d == NULL) {
317 tree_ascend(t); /* Undo "chdir" */
318 tree_pop(t);
319 t->tree_errno = errno;
320 return (t->visit_type = TREE_ERROR_DIR);
322 t->flags &= ~hasLstat;
323 t->flags &= ~hasStat;
324 t->basename = ".";
325 return (t->visit_type = TREE_POSTDESCENT);
328 /* We've done everything necessary for the top stack entry. */
329 if (t->stack->flags & needsPostVisit) {
330 tree_ascend(t);
331 tree_pop(t);
332 t->flags &= ~hasLstat;
333 t->flags &= ~hasStat;
334 return (t->visit_type = TREE_POSTASCENT);
337 return (t->visit_type = 0);
341 * Return error code.
344 tree_errno(struct tree *t)
346 return (t->tree_errno);
350 * Called by the client to mark the directory just returned from
351 * tree_next() as needing to be visited.
353 void
354 tree_descend(struct tree *t)
356 if (t->visit_type != TREE_REGULAR)
357 return;
359 if (tree_current_is_physical_dir(t)) {
360 tree_push(t, t->basename);
361 t->stack->flags |= isDir;
362 } else if (tree_current_is_dir(t)) {
363 tree_push(t, t->basename);
364 t->stack->flags |= isDirLink;
369 * Get the stat() data for the entry just returned from tree_next().
371 const struct stat *
372 tree_current_stat(struct tree *t)
374 if (!(t->flags & hasStat)) {
375 if (stat(t->basename, &t->st) != 0)
376 return NULL;
377 t->flags |= hasStat;
379 return (&t->st);
383 * Get the lstat() data for the entry just returned from tree_next().
385 const struct stat *
386 tree_current_lstat(struct tree *t)
388 if (!(t->flags & hasLstat)) {
389 if (lstat(t->basename, &t->lst) != 0)
390 return NULL;
391 t->flags |= hasLstat;
393 return (&t->lst);
397 * Test whether current entry is a dir or dir link.
400 tree_current_is_dir(struct tree *t)
402 /* If we've already pulled stat(), just use that. */
403 if (t->flags & hasStat)
404 return (S_ISDIR(tree_current_stat(t)->st_mode));
406 /* If we've already pulled lstat(), we may be able to use that. */
407 if (t->flags & hasLstat) {
408 /* If lstat() says it's a dir, it must be a dir. */
409 if (S_ISDIR(tree_current_lstat(t)->st_mode))
410 return 1;
411 /* If it's not a dir and not a link, we're done. */
412 if (!S_ISLNK(tree_current_lstat(t)->st_mode))
413 return 0;
415 * If the above two tests fail, then it's a link, but
416 * we don't know whether it's a link to a dir or a
417 * non-dir.
421 /* TODO: Use a more efficient mechanism when available. */
422 return (S_ISDIR(tree_current_stat(t)->st_mode));
426 * Test whether current entry is a physical directory.
429 tree_current_is_physical_dir(struct tree *t)
431 /* If we've already pulled lstat(), just use that. */
432 if (t->flags & hasLstat)
433 return (S_ISDIR(tree_current_lstat(t)->st_mode));
435 /* TODO: Use a more efficient mechanism when available. */
436 return (S_ISDIR(tree_current_lstat(t)->st_mode));
440 * Test whether current entry is a symbolic link.
443 tree_current_is_physical_link(struct tree *t)
445 /* If we've already pulled lstat(), just use that. */
446 if (t->flags & hasLstat)
447 return (S_ISLNK(tree_current_lstat(t)->st_mode));
449 /* TODO: Use a more efficient mechanism when available. */
450 return (S_ISLNK(tree_current_lstat(t)->st_mode));
454 * Return the access path for the entry just returned from tree_next().
456 const char *
457 tree_current_access_path(struct tree *t)
459 return (t->basename);
463 * Return the full path for the entry just returned from tree_next().
465 const char *
466 tree_current_path(struct tree *t)
468 return (t->buff);
472 * Return the length of the path for the entry just returned from tree_next().
474 size_t
475 tree_current_pathlen(struct tree *t)
477 return (t->path_length);
481 * Return the nesting depth of the entry just returned from tree_next().
484 tree_current_depth(struct tree *t)
486 return (t->depth);
490 * Terminate the traversal and release any resources.
492 void
493 tree_close(struct tree *t)
495 /* Release anything remaining in the stack. */
496 while (t->stack != NULL)
497 tree_pop(t);
498 if (t->buff)
499 free(t->buff);
500 /* chdir() back to where we started. */
501 if (t->initialDirFd >= 0) {
502 fchdir(t->initialDirFd);
503 close(t->initialDirFd);
504 t->initialDirFd = -1;
506 free(t);