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

[PATCH v2 15/15] entry.c: use dir-iterator to avoid explicit dir traversal

From
PKPlato Kiorpelidis <kioplato@gmail.com>
Date
May 9, 2022, 17:51 UTC
Message-ID
<20220509175159.2948802-16-kioplato@gmail.com>
In-Reply-To
<20220509175159.2948802-1-kioplato@gmail.com>

Replace usage of opendir/readdir/closedir API to traverse directories recursively, at remove_subtree() function, by the dir-iterator API. This simplifies the code and avoids recursive calls to remove_subtree().

Signed-off-by: Plato Kiorpelidis <kioplato@gmail.com>
---
 entry.c | 38 +++++++++++++++++++-------------------
 1 file changed, 19 insertions(+), 19 deletions(-)
diff --git a/entry.c b/entry.c
index 1c9df62b30..46ef145f97 100644
--- a/entry.c
+++ b/entry.c
@@ -8,6 +8,8 @@
 #include "fsmonitor.h"
 #include "entry.h"
 #include "parallel-checkout.h"
+#include "iterator.h"
+#include "dir-iterator.h"
 
 static void create_directories(const char *path, int path_len,
 			       const struct checkout *state)
@@ -52,26 +54,24 @@ 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_skip_dot_and_dotdot(dir)) != NULL) {
-		struct stat st;
-
-		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))
-			die_errno("cannot unlink '%s'", path->buf);
-		strbuf_setlen(path, origlen);
+	unsigned int flags = DIR_ITERATOR_DIRS_AFTER;
+	struct dir_iterator *iter = NULL;
+	int ok;
+
+	if (!(iter = dir_iterator_begin(path->buf, flags)))
+		die_errno("cannot open directory '%s'", path->buf);
+
+	while ((ok = dir_iterator_advance(iter)) == ITER_OK) {
+		if (S_ISDIR(iter->st.st_mode) && rmdir(iter->path.buf))
+			die_errno("cannot rmdir '%s'", iter->path.buf);
+
+		if (!S_ISDIR(iter->st.st_mode) && unlink(iter->path.buf))
+			die_errno("cannot unlink '%s'", iter->path.buf);
 	}
-	closedir(dir);
+
+	if (ok != ITER_DONE)
+		die_errno("failed to iterate over directory '%s'", path->buf);
+
 	if (rmdir(path->buf))
 		die_errno("cannot rmdir '%s'", path->buf);
 }
-- 
2.36.1
Previous: Plato KiorpelidisNext: Phillip Wood
Message 20 of 31 in “[GSoC] iterate dirs before or after their contents”
  1. 00/15 [GSoC] iterate dirs before or after their contentsPlato Kiorpelidis, May 9, 2022
  2. 01/15 t0066: refactor dir-iterator testsPlato Kiorpelidis, May 9, 2022
  3. 02/15 t0066: remove dependency between unrelated testsPlato Kiorpelidis, May 9, 2022
  4. 03/15 t0066: shorter expected and actual output file namesPlato Kiorpelidis, May 9, 2022
  5. 04/15 test-dir-iterator: consistently return EXIT_FAILURE or EXIT_SUCCESSPlato Kiorpelidis, May 9, 2022
  6. Junio C HamanoMay 9, 2022
  7. Plato KiorpelidisMay 18, 2022
  8. Junio C HamanoMay 18, 2022
  9. 05/15 test-dir-iterator: print EACCES and ELOOP errno set by dir_iteratorPlato Kiorpelidis, May 9, 2022
  10. 06/15 test-dir-iterator: print errno name set by dir_iterator_advancePlato Kiorpelidis, May 9, 2022
  11. 09/15 t0066: rename test directoriesPlato Kiorpelidis, May 9, 2022
  12. 08/15 t0066: reorder tests from simple to more complexPlato Kiorpelidis, May 9, 2022
  13. 07/15 t0066: better test coverage for dir-iteratorPlato Kiorpelidis, May 9, 2022
  14. 10/15 dir-iterator: refactor dir_iterator_advance()Plato Kiorpelidis, May 9, 2022
  15. Junio C HamanoMay 9, 2022
  16. Plato KiorpelidisMay 18, 2022
  17. Phillip WoodMay 10, 2022
  18. 11/15 dir-iterator: open root dir in dir_iterator_begin()Plato Kiorpelidis, May 9, 2022
  19. 12/15 t0066: rename subtest descriptionsPlato Kiorpelidis, May 9, 2022
  20. 15/15 entry.c: use dir-iterator to avoid explicit dir traversalPlato Kiorpelidis, May 9, 2022
  21. Phillip WoodMay 10, 2022
  22. 13/15 dir-iterator: option to iterate dirs in pre-orderPlato Kiorpelidis, May 9, 2022
  23. Phillip WoodMay 10, 2022
  24. Plato KiorpelidisMay 18, 2022
  25. rsbecker@nexbridge.comMay 18, 2022
  26. Junio C HamanoMay 18, 2022
  27. rsbecker@nexbridge.comMay 18, 2022
  28. 14/15 dir-iterator: option to iterate dirs in post-orderPlato Kiorpelidis, May 9, 2022
  29. Phillip WoodMay 10, 2022
  30. Junio C HamanoMay 10, 2022
  31. Plato KiorpelidisMay 20, 2022

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.