From: Daniel Ferreira <bnmvco@gmail.com>
To: git@vger.kernel.org
Cc: gitster@pobox.com, sbeller@google.com, pclouds@gmail.com,
mhagger@alum.mit.edu, Daniel Ferreira <bnmvco@gmail.com>
Subject: [PATCH v4 2/5] dir_iterator: iterate over dir after its contents
Date: Tue, 28 Mar 2017 21:32:10 -0300 [thread overview]
Message-ID: <1490747533-89143-3-git-send-email-bnmvco@gmail.com> (raw)
In-Reply-To: <1490747533-89143-1-git-send-email-bnmvco@gmail.com>
Create an option for the dir_iterator API to iterate over subdirectories
only after having iterated through their contents. This feature was
predicted, although not implemented by 0fe5043 ("dir_iterator: new API
for iterating over a directory tree", 2016-06-18).
Add the "flags" parameter to dir_iterator_create, allowing for the
aforementioned "depth-first" iteration mode to be enabled. Currently,
the only acceptable flag is DIR_ITERATOR_DEPTH_FIRST.
This is useful for recursively removing a directory and calling rmdir()
on a directory only after all of its contents have been wiped.
Signed-off-by: Daniel Ferreira <bnmvco@gmail.com>
---
dir-iterator.c | 46 ++++++++++++++++++++++++++++++++++++++++++----
dir-iterator.h | 14 +++++++++++---
2 files changed, 53 insertions(+), 7 deletions(-)
diff --git a/dir-iterator.c b/dir-iterator.c
index 853c040..545d333 100644
--- a/dir-iterator.c
+++ b/dir-iterator.c
@@ -48,6 +48,9 @@ struct dir_iterator_int {
* that will be included in this iteration.
*/
struct dir_iterator_level *levels;
+
+ /* Holds the flags passed to dir_iterator_begin(). */
+ unsigned flags;
};
static inline void push_dir_level(struct dir_iterator_int *iter, struct dir_iterator_level *level)
@@ -114,12 +117,14 @@ int dir_iterator_advance(struct dir_iterator *dir_iterator)
}
level->initialized = 1;
- } else if (S_ISDIR(iter->base.st.st_mode)) {
+ } else if (S_ISDIR(iter->base.st.st_mode) &&
+ !iter->flags & DIR_ITERATOR_DEPTH_FIRST) {
if (level->dir_state == DIR_STATE_ITER) {
/*
* The directory was just iterated
* over; now prepare to iterate into
- * it.
+ * it (unless an option is set for us
+ * to do otherwise).
*/
push_dir_level(iter, level);
continue;
@@ -153,10 +158,27 @@ int dir_iterator_advance(struct dir_iterator *dir_iterator)
de = readdir(level->dir);
if (!de) {
- /* This level is exhausted; pop up a level. */
+ /* This level is exhausted */
if (errno) {
warning("error reading directory %s: %s",
iter->base.path.buf, strerror(errno));
+ } else if (iter->flags & DIR_ITERATOR_DEPTH_FIRST) {
+ /* If we are handling dirpaths after their contents,
+ * we have to iterate over the directory now that we'll
+ * have finished iterating into it. */
+ level->dir = NULL;
+
+ if (pop_dir_level(iter, level) == 0)
+ return dir_iterator_abort(dir_iterator);
+
+ level = &iter->levels[iter->levels_nr - 1];
+ /* Remove a trailing slash */
+ strbuf_strip_suffix(&iter->base.path, "/");
+
+ if (set_iterator_data(iter, level))
+ continue;
+
+ return ITER_OK;
} else if (closedir(level->dir))
warning("error closing directory %s: %s",
iter->base.path.buf, strerror(errno));
@@ -175,8 +197,22 @@ int dir_iterator_advance(struct dir_iterator *dir_iterator)
if (set_iterator_data(iter, level))
continue;
+ /*
+ * If we want to iterate dirs after files, we shall
+ * begin looking into them *before* we return the dir
+ * itself.
+ */
+ if (S_ISDIR(iter->base.st.st_mode) &&
+ iter->flags & DIR_ITERATOR_DEPTH_FIRST) {
+ push_dir_level(iter, level);
+ goto continue_outer_loop;
+ }
+
return ITER_OK;
}
+
+continue_outer_loop:
+ ;
}
}
@@ -201,7 +237,7 @@ int dir_iterator_abort(struct dir_iterator *dir_iterator)
return ITER_DONE;
}
-struct dir_iterator *dir_iterator_begin(const char *path)
+struct dir_iterator *dir_iterator_begin(const char *path, unsigned flags)
{
struct dir_iterator_int *iter = xcalloc(1, sizeof(*iter));
struct dir_iterator *dir_iterator = &iter->base;
@@ -209,6 +245,8 @@ struct dir_iterator *dir_iterator_begin(const char *path)
if (!path || !*path)
die("BUG: empty path passed to dir_iterator_begin()");
+ iter->flags = flags;
+
strbuf_init(&iter->base.path, PATH_MAX);
strbuf_addstr(&iter->base.path, path);
diff --git a/dir-iterator.h b/dir-iterator.h
index 27739e6..28ff3df 100644
--- a/dir-iterator.h
+++ b/dir-iterator.h
@@ -38,6 +38,13 @@
* dir_iterator_advance() again.
*/
+/* Possible flags for dir_iterator_begin().
+ *
+ * DIR_ITERATOR_DEPTH_FIRST: ensures subdirectories and their contents
+ * are iterated through before the containing directory.
+ */
+#define DIR_ITERATOR_DEPTH_FIRST (1 << 1)
+
struct dir_iterator {
/* The current path: */
struct strbuf path;
@@ -57,15 +64,16 @@ struct dir_iterator {
};
/*
- * Start a directory iteration over path. Return a dir_iterator that
- * holds the internal state of the iteration.
+ * Start a directory iteration over path, with options specified in
+ * 'flags'. Return a dir_iterator that holds the internal state of
+ * the iteration.
*
* 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.
*/
-struct dir_iterator *dir_iterator_begin(const char *path);
+struct dir_iterator *dir_iterator_begin(const char *path, unsigned flags);
/*
* Advance the iterator to the first or next item and return ITER_OK.
--
2.7.4 (Apple Git-66)
next prev parent reply other threads:[~2017-03-29 0:35 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-03-29 0:32 [PATCH v4 0/5] [GSoC] remove_subtree(): reimplement using iterators Daniel Ferreira
2017-03-29 0:32 ` [PATCH v4 1/5] dir_iterator: add helpers to dir_iterator_advance Daniel Ferreira
2017-03-29 4:32 ` Michael Haggerty
2017-03-29 0:32 ` Daniel Ferreira [this message]
2017-03-29 9:56 ` [PATCH v4 2/5] dir_iterator: iterate over dir after its contents Michael Haggerty
2017-03-29 10:44 ` Michael Haggerty
2017-03-29 16:46 ` Junio C Hamano
2017-03-30 4:59 ` Michael Haggerty
2017-03-30 6:08 ` Junio C Hamano
2017-03-30 6:39 ` Michael Haggerty
2017-03-30 11:08 ` Duy Nguyen
2017-04-02 4:25 ` Daniel Ferreira (theiostream)
2017-04-05 9:21 ` Duy Nguyen
2017-03-30 17:26 ` Junio C Hamano
2017-03-29 0:32 ` [PATCH v4 3/5] remove_subtree(): reimplement using iterators Daniel Ferreira
2017-03-29 10:01 ` Michael Haggerty
2017-03-29 0:32 ` [PATCH v4 4/5] remove_subtree(): test removing nested directories Daniel Ferreira
2017-03-29 0:32 ` [PATCH v4 5/5] files_reflog_iterator: amend use of dir_iterator Daniel Ferreira
2017-03-29 10:45 ` Michael Haggerty
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1490747533-89143-3-git-send-email-bnmvco@gmail.com \
--to=bnmvco@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=mhagger@alum.mit.edu \
--cc=pclouds@gmail.com \
--cc=sbeller@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).