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

[PATCH v3 2/2] [GSoC] remove_subtree(): reimplement using iterators

From
Daniel Ferreira <bnmvco@gmail.com>
Date
Mar 25, 2017, 18:12 UTC
Message-ID
<1490465551-71056-3-git-send-email-bnmvco@gmail.com>
In-Reply-To
<1490465551-71056-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 | 32 +++++++++++---------------------
 1 file changed, 11 insertions(+), 21 deletions(-)
diff --git a/entry.c b/entry.c
index c6eea24..670ffeb 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,17 @@ 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);
+	diter->options.iterate_dirs_after_files = 1;
+
+	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: Daniel FerreiraNext: Daniel Ferreira
Message 2 of 6 in “[GSoC] remove_subtree(): reimplement using iterators”
  1. 0/2 [GSoC] remove_subtree(): reimplement using iteratorsDaniel Ferreira, Mar 25, 2017
  2. 2/2 [GSoC] remove_subtree(): reimplement using iteratorsDaniel Ferreira, Mar 25, 2017
  3. 1/2 [GSoC] dir_iterator: iterate over dir after its contentsDaniel Ferreira, Mar 25, 2017
  4. Michael HaggertyMar 26, 2017
  5. Michael HaggertyMar 26, 2017
  6. Stefan BellerMar 28, 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.