git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Matheus Tavares <matheus.bernardino@usp.br>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>
Subject: [GSoC][PATCH 1/2] clone: extract function from copy_or_link_directory
Date: Fri, 15 Feb 2019 13:49:12 -0200	[thread overview]
Message-ID: <20190215154913.18800-2-matheus.bernardino@usp.br> (raw)
In-Reply-To: <20190215154913.18800-1-matheus.bernardino@usp.br>

Extract dir creation code snippet from copy_or_link_directory to its own
function named mkdir_if_missing. This change will help removing
copy_or_link_directory's explicit recursion, which will be done in patch
"clone: use dir-iterator to avoid explicit dir traversal". Also makes
code more readable.

Signed-off-by: Matheus Tavares <matheus.bernardino@usp.br>
---
 builtin/clone.c | 27 +++++++++++++++++++--------
 1 file changed, 19 insertions(+), 8 deletions(-)

diff --git a/builtin/clone.c b/builtin/clone.c
index 50bde99618..2a1cc4dab9 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -392,6 +392,24 @@ static void copy_alternates(struct strbuf *src, struct strbuf *dst,
 	fclose(in);
 }
 
+static void mkdir_if_missing(const char *pathname, mode_t mode)
+{
+	/*
+	 * Create a dir at pathname unless there's already one.
+	 */
+	struct stat buf;
+
+	if (mkdir(pathname, mode)) {
+		if (errno != EEXIST)
+			die_errno(_("failed to create directory '%s'"),
+				  pathname);
+		else if (stat(pathname, &buf))
+			die_errno(_("failed to stat '%s'"), pathname);
+		else if (!S_ISDIR(buf.st_mode))
+			die(_("%s exists and is not a directory"), pathname);
+	}
+}
+
 static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest,
 				   const char *src_repo, int src_baselen)
 {
@@ -404,14 +422,7 @@ static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest,
 	if (!dir)
 		die_errno(_("failed to open '%s'"), src->buf);
 
-	if (mkdir(dest->buf, 0777)) {
-		if (errno != EEXIST)
-			die_errno(_("failed to create directory '%s'"), dest->buf);
-		else if (stat(dest->buf, &buf))
-			die_errno(_("failed to stat '%s'"), dest->buf);
-		else if (!S_ISDIR(buf.st_mode))
-			die(_("%s exists and is not a directory"), dest->buf);
-	}
+	mkdir_if_missing(dest->buf, 0777);
 
 	strbuf_addch(src, '/');
 	src_len = src->len;
-- 
2.20.1


  reply	other threads:[~2019-02-15 15:49 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-15 15:49 [GSoC][PATCH 0/2] clone: convert explicit traversal to Matheus Tavares
2019-02-15 15:49 ` Matheus Tavares [this message]
2019-02-16  6:23   ` [GSoC][PATCH 1/2] clone: extract function from copy_or_link_directory Christian Couder
2019-02-18 21:28     ` Matheus Tavares Bernardino
2019-02-15 15:49 ` [GSoC][PATCH 2/2] clone: use dir-iterator to avoid explicit dir traversal Matheus Tavares
2019-02-16  6:41   ` Christian Couder
2019-02-16 14:38   ` Thomas Gummerer
2019-02-18 21:13     ` Matheus Tavares Bernardino
2019-02-18 23:35       ` Thomas Gummerer
2019-02-19 16:23         ` Matheus Tavares Bernardino
2019-02-19 23:45           ` Thomas Gummerer
2019-02-21  4:46             ` Matheus Tavares Bernardino
2019-02-21 21:25               ` Thomas Gummerer
     [not found]       ` <CAMy9T_GzgPPMFLrPNbtf4zaYtyCoUDjXQbd2z8JeXFYogvfrVQ@mail.gmail.com>
2019-02-22  0:22         ` Daniel Ferreira (theiostream)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190215154913.18800-2-matheus.bernardino@usp.br \
    --to=matheus.bernardino@usp.br \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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 NNTP newsgroup(s).