Skip to content

Commit f6ecc62

Browse files
mhaggergitster
authored andcommitted
write_shared_index(): use tempfile module
Signed-off-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9939715 commit f6ecc62

File tree

1 file changed

+6
-32
lines changed

1 file changed

+6
-32
lines changed

read-cache.c

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
#define NO_THE_INDEX_COMPATIBILITY_MACROS
77
#include "cache.h"
8+
#include "tempfile.h"
89
#include "lockfile.h"
910
#include "cache-tree.h"
1011
#include "refs.h"
@@ -2136,54 +2137,27 @@ static int write_split_index(struct index_state *istate,
21362137
return ret;
21372138
}
21382139

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;
21562141

21572142
static int write_shared_index(struct index_state *istate,
21582143
struct lock_file *lock, unsigned flags)
21592144
{
21602145
struct split_index *si = istate->split_index;
2161-
static int installed_handler;
21622146
int fd, ret;
21632147

2164-
temporary_sharedindex = git_pathdup("sharedindex_XXXXXX");
2165-
fd = mkstemp(temporary_sharedindex);
2148+
fd = mks_tempfile(&temporary_sharedindex, git_path("sharedindex_XXXXXX"));
21662149
if (fd < 0) {
2167-
free(temporary_sharedindex);
2168-
temporary_sharedindex = NULL;
21692150
hashclr(si->base_sha1);
21702151
return do_write_locked_index(istate, lock, flags);
21712152
}
2172-
if (!installed_handler) {
2173-
atexit(remove_temporary_sharedindex);
2174-
sigchain_push_common(remove_temporary_sharedindex_on_signal);
2175-
}
21762153
move_cache_to_base_index(istate);
21772154
ret = do_write_index(si->base, fd, 1);
2178-
close(fd);
21792155
if (ret) {
2180-
remove_temporary_sharedindex();
2156+
delete_tempfile(&temporary_sharedindex);
21812157
return ret;
21822158
}
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)));
21872161
if (!ret)
21882162
hashcpy(si->base_sha1, si->base->sha1);
21892163
return ret;

0 commit comments

Comments
 (0)