index-storage.h revision 01cbf4ac5d44137ab434791be7f838d98d0fcf3b
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen#ifndef __INDEX_STORAGE_H
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen#define __INDEX_STORAGE_H
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen#include "file-dotlock.h"
d9076f5939edf5d20a261494b1a861dcbb0d32e2Timo Sirainen#include "mail-storage-private.h"
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen#include "mail-index.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "index-mail.h"
5666a3d6a7ea89362b8d9e8b39b15424cd9d6388Timo Sirainen
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen/* Max. mmap()ed size for a message */
146f9076cd456ea1e9b3f8536456d9d3c962fadbStephan Bosch#define MAIL_MMAP_BLOCK_SIZE (1024*256)
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen/* Block size when read()ing message. */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#define MAIL_READ_BLOCK_SIZE (1024*8)
8c909e451d14075c05d90382cf8eebc4e354f569Timo Sirainen
8c909e451d14075c05d90382cf8eebc4e354f569Timo Sirainen#define MAILBOX_FULL_SYNC_INTERVAL 5
8c909e451d14075c05d90382cf8eebc4e354f569Timo Sirainen
573f0491a5733fe21fa062a455acb4790b4e0499Timo Sirainenenum mailbox_lock_notify_type {
573f0491a5733fe21fa062a455acb4790b4e0499Timo Sirainen MAILBOX_LOCK_NOTIFY_NONE,
573f0491a5733fe21fa062a455acb4790b4e0499Timo Sirainen
3ed2d0f6b5e67e2663d44489d9da3176823789a8Timo Sirainen /* Mailbox is locked, will abort in secs_left */
65f8fb656051f1059f7b5a2da9c5555adcc30439Timo Sirainen MAILBOX_LOCK_NOTIFY_MAILBOX_ABORT,
1a5573ebc32fae2fe576ec544e1781323c1db609Timo Sirainen /* Mailbox lock looks stale, will override in secs_left */
1a5573ebc32fae2fe576ec544e1781323c1db609Timo Sirainen MAILBOX_LOCK_NOTIFY_MAILBOX_OVERRIDE
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen};
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainenstruct index_autosync_file {
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen struct index_autosync_file *next;
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen char *path;
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen time_t last_stamp;
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen};
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainenstruct index_autosync_io {
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen struct index_autosync_io *next;
f7539a17ea306191b53b8f5e752e228937df9ec3Timo Sirainen struct io *io;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen int fd;
2dd39e478269d6fb0bb26d12b394aa30ee965e38Timo Sirainen};
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
563273bdac80393af63b9520cbf4d24cc0efd028Timo Sirainenstruct index_storage {
563273bdac80393af63b9520cbf4d24cc0efd028Timo Sirainen struct mail_storage storage;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen char *dir; /* root directory */
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen char *index_dir;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen char *control_dir;
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen char *inbox_path; /* INBOX location */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen char *temp_prefix; /* prefix for temporary files */
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen char *user; /* name of user accessing the storage */
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen struct mail_storage_callbacks *callbacks;
3e564425db51f3921ce4de11859777135fdedd15Timo Sirainen void *callback_context;
857c471c13ca215f4be9dd4b336b742b8d434e31Timo Sirainen};
857c471c13ca215f4be9dd4b336b742b8d434e31Timo Sirainen
857c471c13ca215f4be9dd4b336b742b8d434e31Timo Sirainenstruct index_mailbox {
857c471c13ca215f4be9dd4b336b742b8d434e31Timo Sirainen struct mailbox box;
857c471c13ca215f4be9dd4b336b742b8d434e31Timo Sirainen struct index_storage *storage;
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen char *path, *control_dir;
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen struct mail_index *index;
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen struct mail_index_view *view;
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen struct mail_cache *cache;
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen struct mail_cache_view *cache_view;
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen struct mail *mail_interface;
563273bdac80393af63b9520cbf4d24cc0efd028Timo Sirainen
563273bdac80393af63b9520cbf4d24cc0efd028Timo Sirainen uint32_t (*get_recent_count)(struct index_mailbox *ibox);
563273bdac80393af63b9520cbf4d24cc0efd028Timo Sirainen unsigned int last_recent_count;
563273bdac80393af63b9520cbf4d24cc0efd028Timo Sirainen
e5fd6dfd0a492e4708d4dbb7971d7fc5d7b8fd85Timo Sirainen struct timeout *autosync_to;
e5fd6dfd0a492e4708d4dbb7971d7fc5d7b8fd85Timo Sirainen struct index_autosync_file *autosync_files;
4ba9a1d3facc515b3feb5238a16bcf91f76fac61Timo Sirainen struct index_autosync_io *autosync_ios;
4ba9a1d3facc515b3feb5238a16bcf91f76fac61Timo Sirainen enum mailbox_sync_flags autosync_flags;
dfaefeabae939803ceb8c503101e86b5496541d1Timo Sirainen time_t sync_last_check, sync_last_notify;
dfaefeabae939803ceb8c503101e86b5496541d1Timo Sirainen unsigned int min_newmail_notify_interval;
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen time_t next_lock_notify; /* temporary */
8b31f966d9688e07672ef1958dcbdb7686523c04Timo Sirainen enum mailbox_lock_notify_type last_notify_type;
8b31f966d9688e07672ef1958dcbdb7686523c04Timo Sirainen
9847ec56efa15fa063eea9988eee2d4ed9ec7d58Timo Sirainen uint32_t commit_log_file_seq;
9847ec56efa15fa063eea9988eee2d4ed9ec7d58Timo Sirainen uoff_t commit_log_file_offset;
9847ec56efa15fa063eea9988eee2d4ed9ec7d58Timo Sirainen
d46a1e3f999dda802dc5137e883adcd7a6629cd3Timo Sirainen /* mbox: */
d46a1e3f999dda802dc5137e883adcd7a6629cd3Timo Sirainen int mbox_fd;
d46a1e3f999dda802dc5137e883adcd7a6629cd3Timo Sirainen struct istream *mbox_stream, *mbox_file_stream;
d1e843e77f4760e303c53d9fce10123fc8d230a1Timo Sirainen int mbox_lock_type;
d1e843e77f4760e303c53d9fce10123fc8d230a1Timo Sirainen dev_t mbox_dev;
d1e843e77f4760e303c53d9fce10123fc8d230a1Timo Sirainen ino_t mbox_ino;
291ce16fffca75e8598a8c9dceb08613413dcb07Timo Sirainen unsigned int mbox_locks;
291ce16fffca75e8598a8c9dceb08613413dcb07Timo Sirainen struct dotlock mbox_dotlock;
291ce16fffca75e8598a8c9dceb08613413dcb07Timo Sirainen unsigned int mbox_lock_id;
08e9fd42eb8007e1f9db62c088eef74f906114a5Josef 'Jeff' Sipek
08e9fd42eb8007e1f9db62c088eef74f906114a5Josef 'Jeff' Sipek uint32_t mbox_extra_idx;
08e9fd42eb8007e1f9db62c088eef74f906114a5Josef 'Jeff' Sipek
563273bdac80393af63b9520cbf4d24cc0efd028Timo Sirainen /* maildir sync: */
563273bdac80393af63b9520cbf4d24cc0efd028Timo Sirainen struct maildir_uidlist *uidlist;
306b3f41b05da642d87e7ca7a1496efce9f5902fTimo Sirainen time_t last_new_mtime, last_cur_mtime, last_new_sync_time;
306b3f41b05da642d87e7ca7a1496efce9f5902fTimo Sirainen time_t dirty_cur_time;
306b3f41b05da642d87e7ca7a1496efce9f5902fTimo Sirainen
306b3f41b05da642d87e7ca7a1496efce9f5902fTimo Sirainen mode_t mail_create_mode;
306b3f41b05da642d87e7ca7a1496efce9f5902fTimo Sirainen unsigned int private_flags_mask;
306b3f41b05da642d87e7ca7a1496efce9f5902fTimo Sirainen
306b3f41b05da642d87e7ca7a1496efce9f5902fTimo Sirainen unsigned int readonly:1;
306b3f41b05da642d87e7ca7a1496efce9f5902fTimo Sirainen unsigned int keep_recent:1;
306b3f41b05da642d87e7ca7a1496efce9f5902fTimo Sirainen unsigned int sent_diskspace_warning:1;
306b3f41b05da642d87e7ca7a1496efce9f5902fTimo Sirainen unsigned int sent_readonly_flags_warning:1;
306b3f41b05da642d87e7ca7a1496efce9f5902fTimo Sirainen unsigned int autosync_pending:1;
97ae33602db7d5bc8eede82512a965d49ab8853bTimo Sirainen unsigned int mail_read_mmaped:1;
97ae33602db7d5bc8eede82512a965d49ab8853bTimo Sirainen unsigned int last_recent_count_initialized:1;
97ae33602db7d5bc8eede82512a965d49ab8853bTimo Sirainen};
30d50c7e8706f2d750215e009504109ca19cd485Timo Sirainen
30d50c7e8706f2d750215e009504109ca19cd485Timo Sirainenstruct index_transaction_context {
97ae33602db7d5bc8eede82512a965d49ab8853bTimo Sirainen struct mailbox_transaction_context mailbox_ctx;
97ae33602db7d5bc8eede82512a965d49ab8853bTimo Sirainen struct index_mailbox *ibox;
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen struct mail_index_transaction *trans;
57a8c6a95e4bce3eeaba36985adb81c07dd683ffTimo Sirainen struct mail_cache_transaction_ctx *cache_trans;
563273bdac80393af63b9520cbf4d24cc0efd028Timo Sirainen
afe1da042382720393eca6497253106e4eec75e0Timo Sirainen struct index_mail fetch_mail; /* for index_storage_fetch() */
afe1da042382720393eca6497253106e4eec75e0Timo Sirainen};
afe1da042382720393eca6497253106e4eec75e0Timo Sirainen
afe1da042382720393eca6497253106e4eec75e0Timo Sirainenint mail_storage_set_index_error(struct index_mailbox *ibox);
afe1da042382720393eca6497253106e4eec75e0Timo Sirainen
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainenvoid index_storage_lock_notify(struct index_mailbox *ibox,
539977f9257bd8985be5a8093658da266ae9cd19Timo Sirainen enum mailbox_lock_notify_type notify_type,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen unsigned int secs_left);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenvoid index_storage_lock_notify_reset(struct index_mailbox *ibox);
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen
563273bdac80393af63b9520cbf4d24cc0efd028Timo Sirainenstruct mail_index *
420040a5930a2b497e79ff0b5f59ba4b764a5b39Timo Sirainenindex_storage_alloc(const char *index_dir, const char *mailbox_path,
420040a5930a2b497e79ff0b5f59ba4b764a5b39Timo Sirainen const char *prefix);
563273bdac80393af63b9520cbf4d24cc0efd028Timo Sirainenvoid index_storage_unref(struct mail_index *index);
420040a5930a2b497e79ff0b5f59ba4b764a5b39Timo Sirainenvoid index_storage_destroy_unrefed(void);
420040a5930a2b497e79ff0b5f59ba4b764a5b39Timo Sirainen
420040a5930a2b497e79ff0b5f59ba4b764a5b39Timo Sirainenvoid index_storage_init(struct index_storage *storage);
eecb235c14b49c01774134ea593c266f2d2c2be1Timo Sirainenvoid index_storage_deinit(struct index_storage *storage);
eecb235c14b49c01774134ea593c266f2d2c2be1Timo Sirainen
7900eb30bac4a46b259522c58362884661483d7cJosef 'Jeff' Sipekstruct index_mailbox *
7900eb30bac4a46b259522c58362884661483d7cJosef 'Jeff' Sipekindex_storage_mailbox_init(struct index_storage *storage, struct mailbox *box,
563273bdac80393af63b9520cbf4d24cc0efd028Timo Sirainen struct mail_index *index, const char *name,
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen enum mailbox_open_flags flags);
98c217499d578495e982ea6010ebff831e9669aeMartti Rannanjärvivoid index_storage_mailbox_free(struct mailbox *box);
98c217499d578495e982ea6010ebff831e9669aeMartti Rannanjärvi
98c217499d578495e982ea6010ebff831e9669aeMartti Rannanjärviint index_storage_is_readonly(struct mailbox *box);
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainenint index_storage_allow_new_keywords(struct mailbox *box);
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainenint index_storage_is_inconsistent(struct mailbox *box);
97ae33602db7d5bc8eede82512a965d49ab8853bTimo Sirainen
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainenint index_mailbox_fix_keywords(struct index_mailbox *ibox,
f1743785713e7632459d623d5df2108f4b93accbTimo Sirainen enum mail_flags *flags,
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen const char *keywords[],
70ead6466f9baa8294e71fc2fba0a4f54f488b5eTimo Sirainen unsigned int keywords_count);
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen
ccc895c0358108d2304239063e940b7d75f364abTimo Sirainenunsigned int index_storage_get_recent_count(struct mail_index_view *view);
8d630c15a8ed6f85553467c3a231a273defca5f6Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenvoid index_mailbox_check_add(struct index_mailbox *ibox,
ee116df08d0fdab703483e18fe8076b2ef9fd9d7Timo Sirainen const char *path, int dir);
c5ab90cfad9cc3e33bcb1baeb30ffc82a7b7053aTimo Sirainenvoid index_mailbox_check_remove_all(struct index_mailbox *ibox);
c5ab90cfad9cc3e33bcb1baeb30ffc82a7b7053aTimo Sirainen
306b3f41b05da642d87e7ca7a1496efce9f5902fTimo Sirainenint index_storage_sync(struct mailbox *box, enum mailbox_sync_flags flags);
9fd2181788a61500641c66aec0f8c746b19bf830Timo Sirainen
9fd2181788a61500641c66aec0f8c746b19bf830Timo Sirainen/* mailbox methods: */
9fd2181788a61500641c66aec0f8c746b19bf830Timo Sirainenvoid index_storage_set_callbacks(struct mail_storage *storage,
306b3f41b05da642d87e7ca7a1496efce9f5902fTimo Sirainen struct mail_storage_callbacks *callbacks,
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen void *context);
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainenint index_storage_get_status(struct mailbox *box,
c6afd726060aae56b6622c6c52aec10231c4bf1cTimo Sirainen enum mailbox_status_items items,
9fd2181788a61500641c66aec0f8c746b19bf830Timo Sirainen struct mailbox_status *status);
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen
98c217499d578495e982ea6010ebff831e9669aeMartti Rannanjärvistruct mail *
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenindex_storage_fetch(struct mailbox_transaction_context *t, uint32_t seq,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen enum mail_fetch_field wanted_fields);
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainenint index_storage_get_uids(struct mailbox *box, uint32_t uid1, uint32_t uid2,
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen uint32_t *seq1_r, uint32_t *seq2_r);
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainenint index_storage_search_get_sorting(struct mailbox *box,
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen enum mail_sort_type *sort_program);
aa41b2e17912d6cad3151babea6a85dd88539d28Timo Sirainenstruct mail_search_context *
aa41b2e17912d6cad3151babea6a85dd88539d28Timo Sirainenindex_storage_search_init(struct mailbox_transaction_context *t,
aa41b2e17912d6cad3151babea6a85dd88539d28Timo Sirainen const char *charset, struct mail_search_arg *args,
aa41b2e17912d6cad3151babea6a85dd88539d28Timo Sirainen const enum mail_sort_type *sort_program,
aa41b2e17912d6cad3151babea6a85dd88539d28Timo Sirainen enum mail_fetch_field wanted_fields,
aa41b2e17912d6cad3151babea6a85dd88539d28Timo Sirainen const char *const wanted_headers[]);
aa41b2e17912d6cad3151babea6a85dd88539d28Timo Sirainenint index_storage_search_deinit(struct mail_search_context *ctx);
aa41b2e17912d6cad3151babea6a85dd88539d28Timo Sirainenstruct mail *index_storage_search_next(struct mail_search_context *ctx);
aa41b2e17912d6cad3151babea6a85dd88539d28Timo Sirainen
aa41b2e17912d6cad3151babea6a85dd88539d28Timo Sirainenstruct mailbox_transaction_context *
aa41b2e17912d6cad3151babea6a85dd88539d28Timo Sirainenindex_transaction_begin(struct mailbox *box);
aa41b2e17912d6cad3151babea6a85dd88539d28Timo Sirainenint index_transaction_commit(struct mailbox_transaction_context *t);
aa41b2e17912d6cad3151babea6a85dd88539d28Timo Sirainenvoid index_transaction_rollback(struct mailbox_transaction_context *t);
aa41b2e17912d6cad3151babea6a85dd88539d28Timo Sirainen
aa41b2e17912d6cad3151babea6a85dd88539d28Timo Sirainen#endif
aa41b2e17912d6cad3151babea6a85dd88539d28Timo Sirainen