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

[PATCH v2 4/6] tmp-objdir: preserve errno when directory creation fails

From
Justin Tobler <jltobler@gmail.com>
Date
Nov 19, 2026, 00:20 UTC
Message-ID
<20261119002043.2108654-5-jltobler@gmail.com>
In-Reply-To
<20261119002043.2108654-1-jltobler@gmail.com>

When `tmp_objdir_create()` fails to create the temporary object directory, it cleans up after itself before returning NULL. Depending on where the failure occurred, this cleanup may clobber `errno`, leaving callers without a way to reliably tell why creating the directory failed.

In a subsequent commit, ODB transactions create their temporary object directory eagerly and report a more specific error regarding creation failure. Preserve `errno` across the cleanup so that callers can inspect it accordingly.

Signed-off-by: Justin Tobler <jltobler@gmail.com>
---
 tmp-objdir.c | 11 +++++++++--
 tmp-objdir.h |  3 ++-
 2 files changed, 11 insertions(+), 3 deletions(-)
diff --git a/tmp-objdir.c b/tmp-objdir.c
index c78fc0e6b6..c3549c2e8d 100644
--- a/tmp-objdir.c
+++ b/tmp-objdir.c
@@ -151,6 +151,7 @@ struct tmp_objdir *tmp_objdir_create(struct repository *r,
 	struct odb_source_files *files = odb_source_files_downcast(r->objects->source);
 	static int installed_handlers;
 	struct tmp_objdir *t;
+	int saved_errno;
 	char *path;
 	int ret;
 
@@ -175,9 +176,10 @@ struct tmp_objdir *tmp_objdir_create(struct repository *r,
 		chdir_notify_register(tmp_objdir_reparent, t);
 
 	if (!mkdtemp(t->path.buf)) {
+		saved_errno = errno;
 		/* free, not destroy, as we never touched the filesystem */
 		tmp_objdir_free(t);
-		return NULL;
+		goto error;
 	}
 
 	the_tmp_objdir = t;
@@ -188,10 +190,11 @@ struct tmp_objdir *tmp_objdir_create(struct repository *r,
 
 	path = xstrfmt("%s/pack", t->path.buf);
 	ret = mkdir(path, 0777);
+	saved_errno = errno;
 	free(path);
 	if (ret) {
 		tmp_objdir_destroy(t);
-		return NULL;
+		goto error;
 	}
 
 	env_append(&t->env, ALTERNATE_DB_ENVIRONMENT,
@@ -214,6 +217,10 @@ struct tmp_objdir *tmp_objdir_create(struct repository *r,
 	t->repo->disable_ref_updates = true;
 
 	return t;
+
+error:
+	errno = saved_errno;
+	return NULL;
 }
 
 /*
diff --git a/tmp-objdir.h b/tmp-objdir.h
index a301b01d84..4ef6750a01 100644
--- a/tmp-objdir.h
+++ b/tmp-objdir.h
@@ -26,7 +26,8 @@ struct tmp_objdir;
 
 /*
  * Create a new temporary object directory with the specified prefix and
- * install the directory as the primary write target; returns NULL on failure.
+ * install the directory as the primary write target; returns NULL and sets
+ * errno on failure.
  */
 struct tmp_objdir *tmp_objdir_create(struct repository *r, const char *prefix,
 				     int will_destroy);
-- 
2.56.0.173.gf57c9a7388
Previous: Justin ToblerNext: Justin Tobler
Message 24 of 29 in “object-file: eagerly create ODB transaction temporary directory”
  1. 0/4 object-file: eagerly create ODB transaction temporary directoryJustin Tobler, Nov 10, 2026
  2. 1/4 object-file: rename default transaction prefixJustin Tobler, Nov 10, 2026
  3. Patrick SteinhardtNov 11, 2026
  4. Karthik NayakNov 11, 2026
  5. 2/4 object-file: only flush batched fsync writes when enabledJustin Tobler, Nov 10, 2026
  6. Patrick SteinhardtNov 11, 2026
  7. Justin ToblerNov 17, 2026
  8. Karthik NayakNov 11, 2026
  9. Justin ToblerNov 17, 2026
  10. 3/4 tmp-objdir: preserve errno when directory creation failsJustin Tobler, Nov 10, 2026
  11. Patrick SteinhardtNov 11, 2026
  12. Karthik NayakNov 11, 2026
  13. Justin ToblerNov 17, 2026
  14. 4/4 object-file: create ODB transaction temporary directory eagerlyJustin Tobler, Nov 10, 2026
  15. Patrick SteinhardtNov 11, 2026
  16. Justin ToblerNov 17, 2026
  17. Karthik NayakNov 11, 2026
  18. Justin ToblerNov 17, 2026
  19. Karthik NayakNov 11, 2026
  20. 0/6 object-file: eagerly create ODB transaction temporary directoryJustin Tobler, Nov 19, 2026
  21. 1/6 object-file: rename default transaction prefixJustin Tobler, Nov 19, 2026
  22. 2/6 object-file: only flush batched fsync writes when enabledJustin Tobler, Nov 19, 2026
  23. 3/6 tmp-objdir: inline `setup_tmp_objdir()`Justin Tobler, Nov 19, 2026
  24. 4/6 tmp-objdir: preserve errno when directory creation failsJustin Tobler, Nov 19, 2026
  25. 5/6 odb/transaction: avoid "ODB" in error messagesJustin Tobler, Nov 19, 2026
  26. Patrick SteinhardtNov 19, 2026
  27. 6/6 object-file: create ODB transaction temporary directory eagerlyJustin Tobler, Nov 19, 2026
  28. Patrick SteinhardtNov 19, 2026
  29. Junio C HamanoNov 19, 2026

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.