git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ronnie Sahlberg <sahlberg@google.com>
To: git@vger.kernel.org
Cc: jrnieder@gmail.com, Ronnie Sahlberg <sahlberg@google.com>
Subject: [PATCH v12 39/41] refs.c: propagate any errno==ENOTDIR from _commit back to the callers
Date: Thu, 29 May 2014 09:07:54 -0700	[thread overview]
Message-ID: <1401379676-9307-13-git-send-email-sahlberg@google.com> (raw)
In-Reply-To: <1401379676-9307-1-git-send-email-sahlberg@google.com>

In _commit, ENOTDIR can happen in the call to lock_ref_sha1_basic, either when
we lstat the new refname and it returns ENOTDIR or if the name checking
function reports that the same type of conflict happened. In both cases it
means that we can not create the new ref due to a name conflict.

For these cases, save the errno value and abort and make sure that the caller
can see errno==ENOTDIR.

Also start defining specific return codes for _commit, assign -1 as a generic
error and -2 as the error that refers to a name conflict. Callers can (and
should) use that return value inspecting errno directly.

Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 refs.c | 23 ++++++++++++++++-------
 refs.h |  4 ++++
 2 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/refs.c b/refs.c
index ec8d642..6414afc 100644
--- a/refs.c
+++ b/refs.c
@@ -3504,7 +3504,7 @@ static int ref_update_reject_duplicates(struct ref_update **updates, int n,
 int ref_transaction_commit(struct ref_transaction *transaction,
 			   struct strbuf *err)
 {
-	int ret = 0, delnum = 0, i;
+	int ret = 0, delnum = 0, i, save_errno = 0;
 	const char **delnames;
 	int n = transaction->nr;
 	struct ref_update **updates = transaction->updates;
@@ -3522,9 +3522,10 @@ int ref_transaction_commit(struct ref_transaction *transaction,
 
 	/* Copy, sort, and reject duplicate refs */
 	qsort(updates, n, sizeof(*updates), ref_update_compare);
-	ret = ref_update_reject_duplicates(updates, n, err);
-	if (ret)
+	if (ref_update_reject_duplicates(updates, n, err)) {
+		ret = -1;
 		goto cleanup;
+	}
 
 	/* Acquire all locks while verifying old values */
 	for (i = 0; i < n; i++) {
@@ -3538,10 +3539,12 @@ int ref_transaction_commit(struct ref_transaction *transaction,
 						   &update->type,
 						   delnames, delnum);
 		if (!update->lock) {
+			if (errno == ENOTDIR)
+				save_errno = errno;
 			if (err)
 				strbuf_addf(err, "Cannot lock the ref '%s'.",
 					    update->refname);
-			ret = 1;
+			ret = -1;
 			goto cleanup;
 		}
 	}
@@ -3559,6 +3562,7 @@ int ref_transaction_commit(struct ref_transaction *transaction,
 
 				if (err)
 					strbuf_addf(err, str, update->refname);
+				ret = -1;
 				goto cleanup;
 			}
 		}
@@ -3569,14 +3573,16 @@ int ref_transaction_commit(struct ref_transaction *transaction,
 		struct ref_update *update = updates[i];
 
 		if (update->lock) {
-			ret |= delete_ref_loose(update->lock, update->type,
-						err);
+			if (delete_ref_loose(update->lock, update->type, err))
+				ret = -1;
+
 			if (!(update->flags & REF_ISPRUNING))
 				delnames[delnum++] = update->lock->ref_name;
 		}
 	}
 
-	ret |= repack_without_refs(delnames, delnum, err);
+	if (repack_without_refs(delnames, delnum, err))
+		ret = -1;
 	for (i = 0; i < delnum; i++)
 		unlink_or_warn(git_path("logs/%s", delnames[i]));
 	clear_loose_ref_cache(&ref_cache);
@@ -3589,6 +3595,9 @@ cleanup:
 		if (updates[i]->lock)
 			unlock_ref(updates[i]->lock);
 	free(delnames);
+	errno = save_errno;
+	if (save_errno == ENOTDIR)
+		ret = -2;
 	return ret;
 }
 
diff --git a/refs.h b/refs.h
index 5c6c20f..88732a1 100644
--- a/refs.h
+++ b/refs.h
@@ -323,6 +323,10 @@ int ref_transaction_delete(struct ref_transaction *transaction,
  * Commit all of the changes that have been queued in transaction, as
  * atomically as possible.  Return a nonzero value if there is a
  * problem.
+ * If the transaction is already in failed state this function will return
+ * an error.
+ * Function returns 0 on success, -1 for generic failures and -2 if the
+ * failure was due to a name collision (ENOTDIR).
  */
 int ref_transaction_commit(struct ref_transaction *transaction,
 			   struct strbuf *err);
-- 
2.0.0.rc3.474.g3833130

  parent reply	other threads:[~2014-05-29 16:08 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-29 16:07 [PATCH v12 00/44] Use ref transactions for all ref updates Ronnie Sahlberg
2014-05-29 16:07 ` [PATCH v12 06/41] refs.c: add an err argument to repack_without_refs Ronnie Sahlberg
2014-05-29 18:17   ` Jonathan Nieder
2014-06-03 20:55     ` Ronnie Sahlberg
2014-05-29 16:07 ` [PATCH v12 08/41] refs.c: add an err argument to delete_ref_loose Ronnie Sahlberg
2014-05-29 16:07 ` [PATCH v12 16/41] refs.c: add transaction.status and track OPEN/CLOSED/ERROR Ronnie Sahlberg
2014-05-29 16:07 ` [PATCH v12 24/41] receive-pack.c: use a reference transaction for updating the refs Ronnie Sahlberg
2014-05-29 16:07 ` [PATCH v12 25/41] fast-import.c: use a ref transaction when dumping tags Ronnie Sahlberg
2014-05-29 16:07 ` [PATCH v12 26/41] walker.c: use ref transaction for ref updates Ronnie Sahlberg
2014-05-29 16:07 ` [PATCH v12 31/41] refs.c: make prune_ref use a transaction to delete the ref Ronnie Sahlberg
2014-05-29 16:07 ` [PATCH v12 32/41] refs.c: make delete_ref use a transaction Ronnie Sahlberg
2014-05-29 16:07 ` [PATCH v12 33/41] refs.c: pass the ref log message to _create/delete/update instead of _commit Ronnie Sahlberg
2014-05-29 16:07 ` [PATCH v12 36/41] refs.c: move the check for valid refname to lock_ref_sha1_basic Ronnie Sahlberg
2014-05-29 16:07 ` [PATCH v12 38/41] refs.c: pass a skip list to name_conflict_fn Ronnie Sahlberg
2014-05-29 16:07 ` Ronnie Sahlberg [this message]
2014-05-29 16:07 ` [PATCH v12 40/41] fetch.c: change s_update_ref to use a ref transaction Ronnie Sahlberg
2014-05-29 16:07 ` [PATCH v12 41/41] refs.c: make write_ref_sha1 static Ronnie Sahlberg
2014-05-29 16:13 ` [PATCH v12 00/44] Use ref transactions for all ref updates Ronnie Sahlberg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1401379676-9307-13-git-send-email-sahlberg@google.com \
    --to=sahlberg@google.com \
    --cc=git@vger.kernel.org \
    --cc=jrnieder@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).