From patchwork Wed Sep 8 00:38:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12479617 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7D306C433F5 for ; Wed, 8 Sep 2021 00:38:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 48E6A610F8 for ; Wed, 8 Sep 2021 00:38:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233133AbhIHAjk (ORCPT ); Tue, 7 Sep 2021 20:39:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41366 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230315AbhIHAjj (ORCPT ); Tue, 7 Sep 2021 20:39:39 -0400 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A1C9AC061757 for ; Tue, 7 Sep 2021 17:38:32 -0700 (PDT) Received: by mail-wr1-x42c.google.com with SMTP id z4so580175wrr.6 for ; Tue, 07 Sep 2021 17:38:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=/nBy4/aoyqoYntf/S7KMkKeLOU/XPNj1ixlMSq3cKa4=; b=ALbhQvJxVpM1qhGRl+MQ99EtzetClEofQ7Mgg4FYMSMmnH7SLnmTxhtcls+g1Hyl2i D08HJJWRVOF+FZZYMtgxpXKeDMAkKJWyr8cin8ClqzDwACA1qrYrau3ZPYSdzgNCR+z1 tZNo98SI7FvjLowLVUlg9vi9mSGkY8CaqSj8UkTB4EevpGRt29dgViig0NMlsJk18RWC Nfo6Q0RU4gU6vqgDUm6HdPLGHjtVHmeZJOS+MwULCLICU1YhP2llfwc5BB85i8tvIozB PQd7YbsYI/IEkzaXu2WQi2rqH9AlKHnjZrK4DItWZ6toh7r87QJtpjivmvMA8MD1lBvb 0Brg== 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=/nBy4/aoyqoYntf/S7KMkKeLOU/XPNj1ixlMSq3cKa4=; b=W/XCYALUSWSuMOLgbvgZGCvF9oRnZPh2Gr/+k8wNBF3MdQU12fD4EuYQ4LxRNGxXCD 63Bs4loY+oGY1m1z4F8v6sTHFi3IdWOtxjetkaEaYHYmWcbaQWmnSfaz3emLNt73wZP3 zkJbp10Q4lcb7JTLyanmbx9bvwvqY1aGHVK2KR66uCcB5rpmmxR4UexY00u5/Nt8jbOT FWzj7Jiwtbj9VS/dLc1yzsFcecYYGV1S9OWmIPHtYowfroUfo56JIe/Nh8XTBSfspEfi XNTl95Q5VLbpOy56PECZR1PLq93zICZnLZZ8O1S6xHy+ULTdOOSJq8DW9nhCwcTV7lOM k8+A== X-Gm-Message-State: AOAM5321Oo58JHrw/iqsUFUOU/hNT8R8CWSIm1SaxpD3EAygJI/XzLW9 iZOYgM0wpQJgdrRh0DShk+rR5ElNx4xvwA== X-Google-Smtp-Source: ABdhPJwDyoARrlAAwrxRl32bhyIOgfvQkPfIm80w2kFM7uHtBacAlRXkNjIPy6fYgBIy4Ah5mEbdCw== X-Received: by 2002:adf:c510:: with SMTP id q16mr942045wrf.203.1631061511033; Tue, 07 Sep 2021 17:38:31 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id f17sm548962wmf.4.2021.09.07.17.38.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Sep 2021 17:38:30 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Taylor Blau , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFz?= =?utf-8?b?b24=?= Subject: [PATCH v2 1/4] pack.h: line-wrap the definition of finish_tmp_packfile() Date: Wed, 8 Sep 2021 02:38:25 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.819.gea1b153a43c In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Line-wrap the definition of finish_tmp_packfile() to make subsequent changes easier to read. See 0e990530ae (finish_tmp_packfile(): a helper function, 2011-10-28) for the commit that introduced this overly long line. Signed-off-by: Ævar Arnfjörð Bjarmason --- pack.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pack.h b/pack.h index fa13954526..1c17254c0a 100644 --- a/pack.h +++ b/pack.h @@ -110,6 +110,11 @@ int encode_in_pack_object_header(unsigned char *hdr, int hdr_len, int read_pack_header(int fd, struct pack_header *); struct hashfile *create_tmp_packfile(char **pack_tmp_name); -void finish_tmp_packfile(struct strbuf *name_buffer, const char *pack_tmp_name, struct pack_idx_entry **written_list, uint32_t nr_written, struct pack_idx_option *pack_idx_opts, unsigned char sha1[]); +void finish_tmp_packfile(struct strbuf *name_buffer, + const char *pack_tmp_name, + struct pack_idx_entry **written_list, + uint32_t nr_written, + struct pack_idx_option *pack_idx_opts, + unsigned char sha1[]); #endif From patchwork Wed Sep 8 00:38:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12479621 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3343EC4332F for ; Wed, 8 Sep 2021 00:38:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 16689610F8 for ; Wed, 8 Sep 2021 00:38:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236589AbhIHAjm (ORCPT ); Tue, 7 Sep 2021 20:39:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41372 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230315AbhIHAjk (ORCPT ); Tue, 7 Sep 2021 20:39:40 -0400 Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5C42CC061575 for ; Tue, 7 Sep 2021 17:38:33 -0700 (PDT) Received: by mail-wr1-x42a.google.com with SMTP id q26so569789wrc.7 for ; Tue, 07 Sep 2021 17:38:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=cKSO5NqG5eYlJNvLaglwAoZ6U5gCOtopvVQZt+kWIXI=; b=EcioPPvoeJ/W/I2fiJD+zozqZINL6F7lgcjtGqeUbGSByqrqv4uK5JcvaKIs5kbQoS sMuhp25QYYqu73LfSvEZYjjMBNwLWpJ+GzjJVMBLtWMNYf6/5KRxvXhgdKY3S+Zv5FXA 2YzhCxt995uRV9FrurV72b+lWpEtM6A48wOpDxbZUkl/pFILASF3ds/kLVjm7c6c3pU1 AB9/euunDC01dGBRtbCyvwnCHOCCkj1Av3x6WH5sxu1n/3SlaDvGrTjriXRovbM1/Hpj Euuh0mSTkPSQidsXRhpXaAQoD3GW6wDPoxVUjz5X8JFwPWUL55To1Ae+oNBzvetXolUR oG5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=cKSO5NqG5eYlJNvLaglwAoZ6U5gCOtopvVQZt+kWIXI=; b=AFsrkgn2pNLAmG/EJ2aCZmkzz/f6sc6VUQe+gdKbqc+3LdjcryRDSmJiJWbj3ZsqzV tR1EPWIaTu/XDzudqvCI4hHcSOOKtBPkObjKASlamFliksrZbH5teIylH6tvQhRVcoJE t7hodVrefJ//30hw14jNVJEH10+b4DgdBh9XZ1qWXPJm9ohdmwYqAKNHMO9AX/5X40PE sJ10p3g/Y2+jVQRWR1iQikmiFulSXqc2Pi4YzHX8UkZ2ectSGhwxodX6qoctfpVsfjQy LtDg1URgvnCJFHn5aC6gLYDZtCcRYPhJiBM7MU1LoVOmiYmlyUgXrRdktCYZKGyyIKCn ebWg== X-Gm-Message-State: AOAM532TE0UrNL/vsxTiDkvGKfrECF2sfIpVNF5zgyG7fMrtffZLWl87 A9io9MUe9li60mDVyxTbha93/JisRTOc5A== X-Google-Smtp-Source: ABdhPJyy62WOEB81otENvTeigjjSiZpr+iz6jUB9MTZ6UrZNHN7SxFHuBRbGUeKmnuFVoAMHezQSYQ== X-Received: by 2002:adf:de09:: with SMTP id b9mr949181wrm.114.1631061511694; Tue, 07 Sep 2021 17:38:31 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id f17sm548962wmf.4.2021.09.07.17.38.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Sep 2021 17:38:31 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Taylor Blau , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFz?= =?utf-8?b?b24=?= Subject: [PATCH v2 2/4] pack-write: refactor renaming in finish_tmp_packfile() Date: Wed, 8 Sep 2021 02:38:26 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.819.gea1b153a43c In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Refactor the renaming in finish_tmp_packfile() so that it takes a "const struct strbuf *" instead of a non-const, and refactor the repetitive renaming pattern in finish_tmp_packfile() to use a new static rename_tmp_packfile() helper function. The previous strbuf_reset() idiom originated with 5889271114a (finish_tmp_packfile():use strbuf for pathname construction, 2014-03-03), which in turn was a minimal adjustment of pre-strbuf code added in 0e990530ae (finish_tmp_packfile(): a helper function, 2011-10-28). Since the memory allocation is not a bottleneck here we can afford a bit more readability at the cost of re-allocating this new "struct strbuf sb". Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/pack-objects.c | 8 ++++++-- pack-write.c | 41 +++++++++++++++++++---------------------- pack.h | 2 +- 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c index df49f656b9..f2569b5ca2 100644 --- a/builtin/pack-objects.c +++ b/builtin/pack-objects.c @@ -1250,7 +1250,10 @@ static void write_pack_file(void) &pack_idx_opts, hash); if (write_bitmap_index) { - strbuf_addf(&tmpname, "%s.bitmap", hash_to_hex(hash)); + struct strbuf sb = STRBUF_INIT; + + strbuf_addf(&sb, "%s%s.bitmap", tmpname.buf, + hash_to_hex(hash)); stop_progress(&progress_state); @@ -1258,8 +1261,9 @@ static void write_pack_file(void) bitmap_writer_select_commits(indexed_commits, indexed_commits_nr, -1); bitmap_writer_build(&to_pack); bitmap_writer_finish(written_list, nr_written, - tmpname.buf, write_bitmap_options); + sb.buf, write_bitmap_options); write_bitmap_index = 0; + strbuf_release(&sb); } strbuf_release(&tmpname); diff --git a/pack-write.c b/pack-write.c index 277c60165e..363b395d67 100644 --- a/pack-write.c +++ b/pack-write.c @@ -462,7 +462,21 @@ struct hashfile *create_tmp_packfile(char **pack_tmp_name) return hashfd(fd, *pack_tmp_name); } -void finish_tmp_packfile(struct strbuf *name_buffer, +static void rename_tmp_packfile(const char *source, + const struct strbuf *basename, + const unsigned char hash[], + const char *ext) +{ + struct strbuf sb = STRBUF_INIT; + + strbuf_addf(&sb, "%s%s.%s", basename->buf, hash_to_hex(hash), ext); + if (rename(source, sb.buf)) + die_errno("unable to rename temporary '*.%s' file to '%s'", + ext, sb.buf); + strbuf_release(&sb); +} + +void finish_tmp_packfile(const struct strbuf *basename, const char *pack_tmp_name, struct pack_idx_entry **written_list, uint32_t nr_written, @@ -470,7 +484,6 @@ void finish_tmp_packfile(struct strbuf *name_buffer, unsigned char hash[]) { const char *idx_tmp_name, *rev_tmp_name = NULL; - int basename_len = name_buffer->len; if (adjust_shared_perm(pack_tmp_name)) die_errno("unable to make temporary pack file readable"); @@ -483,26 +496,10 @@ void finish_tmp_packfile(struct strbuf *name_buffer, rev_tmp_name = write_rev_file(NULL, written_list, nr_written, hash, pack_idx_opts->flags); - strbuf_addf(name_buffer, "%s.pack", hash_to_hex(hash)); - - if (rename(pack_tmp_name, name_buffer->buf)) - die_errno("unable to rename temporary pack file"); - - strbuf_setlen(name_buffer, basename_len); - - if (rev_tmp_name) { - strbuf_addf(name_buffer, "%s.rev", hash_to_hex(hash)); - if (rename(rev_tmp_name, name_buffer->buf)) - die_errno("unable to rename temporary reverse-index file"); - - strbuf_setlen(name_buffer, basename_len); - } - - strbuf_addf(name_buffer, "%s.idx", hash_to_hex(hash)); - if (rename(idx_tmp_name, name_buffer->buf)) - die_errno("unable to rename temporary index file"); - - strbuf_setlen(name_buffer, basename_len); + rename_tmp_packfile(pack_tmp_name, basename, hash, "pack"); + if (rev_tmp_name) + rename_tmp_packfile(rev_tmp_name, basename, hash, "rev"); + rename_tmp_packfile(idx_tmp_name, basename, hash, "idx"); free((void *)idx_tmp_name); } diff --git a/pack.h b/pack.h index 1c17254c0a..594d5176aa 100644 --- a/pack.h +++ b/pack.h @@ -110,7 +110,7 @@ int encode_in_pack_object_header(unsigned char *hdr, int hdr_len, int read_pack_header(int fd, struct pack_header *); struct hashfile *create_tmp_packfile(char **pack_tmp_name); -void finish_tmp_packfile(struct strbuf *name_buffer, +void finish_tmp_packfile(const struct strbuf *basename, const char *pack_tmp_name, struct pack_idx_entry **written_list, uint32_t nr_written, From patchwork Wed Sep 8 00:38:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12479623 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 40F5AC433EF for ; Wed, 8 Sep 2021 00:38:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2224761139 for ; Wed, 8 Sep 2021 00:38:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238893AbhIHAjm (ORCPT ); Tue, 7 Sep 2021 20:39:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41378 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229946AbhIHAjl (ORCPT ); Tue, 7 Sep 2021 20:39:41 -0400 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 31AFDC061575 for ; Tue, 7 Sep 2021 17:38:34 -0700 (PDT) Received: by mail-wr1-x42c.google.com with SMTP id n5so541994wro.12 for ; Tue, 07 Sep 2021 17:38:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ZEf6xw8hPAuDqSw+0a54BIDm8CT8PWgMmKtMEly3Gh0=; b=a/THvqK25Z9DQV1bVbTApHHukeL9PO2nTaIyGLzXJNjtsh0n1U6663bMfqm4OOsJ/U lgG9DWw4Hy1T3FL3Yp7MYPLKt4LuKq4xrfePLYEvaTrWc1kxynK+OYZVvHwWuc81v6FL zs4Y/FoZ5pVBefAkZOL0Du3aZYCkkZ7M2GN2PAotFci6qOUTirVUnwuGWHuWqJNzibwG bwsgSjwT9t+0g3tdXhCnQ5jmGsGxT+612H2opz7nsfKoPbisjH36asYrppAZVrsNjOn2 k0Bct5ZKv18COPNKCp6TV5M4yOeNxE7ZYAKwgMV688c1c+n304i4NsdVlY0pX0BzLpwY g/Gw== 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=ZEf6xw8hPAuDqSw+0a54BIDm8CT8PWgMmKtMEly3Gh0=; b=Q8bPwd/FO87qihEdMUTsbOVU5BJcVf9FWjPXURZmjvGLJ9XFwJL/6bOmmR5uC1XdfF 50nGZTBmaeeWkznhlEzgXCH+S3NwgdsTGVs8O9JePh/UScpTuwYH3O4xHRpw8/SEPA1m ZhTEWTHdrsASlmfgnNRXERiWFpcYHsS09buG+Xbi6E+/pZYRRaqX2K2xlC6ZAbXZgdsP HnC1vghKUI0KvkLHAzuf8ffCKtfRptLwU+uTmnwA3Gzvv1mcNZ/bp0DZ6jXCdD8R3fV/ SGYLDL7/11gF7V/QPXfQbwm+lvij8bT6ZCrGLUHZ5ja0zlyCiUXoZ/r9sLLEEgnfLo9g 4TYQ== X-Gm-Message-State: AOAM533wPwMAAEkCo3xirC1wOn3H647wOdSap0Ya+cO28MIVxGI1Axg/ McFkFxTjpL+Up2U+tldObwEZz7WL90ZLJw== X-Google-Smtp-Source: ABdhPJybcp1TKekwo4rhDKDqc2/cZ1WH0rMRbDlXvqpL1LLM9AJ4HQHQiDOCgXwKxLRa4DREvZo7IA== X-Received: by 2002:adf:f541:: with SMTP id j1mr969181wrp.180.1631061512459; Tue, 07 Sep 2021 17:38:32 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id f17sm548962wmf.4.2021.09.07.17.38.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Sep 2021 17:38:31 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Taylor Blau , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFz?= =?utf-8?b?b24=?= Subject: [PATCH v2 3/4] pack-write: split up finish_tmp_packfile() function Date: Wed, 8 Sep 2021 02:38:27 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.819.gea1b153a43c In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Split up the finish_tmp_packfile() function and use the split-up version in pack-objects.c in preparation for moving the step of renaming the *.idx file later as part of a function change. Since the only other caller of finish_tmp_packfile() was in bulk-checkin.c, and it won't be needing a change to its *.idx renaming, provide a thin wrapper for the old function as a static function in that file. If other callers end up needing the simpler version it could be moved back to "pack-write.c" and "pack.h". Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/pack-objects.c | 7 +++++-- bulk-checkin.c | 16 ++++++++++++++++ pack-write.c | 22 +++++++++++++--------- pack.h | 7 +++++-- 4 files changed, 39 insertions(+), 13 deletions(-) diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c index f2569b5ca2..33567aaa74 100644 --- a/builtin/pack-objects.c +++ b/builtin/pack-objects.c @@ -1217,6 +1217,7 @@ static void write_pack_file(void) if (!pack_to_stdout) { struct stat st; struct strbuf tmpname = STRBUF_INIT; + char *idx_tmp_name = NULL; /* * Packs are runtime accessed in their mtime @@ -1245,9 +1246,10 @@ static void write_pack_file(void) &to_pack, written_list, nr_written); } - finish_tmp_packfile(&tmpname, pack_tmp_name, + stage_tmp_packfiles(&tmpname, pack_tmp_name, written_list, nr_written, - &pack_idx_opts, hash); + &pack_idx_opts, hash, &idx_tmp_name); + rename_tmp_packfile_idx(&tmpname, hash, &idx_tmp_name); if (write_bitmap_index) { struct strbuf sb = STRBUF_INIT; @@ -1266,6 +1268,7 @@ static void write_pack_file(void) strbuf_release(&sb); } + free(idx_tmp_name); strbuf_release(&tmpname); free(pack_tmp_name); puts(hash_to_hex(hash)); diff --git a/bulk-checkin.c b/bulk-checkin.c index b023d9959a..ca7697e9f7 100644 --- a/bulk-checkin.c +++ b/bulk-checkin.c @@ -23,6 +23,22 @@ static struct bulk_checkin_state { uint32_t nr_written; } state; +static void finish_tmp_packfile(const struct strbuf *basename, + const char *pack_tmp_name, + struct pack_idx_entry **written_list, + uint32_t nr_written, + struct pack_idx_option *pack_idx_opts, + unsigned char hash[]) +{ + char *idx_tmp_name = NULL; + + stage_tmp_packfiles(basename, pack_tmp_name, written_list, nr_written, + pack_idx_opts, hash, &idx_tmp_name); + rename_tmp_packfile_idx(basename, hash, &idx_tmp_name); + + free(idx_tmp_name); +} + static void finish_bulk_checkin(struct bulk_checkin_state *state) { struct object_id oid; diff --git a/pack-write.c b/pack-write.c index 363b395d67..09af8fccae 100644 --- a/pack-write.c +++ b/pack-write.c @@ -476,21 +476,28 @@ static void rename_tmp_packfile(const char *source, strbuf_release(&sb); } -void finish_tmp_packfile(const struct strbuf *basename, +void rename_tmp_packfile_idx(const struct strbuf *basename, + unsigned char hash[], char **idx_tmp_name) +{ + rename_tmp_packfile(*idx_tmp_name, basename, hash, "idx"); +} + +void stage_tmp_packfiles(const struct strbuf *basename, const char *pack_tmp_name, struct pack_idx_entry **written_list, uint32_t nr_written, struct pack_idx_option *pack_idx_opts, - unsigned char hash[]) + unsigned char hash[], + char **idx_tmp_name) { - const char *idx_tmp_name, *rev_tmp_name = NULL; + const char *rev_tmp_name = NULL; if (adjust_shared_perm(pack_tmp_name)) die_errno("unable to make temporary pack file readable"); - idx_tmp_name = write_idx_file(NULL, written_list, nr_written, - pack_idx_opts, hash); - if (adjust_shared_perm(idx_tmp_name)) + *idx_tmp_name = (char *)write_idx_file(NULL, written_list, nr_written, + pack_idx_opts, hash); + if (adjust_shared_perm(*idx_tmp_name)) die_errno("unable to make temporary index file readable"); rev_tmp_name = write_rev_file(NULL, written_list, nr_written, hash, @@ -499,9 +506,6 @@ void finish_tmp_packfile(const struct strbuf *basename, rename_tmp_packfile(pack_tmp_name, basename, hash, "pack"); if (rev_tmp_name) rename_tmp_packfile(rev_tmp_name, basename, hash, "rev"); - rename_tmp_packfile(idx_tmp_name, basename, hash, "idx"); - - free((void *)idx_tmp_name); } void write_promisor_file(const char *promisor_name, struct ref **sought, int nr_sought) diff --git a/pack.h b/pack.h index 594d5176aa..d5814f3158 100644 --- a/pack.h +++ b/pack.h @@ -110,11 +110,14 @@ int encode_in_pack_object_header(unsigned char *hdr, int hdr_len, int read_pack_header(int fd, struct pack_header *); struct hashfile *create_tmp_packfile(char **pack_tmp_name); -void finish_tmp_packfile(const struct strbuf *basename, +void stage_tmp_packfiles(const struct strbuf *basename, const char *pack_tmp_name, struct pack_idx_entry **written_list, uint32_t nr_written, struct pack_idx_option *pack_idx_opts, - unsigned char sha1[]); + unsigned char hash[], + char **idx_tmp_name); +void rename_tmp_packfile_idx(const struct strbuf *tmp_basename, + unsigned char hash[], char **idx_tmp_name); #endif From patchwork Wed Sep 8 00:38:28 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12479625 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id F0197C433FE for ; Wed, 8 Sep 2021 00:38:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CFFF561139 for ; Wed, 8 Sep 2021 00:38:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239666AbhIHAjn (ORCPT ); Tue, 7 Sep 2021 20:39:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41382 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235760AbhIHAjl (ORCPT ); Tue, 7 Sep 2021 20:39:41 -0400 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D7BEFC061757 for ; Tue, 7 Sep 2021 17:38:34 -0700 (PDT) Received: by mail-wr1-x42b.google.com with SMTP id i28so609970wrb.2 for ; Tue, 07 Sep 2021 17:38:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=XZ4JXp0A1fVqKEUVHsqJwZj+jRLtKHWVH5iYiFBxZIc=; b=Sk8BHz6f6HytEbPDB2WQkGIACRkEN6OhbE7njPaeRmj/D4zsGW4uXJcrVlsvqkjP8y 7fuEEYG5iVabNcC7C2EERVa1wvVR1FzuX0KmdM2C/Yw4GtN2wJZNRWu1K+WaaOhhkddi QAxlhKU0+44U95r44qhGrxj23iFNy61C0l+lCaPcuUx0T3D2278zaylqMB1nQmn/ocY9 5BgD/mqNfIF0/zDbgn1WdpPszDMRunaWNZKlvULETetvuh8GBnQBsS/OY8b6/Z58F36k 8ipySc8G0iS71VpJ1bBOhMA6OJ8mVGQBE64L3LOxnKUoD+WK+asLrcEK51jG0Xwwanmw Q8ww== 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=XZ4JXp0A1fVqKEUVHsqJwZj+jRLtKHWVH5iYiFBxZIc=; b=WxyHTexLfaFc625l9oi0b9+uHTSitH34XQkRLHZH5jqsj2kGAuRbrvD7cDh/1onT3o 58qtS1M6SoXkm9u/CEX3HXuO9EhXgkKS3JKyCS/kuPQzKmQsd1t7ZLzmyQrYrink9KiI jKOXzNceNbe6NLD0FMOc3qvI0w+ZXHDqi/iphMSrljhrjlOWoNRnIBNEYWaPB83CgPUP xPeQEDwDi6IinktFsVR+HtTTF0ZrZPpcXUllA0OmLW7l7xuZPB1dJj3xDelNbz2gIDLp ijBC5IXHUIrQQ/0vogiP8nXBXPK6U0pcbkGfdDlReMF1+ggIVDP2VDVfxFXZyXtKI7WT +Z4w== X-Gm-Message-State: AOAM5302wfvHftXPdZsPngsGIOUq0PzYSyEJAcrB8mx8XRyYxbw+5K0j LQ5y65wdRvXX6GctnPLm/2Rq/RVOWhympA== X-Google-Smtp-Source: ABdhPJwwqzdafMZSGLbnog0E1KVsDu0iAh2Kyy0ZBfOdm8vaoNCC4LL7Gk06OU0tQlunORPAQW70Kg== X-Received: by 2002:adf:b745:: with SMTP id n5mr974480wre.338.1631061513270; Tue, 07 Sep 2021 17:38:33 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id f17sm548962wmf.4.2021.09.07.17.38.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Sep 2021 17:38:32 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Taylor Blau , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFz?= =?utf-8?b?b24=?= Subject: [PATCH v2 4/4] pack-write: rename *.idx file into place last (really!) Date: Wed, 8 Sep 2021 02:38:28 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.819.gea1b153a43c In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Follow-up a preceding commit (pack-write.c: rename `.idx` file into place last, 2021-08-16)[1] and rename the *.idx file in-place after we write the *.bitmap. The preceding commit fixed the issue of *.idx being written before *.rev files, but did not do so for *.idx files. See 7cc8f971085 (pack-objects: implement bitmap writing, 2013-12-21) for commentary at the time when *.bitmap was implemented about how those files are written out, nothing in that commit contradicts what's being done here. Note that the referenced earlier commit[1] is overly optimistic about "clos[ing the] race", i.e. yes we'll now write the files in the right order, but we might still race due to our sloppy use of fsync(). See the thread at [2] for a rabbit hole of various discussions about filesystem races in the face of doing and not doing fsync() (and if doing fsync(), not doing it properly). 1. https://lore.kernel.org/git/a6a4d2154e83d41c10986c5f455279ab249a910c.1630461918.git.me@ttaylorr.com/ 2. https://lore.kernel.org/git/8735qgkvv1.fsf@evledraar.gmail.com/ Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/pack-objects.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c index 33567aaa74..396240c25a 100644 --- a/builtin/pack-objects.c +++ b/builtin/pack-objects.c @@ -1249,7 +1249,6 @@ static void write_pack_file(void) stage_tmp_packfiles(&tmpname, pack_tmp_name, written_list, nr_written, &pack_idx_opts, hash, &idx_tmp_name); - rename_tmp_packfile_idx(&tmpname, hash, &idx_tmp_name); if (write_bitmap_index) { struct strbuf sb = STRBUF_INIT; @@ -1268,6 +1267,8 @@ static void write_pack_file(void) strbuf_release(&sb); } + rename_tmp_packfile_idx(&tmpname, hash, &idx_tmp_name); + free(idx_tmp_name); strbuf_release(&tmpname); free(pack_tmp_name);