@matheusbsbs / matheus-bagliano / commits / 4723ee992c
Fork 0

Close files opened by lock_file() before unlinking.

This is needed on Windows since open files cannot be unlinked. Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Nov 13, 2007 at 21:05 UTC 4723ee992cba052dc735b7d2b6f43aad26d9150a
2 files changed +11 -7
cache.h
+1
@@ -290,6 +290,7 @@ extern int refresh_index(struct index_state *, unsigned int flags, const char **
290
291 struct lock_file {
292 struct lock_file *next;
293 + int fd;
294 pid_t owner;
295 char on_list;
296 char filename[PATH_MAX];
lockfile.c
+10 -7
@@ -12,8 +12,10 @@ static void remove_lock_file(void)
12
13 while (lock_file_list) {
14 if (lock_file_list->owner == me &&
15 - lock_file_list->filename[0])
15 + lock_file_list->filename[0]) {
16 + close(lock_file_list->fd);
17 unlink(lock_file_list->filename);
18 + }
19 lock_file_list = lock_file_list->next;
20 }
21 }
@@ -120,8 +122,6 @@ static char *resolve_symlink(char *p, size_t s)
122
123 static int lock_file(struct lock_file *lk, const char *path)
124 {
123 - int fd;
124 -
125 if (strlen(path) >= sizeof(lk->filename)) return -1;
126 strcpy(lk->filename, path);
127 /*
@@ -130,8 +130,8 @@ static int lock_file(struct lock_file *lk, const char *path)
130 */
131 resolve_symlink(lk->filename, sizeof(lk->filename)-5);
132 strcat(lk->filename, ".lock");
133 - fd = open(lk->filename, O_RDWR | O_CREAT | O_EXCL, 0666);
134 - if (0 <= fd) {
133 + lk->fd = open(lk->filename, O_RDWR | O_CREAT | O_EXCL, 0666);
134 + if (0 <= lk->fd) {
135 if (!lock_file_list) {
136 signal(SIGINT, remove_lock_file_on_signal);
137 atexit(remove_lock_file);
@@ -148,7 +148,7 @@ static int lock_file(struct lock_file *lk, const char *path)
148 }
149 else
150 lk->filename[0] = 0;
151 - return fd;
151 + return lk->fd;
152 }
153
154 int hold_lock_file_for_update(struct lock_file *lk, const char *path, int die_on_error)
@@ -163,6 +163,7 @@ int commit_lock_file(struct lock_file *lk)
163 {
164 char result_file[PATH_MAX];
165 int i;
166 + close(lk->fd);
167 strcpy(result_file, lk->filename);
168 i = strlen(result_file) - 5; /* .lock */
169 result_file[i] = 0;
@@ -194,7 +195,9 @@ int commit_locked_index(struct lock_file *lk)
195
196 void rollback_lock_file(struct lock_file *lk)
197 {
197 - if (lk->filename[0])
198 + if (lk->filename[0]) {
199 + close(lk->fd);
200 unlink(lk->filename);
201 + }
202 lk->filename[0] = 0;
203 }