mail-index-lock.c revision fddec1bf093b45eaedcece13c649b811208e0547
/* Copyright (C) 2003-2004 Timo Sirainen */
/*
Locking should never fail or timeout. Exclusive locks must be kept as short
time as possible. Shared locks can be long living, so if we can't get
exclusive lock directly within 2 seconds, we'll replace the index file with
a copy of it. That means the shared lock holders can keep using the old file
while we're modifying the new file.
lock_id is used to figure out if acquired lock is still valid. When index
file is reopened, the lock_id can become invalid. It doesn't matter however,
as no-one's going to modify the old file anymore.
lock_id also tells if we're referring to shared or exclusive lock. This
allows us to drop back to shared locking once all exclusive locks are
dropped. Shared locks have even numbers, exclusive locks have odd numbers.
The number is increased by two every time the lock is dropped or index file
is reopened.
*/
#include "lib.h"
#include "mmap-util.h"
#include "file-lock.h"
#include "write-full.h"
#include "mail-index-private.h"
#include <stdio.h>
{
struct mail_index_map *old_map;
/* new file, new locks. the old fd can keep it's locks, they don't
matter anymore as no-one's going to modify the file. */
index->shared_lock_count = 0;
if (fd != -1) {
ret = 0;
} else {
if (ret > 0)
else if (ret == 0) {
/* index file is lost */
ret = -1;
}
}
if (ret == 0) {
ret = -1;
}
if (lock_id != 0)
if (ret == 0) {
} else {
}
}
return ret;
}
{
return -1;
return 1;
} else {
return 0;
}
}
{
int prot;
return -1;
}
}
return 0;
}
unsigned int timeout_secs, int update_index,
unsigned int *lock_id_r)
{
int ret;
return 1;
}
index->excl_lock_count++;
return 1;
}
if (index->fcntl_locks_disable) {
/* FIXME: exclusive locking will rewrite the index file every
time. shouldn't really be needed.. reading doesn't require
locks then, though */
return 0;
if (mail_index_has_changed(index) < 0)
return -1;
}
return -1;
return 1;
}
if (update_index) {
if (mail_index_has_changed(index) < 0)
return -1;
}
if (ret <= 0) {
/* deadlock equals to timeout */
return 0;
}
return -1;
}
} else {
index->excl_lock_count++;
}
return -1;
return 1;
}
unsigned int *lock_id_r)
{
int ret;
if (ret > 0)
return 0;
if (ret < 0)
return -1;
"shared fcntl() lock for index file %s",
return -1;
}
{
const char *path;
if (fd == -1)
return -1;
sizeof(struct mail_index_record)) < 0) {
fd = -1;
} else {
}
return fd;
}
{
int fd, old_lock_type;
index->excl_lock_count++;
return 0;
}
/* copy the index to index.tmp and use it */
if (fd == -1)
return -1;
index->excl_lock_count++;
"unlink()");
}
index->excl_lock_count = 0;
return -1;
}
return 0;
}
unsigned int *lock_id_r)
{
int ret;
/* exclusive transaction log lock protects exclusive locking
for the main index file */
/* wait two seconds for exclusive lock */
if (ret > 0)
return 0;
if (ret < 0)
return -1;
return mail_index_lock_exclusive_copy(index);
}
{
if (index->shared_lock_count > 0) {
/* leave ourself shared locked. */
"file_try_lock()");
return -1;
}
}
"fsync()");
return -1;
}
return -1;
}
return 0;
}
{
/* new mapping replaces the old */
}
if (mail_index_copy(index) < 0)
}
if (mail_index_copy_lock_finish(index) < 0)
}
}
{
if ((lock_id & 1) == 0) {
/* shared lock */
/* unlocking some older generation of the index file.
we've already closed the file so just ignore this. */
return;
}
} else {
/* exclusive lock */
if (--index->excl_lock_count == 0)
}
if (!index->fcntl_locks_disable) {
"file_wait_lock()");
}
}
}
}
{
}