Merge branches 'jc/sha1', 'jc/diff' and 'jc/ws'
[git/jrn.git] / index.c
blobf92b960ae46b2d43b2f46693fe086a4c4dbe3737
1 /*
2 * Copyright (c) 2005, Junio C Hamano
3 */
4 #include <signal.h>
5 #include "cache.h"
7 static struct cache_file *cache_file_list;
9 static void remove_lock_file(void)
11 while (cache_file_list) {
12 if (cache_file_list->lockfile[0])
13 unlink(cache_file_list->lockfile);
14 cache_file_list = cache_file_list->next;
18 static void remove_lock_file_on_signal(int signo)
20 remove_lock_file();
21 signal(SIGINT, SIG_DFL);
22 raise(signo);
25 int hold_index_file_for_update(struct cache_file *cf, const char *path)
27 int fd;
28 sprintf(cf->lockfile, "%s.lock", path);
29 fd = open(cf->lockfile, O_RDWR | O_CREAT | O_EXCL, 0666);
30 if (fd >=0 && !cf->next) {
31 cf->next = cache_file_list;
32 cache_file_list = cf;
33 signal(SIGINT, remove_lock_file_on_signal);
34 atexit(remove_lock_file);
36 return fd;
39 int commit_index_file(struct cache_file *cf)
41 char indexfile[PATH_MAX];
42 int i;
43 strcpy(indexfile, cf->lockfile);
44 i = strlen(indexfile) - 5; /* .lock */
45 indexfile[i] = 0;
46 i = rename(cf->lockfile, indexfile);
47 cf->lockfile[0] = 0;
48 return i;
51 void rollback_index_file(struct cache_file *cf)
53 if (cf->lockfile[0])
54 unlink(cf->lockfile);
55 cf->lockfile[0] = 0;