close fd of lockfile before unlinking

This commit is contained in:
Johannes Schindelin
2006-09-19 12:29:28 +02:00
committed by Johannes Sixt
parent 9c6663c419
commit 4a4ae17738
2 changed files with 12 additions and 6 deletions

View File

@@ -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];
};

View File

@@ -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;
}