Skip to content

Commit 40aaae8

Browse files
author
Junio C Hamano
committed
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 <[email protected]>
1 parent fd7bcfb commit 40aaae8

11 files changed

+20
-32
lines changed

builtin-add.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
9393

9494
git_config(git_default_config);
9595

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);
9997

10098
if (read_cache() < 0)
10199
die("index file corrupt");

builtin-apply.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2234,12 +2234,9 @@ static int apply_patch(int fd, const char *filename,
22342234
apply = 0;
22352235

22362236
write_index = check_index && apply;
2237-
if (write_index && newfd < 0) {
2237+
if (write_index && newfd < 0)
22382238
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);
22432240
if (check_index) {
22442241
if (read_cache() < 0)
22452242
die("unable to read index file");

builtin-mv.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
7272

7373
git_config(git_default_config);
7474

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);
7976
if (read_cache() < 0)
8077
die("index file corrupt");
8178

builtin-read-tree.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -884,9 +884,7 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
884884

885885
git_config(git_default_config);
886886

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);
890888

891889
git_config(git_default_config);
892890

builtin-rm.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
5252

5353
git_config(git_default_config);
5454

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);
5856

5957
if (read_cache() < 0)
6058
die("index file corrupt");

builtin-update-index.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -491,9 +491,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
491491
/* We can't free this memory, it becomes part of a linked list parsed atexit() */
492492
lock_file = xcalloc(1, sizeof(struct lock_file));
493493

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);
497495

498496
entries = read_cache();
499497
if (entries < 0)

builtin-write-tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ int write_tree(unsigned char *sha1, int missing_ok, const char *prefix)
1818
/* We can't free this memory, it becomes part of a linked list parsed atexit() */
1919
struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
2020

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);
2222

2323
entries = read_cache();
2424
if (entries < 0)

cache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ struct lock_file {
175175
struct lock_file *next;
176176
char filename[PATH_MAX];
177177
};
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);
179179
extern int commit_lock_file(struct lock_file *);
180180
extern void rollback_lock_file(struct lock_file *);
181181

checkout-index.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ int main(int argc, char **argv)
206206
state.refresh_cache = 1;
207207
if (newfd < 0)
208208
newfd = hold_lock_file_for_update
209-
(&lock_file, get_index_file());
209+
(&lock_file, get_index_file(), 1);
210210
if (newfd < 0)
211211
die("cannot open index.lock file.");
212212
continue;

lockfile.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ static void remove_lock_file_on_signal(int signo)
2222
raise(signo);
2323
}
2424

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)
2626
{
2727
int fd;
2828
sprintf(lk->filename, "%s.lock", path);
@@ -41,6 +41,14 @@ int hold_lock_file_for_update(struct lock_file *lk, const char *path)
4141
return fd;
4242
}
4343

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+
4452
int commit_lock_file(struct lock_file *lk)
4553
{
4654
char result_file[PATH_MAX];

0 commit comments

Comments
 (0)