diff options
author | Taylor Blau <me@ttaylorr.com> | 2024-09-26 17:22:35 +0200 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2024-09-27 20:27:46 +0200 |
commit | 9ca7c2c13bc26afe673172a9b417a43519381968 (patch) | |
tree | 0e9142936c1715f12bd6952c208752f8a630a75f /object-file.c | |
parent | finalize_object_file(): check for name collision before renaming (diff) | |
download | git-9ca7c2c13bc26afe673172a9b417a43519381968.tar.xz git-9ca7c2c13bc26afe673172a9b417a43519381968.zip |
finalize_object_file(): refactor unlink_or_warn() placement
As soon as we've tried to link() a temporary object into place, we then
unlink() the tempfile immediately, whether we were successful or not.
For the success case, this is because we no longer need the old file
(it's now linked into place).
For the error case, there are two outcomes. Either we got EEXIST, in
which case we consider the collision to be a noop. Or we got a system
error, in which we case we are just cleaning up after ourselves.
Using a single line for all of these cases has some problems:
- in the error case, our unlink() may clobber errno, which we use in
the error message
- for the collision case, there's a FIXME that indicates we should do
a collision check. In preparation for implementing that, we'll need
to actually hold on to the file.
Split these three cases into their own calls to unlink_or_warn(). This
is more verbose, but lets us do the right thing in each case.
Co-authored-by: Jeff King <peff@peff.net>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Taylor Blau <me@ttaylorr.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'object-file.c')
-rw-r--r-- | object-file.c | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/object-file.c b/object-file.c index 683e6b2a0b..54a82a5f7a 100644 --- a/object-file.c +++ b/object-file.c @@ -1911,6 +1911,8 @@ int finalize_object_file(const char *tmpfile, const char *filename) goto try_rename; else if (link(tmpfile, filename)) ret = errno; + else + unlink_or_warn(tmpfile); /* * Coda hack - coda doesn't like cross-directory links, @@ -1932,12 +1934,15 @@ int finalize_object_file(const char *tmpfile, const char *filename) else ret = errno; } - unlink_or_warn(tmpfile); if (ret) { if (ret != EEXIST) { + int saved_errno = errno; + unlink_or_warn(tmpfile); + errno = saved_errno; return error_errno(_("unable to write file %s"), filename); } /* FIXME!!! Collision check here ? */ + unlink_or_warn(tmpfile); } out: |