write_shared_index(): use tempfile module

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Aug 10, 2015 at 11:47 UTC f6ecc62dbfbd2a75a65782d8936aacf83daee1e2
1 file changed +6 -32
read-cache.c
+6 -32
@@ -5,6 +5,7 @@
5 */
6 #define NO_THE_INDEX_COMPATIBILITY_MACROS
7 #include "cache.h"
8 +#include "tempfile.h"
9 #include "lockfile.h"
10 #include "cache-tree.h"
11 #include "refs.h"
@@ -2136,54 +2137,27 @@ static int write_split_index(struct index_state *istate,
2137 return ret;
2138 }
2139
2139 -static char *temporary_sharedindex;
2140 -
2141 -static void remove_temporary_sharedindex(void)
2142 -{
2143 - if (temporary_sharedindex) {
2144 - unlink_or_warn(temporary_sharedindex);
2145 - free(temporary_sharedindex);
2146 - temporary_sharedindex = NULL;
2147 - }
2148 -}
2149 -
2150 -static void remove_temporary_sharedindex_on_signal(int signo)
2151 -{
2152 - remove_temporary_sharedindex();
2153 - sigchain_pop(signo);
2154 - raise(signo);
2155 -}
2140 +static struct tempfile temporary_sharedindex;
2141
2142 static int write_shared_index(struct index_state *istate,
2143 struct lock_file *lock, unsigned flags)
2144 {
2145 struct split_index *si = istate->split_index;
2161 - static int installed_handler;
2146 int fd, ret;
2147
2164 - temporary_sharedindex = git_pathdup("sharedindex_XXXXXX");
2165 - fd = mkstemp(temporary_sharedindex);
2148 + fd = mks_tempfile(&temporary_sharedindex, git_path("sharedindex_XXXXXX"));
2149 if (fd < 0) {
2167 - free(temporary_sharedindex);
2168 - temporary_sharedindex = NULL;
2150 hashclr(si->base_sha1);
2151 return do_write_locked_index(istate, lock, flags);
2152 }
2172 - if (!installed_handler) {
2173 - atexit(remove_temporary_sharedindex);
2174 - sigchain_push_common(remove_temporary_sharedindex_on_signal);
2175 - }
2153 move_cache_to_base_index(istate);
2154 ret = do_write_index(si->base, fd, 1);
2178 - close(fd);
2155 if (ret) {
2180 - remove_temporary_sharedindex();
2156 + delete_tempfile(&temporary_sharedindex);
2157 return ret;
2158 }
2183 - ret = rename(temporary_sharedindex,
2184 - git_path("sharedindex.%s", sha1_to_hex(si->base->sha1)));
2185 - free(temporary_sharedindex);
2186 - temporary_sharedindex = NULL;
2159 + ret = rename_tempfile(&temporary_sharedindex,
2160 + git_path("sharedindex.%s", sha1_to_hex(si->base->sha1)));
2161 if (!ret)
2162 hashcpy(si->base_sha1, si->base->sha1);
2163 return ret;