From patchwork Tue Feb 1 14:53:03 2022 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: 12731776 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B19D9C433EF for ; Tue, 1 Feb 2022 14:53:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239491AbiBAOxU (ORCPT ); Tue, 1 Feb 2022 09:53:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58792 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239476AbiBAOxT (ORCPT ); Tue, 1 Feb 2022 09:53:19 -0500 Received: from mail-wm1-x336.google.com (mail-wm1-x336.google.com [IPv6:2a00:1450:4864:20::336]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DC94CC061714 for ; Tue, 1 Feb 2022 06:53:18 -0800 (PST) Received: by mail-wm1-x336.google.com with SMTP id r7so12921019wmq.5 for ; Tue, 01 Feb 2022 06:53:18 -0800 (PST) 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=7AcYF0+j1hmE5qBBW921HkVedgxOdja8BA5zu4q8/PU=; b=HiUcpf9YR58qMBl5fD17S0IRPJOCB7EYxc5iZd2lGz4UI1GQbigHAQPzVz1kiWoE9j v6wJ6R4qP/myqUPuQtOqonH43S33kgXhJw4WTj1peT2+A1xHjjCDnD+KI9IXi4xZh/WZ W7rykujvwugiJYRbUA5azILK9TwQMdJS3cATsyueB9gYNPkq/diwuwhN4gPgGyB5vIq+ kcqaDY9uoVreJ0zVYZTlMmxqlyKtjXHGozOboEWrYii6PwOS3IsNqbOmMQLwSCEqLj1U xag8DQHebviBYw29U1at+nbOXuzikC1tSaHUGFns8pMQdVjxW/4Cx/4Uu1Z4EK+qEoK4 OrzQ== 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=7AcYF0+j1hmE5qBBW921HkVedgxOdja8BA5zu4q8/PU=; b=q/HR3OUpMP4lC4XFaxdZiNBgLmPIU/livT9yrHEJpZ3OOFnm2m/02seWA2TFhlu6SG fFquWTqiLctlEs260++2DwXIXXgyho0MGsYq5eCRM58U8I6Nz6BBVN1o4y5Jye/lzRKC J4RCEFHZw86Mz+BDMwLKgpEC8kG+oCxzI5jx35cyJ9NMkONsUghDnXcoTIpDDmBOUhf5 1CJPF+i5QL/0QMjUVgnHRBTwNyZS/jIWBdnO7cY/cB1/PcZ+Ko5AIlqvo0vY9mdpn9nN hB31qlQIIafRAIF0GwYcIrFdc8R3D64Q9ZMATgcaiZF8kQbFOhWw/V0jXH7NV7C4EIhE cMnw== X-Gm-Message-State: AOAM531dgDWivPjD4ixNm5Gr42iTvsJ/kGEr+HQzIaHxwTzCwSpa/9O7 KkuMZkgIZ1RgdUMYyDrNbRNZlkEvdMgLgQ== X-Google-Smtp-Source: ABdhPJxgC5ofyh48G4KSHKJmKK+jYdA6A7z43cM0utF3nCrj50nroVFJt1YnxnIo32iMMExi9QZmLA== X-Received: by 2002:a1c:a754:: with SMTP id q81mr2163773wme.180.1643727197239; Tue, 01 Feb 2022 06:53:17 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id bg17sm2492054wmb.2.2022.02.01.06.53.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Feb 2022 06:53:16 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Han Xin , Jiang Xin , =?utf-8?q?Ren=C3=A9_Scharfe?= , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH 01/10] object-file.c: split up declaration of unrelated variables Date: Tue, 1 Feb 2022 15:53:03 +0100 Message-Id: X-Mailer: git-send-email 2.35.0.913.g12b4baa2536 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Split up the declaration of the "ret" and "re_allocated" variables. It's not our usual style to group variable declarations simply because they share a type, we'd only prefer to do so when the two are closely related (e.g. "int i, j"). This change makes a subsequent and meaningful change's diff smaller. Signed-off-by: Ævar Arnfjörð Bjarmason --- object-file.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/object-file.c b/object-file.c index 8be57f48de7..ecc77973292 100644 --- a/object-file.c +++ b/object-file.c @@ -2118,7 +2118,8 @@ static int index_mem(struct index_state *istate, enum object_type type, const char *path, unsigned flags) { - int ret, re_allocated = 0; + int ret; + int re_allocated = 0; int write_object = flags & HASH_WRITE_OBJECT; if (!type) From patchwork Tue Feb 1 14:53:04 2022 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: 12731777 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C8978C433FE for ; Tue, 1 Feb 2022 14:53:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239498AbiBAOxW (ORCPT ); Tue, 1 Feb 2022 09:53:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58798 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239489AbiBAOxU (ORCPT ); Tue, 1 Feb 2022 09:53:20 -0500 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0DF37C061714 for ; Tue, 1 Feb 2022 06:53:20 -0800 (PST) Received: by mail-wm1-x334.google.com with SMTP id o30-20020a05600c511e00b0034f4c3186f4so2170440wms.3 for ; Tue, 01 Feb 2022 06:53:19 -0800 (PST) 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=tBtmnYDl0fiOkPBBFn+NE6P3pDJUDzSq0rNX3Wr2X/4=; b=qF8eyisPao0w8zfOuQ5R1zVyPL4SAwXS+kwf5x3fO3P+aCTWfSeOU7Dcp6+muFkOa+ UaSgLJjTh2XvrogOdtSkJESwSGBJyzbWrYyXea0xLhJPGqMWCxPFKskgkSRMo26v1OVn 5RqLHGfNbtDHKlOW69fVO7GwI9Aq1jTnbAqQs0Lj0AuRzlNKN9QpKA4yJFNabDcV6z1O boNVKgKipesal9TMG+fRiGD9AWSp5qt+DXzYN28NNalDsI/E8fJNH1qACV5bv/h3zfqm 461PFgBthQmmSiAjwJ0vRHPaPMgfSAjMCjJduKD+NY5VO4nYAigp+LVe93Be12tX+Han 9msg== 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=tBtmnYDl0fiOkPBBFn+NE6P3pDJUDzSq0rNX3Wr2X/4=; b=FG8IHQbN4zf1fP14f8LICmFoQbjUPXMiLKXAKeGet0ZJWzVl6BSTOAr8EV9K5LiE1V wpSWtmHC0aEp4C/nmLHUhvUqCXStvGP9uGDdRg6hXKUyehQKFG1Inteu6XxhuoIxf/mA WcsCmyH5O0kdsjx7kRhoPT6fYoZnA5NS63vMHH0JGHUGv6x3QVZemYHehhszhRQaz7o7 PcZH22z0+GaquBbsj8B24KwqnSJftzEc8WSqtqO6DcBAUvL2gawktdJXSB8Le8obdtMo Jny0uTNFZp9gwxXl3essP5sIU1OdNcEZ8oL9eeF6MJewxUM5i3SVrFJbMSj7Kx5arttx NNpw== X-Gm-Message-State: AOAM5320o/ALj7stjsZtzZtdpsHDRijv/zv4DxT4EoCSBk22Y6oggdFk Y+637hg/SWC/wLv7hR2wKX+IpYsw3X7i6w== X-Google-Smtp-Source: ABdhPJx32elmkU8Q+vaRjCVYIaLpxYtnlUEWJ++6f3fykiP884pzeqm1x85IczVBYZQsAUUENjfFwA== X-Received: by 2002:a05:600c:250:: with SMTP id 16mr750585wmj.47.1643727198374; Tue, 01 Feb 2022 06:53:18 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id bg17sm2492054wmb.2.2022.02.01.06.53.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Feb 2022 06:53:17 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Han Xin , Jiang Xin , =?utf-8?q?Ren=C3=A9_Scharfe?= , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH 02/10] object-file API: return "void", not "int" from hash_object_file() Date: Tue, 1 Feb 2022 15:53:04 +0100 Message-Id: X-Mailer: git-send-email 2.35.0.913.g12b4baa2536 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org The hash_object_file() function added in abdc3fc8421 (Add hash_sha1_file(), 2006-10-14) did not have a meaningful return value, and it never has. One was seemingly added to avoid adding braces to the "ret = " assignments being modified here. Let's instead assign "0" to the "ret" variables at the beginning of the relevant functions, and have them return "void". Signed-off-by: Ævar Arnfjörð Bjarmason --- object-file.c | 16 ++++++++-------- object-store.h | 6 +++--- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/object-file.c b/object-file.c index ecc77973292..eeb6814780a 100644 --- a/object-file.c +++ b/object-file.c @@ -1836,14 +1836,13 @@ static int write_buffer(int fd, const void *buf, size_t len) return 0; } -int hash_object_file(const struct git_hash_algo *algo, const void *buf, +void hash_object_file(const struct git_hash_algo *algo, const void *buf, unsigned long len, const char *type, struct object_id *oid) { char hdr[MAX_HEADER_LEN]; int hdrlen = sizeof(hdr); write_object_file_prepare(algo, buf, len, type, oid, hdr, &hdrlen); - return 0; } /* Finalize a file on disk, and close it. */ @@ -2118,7 +2117,7 @@ static int index_mem(struct index_state *istate, enum object_type type, const char *path, unsigned flags) { - int ret; + int ret = 0; int re_allocated = 0; int write_object = flags & HASH_WRITE_OBJECT; @@ -2148,8 +2147,9 @@ static int index_mem(struct index_state *istate, if (write_object) ret = write_object_file(buf, size, type_name(type), oid); else - ret = hash_object_file(the_hash_algo, buf, size, - type_name(type), oid); + hash_object_file(the_hash_algo, buf, size, type_name(type), + oid); + if (re_allocated) free(buf); return ret; @@ -2161,7 +2161,7 @@ static int index_stream_convert_blob(struct index_state *istate, const char *path, unsigned flags) { - int ret; + int ret = 0; const int write_object = flags & HASH_WRITE_OBJECT; struct strbuf sbuf = STRBUF_INIT; @@ -2175,8 +2175,8 @@ static int index_stream_convert_blob(struct index_state *istate, ret = write_object_file(sbuf.buf, sbuf.len, type_name(OBJ_BLOB), oid); else - ret = hash_object_file(the_hash_algo, sbuf.buf, sbuf.len, - type_name(OBJ_BLOB), oid); + hash_object_file(the_hash_algo, sbuf.buf, sbuf.len, + type_name(OBJ_BLOB), oid); strbuf_release(&sbuf); return ret; } diff --git a/object-store.h b/object-store.h index 6f89482df03..44f6868cc9c 100644 --- a/object-store.h +++ b/object-store.h @@ -245,9 +245,9 @@ static inline void *repo_read_object_file(struct repository *r, /* Read and unpack an object file into memory, write memory to an object file */ int oid_object_info(struct repository *r, const struct object_id *, unsigned long *); -int hash_object_file(const struct git_hash_algo *algo, const void *buf, - unsigned long len, const char *type, - struct object_id *oid); +void hash_object_file(const struct git_hash_algo *algo, const void *buf, + unsigned long len, const char *type, + struct object_id *oid); int write_object_file_flags(const void *buf, unsigned long len, const char *type, struct object_id *oid, From patchwork Tue Feb 1 14:53:05 2022 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: 12731778 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D0D7EC433F5 for ; Tue, 1 Feb 2022 14:53:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239499AbiBAOxY (ORCPT ); Tue, 1 Feb 2022 09:53:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58818 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239493AbiBAOxV (ORCPT ); Tue, 1 Feb 2022 09:53:21 -0500 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7A487C061714 for ; Tue, 1 Feb 2022 06:53:21 -0800 (PST) Received: by mail-wr1-x42e.google.com with SMTP id v13so32471013wrv.10 for ; Tue, 01 Feb 2022 06:53:21 -0800 (PST) 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=sWC9kerMxGPXbmahXp00v8uj5UV8LZJBisCoqJwl6oU=; b=LQAFRUEpL01fKvB89JSQTfPNFTiTQhD3jvZm3rwtQhqZr2S4wJ+xOiNqd9wPMos4d5 AK4JWFmOVmx5jDRiAzL9wkfVWD3ik3nryPzJrZwJJ5mb9GD24vF1bFy4umWR7bBJmVc5 l1s0S66EmjZe4fxg6NfHpw/kcRxC59s7s6VGbnvawGzJpPyoulsM6KT5/kwPxjPDeV1x k0apQQCoeQ5lUCZJQcyUcvl5IBH6XAYcEzNRY0zJ+JXfeMmVzL6aGL0Q6jvL/8slSDXY evSxON/Tasw4shoY7yn3GJ76EWEpLw/l7DHSP3XHr5b3+Jl9Bc5MEYmsiM2HdmzkPO7s bkHg== 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=sWC9kerMxGPXbmahXp00v8uj5UV8LZJBisCoqJwl6oU=; b=foiw8jn/NewJqeCI/4xpKfXZ+AA812U8/TxOfogeMAWY7oRb8ME9d3INo3UVBmCcWr IcN/fG9gl+i00kx8TYF6p0R4RSfPIioOfjV644xjdFs/ocy5Prfnf2HQ3UiZ/fYhqtKg MLJfUIrJTDqDNOr+0EKtqzsJ7KBKzkdxNg2JcTOF3BFMNCrZL6MF6WNphb9WrRIQRWpO aPOVElnqzOtDLn18KAfXRrQbPf79oc8tv5okMiFIJlGgiuvcNCQx6r1epSLqR44HUTVA 0jW65+9h5pMnmEyPUaALcN0vDHf0/qYXThgIgf7PowwxgVFnR8yRvjPG4Q94RZmlSWt6 Hcew== X-Gm-Message-State: AOAM530poAY9zMU8IjqKxQn7iBKXLhqghy1s12WkM2Rkg8iXOYdcqqnu FApXYmABNZn0lWKcOrV5kPDvpyNaohv8dA== X-Google-Smtp-Source: ABdhPJyMwSXNtiR23vANgo9SZvo6Nt4faSFJa7YBUEn65owyy3J2DisPdUF4nsyuih1RwoJm/c8F2g== X-Received: by 2002:a5d:6daf:: with SMTP id u15mr21805683wrs.324.1643727199594; Tue, 01 Feb 2022 06:53:19 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id bg17sm2492054wmb.2.2022.02.01.06.53.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Feb 2022 06:53:18 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Han Xin , Jiang Xin , =?utf-8?q?Ren=C3=A9_Scharfe?= , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH 03/10] object-file API: add a format_object_header() function Date: Tue, 1 Feb 2022 15:53:05 +0100 Message-Id: X-Mailer: git-send-email 2.35.0.913.g12b4baa2536 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Add a convenience function to wrap the xsnprintf() command that generates loose object headers. This code was copy/pasted in various parts of the codebase, let's define it in one place and re-use it from there. All except one caller of it had a valid "enum object_type" for us, it's only write_object_file_prepare() which might need to deal with "git hash-object --literally" and a potential garbage type. Let's have the primary API use an "enum object_type", and define a *_literally() function that can take an arbitrary "const char *" for the type. See [1] for the discussion that prompted this patch, i.e. new code in object-file.c that wanted to copy/paste the xsnprintf() invocation. In the case of fast-import.c the callers unfortunately need to cast back & forth between "unsigned char *" and "char *", since format_object_header() ad encode_in_pack_object_header() take different signedness. 1. https://lore.kernel.org/git/211213.86bl1l9bfz.gmgdl@evledraar.gmail.com/ Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/fast-import.c | 6 +++--- builtin/index-pack.c | 3 +-- bulk-checkin.c | 4 ++-- http-push.c | 2 +- object-file.c | 23 ++++++++++++++++++++--- object-store.h | 8 ++++++++ 6 files changed, 35 insertions(+), 11 deletions(-) diff --git a/builtin/fast-import.c b/builtin/fast-import.c index 2b2e28bad79..123df7d9a53 100644 --- a/builtin/fast-import.c +++ b/builtin/fast-import.c @@ -937,8 +937,8 @@ static int store_object( git_hash_ctx c; git_zstream s; - hdrlen = xsnprintf((char *)hdr, sizeof(hdr), "%s %lu", - type_name(type), (unsigned long)dat->len) + 1; + hdrlen = format_object_header((char *)hdr, sizeof(hdr), type, + dat->len); the_hash_algo->init_fn(&c); the_hash_algo->update_fn(&c, hdr, hdrlen); the_hash_algo->update_fn(&c, dat->buf, dat->len); @@ -1091,7 +1091,7 @@ static void stream_blob(uintmax_t len, struct object_id *oidout, uintmax_t mark) hashfile_checkpoint(pack_file, &checkpoint); offset = checkpoint.offset; - hdrlen = xsnprintf((char *)out_buf, out_sz, "blob %" PRIuMAX, len) + 1; + hdrlen = format_object_header((char *)out_buf, out_sz, OBJ_BLOB, len); the_hash_algo->init_fn(&c); the_hash_algo->update_fn(&c, out_buf, hdrlen); diff --git a/builtin/index-pack.c b/builtin/index-pack.c index 3c2e6aee3cc..01574378ce2 100644 --- a/builtin/index-pack.c +++ b/builtin/index-pack.c @@ -449,8 +449,7 @@ static void *unpack_entry_data(off_t offset, unsigned long size, int hdrlen; if (!is_delta_type(type)) { - hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %"PRIuMAX, - type_name(type),(uintmax_t)size) + 1; + hdrlen = format_object_header(hdr, sizeof(hdr), type, size); the_hash_algo->init_fn(&c); the_hash_algo->update_fn(&c, hdr, hdrlen); } else diff --git a/bulk-checkin.c b/bulk-checkin.c index 8785b2ac806..85b3ebaf971 100644 --- a/bulk-checkin.c +++ b/bulk-checkin.c @@ -220,8 +220,8 @@ static int deflate_to_pack(struct bulk_checkin_state *state, if (seekback == (off_t) -1) return error("cannot find the current offset"); - header_len = xsnprintf((char *)obuf, sizeof(obuf), "%s %" PRIuMAX, - type_name(type), (uintmax_t)size) + 1; + header_len = format_object_header((char *)obuf, sizeof(obuf), + type, size); the_hash_algo->init_fn(&ctx); the_hash_algo->update_fn(&ctx, obuf, header_len); diff --git a/http-push.c b/http-push.c index 3309aaf004a..f0c044dcf76 100644 --- a/http-push.c +++ b/http-push.c @@ -363,7 +363,7 @@ static void start_put(struct transfer_request *request) git_zstream stream; unpacked = read_object_file(&request->obj->oid, &type, &len); - hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %"PRIuMAX , type_name(type), (uintmax_t)len) + 1; + hdrlen = format_object_header(hdr, sizeof(hdr), type, len); /* Set it up */ git_deflate_init(&stream, zlib_compression_level); diff --git a/object-file.c b/object-file.c index eeb6814780a..3fcd46cf9ed 100644 --- a/object-file.c +++ b/object-file.c @@ -1049,6 +1049,23 @@ void *xmmap(void *start, size_t length, return ret; } +static int format_object_header_literally(char *str, size_t size, + const char *type, size_t objsize) +{ + return xsnprintf(str, size, "%s %"PRIuMAX, type, (uintmax_t)objsize) + 1; +} + +int format_object_header(char *str, size_t size, enum object_type type, + size_t objsize) +{ + const char *name = type_name(type); + + if (!name) + BUG("could not get a type name for 'enum object_type' value %d", type); + + return format_object_header_literally(str, size, name, objsize); +} + /* * With an in-core object data in "map", rehash it to make sure the * object name actually matches "oid" to detect object corruption. @@ -1077,7 +1094,7 @@ int check_object_signature(struct repository *r, const struct object_id *oid, return -1; /* Generate the header */ - hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %"PRIuMAX , type_name(obj_type), (uintmax_t)size) + 1; + hdrlen = format_object_header(hdr, sizeof(hdr), obj_type, size); /* Sha1.. */ r->hash_algo->init_fn(&c); @@ -1777,7 +1794,7 @@ static void write_object_file_prepare(const struct git_hash_algo *algo, git_hash_ctx c; /* Generate the header */ - *hdrlen = xsnprintf(hdr, *hdrlen, "%s %"PRIuMAX , type, (uintmax_t)len)+1; + *hdrlen = format_object_header_literally(hdr, *hdrlen, type, len); /* Sha1.. */ algo->init_fn(&c); @@ -2051,7 +2068,7 @@ int force_object_loose(const struct object_id *oid, time_t mtime) buf = read_object(the_repository, oid, &type, &len); if (!buf) return error(_("cannot read object for %s"), oid_to_hex(oid)); - hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %"PRIuMAX , type_name(type), (uintmax_t)len) + 1; + hdrlen = format_object_header(hdr, sizeof(hdr), type, len); ret = write_loose_object(oid, hdr, hdrlen, buf, len, mtime, 0); free(buf); diff --git a/object-store.h b/object-store.h index 44f6868cc9c..8b4413d0ce7 100644 --- a/object-store.h +++ b/object-store.h @@ -331,6 +331,14 @@ int repo_has_object_file_with_flags(struct repository *r, */ int has_loose_object_nonlocal(const struct object_id *); +/** + * format_object_header() is a thin wrapper around s xsnprintf() that + * writes the initial " " part of the loose object + * header. It returns the size that snprintf() returns + 1. + */ +int format_object_header(char *str, size_t size, enum object_type type, + size_t objsize); + void assert_oid_type(const struct object_id *oid, enum object_type expect); /* From patchwork Tue Feb 1 14:53:06 2022 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: 12731779 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 34AA6C433FE for ; Tue, 1 Feb 2022 14:53:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239493AbiBAOxY (ORCPT ); Tue, 1 Feb 2022 09:53:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58816 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239501AbiBAOxX (ORCPT ); Tue, 1 Feb 2022 09:53:23 -0500 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D6AB8C061401 for ; Tue, 1 Feb 2022 06:53:22 -0800 (PST) Received: by mail-wr1-x432.google.com with SMTP id k18so32582511wrg.11 for ; Tue, 01 Feb 2022 06:53:22 -0800 (PST) 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=QaN4aE/od0GG2wJnwca8ghwjClRFiSZ1WME2631y1hs=; b=kMbk4D8oTjT9bgDYjLHDbtoeThbSLyvR/SDANsdT7tZGOYDmBCzy1nCmiBfpZxhogZ IeA+nZ/Dd0iicFwUFgt05wSWaBa7wL1o0ZVApeCaWZ/xwLwM+MmCG7WLEs1vLzfKORoZ 5l6PPRI8QSrfWqmOIeNqNNjDzN0Do86G2zGoXrwaKF2oohMsattYdKciVcARN9lRqrAr E9bDESRaS+oLCxgkA2iWY15W5XbbVGTPrWhCR5+4DMBbZpsRUwazvaVY3717HoK70/6e V+3Yx4EOCg6YkT71h2P8N1HT5qnlz5qcd1eS6JCQtbhb73yDpH17bSGW4lumrr1f/W4G ZZ5A== 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=QaN4aE/od0GG2wJnwca8ghwjClRFiSZ1WME2631y1hs=; b=0mSsbis6yUZzQTJHSzSlEmuQDVb1rDNUKug+YFt+JeVk+yn8Avkbdjh9p4iDydZyhp 29mGEbMKP6KbgKAhbC0vSDUfdiiPl9fgLx0CBj539ADz6d1yzlGlNROtQ8tQkrsV1Mz1 TInpUX5vXXrEpubaOwYHj1tp5GY7ztXyDdL97n1BJfGRlPE5uo9lxoPjUPaC39mlHXSA Pxnm0S6SQ5lIHds1+jDt58raQx825lgiqkeaSoJimGrQyXM2hlj0V3vwlIhLUTz0sXXG EO6nbyXvQmmNW6HcIDQbMvGUFn4mshk089EifoqFCJW0CLmARWV7KPb83KIP7yaEgCgg gvJA== X-Gm-Message-State: AOAM530UFNNu7qdAfqhkwqyafcn3uuJ40wyeFr9Tew23mCPMpYsXwvum 98TnBCsxzDE/vsktpVjMVsnjOt0N5eiufw== X-Google-Smtp-Source: ABdhPJxQ40W3ZqZU51DcTCrRH1wX7DCnTVR0GRXBQbQ3/Arh+dLiAuoNtJKw/YCS9gfl1tw2uy9UcA== X-Received: by 2002:adf:ef47:: with SMTP id c7mr21765503wrp.330.1643727201050; Tue, 01 Feb 2022 06:53:21 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id bg17sm2492054wmb.2.2022.02.01.06.53.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Feb 2022 06:53:19 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Han Xin , Jiang Xin , =?utf-8?q?Ren=C3=A9_Scharfe?= , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH 04/10] object-file API: have write_object_file() take "enum object_type" Date: Tue, 1 Feb 2022 15:53:06 +0100 Message-Id: X-Mailer: git-send-email 2.35.0.913.g12b4baa2536 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change the write_object_file() function to take an "enum object_type" instead of a "const char *type". Its callers either passed {commit,tree,blob,tag}_type and can pass the corresponding OBJ_* type instead, or were hardcoding strings like "blob". This avoids the back & forth fragility where the callers of write_object_file() would have the enum type, and convert it themselves via type_name(). We do have to now do that conversion ourselves before calling write_object_file_prepare(), but those codepaths will be similarly adjusted in subsequent commits. Signed-off-by: Ævar Arnfjörð Bjarmason --- apply.c | 8 ++++---- builtin/checkout.c | 2 +- builtin/mktag.c | 2 +- builtin/mktree.c | 2 +- builtin/notes.c | 3 +-- builtin/receive-pack.c | 2 +- builtin/replace.c | 2 +- builtin/tag.c | 2 +- builtin/unpack-objects.c | 6 +++--- cache-tree.c | 2 +- commit.c | 2 +- match-trees.c | 2 +- merge-ort.c | 4 ++-- merge-recursive.c | 2 +- notes-cache.c | 2 +- notes.c | 8 ++++---- object-file.c | 10 +++++----- object-store.h | 4 ++-- read-cache.c | 2 +- 19 files changed, 33 insertions(+), 34 deletions(-) diff --git a/apply.c b/apply.c index 7ffadc3b17a..e82a7c30f01 100644 --- a/apply.c +++ b/apply.c @@ -3589,7 +3589,7 @@ static int try_threeway(struct apply_state *state, /* Preimage the patch was prepared for */ if (patch->is_new) - write_object_file("", 0, blob_type, &pre_oid); + write_object_file("", 0, OBJ_BLOB, &pre_oid); else if (get_oid(patch->old_oid_prefix, &pre_oid) || read_blob_object(&buf, &pre_oid, patch->old_mode)) return error(_("repository lacks the necessary blob to perform 3-way merge.")); @@ -3605,7 +3605,7 @@ static int try_threeway(struct apply_state *state, return -1; } /* post_oid is theirs */ - write_object_file(tmp_image.buf, tmp_image.len, blob_type, &post_oid); + write_object_file(tmp_image.buf, tmp_image.len, OBJ_BLOB, &post_oid); clear_image(&tmp_image); /* our_oid is ours */ @@ -3618,7 +3618,7 @@ static int try_threeway(struct apply_state *state, return error(_("cannot read the current contents of '%s'"), patch->old_name); } - write_object_file(tmp_image.buf, tmp_image.len, blob_type, &our_oid); + write_object_file(tmp_image.buf, tmp_image.len, OBJ_BLOB, &our_oid); clear_image(&tmp_image); /* in-core three-way merge between post and our using pre as base */ @@ -4346,7 +4346,7 @@ static int add_index_file(struct apply_state *state, } fill_stat_cache_info(state->repo->index, ce, &st); } - if (write_object_file(buf, size, blob_type, &ce->oid) < 0) { + if (write_object_file(buf, size, OBJ_BLOB, &ce->oid) < 0) { discard_cache_entry(ce); return error(_("unable to create backing store " "for newly created file %s"), path); diff --git a/builtin/checkout.c b/builtin/checkout.c index cc804ba8e1e..24cc235939f 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -298,7 +298,7 @@ static int checkout_merged(int pos, const struct checkout *state, * (it also writes the merge result to the object database even * when it may contain conflicts). */ - if (write_object_file(result_buf.ptr, result_buf.size, blob_type, &oid)) + if (write_object_file(result_buf.ptr, result_buf.size, OBJ_BLOB, &oid)) die(_("Unable to add merge result for '%s'"), path); free(result_buf.ptr); ce = make_transient_cache_entry(mode, &oid, path, 2, ce_mem_pool); diff --git a/builtin/mktag.c b/builtin/mktag.c index 3b2dbbb37e6..96a3686af53 100644 --- a/builtin/mktag.c +++ b/builtin/mktag.c @@ -100,7 +100,7 @@ int cmd_mktag(int argc, const char **argv, const char *prefix) if (verify_object_in_tag(&tagged_oid, &tagged_type)) die(_("tag on stdin did not refer to a valid object")); - if (write_object_file(buf.buf, buf.len, tag_type, &result) < 0) + if (write_object_file(buf.buf, buf.len, OBJ_TAG, &result) < 0) die(_("unable to write tag file")); strbuf_release(&buf); diff --git a/builtin/mktree.c b/builtin/mktree.c index ae78ca1c029..a1213aaa961 100644 --- a/builtin/mktree.c +++ b/builtin/mktree.c @@ -58,7 +58,7 @@ static void write_tree(struct object_id *oid) strbuf_add(&buf, ent->oid.hash, the_hash_algo->rawsz); } - write_object_file(buf.buf, buf.len, tree_type, oid); + write_object_file(buf.buf, buf.len, OBJ_TREE, oid); strbuf_release(&buf); } diff --git a/builtin/notes.c b/builtin/notes.c index 05d60483e82..d81acaeaf78 100644 --- a/builtin/notes.c +++ b/builtin/notes.c @@ -199,9 +199,8 @@ static void prepare_note_data(const struct object_id *object, struct note_data * static void write_note_data(struct note_data *d, struct object_id *oid) { - if (write_object_file(d->buf.buf, d->buf.len, blob_type, oid)) { + if (write_object_file(d->buf.buf, d->buf.len, OBJ_BLOB, oid)) { int status = die_message(_("unable to write note object")); - if (d->edit_path) die_message(_("the note contents have been left in %s"), d->edit_path); diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c index 9f4a0b816cf..231d576927b 100644 --- a/builtin/receive-pack.c +++ b/builtin/receive-pack.c @@ -762,7 +762,7 @@ static void prepare_push_cert_sha1(struct child_process *proc) int bogs /* beginning_of_gpg_sig */; already_done = 1; - if (write_object_file(push_cert.buf, push_cert.len, "blob", + if (write_object_file(push_cert.buf, push_cert.len, OBJ_BLOB, &push_cert_oid)) oidclr(&push_cert_oid); diff --git a/builtin/replace.c b/builtin/replace.c index 6ff1734d587..995ca683302 100644 --- a/builtin/replace.c +++ b/builtin/replace.c @@ -474,7 +474,7 @@ static int create_graft(int argc, const char **argv, int force, int gentle) return -1; } - if (write_object_file(buf.buf, buf.len, commit_type, &new_oid)) { + if (write_object_file(buf.buf, buf.len, OBJ_COMMIT, &new_oid)) { strbuf_release(&buf); return error(_("could not write replacement commit for: '%s'"), old_ref); diff --git a/builtin/tag.c b/builtin/tag.c index 134b3f1edf0..31e6b155c04 100644 --- a/builtin/tag.c +++ b/builtin/tag.c @@ -238,7 +238,7 @@ static int build_tag_object(struct strbuf *buf, int sign, struct object_id *resu { if (sign && do_sign(buf) < 0) return error(_("unable to sign the tag")); - if (write_object_file(buf->buf, buf->len, tag_type, result) < 0) + if (write_object_file(buf->buf, buf->len, OBJ_TAG, result) < 0) return error(_("unable to write tag file")); return 0; } diff --git a/builtin/unpack-objects.c b/builtin/unpack-objects.c index 4a9466295ba..808041551fd 100644 --- a/builtin/unpack-objects.c +++ b/builtin/unpack-objects.c @@ -177,7 +177,7 @@ static void write_cached_object(struct object *obj, struct obj_buffer *obj_buf) struct object_id oid; if (write_object_file(obj_buf->buffer, obj_buf->size, - type_name(obj->type), &oid) < 0) + obj->type, &oid) < 0) die("failed to write object %s", oid_to_hex(&obj->oid)); obj->flags |= FLAG_WRITTEN; } @@ -243,7 +243,7 @@ static void write_object(unsigned nr, enum object_type type, void *buf, unsigned long size) { if (!strict) { - if (write_object_file(buf, size, type_name(type), + if (write_object_file(buf, size, type, &obj_list[nr].oid) < 0) die("failed to write object"); added_object(nr, type, buf, size); @@ -251,7 +251,7 @@ static void write_object(unsigned nr, enum object_type type, obj_list[nr].obj = NULL; } else if (type == OBJ_BLOB) { struct blob *blob; - if (write_object_file(buf, size, type_name(type), + if (write_object_file(buf, size, type, &obj_list[nr].oid) < 0) die("failed to write object"); added_object(nr, type, buf, size); diff --git a/cache-tree.c b/cache-tree.c index 65ca9933613..fdbbd191d22 100644 --- a/cache-tree.c +++ b/cache-tree.c @@ -440,7 +440,7 @@ static int update_one(struct cache_tree *it, } else if (dryrun) { hash_object_file(the_hash_algo, buffer.buf, buffer.len, tree_type, &it->oid); - } else if (write_object_file_flags(buffer.buf, buffer.len, tree_type, + } else if (write_object_file_flags(buffer.buf, buffer.len, OBJ_TREE, &it->oid, flags & WRITE_TREE_SILENT ? HASH_SILENT : 0)) { strbuf_release(&buffer); diff --git a/commit.c b/commit.c index a348f085b2b..6cc0ba9c11b 100644 --- a/commit.c +++ b/commit.c @@ -1567,7 +1567,7 @@ int commit_tree_extended(const char *msg, size_t msg_len, goto out; } - result = write_object_file(buffer.buf, buffer.len, commit_type, ret); + result = write_object_file(buffer.buf, buffer.len, OBJ_COMMIT, ret); out: strbuf_release(&buffer); return result; diff --git a/match-trees.c b/match-trees.c index df413989fa8..49398e599fe 100644 --- a/match-trees.c +++ b/match-trees.c @@ -235,7 +235,7 @@ static int splice_tree(const struct object_id *oid1, const char *prefix, rewrite_with = oid2; } hashcpy(rewrite_here, rewrite_with->hash); - status = write_object_file(buf, sz, tree_type, result); + status = write_object_file(buf, sz, OBJ_TREE, result); free(buf); return status; } diff --git a/merge-ort.c b/merge-ort.c index c3197970219..d4ba2955f11 100644 --- a/merge-ort.c +++ b/merge-ort.c @@ -1888,7 +1888,7 @@ static int handle_content_merge(struct merge_options *opt, if (!ret && write_object_file(result_buf.ptr, result_buf.size, - blob_type, &result->oid)) + OBJ_BLOB, &result->oid)) ret = err(opt, _("Unable to add %s to database"), path); @@ -3343,7 +3343,7 @@ static void write_tree(struct object_id *result_oid, } /* Write this object file out, and record in result_oid */ - write_object_file(buf.buf, buf.len, tree_type, result_oid); + write_object_file(buf.buf, buf.len, OBJ_TREE, result_oid); strbuf_release(&buf); } diff --git a/merge-recursive.c b/merge-recursive.c index d9457797dbb..e0cbbf2d022 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -1373,7 +1373,7 @@ static int merge_mode_and_contents(struct merge_options *opt, if (!ret && write_object_file(result_buf.ptr, result_buf.size, - blob_type, &result->blob.oid)) + OBJ_BLOB, &result->blob.oid)) ret = err(opt, _("Unable to add %s to database"), a->path); diff --git a/notes-cache.c b/notes-cache.c index 2473314d686..9dfd251a815 100644 --- a/notes-cache.c +++ b/notes-cache.c @@ -92,7 +92,7 @@ int notes_cache_put(struct notes_cache *c, struct object_id *key_oid, { struct object_id value_oid; - if (write_object_file(data, size, "blob", &value_oid) < 0) + if (write_object_file(data, size, OBJ_BLOB, &value_oid) < 0) return -1; return add_note(&c->tree, key_oid, &value_oid, NULL); } diff --git a/notes.c b/notes.c index f87dac40684..7452e71cc8d 100644 --- a/notes.c +++ b/notes.c @@ -675,7 +675,7 @@ static int tree_write_stack_finish_subtree(struct tree_write_stack *tws) ret = tree_write_stack_finish_subtree(n); if (ret) return ret; - ret = write_object_file(n->buf.buf, n->buf.len, tree_type, &s); + ret = write_object_file(n->buf.buf, n->buf.len, OBJ_TREE, &s); if (ret) return ret; strbuf_release(&n->buf); @@ -836,7 +836,7 @@ int combine_notes_concatenate(struct object_id *cur_oid, free(new_msg); /* create a new blob object from buf */ - ret = write_object_file(buf, buf_len, blob_type, cur_oid); + ret = write_object_file(buf, buf_len, OBJ_BLOB, cur_oid); free(buf); return ret; } @@ -916,7 +916,7 @@ int combine_notes_cat_sort_uniq(struct object_id *cur_oid, string_list_join_lines_helper, &buf)) goto out; - ret = write_object_file(buf.buf, buf.len, blob_type, cur_oid); + ret = write_object_file(buf.buf, buf.len, OBJ_BLOB, cur_oid); out: strbuf_release(&buf); @@ -1192,7 +1192,7 @@ int write_notes_tree(struct notes_tree *t, struct object_id *result) ret = for_each_note(t, flags, write_each_note, &cb_data) || write_each_non_note_until(NULL, &cb_data) || tree_write_stack_finish_subtree(&root) || - write_object_file(root.buf.buf, root.buf.len, tree_type, result); + write_object_file(root.buf.buf, root.buf.len, OBJ_TREE, result); strbuf_release(&root.buf); return ret; } diff --git a/object-file.c b/object-file.c index 3fcd46cf9ed..59eb793e0ac 100644 --- a/object-file.c +++ b/object-file.c @@ -2014,7 +2014,7 @@ static int freshen_packed_object(const struct object_id *oid) } int write_object_file_flags(const void *buf, unsigned long len, - const char *type, struct object_id *oid, + enum object_type type, struct object_id *oid, unsigned flags) { char hdr[MAX_HEADER_LEN]; @@ -2023,7 +2023,7 @@ int write_object_file_flags(const void *buf, unsigned long len, /* Normally if we have it in the pack then we do not bother writing * it out into .git/objects/??/?{38} file. */ - write_object_file_prepare(the_hash_algo, buf, len, type, oid, hdr, + write_object_file_prepare(the_hash_algo, buf, len, type_name(type), oid, hdr, &hdrlen); if (freshen_packed_object(oid) || freshen_loose_object(oid)) return 0; @@ -2162,7 +2162,7 @@ static int index_mem(struct index_state *istate, } if (write_object) - ret = write_object_file(buf, size, type_name(type), oid); + ret = write_object_file(buf, size, type, oid); else hash_object_file(the_hash_algo, buf, size, type_name(type), oid); @@ -2189,7 +2189,7 @@ static int index_stream_convert_blob(struct index_state *istate, get_conv_flags(flags)); if (write_object) - ret = write_object_file(sbuf.buf, sbuf.len, type_name(OBJ_BLOB), + ret = write_object_file(sbuf.buf, sbuf.len, OBJ_BLOB, oid); else hash_object_file(the_hash_algo, sbuf.buf, sbuf.len, @@ -2313,7 +2313,7 @@ int index_path(struct index_state *istate, struct object_id *oid, if (!(flags & HASH_WRITE_OBJECT)) hash_object_file(the_hash_algo, sb.buf, sb.len, blob_type, oid); - else if (write_object_file(sb.buf, sb.len, blob_type, oid)) + else if (write_object_file(sb.buf, sb.len, OBJ_BLOB, oid)) rc = error(_("%s: failed to insert into database"), path); strbuf_release(&sb); break; diff --git a/object-store.h b/object-store.h index 8b4413d0ce7..eab1e2a967e 100644 --- a/object-store.h +++ b/object-store.h @@ -250,10 +250,10 @@ void hash_object_file(const struct git_hash_algo *algo, const void *buf, struct object_id *oid); int write_object_file_flags(const void *buf, unsigned long len, - const char *type, struct object_id *oid, + enum object_type type, struct object_id *oid, unsigned flags); static inline int write_object_file(const void *buf, unsigned long len, - const char *type, struct object_id *oid) + enum object_type type, struct object_id *oid) { return write_object_file_flags(buf, len, type, oid, 0); } diff --git a/read-cache.c b/read-cache.c index cbe73f14e5e..b8ae30a0e71 100644 --- a/read-cache.c +++ b/read-cache.c @@ -735,7 +735,7 @@ static struct cache_entry *create_alias_ce(struct index_state *istate, void set_object_name_for_intent_to_add_entry(struct cache_entry *ce) { struct object_id oid; - if (write_object_file("", 0, blob_type, &oid)) + if (write_object_file("", 0, OBJ_BLOB, &oid)) die(_("cannot create an empty blob in the object database")); oidcpy(&ce->oid, &oid); } From patchwork Tue Feb 1 14:53:07 2022 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: 12731780 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B0903C433F5 for ; Tue, 1 Feb 2022 14:53:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239526AbiBAOx1 (ORCPT ); Tue, 1 Feb 2022 09:53:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58836 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239496AbiBAOxY (ORCPT ); Tue, 1 Feb 2022 09:53:24 -0500 Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D1C02C06173B for ; Tue, 1 Feb 2022 06:53:23 -0800 (PST) Received: by mail-wm1-x332.google.com with SMTP id o1-20020a1c4d01000000b0034d95625e1fso2158572wmh.4 for ; Tue, 01 Feb 2022 06:53:23 -0800 (PST) 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=51pd1F1xPIlq4828HwG+r4RwyzRiNsyMQG646qOg+aI=; b=JxH6SMBielAd0uc6hO5V/AriGHkCvLz1M9aaKx1z27lkh+bHZUUc9Ykev0CfBuojLC 0uIopkUnsawpqqS+nmLlsS0Fd1CBxA16OxF7nw748CYzKgKRH7SU1Yn/9hiWm/NQ8mDE E/TzT36JXvQarm5z/ZNd5Fh06pP7y9tLtJ8iZn4S7QwARK9VG6u89DwscbywVOcqJvlE wfgfSH/ONIBzXTzbVoSxXr6/omLvNHJdidVaR63s1e8bBC7pLWCWUXN6X+PwRDbx1qFD pXP8yJ7xDbpGHhfSMkwLLJ08BtP7XrXJUVqW3rUUg4hZ54oxxKPLoapOXMRQca8Ws5BV w8aA== 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=51pd1F1xPIlq4828HwG+r4RwyzRiNsyMQG646qOg+aI=; b=xT7tup6G4pl5KrkaKT70yCrXiTWQ+elWtoZXKSPqsoufiDoUJMDy1CcOjq0SbXAdbj vlv55JD37+mtvAZ+p8/qDWOenDCvgey1FWote7cYtSXdOdvAphfvHDeTWpz0PD6f7xkW Z05jznR3J/WYVWJ34IaFgGqSaWZyF4br3pkT2/aFX/I+BwDM2v3xpJ9vrpATz7fLNtV2 u7gUF4tQdWiP4UOUCMaau/n53hxmBFI9uZrFbiYy8ATR5wELsXW8axMFbVuE/rMD8eCr M6/LDPyWgzIc4Z80jdBnodjfuBM3ArAqxEIrAEzaD4U4Xy9b+rPrQDtItLuWUquKdfah uIUw== X-Gm-Message-State: AOAM531WI8pMBUd45zwle8zQBlBgnEgKRvTBZ3kWDy8uxVLtKqP3TBDn +Vn3kTubwWyzUICzjqq/dx11XMfsGdSCyw== X-Google-Smtp-Source: ABdhPJzW5yEumzsEWKr3e0LDiqbk4/JKRmYApoZGY8hlK9OgZjuUy+RJG/+8SfBympnlqpIptqRtQg== X-Received: by 2002:a05:600c:21d9:: with SMTP id x25mr2106209wmj.193.1643727202169; Tue, 01 Feb 2022 06:53:22 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id bg17sm2492054wmb.2.2022.02.01.06.53.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Feb 2022 06:53:21 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Han Xin , Jiang Xin , =?utf-8?q?Ren=C3=A9_Scharfe?= , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH 05/10] object-file API: provide a hash_object_file_oideq() Date: Tue, 1 Feb 2022 15:53:07 +0100 Message-Id: X-Mailer: git-send-email 2.35.0.913.g12b4baa2536 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Provide a new hash_object_file_oideq() for those callers of check_object_signature() that don't care about its streaming interface. I.e. at the start of that function we do: if (map) { hash_object_file(r->hash_algo, map, size, type, real_oid); return !oideq(oid, real_oid) ? -1 : 0; } These callers always provide a "map" (or "buf[fer]"). Let's have them call this simpler hash_object_file_oideq() function instead. None of them use a non-NULL "real_oid" argument, but let's provide it like check_object_signature() did. This'll make it easy to have these emit better error messages in the future as was done in 96e41f58fe1 (fsck: report invalid object type-path combinations, 2021-10-01), i.e. the die() calls here can emit not only the OID we expected, but also what we got. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/fast-export.c | 4 ++-- builtin/index-pack.c | 5 ++--- builtin/mktag.c | 5 ++--- object-file.c | 13 +++++++++++++ object-store.h | 14 ++++++++++++++ object.c | 4 ++-- 6 files changed, 35 insertions(+), 10 deletions(-) diff --git a/builtin/fast-export.c b/builtin/fast-export.c index 9f1c730e587..f084da198c7 100644 --- a/builtin/fast-export.c +++ b/builtin/fast-export.c @@ -299,8 +299,8 @@ static void export_blob(const struct object_id *oid) buf = read_object_file(oid, &type, &size); if (!buf) die("could not read blob %s", oid_to_hex(oid)); - if (check_object_signature(the_repository, oid, buf, size, - type_name(type), NULL) < 0) + if (!hash_object_file_oideq(the_repository->hash_algo, buf, + size, type, oid, NULL)) die("oid mismatch in blob %s", oid_to_hex(oid)); object = parse_object_buffer(the_repository, oid, type, size, buf, &eaten); diff --git a/builtin/index-pack.c b/builtin/index-pack.c index 01574378ce2..5531a6d8bae 100644 --- a/builtin/index-pack.c +++ b/builtin/index-pack.c @@ -1412,9 +1412,8 @@ static void fix_unresolved_deltas(struct hashfile *f) if (!data) continue; - if (check_object_signature(the_repository, &d->oid, - data, size, - type_name(type), NULL)) + if (!hash_object_file_oideq(the_repository->hash_algo, data, + size, type, &d->oid, NULL)) die(_("local object %s is corrupt"), oid_to_hex(&d->oid)); /* diff --git a/builtin/mktag.c b/builtin/mktag.c index 96a3686af53..d8c7cf836b7 100644 --- a/builtin/mktag.c +++ b/builtin/mktag.c @@ -61,9 +61,8 @@ static int verify_object_in_tag(struct object_id *tagged_oid, int *tagged_type) type_name(*tagged_type), type_name(type)); repl = lookup_replace_object(the_repository, tagged_oid); - ret = check_object_signature(the_repository, repl, - buffer, size, type_name(*tagged_type), - NULL); + ret = !hash_object_file_oideq(the_repository->hash_algo, buffer, size, + *tagged_type, repl, NULL); free(buffer); return ret; diff --git a/object-file.c b/object-file.c index 59eb793e0ac..27d10112960 100644 --- a/object-file.c +++ b/object-file.c @@ -1862,6 +1862,19 @@ void hash_object_file(const struct git_hash_algo *algo, const void *buf, write_object_file_prepare(algo, buf, len, type, oid, hdr, &hdrlen); } +int hash_object_file_oideq(const struct git_hash_algo *algo, const void *buf, + unsigned long len, enum object_type type, + const struct object_id *oid, + struct object_id *real_oidp) +{ + struct object_id tmp; + struct object_id *real_oid = real_oidp ? real_oidp : &tmp; + + hash_object_file(algo, buf, len, type_name(type), real_oid); + + return oideq(oid, real_oid); +} + /* Finalize a file on disk, and close it. */ static void close_loose_object(int fd) { diff --git a/object-store.h b/object-store.h index eab1e2a967e..95907062682 100644 --- a/object-store.h +++ b/object-store.h @@ -249,6 +249,20 @@ void hash_object_file(const struct git_hash_algo *algo, const void *buf, unsigned long len, const char *type, struct object_id *oid); +/** + * hash_object_file_oideq() is like hash_object_file() except that + * asserts that "real_oid" is equivalent to an input "oid", and the + * return value is that of oideq(oid, real_oid). + * + * The "real_oid" can be NULL, when non-NULL the caller provides a + * "struct object_id *" that can be used to print what the real OID + * was. + */ +int hash_object_file_oideq(const struct git_hash_algo *algo, const void *buf, + unsigned long len, enum object_type type, + const struct object_id *oid, + struct object_id *real_oidp); + int write_object_file_flags(const void *buf, unsigned long len, enum object_type type, struct object_id *oid, unsigned flags); diff --git a/object.c b/object.c index c37501fc120..d7673332582 100644 --- a/object.c +++ b/object.c @@ -289,8 +289,8 @@ struct object *parse_object(struct repository *r, const struct object_id *oid) buffer = repo_read_object_file(r, oid, &type, &size); if (buffer) { - if (check_object_signature(r, repl, buffer, size, - type_name(type), NULL) < 0) { + if (!hash_object_file_oideq(r->hash_algo, buffer, size, + type, repl, NULL)) { free(buffer); error(_("hash mismatch %s"), oid_to_hex(repl)); return NULL; From patchwork Tue Feb 1 14:53:08 2022 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: 12731781 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E872BC433F5 for ; Tue, 1 Feb 2022 14:53:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239522AbiBAOx3 (ORCPT ); Tue, 1 Feb 2022 09:53:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58844 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239501AbiBAOxZ (ORCPT ); Tue, 1 Feb 2022 09:53:25 -0500 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 CCE13C061714 for ; Tue, 1 Feb 2022 06:53:24 -0800 (PST) Received: by mail-wr1-x42c.google.com with SMTP id e8so32616670wrc.0 for ; Tue, 01 Feb 2022 06:53:24 -0800 (PST) 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=XOSYKHe2bixbR2/YPhWafApg2uOKVw0OfCGyhgqjn5o=; b=Ba918+ZlF4mhw30SCmHDeQV/GdqniXx85EghkLno9VYPjkLF/VvCVRq3bNvn5WxtVV kDpRBP9NTCIyuACpPN1bqg4X7/zHajG1li7r0+qpPovaqZQmqZvqAGdp9P9BiQGIv7fr I+KwWbQ1mkjm34Q48RLBTlYl6IbmCPqQn4tmq3FD3YP649XOzvkhNmXUhN1kEvAGnULN J+p6wZr12sA9hjHvjtCAfqGcy/y9e9eByRvlWFjY4j23IqJoEfk53TbizXY/EY+S6PqN ozouIOqZwAWf3xvC8NNdgLIOre1J909ZhFdI6IvvdtE/OuoWzKSTEutjoUh80S38Lq1u T6Sg== 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=XOSYKHe2bixbR2/YPhWafApg2uOKVw0OfCGyhgqjn5o=; b=k5XsiCc/4y5/HSK2Cg2XwcsycMz3gOkjPcW/XiO8J8z7T3t+JH2CnO8JZS+Djv3iCy I172NJmaWHJd0PB5UWfA35MlBmiX967H9cm1uMZXWB7ysjvFaljaMie2vPPPPwPZTiS9 3qFDarLncYGzZp3DZGhlSPD8VQRQBHIMf3j2Y1Sz7iErxTnysc0NrTA3nfeRzShlBLUG e5OI1p0nsTN8fPfv/D5J5s9CF/O99f8xdKVvPpM16c38M5yuMMvA3fHhe6XhFr8gClZ8 wJeLNtKnWoAgoPK8rQgNvm2HHM+w6xITKCcArrnBEcQKG9Ug6xQu7aLPGm0Mkdpe1fS8 JwHQ== X-Gm-Message-State: AOAM532uC8qCUghwVI2CgsOcjT03TiII4zYV418kFPkEHDBzM7AS2+E8 1mVb8p5lseQzbUDlIAB6vm3SIMb438s8lQ== X-Google-Smtp-Source: ABdhPJwsnbH09kyfAOfnrq/v8iXq02p9KmgcwH+pkpOGuvzQcojiLA84as34RWEdiD8IoZL8Z1YD/w== X-Received: by 2002:adf:d1c6:: with SMTP id b6mr9527093wrd.669.1643727203202; Tue, 01 Feb 2022 06:53:23 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id bg17sm2492054wmb.2.2022.02.01.06.53.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Feb 2022 06:53:22 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Han Xin , Jiang Xin , =?utf-8?q?Ren=C3=A9_Scharfe?= , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH 06/10] object-file API: replace some use of check_object_signature() Date: Tue, 1 Feb 2022 15:53:08 +0100 Message-Id: X-Mailer: git-send-email 2.35.0.913.g12b4baa2536 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Add a "hash_object_file_literally()" function to go with the existing "hash_object_file()" function. This is currently a wrapper for its sibling, but this change will allow us to change it to take an "enum object_type" in a subsequent commit. The only caller that wanted to pass a custom type to "check_object_signature()" was the "git fsck" via its "read_loose_object()", which is being changed here. There was an existing hash_object_file_literally() which I'm renaming to "hash_write_object_file_literally()", that function is only used for "hash-object --literally". That renaming is being done because it would be confusing to have a "hash_object_file_literally()" and a "hash_object_file()" that do very different things. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/hash-object.c | 4 ++-- object-file.c | 18 +++++++++++++----- object-store.h | 6 +++--- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/builtin/hash-object.c b/builtin/hash-object.c index c7b3ad74c60..624e9e677fb 100644 --- a/builtin/hash-object.c +++ b/builtin/hash-object.c @@ -25,8 +25,8 @@ static int hash_literally(struct object_id *oid, int fd, const char *type, unsig if (strbuf_read(&buf, fd, 4096) < 0) ret = -1; else - ret = hash_object_file_literally(buf.buf, buf.len, type, oid, - flags); + ret = hash_write_object_file_literally(buf.buf, buf.len, type, oid, + flags); strbuf_release(&buf); return ret; } diff --git a/object-file.c b/object-file.c index 27d10112960..9fc959fa05d 100644 --- a/object-file.c +++ b/object-file.c @@ -1862,6 +1862,13 @@ void hash_object_file(const struct git_hash_algo *algo, const void *buf, write_object_file_prepare(algo, buf, len, type, oid, hdr, &hdrlen); } +static void hash_object_file_literally(const struct git_hash_algo *algo, const void *buf, + unsigned long len, const char *type, + struct object_id *oid) +{ + hash_object_file(algo, buf, len, type, oid); +} + int hash_object_file_oideq(const struct git_hash_algo *algo, const void *buf, unsigned long len, enum object_type type, const struct object_id *oid, @@ -2043,9 +2050,9 @@ int write_object_file_flags(const void *buf, unsigned long len, return write_loose_object(oid, hdr, hdrlen, buf, len, 0, flags); } -int hash_object_file_literally(const void *buf, unsigned long len, - const char *type, struct object_id *oid, - unsigned flags) +int hash_write_object_file_literally(const void *buf, unsigned long len, + const char *type, struct object_id *oid, + unsigned flags) { char *header; int hdrlen, status = 0; @@ -2630,9 +2637,10 @@ int read_loose_object(const char *path, git_inflate_end(&stream); goto out; } - if (check_object_signature(the_repository, expected_oid, + hash_object_file_literally(the_repository->hash_algo, *contents, *size, - oi->type_name->buf, real_oid)) + oi->type_name->buf, real_oid); + if (!oideq(expected_oid, real_oid)) goto out; } diff --git a/object-store.h b/object-store.h index 95907062682..2ddc20b3304 100644 --- a/object-store.h +++ b/object-store.h @@ -272,9 +272,9 @@ static inline int write_object_file(const void *buf, unsigned long len, return write_object_file_flags(buf, len, type, oid, 0); } -int hash_object_file_literally(const void *buf, unsigned long len, - const char *type, struct object_id *oid, - unsigned flags); +int hash_write_object_file_literally(const void *buf, unsigned long len, + const char *type, struct object_id *oid, + unsigned flags); /* * Add an object file to the in-memory object store, without writing it From patchwork Tue Feb 1 14:53:09 2022 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: 12731783 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 11EF3C433FE for ; Tue, 1 Feb 2022 14:53:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239580AbiBAOxh (ORCPT ); Tue, 1 Feb 2022 09:53:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58876 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239532AbiBAOx2 (ORCPT ); Tue, 1 Feb 2022 09:53:28 -0500 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 45805C06174E for ; Tue, 1 Feb 2022 06:53:27 -0800 (PST) Received: by mail-wr1-x432.google.com with SMTP id m14so32322407wrg.12 for ; Tue, 01 Feb 2022 06:53:27 -0800 (PST) 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=Wy0DXbRsLaSHcdG7SuyTzumH2wAkf6mki3FuPUGPjFE=; b=epNncQC1Rbc3zti+W0XoFcl2I82W/ejtn42E5IF6yZr2cgbx33TWqOhGpIyEEN9mo4 AKNDFuvbMAgb0Gc6Sl8AM688H5uSP20rIqIJ65/pYH+wAVoVa7ptRwKv6UJ/4YO1Rpcb Gq3Z0iiL+ccATe+E5rOa3YHfGoJDhHJiNiyEOwAqh2f9qSSCRWOY0Fy/CfU6XByQ2LEU 33BZRoBaUP4WILinMAiOLIISgywPsRnQkmGTy3b65I9Mub8Gf9RXzZnmZFPxNlwG24Wl K3yWsat1q7hE6HPQ1IdmFDbU0+idh9fkN9Y8oTdT44tV0R8yn47Gj9r/8E0nxVp1fzIN 0gcA== 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=Wy0DXbRsLaSHcdG7SuyTzumH2wAkf6mki3FuPUGPjFE=; b=Y2XTVUHMavpFy0x+0UuyGvYbvfrOtZXrPBNQEvf3F6Pd5GciqFNjXdjZKCmzxAYc+8 TyXtZXduBX8otNQXumKZ+6vWIBuGOmGJun6lmIlb6HQmP2gxuXBuRNZsPGvvvQMkQXYM BbdkmvhNU0XMXSxBZVRQqxVTUerMSXuiIRnQHupUdcFWzIwhziDcXVU5ABBghOYF3QCW fDqh2lTH6VLj1ILINl/qAosNtmJx0d2FP84I7dF1J9YoQILaxac1YbB6eTgyLWYdtFBi VUJdTjIs8WuBjNFU/DY59C6MUStiekxOQ5B13r8Izxa7X+2FBV/+LUExANEwmvduiUdu 1GcQ== X-Gm-Message-State: AOAM531qAgZ+RRhFM+VaSKCyODhqO/JHshIF1Eh4VWG+PjQRxiClVA9Z NK5FryHV3p5rAggaDRKU6hsWEKx0uOrT1g== X-Google-Smtp-Source: ABdhPJxHHyiHUKvE87sSu5UPcHuXglsGxdTolsoM+qn6Da4kyIwMZtpM68ngRSSzKNVdfj5hpCdo8w== X-Received: by 2002:adf:f50e:: with SMTP id q14mr21874215wro.647.1643727205356; Tue, 01 Feb 2022 06:53:25 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id bg17sm2492054wmb.2.2022.02.01.06.53.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Feb 2022 06:53:23 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Han Xin , Jiang Xin , =?utf-8?q?Ren=C3=A9_Scharfe?= , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH 07/10] object-file API: have hash_object_file() take "enum object_type" Date: Tue, 1 Feb 2022 15:53:09 +0100 Message-Id: X-Mailer: git-send-email 2.35.0.913.g12b4baa2536 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change the hash_object_file() function to take an "enum object_type". Almost all of its callers were passing either "{commit,tree,blob,tag}_type", or the result of a call to type_name(). Let's instead benefit from type checking and use the enum directly. "Almost" because one caller here will now pass OBJ_BAD. The previous "NULL" value was not used, as "check_object_signature()" will never use its "type" argument when given a NULL "map" argument, which that caller in "parse_object()" also does. We'll deal with it in a subsequent commit, but for now it'll pass an "OBJ_BAD" placeholder. Signed-off-by: Ævar Arnfjörð Bjarmason --- apply.c | 4 ++-- builtin/index-pack.c | 2 +- builtin/replace.c | 2 +- builtin/unpack-objects.c | 2 +- cache-tree.c | 6 +++--- cache.h | 2 +- convert.c | 2 +- diffcore-rename.c | 2 +- dir.c | 2 +- log-tree.c | 2 +- object-file.c | 26 ++++++++++++++------------ object-store.h | 3 ++- object.c | 2 +- pack-check.c | 2 +- 14 files changed, 31 insertions(+), 28 deletions(-) diff --git a/apply.c b/apply.c index e82a7c30f01..8513755e254 100644 --- a/apply.c +++ b/apply.c @@ -3157,7 +3157,7 @@ static int apply_binary(struct apply_state *state, * See if the old one matches what the patch * applies to. */ - hash_object_file(the_hash_algo, img->buf, img->len, blob_type, + hash_object_file(the_hash_algo, img->buf, img->len, OBJ_BLOB, &oid); if (strcmp(oid_to_hex(&oid), patch->old_oid_prefix)) return error(_("the patch applies to '%s' (%s), " @@ -3203,7 +3203,7 @@ static int apply_binary(struct apply_state *state, name); /* verify that the result matches */ - hash_object_file(the_hash_algo, img->buf, img->len, blob_type, + hash_object_file(the_hash_algo, img->buf, img->len, OBJ_BLOB, &oid); if (strcmp(oid_to_hex(&oid), patch->new_oid_prefix)) return error(_("binary patch to '%s' creates incorrect result (expecting %s, got %s)"), diff --git a/builtin/index-pack.c b/builtin/index-pack.c index 5531a6d8bae..4f2f4550e2e 100644 --- a/builtin/index-pack.c +++ b/builtin/index-pack.c @@ -970,7 +970,7 @@ static struct base_data *resolve_delta(struct object_entry *delta_obj, if (!result_data) bad_object(delta_obj->idx.offset, _("failed to apply delta")); hash_object_file(the_hash_algo, result_data, result_size, - type_name(delta_obj->real_type), &delta_obj->idx.oid); + delta_obj->real_type, &delta_obj->idx.oid); sha1_object(result_data, NULL, result_size, delta_obj->real_type, &delta_obj->idx.oid); diff --git a/builtin/replace.c b/builtin/replace.c index 995ca683302..53a3b246570 100644 --- a/builtin/replace.c +++ b/builtin/replace.c @@ -409,7 +409,7 @@ static int check_one_mergetag(struct commit *commit, int i; hash_object_file(the_hash_algo, extra->value, extra->len, - type_name(OBJ_TAG), &tag_oid); + OBJ_TAG, &tag_oid); tag = lookup_tag(the_repository, &tag_oid); if (!tag) return error(_("bad mergetag in commit '%s'"), ref); diff --git a/builtin/unpack-objects.c b/builtin/unpack-objects.c index 808041551fd..dbeb0680a58 100644 --- a/builtin/unpack-objects.c +++ b/builtin/unpack-objects.c @@ -266,7 +266,7 @@ static void write_object(unsigned nr, enum object_type type, } else { struct object *obj; int eaten; - hash_object_file(the_hash_algo, buf, size, type_name(type), + hash_object_file(the_hash_algo, buf, size, type, &obj_list[nr].oid); added_object(nr, type, buf, size); obj = parse_object_buffer(the_repository, &obj_list[nr].oid, diff --git a/cache-tree.c b/cache-tree.c index fdbbd191d22..6752f69d515 100644 --- a/cache-tree.c +++ b/cache-tree.c @@ -432,14 +432,14 @@ static int update_one(struct cache_tree *it, if (repair) { struct object_id oid; hash_object_file(the_hash_algo, buffer.buf, buffer.len, - tree_type, &oid); + OBJ_TREE, &oid); if (has_object_file_with_flags(&oid, OBJECT_INFO_SKIP_FETCH_OBJECT)) oidcpy(&it->oid, &oid); else to_invalidate = 1; } else if (dryrun) { hash_object_file(the_hash_algo, buffer.buf, buffer.len, - tree_type, &it->oid); + OBJ_TREE, &it->oid); } else if (write_object_file_flags(buffer.buf, buffer.len, OBJ_TREE, &it->oid, flags & WRITE_TREE_SILENT ? HASH_SILENT : 0)) { @@ -948,7 +948,7 @@ static int verify_one(struct repository *r, strbuf_addf(&tree_buf, "%o %.*s%c", mode, entlen, name, '\0'); strbuf_add(&tree_buf, oid->hash, r->hash_algo->rawsz); } - hash_object_file(r->hash_algo, tree_buf.buf, tree_buf.len, tree_type, + hash_object_file(r->hash_algo, tree_buf.buf, tree_buf.len, OBJ_TREE, &new_oid); if (!oideq(&new_oid, &it->oid)) BUG("cache-tree for path %.*s does not match. " diff --git a/cache.h b/cache.h index 281f00ab1b1..d9c68c66d53 100644 --- a/cache.h +++ b/cache.h @@ -1320,7 +1320,7 @@ struct object_info; int parse_loose_header(const char *hdr, struct object_info *oi); int check_object_signature(struct repository *r, const struct object_id *oid, - void *buf, unsigned long size, const char *type, + void *buf, unsigned long size, enum object_type type, struct object_id *real_oidp); int finalize_object_file(const char *tmpfile, const char *filename); diff --git a/convert.c b/convert.c index df7186bd813..d88072fe320 100644 --- a/convert.c +++ b/convert.c @@ -1159,7 +1159,7 @@ static int ident_to_worktree(const char *src, size_t len, /* are we "faking" in place editing ? */ if (src == buf->buf) to_free = strbuf_detach(buf, NULL); - hash_object_file(the_hash_algo, src, len, "blob", &oid); + hash_object_file(the_hash_algo, src, len, OBJ_BLOB, &oid); strbuf_grow(buf, len + cnt * (the_hash_algo->hexsz + 3)); for (;;) { diff --git a/diffcore-rename.c b/diffcore-rename.c index bebd4ed6a42..c0422d9e709 100644 --- a/diffcore-rename.c +++ b/diffcore-rename.c @@ -261,7 +261,7 @@ static unsigned int hash_filespec(struct repository *r, if (diff_populate_filespec(r, filespec, NULL)) return 0; hash_object_file(r->hash_algo, filespec->data, filespec->size, - "blob", &filespec->oid); + OBJ_BLOB, &filespec->oid); } return oidhash(&filespec->oid); } diff --git a/dir.c b/dir.c index d91295f2bcd..5674e8a6256 100644 --- a/dir.c +++ b/dir.c @@ -1113,7 +1113,7 @@ static int add_patterns(const char *fname, const char *base, int baselen, &istate->cache[pos]->oid); else hash_object_file(the_hash_algo, buf, size, - "blob", &oid_stat->oid); + OBJ_BLOB, &oid_stat->oid); fill_stat_data(&oid_stat->stat, &st); oid_stat->valid = 1; } diff --git a/log-tree.c b/log-tree.c index d3e7a40b648..27a5b0bd61a 100644 --- a/log-tree.c +++ b/log-tree.c @@ -561,7 +561,7 @@ static int show_one_mergetag(struct commit *commit, struct strbuf signature = STRBUF_INIT; hash_object_file(the_hash_algo, extra->value, extra->len, - type_name(OBJ_TAG), &oid); + OBJ_TAG, &oid); tag = lookup_tag(the_repository, &oid); if (!tag) return -1; /* error message already given */ diff --git a/object-file.c b/object-file.c index 9fc959fa05d..4a23a7aa79f 100644 --- a/object-file.c +++ b/object-file.c @@ -1073,7 +1073,7 @@ int format_object_header(char *str, size_t size, enum object_type type, * the streaming interface and rehash it to do the same. */ int check_object_signature(struct repository *r, const struct object_id *oid, - void *map, unsigned long size, const char *type, + void *map, unsigned long size, enum object_type type, struct object_id *real_oidp) { struct object_id tmp; @@ -1679,7 +1679,7 @@ int pretend_object_file(void *buf, unsigned long len, enum object_type type, { struct cached_object *co; - hash_object_file(the_hash_algo, buf, len, type_name(type), oid); + hash_object_file(the_hash_algo, buf, len, type, oid); if (has_object_file_with_flags(oid, OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT) || find_cached_object(oid)) return 0; @@ -1854,19 +1854,23 @@ static int write_buffer(int fd, const void *buf, size_t len) } void hash_object_file(const struct git_hash_algo *algo, const void *buf, - unsigned long len, const char *type, + unsigned long len, enum object_type type, struct object_id *oid) { char hdr[MAX_HEADER_LEN]; int hdrlen = sizeof(hdr); - write_object_file_prepare(algo, buf, len, type, oid, hdr, &hdrlen); + + write_object_file_prepare(algo, buf, len, type_name(type), oid, hdr, &hdrlen); } static void hash_object_file_literally(const struct git_hash_algo *algo, const void *buf, unsigned long len, const char *type, struct object_id *oid) { - hash_object_file(algo, buf, len, type, oid); + char hdr[MAX_HEADER_LEN]; + int hdrlen = sizeof(hdr); + + write_object_file_prepare(algo, buf, len, type, oid, hdr, &hdrlen); } int hash_object_file_oideq(const struct git_hash_algo *algo, const void *buf, @@ -1877,7 +1881,7 @@ int hash_object_file_oideq(const struct git_hash_algo *algo, const void *buf, struct object_id tmp; struct object_id *real_oid = real_oidp ? real_oidp : &tmp; - hash_object_file(algo, buf, len, type_name(type), real_oid); + hash_object_file(algo, buf, len, type, real_oid); return oideq(oid, real_oid); } @@ -2184,9 +2188,7 @@ static int index_mem(struct index_state *istate, if (write_object) ret = write_object_file(buf, size, type, oid); else - hash_object_file(the_hash_algo, buf, size, type_name(type), - oid); - + hash_object_file(the_hash_algo, buf, size, type, oid); if (re_allocated) free(buf); return ret; @@ -2212,8 +2214,8 @@ static int index_stream_convert_blob(struct index_state *istate, ret = write_object_file(sbuf.buf, sbuf.len, OBJ_BLOB, oid); else - hash_object_file(the_hash_algo, sbuf.buf, sbuf.len, - type_name(OBJ_BLOB), oid); + hash_object_file(the_hash_algo, sbuf.buf, sbuf.len, OBJ_BLOB, + oid); strbuf_release(&sbuf); return ret; } @@ -2332,7 +2334,7 @@ int index_path(struct index_state *istate, struct object_id *oid, return error_errno("readlink(\"%s\")", path); if (!(flags & HASH_WRITE_OBJECT)) hash_object_file(the_hash_algo, sb.buf, sb.len, - blob_type, oid); + OBJ_BLOB, oid); else if (write_object_file(sb.buf, sb.len, OBJ_BLOB, oid)) rc = error(_("%s: failed to insert into database"), path); strbuf_release(&sb); diff --git a/object-store.h b/object-store.h index 2ddc20b3304..719122f047f 100644 --- a/object-store.h +++ b/object-store.h @@ -245,8 +245,9 @@ static inline void *repo_read_object_file(struct repository *r, /* Read and unpack an object file into memory, write memory to an object file */ int oid_object_info(struct repository *r, const struct object_id *, unsigned long *); + void hash_object_file(const struct git_hash_algo *algo, const void *buf, - unsigned long len, const char *type, + unsigned long len, enum object_type type, struct object_id *oid); /** diff --git a/object.c b/object.c index d7673332582..795b1356a1b 100644 --- a/object.c +++ b/object.c @@ -279,7 +279,7 @@ struct object *parse_object(struct repository *r, const struct object_id *oid) if ((obj && obj->type == OBJ_BLOB && repo_has_object_file(r, oid)) || (!obj && repo_has_object_file(r, oid) && oid_object_info(r, oid, NULL) == OBJ_BLOB)) { - if (check_object_signature(r, repl, NULL, 0, NULL, NULL) < 0) { + if (check_object_signature(r, repl, NULL, 0, OBJ_BAD, NULL) < 0) { error(_("hash mismatch %s"), oid_to_hex(oid)); return NULL; } diff --git a/pack-check.c b/pack-check.c index 3f418e3a6af..faceae610d3 100644 --- a/pack-check.c +++ b/pack-check.c @@ -143,7 +143,7 @@ static int verify_packfile(struct repository *r, oid_to_hex(&oid), p->pack_name, (uintmax_t)entries[i].offset); else if (check_object_signature(r, &oid, data, size, - type_name(type), NULL)) + type, NULL)) err = error("packed %s from %s is corrupt", oid_to_hex(&oid), p->pack_name); else if (fn) { From patchwork Tue Feb 1 14:53:10 2022 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: 12731782 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id ED7C2C433F5 for ; Tue, 1 Feb 2022 14:53:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239561AbiBAOxd (ORCPT ); Tue, 1 Feb 2022 09:53:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58880 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239536AbiBAOx2 (ORCPT ); Tue, 1 Feb 2022 09:53:28 -0500 Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 26CFDC061751 for ; Tue, 1 Feb 2022 06:53:28 -0800 (PST) Received: by mail-wr1-x429.google.com with SMTP id l25so32447885wrb.13 for ; Tue, 01 Feb 2022 06:53:28 -0800 (PST) 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=l/Xo0wVgPKBzazvAr/XlilEXWF0mmWaCHFyW89trm9U=; b=cekJ3UTmuQgDh8XDegxxOcdwnXDxc8MzRg6uy5Vsh48kyUekapgIzeCA39awPo88DY +HpFOw6SLPh7Zf0N4y1wvfXB4Zeau3zRtecrzuizSvytyrx6W/1mHcevRnu8ZqIjutiN Zl0OmePi6/7E/l7irjOQQ1LLvCeKXHGUAsB+hbY7MW7QcNq6WoR/MiaHfQkKxeusnfXE fGexqAnvGPBJtXWV6/KOC6+YUmOnUbgbx2EcMPjyeRfbAZs1FWd9PQzh6XNgodwzcG5D nI8z0VbbysAl/3ErV42v0Q6rPo7k9GSMQiYV4FykqxAgV0OrL9KKaAxnN4ACehvaP9O8 ACtA== 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=l/Xo0wVgPKBzazvAr/XlilEXWF0mmWaCHFyW89trm9U=; b=bUIj2asiUqO7k9TjsFfpQCV3qfHo6tdJmlp9UhVIz173Ec3jX7jXnT+rTmXU+0BYjd fedE/3eUcvCQ0yJdGDyQlKUXCoT72v3PWHEBXy515IW6d5Yus39oDO0wVRnKI6d3Z45d VZYQDPIh9OSSEA2brxeyawag+0t5q+OnkhMOPvFw/+Q+ynHB3ovzk5HfulbiB8boxE2F 7giVNHwi1T5OqcQy4HMNU+LnDHURc24wyhrX8ckE1Sdx2XEhIfwpriarw1ts4IFadCev qijZtZAZAGKcxkX4+qc0tGLaGuWM+vWu/DYnYk589rTwpqf9QeFUFTS/Cnn1DV9d3vHw 0tMQ== X-Gm-Message-State: AOAM533hvKoG3Vf3Gn0ZGs7I/wixJ9QcFvdEixoeoc6/eLs3GM6m0usy ZVCf4qv1CrEpaoYavALr/f0S4DzZw9RHKg== X-Google-Smtp-Source: ABdhPJxFvsqmmJDC4ic8K1U+arTjPtdZrajRV3RSraJkYnpaqAqNGGkHPXvavhmehpsqNtQUOmRwgw== X-Received: by 2002:a05:6000:170b:: with SMTP id n11mr21994005wrc.163.1643727206458; Tue, 01 Feb 2022 06:53:26 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id bg17sm2492054wmb.2.2022.02.01.06.53.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Feb 2022 06:53:25 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Han Xin , Jiang Xin , =?utf-8?q?Ren=C3=A9_Scharfe?= , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH 08/10] object-file API: replace check_object_signature() with stream_* Date: Tue, 1 Feb 2022 15:53:10 +0100 Message-Id: X-Mailer: git-send-email 2.35.0.913.g12b4baa2536 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change the task of the the check_object_signature() function so that it no longer confusingly does two unrelated things. Now the "!map" branch of it is exposed as a renamed stream_object_signature(). In preceding commits we migrated all of the "if (map)" callers over to "hash_object_file_oideq()" or "hash_object_file_literally()" followed by "!oideq(...)". This makes the caller in parse_object() less confusing, as we'll now no longer need to pass NULL for "map" and "OBJ_BAD" for the unused "type" argument. The "verify_packfile()" caller is arguably a bit worse off than before, but as we're only using this function in a couple of places now a bit more verbosity there is acceptable. Signed-off-by: Ævar Arnfjörð Bjarmason --- cache.h | 11 ++++++++--- object-file.c | 17 +++-------------- object.c | 2 +- pack-check.c | 8 ++++++-- 4 files changed, 18 insertions(+), 20 deletions(-) diff --git a/cache.h b/cache.h index d9c68c66d53..881ae36fd47 100644 --- a/cache.h +++ b/cache.h @@ -1319,9 +1319,14 @@ enum unpack_loose_header_result unpack_loose_header(git_zstream *stream, struct object_info; int parse_loose_header(const char *hdr, struct object_info *oi); -int check_object_signature(struct repository *r, const struct object_id *oid, - void *buf, unsigned long size, enum object_type type, - struct object_id *real_oidp); + +/** + * stream_object_signature() tries to read the object named with "oid" + * using the streaming interface and rehash it to make sure the object + * name actually matches "oid" to detect object corruption. + */ +int stream_object_signature(struct repository *r, const struct object_id *oid, + struct object_id *real_oidp); int finalize_object_file(const char *tmpfile, const char *filename); diff --git a/object-file.c b/object-file.c index 4a23a7aa79f..27f587d1dcd 100644 --- a/object-file.c +++ b/object-file.c @@ -1066,29 +1066,18 @@ int format_object_header(char *str, size_t size, enum object_type type, return format_object_header_literally(str, size, name, objsize); } -/* - * With an in-core object data in "map", rehash it to make sure the - * object name actually matches "oid" to detect object corruption. - * With "map" == NULL, try reading the object named with "oid" using - * the streaming interface and rehash it to do the same. - */ -int check_object_signature(struct repository *r, const struct object_id *oid, - void *map, unsigned long size, enum object_type type, - struct object_id *real_oidp) +int stream_object_signature(struct repository *r, const struct object_id *oid, + struct object_id *real_oidp) { struct object_id tmp; struct object_id *real_oid = real_oidp ? real_oidp : &tmp; + unsigned long size; enum object_type obj_type; struct git_istream *st; git_hash_ctx c; char hdr[MAX_HEADER_LEN]; int hdrlen; - if (map) { - hash_object_file(r->hash_algo, map, size, type, real_oid); - return !oideq(oid, real_oid) ? -1 : 0; - } - st = open_istream(r, oid, &obj_type, &size, NULL); if (!st) return -1; diff --git a/object.c b/object.c index 795b1356a1b..bf51b2056e9 100644 --- a/object.c +++ b/object.c @@ -279,7 +279,7 @@ struct object *parse_object(struct repository *r, const struct object_id *oid) if ((obj && obj->type == OBJ_BLOB && repo_has_object_file(r, oid)) || (!obj && repo_has_object_file(r, oid) && oid_object_info(r, oid, NULL) == OBJ_BLOB)) { - if (check_object_signature(r, repl, NULL, 0, OBJ_BAD, NULL) < 0) { + if (stream_object_signature(r, repl, NULL) < 0) { error(_("hash mismatch %s"), oid_to_hex(oid)); return NULL; } diff --git a/pack-check.c b/pack-check.c index faceae610d3..5d5169507ec 100644 --- a/pack-check.c +++ b/pack-check.c @@ -142,8 +142,12 @@ static int verify_packfile(struct repository *r, err = error("cannot unpack %s from %s at offset %"PRIuMAX"", oid_to_hex(&oid), p->pack_name, (uintmax_t)entries[i].offset); - else if (check_object_signature(r, &oid, data, size, - type, NULL)) + else if (data && !hash_object_file_oideq(r->hash_algo, data, + size, type, &oid, + NULL)) + err = error("packed %s from %s is corrupt", + oid_to_hex(&oid), p->pack_name); + else if (!data && stream_object_signature(r, &oid, NULL)) err = error("packed %s from %s is corrupt", oid_to_hex(&oid), p->pack_name); else if (fn) { From patchwork Tue Feb 1 14:53:11 2022 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: 12731784 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D5C1CC433F5 for ; Tue, 1 Feb 2022 14:53:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239543AbiBAOxj (ORCPT ); Tue, 1 Feb 2022 09:53:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58882 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239527AbiBAOx3 (ORCPT ); Tue, 1 Feb 2022 09:53:29 -0500 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 26D4EC061757 for ; Tue, 1 Feb 2022 06:53:29 -0800 (PST) Received: by mail-wr1-x434.google.com with SMTP id u15so32583603wrt.3 for ; Tue, 01 Feb 2022 06:53:29 -0800 (PST) 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=HFXOdfGCqyBDNrZ6+h/fCwoYY60e2HMCpK3xuGxEg+8=; b=qaw/GkVeBJ9W9Y0DgVEDx5X0IOUy0+N2D4kG21zodOUbc7+HUSL2nX7S+ylquvBUbO de7F46hqH/yxiTo5wT4yChjSJki/MxpGctdCpztzwYCz2vHPh5D2E422u8MmG6JmsVsz 8AVSouY6fXRnLd5/40iPcWukTYVUaXf78kVi0E49O0G803aw/PVieTTOLTTNXBZVvy1P KWgt52tssT+QBBkpKwaPTOjNsCNY8kChYrSRXDgl5NbinDiZFdVsYuAOKqj9kF1FOBM9 6YDorW3ZsvH5/KwdLXzxSmLqmmcHaUpbRxU3+LjU0wO05IEF8lQv/wfEs1nGrpJZjfJ5 Iuuw== 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=HFXOdfGCqyBDNrZ6+h/fCwoYY60e2HMCpK3xuGxEg+8=; b=HKNSQkqKeUy/6tbGVtei0A7zQLAPmsrEuquVA1DYN1ukVKEQAUAtPnp2OtURAb54U1 5hw07cRwKat3zcQdNhOW2kDI7TCXfI1qenWfzdrGTq95IahGGl2TuAL+nogzWp0zcR2k GxufmWj4ZOv0Gdkh+LOjHWvAvW2Y7ta2NZxgMGeu+S5bMYdocePP0zLdxBYT/B6wImEl KeG2HdxgEW/3g4YohWqzkCQ+wn/6vGdts5+F9Zvks7ll9/jt3GjIZdNjDvvlGTo+z/C/ n0tJJjmQQuKOJAiQ6r0FXp2Fd2JWZ7q89mbQz4QzNOC2iCQz0ISLDsFdjF23ioilicIh wweg== X-Gm-Message-State: AOAM531htJKyJfKVEhamlBp1RqZvzVP1hoD3nVCMjjlUswoxGS68SavG nu1kZrgZkbzDnE/jzrwFpZ1K0ExxTEl2Pw== X-Google-Smtp-Source: ABdhPJzBEjWOiI8vj2fPsMQFhKOhQDBZhkxQEBALuX3sV4OecSQh87qADjVH46D9yozkQhhlEK/7SA== X-Received: by 2002:adf:ebc2:: with SMTP id v2mr22346899wrn.498.1643727207497; Tue, 01 Feb 2022 06:53:27 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id bg17sm2492054wmb.2.2022.02.01.06.53.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Feb 2022 06:53:26 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Han Xin , Jiang Xin , =?utf-8?q?Ren=C3=A9_Scharfe?= , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH 09/10] object-file.c: add a literal version of write_object_file_prepare() Date: Tue, 1 Feb 2022 15:53:11 +0100 Message-Id: X-Mailer: git-send-email 2.35.0.913.g12b4baa2536 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Split off a *_literally() variant of the write_object_file_prepare() function. To do this create a new "hash_object_body()" static helper. We now defer the type_name() call until the very last moment in format_object_header() for those callers that aren't "hash-object --literally". Signed-off-by: Ævar Arnfjörð Bjarmason --- object-file.c | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/object-file.c b/object-file.c index 27f587d1dcd..0a6d56db39f 100644 --- a/object-file.c +++ b/object-file.c @@ -1775,21 +1775,40 @@ void *read_object_with_reference(struct repository *r, } } +static void hash_object_body(const struct git_hash_algo *algo, git_hash_ctx *c, + const void *buf, unsigned long len, + struct object_id *oid, + char *hdr, int *hdrlen) +{ + algo->init_fn(c); + algo->update_fn(c, hdr, *hdrlen); + algo->update_fn(c, buf, len); + algo->final_oid_fn(oid, c); +} + static void write_object_file_prepare(const struct git_hash_algo *algo, const void *buf, unsigned long len, - const char *type, struct object_id *oid, + enum object_type type, struct object_id *oid, char *hdr, int *hdrlen) { git_hash_ctx c; /* Generate the header */ - *hdrlen = format_object_header_literally(hdr, *hdrlen, type, len); + *hdrlen = format_object_header(hdr, *hdrlen, type, len); /* Sha1.. */ - algo->init_fn(&c); - algo->update_fn(&c, hdr, *hdrlen); - algo->update_fn(&c, buf, len); - algo->final_oid_fn(oid, &c); + hash_object_body(algo, &c, buf, len, oid, hdr, hdrlen); +} + +static void write_object_file_prepare_literally(const struct git_hash_algo *algo, + const void *buf, unsigned long len, + const char *type, struct object_id *oid, + char *hdr, int *hdrlen) +{ + git_hash_ctx c; + + *hdrlen = format_object_header_literally(hdr, *hdrlen, type, len); + hash_object_body(algo, &c, buf, len, oid, hdr, hdrlen); } /* @@ -1849,7 +1868,7 @@ void hash_object_file(const struct git_hash_algo *algo, const void *buf, char hdr[MAX_HEADER_LEN]; int hdrlen = sizeof(hdr); - write_object_file_prepare(algo, buf, len, type_name(type), oid, hdr, &hdrlen); + write_object_file_prepare(algo, buf, len, type, oid, hdr, &hdrlen); } static void hash_object_file_literally(const struct git_hash_algo *algo, const void *buf, @@ -1859,7 +1878,7 @@ static void hash_object_file_literally(const struct git_hash_algo *algo, const v char hdr[MAX_HEADER_LEN]; int hdrlen = sizeof(hdr); - write_object_file_prepare(algo, buf, len, type, oid, hdr, &hdrlen); + write_object_file_prepare_literally(algo, buf, len, type, oid, hdr, &hdrlen); } int hash_object_file_oideq(const struct git_hash_algo *algo, const void *buf, @@ -2036,7 +2055,7 @@ int write_object_file_flags(const void *buf, unsigned long len, /* Normally if we have it in the pack then we do not bother writing * it out into .git/objects/??/?{38} file. */ - write_object_file_prepare(the_hash_algo, buf, len, type_name(type), oid, hdr, + write_object_file_prepare(the_hash_algo, buf, len, type, oid, hdr, &hdrlen); if (freshen_packed_object(oid) || freshen_loose_object(oid)) return 0; @@ -2053,8 +2072,8 @@ int hash_write_object_file_literally(const void *buf, unsigned long len, /* type string, SP, %lu of the length plus NUL must fit this */ hdrlen = strlen(type) + MAX_HEADER_LEN; header = xmalloc(hdrlen); - write_object_file_prepare(the_hash_algo, buf, len, type, oid, header, - &hdrlen); + write_object_file_prepare_literally(the_hash_algo, buf, len, type, + oid, header, &hdrlen); if (!(flags & HASH_WRITE_OBJECT)) goto cleanup; From patchwork Tue Feb 1 14:53:12 2022 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: 12731785 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BADBEC433F5 for ; Tue, 1 Feb 2022 14:53:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239531AbiBAOxk (ORCPT ); Tue, 1 Feb 2022 09:53:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58896 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239496AbiBAOxa (ORCPT ); Tue, 1 Feb 2022 09:53:30 -0500 Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1C2DEC061748 for ; Tue, 1 Feb 2022 06:53:30 -0800 (PST) Received: by mail-wm1-x32b.google.com with SMTP id c192so12913093wma.4 for ; Tue, 01 Feb 2022 06:53:30 -0800 (PST) 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=NAp7EfJr5bbDUe1qTWWr8w0y3gHJmRvaUHoDIvAXrGo=; b=X1BKrWP49guuywFQh8YzSHrZfhNI/VpPMcOPgNvnNdN3vEjLktNsd4Elw/oFzgQp2Q eRLwqrB5No5a6Dtkfs/eKQTtU1fLLA5XwRU6yhLuhf09rzXs4IviXvB8jQaDJGYyMePP nzkZpIXh4cE7XOpa0KrLtvKiU+AY8cOt02j9WF7sdEWi85ubmRI0YLp/OPGuuYFjshRI UfAvzo8LB9iRzM8kYtWoSzdeiDstiQNGkMhxc5KXDbbm7C5hzZ+R6efs4JQut5uK7fkA +7vDP22+0Ntr+ckU/eG7oL67z99qUn1IleiOUbqhfkzjchpNBamM1jKCbeqPYQ9gJl2B euYQ== 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=NAp7EfJr5bbDUe1qTWWr8w0y3gHJmRvaUHoDIvAXrGo=; b=kbfTsFgAzApRjtfMq0KHl39pUGSZJKToGQbgWXvV5K851TthXUKJGw59O50jnBJSMJ diXTAOQzPxpH69vRYkpHYEIKftg5n1gE/tU+q6SaWjt9rytq+A/vxzBJYXvGK94GnSAe Ei5D/XvjjSp3JAeeXACWpRpdxkviNXwnrvhQMJhLNQdF2ykpYgpyLU9Q/F3ux/A73cEk I4sCSqSBUS3zgrbXHG/SjevEVt1bE4GGtqTir2CeVHfIr6UJ/7NlonriTkDKEDowAZpI VtpIS2K9PHUy11D84458R/sslKOvLZ/izVLjFIH3kzmAhgkih50vBD6q2mgc/XNHUATI yc7Q== X-Gm-Message-State: AOAM533fFEqWeRNpWASNo7gJTcFzeFneSq+ajDC5+Q/5nT0ACC5yvED0 PibkBngDkyqMCnQ3+Fo4m85u01IKRTTMMg== X-Google-Smtp-Source: ABdhPJySEes/dLveC1VrQgquHvpLz5YnpcEN9xIjDZjmWr2wahsDUm6Q9Ga7QG9SzqMdXNxT7mzjJQ== X-Received: by 2002:a05:600c:4f53:: with SMTP id m19mr2082538wmq.45.1643727208424; Tue, 01 Feb 2022 06:53:28 -0800 (PST) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id bg17sm2492054wmb.2.2022.02.01.06.53.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Feb 2022 06:53:27 -0800 (PST) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Han Xin , Jiang Xin , =?utf-8?q?Ren=C3=A9_Scharfe?= , Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFy?= =?utf-8?b?bWFzb24=?= Subject: [PATCH 10/10] object-file API: pass an enum to read_object_with_reference() Date: Tue, 1 Feb 2022 15:53:12 +0100 Message-Id: X-Mailer: git-send-email 2.35.0.913.g12b4baa2536 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change the read_object_with_reference() function to take an "enum object_type". It was not prepared to handle an arbitrary "const char *type", as it was itself calling type_from_string(). Let's change the only caller that passes in user data to use type_from_string(), and convert the rest to use e.g. "OBJ_TREE" instead of "tree_type". The "cat-file" caller is not on the codepath that handles"--allow-unknown", so the type_from_string() there is safe. Its use of type_from_string() doesn't functionally differ from that of the pre-image. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/cat-file.c | 11 +++++++---- builtin/fast-import.c | 6 +++--- builtin/grep.c | 4 ++-- builtin/pack-objects.c | 2 +- cache.h | 2 +- object-file.c | 5 ++--- tree-walk.c | 6 +++--- 7 files changed, 19 insertions(+), 17 deletions(-) diff --git a/builtin/cat-file.c b/builtin/cat-file.c index d94050e6c18..3c5bc505e0a 100644 --- a/builtin/cat-file.c +++ b/builtin/cat-file.c @@ -154,7 +154,10 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name, break; case 0: - if (type_from_string(exp_type) == OBJ_BLOB) { + { + enum object_type exp_type_id = type_from_string(exp_type); + + if (exp_type_id == OBJ_BLOB) { struct object_id blob_oid; if (oid_object_info(the_repository, &oid, NULL) == OBJ_TAG) { char *buffer = read_object_file(&oid, &type, @@ -176,10 +179,10 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name, * fall-back to the usual case. */ } - buf = read_object_with_reference(the_repository, - &oid, exp_type, &size, NULL); + buf = read_object_with_reference(the_repository, &oid, + exp_type_id, &size, NULL); break; - + } default: die("git cat-file: unknown option: %s", exp_type); } diff --git a/builtin/fast-import.c b/builtin/fast-import.c index 123df7d9a53..c52e807f56e 100644 --- a/builtin/fast-import.c +++ b/builtin/fast-import.c @@ -2483,7 +2483,7 @@ static void note_change_n(const char *p, struct branch *b, unsigned char *old_fa unsigned long size; char *buf = read_object_with_reference(the_repository, &commit_oid, - commit_type, &size, + OBJ_COMMIT, &size, &commit_oid); if (!buf || size < the_hash_algo->hexsz + 6) die("Not a valid commit: %s", p); @@ -2555,7 +2555,7 @@ static void parse_from_existing(struct branch *b) char *buf; buf = read_object_with_reference(the_repository, - &b->oid, commit_type, &size, + &b->oid, OBJ_COMMIT, &size, &b->oid); parse_from_commit(b, buf, size); free(buf); @@ -2651,7 +2651,7 @@ static struct hash_list *parse_merge(unsigned int *count) unsigned long size; char *buf = read_object_with_reference(the_repository, &n->oid, - commit_type, + OBJ_COMMIT, &size, &n->oid); if (!buf || size < the_hash_algo->hexsz + 6) die("Not a valid commit: %s", from); diff --git a/builtin/grep.c b/builtin/grep.c index 9e34a820ad4..75e07b5623a 100644 --- a/builtin/grep.c +++ b/builtin/grep.c @@ -482,7 +482,7 @@ static int grep_submodule(struct grep_opt *opt, object_type = oid_object_info(subrepo, oid, NULL); obj_read_unlock(); data = read_object_with_reference(subrepo, - oid, tree_type, + oid, OBJ_TREE, &size, NULL); if (!data) die(_("unable to read tree (%s)"), oid_to_hex(oid)); @@ -651,7 +651,7 @@ static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec, int hit, len; data = read_object_with_reference(opt->repo, - &obj->oid, tree_type, + &obj->oid, OBJ_TREE, &size, NULL); if (!data) die(_("unable to read tree (%s)"), oid_to_hex(&obj->oid)); diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c index ba2006f2212..c4df3df3141 100644 --- a/builtin/pack-objects.c +++ b/builtin/pack-objects.c @@ -1802,7 +1802,7 @@ static void add_preferred_base(struct object_id *oid) return; data = read_object_with_reference(the_repository, oid, - tree_type, &size, &tree_oid); + OBJ_TREE, &size, &tree_oid); if (!data) return; diff --git a/cache.h b/cache.h index 881ae36fd47..92da61ef47a 100644 --- a/cache.h +++ b/cache.h @@ -1552,7 +1552,7 @@ int cache_name_stage_compare(const char *name1, int len1, int stage1, const char void *read_object_with_reference(struct repository *r, const struct object_id *oid, - const char *required_type, + enum object_type required_type, unsigned long *size, struct object_id *oid_ret); diff --git a/object-file.c b/object-file.c index 0a6d56db39f..6c6e21ccc92 100644 --- a/object-file.c +++ b/object-file.c @@ -1728,16 +1728,15 @@ void *read_object_file_extended(struct repository *r, void *read_object_with_reference(struct repository *r, const struct object_id *oid, - const char *required_type_name, + enum object_type required_type, unsigned long *size, struct object_id *actual_oid_return) { - enum object_type type, required_type; + enum object_type type; void *buffer; unsigned long isize; struct object_id actual_oid; - required_type = type_from_string(required_type_name); oidcpy(&actual_oid, oid); while (1) { int ref_length = -1; diff --git a/tree-walk.c b/tree-walk.c index 3a94959d64a..506234b4b81 100644 --- a/tree-walk.c +++ b/tree-walk.c @@ -89,7 +89,7 @@ void *fill_tree_descriptor(struct repository *r, void *buf = NULL; if (oid) { - buf = read_object_with_reference(r, oid, tree_type, &size, NULL); + buf = read_object_with_reference(r, oid, OBJ_TREE, &size, NULL); if (!buf) die("unable to read tree %s", oid_to_hex(oid)); } @@ -605,7 +605,7 @@ int get_tree_entry(struct repository *r, unsigned long size; struct object_id root; - tree = read_object_with_reference(r, tree_oid, tree_type, &size, &root); + tree = read_object_with_reference(r, tree_oid, OBJ_TREE, &size, &root); if (!tree) return -1; @@ -677,7 +677,7 @@ enum get_oid_result get_tree_entry_follow_symlinks(struct repository *r, unsigned long size; tree = read_object_with_reference(r, ¤t_tree_oid, - tree_type, &size, + OBJ_TREE, &size, &root); if (!tree) goto done;