mail-transaction-log-private.h revision 2d49f150b4bce6f2f59a84e268e4777901c3e42c
e59faf65ce864fe95dc00f5d52b8323cdbd0608aTimo Sirainen#ifndef __MAIL_TRANSACTION_LOG_VIEW_H
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen#define __MAIL_TRANSACTION_LOG_VIEW_H
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen#include "file-dotlock.h"
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen#include "mail-transaction-log.h"
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen/* Rotate when log is older than ROTATE_TIME and larger than MIN_SIZE */
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen#define MAIL_TRANSACTION_LOG_ROTATE_MIN_SIZE (1024*32)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen/* If log is larger than MAX_SIZE, rotate regardless of the time */
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen#define MAIL_TRANSACTION_LOG_ROTATE_MAX_SIZE (1024*1024)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen#define MAIL_TRANSACTION_LOG_ROTATE_TIME (60*5)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen#define MAIL_TRANSACTION_LOG_FILE_IN_MEMORY(file) ((file)->fd == -1)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
77bc2bda5b781c4ffddc8a74b175cf32e9e2c2ecTimo Sirainenstruct mail_transaction_log_file {
77bc2bda5b781c4ffddc8a74b175cf32e9e2c2ecTimo Sirainen struct mail_transaction_log *log;
77bc2bda5b781c4ffddc8a74b175cf32e9e2c2ecTimo Sirainen struct mail_transaction_log_file *next;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
40a5aeebf6b4858b93f0ddff0bf12fba769cf903Timo Sirainen /* refcount=0 is a valid state. files start that way, and they're
40a5aeebf6b4858b93f0ddff0bf12fba769cf903Timo Sirainen freed only when mail_transaction_logs_clean() is called. */
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen int refcount;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen char *filepath;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen int fd;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen ino_t st_ino;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen dev_t st_dev;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen time_t last_mtime;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen uoff_t last_size;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen struct mail_transaction_log_header hdr;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen buffer_t *buffer;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen uoff_t buffer_offset;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen void *mmap_base;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen size_t mmap_size;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen /* points to the next uncommitted transaction. usually same as EOF. */
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen uoff_t sync_offset;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen /* saved_tail_offset is the offset that was last written to transaction
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen log. max_tail_offset is what should be written to the log the next
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen time a transaction is written. transaction log handling may update
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen max_tail_offset automatically by making it skip external transactions
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen after the last saved offset (to avoid re-reading them unneededly). */
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen uoff_t saved_tail_offset, max_tail_offset;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen struct file_lock *file_lock;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen unsigned int locked:1;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen unsigned int corrupted:1;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen};
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenstruct mail_transaction_log {
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen struct mail_index *index;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen struct mail_transaction_log_view *views;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen /* files is a linked list of all the opened log files. the list is
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen sorted by the log file sequence, so that transaction views can use
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen them easily. head contains a pointer to the newest log file. */
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen struct mail_transaction_log_file *files, *head;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen /* open_file is used temporarily while opening the log file.
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen if _open() failed, it's left there for _create(). */
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen struct mail_transaction_log_file *open_file;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen unsigned int dotlock_count;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen struct dotlock_settings dotlock_settings, new_dotlock_settings;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen struct dotlock *dotlock;
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen};
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenvoid
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenmail_transaction_log_file_set_corrupted(struct mail_transaction_log_file *file,
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen const char *fmt, ...)
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen __attr_format__(2, 3);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenstruct mail_transaction_log_file *
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenmail_transaction_log_file_alloc_in_memory(struct mail_transaction_log *log);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenstruct mail_transaction_log_file *
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenmail_transaction_log_file_alloc(struct mail_transaction_log *log,
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen const char *path);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenvoid mail_transaction_log_file_free(struct mail_transaction_log_file **file);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenint mail_transaction_log_file_open(struct mail_transaction_log_file *file,
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen bool check_existing);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenint mail_transaction_log_file_create(struct mail_transaction_log_file *file);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenint mail_transaction_log_file_lock(struct mail_transaction_log_file *file);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenint mail_transaction_log_find_file(struct mail_transaction_log *log,
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen uint32_t file_seq,
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen struct mail_transaction_log_file **file_r);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen/* Returns 1 if ok, 0 if file is corrupted or offset range is invalid,
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen -1 if I/O error */
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenint mail_transaction_log_file_map(struct mail_transaction_log_file *file,
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen uoff_t start_offset, uoff_t end_offset);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenvoid mail_transaction_log_file_move_to_memory(struct mail_transaction_log_file
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen *file);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenvoid mail_transaction_logs_clean(struct mail_transaction_log *log);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenbool mail_transaction_log_want_rotate(struct mail_transaction_log *log);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenint mail_transaction_log_rotate(struct mail_transaction_log *log);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenint mail_transaction_log_lock_head(struct mail_transaction_log *log);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenvoid mail_transaction_log_file_unlock(struct mail_transaction_log_file *file);
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen#endif
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen