mail-index-private.h revision 3675a7e9bd3775ba13fe8bc93915902513a0f1a4
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen#ifndef MAIL_INDEX_PRIVATE_H
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen#define MAIL_INDEX_PRIVATE_H
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen#include "file-lock.h"
78fa3c578c14ee8a612f86cf73b6181c7f16463fTimo Sirainen#include "mail-index.h"
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen#include "mail-index-view-private.h"
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen#include "mail-index-transaction-private.h"
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen#include <sys/stat.h>
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenstruct mail_transaction_header;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenstruct mail_transaction_log_view;
f7423cbbd9dea363a5df18ebb96da055a977ae79Timo Sirainenstruct mail_index_sync_map_ctx;
2cd2518bab14292a67cf8a490b58ab9ef89879daTimo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen/* How large index files to mmap() instead of reading to memory. */
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen#define MAIL_INDEX_MMAP_MIN_SIZE (1024*64)
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen/* How many times to retry opening index files if read/fstat returns ESTALE.
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen This happens with NFS when the file has been deleted (ie. index file was
6ec925f52d04ec8700e47cb005bd7ddc65ac5614Timo Sirainen rewritten by another computer than us). */
c60178b2610a9b193ff72aa18448398ef72529a1Timo Sirainen#define MAIL_INDEX_ESTALE_RETRY_COUNT NFS_ESTALE_RETRY_COUNT
4d4d6d4745682790c20d759ba93dbea46b812c5dTimo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen#define MAIL_INDEX_IS_IN_MEMORY(index) \
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen ((index)->dir == NULL)
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
cf2e6953d03a1c22f272ec19432fc03c136ac1bbTimo Sirainen#define MAIL_INDEX_MAP_IS_IN_MEMORY(map) \
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen ((map)->rec_map->mmap_base == NULL)
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen#define MAIL_INDEX_MAP_IDX(map, idx) \
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen ((struct mail_index_record *) \
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen PTR_OFFSET((map)->rec_map->records, (idx) * (map)->hdr.record_size))
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
617e13833c798435e2be425b99c27ecaad1b8393Timo Sirainentypedef int mail_index_expunge_handler_t(struct mail_index_sync_map_ctx *ctx,
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen uint32_t seq, const void *data,
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen void **sync_context, void *context);
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainentypedef int mail_index_sync_handler_t(struct mail_index_sync_map_ctx *ctx,
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen uint32_t seq, void *old_data,
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen const void *new_data, void **context);
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainentypedef void mail_index_sync_lost_handler_t(struct mail_index *index);
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo SirainenARRAY_DEFINE_TYPE(seq_array, uint32_t);
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen#define MAIL_INDEX_HEADER_SIZE_ALIGN(size) \
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen (((size) + 7) & ~7)
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainenstruct mail_index_ext {
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen const char *name;
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen uint32_t index_idx; /* index ext_id */
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen uint32_t reset_id;
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen uint32_t ext_offset; /* points to beginning of mail_index_ext_header */
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen uint32_t hdr_offset; /* points to mail_index_ext_header.data[] */
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen uint32_t hdr_size; /* size of mail_index_ext_header.data[] */
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen uint16_t record_offset;
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen uint16_t record_size;
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen uint16_t record_align;
78fa3c578c14ee8a612f86cf73b6181c7f16463fTimo Sirainen};
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenstruct mail_index_ext_header {
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen uint32_t hdr_size; /* size of data[] */
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen uint32_t reset_id;
71056e0f5e1f68cb9ac002a7827eb98435c40d62Timo Sirainen uint16_t record_offset;
e9ac518aaf49a06d976bf7f24ab14a3e2d6d86abTimo Sirainen uint16_t record_size;
71056e0f5e1f68cb9ac002a7827eb98435c40d62Timo Sirainen uint16_t record_align;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen uint16_t name_size;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen /* unsigned char name[name_size] */
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen /* unsigned char data[hdr_size] (starting 64bit aligned) */
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen};
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenstruct mail_index_keyword_header {
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen uint32_t keywords_count;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen /* struct mail_index_keyword_header_rec[] */
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen /* char name[][] */
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen};
78fa3c578c14ee8a612f86cf73b6181c7f16463fTimo Sirainen
78fa3c578c14ee8a612f86cf73b6181c7f16463fTimo Sirainenstruct mail_index_keyword_header_rec {
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen uint32_t unused; /* for backwards compatibility */
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen uint32_t name_offset; /* relative to beginning of name[] */
e9ac518aaf49a06d976bf7f24ab14a3e2d6d86abTimo Sirainen};
e9ac518aaf49a06d976bf7f24ab14a3e2d6d86abTimo Sirainen
78fa3c578c14ee8a612f86cf73b6181c7f16463fTimo Sirainenenum mail_index_sync_handler_type {
e9ac518aaf49a06d976bf7f24ab14a3e2d6d86abTimo Sirainen MAIL_INDEX_SYNC_HANDLER_FILE = 0x01,
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen MAIL_INDEX_SYNC_HANDLER_HEAD = 0x02,
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen MAIL_INDEX_SYNC_HANDLER_VIEW = 0x04
230ef558135f16a66b86cbe3762524eaa9ae9d81Timo Sirainen};
230ef558135f16a66b86cbe3762524eaa9ae9d81Timo Sirainen
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainenstruct mail_index_sync_handler {
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen mail_index_sync_handler_t *callback;
e9ac518aaf49a06d976bf7f24ab14a3e2d6d86abTimo Sirainen enum mail_index_sync_handler_type type;
e9ac518aaf49a06d976bf7f24ab14a3e2d6d86abTimo Sirainen};
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainenstruct mail_index_registered_ext {
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen const char *name;
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen uint32_t index_idx; /* index ext_id */
78fa3c578c14ee8a612f86cf73b6181c7f16463fTimo Sirainen uint32_t hdr_size; /* size of mail_index_ext_header.data[] */
78fa3c578c14ee8a612f86cf73b6181c7f16463fTimo Sirainen uint16_t record_size;
71056e0f5e1f68cb9ac002a7827eb98435c40d62Timo Sirainen uint16_t record_align;
71056e0f5e1f68cb9ac002a7827eb98435c40d62Timo Sirainen
78fa3c578c14ee8a612f86cf73b6181c7f16463fTimo Sirainen struct mail_index_sync_handler sync_handler;
71056e0f5e1f68cb9ac002a7827eb98435c40d62Timo Sirainen mail_index_expunge_handler_t *expunge_handler;
71056e0f5e1f68cb9ac002a7827eb98435c40d62Timo Sirainen
71056e0f5e1f68cb9ac002a7827eb98435c40d62Timo Sirainen void *expunge_context;
71056e0f5e1f68cb9ac002a7827eb98435c40d62Timo Sirainen unsigned int expunge_handler_call_always:1;
71056e0f5e1f68cb9ac002a7827eb98435c40d62Timo Sirainen};
71056e0f5e1f68cb9ac002a7827eb98435c40d62Timo Sirainen
71056e0f5e1f68cb9ac002a7827eb98435c40d62Timo Sirainenstruct mail_index_record_map {
8761992b5aa05862e7ec3a460cdc17af41a4a0f5Timo Sirainen ARRAY_DEFINE(maps, struct mail_index_map *);
8761992b5aa05862e7ec3a460cdc17af41a4a0f5Timo Sirainen
71056e0f5e1f68cb9ac002a7827eb98435c40d62Timo Sirainen void *mmap_base;
71056e0f5e1f68cb9ac002a7827eb98435c40d62Timo Sirainen size_t mmap_size, mmap_used_size;
e9ac518aaf49a06d976bf7f24ab14a3e2d6d86abTimo Sirainen unsigned int lock_id;
71056e0f5e1f68cb9ac002a7827eb98435c40d62Timo Sirainen
71056e0f5e1f68cb9ac002a7827eb98435c40d62Timo Sirainen buffer_t *buffer;
71056e0f5e1f68cb9ac002a7827eb98435c40d62Timo Sirainen
e9ac518aaf49a06d976bf7f24ab14a3e2d6d86abTimo Sirainen void *records; /* struct mail_index_record[] */
7487ff578435377bbeefffdbfb78ca09ed1292dfTimo Sirainen unsigned int records_count;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen uint32_t last_appended_uid;
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen /* If this mapping is written to disk and write_atomic=FALSE,
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen write_seq_* specify the message sequence range that needs to be
6c2ce1d5bf17b21e804a079eb0f973b7ab83e0d8Timo Sirainen written. */
efc5c69c572e83db7bf7eab5d4698c0ab0d3d886Timo Sirainen uint32_t write_seq_first, write_seq_last;
efc5c69c572e83db7bf7eab5d4698c0ab0d3d886Timo Sirainen};
efc5c69c572e83db7bf7eab5d4698c0ab0d3d886Timo Sirainen
efc5c69c572e83db7bf7eab5d4698c0ab0d3d886Timo Sirainenstruct mail_index_map {
efc5c69c572e83db7bf7eab5d4698c0ab0d3d886Timo Sirainen struct mail_index *index;
efc5c69c572e83db7bf7eab5d4698c0ab0d3d886Timo Sirainen int refcount;
efc5c69c572e83db7bf7eab5d4698c0ab0d3d886Timo Sirainen
efc5c69c572e83db7bf7eab5d4698c0ab0d3d886Timo Sirainen struct mail_index_header hdr;
dc07b75b7ea83ff5f447970a20419032725271a7Timo Sirainen const void *hdr_base;
efc5c69c572e83db7bf7eab5d4698c0ab0d3d886Timo Sirainen buffer_t *hdr_copy_buf;
efc5c69c572e83db7bf7eab5d4698c0ab0d3d886Timo Sirainen
efc5c69c572e83db7bf7eab5d4698c0ab0d3d886Timo Sirainen pool_t extension_pool;
efc5c69c572e83db7bf7eab5d4698c0ab0d3d886Timo Sirainen ARRAY_DEFINE(extensions, struct mail_index_ext);
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen ARRAY_DEFINE(ext_id_map, uint32_t); /* index -> file */
29f32cdcf44cda9688576bfdc7450a8a15e90e86Timo Sirainen
29f32cdcf44cda9688576bfdc7450a8a15e90e86Timo Sirainen ARRAY_DEFINE(keyword_idx_map, unsigned int); /* file -> index */
29f32cdcf44cda9688576bfdc7450a8a15e90e86Timo Sirainen
29f32cdcf44cda9688576bfdc7450a8a15e90e86Timo Sirainen struct mail_index_record_map *rec_map;
29f32cdcf44cda9688576bfdc7450a8a15e90e86Timo Sirainen
29f32cdcf44cda9688576bfdc7450a8a15e90e86Timo Sirainen unsigned int write_base_header:1;
29f32cdcf44cda9688576bfdc7450a8a15e90e86Timo Sirainen unsigned int write_ext_header:1;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen unsigned int write_atomic:1; /* write to a new file and rename() */
b955a1c1b6d466977d971c029a9305bee492f73cTimo Sirainen};
b955a1c1b6d466977d971c029a9305bee492f73cTimo Sirainen
b955a1c1b6d466977d971c029a9305bee492f73cTimo Sirainenstruct mail_index_module_register {
b955a1c1b6d466977d971c029a9305bee492f73cTimo Sirainen unsigned int id;
b955a1c1b6d466977d971c029a9305bee492f73cTimo Sirainen};
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenunion mail_index_module_context {
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen struct mail_index_module_register *reg;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen};
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenstruct mail_index {
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen char *dir, *prefix;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen struct mail_cache *cache;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen struct mail_transaction_log *log;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen enum mail_index_sync_type fsync_mask;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen mode_t mode;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen gid_t gid;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen pool_t extension_pool;
601b455f4d5e780044b9e4fac5f687c1b07ae145Timo Sirainen ARRAY_DEFINE(extensions, struct mail_index_registered_ext);
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
e9ac518aaf49a06d976bf7f24ab14a3e2d6d86abTimo Sirainen ARRAY_DEFINE(sync_lost_handlers, mail_index_sync_lost_handler_t *);
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen char *filepath;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen int fd;
fb7ac3e31c92627efe076318319976ac1c27ae2aTimo Sirainen
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen struct mail_index_map *map;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen uint32_t indexid;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen unsigned int inconsistency_id;
72cc352b25ad401b923436c6ed0f1f3adaffa737Timo Sirainen
e1f866daa1bd1a5cd7516f3b19c6f197bcf6cc8aTimo Sirainen /* last_read_log_file_* contains the seq/offsets we last read from
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen the main index file's headers. these are used to figure out when
614529ee060755c0b282102b70daf56bcd64222dTimo Sirainen the main index file should be updated, and if we can update it
9ebd0c59de5f8240c0dbc58773fe5679391199dbTimo Sirainen by writing on top of it or if we need to recreate it. */
6ec925f52d04ec8700e47cb005bd7ddc65ac5614Timo Sirainen uint32_t last_read_log_file_seq;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen uint32_t last_read_log_file_head_offset;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen uint32_t last_read_log_file_tail_offset;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen struct stat last_read_stat;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen /* transaction log head seq/offset when we last fscked */
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen uint32_t fsck_log_head_file_seq;
e1f866daa1bd1a5cd7516f3b19c6f197bcf6cc8aTimo Sirainen uoff_t fsck_log_head_file_offset;
e1f866daa1bd1a5cd7516f3b19c6f197bcf6cc8aTimo Sirainen
e1f866daa1bd1a5cd7516f3b19c6f197bcf6cc8aTimo Sirainen int lock_type, shared_lock_count, excl_lock_count;
e1f866daa1bd1a5cd7516f3b19c6f197bcf6cc8aTimo Sirainen unsigned int lock_id_counter;
e1f866daa1bd1a5cd7516f3b19c6f197bcf6cc8aTimo Sirainen enum file_lock_method lock_method;
e1f866daa1bd1a5cd7516f3b19c6f197bcf6cc8aTimo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen struct file_lock *file_lock;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen struct dotlock *dotlock;
4b2a1030f9b51e90f6ff4cdbc115871a398c1e0fTimo Sirainen
72cc352b25ad401b923436c6ed0f1f3adaffa737Timo Sirainen pool_t keywords_pool;
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen ARRAY_TYPE(keywords) keywords;
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen struct hash_table *keywords_hash; /* name -> idx */
72cc352b25ad401b923436c6ed0f1f3adaffa737Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen uint32_t keywords_ext_id;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
9e291f6a7ca67b86d1d65a730ffb71fb9a334fb0Timo Sirainen /* Module-specific contexts. */
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen ARRAY_DEFINE(module_contexts, union mail_index_module_context *);
bad5fa318c6c1384ab83bd72d53ce06593274c18Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen char *error;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen unsigned int nodiskspace:1;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen unsigned int index_lock_timeout:1;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen unsigned int opened:1;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen unsigned int log_locked:1;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen unsigned int mmap_disable:1;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen unsigned int fsync_disable:1;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen unsigned int use_excl_dotlocks:1;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen unsigned int nfs_flush:1;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen unsigned int readonly:1;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen unsigned int mapping:1;
f5e1d3d6b34ec152aa1ff15c7bd3d3552e9227eaTimo Sirainen unsigned int syncing:1;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen unsigned int need_recreate:1;
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen};
f2880c3c3de56ec757ffc393363bb863efd7bcbeTimo Sirainen
f2880c3c3de56ec757ffc393363bb863efd7bcbeTimo Sirainenextern struct mail_index_module_register mail_index_module_register;
f29756821a4c6b12b73e4a2a3e1c230117a43773Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen/* Add/replace sync handler for specified extra record. */
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenvoid mail_index_register_expunge_handler(struct mail_index *index,
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen uint32_t ext_id, bool call_always,
bad5fa318c6c1384ab83bd72d53ce06593274c18Timo Sirainen mail_index_expunge_handler_t *callback,
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen void *context);
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenvoid mail_index_unregister_expunge_handler(struct mail_index *index,
5d46f4d076fc13ae2779c365cf4bd9bda3a5bc69Timo Sirainen uint32_t ext_id);
5d46f4d076fc13ae2779c365cf4bd9bda3a5bc69Timo Sirainenvoid mail_index_register_sync_handler(struct mail_index *index, uint32_t ext_id,
5d46f4d076fc13ae2779c365cf4bd9bda3a5bc69Timo Sirainen mail_index_sync_handler_t *cb,
28482afc8139462ff9bcc6cedc4936129e8763efTimo Sirainen enum mail_index_sync_handler_type type);
28482afc8139462ff9bcc6cedc4936129e8763efTimo Sirainenvoid mail_index_unregister_sync_handler(struct mail_index *index,
5d46f4d076fc13ae2779c365cf4bd9bda3a5bc69Timo Sirainen uint32_t ext_id);
5d46f4d076fc13ae2779c365cf4bd9bda3a5bc69Timo Sirainenvoid mail_index_register_sync_lost_handler(struct mail_index *index,
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen mail_index_sync_lost_handler_t *cb);
d482079eb385cd071bbc9637cacee225e4aff968Timo Sirainenvoid mail_index_unregister_sync_lost_handler(struct mail_index *index,
9e291f6a7ca67b86d1d65a730ffb71fb9a334fb0Timo Sirainen mail_index_sync_lost_handler_t *cb);
3a0f9aa9504497e4e47f32df54fbf47fdc7423b6Timo Sirainen
3a0f9aa9504497e4e47f32df54fbf47fdc7423b6Timo Sirainenint mail_index_create_tmp_file(struct mail_index *index, const char **path_r);
1ffb2afe6d7e8860a2231a4827078cf2ef9c22cdTimo Sirainen
1ffb2afe6d7e8860a2231a4827078cf2ef9c22cdTimo Sirainenint mail_index_try_open_only(struct mail_index *index);
d4845c4245638fd6f02dc0cb92c3465fae763cbbTimo Sirainenvoid mail_index_close_file(struct mail_index *index);
d4845c4245638fd6f02dc0cb92c3465fae763cbbTimo Sirainenint mail_index_reopen_if_changed(struct mail_index *index);
0161376aac025266d8654577c4b9ce371ffc87eaTimo Sirainen/* Update/rewrite the main index file from index->map */
0161376aac025266d8654577c4b9ce371ffc87eaTimo Sirainenvoid mail_index_write(struct mail_index *index, bool want_rotate);
788f275469ad9ed530e440d6690d0e4381a323b2Timo Sirainen
788f275469ad9ed530e440d6690d0e4381a323b2Timo Sirainenvoid mail_index_flush_read_cache(struct mail_index *index, const char *path,
788f275469ad9ed530e440d6690d0e4381a323b2Timo Sirainen int fd, bool locked);
788f275469ad9ed530e440d6690d0e4381a323b2Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen/* Returns 0 = ok, -1 = error. */
a8aec60b5359e5ea7fc7a9464217ae3626df8785Timo Sirainenint mail_index_lock_shared(struct mail_index *index, unsigned int *lock_id_r);
28482afc8139462ff9bcc6cedc4936129e8763efTimo Sirainen/* Returns 1 = ok, 0 = already locked, -1 = error. */
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenint mail_index_try_lock_exclusive(struct mail_index *index,
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen unsigned int *lock_id_r);
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenvoid mail_index_unlock(struct mail_index *index, unsigned int *lock_id);
43d7e7ce608f5451e4907b5f5c48c00beb265802Timo Sirainen/* Returns TRUE if given lock_id is valid. */
0547a828ee9647fc5fd2e359b145774740a697feTimo Sirainenbool mail_index_is_locked(struct mail_index *index, unsigned int lock_id);
43d7e7ce608f5451e4907b5f5c48c00beb265802Timo Sirainen
43d7e7ce608f5451e4907b5f5c48c00beb265802Timo Sirainenint mail_index_lock_fd(struct mail_index *index, const char *path, int fd,
43d7e7ce608f5451e4907b5f5c48c00beb265802Timo Sirainen int lock_type, unsigned int timeout_secs,
43d7e7ce608f5451e4907b5f5c48c00beb265802Timo Sirainen struct file_lock **lock_r);
43d7e7ce608f5451e4907b5f5c48c00beb265802Timo Sirainen
5d46f4d076fc13ae2779c365cf4bd9bda3a5bc69Timo Sirainen/* Allocate a new empty map. */
5d46f4d076fc13ae2779c365cf4bd9bda3a5bc69Timo Sirainenstruct mail_index_map *mail_index_map_alloc(struct mail_index *index);
5d46f4d076fc13ae2779c365cf4bd9bda3a5bc69Timo Sirainen/* Replace index->map with the latest index changes. This may reopen the index
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen file and/or it may read the latest changes from transaction log. The log is
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen read up to EOF, but non-synced expunges are skipped.
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen If we mmap()ed the index file, the map is returned locked.
55bc6a7a0940ec48a68558ef70838991c5d301d2Timo Sirainen
55bc6a7a0940ec48a68558ef70838991c5d301d2Timo Sirainen Returns 1 = ok, 0 = corrupted, -1 = error. */
55bc6a7a0940ec48a68558ef70838991c5d301d2Timo Sirainenint mail_index_map(struct mail_index *index,
55bc6a7a0940ec48a68558ef70838991c5d301d2Timo Sirainen enum mail_index_sync_handler_type type);
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen/* Unreference given mapping and unmap it if it's dropped to zero. */
55bc6a7a0940ec48a68558ef70838991c5d301d2Timo Sirainenvoid mail_index_unmap(struct mail_index_map **map);
55bc6a7a0940ec48a68558ef70838991c5d301d2Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen/* Clone a map. The returned map is always in memory. */
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenstruct mail_index_map *mail_index_map_clone(const struct mail_index_map *map);
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenvoid mail_index_record_map_move_to_private(struct mail_index_map *map);
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen/* Move a mmaped map to memory. */
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainenvoid mail_index_map_move_to_memory(struct mail_index_map *map);
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenbool mail_index_map_lookup_ext(struct mail_index_map *map, const char *name,
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen uint32_t *idx_r);
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenuint32_t
28482afc8139462ff9bcc6cedc4936129e8763efTimo Sirainenmail_index_map_register_ext(struct mail_index_map *map,
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen const char *name, uint32_t ext_offset,
4f4943f6ef1bc45c23de73eebe83779712b3c8cbTimo Sirainen const struct mail_index_ext_header *ext_hdr);
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenbool mail_index_map_get_ext_idx(struct mail_index_map *map,
601b455f4d5e780044b9e4fac5f687c1b07ae145Timo Sirainen uint32_t ext_id, uint32_t *idx_r);
601b455f4d5e780044b9e4fac5f687c1b07ae145Timo Sirainenconst struct mail_index_ext *
cdc8485491045d82bb98405d4b995f277d12838eTimo Sirainenmail_index_view_get_ext(struct mail_index_view *view, uint32_t ext_id);
cdc8485491045d82bb98405d4b995f277d12838eTimo Sirainen
b2ed2b25c4c457ec1c99ebe5e9bd66a2e2f89cfdTimo Sirainenint mail_index_map_check_header(struct mail_index_map *map);
b2ed2b25c4c457ec1c99ebe5e9bd66a2e2f89cfdTimo Sirainenint mail_index_map_parse_keywords(struct mail_index_map *map);
e5097d2c8efecdd274272b222cf2b30a8ae4ca2aTimo Sirainen
e5097d2c8efecdd274272b222cf2b30a8ae4ca2aTimo Sirainenint mail_index_map_ext_get_next(struct mail_index_map *map,
e5097d2c8efecdd274272b222cf2b30a8ae4ca2aTimo Sirainen unsigned int *offset,
e5097d2c8efecdd274272b222cf2b30a8ae4ca2aTimo Sirainen const struct mail_index_ext_header **ext_hdr_r,
e5097d2c8efecdd274272b222cf2b30a8ae4ca2aTimo Sirainen const char **name_r);
28482afc8139462ff9bcc6cedc4936129e8763efTimo Sirainenint mail_index_map_ext_hdr_check(const struct mail_index_header *hdr,
28482afc8139462ff9bcc6cedc4936129e8763efTimo Sirainen const struct mail_index_ext_header *ext_hdr,
28482afc8139462ff9bcc6cedc4936129e8763efTimo Sirainen const char *name, const char **error_r);
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenunsigned int mail_index_map_ext_hdr_offset(unsigned int name_len);
4f4943f6ef1bc45c23de73eebe83779712b3c8cbTimo Sirainen
4f4943f6ef1bc45c23de73eebe83779712b3c8cbTimo Sirainenvoid mail_index_view_transaction_ref(struct mail_index_view *view);
4f4943f6ef1bc45c23de73eebe83779712b3c8cbTimo Sirainenvoid mail_index_view_transaction_unref(struct mail_index_view *view);
4f4943f6ef1bc45c23de73eebe83779712b3c8cbTimo Sirainen
4f4943f6ef1bc45c23de73eebe83779712b3c8cbTimo Sirainenvoid mail_index_fsck_locked(struct mail_index *index);
4f4943f6ef1bc45c23de73eebe83779712b3c8cbTimo Sirainen
4f4943f6ef1bc45c23de73eebe83779712b3c8cbTimo Sirainenint mail_index_set_error(struct mail_index *index, const char *fmt, ...)
4f4943f6ef1bc45c23de73eebe83779712b3c8cbTimo Sirainen ATTR_FORMAT(2, 3);
e7a4c77db3a37224b91e16d139fa80ce67dc37ffTimo Sirainen/* "%s failed with index file %s: %m" */
4f4943f6ef1bc45c23de73eebe83779712b3c8cbTimo Sirainenint mail_index_set_syscall_error(struct mail_index *index,
4f4943f6ef1bc45c23de73eebe83779712b3c8cbTimo Sirainen const char *function);
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen/* "%s failed with file %s: %m" */
55bc6a7a0940ec48a68558ef70838991c5d301d2Timo Sirainenint mail_index_file_set_syscall_error(struct mail_index *index,
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen const char *filepath,
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen const char *function);
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenuint32_t mail_index_uint32_to_offset(uint32_t offset);
28482afc8139462ff9bcc6cedc4936129e8763efTimo Sirainenuint32_t mail_index_offset_to_uint32(uint32_t offset);
5d4c793b4e3dbc07f08daa4465594b1857f80725Timo Sirainen
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen#endif
cf0ad1a0bddb0787f3d7b408a96d721a8b2a98a3Timo Sirainen