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