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

[PATCH v2 1/4] lockfile: report when rollback fails

From
Patrick Steinhardt <ps@pks.im>
Date
Mar 7, 2024, 13:10 UTC
Message-ID
<782e96a678346bbdfca51ebcb4514307de84b882.1709816483.git.ps@pks.im>
In-Reply-To
<cover.1709816483.git.ps@pks.im>

We do not report to the caller when rolling back a lockfile fails, which will be needed by the reftable compaction logic in a subsequent commit. It also cannot really report on all errors because the function calls `delete_tempfile()`, which doesn't return an error either.

Refactor the code so that both `delete_tempfile()` and `rollback_lock_file()` return an error code.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 lockfile.h |  6 +++---
 tempfile.c | 21 +++++++++++++--------
 tempfile.h |  2 +-
 3 files changed, 17 insertions(+), 12 deletions(-)
diff --git a/lockfile.h b/lockfile.h
index 90af4e66b2..1bb9926497 100644
--- a/lockfile.h
+++ b/lockfile.h
@@ -321,11 +321,11 @@ static inline int commit_lock_file_to(struct lock_file *lk, const char *path)
  * Roll back `lk`: close the file descriptor and/or file pointer and
  * remove the lockfile. It is a NOOP to call `rollback_lock_file()`
  * for a `lock_file` object that has already been committed or rolled
- * back.
+ * back. No error will be returned in this case.
  */
-static inline void rollback_lock_file(struct lock_file *lk)
+static inline int rollback_lock_file(struct lock_file *lk)
 {
-	delete_tempfile(&lk->tempfile);
+	return delete_tempfile(&lk->tempfile);
 }
 
 #endif /* LOCKFILE_H */
diff --git a/tempfile.c b/tempfile.c
index ecdebf1afb..ed88cf8431 100644
--- a/tempfile.c
+++ b/tempfile.c
@@ -50,15 +50,17 @@
 
 static VOLATILE_LIST_HEAD(tempfile_list);
 
-static void remove_template_directory(struct tempfile *tempfile,
+static int remove_template_directory(struct tempfile *tempfile,
 				      int in_signal_handler)
 {
 	if (tempfile->directory) {
 		if (in_signal_handler)
-			rmdir(tempfile->directory);
+			return rmdir(tempfile->directory);
 		else
-			rmdir_or_warn(tempfile->directory);
+			return rmdir_or_warn(tempfile->directory);
 	}
+
+	return 0;
 }
 
 static void remove_tempfiles(int in_signal_handler)
@@ -353,16 +355,19 @@ int rename_tempfile(struct tempfile **tempfile_p, const char *path)
 	return 0;
 }
 
-void delete_tempfile(struct tempfile **tempfile_p)
+int delete_tempfile(struct tempfile **tempfile_p)
 {
 	struct tempfile *tempfile = *tempfile_p;
+	int err = 0;
 
 	if (!is_tempfile_active(tempfile))
-		return;
+		return 0;
 
-	close_tempfile_gently(tempfile);
-	unlink_or_warn(tempfile->filename.buf);
-	remove_template_directory(tempfile, 0);
+	err |= close_tempfile_gently(tempfile);
+	err |= unlink_or_warn(tempfile->filename.buf);
+	err |= remove_template_directory(tempfile, 0);
 	deactivate_tempfile(tempfile);
 	*tempfile_p = NULL;
+
+	return err ? -1 : 0;
 }
diff --git a/tempfile.h b/tempfile.h
index d0413af733..2d2ae5b657 100644
--- a/tempfile.h
+++ b/tempfile.h
@@ -269,7 +269,7 @@ int reopen_tempfile(struct tempfile *tempfile);
  * `delete_tempfile()` for a `tempfile` object that has already been
  * deleted or renamed.
  */
-void delete_tempfile(struct tempfile **tempfile_p);
+int delete_tempfile(struct tempfile **tempfile_p);
 
 /*
  * Close the file descriptor and/or file pointer if they are still
-- 
2.44.0
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 23 of 26 in “reftable/stack: register temporary files”
  1. 0/4 reftable/stack: register temporary filesPatrick Steinhardt, Mar 4, 2024
  2. 1/4 lockfile: report when rollback failsPatrick Steinhardt, Mar 4, 2024
  3. Justin ToblerMar 5, 2024
  4. Patrick SteinhardtMar 6, 2024
  5. 2/4 reftable/stack: register new tables as tempfilesPatrick Steinhardt, Mar 4, 2024
  6. Justin ToblerMar 5, 2024
  7. Patrick SteinhardtMar 6, 2024
  8. Justin ToblerMar 6, 2024
  9. Junio C HamanoMar 6, 2024
  10. Patrick SteinhardtMar 7, 2024
  11. Junio C HamanoMar 7, 2024
  12. Patrick SteinhardtMar 7, 2024
  13. Junio C HamanoMar 7, 2024
  14. 3/4 reftable/stack: register lockfiles during compactionPatrick Steinhardt, Mar 4, 2024
  15. Justin ToblerMar 5, 2024
  16. Patrick SteinhardtMar 6, 2024
  17. Junio C HamanoMar 6, 2024
  18. Justin ToblerMar 6, 2024
  19. 4/4 reftable/stack: register compacted tables as tempfilesPatrick Steinhardt, Mar 4, 2024
  20. Toon claesMar 7, 2024
  21. Patrick SteinhardtMar 7, 2024
  22. 0/4 reftable/stack: register temporary filesPatrick Steinhardt, Mar 7, 2024
  23. 1/4 lockfile: report when rollback failsPatrick Steinhardt, Mar 7, 2024
  24. 2/4 reftable/stack: register new tables as tempfilesPatrick Steinhardt, Mar 7, 2024
  25. 3/4 reftable/stack: register lockfiles during compactionPatrick Steinhardt, Mar 7, 2024
  26. 4/4 reftable/stack: register compacted tables as tempfilesPatrick Steinhardt, Mar 7, 2024

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.