diff options
author | Jeff King <peff@peff.net> | 2017-09-05 08:14:30 -0400 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2017-09-06 17:19:53 +0900 |
commit | 49bd0fc2220eef17d8f5fd3ee76e391d03df8a6d (patch) | |
tree | fb867abaa9885a795b7cc34ff870211ac522302f /tempfile.h | |
parent | always check return value of close_tempfile (diff) | |
download | tgif-49bd0fc2220eef17d8f5fd3ee76e391d03df8a6d.tar.xz |
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 <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'tempfile.h')
-rw-r--r-- | tempfile.h | 25 |
1 files changed, 13 insertions, 12 deletions
diff --git a/tempfile.h b/tempfile.h index 2f0038decd..d854dcdd3e 100644 --- a/tempfile.h +++ b/tempfile.h @@ -47,7 +47,7 @@ * control of the file. * * * Close the file descriptor without removing or renaming the - * temporary file by calling `close_tempfile()`, and later call + * temporary file by calling `close_tempfile_gently()`, and later call * `delete_tempfile()` or `rename_tempfile()`. * * Even after the temporary file is renamed or deleted, the `tempfile` @@ -59,7 +59,7 @@ * and remove the temporary file. * * If you need to close the file descriptor yourself, do so by calling - * `close_tempfile()`. You should never call `close(2)` or `fclose(3)` + * `close_tempfile_gently()`. You should never call `close(2)` or `fclose(3)` * yourself, otherwise the `struct tempfile` structure would still * think that the file descriptor needs to be closed, and a later * cleanup would result in duplicate calls to `close(2)`. Worse yet, @@ -74,9 +74,10 @@ * `create_tempfile()` returns a file descriptor on success or -1 on * failure. On errors, `errno` describes the reason for failure. * - * `delete_tempfile()`, `rename_tempfile()`, and `close_tempfile()` - * return 0 on success. On failure they set `errno` appropriately, do - * their best to delete the temporary file, and return -1. + * `delete_tempfile()`, `rename_tempfile()`, and `close_tempfile_gently()` + * return 0 on success. On failure they set `errno` appropriately and return + * -1. `delete` and `rename` (but not `close`) do their best to delete the + * temporary file before returning. */ struct tempfile { @@ -203,7 +204,7 @@ static inline int xmks_tempfile(struct tempfile *tempfile, /* * Associate a stdio stream with the temporary file (which must still * be open). Return `NULL` (*without* deleting the file) on error. The - * stream is closed automatically when `close_tempfile()` is called or + * stream is closed automatically when `close_tempfile_gently()` is called or * when the file is deleted or renamed. */ extern FILE *fdopen_tempfile(struct tempfile *tempfile, const char *mode); @@ -226,20 +227,20 @@ extern FILE *get_tempfile_fp(struct tempfile *tempfile); * If the temporary file is still open, close it (and the file pointer * too, if it has been opened using `fdopen_tempfile()`) without * deleting the file. Return 0 upon success. On failure to `close(2)`, - * return a negative value and delete the file. Usually - * `delete_tempfile()` or `rename_tempfile()` should eventually be - * called if `close_tempfile()` succeeds. + * return a negative value. Usually `delete_tempfile()` or `rename_tempfile()` + * should eventually be called regardless of whether `close_tempfile_gently()` + * succeeds. */ -extern int close_tempfile(struct tempfile *tempfile); +extern int close_tempfile_gently(struct tempfile *tempfile); /* * Re-open a temporary file that has been closed using - * `close_tempfile()` but not yet deleted or renamed. This can be used + * `close_tempfile_gently()` but not yet deleted or renamed. This can be used * to implement a sequence of operations like the following: * * * Create temporary file. * - * * Write new contents to file, then `close_tempfile()` to cause the + * * Write new contents to file, then `close_tempfile_gently()` to cause the * contents to be written to disk. * * * Pass the name of the temporary file to another program to allow |