Commit | Line | Data |
---|---|---|
021b6e45 JH |
1 | /* |
2 | * Copyright (c) 2005, Junio C Hamano | |
3 | */ | |
021b6e45 JH |
4 | #include "cache.h" |
5 | ||
6 | static struct lock_file *lock_file_list; | |
7 | ||
8 | static void remove_lock_file(void) | |
9 | { | |
10 | while (lock_file_list) { | |
11 | if (lock_file_list->filename[0]) | |
12 | unlink(lock_file_list->filename); | |
13 | lock_file_list = lock_file_list->next; | |
14 | } | |
15 | } | |
16 | ||
17 | static void remove_lock_file_on_signal(int signo) | |
18 | { | |
19 | remove_lock_file(); | |
20 | signal(SIGINT, SIG_DFL); | |
21 | raise(signo); | |
22 | } | |
23 | ||
40aaae88 | 24 | static int lock_file(struct lock_file *lk, const char *path) |
021b6e45 JH |
25 | { |
26 | int fd; | |
27 | sprintf(lk->filename, "%s.lock", path); | |
28 | fd = open(lk->filename, O_RDWR | O_CREAT | O_EXCL, 0666); | |
138086a7 | 29 | if (0 <= fd) { |
1084b845 | 30 | if (!lk->on_list) { |
138086a7 JH |
31 | lk->next = lock_file_list; |
32 | lock_file_list = lk; | |
1084b845 JH |
33 | lk->on_list = 1; |
34 | } | |
35 | if (lock_file_list) { | |
138086a7 JH |
36 | signal(SIGINT, remove_lock_file_on_signal); |
37 | atexit(remove_lock_file); | |
38 | } | |
39 | if (adjust_shared_perm(lk->filename)) | |
40 | return error("cannot fix permission bits on %s", | |
41 | lk->filename); | |
021b6e45 | 42 | } |
1084b845 JH |
43 | else |
44 | lk->filename[0] = 0; | |
021b6e45 JH |
45 | return fd; |
46 | } | |
47 | ||
40aaae88 JH |
48 | int hold_lock_file_for_update(struct lock_file *lk, const char *path, int die_on_error) |
49 | { | |
50 | int fd = lock_file(lk, path); | |
51 | if (fd < 0 && die_on_error) | |
f9e8a43a | 52 | die("unable to create '%s.lock': %s", path, strerror(errno)); |
40aaae88 JH |
53 | return fd; |
54 | } | |
55 | ||
021b6e45 JH |
56 | int commit_lock_file(struct lock_file *lk) |
57 | { | |
58 | char result_file[PATH_MAX]; | |
59 | int i; | |
60 | strcpy(result_file, lk->filename); | |
61 | i = strlen(result_file) - 5; /* .lock */ | |
62 | result_file[i] = 0; | |
63 | i = rename(lk->filename, result_file); | |
64 | lk->filename[0] = 0; | |
65 | return i; | |
66 | } | |
67 | ||
30ca07a2 JH |
68 | int hold_locked_index(struct lock_file *lk, int die_on_error) |
69 | { | |
70 | return hold_lock_file_for_update(lk, get_index_file(), die_on_error); | |
71 | } | |
72 | ||
73 | int commit_locked_index(struct lock_file *lk) | |
74 | { | |
75 | char *output = getenv(INDEX_OUTPUT_ENVIRONMENT); | |
76 | if (output && *output) { | |
77 | int result = rename(lk->filename, output); | |
78 | lk->filename[0] = 0; | |
79 | return result; | |
80 | } | |
81 | else | |
82 | return commit_lock_file(lk); | |
83 | } | |
84 | ||
021b6e45 JH |
85 | void rollback_lock_file(struct lock_file *lk) |
86 | { | |
87 | if (lk->filename[0]) | |
88 | unlink(lk->filename); | |
89 | lk->filename[0] = 0; | |
90 | } | |
91 |