105addcb709523868418cc3e3baad7ad3453a91eTimo Sirainen#define MAIL_TRANSACTION_LOG_HEADER_MIN_SIZE 24
e0f4fbf127b192667c62af7875c4f2ca294b6c7aTimo Sirainen#define MAIL_TRANSACTION_LOG_VERSION_FULL(major, minor) \
e0f4fbf127b192667c62af7875c4f2ca294b6c7aTimo Sirainen#define MAIL_TRANSACTION_LOG_VERSION_HAVE(version, wanted_feature) \
e0f4fbf127b192667c62af7875c4f2ca294b6c7aTimo Sirainen ((version) >= MAIL_TRANSACTION_LOG_VERSION_##wanted_feature)
e0f4fbf127b192667c62af7875c4f2ca294b6c7aTimo Sirainen#define MAIL_TRANSACTION_LOG_HDR_VERSION(hdr) \
e0f4fbf127b192667c62af7875c4f2ca294b6c7aTimo Sirainen MAIL_TRANSACTION_LOG_VERSION_FULL((hdr)->major_version, (hdr)->minor_version)
e0f4fbf127b192667c62af7875c4f2ca294b6c7aTimo Sirainen#define MAIL_TRANSACTION_LOG_VERSION_COMPAT_FLAGS \
92dab926b2f2270057b40a907a00cf8eb2309ed6Timo Sirainen#define MAIL_TRANSACTION_LOG_VERSION_HIDE_INTERNAL_MODSEQS \
49d4afbb76f47c8904537d087bc81e43f1c0aa25Timo Sirainen uint64_t initial_modseq; /* v1.1+ (note: log's major/minor version) */
aef92409cf369afdd2ecd81a4f80083cd4082f46Timo Sirainen uint8_t compat_flags; /* enum mail_index_header_compat_flags, v1.2+ */
b44faf865da16ac4d18eecd85a55b3fab6b9e63aTimo Sirainen uint32_t unused2; /* so that this struct is 64bit aligned */
e76f5e07be5bec4e5ca99c3e093ff7f11edbe1b7Timo Sirainen MAIL_TRANSACTION_EXT_HDR_UPDATE32 = 0x00010000,
8872e5c991430f96138a46e36b7f3c2c40d8e5c2Timo Sirainen MAIL_TRANSACTION_INDEX_UNDELETED = 0x00040000,
fee561b9d9162b130e662914fcebc9dd99b5c320Timo Sirainen MAIL_TRANSACTION_ATTRIBUTE_UPDATE = 0x00100000,
51795bfe9d05d92fe942cb451aec2b9d16d32a11Timo Sirainen (MAIL_TRANSACTION_EXT_INTRO | MAIL_TRANSACTION_EXT_RESET | \
041d312b44f8d41f0c9a5762c23e4d146ef7302bTimo Sirainen MAIL_TRANSACTION_EXT_HDR_UPDATE | MAIL_TRANSACTION_EXT_HDR_UPDATE32 | \
041d312b44f8d41f0c9a5762c23e4d146ef7302bTimo Sirainen MAIL_TRANSACTION_EXT_REC_UPDATE | MAIL_TRANSACTION_EXT_ATOMIC_INC)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* since we'll expunge mails based on data read from transaction log,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen try to avoid the possibility of corrupted transaction log expunging
0c909e3461607eadcd66f4eac69b7f34e37fccf1Timo Sirainen messages. this value is ORed to the actual MAIL_TRANSACTION_EXPUNGE*
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen flag. if it's not present, assume corrupted log. */
e169102fb38ce788b76c2a344bee7d77079dea05Timo Sirainen /* Mailbox storage backend synchronization noticed this change. */
e169102fb38ce788b76c2a344bee7d77079dea05Timo Sirainen /* This change syncs the state with another mailbox (dsync),
e169102fb38ce788b76c2a344bee7d77079dea05Timo Sirainen i.e. the change isn't something that a user requested locally. */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen uint32_t type; /* enum mail_transaction_type */
19557f192d37cd54a1a090a8a26d9d47265e4413Aki Tuomi /* don't use uint64_t here. it adds extra 32 bits of padding and also
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen causes problems with CPUs that require alignment */
faed8babca9914257f34fb2e603d74016d563b2dTimo Sirainen uint8_t modify_type; /* enum modify_type : MODIFY_ADD / MODIFY_REMOVE */
faed8babca9914257f34fb2e603d74016d563b2dTimo Sirainen /* unsigned char name[];
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen array of { uint32_t uid1, uid2; }
a928e7efabb1672b1476e597106d4b4b81ac6f3cTimo Sirainen /* unsigned char data[]; */
894987bf45718f8849cc3898afdfb1ac3cfa2445Timo Sirainen /* Don't shrink hdr_size, record_size or record_align but grow them
894987bf45718f8849cc3898afdfb1ac3cfa2445Timo Sirainen if necessary. */
894987bf45718f8849cc3898afdfb1ac3cfa2445Timo Sirainen MAIL_TRANSACTION_EXT_INTRO_FLAG_NO_SHRINK = 0x01
d143077bd518de129b8d446fb58e003903e50867Timo Sirainen /* old extension: set ext_id. don't set name.
d143077bd518de129b8d446fb58e003903e50867Timo Sirainen new extension: ext_id = (uint32_t)-1. give name. */
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen /* unsigned char name[]; */
d143077bd518de129b8d446fb58e003903e50867Timo Sirainen/* these are set for the last ext_intro */
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen /* unsigned char data[]; */
e76f5e07be5bec4e5ca99c3e093ff7f11edbe1b7Timo Sirainen/* this _update32 version should have been the only ext_hdr_update,
e76f5e07be5bec4e5ca99c3e093ff7f11edbe1b7Timo Sirainen but since 16bit integers were originally used for now we'll just use this
e76f5e07be5bec4e5ca99c3e093ff7f11edbe1b7Timo Sirainen only when actually needed to be backwards compatible. */
e76f5e07be5bec4e5ca99c3e093ff7f11edbe1b7Timo Sirainen /* unsigned char data[]; */
a928e7efabb1672b1476e597106d4b4b81ac6f3cTimo Sirainen /* unsigned char data[]; */
082e82792b8ac33ad42beac510441b37a3c50737Timo Sirainen /* same as mail_index_transaction->sync_transaction */
082e82792b8ac33ad42beac510441b37a3c50737Timo Sirainen /* same as mail_index_transaction->tail_offset_changed */
0d70a702dec63d22535684fec6a7247c5f153208Timo Sirainen#define LOG_IS_BEFORE(seq1, offset1, seq2, offset2) \
0d70a702dec63d22535684fec6a7247c5f153208Timo Sirainen (((offset1) < (offset2) && (seq1) == (seq2)) || (seq1) < (seq2))
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainenmail_transaction_log_alloc(struct mail_index *index);
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainenvoid mail_transaction_log_free(struct mail_transaction_log **log);
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen/* Open the transaction log. Returns 1 if ok, 0 if file doesn't exist or it's
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen is corrupted, -1 if there was some I/O error. */
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainenint mail_transaction_log_open(struct mail_transaction_log *log);
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen/* Create, or recreate, the transaction log. Returns 0 if ok, -1 if error. */
94aa90d2d17a7aebcda5a4193a62e80ddbb169b7Timo Sirainenint mail_transaction_log_create(struct mail_transaction_log *log, bool reset);
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen/* Close all the open transactions log files. */
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainenvoid mail_transaction_log_close(struct mail_transaction_log *log);
2d49f150b4bce6f2f59a84e268e4777901c3e42cTimo Sirainen/* Notify of indexid change */
2d49f150b4bce6f2f59a84e268e4777901c3e42cTimo Sirainenvoid mail_transaction_log_indexid_changed(struct mail_transaction_log *log);
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen/* Returns the file seq/offset where the mailbox is currently synced at.
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen Since the log is rotated only when mailbox is fully synced, the sequence
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen points always to the latest file. This function doesn't actually find the
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen latest sync position, so you'll need to use eg. log_view_set() before
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen calling this. */
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainenvoid mail_transaction_log_get_mailbox_sync_pos(struct mail_transaction_log *log,
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen/* Set the current mailbox sync position. file_seq must always be the latest
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen log file's sequence. The offset written automatically to the log when
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen other transactions are being written. */
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainenvoid mail_transaction_log_set_mailbox_sync_pos(struct mail_transaction_log *log,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenmail_transaction_log_view_open(struct mail_transaction_log *log);
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainenvoid mail_transaction_log_view_close(struct mail_transaction_log_view **view);
c2378fc4d82577009b0b5125f381387e7d74ce46Timo Sirainen/* Set view boundaries. Returns -1 if error, 0 if files are lost or corrupted,
c2378fc4d82577009b0b5125f381387e7d74ce46Timo Sirainen 1 if ok. reset_r=TRUE if the whole index should be reset before applying any
ae8817f05005f57bba32479a610b52d083e2b6ebTimo Sirainenint mail_transaction_log_view_set(struct mail_transaction_log_view *view,
ae8817f05005f57bba32479a610b52d083e2b6ebTimo Sirainen uint32_t min_file_seq, uoff_t min_file_offset,
ae8817f05005f57bba32479a610b52d083e2b6ebTimo Sirainen uint32_t max_file_seq, uoff_t max_file_offset,
6d3bb841d4ec2c12ac2d4ecf6146ef8d8a7dd731Timo Sirainen/* Scan through all of the log files that we can find.
6d3bb841d4ec2c12ac2d4ecf6146ef8d8a7dd731Timo Sirainen Returns -1 if error, 0 if ok. */
6d3bb841d4ec2c12ac2d4ecf6146ef8d8a7dd731Timo Sirainenint mail_transaction_log_view_set_all(struct mail_transaction_log_view *view);
0fa842717a8b163252e55c229c37ca0c5d7ff056Timo Sirainen/* Clear the view. If oldest_file_seq > 0, keep it and newer log files
0fa842717a8b163252e55c229c37ca0c5d7ff056Timo Sirainen referenced so we don't get desynced. */
c680a6b35b459045e92814778908da5a93922107Timo Sirainenvoid mail_transaction_log_view_clear(struct mail_transaction_log_view *view,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* Read next transaction record from current position. The position is updated.
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen Returns -1 if error, 0 if we're at end of the view, 1 if ok. */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenint mail_transaction_log_view_next(struct mail_transaction_log_view *view,
5f78b33aa505b17e23cdf27b071a24e127b3db54Timo Sirainen const void **data_r);
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen/* Mark the current view's position to the record returned previously with
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen _log_view_next(). */
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainenvoid mail_transaction_log_view_mark(struct mail_transaction_log_view *view);
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen/* Seek to previously marked position. */
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainenvoid mail_transaction_log_view_rewind(struct mail_transaction_log_view *view);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* Returns the position of the record returned previously with
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_transaction_log_view_next() */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenmail_transaction_log_view_get_prev_pos(struct mail_transaction_log_view *view,
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen/* Return the modseq of the change returned previously with _view_next(). */
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainenmail_transaction_log_view_get_prev_modseq(struct mail_transaction_log_view *view);
c9c24293550541307f1bb41bba4a0fdfe2fa59e0Timo Sirainen/* Returns TRUE if we're at the end of the view window. */
c9c24293550541307f1bb41bba4a0fdfe2fa59e0Timo Sirainenbool mail_transaction_log_view_is_last(struct mail_transaction_log_view *view);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* Marks the log file in current position to be corrupted. */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenmail_transaction_log_view_set_corrupted(struct mail_transaction_log_view *view,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char *fmt, ...)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenmail_transaction_log_view_is_corrupted(struct mail_transaction_log_view *view);
e169102fb38ce788b76c2a344bee7d77079dea05Timo Sirainenint mail_transaction_log_append_begin(struct mail_index *index,
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainen struct mail_transaction_log_append_ctx **ctx_r);
77bc2bda5b781c4ffddc8a74b175cf32e9e2c2ecTimo Sirainenvoid mail_transaction_log_append_add(struct mail_transaction_log_append_ctx *ctx,
1279090ba03f9c176976a69ab7718f0ed77b19afTimo Sirainenint mail_transaction_log_append_commit(struct mail_transaction_log_append_ctx **ctx);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* Lock transaction log for index synchronization. Log cannot be read or
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen written to while it's locked. Returns end offset. */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenint mail_transaction_log_sync_lock(struct mail_transaction_log *log,
2f8da04d700cc23fcd6630226a4866e828b761bdTimo Sirainenvoid mail_transaction_log_sync_unlock(struct mail_transaction_log *log,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* Returns the current head. Works only when log is locked. */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenvoid mail_transaction_log_get_head(struct mail_transaction_log *log,
3ee5f5427b36ea30a01561b35f4002232db7b061Timo Sirainen/* Returns the current tail from which all files are open to head. */
3ee5f5427b36ea30a01561b35f4002232db7b061Timo Sirainenvoid mail_transaction_log_get_tail(struct mail_transaction_log *log,
a050ca9def13949dbaa67bd6574a41c4f397ae26Timo Sirainen/* Returns TRUE if given seq/offset is current head log's rotate point. */
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainenbool mail_transaction_log_is_head_prev(struct mail_transaction_log *log,
902222fb0928d1701f20a384b73f327b1d9a15ddTimo Sirainen/* Move currently opened log head file to memory (called by
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen mail_index_move_to_memory()) */
5b809b97673fb0a73aa5b9d82122612d699f6c5bTimo Sirainenint mail_transaction_log_move_to_memory(struct mail_transaction_log *log);
1be964ec6d835f95b4fdebf02add9265d58ad290Timo Sirainen/* Unlink transaction log files */