|
Packit Service |
1d0348 |
/*-
|
|
Packit Service |
1d0348 |
* Copyright (c) 2003-2007 Tim Kientzle
|
|
Packit Service |
1d0348 |
* All rights reserved.
|
|
Packit Service |
1d0348 |
*
|
|
Packit Service |
1d0348 |
* Redistribution and use in source and binary forms, with or without
|
|
Packit Service |
1d0348 |
* modification, are permitted provided that the following conditions
|
|
Packit Service |
1d0348 |
* are met:
|
|
Packit Service |
1d0348 |
* 1. Redistributions of source code must retain the above copyright
|
|
Packit Service |
1d0348 |
* notice, this list of conditions and the following disclaimer.
|
|
Packit Service |
1d0348 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
Packit Service |
1d0348 |
* notice, this list of conditions and the following disclaimer in the
|
|
Packit Service |
1d0348 |
* documentation and/or other materials provided with the distribution.
|
|
Packit Service |
1d0348 |
*
|
|
Packit Service |
1d0348 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
|
|
Packit Service |
1d0348 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
Packit Service |
1d0348 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
Packit Service |
1d0348 |
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
Packit Service |
1d0348 |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
Packit Service |
1d0348 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
Packit Service |
1d0348 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
Packit Service |
1d0348 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
Packit Service |
1d0348 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
Packit Service |
1d0348 |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
Packit Service |
1d0348 |
*
|
|
Packit Service |
1d0348 |
* $FreeBSD$
|
|
Packit Service |
1d0348 |
*/
|
|
Packit Service |
1d0348 |
|
|
Packit Service |
1d0348 |
/*-
|
|
Packit Service |
1d0348 |
* A set of routines for traversing directory trees.
|
|
Packit Service |
1d0348 |
* Similar in concept to the fts library, but with a few
|
|
Packit Service |
1d0348 |
* important differences:
|
|
Packit Service |
1d0348 |
* * Uses less memory. In particular, fts stores an entire directory
|
|
Packit Service |
1d0348 |
* in memory at a time. This package only keeps enough subdirectory
|
|
Packit Service |
1d0348 |
* information in memory to track the traversal. Information
|
|
Packit Service |
1d0348 |
* about non-directories is discarded as soon as possible.
|
|
Packit Service |
1d0348 |
* * Supports very deep logical traversals. The fts package
|
|
Packit Service |
1d0348 |
* uses "non-chdir" approach for logical traversals. This
|
|
Packit Service |
1d0348 |
* package does use a chdir approach for logical traversals
|
|
Packit Service |
1d0348 |
* and can therefore handle pathnames much longer than
|
|
Packit Service |
1d0348 |
* PATH_MAX.
|
|
Packit Service |
1d0348 |
* * Supports deep physical traversals "out of the box."
|
|
Packit Service |
1d0348 |
* Due to the memory optimizations above, there's no need to
|
|
Packit Service |
1d0348 |
* limit dir names to 32k.
|
|
Packit Service |
1d0348 |
*/
|
|
Packit Service |
1d0348 |
|
|
Packit Service |
1d0348 |
#include <sys/stat.h>
|
|
Packit Service |
1d0348 |
#include <stdio.h>
|
|
Packit Service |
1d0348 |
|
|
Packit Service |
1d0348 |
struct tree;
|
|
Packit Service |
1d0348 |
|
|
Packit Service |
1d0348 |
/* Initiate/terminate a tree traversal. */
|
|
Packit Service |
1d0348 |
struct tree *tree_open(const char * /* pathname */);
|
|
Packit Service |
1d0348 |
void tree_close(struct tree *);
|
|
Packit Service |
1d0348 |
|
|
Packit Service |
1d0348 |
/*
|
|
Packit Service |
1d0348 |
* tree_next() returns Zero if there is no next entry, non-zero if there is.
|
|
Packit Service |
1d0348 |
* Note that directories are potentially visited three times. The first
|
|
Packit Service |
1d0348 |
* time as "regular" file. If tree_descend() is invoked at that time,
|
|
Packit Service |
1d0348 |
* the directory is added to a work list and will be visited two more
|
|
Packit Service |
1d0348 |
* times: once just after descending into the directory and again
|
|
Packit Service |
1d0348 |
* just after ascending back to the parent.
|
|
Packit Service |
1d0348 |
*
|
|
Packit Service |
1d0348 |
* TREE_ERROR is returned if the descent failed (because the
|
|
Packit Service |
1d0348 |
* directory couldn't be opened, for instance). This is returned
|
|
Packit Service |
1d0348 |
* instead of TREE_PREVISIT/TREE_POSTVISIT.
|
|
Packit Service |
1d0348 |
*/
|
|
Packit Service |
1d0348 |
#define TREE_REGULAR 1
|
|
Packit Service |
1d0348 |
#define TREE_POSTDESCENT 2
|
|
Packit Service |
1d0348 |
#define TREE_POSTASCENT 3
|
|
Packit Service |
1d0348 |
#define TREE_ERROR_DIR -1
|
|
Packit Service |
1d0348 |
int tree_next(struct tree *);
|
|
Packit Service |
1d0348 |
|
|
Packit Service |
1d0348 |
int tree_errno(struct tree *);
|
|
Packit Service |
1d0348 |
|
|
Packit Service |
1d0348 |
/*
|
|
Packit Service |
1d0348 |
* Request that current entry be visited. If you invoke it on every
|
|
Packit Service |
1d0348 |
* directory, you'll get a physical traversal. This is ignored if the
|
|
Packit Service |
1d0348 |
* current entry isn't a directory or a link to a directory. So, if
|
|
Packit Service |
1d0348 |
* you invoke this on every returned path, you'll get a full logical
|
|
Packit Service |
1d0348 |
* traversal.
|
|
Packit Service |
1d0348 |
*/
|
|
Packit Service |
1d0348 |
void tree_descend(struct tree *);
|
|
Packit Service |
1d0348 |
|
|
Packit Service |
1d0348 |
/*
|
|
Packit Service |
1d0348 |
* Return information about the current entry.
|
|
Packit Service |
1d0348 |
*/
|
|
Packit Service |
1d0348 |
|
|
Packit Service |
1d0348 |
int tree_current_depth(struct tree *);
|
|
Packit Service |
1d0348 |
/*
|
|
Packit Service |
1d0348 |
* The current full pathname, length of the full pathname,
|
|
Packit Service |
1d0348 |
* and a name that can be used to access the file.
|
|
Packit Service |
1d0348 |
* Because tree does use chdir extensively, the access path is
|
|
Packit Service |
1d0348 |
* almost never the same as the full current path.
|
|
Packit Service |
1d0348 |
*/
|
|
Packit Service |
1d0348 |
const char *tree_current_path(struct tree *);
|
|
Packit Service |
1d0348 |
size_t tree_current_pathlen(struct tree *);
|
|
Packit Service |
1d0348 |
const char *tree_current_access_path(struct tree *);
|
|
Packit Service |
1d0348 |
/*
|
|
Packit Service |
1d0348 |
* Request the lstat() or stat() data for the current path. Since the
|
|
Packit Service |
1d0348 |
* tree package needs to do some of this anyway, and caches the
|
|
Packit Service |
1d0348 |
* results, you should take advantage of it here if you need it rather
|
|
Packit Service |
1d0348 |
* than make a redundant stat() or lstat() call of your own.
|
|
Packit Service |
1d0348 |
*/
|
|
Packit Service |
1d0348 |
const struct stat *tree_current_stat(struct tree *);
|
|
Packit Service |
1d0348 |
const struct stat *tree_current_lstat(struct tree *);
|
|
Packit Service |
1d0348 |
/* The following tests may use mechanisms much faster than stat()/lstat(). */
|
|
Packit Service |
1d0348 |
/* "is_physical_dir" is equivalent to S_ISDIR(tree_current_lstat()->st_mode) */
|
|
Packit Service |
1d0348 |
int tree_current_is_physical_dir(struct tree *);
|
|
Packit Service |
1d0348 |
/* "is_physical_link" is equivalent to S_ISLNK(tree_current_lstat()->st_mode) */
|
|
Packit Service |
1d0348 |
int tree_current_is_physical_link(struct tree *);
|
|
Packit Service |
1d0348 |
/* "is_dir" is equivalent to S_ISDIR(tree_current_stat()->st_mode) */
|
|
Packit Service |
1d0348 |
int tree_current_is_dir(struct tree *);
|
|
Packit Service |
1d0348 |
|
|
Packit Service |
1d0348 |
/* For testing/debugging: Dump the internal status to the given filehandle. */
|
|
Packit Service |
1d0348 |
void tree_dump(struct tree *, FILE *);
|