maildir-storage.h revision ce742bbf646d5cc46c6aff6d8da7786c13203f47
183bea41fa640dc8117f3eb45ff935cd81377a84Timo Sirainen#ifndef MAILDIR_STORAGE_H
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen#define MAILDIR_STORAGE_H
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen
47e0598840ecffa364ebed523e06939e22738f06Timo Sirainen#define MAILDIR_STORAGE_NAME "maildir"
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen#define MAILDIR_SUBSCRIPTION_FILE_NAME "subscriptions"
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen#define MAILDIR_INDEX_PREFIX "dovecot.index"
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen#define MAILDIR_UNLINK_DIRNAME "DOVECOT-TRASHED"
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen/* "base,S=123:2," means:
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen <base> [<extra sep> <extra data> [..]] <info sep> 2 <flags sep> */
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen#define MAILDIR_INFO_SEP ':'
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen#define MAILDIR_EXTRA_SEP ','
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen#define MAILDIR_FLAGS_SEP ','
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen
1d738cce754bc64bbc66d3355ebdaf3f6eac55f1Timo Sirainen#define MAILDIR_INFO_SEP_S ":"
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen#define MAILDIR_EXTRA_SEP_S ","
bdd7a96c363346f7c38f389791be1487ca08775bTimo Sirainen#define MAILDIR_FLAGS_SEP_S ","
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen
63e2edd14ae7b1dc4a08e2e659501dbf519462f9Timo Sirainen/* ":2," is the standard flags separator */
63e2edd14ae7b1dc4a08e2e659501dbf519462f9Timo Sirainen#define MAILDIR_FLAGS_FULL_SEP MAILDIR_INFO_SEP_S "2" MAILDIR_FLAGS_SEP_S
63e2edd14ae7b1dc4a08e2e659501dbf519462f9Timo Sirainen
029cfcdce65b284d5230adf1c920a5f526b03b5cTimo Sirainen#define MAILDIR_KEYWORD_FIRST 'a'
029cfcdce65b284d5230adf1c920a5f526b03b5cTimo Sirainen#define MAILDIR_KEYWORD_LAST 'z'
029cfcdce65b284d5230adf1c920a5f526b03b5cTimo Sirainen#define MAILDIR_MAX_KEYWORDS (MAILDIR_KEYWORD_LAST - MAILDIR_KEYWORD_FIRST + 1)
029cfcdce65b284d5230adf1c920a5f526b03b5cTimo Sirainen
029cfcdce65b284d5230adf1c920a5f526b03b5cTimo Sirainen/* Maildir++ extension: include file size in the filename to avoid stat() */
029cfcdce65b284d5230adf1c920a5f526b03b5cTimo Sirainen#define MAILDIR_EXTRA_FILE_SIZE 'S'
029cfcdce65b284d5230adf1c920a5f526b03b5cTimo Sirainen/* Something (can't remember what anymore) could use 'W' in filename to avoid
029cfcdce65b284d5230adf1c920a5f526b03b5cTimo Sirainen calculating file's virtual size (added missing CRs). */
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainen#define MAILDIR_EXTRA_VIRTUAL_SIZE 'W'
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainen
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainen/* How often to scan tmp/ directory for old files (based on dir's atime) */
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainen#define MAILDIR_TMP_SCAN_SECS (8*60*60)
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainen/* Delete files having ctime older than this from tmp/. 36h is standard. */
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainen#define MAILDIR_TMP_DELETE_SECS (36*60*60)
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainen
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainen/* How often to touch the uidlist lock file when it's locked.
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainen This is done both when using KEEP_LOCKED flag and when syncing a large
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainen maildir. */
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainen#define MAILDIR_LOCK_TOUCH_SECS 10
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainen
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainen#define MAILDIR_SAVE_FLAG_HARDLINK 0x10000000
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainen#define MAILDIR_SAVE_FLAG_DELETED 0x20000000
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainen
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainen/* If an operation fails with ENOENT, we'll check if the mailbox is deleted
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainen or if some directory is just missing. If it's missing, we'll create the
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainen directories and try again this many times before failing. */
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen#define MAILDIR_DELETE_RETRY_COUNT 3
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen#include "index-storage.h"
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen#include "mailbox-list-private.h"
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainenstruct timeval;
2d49f150b4bce6f2f59a84e268e4777901c3e42cTimo Sirainenstruct maildir_save_context;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainenstruct maildir_copy_context;
90c23747727c85f80e4e8eed7968f0edbeac7ac5Timo Sirainen
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainenstruct maildir_index_header {
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen uint32_t new_check_time, new_mtime, new_mtime_nsecs;
19e8adccba16ff419f5675b1575358c2956dce83Timo Sirainen uint32_t cur_check_time, cur_mtime, cur_mtime_nsecs;
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen};
b397665e90fa0fc7c6a9156fdd6cf28b571e8e39Timo Sirainen
b397665e90fa0fc7c6a9156fdd6cf28b571e8e39Timo Sirainenstruct maildir_list_index_record {
b397665e90fa0fc7c6a9156fdd6cf28b571e8e39Timo Sirainen uint32_t new_mtime, cur_mtime;
b397665e90fa0fc7c6a9156fdd6cf28b571e8e39Timo Sirainen};
19e8adccba16ff419f5675b1575358c2956dce83Timo Sirainen
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainenstruct maildir_storage {
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen struct mail_storage storage;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen union mailbox_list_module_context list_module_ctx;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen const char *temp_prefix;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen uint32_t maildir_list_ext_id;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen unsigned int copy_with_hardlinks:1;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen unsigned int copy_preserve_filename:1;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen unsigned int save_size_in_filename:1;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen unsigned int stat_dirs:1;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen};
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainenstruct maildir_mailbox {
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen struct index_mailbox ibox;
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen struct maildir_storage *storage;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen const char *path;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen struct timeout *keep_lock_to;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen /* maildir sync: */
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen struct maildir_uidlist *uidlist;
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen struct maildir_keywords *keywords;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen struct maildir_index_header maildir_hdr;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen uint32_t maildir_ext_id;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen unsigned int syncing_commit:1;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen};
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainenstruct maildir_transaction_context {
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen struct index_transaction_context ictx;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen union mail_index_transaction_module_context module_ctx;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen struct maildir_save_context *save_ctx;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen};
0f66f12eb4cdbf47670975044c88d8f388bf92dfTimo Sirainen
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainenextern struct mail_vfuncs maildir_mail_vfuncs;
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen
029cfcdce65b284d5230adf1c920a5f526b03b5cTimo Sirainen/* Return -1 = error, 0 = file not found, 1 = ok */
029cfcdce65b284d5230adf1c920a5f526b03b5cTimo Sirainentypedef int maildir_file_do_func(struct maildir_mailbox *mbox,
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen const char *path, void *context);
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainenint maildir_file_do(struct maildir_mailbox *mbox, uint32_t uid,
029cfcdce65b284d5230adf1c920a5f526b03b5cTimo Sirainen maildir_file_do_func *callback, void *context);
029cfcdce65b284d5230adf1c920a5f526b03b5cTimo Sirainen#ifdef CONTEXT_TYPE_SAFETY
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen# define maildir_file_do(mbox, seq, callback, context) \
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen ({(void)(1 ? 0 : callback((struct maildir_mailbox *)NULL, \
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen (const char *)NULL, context)); \
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen maildir_file_do(mbox, seq, \
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen (maildir_file_do_func *)callback, context); })
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen#else
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen# define maildir_file_do(mbox, seq, callback, context) \
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen maildir_file_do(mbox, seq, (maildir_file_do_func *)callback, context)
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen#endif
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainenbool maildir_set_deleted(struct maildir_mailbox *mbox);
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainenvoid maildir_transaction_class_init(void);
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainenvoid maildir_transaction_class_deinit(void);
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainenint maildir_save_init(struct mailbox_transaction_context *_t,
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen enum mail_flags flags, struct mail_keywords *keywords,
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen time_t received_date, int timezone_offset,
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen const char *from_envelope, struct istream *input,
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen struct mail *dest_mail, struct mail_save_context **ctx_r);
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainenint maildir_save_continue(struct mail_save_context *ctx);
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainenint maildir_save_finish(struct mail_save_context *ctx);
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainenvoid maildir_save_cancel(struct mail_save_context *ctx);
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainenstruct maildir_save_context *
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainenmaildir_save_transaction_init(struct maildir_transaction_context *t);
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainenuint32_t maildir_save_add(struct maildir_transaction_context *t,
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen const char *base_fname, enum mail_flags flags,
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen struct mail_keywords *keywords,
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen struct mail *dest_mail);
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainenconst char *maildir_save_file_get_path(struct mailbox_transaction_context *t,
95a1a5195d56f3cf5d1e529aad668f87ad3b979bTimo Sirainen uint32_t seq);
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainenint maildir_transaction_save_commit_pre(struct maildir_save_context *ctx);
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainenvoid maildir_transaction_save_commit_post(struct maildir_save_context *ctx);
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainenvoid maildir_transaction_save_rollback(struct maildir_save_context *ctx);
0bf3eac1110a902e7ec7e695c64e8e46c114e623Timo Sirainen
0bf3eac1110a902e7ec7e695c64e8e46c114e623Timo Sirainenint maildir_copy(struct mailbox_transaction_context *t, struct mail *mail,
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen enum mail_flags flags, struct mail_keywords *keywords,
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen struct mail *dest_mail);
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainenint maildir_transaction_copy_commit(struct maildir_copy_context *ctx);
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainenvoid maildir_transaction_copy_rollback(struct maildir_copy_context *ctx);
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen#endif
85144b5f0bc763de14c7d87291a90ef74ac241a2Timo Sirainen