path: root/lockfile.h
diff options
authorJeff King <>2017-09-05 12:14:30 (GMT)
committerJunio C Hamano <>2017-09-06 08:19:53 (GMT)
commit49bd0fc2220eef17d8f5fd3ee76e391d03df8a6d (patch)
treefb867abaa9885a795b7cc34ff870211ac522302f /lockfile.h
parent45c6b1ed24724f7f3041a60a4313df7d9c4b9909 (diff)
tempfile: do not delete tempfile on failed close
When close_tempfile() fails, we delete the tempfile and reset the fields of the tempfile struct. This makes it easier for callers to return without cleaning up, but it also makes this common pattern: if (close_tempfile(tempfile)) return error_errno("error closing %s", tempfile->filename.buf); wrong, because the "filename" field has been reset after the failed close. And it's not easy to fix, as in many cases we don't have another copy of the filename (e.g., if it was created via one of the mks_tempfile functions, and we just have the original template string). Let's drop the feature that a failed close automatically deletes the file. This puts the burden on the caller to do the deletion themselves, but this isn't that big a deal. Callers which do: if (write(...) || close_tempfile(...)) { delete_tempfile(...); return -1; } already had to call delete when the write() failed, and so aren't affected. Likewise, any caller which just calls die() in the error path is OK; we'll delete the tempfile during the atexit handler. Because this patch changes the semantics of close_tempfile() without changing its signature, all callers need to be manually checked and converted to the new scheme. This patch covers all in-tree callers, but there may be others for not-yet-merged topics. To catch these, we rename the function to close_tempfile_gently(), which will attract compile-time attention to new callers. (Technically the original could be considered "gentle" already in that it didn't die() on errors, but this one is even more so). Signed-off-by: Jeff King <> Signed-off-by: Junio C Hamano <>
Diffstat (limited to 'lockfile.h')
1 files changed, 7 insertions, 1 deletions
diff --git a/lockfile.h b/lockfile.h
index 5720649..dd4259b 100644
--- a/lockfile.h
+++ b/lockfile.h
@@ -246,7 +246,13 @@ extern char *get_locked_file_path(struct lock_file *lk);
static inline int close_lock_file(struct lock_file *lk)
- return close_tempfile(&lk->tempfile);
+ int ret = close_tempfile_gently(&lk->tempfile);
+ if (ret) {
+ int saved_errno = errno;
+ delete_tempfile(&lk->tempfile);
+ errno = saved_errno;
+ }
+ return ret;