git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v4 3/5] remove_subtree(): reimplement using iterators

From
Daniel Ferreira <bnmvco@gmail.com>
Date
Mar 29, 2017, 00:32 UTC
Message-ID
<1490747533-89143-4-git-send-email-bnmvco@gmail.com>
In-Reply-To
<1490747533-89143-1-git-send-email-bnmvco@gmail.com>

Use dir_iterator to traverse through remove_subtree()'s directory tree, avoiding the need for recursive calls to readdir(). Simplify remove_subtree()'s code.

A conversion similar in purpose was previously done at 46d092a ("for_each_reflog(): reimplement using iterators", 2016-05-21).

Signed-off-by: Daniel Ferreira <bnmvco@gmail.com>
---
 entry.c | 31 ++++++++++---------------------
 1 file changed, 10 insertions(+), 21 deletions(-)
diff --git a/entry.c b/entry.c
index c6eea24..bbebd16 100644
--- a/entry.c
+++ b/entry.c
@@ -2,6 +2,8 @@
 #include "blob.h"
 #include "dir.h"
 #include "streaming.h"
+#include "iterator.h"
+#include "dir-iterator.h"

 static void create_directories(const char *path, int path_len,
 			       const struct checkout *state)
@@ -46,29 +48,16 @@ static void create_directories(const char *path, int path_len,

 static void remove_subtree(struct strbuf *path)
 {
-	DIR *dir = opendir(path->buf);
-	struct dirent *de;
-	int origlen = path->len;
-
-	if (!dir)
-		die_errno("cannot opendir '%s'", path->buf);
-	while ((de = readdir(dir)) != NULL) {
-		struct stat st;
-
-		if (is_dot_or_dotdot(de->d_name))
-			continue;
-
-		strbuf_addch(path, '/');
-		strbuf_addstr(path, de->d_name);
-		if (lstat(path->buf, &st))
-			die_errno("cannot lstat '%s'", path->buf);
-		if (S_ISDIR(st.st_mode))
-			remove_subtree(path);
-		else if (unlink(path->buf))
+	struct dir_iterator *diter = dir_iterator_begin(path->buf, DIR_ITERATOR_DEPTH_FIRST);
+
+	while (dir_iterator_advance(diter) == ITER_OK) {
+		if (S_ISDIR(diter->st.st_mode)) {
+			if (rmdir(diter->path.buf))
+				die_errno("cannot rmdir '%s'", path->buf);
+		} else if (unlink(diter->path.buf))
 			die_errno("cannot unlink '%s'", path->buf);
-		strbuf_setlen(path, origlen);
 	}
-	closedir(dir);
+
 	if (rmdir(path->buf))
 		die_errno("cannot rmdir '%s'", path->buf);
 }
--
2.7.4 (Apple Git-66)
Previous: Michael HaggertyNext: Michael Haggerty
Message 7 of 19 in “[GSoC] remove_subtree(): reimplement using iterators”
  1. 0/5 [GSoC] remove_subtree(): reimplement using iteratorsDaniel Ferreira, Mar 29, 2017
  2. 4/5 remove_subtree(): test removing nested directoriesDaniel Ferreira, Mar 29, 2017
  3. 5/5 files_reflog_iterator: amend use of dir_iteratorDaniel Ferreira, Mar 29, 2017
  4. Michael HaggertyMar 29, 2017
  5. 1/5 dir_iterator: add helpers to dir_iterator_advanceDaniel Ferreira, Mar 29, 2017
  6. Michael HaggertyMar 29, 2017
  7. 3/5 remove_subtree(): reimplement using iteratorsDaniel Ferreira, Mar 29, 2017
  8. Michael HaggertyMar 29, 2017
  9. 2/5 dir_iterator: iterate over dir after its contentsDaniel Ferreira, Mar 29, 2017
  10. Michael HaggertyMar 29, 2017
  11. Michael HaggertyMar 29, 2017
  12. Junio C HamanoMar 29, 2017
  13. Michael HaggertyMar 30, 2017
  14. Junio C HamanoMar 30, 2017
  15. Michael HaggertyMar 30, 2017
  16. Duy NguyenMar 30, 2017
  17. Daniel Ferreira (theiostream)Apr 2, 2017
  18. Duy NguyenApr 5, 2017
  19. Junio C HamanoMar 30, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.