dir-iterator: add flags parameter to dir_iterator_begin

Add the possibility of giving flags to dir_iterator_begin to initialize
a dir-iterator with special options.

Currently possible flags are:
- DIR_ITERATOR_PEDANTIC, which makes dir_iterator_advance abort
immediately in the case of an error, instead of keep looking for the
next valid entry;
- DIR_ITERATOR_FOLLOW_SYMLINKS, which makes the iterator follow
symlinks and include linked directories' contents in the iteration.

These new flags will be used in a subsequent patch.

Also add tests for the flags' usage and adjust refs/files-backend.c to
the new dir_iterator_begin signature.

Signed-off-by: Matheus Tavares <matheus.bernardino@usp.br>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Matheus Tavares
2019-07-10 20:59:00 -03:00
committed by Junio C Hamano
parent 3012397e03
commit fa1da7d2ee
5 changed files with 190 additions and 35 deletions

View File

@@ -20,7 +20,8 @@
* A typical iteration looks like this:
*
* int ok;
* struct dir_iterator *iter = dir_iterator_begin(path);
* unsigned int flags = DIR_ITERATOR_PEDANTIC;
* struct dir_iterator *iter = dir_iterator_begin(path, flags);
*
* if (!iter)
* goto error_handler;
@@ -44,6 +45,29 @@
* dir_iterator_advance() again.
*/
/*
* 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.
*
* 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)
struct dir_iterator {
/* The current path: */
struct strbuf path;
@@ -58,29 +82,38 @@ struct dir_iterator {
/* The current basename: */
const char *basename;
/* The result of calling lstat() on path: */
/*
* The result of calling lstat() on path; or stat(), if the
* DIR_ITERATOR_FOLLOW_SYMLINKS flag was set at
* dir_iterator's initialization.
*/
struct stat st;
};
/*
* Start a directory iteration over path. On success, return a
* dir_iterator that holds the internal state of the iteration.
* In case of failure, return NULL and set errno accordingly.
* 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.
*
* The iteration includes all paths under path, not including path
* itself and not including "." or ".." entries.
*
* path is the starting directory. An internal copy will be made.
* 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.
*/
struct dir_iterator *dir_iterator_begin(const char *path);
struct dir_iterator *dir_iterator_begin(const char *path, unsigned int flags);
/*
* Advance the iterator to the first or next item and return ITER_OK.
* If the iteration is exhausted, free the dir_iterator and any
* resources associated with it and return ITER_DONE. On error, free
* dir_iterator and associated resources and return ITER_ERROR. It is
* a bug to use iterator or call this function again after it has
* returned ITER_DONE or ITER_ERROR.
* 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).
*/
int dir_iterator_advance(struct dir_iterator *iterator);