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

[GSoC][PATCH 2/2] clone: use dir-iterator to avoid explicit dir traversal

From
Matheus Tavares <matheus.bernardino@usp.br>
Date
Feb 15, 2019, 15:49 UTC
Message-ID
<20190215154913.18800-3-matheus.bernardino@usp.br>
In-Reply-To
<20190215154913.18800-1-matheus.bernardino@usp.br>

Replace usage of opendir/readdir/closedir API to traverse directories recursively, at copy_or_link_directory function, by the dir-iterator API.

Signed-off-by: Matheus Tavares <matheus.bernardino@usp.br>
---
 builtin/clone.c | 39 +++++++++++++++++++--------------------
 1 file changed, 19 insertions(+), 20 deletions(-)
diff --git a/builtin/clone.c b/builtin/clone.c
index 2a1cc4dab9..66ae347f79 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -23,6 +23,8 @@
 #include "transport.h"
 #include "strbuf.h"
 #include "dir.h"
+#include "dir-iterator.h"
+#include "iterator.h"
 #include "sigchain.h"
 #include "branch.h"
 #include "remote.h"
@@ -413,40 +415,33 @@ static void mkdir_if_missing(const char *pathname, mode_t mode)
 static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest,
 				   const char *src_repo, int src_baselen)
 {
-	struct dirent *de;
-	struct stat buf;
 	int src_len, dest_len;
-	DIR *dir;
-
-	dir = opendir(src->buf);
-	if (!dir)
-		die_errno(_("failed to open '%s'"), src->buf);
+	struct dir_iterator *iter;
+	int iter_status;
 
 	mkdir_if_missing(dest->buf, 0777);
 
+	iter = dir_iterator_begin(src->buf);
+
 	strbuf_addch(src, '/');
 	src_len = src->len;
 	strbuf_addch(dest, '/');
 	dest_len = dest->len;
 
-	while ((de = readdir(dir)) != NULL) {
+	while ((iter_status = dir_iterator_advance(iter)) == ITER_OK) {
 		strbuf_setlen(src, src_len);
-		strbuf_addstr(src, de->d_name);
+		strbuf_addstr(src, iter->relative_path);
 		strbuf_setlen(dest, dest_len);
-		strbuf_addstr(dest, de->d_name);
-		if (stat(src->buf, &buf)) {
-			warning (_("failed to stat %s\n"), src->buf);
-			continue;
-		}
-		if (S_ISDIR(buf.st_mode)) {
-			if (de->d_name[0] != '.')
-				copy_or_link_directory(src, dest,
-						       src_repo, src_baselen);
+		strbuf_addstr(dest, iter->relative_path);
+
+		if (S_ISDIR(iter->st.st_mode)) {
+			if (iter->basename[0] != '.')
+				mkdir_if_missing(dest->buf, 0777);
 			continue;
 		}
 
 		/* Files that cannot be copied bit-for-bit... */
-		if (!strcmp(src->buf + src_baselen, "/info/alternates")) {
+		if (!strcmp(iter->relative_path, "info/alternates")) {
 			copy_alternates(src, dest, src_repo);
 			continue;
 		}
@@ -463,7 +458,11 @@ static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest,
 		if (copy_file_with_time(dest->buf, src->buf, 0666))
 			die_errno(_("failed to copy file to '%s'"), dest->buf);
 	}
-	closedir(dir);
+
+	if (iter_status != ITER_DONE) {
+		strbuf_setlen(src, src_len);
+		die(_("failed to iterate over '%s'"), src->buf);
+	}
 }
 
 static void clone_local(const char *src_repo, const char *dest_repo)
-- 
2.20.1
Previous: Matheus Tavares BernardinoNext: Christian Couder
Message 5 of 14 in “clone: convert explicit traversal to”
  1. Matheus TavaresFeb 15, 2019
  2. [GSoC][PATCH 1/2] clone: extract function from copy_or_link_directoryMatheus Tavares, Feb 15, 2019
  3. Christian CouderFeb 16, 2019
  4. Matheus Tavares BernardinoFeb 18, 2019
  5. [GSoC][PATCH 2/2] clone: use dir-iterator to avoid explicit dir traversalMatheus Tavares, Feb 15, 2019
  6. Christian CouderFeb 16, 2019
  7. Thomas GummererFeb 16, 2019
  8. Matheus Tavares BernardinoFeb 18, 2019
  9. Thomas GummererFeb 18, 2019
  10. Matheus Tavares BernardinoFeb 19, 2019
  11. Thomas GummererFeb 19, 2019
  12. Matheus Tavares BernardinoFeb 21, 2019
  13. Thomas GummererFeb 21, 2019
  14. Daniel Ferreira (theiostream)Feb 22, 2019

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.