Skip to content

Commit 9ca7c2c

Browse files
ttaylorrpeff
authored andcommitted
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 <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d1b44bb commit 9ca7c2c

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

object-file.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1911,6 +1911,8 @@ int finalize_object_file(const char *tmpfile, const char *filename)
19111911
goto try_rename;
19121912
else if (link(tmpfile, filename))
19131913
ret = errno;
1914+
else
1915+
unlink_or_warn(tmpfile);
19141916

19151917
/*
19161918
* Coda hack - coda doesn't like cross-directory links,
@@ -1932,12 +1934,15 @@ int finalize_object_file(const char *tmpfile, const char *filename)
19321934
else
19331935
ret = errno;
19341936
}
1935-
unlink_or_warn(tmpfile);
19361937
if (ret) {
19371938
if (ret != EEXIST) {
1939+
int saved_errno = errno;
1940+
unlink_or_warn(tmpfile);
1941+
errno = saved_errno;
19381942
return error_errno(_("unable to write file %s"), filename);
19391943
}
19401944
/* FIXME!!! Collision check here ? */
1945+
unlink_or_warn(tmpfile);
19411946
}
19421947

19431948
out:

0 commit comments

Comments
 (0)