clone: extract function from copy_or_link_directory

Extract dir creation code snippet from copy_or_link_directory to its own function named mkdir_if_missing. This change will help to remove copy_or_link_directory's explicit recursion, which will be done in a following patch. Also makes the code more readable. Signed-off-by: Matheus Tavares <matheus.bernardino@usp.br> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Matheus Tavares committed Jul 10, 2019 at 20:59 UTC 14954b799f0bac76e500593c95d1f274cf0636e5
1 file changed +16 -8
builtin/clone.c
+16 -8
@@ -391,6 +391,21 @@ static void copy_alternates(struct strbuf *src, const char *src_repo)
391 fclose(in);
392 }
393
394 +static void mkdir_if_missing(const char *pathname, mode_t mode)
395 +{
396 + struct stat st;
397 +
398 + if (!mkdir(pathname, mode))
399 + return;
400 +
401 + if (errno != EEXIST)
402 + die_errno(_("failed to create directory '%s'"), pathname);
403 + else if (stat(pathname, &st))
404 + die_errno(_("failed to stat '%s'"), pathname);
405 + else if (!S_ISDIR(st.st_mode))
406 + die(_("%s exists and is not a directory"), pathname);
407 +}
408 +
409 static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest,
410 const char *src_repo, int src_baselen)
411 {
@@ -403,14 +418,7 @@ static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest,
418 if (!dir)
419 die_errno(_("failed to open '%s'"), src->buf);
420
406 - if (mkdir(dest->buf, 0777)) {
407 - if (errno != EEXIST)
408 - die_errno(_("failed to create directory '%s'"), dest->buf);
409 - else if (stat(dest->buf, &buf))
410 - die_errno(_("failed to stat '%s'"), dest->buf);
411 - else if (!S_ISDIR(buf.st_mode))
412 - die(_("%s exists and is not a directory"), dest->buf);
413 - }
421 + mkdir_if_missing(dest->buf, 0777);
422
423 strbuf_addch(src, '/');
424 src_len = src->len;