aboutsummaryrefslogtreecommitdiff
path: root/index.c
blob: bdde65f75c9d2a9b77e6bf290ebed57f12c8e70b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
 * Copyright (c) 2005, Junio C Hamano
 */
#include <signal.h>
#include "cache.h"

static struct cache_file *cache_file_list;

static void remove_lock_file(void)
{
	while (cache_file_list) {
		if (cache_file_list->lockfile[0])
			unlink(cache_file_list->lockfile);
		cache_file_list = cache_file_list->next;
	}
}

static void remove_lock_file_on_signal(int signo)
{
	remove_lock_file();
}

int hold_index_file_for_update(struct cache_file *cf, const char *path)
{
	sprintf(cf->lockfile, "%s.lock", path);
	cf->next = cache_file_list;
	cache_file_list = cf;
	if (!cf->next) {
		signal(SIGINT, remove_lock_file_on_signal);
		atexit(remove_lock_file);
	}
	return open(cf->lockfile, O_RDWR | O_CREAT | O_EXCL, 0666);
}

int commit_index_file(struct cache_file *cf)
{
	char indexfile[PATH_MAX];
	int i;
	strcpy(indexfile, cf->lockfile);
	i = strlen(indexfile) - 5; /* .lock */
	indexfile[i] = 0;
	i = rename(cf->lockfile, indexfile);
	cf->lockfile[0] = 0;
	return i;
}

void rollback_index_file(struct cache_file *cf)
{
	if (cf->lockfile[0])
		unlink(cf->lockfile);
	cf->lockfile[0] = 0;
}