Blame contrib/shar/tree.c

Packit 08bd4c
/*-
Packit 08bd4c
 * Copyright (c) 2003-2007 Tim Kientzle
Packit 08bd4c
 * All rights reserved.
Packit 08bd4c
 *
Packit 08bd4c
 * Redistribution and use in source and binary forms, with or without
Packit 08bd4c
 * modification, are permitted provided that the following conditions
Packit 08bd4c
 * are met:
Packit 08bd4c
 * 1. Redistributions of source code must retain the above copyright
Packit 08bd4c
 *    notice, this list of conditions and the following disclaimer.
Packit 08bd4c
 * 2. Redistributions in binary form must reproduce the above copyright
Packit 08bd4c
 *    notice, this list of conditions and the following disclaimer in the
Packit 08bd4c
 *    documentation and/or other materials provided with the distribution.
Packit 08bd4c
 *
Packit 08bd4c
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
Packit 08bd4c
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Packit 08bd4c
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Packit 08bd4c
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit 08bd4c
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
Packit 08bd4c
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit 08bd4c
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit 08bd4c
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit 08bd4c
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
Packit 08bd4c
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 08bd4c
 */
Packit 08bd4c
Packit 08bd4c
/*-
Packit 08bd4c
 * This is a new directory-walking system that addresses a number
Packit 08bd4c
 * of problems I've had with fts(3).  In particular, it has no
Packit 08bd4c
 * pathname-length limits (other than the size of 'int'), handles
Packit 08bd4c
 * deep logical traversals, uses considerably less memory, and has
Packit 08bd4c
 * an opaque interface (easier to modify in the future).
Packit 08bd4c
 *
Packit 08bd4c
 * Internally, it keeps a single list of "tree_entry" items that
Packit 08bd4c
 * represent filesystem objects that require further attention.
Packit 08bd4c
 * Non-directories are not kept in memory: they are pulled from
Packit 08bd4c
 * readdir(), returned to the client, then freed as soon as possible.
Packit 08bd4c
 * Any directory entry to be traversed gets pushed onto the stack.
Packit 08bd4c
 *
Packit 08bd4c
 * There is surprisingly little information that needs to be kept for
Packit 08bd4c
 * each item on the stack.  Just the name, depth (represented here as the
Packit 08bd4c
 * string length of the parent directory's pathname), and some markers
Packit 08bd4c
 * indicating how to get back to the parent (via chdir("..") for a
Packit 08bd4c
 * regular dir or via fchdir(2) for a symlink).
Packit 08bd4c
 */
Packit 08bd4c
#include "tree_config.h"
Packit 08bd4c
__FBSDID("$FreeBSD$");
Packit 08bd4c
Packit 08bd4c
#ifdef HAVE_SYS_STAT_H
Packit 08bd4c
#include <sys/stat.h>
Packit 08bd4c
#endif
Packit 08bd4c
#ifdef HAVE_DIRENT_H
Packit 08bd4c
#include <dirent.h>
Packit 08bd4c
#endif
Packit 08bd4c
#ifdef HAVE_ERRNO_H
Packit 08bd4c
#include <errno.h>
Packit 08bd4c
#endif
Packit 08bd4c
#ifdef HAVE_FCNTL_H
Packit 08bd4c
#include <fcntl.h>
Packit 08bd4c
#endif
Packit 08bd4c
#ifdef HAVE_STDLIB_H
Packit 08bd4c
#include <stdlib.h>
Packit 08bd4c
#endif
Packit 08bd4c
#ifdef HAVE_STRING_H
Packit 08bd4c
#include <string.h>
Packit 08bd4c
#endif
Packit 08bd4c
#ifdef HAVE_UNISTD_H
Packit 08bd4c
#include <unistd.h>
Packit 08bd4c
#endif
Packit 08bd4c
Packit 08bd4c
#include "tree.h"
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * TODO:
Packit 08bd4c
 *    1) Loop checking.
Packit 08bd4c
 *    3) Arbitrary logical traversals by closing/reopening intermediate fds.
Packit 08bd4c
 */
Packit 08bd4c
Packit 08bd4c
struct tree_entry {
Packit 08bd4c
	struct tree_entry *next;
Packit 08bd4c
	struct tree_entry *parent;
Packit 08bd4c
	char *name;
Packit 08bd4c
	size_t dirname_length;
Packit 08bd4c
	dev_t dev;
Packit 08bd4c
	ino_t ino;
Packit 08bd4c
	int fd;
Packit 08bd4c
	int flags;
Packit 08bd4c
};
Packit 08bd4c
Packit 08bd4c
/* Definitions for tree_entry.flags bitmap. */
Packit 08bd4c
#define	isDir 1 /* This entry is a regular directory. */
Packit 08bd4c
#define	isDirLink 2 /* This entry is a symbolic link to a directory. */
Packit 08bd4c
#define	needsPreVisit 4 /* This entry needs to be previsited. */
Packit 08bd4c
#define	needsPostVisit 8 /* This entry needs to be postvisited. */
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * Local data for this package.
Packit 08bd4c
 */
Packit 08bd4c
struct tree {
Packit 08bd4c
	struct tree_entry	*stack;
Packit 08bd4c
	struct tree_entry	*current;
Packit 08bd4c
	DIR	*d;
Packit 08bd4c
	int	 initialDirFd;
Packit 08bd4c
	int	 flags;
Packit 08bd4c
	int	 visit_type;
Packit 08bd4c
	int	 tree_errno; /* Error code from last failed operation. */
Packit 08bd4c
Packit 08bd4c
	char	*buff;
Packit 08bd4c
	const char	*basename;
Packit 08bd4c
	size_t	 buff_length;
Packit 08bd4c
	size_t	 path_length;
Packit 08bd4c
	size_t	 dirname_length;
Packit 08bd4c
Packit 08bd4c
	int	 depth;
Packit 08bd4c
	int	 openCount;
Packit 08bd4c
	int	 maxOpenCount;
Packit 08bd4c
Packit 08bd4c
	struct stat	lst;
Packit 08bd4c
	struct stat	st;
Packit 08bd4c
};
Packit 08bd4c
Packit 08bd4c
/* Definitions for tree.flags bitmap. */
Packit 08bd4c
#define needsReturn 8  /* Marks first entry as not having been returned yet. */
Packit 08bd4c
#define hasStat 16  /* The st entry is set. */
Packit 08bd4c
#define hasLstat 32 /* The lst entry is set. */
Packit 08bd4c
Packit 08bd4c
Packit 08bd4c
#ifdef HAVE_DIRENT_D_NAMLEN
Packit 08bd4c
/* BSD extension; avoids need for a strlen() call. */
Packit 08bd4c
#define D_NAMELEN(dp)	(dp)->d_namlen
Packit 08bd4c
#else
Packit 08bd4c
#define D_NAMELEN(dp)	(strlen((dp)->d_name))
Packit 08bd4c
#endif
Packit 08bd4c
Packit 08bd4c
#if 0
Packit 08bd4c
#include <stdio.h>
Packit 08bd4c
void
Packit 08bd4c
tree_dump(struct tree *t, FILE *out)
Packit 08bd4c
{
Packit 08bd4c
	struct tree_entry *te;
Packit 08bd4c
Packit 08bd4c
	fprintf(out, "\tdepth: %d\n", t->depth);
Packit 08bd4c
	fprintf(out, "\tbuff: %s\n", t->buff);
Packit 08bd4c
	fprintf(out, "\tpwd: "); fflush(stdout); system("pwd");
Packit 08bd4c
	fprintf(out, "\taccess: %s\n", t->basename);
Packit 08bd4c
	fprintf(out, "\tstack:\n");
Packit 08bd4c
	for (te = t->stack; te != NULL; te = te->next) {
Packit 08bd4c
		fprintf(out, "\t\tte->name: %s%s%s\n", te->name,
Packit 08bd4c
		    te->flags & needsPreVisit ? "" : " *",
Packit 08bd4c
		    t->current == te ? " (current)" : "");
Packit 08bd4c
	}
Packit 08bd4c
}
Packit 08bd4c
#endif
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * Add a directory path to the current stack.
Packit 08bd4c
 */
Packit 08bd4c
static void
Packit 08bd4c
tree_push(struct tree *t, const char *path)
Packit 08bd4c
{
Packit 08bd4c
	struct tree_entry *te;
Packit 08bd4c
Packit 08bd4c
	te = malloc(sizeof(*te));
Packit 08bd4c
	memset(te, 0, sizeof(*te));
Packit 08bd4c
	te->next = t->stack;
Packit 08bd4c
	t->stack = te;
Packit 08bd4c
	te->fd = -1;
Packit 08bd4c
	te->name = strdup(path);
Packit 08bd4c
	te->flags = needsPreVisit | needsPostVisit;
Packit 08bd4c
	te->dirname_length = t->dirname_length;
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * Append a name to the current path.
Packit 08bd4c
 */
Packit 08bd4c
static void
Packit 08bd4c
tree_append(struct tree *t, const char *name, size_t name_length)
Packit 08bd4c
{
Packit 08bd4c
	char *p;
Packit 08bd4c
Packit 08bd4c
	if (t->buff != NULL)
Packit 08bd4c
		t->buff[t->dirname_length] = '\0';
Packit 08bd4c
	/* Strip trailing '/' from name, unless entire name is "/". */
Packit 08bd4c
	while (name_length > 1 && name[name_length - 1] == '/')
Packit 08bd4c
		name_length--;
Packit 08bd4c
Packit 08bd4c
	/* Resize pathname buffer as needed. */
Packit 08bd4c
	while (name_length + 1 + t->dirname_length >= t->buff_length) {
Packit 08bd4c
		t->buff_length *= 2;
Packit 08bd4c
		if (t->buff_length < 1024)
Packit 08bd4c
			t->buff_length = 1024;
Packit 08bd4c
		t->buff = realloc(t->buff, t->buff_length);
Packit 08bd4c
	}
Packit 08bd4c
	p = t->buff + t->dirname_length;
Packit 08bd4c
	t->path_length = t->dirname_length + name_length;
Packit 08bd4c
	/* Add a separating '/' if it's needed. */
Packit 08bd4c
	if (t->dirname_length > 0 && p[-1] != '/') {
Packit 08bd4c
		*p++ = '/';
Packit 08bd4c
		t->path_length ++;
Packit 08bd4c
	}
Packit 08bd4c
	strncpy(p, name, name_length);
Packit 08bd4c
	p[name_length] = '\0';
Packit 08bd4c
	t->basename = p;
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * Open a directory tree for traversal.
Packit 08bd4c
 */
Packit 08bd4c
struct tree *
Packit 08bd4c
tree_open(const char *path)
Packit 08bd4c
{
Packit 08bd4c
	struct tree *t;
Packit 08bd4c
Packit 08bd4c
	t = malloc(sizeof(*t));
Packit 08bd4c
	memset(t, 0, sizeof(*t));
Packit 08bd4c
	tree_append(t, path, strlen(path));
Packit 08bd4c
	t->initialDirFd = open(".", O_RDONLY);
Packit 08bd4c
	/*
Packit 08bd4c
	 * During most of the traversal, items are set up and then
Packit 08bd4c
	 * returned immediately from tree_next().  That doesn't work
Packit 08bd4c
	 * for the very first entry, so we set a flag for this special
Packit 08bd4c
	 * case.
Packit 08bd4c
	 */
Packit 08bd4c
	t->flags = needsReturn;
Packit 08bd4c
	return (t);
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * We've finished a directory; ascend back to the parent.
Packit 08bd4c
 */
Packit 08bd4c
static void
Packit 08bd4c
tree_ascend(struct tree *t)
Packit 08bd4c
{
Packit 08bd4c
	struct tree_entry *te;
Packit 08bd4c
Packit 08bd4c
	te = t->stack;
Packit 08bd4c
	t->depth--;
Packit 08bd4c
	if (te->flags & isDirLink) {
Packit 08bd4c
		fchdir(te->fd);
Packit 08bd4c
		close(te->fd);
Packit 08bd4c
		t->openCount--;
Packit 08bd4c
	} else {
Packit 08bd4c
		chdir("..");
Packit 08bd4c
	}
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * Pop the working stack.
Packit 08bd4c
 */
Packit 08bd4c
static void
Packit 08bd4c
tree_pop(struct tree *t)
Packit 08bd4c
{
Packit 08bd4c
	struct tree_entry *te;
Packit 08bd4c
Packit 08bd4c
	t->buff[t->dirname_length] = '\0';
Packit 08bd4c
	if (t->stack == t->current && t->current != NULL)
Packit 08bd4c
		t->current = t->current->parent;
Packit 08bd4c
	te = t->stack;
Packit 08bd4c
	t->stack = te->next;
Packit 08bd4c
	t->dirname_length = te->dirname_length;
Packit 08bd4c
	t->basename = t->buff + t->dirname_length;
Packit 08bd4c
	/* Special case: starting dir doesn't skip leading '/'. */
Packit 08bd4c
	if (t->dirname_length > 0)
Packit 08bd4c
		t->basename++;
Packit 08bd4c
	free(te->name);
Packit 08bd4c
	free(te);
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * Get the next item in the tree traversal.
Packit 08bd4c
 */
Packit 08bd4c
int
Packit 08bd4c
tree_next(struct tree *t)
Packit 08bd4c
{
Packit 08bd4c
	struct dirent *de = NULL;
Packit 08bd4c
Packit 08bd4c
	/* Handle the startup case by returning the initial entry. */
Packit 08bd4c
	if (t->flags & needsReturn) {
Packit 08bd4c
		t->flags &= ~needsReturn;
Packit 08bd4c
		return (t->visit_type = TREE_REGULAR);
Packit 08bd4c
	}
Packit 08bd4c
Packit 08bd4c
	while (t->stack != NULL) {
Packit 08bd4c
		/* If there's an open dir, get the next entry from there. */
Packit 08bd4c
		while (t->d != NULL) {
Packit 08bd4c
			de = readdir(t->d);
Packit 08bd4c
			if (de == NULL) {
Packit 08bd4c
				closedir(t->d);
Packit 08bd4c
				t->d = NULL;
Packit 08bd4c
			} else if (de->d_name[0] == '.'
Packit 08bd4c
			    && de->d_name[1] == '\0') {
Packit 08bd4c
				/* Skip '.' */
Packit 08bd4c
			} else if (de->d_name[0] == '.'
Packit 08bd4c
			    && de->d_name[1] == '.'
Packit 08bd4c
			    && de->d_name[2] == '\0') {
Packit 08bd4c
				/* Skip '..' */
Packit 08bd4c
			} else {
Packit 08bd4c
				/*
Packit 08bd4c
				 * Append the path to the current path
Packit 08bd4c
				 * and return it.
Packit 08bd4c
				 */
Packit 08bd4c
				tree_append(t, de->d_name, D_NAMELEN(de));
Packit 08bd4c
				t->flags &= ~hasLstat;
Packit 08bd4c
				t->flags &= ~hasStat;
Packit 08bd4c
				return (t->visit_type = TREE_REGULAR);
Packit 08bd4c
			}
Packit 08bd4c
		}
Packit 08bd4c
Packit 08bd4c
		/* If the current dir needs to be visited, set it up. */
Packit 08bd4c
		if (t->stack->flags & needsPreVisit) {
Packit 08bd4c
			t->current = t->stack;
Packit 08bd4c
			tree_append(t, t->stack->name, strlen(t->stack->name));
Packit 08bd4c
			t->stack->flags &= ~needsPreVisit;
Packit 08bd4c
			/* If it is a link, set up fd for the ascent. */
Packit 08bd4c
			if (t->stack->flags & isDirLink) {
Packit 08bd4c
				t->stack->fd = open(".", O_RDONLY);
Packit 08bd4c
				t->openCount++;
Packit 08bd4c
				if (t->openCount > t->maxOpenCount)
Packit 08bd4c
					t->maxOpenCount = t->openCount;
Packit 08bd4c
			}
Packit 08bd4c
			t->dirname_length = t->path_length;
Packit 08bd4c
			if (chdir(t->stack->name) != 0) {
Packit 08bd4c
				/* chdir() failed; return error */
Packit 08bd4c
				tree_pop(t);
Packit 08bd4c
				t->tree_errno = errno;
Packit 08bd4c
				return (t->visit_type = TREE_ERROR_DIR);
Packit 08bd4c
			}
Packit 08bd4c
			t->depth++;
Packit 08bd4c
			t->d = opendir(".");
Packit 08bd4c
			if (t->d == NULL) {
Packit 08bd4c
				tree_ascend(t); /* Undo "chdir" */
Packit 08bd4c
				tree_pop(t);
Packit 08bd4c
				t->tree_errno = errno;
Packit 08bd4c
				return (t->visit_type = TREE_ERROR_DIR);
Packit 08bd4c
			}
Packit 08bd4c
			t->flags &= ~hasLstat;
Packit 08bd4c
			t->flags &= ~hasStat;
Packit 08bd4c
			t->basename = ".";
Packit 08bd4c
			return (t->visit_type = TREE_POSTDESCENT);
Packit 08bd4c
		}
Packit 08bd4c
Packit 08bd4c
		/* We've done everything necessary for the top stack entry. */
Packit 08bd4c
		if (t->stack->flags & needsPostVisit) {
Packit 08bd4c
			tree_ascend(t);
Packit 08bd4c
			tree_pop(t);
Packit 08bd4c
			t->flags &= ~hasLstat;
Packit 08bd4c
			t->flags &= ~hasStat;
Packit 08bd4c
			return (t->visit_type = TREE_POSTASCENT);
Packit 08bd4c
		}
Packit 08bd4c
	}
Packit 08bd4c
	return (t->visit_type = 0);
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * Return error code.
Packit 08bd4c
 */
Packit 08bd4c
int
Packit 08bd4c
tree_errno(struct tree *t)
Packit 08bd4c
{
Packit 08bd4c
	return (t->tree_errno);
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * Called by the client to mark the directory just returned from
Packit 08bd4c
 * tree_next() as needing to be visited.
Packit 08bd4c
 */
Packit 08bd4c
void
Packit 08bd4c
tree_descend(struct tree *t)
Packit 08bd4c
{
Packit 08bd4c
	if (t->visit_type != TREE_REGULAR)
Packit 08bd4c
		return;
Packit 08bd4c
Packit 08bd4c
	if (tree_current_is_physical_dir(t)) {
Packit 08bd4c
		tree_push(t, t->basename);
Packit 08bd4c
		t->stack->flags |= isDir;
Packit 08bd4c
	} else if (tree_current_is_dir(t)) {
Packit 08bd4c
		tree_push(t, t->basename);
Packit 08bd4c
		t->stack->flags |= isDirLink;
Packit 08bd4c
	}
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * Get the stat() data for the entry just returned from tree_next().
Packit 08bd4c
 */
Packit 08bd4c
const struct stat *
Packit 08bd4c
tree_current_stat(struct tree *t)
Packit 08bd4c
{
Packit 08bd4c
	if (!(t->flags & hasStat)) {
Packit 08bd4c
		if (stat(t->basename, &t->st) != 0)
Packit 08bd4c
			return NULL;
Packit 08bd4c
		t->flags |= hasStat;
Packit 08bd4c
	}
Packit 08bd4c
	return (&t->st);
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * Get the lstat() data for the entry just returned from tree_next().
Packit 08bd4c
 */
Packit 08bd4c
const struct stat *
Packit 08bd4c
tree_current_lstat(struct tree *t)
Packit 08bd4c
{
Packit 08bd4c
	if (!(t->flags & hasLstat)) {
Packit 08bd4c
		if (lstat(t->basename, &t->lst) != 0)
Packit 08bd4c
			return NULL;
Packit 08bd4c
		t->flags |= hasLstat;
Packit 08bd4c
	}
Packit 08bd4c
	return (&t->lst);
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * Test whether current entry is a dir or link to a dir.
Packit 08bd4c
 */
Packit 08bd4c
int
Packit 08bd4c
tree_current_is_dir(struct tree *t)
Packit 08bd4c
{
Packit 08bd4c
	const struct stat *st;
Packit 08bd4c
Packit 08bd4c
	/*
Packit 08bd4c
	 * If we already have lstat() info, then try some
Packit 08bd4c
	 * cheap tests to determine if this is a dir.
Packit 08bd4c
	 */
Packit 08bd4c
	if (t->flags & hasLstat) {
Packit 08bd4c
		/* If lstat() says it's a dir, it must be a dir. */
Packit 08bd4c
		if (S_ISDIR(tree_current_lstat(t)->st_mode))
Packit 08bd4c
			return 1;
Packit 08bd4c
		/* Not a dir; might be a link to a dir. */
Packit 08bd4c
		/* If it's not a link, then it's not a link to a dir. */
Packit 08bd4c
		if (!S_ISLNK(tree_current_lstat(t)->st_mode))
Packit 08bd4c
			return 0;
Packit 08bd4c
		/*
Packit 08bd4c
		 * It's a link, but we don't know what it's a link to,
Packit 08bd4c
		 * so we'll have to use stat().
Packit 08bd4c
		 */
Packit 08bd4c
	}
Packit 08bd4c
Packit 08bd4c
	st = tree_current_stat(t);
Packit 08bd4c
	/* If we can't stat it, it's not a dir. */
Packit 08bd4c
	if (st == NULL)
Packit 08bd4c
		return 0;
Packit 08bd4c
	/* Use the definitive test.  Hopefully this is cached. */
Packit 08bd4c
	return (S_ISDIR(st->st_mode));
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * Test whether current entry is a physical directory.  Usually, we
Packit 08bd4c
 * already have at least one of stat() or lstat() in memory, so we
Packit 08bd4c
 * use tricks to try to avoid an extra trip to the disk.
Packit 08bd4c
 */
Packit 08bd4c
int
Packit 08bd4c
tree_current_is_physical_dir(struct tree *t)
Packit 08bd4c
{
Packit 08bd4c
	const struct stat *st;
Packit 08bd4c
Packit 08bd4c
	/*
Packit 08bd4c
	 * If stat() says it isn't a dir, then it's not a dir.
Packit 08bd4c
	 * If stat() data is cached, this check is free, so do it first.
Packit 08bd4c
	 */
Packit 08bd4c
	if ((t->flags & hasStat)
Packit 08bd4c
	    && (!S_ISDIR(tree_current_stat(t)->st_mode)))
Packit 08bd4c
		return 0;
Packit 08bd4c
Packit 08bd4c
	/*
Packit 08bd4c
	 * Either stat() said it was a dir (in which case, we have
Packit 08bd4c
	 * to determine whether it's really a link to a dir) or
Packit 08bd4c
	 * stat() info wasn't available.  So we use lstat(), which
Packit 08bd4c
	 * hopefully is already cached.
Packit 08bd4c
	 */
Packit 08bd4c
Packit 08bd4c
	st = tree_current_lstat(t);
Packit 08bd4c
	/* If we can't stat it, it's not a dir. */
Packit 08bd4c
	if (st == NULL)
Packit 08bd4c
		return 0;
Packit 08bd4c
	/* Use the definitive test.  Hopefully this is cached. */
Packit 08bd4c
	return (S_ISDIR(st->st_mode));
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * Test whether current entry is a symbolic link.
Packit 08bd4c
 */
Packit 08bd4c
int
Packit 08bd4c
tree_current_is_physical_link(struct tree *t)
Packit 08bd4c
{
Packit 08bd4c
	const struct stat *st = tree_current_lstat(t);
Packit 08bd4c
	if (st == NULL)
Packit 08bd4c
		return 0;
Packit 08bd4c
	return (S_ISLNK(st->st_mode));
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * Return the access path for the entry just returned from tree_next().
Packit 08bd4c
 */
Packit 08bd4c
const char *
Packit 08bd4c
tree_current_access_path(struct tree *t)
Packit 08bd4c
{
Packit 08bd4c
	return (t->basename);
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * Return the full path for the entry just returned from tree_next().
Packit 08bd4c
 */
Packit 08bd4c
const char *
Packit 08bd4c
tree_current_path(struct tree *t)
Packit 08bd4c
{
Packit 08bd4c
	return (t->buff);
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * Return the length of the path for the entry just returned from tree_next().
Packit 08bd4c
 */
Packit 08bd4c
size_t
Packit 08bd4c
tree_current_pathlen(struct tree *t)
Packit 08bd4c
{
Packit 08bd4c
	return (t->path_length);
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * Return the nesting depth of the entry just returned from tree_next().
Packit 08bd4c
 */
Packit 08bd4c
int
Packit 08bd4c
tree_current_depth(struct tree *t)
Packit 08bd4c
{
Packit 08bd4c
	return (t->depth);
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * Terminate the traversal and release any resources.
Packit 08bd4c
 */
Packit 08bd4c
void
Packit 08bd4c
tree_close(struct tree *t)
Packit 08bd4c
{
Packit 08bd4c
	/* Release anything remaining in the stack. */
Packit 08bd4c
	while (t->stack != NULL)
Packit 08bd4c
		tree_pop(t);
Packit 08bd4c
	if (t->buff)
Packit 08bd4c
		free(t->buff);
Packit 08bd4c
	/* chdir() back to where we started. */
Packit 08bd4c
	if (t->initialDirFd >= 0) {
Packit 08bd4c
		fchdir(t->initialDirFd);
Packit 08bd4c
		close(t->initialDirFd);
Packit 08bd4c
		t->initialDirFd = -1;
Packit 08bd4c
	}
Packit 08bd4c
	free(t);
Packit 08bd4c
}