@matheusbsbs / matheus-bagliano / commits / 021b6e4549
Fork 0

Make index file locking code reusable to others.

The framework to create lockfiles that are removed at exit is first used to reliably write the index file, but it is applicable to other things, so stop calling it "cache_file". This also rewords a few remaining error message that called the index file "cache file". Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Jun 6, 2006 at 12:51 UTC 021b6e454944a4fba878651ebf9bfe0a3f6c3077
11 files changed +98 -95
Makefile
+1 -1
@@ -210,7 +210,7 @@ DIFF_OBJS = \
210
211 LIB_OBJS = \
212 blob.o commit.o connect.o csum-file.o cache-tree.o base85.o \
213 - date.o diff-delta.o entry.o exec_cmd.o ident.o index.o \
213 + date.o diff-delta.o entry.o exec_cmd.o ident.o lockfile.o \
214 object.o pack-check.o patch-delta.o path.o pkt-line.o \
215 quote.o read-cache.o refs.o run-command.o dir.o \
216 server-info.o setup.o sha1_file.o sha1_name.o strbuf.o \
builtin-add.c
+4 -4
@@ -122,7 +122,7 @@ static int add_file_to_index(const char *path, int verbose)
122 return 0;
123 }
124
125 -static struct cache_file cache_file;
125 +static struct lock_file lock_file;
126
127 int cmd_add(int argc, const char **argv, char **envp)
128 {
@@ -134,9 +134,9 @@ int cmd_add(int argc, const char **argv, char **envp)
134
135 git_config(git_default_config);
136
137 - newfd = hold_index_file_for_update(&cache_file, get_index_file());
137 + newfd = hold_lock_file_for_update(&lock_file, get_index_file());
138 if (newfd < 0)
139 - die("unable to create new cachefile");
139 + die("unable to create new index file");
140
141 if (read_cache() < 0)
142 die("index file corrupt");
@@ -181,7 +181,7 @@ int cmd_add(int argc, const char **argv, char **envp)
181
182 if (active_cache_changed) {
183 if (write_cache(newfd, active_cache, active_nr) ||
184 - commit_index_file(&cache_file))
184 + commit_lock_file(&lock_file))
185 die("Unable to write new index file");
186 }
187
builtin-apply.c
+9 -5
@@ -2072,7 +2072,7 @@ static void write_out_results(struct patch *list, int skipped_patch)
2072 }
2073 }
2074
2075 -static struct cache_file cache_file;
2075 +static struct lock_file lock_file;
2076
2077 static struct excludes {
2078 struct excludes *next;
@@ -2133,8 +2133,12 @@ static int apply_patch(int fd, const char *filename)
2133 apply = 0;
2134
2135 write_index = check_index && apply;
2136 - if (write_index && newfd < 0)
2137 - newfd = hold_index_file_for_update(&cache_file, get_index_file());
2136 + if (write_index && newfd < 0) {
2137 + newfd = hold_lock_file_for_update(&lock_file,
2138 + get_index_file());
2139 + if (newfd < 0)
2140 + die("unable to create new index file");
2141 + }
2142 if (check_index) {
2143 if (read_cache() < 0)
2144 die("unable to read index file");
@@ -2312,8 +2316,8 @@ int cmd_apply(int argc, const char **argv, char **envp)
2316
2317 if (write_index) {
2318 if (write_cache(newfd, active_cache, active_nr) ||
2315 - commit_index_file(&cache_file))
2316 - die("Unable to write new cachefile");
2319 + commit_lock_file(&lock_file))
2320 + die("Unable to write new index file");
2321 }
2322
2323 return 0;
builtin-read-tree.c
+4 -4
@@ -853,7 +853,7 @@ static void prime_cache_tree(void)
853
854 static const char read_tree_usage[] = "git-read-tree (<sha> | -m [--aggressive] [-u | -i] <sha1> [<sha2> [<sha3>]])";
855
856 -static struct cache_file cache_file;
856 +static struct lock_file lock_file;
857
858 int cmd_read_tree(int argc, const char **argv, char **envp)
859 {
@@ -864,9 +864,9 @@ int cmd_read_tree(int argc, const char **argv, char **envp)
864 setup_git_directory();
865 git_config(git_default_config);
866
867 - newfd = hold_index_file_for_update(&cache_file, get_index_file());
867 + newfd = hold_lock_file_for_update(&lock_file, get_index_file());
868 if (newfd < 0)
869 - die("unable to create new cachefile");
869 + die("unable to create new index file");
870
871 git_config(git_default_config);
872
@@ -981,7 +981,7 @@ int cmd_read_tree(int argc, const char **argv, char **envp)
981 }
982
983 if (write_cache(newfd, active_cache, active_nr) ||
984 - commit_index_file(&cache_file))
984 + commit_lock_file(&lock_file))
985 die("unable to write new index file");
986 return 0;
987 }
builtin-rm.c
+3 -3
@@ -41,7 +41,7 @@ static int remove_file(const char *name)
41 return ret;
42 }
43
44 -static struct cache_file cache_file;
44 +static struct lock_file lock_file;
45
46 int cmd_rm(int argc, const char **argv, char **envp)
47 {
@@ -53,7 +53,7 @@ int cmd_rm(int argc, const char **argv, char **envp)
53
54 git_config(git_default_config);
55
56 - newfd = hold_index_file_for_update(&cache_file, get_index_file());
56 + newfd = hold_lock_file_for_update(&lock_file, get_index_file());
57 if (newfd < 0)
58 die("unable to create new index file");
59
@@ -144,7 +144,7 @@ int cmd_rm(int argc, const char **argv, char **envp)
144
145 if (active_cache_changed) {
146 if (write_cache(newfd, active_cache, active_nr) ||
147 - commit_index_file(&cache_file))
147 + commit_lock_file(&lock_file))
148 die("Unable to write new index file");
149 }
150
cache.h
+6 -6
@@ -167,13 +167,13 @@ extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);
167 #define REFRESH_IGNORE_MISSING 0x0008 /* ignore non-existent */
168 extern int refresh_cache(unsigned int flags);
169
170 -struct cache_file {
171 - struct cache_file *next;
172 - char lockfile[PATH_MAX];
170 +struct lock_file {
171 + struct lock_file *next;
172 + char filename[PATH_MAX];
173 };
174 -extern int hold_index_file_for_update(struct cache_file *, const char *path);
175 -extern int commit_index_file(struct cache_file *);
176 -extern void rollback_index_file(struct cache_file *);
174 +extern int hold_lock_file_for_update(struct lock_file *, const char *path);
175 +extern int commit_lock_file(struct lock_file *);
176 +extern void rollback_lock_file(struct lock_file *);
177
178 /* Environment bits from configuration mechanism */
179 extern int trust_executable_bit;
checkout-index.c
+6 -7
@@ -168,7 +168,7 @@ static int checkout_all(void)
168 static const char checkout_cache_usage[] =
169 "git-checkout-index [-u] [-q] [-a] [-f] [-n] [--stage=[123]|all] [--prefix=<string>] [--temp] [--] <file>...";
170
171 -static struct cache_file cache_file;
171 +static struct lock_file lock_file;
172
173 int main(int argc, char **argv)
174 {
@@ -211,9 +211,8 @@ int main(int argc, char **argv)
211 if (!strcmp(arg, "-u") || !strcmp(arg, "--index")) {
212 state.refresh_cache = 1;
213 if (newfd < 0)
214 - newfd = hold_index_file_for_update
215 - (&cache_file,
216 - get_index_file());
214 + newfd = hold_lock_file_for_update
215 + (&lock_file, get_index_file());
216 if (newfd < 0)
217 die("cannot open index.lock file.");
218 continue;
@@ -262,7 +261,7 @@ int main(int argc, char **argv)
261 */
262 if (state.refresh_cache) {
263 close(newfd); newfd = -1;
265 - rollback_index_file(&cache_file);
264 + rollback_lock_file(&lock_file);
265 }
266 state.refresh_cache = 0;
267 }
@@ -312,7 +311,7 @@ int main(int argc, char **argv)
311
312 if (0 <= newfd &&
313 (write_cache(newfd, active_cache, active_nr) ||
315 - commit_index_file(&cache_file)))
316 - die("Unable to write new cachefile");
314 + commit_lock_file(&lock_file)))
315 + die("Unable to write new index file");
316 return 0;
317 }
index.c deleted
-57
@@ -1,57 +0,0 @@
1 -/*
2 - * Copyright (c) 2005, Junio C Hamano
3 - */
4 -#include <signal.h>
5 -#include "cache.h"
6 -
7 -static struct cache_file *cache_file_list;
8 -
9 -static void remove_lock_file(void)
10 -{
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;
15 - }
16 -}
17 -
18 -static void remove_lock_file_on_signal(int signo)
19 -{
20 - remove_lock_file();
21 - signal(SIGINT, SIG_DFL);
22 - raise(signo);
23 -}
24 -
25 -int hold_index_file_for_update(struct cache_file *cf, const char *path)
26 -{
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);
35 - }
36 - return fd;
37 -}
38 -
39 -int commit_index_file(struct cache_file *cf)
40 -{
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;
49 -}
50 -
51 -void rollback_index_file(struct cache_file *cf)
52 -{
53 - if (cf->lockfile[0])
54 - unlink(cf->lockfile);
55 - cf->lockfile[0] = 0;
56 -}
57 -
lockfile.c new
+57
@@ -0,0 +1,57 @@
1 +/*
2 + * Copyright (c) 2005, Junio C Hamano
3 + */
4 +#include <signal.h>
5 +#include "cache.h"
6 +
7 +static struct lock_file *lock_file_list;
8 +
9 +static 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 +
18 +static void remove_lock_file_on_signal(int signo)
19 +{
20 + remove_lock_file();
21 + signal(SIGINT, SIG_DFL);
22 + raise(signo);
23 +}
24 +
25 +int hold_lock_file_for_update(struct lock_file *lk, const char *path)
26 +{
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);
35 + }
36 + return fd;
37 +}
38 +
39 +int commit_lock_file(struct lock_file *lk)
40 +{
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;
49 +}
50 +
51 +void rollback_lock_file(struct lock_file *lk)
52 +{
53 + if (lk->filename[0])
54 + unlink(lk->filename);
55 + lk->filename[0] = 0;
56 +}
57 +
update-index.c
+5 -5
@@ -186,7 +186,7 @@ static void chmod_path(int flip, const char *path)
186 die("git-update-index: cannot chmod %cx '%s'", flip, path);
187 }
188
189 -static struct cache_file cache_file;
189 +static struct lock_file lock_file;
190
191 static void update_one(const char *path, const char *prefix, int prefix_length)
192 {
@@ -489,9 +489,9 @@ int main(int argc, const char **argv)
489
490 git_config(git_default_config);
491
492 - newfd = hold_index_file_for_update(&cache_file, get_index_file());
492 + newfd = hold_lock_file_for_update(&lock_file, get_index_file());
493 if (newfd < 0)
494 - die("unable to create new cachefile");
494 + die("unable to create new index file");
495
496 entries = read_cache();
497 if (entries < 0)
@@ -645,8 +645,8 @@ int main(int argc, const char **argv)
645 finish:
646 if (active_cache_changed) {
647 if (write_cache(newfd, active_cache, active_nr) ||
648 - commit_index_file(&cache_file))
649 - die("Unable to write new cachefile");
648 + commit_lock_file(&lock_file))
649 + die("Unable to write new index file");
650 }
651
652 return has_errors ? 1 : 0;
write-tree.c
+3 -3
@@ -11,7 +11,7 @@ static int missing_ok = 0;
11
12 static const char write_tree_usage[] = "git-write-tree [--missing-ok]";
13
14 -static struct cache_file cache_file;
14 +static struct lock_file lock_file;
15
16 int main(int argc, char **argv)
17 {
@@ -19,7 +19,7 @@ int main(int argc, char **argv)
19
20 setup_git_directory();
21
22 - newfd = hold_index_file_for_update(&cache_file, get_index_file());
22 + newfd = hold_lock_file_for_update(&lock_file, get_index_file());
23 entries = read_cache();
24 if (argc == 2) {
25 if (!strcmp(argv[1], "--missing-ok"))
@@ -45,7 +45,7 @@ int main(int argc, char **argv)
45 die("git-write-tree: error building trees");
46 if (0 <= newfd) {
47 if (!write_cache(newfd, active_cache, active_nr))
48 - commit_index_file(&cache_file);
48 + commit_lock_file(&lock_file);
49 }
50 /* Not being able to write is fine -- we are only interested
51 * in updating the cache-tree part, and if the next caller