@matheusbsbs / matheus-bagliano / commits / 40aaae88ad
Fork 0

Better error message when we are unable to lock the index file

Most of the callers except the one in refs.c use the function to update the index file. Among the index writers, everybody except write-tree dies if they cannot open it for writing. This gives the function an extra argument, to tell it to die when it cannot create a new file as the lockfile. The only caller that does not have to die is write-tree, because updating the index for the cache-tree part is optional and not being able to do so does not affect the correctness. I think we do not have to be so careful and make the failure into die() the same way as other callers, but that would be a different patch. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Aug 12, 2006 at 01:03 UTC 40aaae88adfe05be435b7bd17093d49869f3276f
11 files changed +20 -32
builtin-add.c
+1 -3
@@ -93,9 +93,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
93
94 git_config(git_default_config);
95
96 - newfd = hold_lock_file_for_update(&lock_file, get_index_file());
97 - if (newfd < 0)
98 - die("unable to create new index file");
96 + newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1);
97
98 if (read_cache() < 0)
99 die("index file corrupt");
builtin-apply.c
+2 -5
@@ -2234,12 +2234,9 @@ static int apply_patch(int fd, const char *filename,
2234 apply = 0;
2235
2236 write_index = check_index && apply;
2237 - if (write_index && newfd < 0) {
2237 + if (write_index && newfd < 0)
2238 newfd = hold_lock_file_for_update(&lock_file,
2239 - get_index_file());
2240 - if (newfd < 0)
2241 - die("unable to create new index file");
2242 - }
2239 + get_index_file(), 1);
2240 if (check_index) {
2241 if (read_cache() < 0)
2242 die("unable to read index file");
builtin-mv.c
+1 -4
@@ -72,10 +72,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
72
73 git_config(git_default_config);
74
75 - newfd = hold_lock_file_for_update(&lock_file, get_index_file());
76 - if (newfd < 0)
77 - die("unable to create new index file");
78 -
75 + newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1);
76 if (read_cache() < 0)
77 die("index file corrupt");
78
builtin-read-tree.c
+1 -3
@@ -884,9 +884,7 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
884
885 git_config(git_default_config);
886
887 - newfd = hold_lock_file_for_update(&lock_file, get_index_file());
888 - if (newfd < 0)
889 - die("unable to create new index file");
887 + newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1);
888
889 git_config(git_default_config);
890
builtin-rm.c
+1 -3
@@ -52,9 +52,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
52
53 git_config(git_default_config);
54
55 - newfd = hold_lock_file_for_update(&lock_file, get_index_file());
56 - if (newfd < 0)
57 - die("unable to create new index file");
55 + newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1);
56
57 if (read_cache() < 0)
58 die("index file corrupt");
builtin-update-index.c
+1 -3
@@ -491,9 +491,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
491 /* We can't free this memory, it becomes part of a linked list parsed atexit() */
492 lock_file = xcalloc(1, sizeof(struct lock_file));
493
494 - newfd = hold_lock_file_for_update(lock_file, get_index_file());
495 - if (newfd < 0)
496 - die("unable to create new cachefile");
494 + newfd = hold_lock_file_for_update(lock_file, get_index_file(), 1);
495
496 entries = read_cache();
497 if (entries < 0)
builtin-write-tree.c
+1 -1
@@ -18,7 +18,7 @@ int write_tree(unsigned char *sha1, int missing_ok, const char *prefix)
18 /* We can't free this memory, it becomes part of a linked list parsed atexit() */
19 struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
20
21 - newfd = hold_lock_file_for_update(lock_file, get_index_file());
21 + newfd = hold_lock_file_for_update(lock_file, get_index_file(), 0);
22
23 entries = read_cache();
24 if (entries < 0)
cache.h
+1 -1
@@ -175,7 +175,7 @@ struct lock_file {
175 struct lock_file *next;
176 char filename[PATH_MAX];
177 };
178 -extern int hold_lock_file_for_update(struct lock_file *, const char *path);
178 +extern int hold_lock_file_for_update(struct lock_file *, const char *path, int);
179 extern int commit_lock_file(struct lock_file *);
180 extern void rollback_lock_file(struct lock_file *);
181
checkout-index.c
+1 -1
@@ -206,7 +206,7 @@ int main(int argc, char **argv)
206 state.refresh_cache = 1;
207 if (newfd < 0)
208 newfd = hold_lock_file_for_update
209 - (&lock_file, get_index_file());
209 + (&lock_file, get_index_file(), 1);
210 if (newfd < 0)
211 die("cannot open index.lock file.");
212 continue;
lockfile.c
+9 -1
@@ -22,7 +22,7 @@ static void remove_lock_file_on_signal(int signo)
22 raise(signo);
23 }
24
25 -int hold_lock_file_for_update(struct lock_file *lk, const char *path)
25 +static int lock_file(struct lock_file *lk, const char *path)
26 {
27 int fd;
28 sprintf(lk->filename, "%s.lock", path);
@@ -41,6 +41,14 @@ int hold_lock_file_for_update(struct lock_file *lk, const char *path)
41 return fd;
42 }
43
44 +int hold_lock_file_for_update(struct lock_file *lk, const char *path, int die_on_error)
45 +{
46 + int fd = lock_file(lk, path);
47 + if (fd < 0 && die_on_error)
48 + die("unable to create '%s': %s", path, strerror(errno));
49 + return fd;
50 +}
51 +
52 int commit_lock_file(struct lock_file *lk)
53 {
54 char result_file[PATH_MAX];
refs.c
+1 -7
@@ -319,13 +319,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *path,
319
320 if (safe_create_leading_directories(lock->ref_file))
321 die("unable to create directory for %s", lock->ref_file);
322 - lock->lock_fd = hold_lock_file_for_update(lock->lk, lock->ref_file);
323 - if (lock->lock_fd < 0) {
324 - error("Couldn't open lock file %s: %s",
325 - lock->lk->filename, strerror(errno));
326 - unlock_ref(lock);
327 - return NULL;
328 - }
322 + lock->lock_fd = hold_lock_file_for_update(lock->lk, lock->ref_file, 1);
323
324 return old_sha1 ? verify_lock(lock, old_sha1, mustexist) : lock;
325 }