From 4a4ae17738a52abf6dbd64ba9f01fe497229337d Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Tue, 19 Sep 2006 12:29:28 +0200 Subject: [PATCH] close fd of lockfile before unlinking --- cache.h | 1 + lockfile.c | 17 +++++++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/cache.h b/cache.h index e6e19bdef3..c944e843e0 100644 --- a/cache.h +++ b/cache.h @@ -180,6 +180,7 @@ extern int refresh_cache(unsigned int flags); struct lock_file { struct lock_file *next; + int fd; char on_list; char filename[PATH_MAX]; }; diff --git a/lockfile.c b/lockfile.c index 4824f4dc02..bf80246dc3 100644 --- a/lockfile.c +++ b/lockfile.c @@ -8,8 +8,10 @@ static struct lock_file *lock_file_list; static void remove_lock_file(void) { while (lock_file_list) { - if (lock_file_list->filename[0]) + if (lock_file_list->filename[0]) { + close(lock_file_list->fd); unlink(lock_file_list->filename); + } lock_file_list = lock_file_list->next; } } @@ -23,10 +25,9 @@ static void remove_lock_file_on_signal(int signo) static int lock_file(struct lock_file *lk, const char *path) { - int fd; sprintf(lk->filename, "%s.lock", path); - fd = open(lk->filename, O_RDWR | O_CREAT | O_EXCL, 0666); - if (0 <= fd) { + lk->fd = open(lk->filename, O_RDWR | O_CREAT | O_EXCL, 0666); + if (0 <= lk->fd) { if (!lk->on_list) { lk->next = lock_file_list; lock_file_list = lk; @@ -42,7 +43,7 @@ static int lock_file(struct lock_file *lk, const char *path) } else lk->filename[0] = 0; - return fd; + return lk->fd; } int hold_lock_file_for_update(struct lock_file *lk, const char *path, int die_on_error) @@ -57,9 +58,11 @@ int commit_lock_file(struct lock_file *lk) { char result_file[PATH_MAX]; int i; + close(lk->fd); strcpy(result_file, lk->filename); i = strlen(result_file) - 5; /* .lock */ result_file[i] = 0; + unlink(result_file); i = rename(lk->filename, result_file); lk->filename[0] = 0; return i; @@ -67,8 +70,10 @@ int commit_lock_file(struct lock_file *lk) void rollback_lock_file(struct lock_file *lk) { - if (lk->filename[0]) + if (lk->filename[0]) { + close(lk->fd); unlink(lk->filename); + } lk->filename[0] = 0; } -- 2.11.4.GIT