git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 22/29] commit_packed_refs(): use a staging file separate from the lockfile

From
Michael Haggerty <mhagger@alum.mit.edu>
Date
Jun 23, 2017, 07:01 UTC
Message-ID
<d8fa8dec13eb095c0c39ef0de2ee8987d6bf9d77.1498200513.git.mhagger@alum.mit.edu>
In-Reply-To
<cover.1498200513.git.mhagger@alum.mit.edu>

We will want to be able to hold the lockfile for `packed-refs` even after we have activated the new values. So use a separate tempfile, `packed-refs.new`, as a place to stage the new contents of the `packed-refs` file. For now this is all done within `commit_packed_refs()`, but that will change shortly.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 refs/packed-backend.c | 40 ++++++++++++++++++++++++++++++++--------
 1 file changed, 32 insertions(+), 8 deletions(-)
diff --git a/refs/packed-backend.c b/refs/packed-backend.c
index 18ce47fcb7..71f92ed6f0 100644
--- a/refs/packed-backend.c
+++ b/refs/packed-backend.c
@@ -68,6 +68,13 @@ struct packed_ref_store {
 	 * thus the enclosing `packed_ref_store`) must not be freed.
 	 */
 	struct lock_file lock;
+
+	/*
+	 * Temporary file used when rewriting new contents to the
+	 * "packed-refs" file. Note that this (and thus the enclosing
+	 * `packed_ref_store`) must not be freed.
+	 */
+	struct tempfile tempfile;
 };
 
 struct ref_store *packed_ref_store_create(const char *path,
@@ -522,10 +529,16 @@ int lock_packed_refs(struct ref_store *ref_store, int flags)
 		timeout_configured = 1;
 	}
 
+	/*
+	 * Note that we close the lockfile immediately because we
+	 * don't write new content to it, but rather to a separate
+	 * tempfile.
+	 */
 	if (hold_lock_file_for_update_timeout(
 			    &refs->lock,
 			    refs->path,
-			    flags, timeout_value) < 0)
+			    flags, timeout_value) < 0 ||
+	    close_lock_file(&refs->lock))
 		return -1;
 
 	/*
@@ -567,13 +580,23 @@ int commit_packed_refs(struct ref_store *ref_store, struct strbuf *err)
 		get_packed_ref_cache(refs);
 	int ok;
 	int ret = -1;
+	struct strbuf sb = STRBUF_INIT;
 	FILE *out;
 	struct ref_iterator *iter;
 
 	if (!is_lock_file_locked(&refs->lock))
 		die("BUG: commit_packed_refs() called when unlocked");
 
-	out = fdopen_lock_file(&refs->lock, "w");
+	strbuf_addf(&sb, "%s.new", refs->path);
+	if (create_tempfile(&refs->tempfile, sb.buf) < 0) {
+		strbuf_addf(err, "unable to create file %s: %s",
+			    sb.buf, strerror(errno));
+		strbuf_release(&sb);
+		goto out;
+	}
+	strbuf_release(&sb);
+
+	out = fdopen_tempfile(&refs->tempfile, "w");
 	if (!out) {
 		strbuf_addf(err, "unable to fdopen packed-refs tempfile: %s",
 			    strerror(errno));
@@ -582,7 +605,7 @@ int commit_packed_refs(struct ref_store *ref_store, struct strbuf *err)
 
 	if (fprintf(out, "%s", PACKED_REFS_HEADER) < 0) {
 		strbuf_addf(err, "error writing to %s: %s",
-			    get_lock_file_path(&refs->lock), strerror(errno));
+			    get_tempfile_path(&refs->tempfile), strerror(errno));
 		goto error;
 	}
 
@@ -594,7 +617,7 @@ int commit_packed_refs(struct ref_store *ref_store, struct strbuf *err)
 		if (write_packed_entry(out, iter->refname, iter->oid->hash,
 				       peel_error ? NULL : peeled.hash)) {
 			strbuf_addf(err, "error writing to %s: %s",
-				    get_lock_file_path(&refs->lock),
+				    get_tempfile_path(&refs->tempfile),
 				    strerror(errno));
 			ref_iterator_abort(iter);
 			goto error;
@@ -602,13 +625,13 @@ int commit_packed_refs(struct ref_store *ref_store, struct strbuf *err)
 	}
 
 	if (ok != ITER_DONE) {
-		strbuf_addf(err, "unable to write packed-refs file: "
+		strbuf_addf(err, "unable to rewrite packed-refs file: "
 			    "error iterating over old contents");
 		goto error;
 	}
 
-	if (commit_lock_file(&refs->lock)) {
-		strbuf_addf(err, "error overwriting %s: %s",
+	if (rename_tempfile(&refs->tempfile, refs->path)) {
+		strbuf_addf(err, "error replacing %s: %s",
 			    refs->path, strerror(errno));
 		goto out;
 	}
@@ -617,9 +640,10 @@ int commit_packed_refs(struct ref_store *ref_store, struct strbuf *err)
 	goto out;
 
 error:
-	rollback_lock_file(&refs->lock);
+	delete_tempfile(&refs->tempfile);
 
 out:
+	rollback_lock_file(&refs->lock);
 	release_packed_ref_cache(packed_ref_cache);
 	return ret;
 }
-- 
2.11.0
Previous: Michael HaggertyNext: Jeff King
Message 25 of 52 in “Create a reference backend for packed refs”
  1. 00/29 Create a reference backend for packed refsMichael Haggerty, Jun 23, 2017
  2. 01/29 t1408: add a test of stale packed refs covered by loose refsMichael Haggerty, Jun 23, 2017
  3. Jeff KingJun 23, 2017
  4. Junio C HamanoJun 23, 2017
  5. 02/29 add_packed_ref(): teach function to overwrite existing refsMichael Haggerty, Jun 23, 2017
  6. 07/29 validate_packed_ref_cache(): take a `packed_ref_store *` parameterMichael Haggerty, Jun 23, 2017
  7. 05/29 packed_ref_store: move `packed_refs_lock` member hereMichael Haggerty, Jun 23, 2017
  8. 03/29 packed_ref_store: new structMichael Haggerty, Jun 23, 2017
  9. 09/29 get_packed_refs(): take a `packed_ref_store *` parameterMichael Haggerty, Jun 23, 2017
  10. 04/29 packed_ref_store: move `packed_refs_path` hereMichael Haggerty, Jun 23, 2017
  11. 13/29 rollback_packed_refs(): take a `packed_ref_store *` parameterMichael Haggerty, Jun 23, 2017
  12. 12/29 commit_packed_refs(): take a `packed_ref_store *` parameterMichael Haggerty, Jun 23, 2017
  13. 14/29 get_packed_ref(): take a `packed_ref_store *` parameterMichael Haggerty, Jun 23, 2017
  14. 06/29 clear_packed_ref_cache(): take a `packed_ref_store *` parameterMichael Haggerty, Jun 23, 2017
  15. 17/29 packed_ref_store: support iterationMichael Haggerty, Jun 23, 2017
  16. Jeff KingJun 23, 2017
  17. 16/29 packed_peel_ref(): new function, extracted from `files_peel_ref()`Michael Haggerty, Jun 23, 2017
  18. 19/29 packed-backend: new module for handling packed referencesMichael Haggerty, Jun 23, 2017
  19. Jeff KingJun 23, 2017
  20. Stefan BellerJun 23, 2017
  21. Junio C HamanoJun 23, 2017
  22. 20/29 packed_ref_store: make class into a subclass of `ref_store`Michael Haggerty, Jun 23, 2017
  23. Jeff KingJun 23, 2017
  24. 21/29 commit_packed_refs(): report errors rather than dyingMichael Haggerty, Jun 23, 2017
  25. 22/29 commit_packed_refs(): use a staging file separate from the lockfileMichael Haggerty, Jun 23, 2017
  26. Jeff KingJun 23, 2017
  27. Michael HaggertyJun 24, 2017
  28. Jeff KingJun 24, 2017
  29. 24/29 packed_refs_lock(): report errors via a `struct strbuf *err`Michael Haggerty, Jun 23, 2017
  30. 26/29 clear_packed_ref_cache(): don't protest if the lock is heldMichael Haggerty, Jun 23, 2017
  31. Jeff KingJun 23, 2017
  32. 25/29 packed_refs_unlock(), packed_refs_is_locked(): new functionsMichael Haggerty, Jun 23, 2017
  33. 23/29 packed_refs_lock(): function renamed from lock_packed_refs()Michael Haggerty, Jun 23, 2017
  34. 28/29 repack_without_refs(): don't lock or unlock the packed refsMichael Haggerty, Jun 23, 2017
  35. Jeff KingJun 23, 2017
  36. Michael HaggertyJul 1, 2017
  37. 27/29 commit_packed_refs(): remove call to `packed_refs_unlock()`Michael Haggerty, Jun 23, 2017
  38. Jeff KingJun 23, 2017
  39. 29/29 read_packed_refs(): die if `packed-refs` contains bogus dataMichael Haggerty, Jun 23, 2017
  40. Jeff KingJun 23, 2017
  41. Michael HaggertyJul 1, 2017
  42. 11/29 lock_packed_refs(): take a `packed_ref_store *` parameterMichael Haggerty, Jun 23, 2017
  43. 10/29 add_packed_ref(): take a `packed_ref_store *` parameterMichael Haggerty, Jun 23, 2017
  44. 15/29 repack_without_refs(): take a `packed_ref_store *` parameterMichael Haggerty, Jun 23, 2017
  45. 18/29 packed_read_raw_ref(): new function, replacing `resolve_packed_ref()`Michael Haggerty, Jun 23, 2017
  46. 08/29 get_packed_ref_cache(): take a `packed_ref_store *` parameterMichael Haggerty, Jun 23, 2017
  47. Jeff KingJun 23, 2017
  48. Jeff KingJun 23, 2017
  49. Junio C HamanoJun 23, 2017
  50. Jeff KingJun 24, 2017
  51. Michael HaggertyJun 24, 2017
  52. Junio C HamanoJun 23, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.