write_shared_index(): use tempfile module
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
99397152a3
commit
f6ecc62dbf
38
read-cache.c
38
read-cache.c
@ -5,6 +5,7 @@
|
|||||||
*/
|
*/
|
||||||
#define NO_THE_INDEX_COMPATIBILITY_MACROS
|
#define NO_THE_INDEX_COMPATIBILITY_MACROS
|
||||||
#include "cache.h"
|
#include "cache.h"
|
||||||
|
#include "tempfile.h"
|
||||||
#include "lockfile.h"
|
#include "lockfile.h"
|
||||||
#include "cache-tree.h"
|
#include "cache-tree.h"
|
||||||
#include "refs.h"
|
#include "refs.h"
|
||||||
@ -2136,54 +2137,27 @@ static int write_split_index(struct index_state *istate,
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *temporary_sharedindex;
|
static struct tempfile temporary_sharedindex;
|
||||||
|
|
||||||
static void remove_temporary_sharedindex(void)
|
|
||||||
{
|
|
||||||
if (temporary_sharedindex) {
|
|
||||||
unlink_or_warn(temporary_sharedindex);
|
|
||||||
free(temporary_sharedindex);
|
|
||||||
temporary_sharedindex = NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void remove_temporary_sharedindex_on_signal(int signo)
|
|
||||||
{
|
|
||||||
remove_temporary_sharedindex();
|
|
||||||
sigchain_pop(signo);
|
|
||||||
raise(signo);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int write_shared_index(struct index_state *istate,
|
static int write_shared_index(struct index_state *istate,
|
||||||
struct lock_file *lock, unsigned flags)
|
struct lock_file *lock, unsigned flags)
|
||||||
{
|
{
|
||||||
struct split_index *si = istate->split_index;
|
struct split_index *si = istate->split_index;
|
||||||
static int installed_handler;
|
|
||||||
int fd, ret;
|
int fd, ret;
|
||||||
|
|
||||||
temporary_sharedindex = git_pathdup("sharedindex_XXXXXX");
|
fd = mks_tempfile(&temporary_sharedindex, git_path("sharedindex_XXXXXX"));
|
||||||
fd = mkstemp(temporary_sharedindex);
|
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
free(temporary_sharedindex);
|
|
||||||
temporary_sharedindex = NULL;
|
|
||||||
hashclr(si->base_sha1);
|
hashclr(si->base_sha1);
|
||||||
return do_write_locked_index(istate, lock, flags);
|
return do_write_locked_index(istate, lock, flags);
|
||||||
}
|
}
|
||||||
if (!installed_handler) {
|
|
||||||
atexit(remove_temporary_sharedindex);
|
|
||||||
sigchain_push_common(remove_temporary_sharedindex_on_signal);
|
|
||||||
}
|
|
||||||
move_cache_to_base_index(istate);
|
move_cache_to_base_index(istate);
|
||||||
ret = do_write_index(si->base, fd, 1);
|
ret = do_write_index(si->base, fd, 1);
|
||||||
close(fd);
|
|
||||||
if (ret) {
|
if (ret) {
|
||||||
remove_temporary_sharedindex();
|
delete_tempfile(&temporary_sharedindex);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
ret = rename(temporary_sharedindex,
|
ret = rename_tempfile(&temporary_sharedindex,
|
||||||
git_path("sharedindex.%s", sha1_to_hex(si->base->sha1)));
|
git_path("sharedindex.%s", sha1_to_hex(si->base->sha1)));
|
||||||
free(temporary_sharedindex);
|
|
||||||
temporary_sharedindex = NULL;
|
|
||||||
if (!ret)
|
if (!ret)
|
||||||
hashcpy(si->base_sha1, si->base->sha1);
|
hashcpy(si->base_sha1, si->base->sha1);
|
||||||
return ret;
|
return ret;
|
||||||
|
Loading…
Reference in New Issue
Block a user