From: Justin Tobler Date: Tue, 10 Nov 2026 00:22:50 GMT Subject: [PATCH 3/4] tmp-objdir: preserve errno when directory creation fails Message-ID: <20261110002251.2007617-4-jltobler@gmail.com> In-Reply-To: <20261110002251.2007617-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 --- tmp-objdir.c | 8 ++++++++ tmp-objdir.h | 3 ++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/tmp-objdir.c b/tmp-objdir.c index 5307717354..4b91d86787 100644 --- a/tmp-objdir.c +++ b/tmp-objdir.c @@ -148,10 +148,13 @@ static int setup_tmp_objdir(const char *root) { char *path; int ret = 0; + int saved_errno; path = xstrfmt("%s/pack", root); ret = mkdir(path, 0777); + saved_errno = errno; free(path); + errno = saved_errno; return ret; } @@ -163,6 +166,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; if (the_tmp_objdir) BUG("only one tmp_objdir can be used at a time"); @@ -185,8 +189,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); + errno = saved_errno; return NULL; } @@ -197,7 +203,9 @@ struct tmp_objdir *tmp_objdir_create(struct repository *r, } if (setup_tmp_objdir(t->path.buf)) { + saved_errno = errno; tmp_objdir_destroy(t); + 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