mail-transaction-log.c revision 1531dfe21973fccfc7547798621a1ee1b4e82577
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen/* Copyright (C) 2003-2004 Timo Sirainen */
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen/* this lock should never exist for a long time.. */
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen#define LOG_DOTLOCK_IMMEDIATE_STALE_TIMEOUT 300
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainenmail_transaction_log_file_open_or_create(struct mail_transaction_log *log,
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen const char *path);
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainenstatic int mail_transaction_log_rotate(struct mail_transaction_log *log,
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainenstatic int mail_transaction_log_lock_head(struct mail_transaction_log *log);
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainenmail_transaction_log_file_set_corrupted(struct mail_transaction_log_file *file,
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen const char *fmt, ...)
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen mail_index_file_set_syscall_error(file->log->index,
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen "Corrupted transaction log file %s: %s",
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainenmail_transaction_log_file_dotlock(struct mail_transaction_log_file *file)
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen ret = file_lock_dotlock(file->filepath, NULL, FALSE,
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen mail_index_file_set_syscall_error(file->log->index,
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen "file_lock_dotlock()");
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen "Timeout while waiting for release of "
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen "dotlock for transaction log file %s",
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainenmail_transaction_log_file_undotlock(struct mail_transaction_log_file *file)
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen ret = file_unlock_dotlock(file->filepath, &file->log->dotlock);
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen mail_index_file_set_syscall_error(file->log->index,
6d2b3ce2c6ef62334985ece4f0ab8b154e0e9560Timo Sirainen "Dotlock was lost for transaction log file %s",
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainenmail_transaction_log_file_lock(struct mail_transaction_log_file *file)
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen return mail_transaction_log_file_dotlock(file);
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen ret = file_wait_lock_full(file->fd, F_WRLCK, DEFAULT_LOCK_TIMEOUT,
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen mail_index_file_set_syscall_error(file->log->index,
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen "file_wait_lock()");
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen "Timeout while waiting for release of "
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen "fcntl() lock for transaction log file %s",
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainenmail_transaction_log_file_unlock(struct mail_transaction_log_file *file)
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen mail_index_file_set_syscall_error(file->log->index,
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen "file_wait_lock()");
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen !(((file)->hdr.file_seq == (index)->hdr->log_file_seq && \
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen sizeof(struct mail_transaction_log_header)) || \
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen ((file)->hdr.prev_file_seq == (index)->hdr->log_file_seq && \
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen (file)->hdr.prev_file_offset == (index)->hdr->log_file_offset))
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainenstatic int mail_transaction_log_check_file_seq(struct mail_transaction_log *log)
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen unsigned int lock_id;
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen ret = mail_index_lock_shared(index, TRUE, &lock_id);
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen else if (INDEX_HAS_MISSING_LOGS(index, file)) {
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen /* broken - fix it by creating a new log file */
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainen ret = mail_transaction_log_rotate(log, F_UNLCK);
8d3278a82b964217d95c340ec6f82037cdc59d19Timo Sirainenmail_transaction_log_open_or_create(struct mail_index *index)
return NULL;
return log;
int ret;
if (ret < 0) {
if (ret == 0) {
unsigned int lock_id;
fd);
return fd2;
if (ret < 0)
if (fd2 < 0) {
return fd2;
if (fd2 < 0) {
return fd2;
static struct mail_transaction_log_file *
struct mail_transaction_log_file **p;
int ret;
return NULL;
if (ret == 0) {
if (ret <= 0) {
return NULL;
*p = file;
return file;
static struct mail_transaction_log_file *
const char *path)
int fd;
return NULL;
return NULL;
if ((*p)->refcount != 0)
p = &(*p)->next;
*p = next;
int fd;
if (lock) {
unsigned int lock_id;
int ret;
return ret;
const char *path;
const void *data;
if (hdr_size == 0) {
void *data;
int ret;
if (ret == 0) {
if (ret < 0) {
if (ret > 0)
if (ret == 0) {
if (!use_mmap) {
if (ret <= 0) {
return ret;
MADV_SEQUENTIAL) < 0) {
int ret = 0;
if (ret < 0)
return ret;
const void *data)
if (deleted) {
dest++;
struct mail_index_transaction *t)
const void *data;
int ret;
case MAIL_TRANSACTION_APPEND:
case MAIL_TRANSACTION_CACHE_RESET: {
return ret;
if (size == 0)
hdr_data_size = 0;
if (external)
hdr_size =
if (hdr_data_size > 0) {
struct mail_transaction_cache_reset u;
memset(&u, 0, sizeof(u));
return buf;
static const buffer_t *
struct mail_transaction_header_update u;
int state = 0;
memset(&u, 0, sizeof(u));
if (state == 0) {
state++;
if (state > 0) {
u.size);
state = 0;
return buf;
unsigned int i, lock_id;
int ret;
*log_file_seq_r = 0;
*log_file_offset_r = 0;
ret = 0;
if (t->new_cache_file_seq != 0) {
if (ret < 0) {
t->hide_transaction) {
if (ret < 0) {
if (ret < 0)
return ret;