From 4fa1b32871f661a7e11898b274ec162d52c23ee9 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Fri, 1 Jun 2012 20:04:39 -0500 Subject: Factor out dirtree_comeagain() callback, setting up depth-first search with open filehandle in node->extra. --- lib/dirtree.c | 17 +++++++++++++++++ lib/lib.h | 1 + 2 files changed, 18 insertions(+) (limited to 'lib') diff --git a/lib/dirtree.c b/lib/dirtree.c index 418e8eb1..01e0ea8c 100644 --- a/lib/dirtree.c +++ b/lib/dirtree.c @@ -78,6 +78,23 @@ int dirtree_notdotdot(struct dirtree *catch) return DIRTREE_SAVE|DIRTREE_RECURSE; } +// depth first recursion +int dirtree_comeagain(struct dirtree *try, int recurse) +{ + int ret = dirtree_notdotdot(try); + if (ret) { + if (S_ISDIR(try->st.st_mode)) { + if (!try->extra) { + try->extra = xdup(try->data); + if (recurse) return DIRTREE_COMEAGAIN; + } + } else try->extra = openat(try->parent ? try->parent->data : AT_FDCWD, + try->name, 0); + } + + return ret; +} + // Handle callback for a node in the tree. Returns saved node(s) or NULL. // // By default, allocates a tree of struct dirtree, not following symlinks diff --git a/lib/lib.h b/lib/lib.h index 5a184ccf..88810b1f 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -74,6 +74,7 @@ struct dirtree { struct dirtree *dirtree_add_node(int dirfd, char *name); char *dirtree_path(struct dirtree *node, int *plen); int dirtree_notdotdot(struct dirtree *catch); +int dirtree_comeagain(struct dirtree *try, int recurse); struct dirtree *handle_callback(struct dirtree *new, int (*callback)(struct dirtree *node)); void dirtree_recurse(struct dirtree *node, -- cgit v1.2.3