From patchwork Wed Jul 7 19:07:44 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12363893 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 702DBC07E95 for ; Wed, 7 Jul 2021 19:07:57 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 54C7361CCC for ; Wed, 7 Jul 2021 19:07:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231225AbhGGTKh (ORCPT ); Wed, 7 Jul 2021 15:10:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47242 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230441AbhGGTKe (ORCPT ); Wed, 7 Jul 2021 15:10:34 -0400 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 63504C061574 for ; Wed, 7 Jul 2021 12:07:53 -0700 (PDT) Received: by mail-wr1-x42e.google.com with SMTP id a8so4295739wrp.5 for ; Wed, 07 Jul 2021 12:07:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:in-reply-to:references:from:date:subject:mime-version :content-transfer-encoding:fcc:to:cc; bh=28uzDAeGOKcnmFSDAZZdAYg3F6G6ewQ9Uyr464q53QI=; b=CpOgMXFrhB0ZUaffLLDVxsE/e03+25XWXPBqBfhfKrH5GqglaQ9ZOb6NotjKli/LXh r2oXFlsuOuKaNTuCIHLa5HF+ggzrsTRm9vidTyuY53HgMYpBUprCQxfeb4ljr1crq420 qUNCoHum91i13kcRi4/8cSwjOao1rInV8EfHy0DE9N3xno/3hcCCbZLmCw2ewSe/WqN1 0o5d9uVp/xE9wV0tzOvVJkn1xo9C2u80+CvTFAg3sHavqDyJ1tJIdsdDhcSrzXGURb1v ZpezadfpT7z8XH1LKg7w0oUOOuLynPgOIBwv9aWB614ji1CB+IYe0+hNGYeUKK0cHwZz H92Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:mime-version:content-transfer-encoding:fcc:to:cc; bh=28uzDAeGOKcnmFSDAZZdAYg3F6G6ewQ9Uyr464q53QI=; b=k89YR3UvzHZYyGNwh2ed3vW9pF5CK+nYo8TbfMlliVN4/zv8z+A1nHCGqHN/YyWWd8 bG/xUdNGRo/jpdMO0e+kW2aBiz3FMkmuv8qmVS1NH4bMrvaORNd/yQJec9Gp9PkeoUV6 AmeOGcpM8RCJFckEwLZxWo8vJq2oQ+rpxpCjcCWsbGRkbbbenpGx0rcMIBmgcKMIX2aB fV+reSVDxLYLTar9WZB+nkgIfITuYMY03jzuvieiUtiBrQtWR/WXr/CFWe6ZgDb+dDjy D2y68Vn23BKoojuoyjen3b/lnF2jRjIqnkgGjpKaka3FZ9eGsQTmZDiTRFbNhhGw0RUP ibDw== X-Gm-Message-State: AOAM532RVAdYVvK7sYNonZbpb52p7ufDxaU7g1kxU/Qbjs9pczCqu8cH gNB4etcjEIaUlMP/Ru8kdgSFZc/Wb2s= X-Google-Smtp-Source: ABdhPJxFoGjl+T3mL4cyV1Sdzv3oCEyOxI1Tb8FjExGnjWQk6o6LiF26mkjH1zR4ktHPAHHPY9M8bg== X-Received: by 2002:a5d:4086:: with SMTP id o6mr3006626wrp.379.1625684872019; Wed, 07 Jul 2021 12:07:52 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id t16sm21258619wrp.44.2021.07.07.12.07.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 12:07:51 -0700 (PDT) Message-Id: In-Reply-To: References: Date: Wed, 07 Jul 2021 19:07:44 +0000 Subject: [PATCH v5 1/6] refs: remove EINVAL errno output from specification of read_raw_ref_fn MIME-Version: 1.0 Fcc: Sent To: git@vger.kernel.org Cc: Han-Wen Nienhuys , Jonathan Tan , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Han-Wen Nienhuys , Han-Wen Nienhuys Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Han-Wen Nienhuys From: Han-Wen Nienhuys This commit does not change code; it documents the fact that an alternate ref backend does not need to return EINVAL from read_raw_ref_fn to function properly. This is correct, because refs_read_raw_ref is only called from; * resolve_ref_unsafe(), which does not care for the EINVAL errno result. * refs_verify_refname_available(), which does not inspect errno. * files-backend.c, where errno is overwritten on failure. * packed-backend.c (is_packed_transaction_needed), which calls it for the packed ref backend, which never emits EINVAL. A grep for EINVAL */*c reveals that no code checks errno against EINVAL after reading references. In addition, the refs.h file does not mention errno at all. A grep over resolve_ref_unsafe() turned up the following callers that inspect errno: * sequencer.c::print_commit_summary, which uses it for die_errno * lock_ref_oid_basic(), which only treats EISDIR and ENOTDIR specially. The files ref backend does use EINVAL. The files backend does not call into the generic API (refs_read_raw), but into the files-specific function (files_read_raw_ref), which we are not changing in this commit. As the errno sideband is unintuitive and error-prone, remove EINVAL value, as a step towards getting rid of the errno sideband altogether. Spotted by Ævar Arnfjörð Bjarmason . Signed-off-by: Han-Wen Nienhuys --- refs/refs-internal.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/refs/refs-internal.h b/refs/refs-internal.h index 467f4b3c936..f4445e32904 100644 --- a/refs/refs-internal.h +++ b/refs/refs-internal.h @@ -619,9 +619,9 @@ typedef int reflog_expire_fn(struct ref_store *ref_store, * * Return 0 on success. If the ref doesn't exist, set errno to ENOENT * and return -1. If the ref exists but is neither a symbolic ref nor - * an object ID, it is broken; set REF_ISBROKEN in type, set errno to - * EINVAL, and return -1. If there is another error reading the ref, - * set errno appropriately and return -1. + * an object ID, it is broken; set REF_ISBROKEN in type, and return -1 + * (errno should not be ENOENT) If there is another error reading the + * ref, set errno appropriately and return -1. * * Backend-specific flags might be set in type as well, regardless of * outcome. From patchwork Wed Jul 7 19:07:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12363895 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 13E69C07E9B for ; Wed, 7 Jul 2021 19:07:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EE63C61CCC for ; Wed, 7 Jul 2021 19:07:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231240AbhGGTKh (ORCPT ); Wed, 7 Jul 2021 15:10:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47246 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231193AbhGGTKf (ORCPT ); Wed, 7 Jul 2021 15:10:35 -0400 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 0EF7BC06175F for ; Wed, 7 Jul 2021 12:07:54 -0700 (PDT) Received: by mail-wr1-x434.google.com with SMTP id a13so4263160wrf.10 for ; Wed, 07 Jul 2021 12:07:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:in-reply-to:references:from:date:subject:mime-version :content-transfer-encoding:fcc:to:cc; bh=bvUW0G4+ZOHhTiiIC/e71UTDkt5bfv+FBm5/WfeqzH4=; b=WXIAoggDgc7yKlqZC2XzRzSWCH/Z6+bkMaOsan91NUy48hJXJMI2jZ9OxkWgBfym9G ezBaDCwjUTOW4UXZwIxc0b7zQycIKGy9u+zhjKvrFEseFZAG7bvC9lIDp+zy6IWTt5Vb 5nqQOmjV9VOa+2Kt4jROKnykIbJv5t8RavxNcCBXtlcnCWul9C6Vs9aUTvNGgCA9fVT4 fphoQtLi8DsjsS7sUgpQdpJftMbUI1g1nY6pybfygQz3M0LrnGTjPimVpNIAY6zoFpuJ Bc6O+pXGT8HeO9OMblMvR6PFtFVqtZmJ1tl5rzz1+8zuYZ4pqZ2oFGAaeV2/rEOycYF7 yw2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:mime-version:content-transfer-encoding:fcc:to:cc; bh=bvUW0G4+ZOHhTiiIC/e71UTDkt5bfv+FBm5/WfeqzH4=; b=EK5gnNP6EvBjPLTWiJ3hrOuhN+QEcWrSF0l3NWd1lqqxuPD0rMU2cDZ5Ibw1BYxkk9 utPaSoL3XCtPuSehg0eNckw9NmkcRwkP43+6yyz945M1CozBRTmkFMi23jOqkwAufG2c XJJV41EuZdGBpkht/J6yGt2RSYuJieCXmJp7ryHda/rG8UhJ1dP/4SzXR+GsTJrNVLs0 fprFiV4rGrF37OIhcopMAvN7dzgZFR/0S8fpB8zG1SakyRcGnL7qmLAuYmXN9hWoh5M+ A0XhnGxzQ7KhrwbV4xEk6yhLbPETnGgoRxRkcve8nVq3OtWID21XdHRZVbKakr5v9fUp sggQ== X-Gm-Message-State: AOAM531AXYiK7TevtyNT6tjk+mGw7CzfLQ3YKAkOaYZkRQMxb1VlQ47Y DMqgoXxl6n24fi8Mmuq7vHSt9JFRr4Q= X-Google-Smtp-Source: ABdhPJyqYjgbxEM2DSJ+D5T8K6MM3EuCkR9jzm0wcjjfNLGGWU/MtrqWsP6BDE5qRXlFcEoEVXMWVg== X-Received: by 2002:a5d:4d4b:: with SMTP id a11mr5631747wru.325.1625684872589; Wed, 07 Jul 2021 12:07:52 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id c9sm14394350wrx.22.2021.07.07.12.07.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 12:07:52 -0700 (PDT) Message-Id: <95025080c16f535599826ed4f013845d712b0e8d.1625684869.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 07 Jul 2021 19:07:45 +0000 Subject: [PATCH v5 2/6] refs/files-backend: stop setting errno from lock_ref_oid_basic MIME-Version: 1.0 Fcc: Sent To: git@vger.kernel.org Cc: Han-Wen Nienhuys , Jonathan Tan , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Han-Wen Nienhuys , Han-Wen Nienhuys Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Han-Wen Nienhuys From: Han-Wen Nienhuys refs/files-backend.c::lock_ref_oid_basic() tries to signal how it failed to its callers using errno. It is safe to stop setting errno here, because the callers of this file-scope static function are * files_copy_or_rename_ref() * files_create_symref() * files_reflog_expire() None of them looks at errno after seeing a negative return from lock_ref_oid_basic() to make any decision, and no caller of these three functions looks at errno after they signal a failure by returning a negative value. In particular, * files_copy_or_rename_ref() - here, calls are followed by error() (which performs I/O) or write_ref_to_lockfile() (which calls parse_object() which may perform I/O) * files_create_symref() - here, calls are followed by error() or create_symref_locked() (which performs I/O and does not inspect errno) * files_reflog_expire() - here, calls are followed by error() or refs_reflog_exists() (which calls a function in a vtable that is not documented to use and/or preserve errno) Signed-off-by: Han-Wen Nienhuys Reviewed-by: Ævar Arnfjörð Bjarmason --- refs/files-backend.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/refs/files-backend.c b/refs/files-backend.c index 677b7e4cdd2..83ddfb3b627 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -910,7 +910,6 @@ static int create_reflock(const char *path, void *cb) /* * Locks a ref returning the lock on success and NULL on failure. - * On failure errno is set to something meaningful. */ static struct ref_lock *lock_ref_oid_basic(struct files_ref_store *refs, const char *refname, @@ -922,7 +921,6 @@ static struct ref_lock *lock_ref_oid_basic(struct files_ref_store *refs, { struct strbuf ref_file = STRBUF_INIT; struct ref_lock *lock; - int last_errno = 0; int mustexist = (old_oid && !is_null_oid(old_oid)); int resolve_flags = RESOLVE_REF_NO_RECURSE; int resolved; @@ -949,7 +947,6 @@ static struct ref_lock *lock_ref_oid_basic(struct files_ref_store *refs, * to remain. */ if (remove_empty_directories(&ref_file)) { - last_errno = errno; if (!refs_verify_refname_available( &refs->base, refname, extras, skip, err)) @@ -962,10 +959,13 @@ static struct ref_lock *lock_ref_oid_basic(struct files_ref_store *refs, &lock->old_oid, type); } if (!resolved) { - last_errno = errno; + int last_errno = errno; if (last_errno != ENOTDIR || - !refs_verify_refname_available(&refs->base, refname, - extras, skip, err)) + /* in case of D/F conflict, try to generate a better error + * message. If that fails, fall back to strerror(ENOTDIR). + */ + !refs_verify_refname_available(&refs->base, refname, extras, + skip, err)) strbuf_addf(err, "unable to resolve reference '%s': %s", refname, strerror(last_errno)); @@ -981,20 +981,17 @@ static struct ref_lock *lock_ref_oid_basic(struct files_ref_store *refs, if (is_null_oid(&lock->old_oid) && refs_verify_refname_available(refs->packed_ref_store, refname, extras, skip, err)) { - last_errno = ENOTDIR; goto error_return; } lock->ref_name = xstrdup(refname); if (raceproof_create_file(ref_file.buf, create_reflock, &lock->lk)) { - last_errno = errno; unable_to_lock_message(ref_file.buf, errno, err); goto error_return; } if (verify_lock(&refs->base, lock, old_oid, mustexist, err)) { - last_errno = errno; goto error_return; } goto out; @@ -1005,7 +1002,6 @@ static struct ref_lock *lock_ref_oid_basic(struct files_ref_store *refs, out: strbuf_release(&ref_file); - errno = last_errno; return lock; } From patchwork Wed Jul 7 19:07:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12363897 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0A02CC07E9C for ; Wed, 7 Jul 2021 19:07:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E38CE61CBE for ; Wed, 7 Jul 2021 19:07:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231263AbhGGTKi (ORCPT ); Wed, 7 Jul 2021 15:10:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47252 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231205AbhGGTKg (ORCPT ); Wed, 7 Jul 2021 15:10:36 -0400 Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7C3F3C061760 for ; Wed, 7 Jul 2021 12:07:54 -0700 (PDT) Received: by mail-wm1-x32d.google.com with SMTP id t14-20020a05600c198eb029020c8aac53d4so14011121wmq.1 for ; Wed, 07 Jul 2021 12:07:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:in-reply-to:references:from:date:subject:mime-version :content-transfer-encoding:fcc:to:cc; bh=oAmNf/jz/yO9V3fVJrHPlTIESQeKmmSoIYZ7Z1Fy+UE=; b=WvcYvyiG0THYl24Z63oS52EAmFSQzV7rzJ77wBO6iqjMb+nqN7bu+xUP6hh2zoMZ0b TV6noXzYyCJzd6rNzwM8nyhuGypC+9Zqfaku412+3dy2mhhmJSeROtVjPr6slq2FTr5w n2ag/xfEGyKogplwxBDNtxEotHD7xlWxWfFICTQx1zCfHwTasXZtAEACGyETJnLOFwWV HriRGwLsW/54U2fHrMQAdtHdOVCHTRmV4M/RsMoQHBvhxsyPzM/rUA20/xUYc23NTvYO wJZQsThiU235F0lF8EwmkLH5LX/ZTaygVJiL3kRvu78Wwk6ZvBFV+Ib5j5LrVbYpCAIG R/Cw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:mime-version:content-transfer-encoding:fcc:to:cc; bh=oAmNf/jz/yO9V3fVJrHPlTIESQeKmmSoIYZ7Z1Fy+UE=; b=nu3pDzYgNiSdvqpn7rIS8SgIHOAU+wHe+WXAaEI1clFm3Z6mPxE4Rq75YOT6BoA4cL YzplapwH3NeTVBQnyF05KkB5j+lRJHyJb13d3dTc+4hPCmdvybPAxY0fmab6YgUGOC0f NTCsV59ZLPmPfDxfWushRlVMLcUOEM6uDVTDZXeGn6q17UG6hpIZ3BTqvHfCdxILyi0x 9UAZ/RGjlYTHwDhBZl8liiWbx7UGMEBRJkiWyrqBIdEbwv3J8AY1i+vP2od+M5VjwVAT pamJXVu8+fvkuc79V2NlQLied5wyBl1fWiWjIpBv0wZmJZNVnw6ID+qGIz/bbX3QW+tM b50g== X-Gm-Message-State: AOAM533BB5qG0DKw6WjXDSEfyuO7XRWuj9mnkPU/ygMkU0lfEuhJXn3h MdebgG8bsXJrofEDYLG/lS9Dxxq5Ki0= X-Google-Smtp-Source: ABdhPJwYn1VXcywSk+NaHXHPnH9XVmdsROpYn2Ju6zVL6nRC265MzfzM/FkPk3V6wDdJ06ByILGGIw== X-Received: by 2002:a05:600c:2052:: with SMTP id p18mr644914wmg.168.1625684873150; Wed, 07 Jul 2021 12:07:53 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id c8sm20678421wri.91.2021.07.07.12.07.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 12:07:52 -0700 (PDT) Message-Id: <7feedb9720127605b5e84aa1143c6ddc736597f3.1625684869.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 07 Jul 2021 19:07:46 +0000 Subject: [PATCH v5 3/6] refs: make errno output explicit for read_raw_ref_fn MIME-Version: 1.0 Fcc: Sent To: git@vger.kernel.org Cc: Han-Wen Nienhuys , Jonathan Tan , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Han-Wen Nienhuys , Han-Wen Nienhuys Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Han-Wen Nienhuys From: Han-Wen Nienhuys This makes it explicit how alternative ref backends should report errors in read_raw_ref_fn. read_raw_ref_fn needs to supply a credible errno for a number of cases. These are primarily: 1) The files backend calls read_raw_ref from lock_raw_ref, and uses the resulting error codes to create/remove directories as needed. 2) ENOENT should be translated in a zero OID, optionally with REF_ISBROKEN set, returning the last successfully resolved symref. This is necessary so read_raw_ref("HEAD") on an empty repo returns refs/heads/main (or the default branch du-jour), and we know on which branch to create the first commit. Make this information flow explicit by adding a failure_errno to the signature of read_raw_ref. All errnos from the files backend are still propagated unchanged, even though inspection suggests only ENOTDIR, EISDIR and ENOENT are relevant. Signed-off-by: Han-Wen Nienhuys Reviewed-by: Ævar Arnfjörð Bjarmason --- refs.c | 10 ++++++++-- refs/debug.c | 4 ++-- refs/files-backend.c | 23 +++++++++++------------ refs/packed-backend.c | 8 ++++---- refs/refs-internal.h | 20 ++++++++++++-------- 5 files changed, 37 insertions(+), 28 deletions(-) diff --git a/refs.c b/refs.c index 8c9490235ea..5e5e3af8da0 100644 --- a/refs.c +++ b/refs.c @@ -1675,13 +1675,19 @@ int refs_read_raw_ref(struct ref_store *ref_store, const char *refname, struct object_id *oid, struct strbuf *referent, unsigned int *type) { + int result; + int failure_errno; if (!strcmp(refname, "FETCH_HEAD") || !strcmp(refname, "MERGE_HEAD")) { return refs_read_special_head(ref_store, refname, oid, referent, type); } - return ref_store->be->read_raw_ref(ref_store, refname, oid, referent, - type); + failure_errno = 0; + result = ref_store->be->read_raw_ref(ref_store, refname, oid, referent, + type, &failure_errno); + if (failure_errno) + errno = failure_errno; + return result; } /* This function needs to return a meaningful errno on failure */ diff --git a/refs/debug.c b/refs/debug.c index 7db4abccc34..f12413a9bc0 100644 --- a/refs/debug.c +++ b/refs/debug.c @@ -238,7 +238,7 @@ debug_ref_iterator_begin(struct ref_store *ref_store, const char *prefix, static int debug_read_raw_ref(struct ref_store *ref_store, const char *refname, struct object_id *oid, struct strbuf *referent, - unsigned int *type) + unsigned int *type, int *failure_errno) { struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store; int res = 0; @@ -246,7 +246,7 @@ static int debug_read_raw_ref(struct ref_store *ref_store, const char *refname, oidcpy(oid, null_oid()); errno = 0; res = drefs->refs->be->read_raw_ref(drefs->refs, refname, oid, referent, - type); + type, failure_errno); if (res == 0) { trace_printf_key(&trace_refs, "read_raw_ref: %s: %s (=> %s) type %x: %d\n", diff --git a/refs/files-backend.c b/refs/files-backend.c index 83ddfb3b627..f98b1be9386 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -341,9 +341,9 @@ static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs) return refs->loose; } -static int files_read_raw_ref(struct ref_store *ref_store, - const char *refname, struct object_id *oid, - struct strbuf *referent, unsigned int *type) +static int files_read_raw_ref(struct ref_store *ref_store, const char *refname, + struct object_id *oid, struct strbuf *referent, + unsigned int *type, int *failure_errno) { struct files_ref_store *refs = files_downcast(ref_store, REF_STORE_READ, "read_raw_ref"); @@ -354,7 +354,6 @@ static int files_read_raw_ref(struct ref_store *ref_store, struct stat st; int fd; int ret = -1; - int save_errno; int remaining_retries = 3; *type = 0; @@ -459,10 +458,9 @@ stat_ref: ret = parse_loose_ref_contents(buf, oid, referent, type); out: - save_errno = errno; + *failure_errno = errno; strbuf_release(&sb_path); strbuf_release(&sb_contents); - errno = save_errno; return ret; } @@ -541,6 +539,7 @@ static int lock_raw_ref(struct files_ref_store *refs, struct strbuf ref_file = STRBUF_INIT; int attempts_remaining = 3; int ret = TRANSACTION_GENERIC_ERROR; + int failure_errno = 0; assert(err); files_assert_main_repository(refs, "lock_raw_ref"); @@ -629,9 +628,9 @@ retry: * fear that its value will change. */ - if (files_read_raw_ref(&refs->base, refname, - &lock->old_oid, referent, type)) { - if (errno == ENOENT) { + if (files_read_raw_ref(&refs->base, refname, &lock->old_oid, referent, + type, &failure_errno)) { + if (failure_errno == ENOENT) { if (mustexist) { /* Garden variety missing reference. */ strbuf_addf(err, "unable to resolve reference '%s'", @@ -655,7 +654,7 @@ retry: * reference named "refs/foo/bar/baz". */ } - } else if (errno == EISDIR) { + } else if (failure_errno == EISDIR) { /* * There is a directory in the way. It might have * contained references that have been deleted. If @@ -693,13 +692,13 @@ retry: goto error_return; } } - } else if (errno == EINVAL && (*type & REF_ISBROKEN)) { + } else if (failure_errno == EINVAL && (*type & REF_ISBROKEN)) { strbuf_addf(err, "unable to resolve reference '%s': " "reference broken", refname); goto error_return; } else { strbuf_addf(err, "unable to resolve reference '%s': %s", - refname, strerror(errno)); + refname, strerror(failure_errno)); goto error_return; } diff --git a/refs/packed-backend.c b/refs/packed-backend.c index dfecdbc1db6..a457f18e93c 100644 --- a/refs/packed-backend.c +++ b/refs/packed-backend.c @@ -724,9 +724,9 @@ static struct snapshot *get_snapshot(struct packed_ref_store *refs) return refs->snapshot; } -static int packed_read_raw_ref(struct ref_store *ref_store, - const char *refname, struct object_id *oid, - struct strbuf *referent, unsigned int *type) +static int packed_read_raw_ref(struct ref_store *ref_store, const char *refname, + struct object_id *oid, struct strbuf *referent, + unsigned int *type, int *failure_errno) { struct packed_ref_store *refs = packed_downcast(ref_store, REF_STORE_READ, "read_raw_ref"); @@ -739,7 +739,7 @@ static int packed_read_raw_ref(struct ref_store *ref_store, if (!rec) { /* refname is not a packed reference. */ - errno = ENOENT; + *failure_errno = ENOENT; return -1; } diff --git a/refs/refs-internal.h b/refs/refs-internal.h index f4445e32904..79dfb3af484 100644 --- a/refs/refs-internal.h +++ b/refs/refs-internal.h @@ -617,11 +617,15 @@ typedef int reflog_expire_fn(struct ref_store *ref_store, * properly-formatted or even safe reference name. NEITHER INPUT NOR * OUTPUT REFERENCE NAMES ARE VALIDATED WITHIN THIS FUNCTION. * - * Return 0 on success. If the ref doesn't exist, set errno to ENOENT - * and return -1. If the ref exists but is neither a symbolic ref nor - * an object ID, it is broken; set REF_ISBROKEN in type, and return -1 - * (errno should not be ENOENT) If there is another error reading the - * ref, set errno appropriately and return -1. + * Return 0 on success, or -1 on failure. If the ref exists but is neither a + * symbolic ref nor an object ID, it is broken. In this case set REF_ISBROKEN in + * type, and return -1 (failure_errno should not be ENOENT) + * + * failure_errno provides errno codes that are interpreted beyond error + * reporting. The following error codes have special meaning: + * * ENOENT: the ref doesn't exist + * * EISDIR: ref name is a directory + * * ENOTDIR: ref prefix is not a directory * * Backend-specific flags might be set in type as well, regardless of * outcome. @@ -635,9 +639,9 @@ typedef int reflog_expire_fn(struct ref_store *ref_store, * - in all other cases, referent will be untouched, and therefore * refname will still be valid and unchanged. */ -typedef int read_raw_ref_fn(struct ref_store *ref_store, - const char *refname, struct object_id *oid, - struct strbuf *referent, unsigned int *type); +typedef int read_raw_ref_fn(struct ref_store *ref_store, const char *refname, + struct object_id *oid, struct strbuf *referent, + unsigned int *type, int *failure_errno); struct ref_storage_be { struct ref_storage_be *next; From patchwork Wed Jul 7 19:07:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12363899 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AA6F9C07E95 for ; Wed, 7 Jul 2021 19:08:00 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 95E1361CBE for ; Wed, 7 Jul 2021 19:08:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231341AbhGGTKj (ORCPT ); Wed, 7 Jul 2021 15:10:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47254 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231213AbhGGTKg (ORCPT ); Wed, 7 Jul 2021 15:10:36 -0400 Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1A590C061762 for ; Wed, 7 Jul 2021 12:07:55 -0700 (PDT) Received: by mail-wm1-x32c.google.com with SMTP id l18-20020a1ced120000b029014c1adff1edso5063386wmh.4 for ; Wed, 07 Jul 2021 12:07:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:in-reply-to:references:from:date:subject:mime-version :content-transfer-encoding:fcc:to:cc; bh=GCfG8rTvAfcyfyyeaBKL658tAZ6pdhfGo5h/dqS0vGc=; b=CFw2/+YIrJCcaMy9tegAgAUX9I/MxijgRsV23Yii0xeSilUDSaH9xnajUDFmWzyanc hyu0LFfDFwMj5Rq9RAJ8rLT5dSVtEQ81T+mZ6WIwEeVlSxGNWkNEgcEBoy0kZda2qcKb 4F7myf1vJDCl2VkTN/eyeqrSaoor7eO19/Na6cVQ7M6uCEIFYpIoutg8S9NabInpGLhY Wlk9K6GeDSZLC3x1xXaAar89PK4fX23SydtU5hLrDonejEKdLncfhFcvAE4sS8Eo9LJp FFfLm2l7+lTwvNhedJ3s3jkMxHW1NLthzD3oaZQgh2kTgydxbDTWDnEjESQPDtyqy2U/ VB+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:mime-version:content-transfer-encoding:fcc:to:cc; bh=GCfG8rTvAfcyfyyeaBKL658tAZ6pdhfGo5h/dqS0vGc=; b=GBlxVNxl/zgewYdE6/KTOIo6N2sM0WCcxVDCkUD6LgyGtoBXRx/apxzsUvMkOcbDTX 8K7/8JGnGME1r4qN3/+4wJvcSspkcSucEq++rtwevRIH7U7/YOBUNRJBsrNbNAYdmI8T z5fo8sKiUax+iK11hZFBe1Ekn4hfkIV5CPVWJiQmBJocb8YaYN7gYyqZxc+QBpbW/xrv ENHhBlcvWPtoYKIQPYlmWY5rVN+V38rn/jtghCilzUGb9QUDTmwQQBNd2XwLOYmcFVcX My95SJVR2oLVbRT29XV1AocW6EBkX4w44Lj+7RlA6nCWSNbxT9smV7GifKaplQuZIL3v aKHA== X-Gm-Message-State: AOAM533eaSsJNS7AsjhX3r1qLSNZED7T1QFPQgeBcw6cN4lvAFl1Ylok W11TJiHMw69zOa5C7dzIFvJE2G+SZRM= X-Google-Smtp-Source: ABdhPJxiAvuqoo5eKeBAoR9ULgr9fHbMz2kSX2i9jSSbmz7HoBTp/Q98P13YSv2ETTujA6ka5Zqiqg== X-Received: by 2002:a05:600c:4152:: with SMTP id h18mr65056wmm.94.1625684873736; Wed, 07 Jul 2021 12:07:53 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id k5sm21733558wmk.11.2021.07.07.12.07.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 12:07:53 -0700 (PDT) Message-Id: In-Reply-To: References: Date: Wed, 07 Jul 2021 19:07:47 +0000 Subject: [PATCH v5 4/6] refs: add failure_errno to refs_read_raw_ref() signature MIME-Version: 1.0 Fcc: Sent To: git@vger.kernel.org Cc: Han-Wen Nienhuys , Jonathan Tan , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Han-Wen Nienhuys , Han-Wen Nienhuys Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Han-Wen Nienhuys From: Han-Wen Nienhuys This lets us use the explicit errno output parameter in refs_resolve_ref_unsafe. Signed-off-by: Han-Wen Nienhuys Reviewed-by: Ævar Arnfjörð Bjarmason --- refs.c | 60 ++++++++++++++++++++++++++----------------- refs/files-backend.c | 8 +++--- refs/packed-backend.c | 7 ++--- refs/refs-internal.h | 6 ++--- 4 files changed, 48 insertions(+), 33 deletions(-) diff --git a/refs.c b/refs.c index 5e5e3af8da0..de3826f60c9 100644 --- a/refs.c +++ b/refs.c @@ -1671,34 +1671,33 @@ done: return result; } -int refs_read_raw_ref(struct ref_store *ref_store, - const char *refname, struct object_id *oid, - struct strbuf *referent, unsigned int *type) -{ - int result; - int failure_errno; +int refs_read_raw_ref(struct ref_store *ref_store, const char *refname, + struct object_id *oid, struct strbuf *referent, + unsigned int *type, int *failure_errno) +{ + int unused_errno; + if (!failure_errno) + failure_errno = &unused_errno; + *failure_errno = 0; if (!strcmp(refname, "FETCH_HEAD") || !strcmp(refname, "MERGE_HEAD")) { return refs_read_special_head(ref_store, refname, oid, referent, type); } - failure_errno = 0; - result = ref_store->be->read_raw_ref(ref_store, refname, oid, referent, - type, &failure_errno); - if (failure_errno) - errno = failure_errno; - return result; + return ref_store->be->read_raw_ref(ref_store, refname, oid, referent, + type, failure_errno); } -/* This function needs to return a meaningful errno on failure */ -const char *refs_resolve_ref_unsafe(struct ref_store *refs, - const char *refname, - int resolve_flags, - struct object_id *oid, int *flags) +const char *refs_resolve_ref_unsafe_with_errno(struct ref_store *refs, + const char *refname, + int resolve_flags, + struct object_id *oid, + int *flags, int *failure_errno) { static struct strbuf sb_refname = STRBUF_INIT; struct object_id unused_oid; int unused_flags; + int unused_errno; int symref_count; if (!oid) @@ -1707,6 +1706,9 @@ const char *refs_resolve_ref_unsafe(struct ref_store *refs, flags = &unused_flags; *flags = 0; + if (!failure_errno) + failure_errno = &unused_errno; + *failure_errno = 0; if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) { if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) || @@ -1728,11 +1730,14 @@ const char *refs_resolve_ref_unsafe(struct ref_store *refs, for (symref_count = 0; symref_count < SYMREF_MAXDEPTH; symref_count++) { unsigned int read_flags = 0; + int read_failure = 0; - if (refs_read_raw_ref(refs, refname, - oid, &sb_refname, &read_flags)) { + if (refs_read_raw_ref(refs, refname, oid, &sb_refname, + &read_flags, &read_failure)) { *flags |= read_flags; + *failure_errno = read_failure; + /* In reading mode, refs must eventually resolve */ if (resolve_flags & RESOLVE_REF_READING) return NULL; @@ -1742,9 +1747,8 @@ const char *refs_resolve_ref_unsafe(struct ref_store *refs, * may show errors besides ENOENT if there are * similarly-named refs. */ - if (errno != ENOENT && - errno != EISDIR && - errno != ENOTDIR) + if (read_failure != ENOENT && read_failure != EISDIR && + read_failure != ENOTDIR) return NULL; oidclr(oid); @@ -1783,6 +1787,15 @@ const char *refs_resolve_ref_unsafe(struct ref_store *refs, return NULL; } +const char *refs_resolve_ref_unsafe(struct ref_store *refs, const char *refname, + int resolve_flags, struct object_id *oid, + int *flags) +{ + int ignore = 0; + return refs_resolve_ref_unsafe_with_errno(refs, refname, resolve_flags, + oid, flags, &ignore); +} + /* backend functions */ int refs_init_db(struct strbuf *err) { @@ -2244,7 +2257,8 @@ int refs_verify_refname_available(struct ref_store *refs, if (skip && string_list_has_string(skip, dirname.buf)) continue; - if (!refs_read_raw_ref(refs, dirname.buf, &oid, &referent, &type)) { + if (!refs_read_raw_ref(refs, dirname.buf, &oid, &referent, + &type, NULL)) { strbuf_addf(err, _("'%s' exists; cannot create '%s'"), dirname.buf, refname); goto cleanup; diff --git a/refs/files-backend.c b/refs/files-backend.c index f98b1be9386..f38c9703504 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -383,8 +383,8 @@ stat_ref: if (lstat(path, &st) < 0) { if (errno != ENOENT) goto out; - if (refs_read_raw_ref(refs->packed_ref_store, refname, - oid, referent, type)) { + if (refs_read_raw_ref(refs->packed_ref_store, refname, oid, + referent, type, NULL)) { errno = ENOENT; goto out; } @@ -423,8 +423,8 @@ stat_ref: * ref is supposed to be, there could still be a * packed ref: */ - if (refs_read_raw_ref(refs->packed_ref_store, refname, - oid, referent, type)) { + if (refs_read_raw_ref(refs->packed_ref_store, refname, oid, + referent, type, NULL)) { errno = EISDIR; goto out; } diff --git a/refs/packed-backend.c b/refs/packed-backend.c index a457f18e93c..530dba29788 100644 --- a/refs/packed-backend.c +++ b/refs/packed-backend.c @@ -1347,6 +1347,7 @@ int is_packed_transaction_needed(struct ref_store *ref_store, ret = 0; for (i = 0; i < transaction->nr; i++) { struct ref_update *update = transaction->updates[i]; + int failure_errno = 0; unsigned int type; struct object_id oid; @@ -1357,9 +1358,9 @@ int is_packed_transaction_needed(struct ref_store *ref_store, */ continue; - if (!refs_read_raw_ref(ref_store, update->refname, - &oid, &referent, &type) || - errno != ENOENT) { + if (!refs_read_raw_ref(ref_store, update->refname, &oid, + &referent, &type, &failure_errno) || + failure_errno != ENOENT) { /* * We have to actually delete that reference * -> this transaction is needed. diff --git a/refs/refs-internal.h b/refs/refs-internal.h index 79dfb3af484..54f57c6a2df 100644 --- a/refs/refs-internal.h +++ b/refs/refs-internal.h @@ -149,9 +149,9 @@ struct ref_update { const char refname[FLEX_ARRAY]; }; -int refs_read_raw_ref(struct ref_store *ref_store, - const char *refname, struct object_id *oid, - struct strbuf *referent, unsigned int *type); +int refs_read_raw_ref(struct ref_store *ref_store, const char *refname, + struct object_id *oid, struct strbuf *referent, + unsigned int *type, int *failure_errno); /* * Write an error to `err` and return a nonzero value iff the same From patchwork Wed Jul 7 19:07:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12363901 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 17D5DC07E9B for ; Wed, 7 Jul 2021 19:08:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id F066761CBE for ; Wed, 7 Jul 2021 19:08:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231356AbhGGTKl (ORCPT ); Wed, 7 Jul 2021 15:10:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47266 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231231AbhGGTKh (ORCPT ); Wed, 7 Jul 2021 15:10:37 -0400 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 9CF85C061764 for ; Wed, 7 Jul 2021 12:07:55 -0700 (PDT) Received: by mail-wm1-x334.google.com with SMTP id t14-20020a05600c198eb029020c8aac53d4so14011244wmq.1 for ; Wed, 07 Jul 2021 12:07:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:in-reply-to:references:from:date:subject:mime-version :content-transfer-encoding:fcc:to:cc; bh=cfBVhUna2lbjAsrA3JNI32sKNpt3u/ybL18YS4VeEvg=; b=qO2bSnZ7FS9y5qfn07yp3ghCtfS8fBN72dKWaMjd7zEve5tbuhdMsQkiI5kwN8yDkr pCHscGWxyUrSSSAAmnsI+MpUgSkbAwN+p85rF/B26ugL2ve2mNxQn+CA7zX4W/D3ZmZK pQzPHogkFyN1XmvXOzUMGEycJ42FG3cAXXpgxVmNuOHFpLGGTsIoIv8XQshgnTBKvtcr CHZqVZCjacAlfXjr6v8wuoaGAcbBe/80xeg1aB0OFAa+l4XOby57ebDMpGmO2Ejtct1u 742NGGjOBjdsbT0KRteNF06buYziMkpbgJDQHOxfm9Kq7W3k4PDonDcSW+U9E6boBsn6 +Vog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:mime-version:content-transfer-encoding:fcc:to:cc; bh=cfBVhUna2lbjAsrA3JNI32sKNpt3u/ybL18YS4VeEvg=; b=aeLccGmwSBzhvlQFXD6CsXDhj9AdkBCcZf3wLVKZnn7O4F9T26ZPr/bCsLDIiOcTI2 YRIimCG8sBuRjvbQ56qkfKZr9RPPSzokla+Zd/gWUVU2GmXZcJH8WgsD9clJNvQoYriJ fSZWvgY8HFG3pU+p4jFV14mBQ5SyRmPhZlnbj0hCr9qLuIL5Y25FfN5L6Negn5Rux/7k PkobmvZ/RpSocoCCVTvBITRIzEl4hvgAf+8MH5uXlk6TlRfxpJtmFCgmvQaTmXUgcAw7 Xsjp2uGPIO49PCdiZXm3QGm7duVqQsD+w2TxY4HAzQ9tXXGuQdpWmVDigSOiQcXdsqCq TI6A== X-Gm-Message-State: AOAM5302kqcJpEIMvj8mIBhArFHLRupuCKz4TPPulYlqtZIrGo0oc3yw /HLM1xIn4BbKoO2nWvMh8b6f2nJgZ04= X-Google-Smtp-Source: ABdhPJwTodhw0dVCiezvYsJ2Z/Au20JGgvxJzu0jQ4Cn7zTYjF8+aB3imEy1xfhR+rtj7uBMWawhyw== X-Received: by 2002:a05:600c:2058:: with SMTP id p24mr28495614wmg.76.1625684874320; Wed, 07 Jul 2021 12:07:54 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id z12sm18081403wrs.39.2021.07.07.12.07.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 12:07:54 -0700 (PDT) Message-Id: <6918c214d1b41b16c47625f0935389440253ec4c.1625684869.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 07 Jul 2021 19:07:48 +0000 Subject: [PATCH v5 5/6] refs: explicitly return failure_errno from parse_loose_ref_contents MIME-Version: 1.0 Fcc: Sent To: git@vger.kernel.org Cc: Han-Wen Nienhuys , Jonathan Tan , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Han-Wen Nienhuys , Han-Wen Nienhuys Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Han-Wen Nienhuys From: Han-Wen Nienhuys The EINVAL error from parse_loose_ref_contents is used in files-backend to create a custom error message. Signed-off-by: Han-Wen Nienhuys Reviewed-by: Ævar Arnfjörð Bjarmason --- refs.c | 8 +++++--- refs/files-backend.c | 14 ++++++++++---- refs/refs-internal.h | 6 ++++-- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/refs.c b/refs.c index de3826f60c9..96df78a9509 100644 --- a/refs.c +++ b/refs.c @@ -1653,7 +1653,8 @@ int for_each_fullref_in_prefixes(const char *namespace, static int refs_read_special_head(struct ref_store *ref_store, const char *refname, struct object_id *oid, - struct strbuf *referent, unsigned int *type) + struct strbuf *referent, unsigned int *type, + int *failure_errno) { struct strbuf full_path = STRBUF_INIT; struct strbuf content = STRBUF_INIT; @@ -1663,7 +1664,8 @@ static int refs_read_special_head(struct ref_store *ref_store, if (strbuf_read_file(&content, full_path.buf, 0) < 0) goto done; - result = parse_loose_ref_contents(content.buf, oid, referent, type); + result = parse_loose_ref_contents(content.buf, oid, referent, type, + failure_errno); done: strbuf_release(&full_path); @@ -1681,7 +1683,7 @@ int refs_read_raw_ref(struct ref_store *ref_store, const char *refname, *failure_errno = 0; if (!strcmp(refname, "FETCH_HEAD") || !strcmp(refname, "MERGE_HEAD")) { return refs_read_special_head(ref_store, refname, oid, referent, - type); + type, failure_errno); } return ref_store->be->read_raw_ref(ref_store, refname, oid, referent, diff --git a/refs/files-backend.c b/refs/files-backend.c index f38c9703504..7aafdf2ce3d 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -455,9 +455,14 @@ stat_ref: strbuf_rtrim(&sb_contents); buf = sb_contents.buf; - ret = parse_loose_ref_contents(buf, oid, referent, type); - + ret = parse_loose_ref_contents(buf, oid, referent, type, failure_errno); + errno = *failure_errno; out: + /* + * Many system calls in this function can fail with ENOTDIR/EISDIR, and + * we want to collect all of them, so simply copy the error out from + * errno. + */ *failure_errno = errno; strbuf_release(&sb_path); strbuf_release(&sb_contents); @@ -465,7 +470,8 @@ out: } int parse_loose_ref_contents(const char *buf, struct object_id *oid, - struct strbuf *referent, unsigned int *type) + struct strbuf *referent, unsigned int *type, + int *failure_errno) { const char *p; if (skip_prefix(buf, "ref:", &buf)) { @@ -484,7 +490,7 @@ int parse_loose_ref_contents(const char *buf, struct object_id *oid, if (parse_oid_hex(buf, oid, &p) || (*p != '\0' && !isspace(*p))) { *type |= REF_ISBROKEN; - errno = EINVAL; + *failure_errno = EINVAL; return -1; } return 0; diff --git a/refs/refs-internal.h b/refs/refs-internal.h index 54f57c6a2df..bf581e70cf6 100644 --- a/refs/refs-internal.h +++ b/refs/refs-internal.h @@ -689,10 +689,12 @@ struct ref_store { }; /* - * Parse contents of a loose ref file. + * Parse contents of a loose ref file. *failure_errno maybe be set to EINVAL for + * invalid contents. */ int parse_loose_ref_contents(const char *buf, struct object_id *oid, - struct strbuf *referent, unsigned int *type); + struct strbuf *referent, unsigned int *type, + int *failure_errno); /* * Fill in the generic part of refs and add it to our collection of From patchwork Wed Jul 7 19:07:49 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Han-Wen Nienhuys X-Patchwork-Id: 12363903 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1C39EC07E95 for ; Wed, 7 Jul 2021 19:08:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 036DB61CCC for ; Wed, 7 Jul 2021 19:08:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232066AbhGGTKp (ORCPT ); Wed, 7 Jul 2021 15:10:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47274 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231300AbhGGTKi (ORCPT ); Wed, 7 Jul 2021 15:10:38 -0400 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 687FAC061760 for ; Wed, 7 Jul 2021 12:07:57 -0700 (PDT) Received: by mail-wm1-x32a.google.com with SMTP id t14-20020a05600c198eb029020c8aac53d4so14011322wmq.1 for ; Wed, 07 Jul 2021 12:07:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:in-reply-to:references:from:date:subject:mime-version :content-transfer-encoding:fcc:to:cc; bh=UeWzuA2GPf46Kjx6p9Ss1NudAAoidr8Z9RGTJp2ICOs=; b=q/k/+ORSzlFRarkD0QlZ8LHmiYvMoXVXVYGbIfHS5er5ADJQB9dAQXY+FOSjP1H0Lj BFl7YmGIlhR82DmJVbo8V4a8m/QdL8w8mQyswmhl2drWuWW8MgKUJFaJ4L1kfxlT186K amJXi8JF4huYiSIsnG1bR09gW04PwQq2yVZAo6ppjZHIYsPcHqyy3Eb396yGdAugrLdR o8OKsUBAzcwQB6IUB1BJ5Q+pzy06R0sgjYMtkz/93NKH0cCbssp4fmb71GLFtrzsVIJd QflJ+lRfQ5j2anjSRloFfMTfUOxYxUVQ/BAkdGWjqXI4XInyv96XldIUY39sQ41QcbQZ XLZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:mime-version:content-transfer-encoding:fcc:to:cc; bh=UeWzuA2GPf46Kjx6p9Ss1NudAAoidr8Z9RGTJp2ICOs=; b=Qj2/wMO3MHzRLABsLpV50Ensytc4VPv//qO1cXXA0TLWkA7vRmZf0ZXiPuQ2uf/8Nm w7stNCZL/aahv9RUBIdLzcjC1QSF8RSgtVZmdwFu3mhdL2Yq9lKV2uFim05fsO/4jDec eyzIf86Ft/ltQ0fEOa6T4OMImG0OkIcwbD1YXm0oor3RgT0zBM8DWD3QG6EOJ7rGq1yP dDlu5V6ZGaUZHRl7z84Cj4i7udyf6QrPBroAFV6F0XoXyYwqeCw/OMpWluvU2ht0BxYf oSfgibTqUQJVRb73JrHY0Q/b8smK9gheFPz/2+VOWEBA+drzLQ2hCCfsrlMqmcbJJ1uE r/rg== X-Gm-Message-State: AOAM531HSLE0hHF5LLE+9gmPdSza9CWap3slbk/nuCnVTVCpqFxDvpoN D2rTLM02Q3DP3xbZILfl9HQISo231NQ= X-Google-Smtp-Source: ABdhPJyvzPyjKRPBSoNAR7nmn8paBSQYXspt8u5jdpPL4yf6ThzZ/3ikS8FParyq1fkwis0cPgHgkg== X-Received: by 2002:a1c:b457:: with SMTP id d84mr637356wmf.144.1625684875104; Wed, 07 Jul 2021 12:07:55 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id u1sm17598000wmm.6.2021.07.07.12.07.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 12:07:54 -0700 (PDT) Message-Id: <85a14bde90476bae765fe83edef414337fba377a.1625684869.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 07 Jul 2021 19:07:49 +0000 Subject: [PATCH v5 6/6] refs: make errno output explicit for refs_resolve_ref_unsafe MIME-Version: 1.0 Fcc: Sent To: git@vger.kernel.org Cc: Han-Wen Nienhuys , Jonathan Tan , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Han-Wen Nienhuys , Han-Wen Nienhuys Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Han-Wen Nienhuys From: Han-Wen Nienhuys This introduces refs_resolve_ref_unsafe_with_errno(), which makes the API contract for the errno output explicit. The implementation still relies on the global errno variable to ensure no side effects of this refactoring. lock_ref_oid_basic() in files-backend.c is the only caller of refs_resolve_ref() that needs error information to make logic decisions, so update that caller Signed-off-by: Han-Wen Nienhuys Reviewed-by: Ævar Arnfjörð Bjarmason --- refs/files-backend.c | 15 ++++++++------- refs/refs-internal.h | 8 ++++++++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/refs/files-backend.c b/refs/files-backend.c index 7aafdf2ce3d..d6a7a0ee919 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -929,6 +929,7 @@ static struct ref_lock *lock_ref_oid_basic(struct files_ref_store *refs, int mustexist = (old_oid && !is_null_oid(old_oid)); int resolve_flags = RESOLVE_REF_NO_RECURSE; int resolved; + int resolve_errno = 0; files_assert_main_repository(refs, "lock_ref_oid_basic"); assert(err); @@ -941,10 +942,11 @@ static struct ref_lock *lock_ref_oid_basic(struct files_ref_store *refs, resolve_flags |= RESOLVE_REF_ALLOW_BAD_NAME; files_ref_path(refs, &ref_file, refname); - resolved = !!refs_resolve_ref_unsafe(&refs->base, - refname, resolve_flags, - &lock->old_oid, type); - if (!resolved && errno == EISDIR) { + resolved = !!refs_resolve_ref_unsafe_with_errno(&refs->base, refname, + resolve_flags, + &lock->old_oid, type, + &resolve_errno); + if (!resolved && resolve_errno == EISDIR) { /* * we are trying to lock foo but we used to * have foo/bar which now does not exist; @@ -964,15 +966,14 @@ static struct ref_lock *lock_ref_oid_basic(struct files_ref_store *refs, &lock->old_oid, type); } if (!resolved) { - int last_errno = errno; - if (last_errno != ENOTDIR || + if (resolve_errno != ENOTDIR || /* in case of D/F conflict, try to generate a better error * message. If that fails, fall back to strerror(ENOTDIR). */ !refs_verify_refname_available(&refs->base, refname, extras, skip, err)) strbuf_addf(err, "unable to resolve reference '%s': %s", - refname, strerror(last_errno)); + refname, strerror(resolve_errno)); goto error_return; } diff --git a/refs/refs-internal.h b/refs/refs-internal.h index bf581e70cf6..df01d5dc8df 100644 --- a/refs/refs-internal.h +++ b/refs/refs-internal.h @@ -153,6 +153,14 @@ int refs_read_raw_ref(struct ref_store *ref_store, const char *refname, struct object_id *oid, struct strbuf *referent, unsigned int *type, int *failure_errno); +/* Like refs_resolve_ref_unsafe, but provide access to errno code that lead to a + * failure. */ +const char *refs_resolve_ref_unsafe_with_errno(struct ref_store *refs, + const char *refname, + int resolve_flags, + struct object_id *oid, + int *flags, int *failure_errno); + /* * Write an error to `err` and return a nonzero value iff the same * refname appears multiple times in `refnames`. `refnames` must be