2016-06-18 04:15:18 +00:00
|
|
|
#ifndef DIR_ITERATOR_H
|
|
|
|
#define DIR_ITERATOR_H
|
|
|
|
|
2018-08-15 17:54:05 +00:00
|
|
|
#include "strbuf.h"
|
|
|
|
|
2016-06-18 04:15:18 +00:00
|
|
|
/*
|
|
|
|
* Iterate over a directory tree.
|
|
|
|
*
|
|
|
|
* Iterate over a directory tree, recursively, including paths of all
|
|
|
|
* types and hidden paths. Skip "." and ".." entries and don't follow
|
2019-07-10 23:58:59 +00:00
|
|
|
* symlinks except for the original path. Note that the original path
|
|
|
|
* is not included in the iteration.
|
2016-06-18 04:15:18 +00:00
|
|
|
*
|
|
|
|
* Every time dir_iterator_advance() is called, update the members of
|
|
|
|
* the dir_iterator structure to reflect the next path in the
|
|
|
|
* iteration. The order that paths are iterated over within a
|
2019-07-10 23:58:59 +00:00
|
|
|
* directory is undefined, directory paths are always given before
|
|
|
|
* their contents.
|
2016-06-18 04:15:18 +00:00
|
|
|
*
|
|
|
|
* A typical iteration looks like this:
|
|
|
|
*
|
|
|
|
* int ok;
|
2019-07-10 23:59:00 +00:00
|
|
|
* unsigned int flags = DIR_ITERATOR_PEDANTIC;
|
|
|
|
* struct dir_iterator *iter = dir_iterator_begin(path, flags);
|
2019-07-10 23:58:59 +00:00
|
|
|
*
|
|
|
|
* if (!iter)
|
|
|
|
* goto error_handler;
|
2016-06-18 04:15:18 +00:00
|
|
|
*
|
|
|
|
* while ((ok = dir_iterator_advance(iter)) == ITER_OK) {
|
|
|
|
* if (want_to_stop_iteration()) {
|
|
|
|
* ok = dir_iterator_abort(iter);
|
|
|
|
* break;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* // Access information about the current path:
|
|
|
|
* if (S_ISDIR(iter->st.st_mode))
|
|
|
|
* printf("%s is a directory\n", iter->relative_path);
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* if (ok != ITER_DONE)
|
|
|
|
* handle_error();
|
|
|
|
*
|
|
|
|
* Callers are allowed to modify iter->path while they are working,
|
|
|
|
* but they must restore it to its original contents before calling
|
|
|
|
* dir_iterator_advance() again.
|
|
|
|
*/
|
|
|
|
|
2019-07-10 23:59:00 +00:00
|
|
|
/*
|
|
|
|
* Flags for dir_iterator_begin:
|
|
|
|
*
|
|
|
|
* - DIR_ITERATOR_PEDANTIC: override dir-iterator's default behavior
|
|
|
|
* in case of an error at dir_iterator_advance(), which is to keep
|
|
|
|
* looking for a next valid entry. With this flag, resources are freed
|
|
|
|
* and ITER_ERROR is returned immediately. In both cases, a meaningful
|
|
|
|
* warning is emitted. Note: ENOENT errors are always ignored so that
|
|
|
|
* the API users may remove files during iteration.
|
|
|
|
*
|
|
|
|
* - DIR_ITERATOR_FOLLOW_SYMLINKS: make dir-iterator follow symlinks.
|
|
|
|
* i.e., linked directories' contents will be iterated over and
|
|
|
|
* iter->base.st will contain information on the referred files,
|
|
|
|
* not the symlinks themselves, which is the default behavior. Broken
|
|
|
|
* symlinks are ignored.
|
|
|
|
*
|
2023-01-25 00:43:51 +00:00
|
|
|
* Note: setting DIR_ITERATOR_FOLLOW_SYMLINKS affects resolving the
|
|
|
|
* starting path as well (e.g., attempting to iterate starting at a
|
|
|
|
* symbolic link pointing to a directory without FOLLOW_SYMLINKS will
|
|
|
|
* result in an error).
|
|
|
|
*
|
2019-07-10 23:59:00 +00:00
|
|
|
* Warning: circular symlinks are also followed when
|
|
|
|
* DIR_ITERATOR_FOLLOW_SYMLINKS is set. The iteration may end up with
|
|
|
|
* an ELOOP if they happen and DIR_ITERATOR_PEDANTIC is set.
|
|
|
|
*/
|
|
|
|
#define DIR_ITERATOR_PEDANTIC (1 << 0)
|
|
|
|
#define DIR_ITERATOR_FOLLOW_SYMLINKS (1 << 1)
|
|
|
|
|
2016-06-18 04:15:18 +00:00
|
|
|
struct dir_iterator {
|
|
|
|
/* The current path: */
|
|
|
|
struct strbuf path;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The current path relative to the starting path. This part
|
|
|
|
* of the path always uses "/" characters to separate path
|
|
|
|
* components:
|
|
|
|
*/
|
|
|
|
const char *relative_path;
|
|
|
|
|
|
|
|
/* The current basename: */
|
|
|
|
const char *basename;
|
|
|
|
|
2019-07-10 23:59:00 +00:00
|
|
|
/*
|
|
|
|
* The result of calling lstat() on path; or stat(), if the
|
|
|
|
* DIR_ITERATOR_FOLLOW_SYMLINKS flag was set at
|
|
|
|
* dir_iterator's initialization.
|
|
|
|
*/
|
2016-06-18 04:15:18 +00:00
|
|
|
struct stat st;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2019-07-10 23:59:00 +00:00
|
|
|
* Start a directory iteration over path with the combination of
|
|
|
|
* options specified by flags. On success, return a dir_iterator
|
|
|
|
* that holds the internal state of the iteration. In case of
|
|
|
|
* failure, return NULL and set errno accordingly.
|
2016-06-18 04:15:18 +00:00
|
|
|
*
|
|
|
|
* The iteration includes all paths under path, not including path
|
|
|
|
* itself and not including "." or ".." entries.
|
|
|
|
*
|
2019-07-10 23:59:00 +00:00
|
|
|
* Parameters are:
|
|
|
|
* - path is the starting directory. An internal copy will be made.
|
|
|
|
* - flags is a combination of the possible flags to initialize a
|
|
|
|
* dir-iterator or 0 for default behavior.
|
2016-06-18 04:15:18 +00:00
|
|
|
*/
|
2019-07-10 23:59:00 +00:00
|
|
|
struct dir_iterator *dir_iterator_begin(const char *path, unsigned int flags);
|
2016-06-18 04:15:18 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Advance the iterator to the first or next item and return ITER_OK.
|
|
|
|
* If the iteration is exhausted, free the dir_iterator and any
|
2019-07-10 23:59:00 +00:00
|
|
|
* resources associated with it and return ITER_DONE.
|
|
|
|
*
|
|
|
|
* It is a bug to use iterator or call this function again after it
|
|
|
|
* has returned ITER_DONE or ITER_ERROR (which may be returned iff
|
|
|
|
* the DIR_ITERATOR_PEDANTIC flag was set).
|
2016-06-18 04:15:18 +00:00
|
|
|
*/
|
|
|
|
int dir_iterator_advance(struct dir_iterator *iterator);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* End the iteration before it has been exhausted. Free the
|
|
|
|
* dir_iterator and any associated resources and return ITER_DONE. On
|
|
|
|
* error, free the dir_iterator and return ITER_ERROR.
|
|
|
|
*/
|
|
|
|
int dir_iterator_abort(struct dir_iterator *iterator);
|
|
|
|
|
|
|
|
#endif
|