git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] entry.c: use dir-iterator to avoid explicit dir traversal
@ 2019-12-08 18:04 otalpster
  2019-12-09 15:21 ` Derrick Stolee
  2019-12-09 17:41 ` Matheus Tavares Bernardino
  0 siblings, 2 replies; 6+ messages in thread
From: otalpster @ 2019-12-08 18:04 UTC (permalink / raw)
  To: git; +Cc: mhagger, matheus.bernardino, Plato

From: Plato <otalpster@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 <otalpster@gmail.com>
---
Hello,

This is my first patch.
I hope I cc'd the correct people and didn't mess up.

The changes pass the test suite t/ and Travis CI.
Please point out any mistakes.

Thanks for your time! :)

 entry.c | 32 +++++++++++++++-----------------
 1 file changed, 15 insertions(+), 17 deletions(-)

diff --git a/entry.c b/entry.c
index 53380bb614..e7f4881d3b 100644
--- a/entry.c
+++ b/entry.c
@@ -2,6 +2,8 @@
 #include "blob.h"
 #include "object-store.h"
 #include "dir.h"
+#include "iterator.h"
+#include "dir-iterator.h"
 #include "streaming.h"
 #include "submodule.h"
 #include "progress.h"
@@ -50,29 +52,25 @@ 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;
+	int ok;
+	unsigned int flags = DIR_ITERATOR_PEDANTIC;
+	struct dir_iterator *iter = dir_iterator_begin(path->buf, flags);
 
-	if (!dir)
+	if (!iter)
 		die_errno("cannot opendir '%s'", path->buf);
-	while ((de = readdir(dir)) != NULL) {
-		struct stat st;
 
-		if (is_dot_or_dotdot(de->d_name))
+	while ((ok = dir_iterator_advance(iter)) == ITER_OK) {
+		if (is_dot_or_dotdot(iter->path.buf))
 			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))
-			die_errno("cannot unlink '%s'", path->buf);
-		strbuf_setlen(path, origlen);
+		if (unlink(iter->path.buf)) {
+			die_errno("cannot unlink '%s'", iter->path.buf);
+		}
 	}
-	closedir(dir);
+
+	if (ok != ITER_DONE)
+		die(_("failed to iterate over '%s'"), path->buf);
+
 	if (rmdir(path->buf))
 		die_errno("cannot rmdir '%s'", path->buf);
 }
-- 
2.24.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2019-12-16 16:13 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-08 18:04 [PATCH] entry.c: use dir-iterator to avoid explicit dir traversal otalpster
2019-12-09 15:21 ` Derrick Stolee
2019-12-09 17:41 ` Matheus Tavares Bernardino
2019-12-09 21:18   ` Junio C Hamano
2019-12-10  4:38     ` Matheus Tavares Bernardino
2019-12-16 16:12       ` otalpster

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).