Commit d030bba9fa64c4363368f9e3e0ed3f115dc06dc2

Edward Thomson 2017-01-21T17:15:33

indexer: only delete temp file if it was unused Only try to `unlink` our temp file when we know that we didn't copy it into its permanent location.

diff --git a/src/indexer.c b/src/indexer.c
index 3d1c81c..aa56460 100644
--- a/src/indexer.c
+++ b/src/indexer.c
@@ -33,7 +33,7 @@ struct entry {
 
 struct git_indexer {
 	unsigned int parsed_header :1,
-		opened_pack :1,
+		pack_committed :1,
 		have_stream :1,
 		have_delta :1;
 	struct git_pack_header hdr;
@@ -1060,6 +1060,7 @@ int git_indexer_commit(git_indexer *idx, git_transfer_progress *stats)
 
 	/* And don't forget to rename the packfile to its new place. */
 	p_rename(idx->pack->pack_name, git_buf_cstr(&filename));
+	idx->pack_committed = 1;
 
 	git_buf_free(&filename);
 	git_hash_ctx_cleanup(&ctx);
@@ -1092,12 +1093,13 @@ void git_indexer_free(git_indexer *idx)
 
 	/* Try to delete the temporary file in case it was not committed. */
 	git_mwindow_free_all(&idx->pack->mwf);
+
 	/* We need to close the descriptor here so Windows doesn't choke on unlink */
-	if (idx->pack->mwf.fd != -1) {
+	if (idx->pack->mwf.fd != -1)
 		p_close(idx->pack->mwf.fd);
-		idx->pack->mwf.fd = -1;
-	}
-	p_unlink(idx->pack->pack_name);
+
+	if (!idx->pack_committed)
+		p_unlink(idx->pack->pack_name);
 
 	if (!git_mutex_lock(&git__mwindow_mutex)) {
 		git_packfile_free(idx->pack);