refs/files-backend: stop setting errno from lock_ref_oid_basic

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 <hanwen@google.com> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Han-Wen Nienhuys committed Aug 23, 2021 at 13:52 UTC 1ae6ed230ae696ceb7c4607e00d94642b416ea1c
1 file changed +2 -9
refs/files-backend.c
+2 -9
@@ -982,7 +982,6 @@ static int create_reflock(const char *path, void *cb)
982
983 /*
984 * Locks a ref returning the lock on success and NULL on failure.
985 - * On failure errno is set to something meaningful.
985 */
986 static struct ref_lock *lock_ref_oid_basic(struct files_ref_store *refs,
987 const char *refname, int *type,
@@ -990,7 +989,6 @@ static struct ref_lock *lock_ref_oid_basic(struct files_ref_store *refs,
989 {
990 struct strbuf ref_file = STRBUF_INIT;
991 struct ref_lock *lock;
993 - int last_errno = 0;
992
993 files_assert_main_repository(refs, "lock_ref_oid_basic");
994 assert(err);
@@ -1001,11 +999,10 @@ static struct ref_lock *lock_ref_oid_basic(struct files_ref_store *refs,
999 if (!refs_resolve_ref_unsafe(&refs->base, refname,
1000 RESOLVE_REF_NO_RECURSE,
1001 &lock->old_oid, type)) {
1004 - last_errno = errno;
1002 if (!refs_verify_refname_available(&refs->base, refname,
1003 NULL, NULL, err))
1004 strbuf_addf(err, "unable to resolve reference '%s': %s",
1008 - refname, strerror(last_errno));
1005 + refname, strerror(errno));
1006
1007 goto error_return;
1008 }
@@ -1018,15 +1015,12 @@ static struct ref_lock *lock_ref_oid_basic(struct files_ref_store *refs,
1015 */
1016 if (is_null_oid(&lock->old_oid) &&
1017 refs_verify_refname_available(refs->packed_ref_store, refname,
1021 - NULL, NULL, err)) {
1022 - last_errno = ENOTDIR;
1018 + NULL, NULL, err))
1019 goto error_return;
1024 - }
1020
1021 lock->ref_name = xstrdup(refname);
1022
1023 if (raceproof_create_file(ref_file.buf, create_reflock, &lock->lk)) {
1029 - last_errno = errno;
1024 unable_to_lock_message(ref_file.buf, errno, err);
1025 goto error_return;
1026 }
@@ -1043,7 +1037,6 @@ static struct ref_lock *lock_ref_oid_basic(struct files_ref_store *refs,
1037
1038 out:
1039 strbuf_release(&ref_file);
1046 - errno = last_errno;
1040 return lock;
1041 }
1042