Blame contrib/shar/tree.h

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
 * $FreeBSD$
Packit 08bd4c
 */
Packit 08bd4c
Packit 08bd4c
/*-
Packit 08bd4c
 * A set of routines for traversing directory trees.
Packit 08bd4c
 * Similar in concept to the fts library, but with a few
Packit 08bd4c
 * important differences:
Packit 08bd4c
 *    * Uses less memory.  In particular, fts stores an entire directory
Packit 08bd4c
 *      in memory at a time.  This package only keeps enough subdirectory
Packit 08bd4c
 *      information in memory to track the traversal.  Information
Packit 08bd4c
 *      about non-directories is discarded as soon as possible.
Packit 08bd4c
 *    * Supports very deep logical traversals.  The fts package
Packit 08bd4c
 *      uses "non-chdir" approach for logical traversals.  This
Packit 08bd4c
 *      package does use a chdir approach for logical traversals
Packit 08bd4c
 *      and can therefore handle pathnames much longer than
Packit 08bd4c
 *      PATH_MAX.
Packit 08bd4c
 *    * Supports deep physical traversals "out of the box."
Packit 08bd4c
 *      Due to the memory optimizations above, there's no need to
Packit 08bd4c
 *      limit dir names to 32k.
Packit 08bd4c
 */
Packit 08bd4c
Packit 08bd4c
#include <sys/stat.h>
Packit 08bd4c
#include <stdio.h>
Packit 08bd4c
Packit 08bd4c
struct tree;
Packit 08bd4c
Packit 08bd4c
/* Initiate/terminate a tree traversal. */
Packit 08bd4c
struct tree *tree_open(const char * /* pathname */);
Packit 08bd4c
void tree_close(struct tree *);
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * tree_next() returns Zero if there is no next entry, non-zero if there is.
Packit 08bd4c
 * Note that directories are potentially visited three times.  The first
Packit 08bd4c
 * time as "regular" file.  If tree_descend() is invoked at that time,
Packit 08bd4c
 * the directory is added to a work list and will be visited two more
Packit 08bd4c
 * times:  once just after descending into the directory and again
Packit 08bd4c
 * just after ascending back to the parent.
Packit 08bd4c
 *
Packit 08bd4c
 * TREE_ERROR is returned if the descent failed (because the
Packit 08bd4c
 * directory couldn't be opened, for instance).  This is returned
Packit 08bd4c
 * instead of TREE_PREVISIT/TREE_POSTVISIT.
Packit 08bd4c
 */
Packit 08bd4c
#define	TREE_REGULAR	1
Packit 08bd4c
#define	TREE_POSTDESCENT	2
Packit 08bd4c
#define	TREE_POSTASCENT	3
Packit 08bd4c
#define	TREE_ERROR_DIR	-1
Packit 08bd4c
int tree_next(struct tree *);
Packit 08bd4c
Packit 08bd4c
int tree_errno(struct tree *);
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * Request that current entry be visited.  If you invoke it on every
Packit 08bd4c
 * directory, you'll get a physical traversal.  This is ignored if the
Packit 08bd4c
 * current entry isn't a directory or a link to a directory.  So, if
Packit 08bd4c
 * you invoke this on every returned path, you'll get a full logical
Packit 08bd4c
 * traversal.
Packit 08bd4c
 */
Packit 08bd4c
void tree_descend(struct tree *);
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * Return information about the current entry.
Packit 08bd4c
 */
Packit 08bd4c
Packit 08bd4c
int tree_current_depth(struct tree *);
Packit 08bd4c
/*
Packit 08bd4c
 * The current full pathname, length of the full pathname,
Packit 08bd4c
 * and a name that can be used to access the file.
Packit 08bd4c
 * Because tree does use chdir extensively, the access path is
Packit 08bd4c
 * almost never the same as the full current path.
Packit 08bd4c
 */
Packit 08bd4c
const char *tree_current_path(struct tree *);
Packit 08bd4c
size_t tree_current_pathlen(struct tree *);
Packit 08bd4c
const char *tree_current_access_path(struct tree *);
Packit 08bd4c
/*
Packit 08bd4c
 * Request the lstat() or stat() data for the current path.  Since the
Packit 08bd4c
 * tree package needs to do some of this anyway, and caches the
Packit 08bd4c
 * results, you should take advantage of it here if you need it rather
Packit 08bd4c
 * than make a redundant stat() or lstat() call of your own.
Packit 08bd4c
 */
Packit 08bd4c
const struct stat *tree_current_stat(struct tree *);
Packit 08bd4c
const struct stat *tree_current_lstat(struct tree *);
Packit 08bd4c
/* The following tests may use mechanisms much faster than stat()/lstat(). */
Packit 08bd4c
/* "is_physical_dir" is equivalent to S_ISDIR(tree_current_lstat()->st_mode) */
Packit 08bd4c
int tree_current_is_physical_dir(struct tree *);
Packit 08bd4c
/* "is_physical_link" is equivalent to S_ISLNK(tree_current_lstat()->st_mode) */
Packit 08bd4c
int tree_current_is_physical_link(struct tree *);
Packit 08bd4c
/* "is_dir" is equivalent to S_ISDIR(tree_current_stat()->st_mode) */
Packit 08bd4c
int tree_current_is_dir(struct tree *);
Packit 08bd4c
Packit 08bd4c
/* For testing/debugging: Dump the internal status to the given filehandle. */
Packit 08bd4c
void tree_dump(struct tree *, FILE *);