show-branch: Be nicer when running in a corrupt repository.
[git/jnareb-git/bp-gitweb.git] / index.c
blob87fc7b0387b818001fb0e53fdb713fe1bad4f822
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();
23 int hold_index_file_for_update(struct cache_file *cf, const char *path)
25 sprintf(cf->lockfile, "%s.lock", path);
26 cf->next = cache_file_list;
27 cache_file_list = cf;
28 if (!cf->next) {
29 signal(SIGINT, remove_lock_file_on_signal);
30 atexit(remove_lock_file);
32 return open(cf->lockfile, O_RDWR | O_CREAT | O_EXCL, 0600);
35 int commit_index_file(struct cache_file *cf)
37 char indexfile[PATH_MAX];
38 int i;
39 strcpy(indexfile, cf->lockfile);
40 i = strlen(indexfile) - 5; /* .lock */
41 indexfile[i] = 0;
42 i = rename(cf->lockfile, indexfile);
43 cf->lockfile[0] = 0;
44 return i;
47 void rollback_index_file(struct cache_file *cf)
49 if (cf->lockfile[0])
50 unlink(cf->lockfile);
51 cf->lockfile[0] = 0;