From patchwork Tue Feb 26 12:28:28 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 10830121 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id BD5C613B5 for ; Tue, 26 Feb 2019 12:29:02 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A97192A3A0 for ; Tue, 26 Feb 2019 12:29:02 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9DDFC2A5D9; Tue, 26 Feb 2019 12:29:02 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8947A2A5CF for ; Tue, 26 Feb 2019 12:29:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726797AbfBZM3A (ORCPT ); Tue, 26 Feb 2019 07:29:00 -0500 Received: from mail-wr1-f65.google.com ([209.85.221.65]:43970 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726346AbfBZM27 (ORCPT ); Tue, 26 Feb 2019 07:28:59 -0500 Received: by mail-wr1-f65.google.com with SMTP id d17so13701275wre.10 for ; Tue, 26 Feb 2019 04:28:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=s19ZXMiZ2Pz/32SzBHiGwaVI4JPvSPJxwUqDbiZAkAo=; b=FnDMy48TZyMyST5bmYYH8Us3hpzVFC5XVLbEQhftHxdt2PneIP5AtFCuCaAo2W3XoT CHoHhhqmpqW2blOuIbCkhfjuaP+tE7u/WqFGX2a4fU1p4mWNyvNxDrmLjXrd1DZbvvWd O2j/+mKup7qFdDDIFJMn9obd9QPATevF8cjDom5loueWVSVbsT0POSw0auCAgy47FtXq QbbpOz8AgZVtM7xbHcUc9IY4DBXxAjmQvdv84gbTwvtGe5sWxGHdv+ODoUleIxgNzhw0 Ff18C2NPInyRFbhZjejAOOKAEpGrTtzUxwflwoxK3m1dBhg8Y0ldQNo2I1Petod8R353 ryTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=s19ZXMiZ2Pz/32SzBHiGwaVI4JPvSPJxwUqDbiZAkAo=; b=TnDyTW1f045np+l0H6aZ16pvzpYUEmZnkXIHW/F/nslGPmkLyMixcK58aybMtw1etz wSWoWtcpiZtybmJsS+YJeCu4WJ7HvQn5SbeG4ABnlVmpnw4+7sqqiipFE6WA8prNUfV9 PUWHNoTg4gvNvoKgxyUSOuRXUBUT/GDV0MvrGDeHQg77fVXa56alh4otGy8pa1M2uq6Z LInJ+Hv3PYriu20v2Vax4gOwzo3it6NT3ppIA8nIPi5xMzNgcTioqaJhpOBFcX2I9ZW3 hy5E0SCwAGCC4GY4NJJP5fUQWCEa0LmWaeXihqn8MyUPQt4RhaFFs1f6PPW8qE5klEut RC1Q== X-Gm-Message-State: AHQUAuZk28XBI+P5nIykvE2yxaPyLyUk/tKu7xJmShc9eWwW121OrUaY j6MAp1X333Bb+ktgYJukg0Pysng3nyg= X-Google-Smtp-Source: AHgI3IbhFiEJFcTlKMfeE8Qr1ukuoyO78YJOHw201IyO2I2KPMWPsf0xnLFEatlz0MTYLG5WpEGLHQ== X-Received: by 2002:adf:a49c:: with SMTP id g28mr15717362wrb.147.1551184137082; Tue, 26 Feb 2019 04:28:57 -0800 (PST) Received: from vm.nix.is ([2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id v6sm2733165wme.24.2019.02.26.04.28.56 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Tue, 26 Feb 2019 04:28:56 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , =?utf-8?b?Tmd1eeG7hW4gVGjDoWkgTmc=?= =?utf-8?b?4buNYyBEdXk=?= , Matheus Tavares , Thomas Gummerer Subject: [RFC PATCH v3 4/5] clone: extract function from copy_or_link_directory Date: Tue, 26 Feb 2019 13:28:28 +0100 Message-Id: <20190226122829.19178-5-avarab@gmail.com> X-Mailer: git-send-email 2.21.0.rc2.261.ga7da99ff1b In-Reply-To: <20190226051804.10631-1-matheus.bernardino@usp.br> References: <20190226051804.10631-1-matheus.bernardino@usp.br> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Matheus Tavares 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 a following patch. Also makes code more readable. Signed-off-by: Matheus Tavares --- builtin/clone.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/builtin/clone.c b/builtin/clone.c index cae069f03b..fd580fa98d 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 st; + + if (mkdir(pathname, mode)) { + if (errno != EEXIST) + die_errno(_("failed to create directory '%s'"), + pathname); + else if (stat(pathname, &st)) + die_errno(_("failed to stat '%s'"), pathname); + else if (!S_ISDIR(st.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;