Documentation: add another example to git-ls-files
[git/jrn.git] / lockfile.c
blob9bc60837aa12fbe2e659ff6fb5b52e862fbe03c3
1 /*
2 * Copyright (c) 2005, Junio C Hamano
3 */
4 #include <signal.h>
5 #include "cache.h"
7 static struct lock_file *lock_file_list;
9 static void remove_lock_file(void)
11 while (lock_file_list) {
12 if (lock_file_list->filename[0])
13 unlink(lock_file_list->filename);
14 lock_file_list = lock_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_lock_file_for_update(struct lock_file *lk, const char *path)
27 int fd;
28 sprintf(lk->filename, "%s.lock", path);
29 fd = open(lk->filename, O_RDWR | O_CREAT | O_EXCL, 0666);
30 if (fd >=0 && !lk->next) {
31 lk->next = lock_file_list;
32 lock_file_list = lk;
33 signal(SIGINT, remove_lock_file_on_signal);
34 atexit(remove_lock_file);
36 return fd;
39 int commit_lock_file(struct lock_file *lk)
41 char result_file[PATH_MAX];
42 int i;
43 strcpy(result_file, lk->filename);
44 i = strlen(result_file) - 5; /* .lock */
45 result_file[i] = 0;
46 i = rename(lk->filename, result_file);
47 lk->filename[0] = 0;
48 return i;
51 void rollback_lock_file(struct lock_file *lk)
53 if (lk->filename[0])
54 unlink(lk->filename);
55 lk->filename[0] = 0;