From f63aa21beaffbb8da4978bfa37f070170cb8122e Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sun, 13 Nov 2016 21:29:04 -0500 Subject: bftw: Keep trailing slashes on the root in BFTW_DEPTH mode. --- bftw.c | 22 ++++++++++++++++------ tests.sh | 6 +++++- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/bftw.c b/bftw.c index 1feeeed..630c643 100644 --- a/bftw.c +++ b/bftw.c @@ -621,6 +621,8 @@ struct bftw_state { /** The current traversal status. */ enum bftw_status status; + /** The root path of the walk. */ + const char *root; /** The current path being explored. */ char *path; @@ -633,7 +635,8 @@ struct bftw_state { /** * Initialize the bftw() state. */ -static int bftw_state_init(struct bftw_state *state, bftw_fn *fn, int nopenfd, int flags, void *ptr) { +static int bftw_state_init(struct bftw_state *state, const char *root, bftw_fn *fn, int nopenfd, int flags, void *ptr) { + state->root = root; state->fn = fn; state->flags = flags; state->ptr = ptr; @@ -697,10 +700,17 @@ static int bftw_path_concat(struct bftw_state *state, const char *subpath) { static void bftw_path_trim(struct bftw_state *state) { struct dircache_entry *current = state->current; - size_t length = current->nameoff + current->namelen; - if (current->namelen > 1) { - // Trim the trailing slash - --length; + size_t length; + if (current->depth == 0) { + // Use exactly the string passed to bftw(), including any + // trailing slashes + length = strlen(state->root); + } else { + length = current->nameoff + current->namelen; + if (current->namelen > 1) { + // Trim the trailing slash + --length; + } } dstresize(&state->path, length); @@ -958,7 +968,7 @@ int bftw(const char *path, bftw_fn *fn, int nopenfd, enum bftw_flags flags, void int ret = -1, error; struct bftw_state state; - if (bftw_state_init(&state, fn, nopenfd, flags, ptr) != 0) { + if (bftw_state_init(&state, path, fn, nopenfd, flags, ptr) != 0) { return -1; } diff --git a/tests.sh b/tests.sh index 87a0ea1..cf39fd6 100755 --- a/tests.sh +++ b/tests.sh @@ -444,7 +444,11 @@ function test_0080() { yes | "$BFS" "$basic" -okdir cat ';' 2>/dev/null } -for i in {1..80}; do +function test_0081() { + find_diff "$basic/" -depth +} + +for i in {1..81}; do test="test_$(printf '%04d' $i)" if [ -t 1 ]; then -- cgit v1.2.3