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

[PATCH 04/14] commit_lock_file(): use get_locked_file_path()

From
Michael Haggerty <mhagger@alum.mit.edu>
Date
Jun 8, 2015, 09:07 UTC
Message-ID
<54a72741f122826ffc1c7f87941b8d9961bc4642.1433751986.git.mhagger@alum.mit.edu>
In-Reply-To
<cover.1433751986.git.mhagger@alum.mit.edu>

First beef up the sanity checking in get_locked_file_path() to match that in commit_lock_file(). Then rewrite commit_lock_file() to use get_locked_file_path() for its pathname computation.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 lockfile.c | 30 +++++++++++++-----------------
 1 file changed, 13 insertions(+), 17 deletions(-)
diff --git a/lockfile.c b/lockfile.c
index c2d6ad1..7d04ed1 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -232,8 +232,11 @@ char *get_locked_file_path(struct lock_file *lk)
 {
 	if (!lk->tempfile.active)
 		die("BUG: get_locked_file_path() called for unlocked object");
-	if (lk->tempfile.filename.len <= LOCK_SUFFIX_LEN)
+	if (lk->tempfile.filename.len <= LOCK_SUFFIX_LEN ||
+	    strcmp(lk->tempfile.filename.buf + lk->tempfile.filename.len - LOCK_SUFFIX_LEN,
+		   LOCK_SUFFIX))
 		die("BUG: get_locked_file_path() called for malformed lock object");
+	/* remove ".lock": */
 	return xmemdupz(lk->tempfile.filename.buf, lk->tempfile.filename.len - LOCK_SUFFIX_LEN);
 }
 
@@ -244,23 +247,16 @@ int commit_lock_file_to(struct lock_file *lk, const char *path)
 
 int commit_lock_file(struct lock_file *lk)
 {
-	static struct strbuf result_file = STRBUF_INIT;
-	int err;
+	char *result_path = get_locked_file_path(lk);
 
-	if (!lk->tempfile.active)
-		die("BUG: attempt to commit unlocked object");
-
-	if (lk->tempfile.filename.len <= LOCK_SUFFIX_LEN ||
-	    strcmp(lk->tempfile.filename.buf + lk->tempfile.filename.len - LOCK_SUFFIX_LEN,
-		   LOCK_SUFFIX))
-		die("BUG: lockfile filename corrupt");
-
-	/* remove ".lock": */
-	strbuf_add(&result_file, lk->tempfile.filename.buf,
-		   lk->tempfile.filename.len - LOCK_SUFFIX_LEN);
-	err = commit_lock_file_to(lk, result_file.buf);
-	strbuf_reset(&result_file);
-	return err;
+	if (commit_lock_file_to(lk, result_path)) {
+		int save_errno = errno;
+		free(result_path);
+		errno = save_errno;
+		return -1;
+	}
+	free(result_path);
+	return 0;
 }
 
 void rollback_lock_file(struct lock_file *lk)
-- 
2.1.4
Previous: Johannes SixtNext: Michael Haggerty
Message 10 of 26 in “Introduce a tempfile module”
  1. 00/14 Introduce a tempfile moduleMichael Haggerty, Jun 8, 2015
  2. 01/14 Move lockfile API documentation to lockfile.hMichael Haggerty, Jun 8, 2015
  3. 02/14 tempfile: a new module for handling temporary filesMichael Haggerty, Jun 8, 2015
  4. Junio C HamanoJun 10, 2015
  5. Michael HaggertyJun 10, 2015
  6. Junio C HamanoJun 10, 2015
  7. 03/14 lockfile: remove some redundant functionsMichael Haggerty, Jun 8, 2015
  8. Junio C HamanoJun 10, 2015
  9. Johannes SixtJun 10, 2015
  10. 04/14 commit_lock_file(): use get_locked_file_path()Michael Haggerty, Jun 8, 2015
  11. 05/14 register_tempfile_object(): new function, extracted from create_tempfile()Michael Haggerty, Jun 8, 2015
  12. 06/14 tempfile: add several functions for creating temporary filesMichael Haggerty, Jun 8, 2015
  13. Junio C HamanoJun 10, 2015
  14. Michael HaggertyAug 10, 2015
  15. 07/14 register_tempfile(): new function to handle an existing temporary fileMichael Haggerty, Jun 8, 2015
  16. Junio C HamanoJun 10, 2015
  17. Michael HaggertyAug 10, 2015
  18. 08/14 write_shared_index(): use tempfile moduleMichael Haggerty, Jun 8, 2015
  19. Junio C HamanoJun 10, 2015
  20. 09/14 setup_temporary_shallow(): use tempfile moduleMichael Haggerty, Jun 8, 2015
  21. 10/14 diff: use tempfile moduleMichael Haggerty, Jun 8, 2015
  22. 11/14 lock_repo_for_gc(): compute the path to "gc.pid" only onceMichael Haggerty, Jun 8, 2015
  23. 12/14 gc: use tempfile module to handle gc.pid fileMichael Haggerty, Jun 8, 2015
  24. 13/14 credential-cache--daemon: delete socket from main()Michael Haggerty, Jun 8, 2015
  25. 14/14 credential-cache--daemon: use tempfile moduleMichael Haggerty, Jun 8, 2015
  26. Junio C HamanoJun 10, 2015

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.