bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2007-2018 Dovecot authors, see the included COPYING file */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include "lib.h"
c6227d7ecc972b6c58602d78602210f75190ae3fTimo Sirainen#include "fs-api.h"
bca919b207e27d0d08b431bdb0f2ac099ef8b512Timo Sirainen#include "master-service.h"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include "mail-index-modseq.h"
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen#include "mail-search-build.h"
7a7d2aa11e46195e2d92d6c337d7e78052a5ce67Timo Sirainen#include "mailbox-list-private.h"
a956137aa86d16e5a90b9d4a9e69d3854d824380Timo Sirainen#include "index-pop3-uidl.h"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include "dbox-mail.h"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include "dbox-save.h"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include "sdbox-file.h"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include "sdbox-sync.h"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#include "sdbox-storage.h"
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
2b9e49e4e65e3e2dca38f56971029a3051ccdb99Timo Sirainenextern struct mail_storage dbox_storage, sdbox_storage;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenextern struct mailbox sdbox_mailbox;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenextern struct dbox_storage_vfuncs sdbox_dbox_storage_vfuncs;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic struct mail_storage *sdbox_storage_alloc(void)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen{
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct sdbox_storage *storage;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen pool_t pool;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
2b9e49e4e65e3e2dca38f56971029a3051ccdb99Timo Sirainen pool = pool_alloconly_create("sdbox storage", 512+256);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen storage = p_new(pool, struct sdbox_storage, 1);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen storage->storage.v = sdbox_dbox_storage_vfuncs;
afa201e7e1d2447e8dfa1aff43de0fdad564105fTimo Sirainen storage->storage.storage = sdbox_storage;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen storage->storage.storage.pool = pool;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen return &storage->storage.storage;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen}
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
c6227d7ecc972b6c58602d78602210f75190ae3fTimo Sirainenstatic int sdbox_storage_create(struct mail_storage *_storage,
c6227d7ecc972b6c58602d78602210f75190ae3fTimo Sirainen struct mail_namespace *ns,
c6227d7ecc972b6c58602d78602210f75190ae3fTimo Sirainen const char **error_r)
c6227d7ecc972b6c58602d78602210f75190ae3fTimo Sirainen{
804fa3f03bd9170272168a5ad214053bbe3160c7Josef 'Jeff' Sipek struct dbox_storage *storage = DBOX_STORAGE(_storage);
c6227d7ecc972b6c58602d78602210f75190ae3fTimo Sirainen enum fs_properties props;
c6227d7ecc972b6c58602d78602210f75190ae3fTimo Sirainen
c6227d7ecc972b6c58602d78602210f75190ae3fTimo Sirainen if (dbox_storage_create(_storage, ns, error_r) < 0)
c6227d7ecc972b6c58602d78602210f75190ae3fTimo Sirainen return -1;
c6227d7ecc972b6c58602d78602210f75190ae3fTimo Sirainen
c6227d7ecc972b6c58602d78602210f75190ae3fTimo Sirainen if (storage->attachment_fs != NULL) {
c6227d7ecc972b6c58602d78602210f75190ae3fTimo Sirainen props = fs_get_properties(storage->attachment_fs);
c6227d7ecc972b6c58602d78602210f75190ae3fTimo Sirainen if ((props & FS_PROPERTY_RENAME) == 0) {
c6227d7ecc972b6c58602d78602210f75190ae3fTimo Sirainen *error_r = "mail_attachment_fs: "
c6227d7ecc972b6c58602d78602210f75190ae3fTimo Sirainen "Backend doesn't support renaming";
c6227d7ecc972b6c58602d78602210f75190ae3fTimo Sirainen return -1;
c6227d7ecc972b6c58602d78602210f75190ae3fTimo Sirainen }
c6227d7ecc972b6c58602d78602210f75190ae3fTimo Sirainen }
c6227d7ecc972b6c58602d78602210f75190ae3fTimo Sirainen return 0;
c6227d7ecc972b6c58602d78602210f75190ae3fTimo Sirainen}
c6227d7ecc972b6c58602d78602210f75190ae3fTimo Sirainen
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainenstatic const char *
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainensdbox_storage_find_root_dir(const struct mail_namespace *ns)
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen{
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen bool debug = ns->mail_set->mail_debug;
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen const char *home, *path;
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen
860e6dd603921f61b0cd53c1cc16e1d66d312699Timo Sirainen if (ns->owner != NULL &&
860e6dd603921f61b0cd53c1cc16e1d66d312699Timo Sirainen mail_user_get_home(ns->owner, &home) > 0) {
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen path = t_strconcat(home, "/sdbox", NULL);
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen if (access(path, R_OK|W_OK|X_OK) == 0) {
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen if (debug)
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen i_debug("sdbox: root exists (%s)", path);
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen return path;
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen }
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen if (debug)
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen i_debug("sdbox: access(%s, rwx): failed: %m", path);
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen }
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen return NULL;
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen}
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainenstatic bool sdbox_storage_autodetect(const struct mail_namespace *ns,
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen struct mailbox_list_settings *set)
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen{
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen bool debug = ns->mail_set->mail_debug;
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen struct stat st;
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen const char *path, *root_dir;
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen if (set->root_dir != NULL)
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen root_dir = set->root_dir;
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen else {
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen root_dir = sdbox_storage_find_root_dir(ns);
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen if (root_dir == NULL) {
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen if (debug)
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen i_debug("sdbox: couldn't find root dir");
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen return FALSE;
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen }
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen }
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen /* NOTE: this check works for mdbox as well. we'll rely on the
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen autodetect ordering to catch mdbox before we get here. */
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen path = t_strconcat(root_dir, "/"DBOX_MAILBOX_DIR_NAME, NULL);
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen if (stat(path, &st) < 0) {
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen if (debug)
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen i_debug("sdbox autodetect: stat(%s) failed: %m", path);
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen return FALSE;
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen }
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen if (!S_ISDIR(st.st_mode)) {
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen if (debug)
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen i_debug("sdbox autodetect: %s not a directory", path);
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen return FALSE;
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen }
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen set->root_dir = root_dir;
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen dbox_storage_get_list_settings(ns, set);
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen return TRUE;
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen}
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen
e5fd6dfd0a492e4708d4dbb7971d7fc5d7b8fd85Timo Sirainenstatic struct mailbox *
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainensdbox_mailbox_alloc(struct mail_storage *storage, struct mailbox_list *list,
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen const char *vname, enum mailbox_flags flags)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen{
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct sdbox_mailbox *mbox;
d22301419109ed4a38351715e6760011421dadecTimo Sirainen struct index_mailbox_context *ibox;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen pool_t pool;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* dbox can't work without index files */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen flags &= ~MAILBOX_FLAG_NO_INDEX_FILES;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
2b9e49e4e65e3e2dca38f56971029a3051ccdb99Timo Sirainen pool = pool_alloconly_create("sdbox mailbox", 1024*3);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen mbox = p_new(pool, struct sdbox_mailbox, 1);
d22301419109ed4a38351715e6760011421dadecTimo Sirainen mbox->box = sdbox_mailbox;
d22301419109ed4a38351715e6760011421dadecTimo Sirainen mbox->box.pool = pool;
d22301419109ed4a38351715e6760011421dadecTimo Sirainen mbox->box.storage = storage;
d22301419109ed4a38351715e6760011421dadecTimo Sirainen mbox->box.list = list;
d22301419109ed4a38351715e6760011421dadecTimo Sirainen mbox->box.mail_vfuncs = &sdbox_mail_vfuncs;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
8c909e451d14075c05d90382cf8eebc4e354f569Timo Sirainen index_storage_mailbox_alloc(&mbox->box, vname, flags, MAIL_INDEX_PREFIX);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
d22301419109ed4a38351715e6760011421dadecTimo Sirainen ibox = INDEX_STORAGE_CONTEXT(&mbox->box);
d22301419109ed4a38351715e6760011421dadecTimo Sirainen ibox->index_flags |= MAIL_INDEX_OPEN_FLAG_KEEP_BACKUPS |
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen MAIL_INDEX_OPEN_FLAG_NEVER_IN_MEMORY;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
df3963075d67f76ade66b6a74764f4eae31d9c87Josef 'Jeff' Sipek mbox->storage = SDBOX_STORAGE(storage);
d22301419109ed4a38351715e6760011421dadecTimo Sirainen return &mbox->box;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen}
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenint sdbox_read_header(struct sdbox_mailbox *mbox,
9865d9e7c5713e41db939222ed9c0225a11fb99eTimo Sirainen struct sdbox_index_header *hdr, bool log_error,
9865d9e7c5713e41db939222ed9c0225a11fb99eTimo Sirainen bool *need_resize_r)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen{
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen struct mail_index_view *view;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen const void *data;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen size_t data_size;
6b8043a4d96d696a6b65ebe9ae4f56fbec638348Timo Sirainen int ret = 0;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen i_assert(mbox->box.opened);
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen view = mail_index_view_open(mbox->box.index);
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen mail_index_get_header_ext(view, mbox->hdr_ext_id,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen &data, &data_size);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (data_size < SDBOX_INDEX_HEADER_MIN_SIZE &&
7baab0b0b60df7ce9093d0881cd322dff1e79491Timo Sirainen (!mbox->box.creating || data_size != 0)) {
4b2a4c8c762e3eaddf7fd2abfe7d4cca6e5e3fd8Timo Sirainen if (log_error) {
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi mailbox_set_critical(&mbox->box,
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi "sdbox: Invalid dbox header size");
4b2a4c8c762e3eaddf7fd2abfe7d4cca6e5e3fd8Timo Sirainen }
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen ret = -1;
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen } else {
efe78d3ba24fc866af1c79b9223dc0809ba26cadStephan Bosch i_zero(hdr);
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen memcpy(hdr, data, I_MIN(data_size, sizeof(*hdr)));
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen if (guid_128_is_empty(hdr->mailbox_guid))
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen ret = -1;
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen else {
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen /* data is valid. remember it in case mailbox
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen is being reset */
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen mail_index_set_ext_init_data(mbox->box.index,
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen mbox->hdr_ext_id,
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen hdr, sizeof(*hdr));
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen }
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen }
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen mail_index_view_close(&view);
9865d9e7c5713e41db939222ed9c0225a11fb99eTimo Sirainen *need_resize_r = data_size < sizeof(*hdr);
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen return ret;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen}
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainenstatic void sdbox_update_header(struct sdbox_mailbox *mbox,
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen struct mail_index_transaction *trans,
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen const struct mailbox_update *update)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen{
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen struct sdbox_index_header hdr, new_hdr;
9865d9e7c5713e41db939222ed9c0225a11fb99eTimo Sirainen bool need_resize;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1127f3e1ad7135b6ee5d5e13f8fd1e72f85a76f8Timo Sirainen if (sdbox_read_header(mbox, &hdr, TRUE, &need_resize) < 0) {
efe78d3ba24fc866af1c79b9223dc0809ba26cadStephan Bosch i_zero(&hdr);
1127f3e1ad7135b6ee5d5e13f8fd1e72f85a76f8Timo Sirainen need_resize = TRUE;
1127f3e1ad7135b6ee5d5e13f8fd1e72f85a76f8Timo Sirainen }
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen new_hdr = hdr;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen if (update != NULL && !guid_128_is_empty(update->mailbox_guid)) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen memcpy(new_hdr.mailbox_guid, update->mailbox_guid,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen sizeof(new_hdr.mailbox_guid));
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen } else if (guid_128_is_empty(new_hdr.mailbox_guid)) {
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen guid_128_generate(new_hdr.mailbox_guid);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen }
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
9865d9e7c5713e41db939222ed9c0225a11fb99eTimo Sirainen if (need_resize) {
9865d9e7c5713e41db939222ed9c0225a11fb99eTimo Sirainen mail_index_ext_resize_hdr(trans, mbox->hdr_ext_id,
9865d9e7c5713e41db939222ed9c0225a11fb99eTimo Sirainen sizeof(new_hdr));
9865d9e7c5713e41db939222ed9c0225a11fb99eTimo Sirainen }
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (memcmp(&hdr, &new_hdr, sizeof(hdr)) != 0) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen mail_index_update_header_ext(trans, mbox->hdr_ext_id, 0,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen &new_hdr, sizeof(new_hdr));
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen }
7e1b549a1d841e59faad16430cbd1d56317db8afTimo Sirainen memcpy(mbox->mailbox_guid, new_hdr.mailbox_guid,
7e1b549a1d841e59faad16430cbd1d56317db8afTimo Sirainen sizeof(mbox->mailbox_guid));
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen}
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
f605df8a4c15cc7a11e16fdde994d51473700890Timo Sirainenint sdbox_mailbox_create_indexes(struct mailbox *box,
f605df8a4c15cc7a11e16fdde994d51473700890Timo Sirainen const struct mailbox_update *update,
f605df8a4c15cc7a11e16fdde994d51473700890Timo Sirainen struct mail_index_transaction *trans)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen{
df3963075d67f76ade66b6a74764f4eae31d9c87Josef 'Jeff' Sipek struct sdbox_mailbox *mbox = SDBOX_MAILBOX(box);
dd4f30895ebbddd77e000472fbadcb3128ae2883Timo Sirainen struct mail_index_transaction *new_trans = NULL;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen const struct mail_index_header *hdr;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen uint32_t uid_validity, uid_next;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
dd4f30895ebbddd77e000472fbadcb3128ae2883Timo Sirainen if (trans == NULL) {
dd4f30895ebbddd77e000472fbadcb3128ae2883Timo Sirainen new_trans = mail_index_transaction_begin(box->view, 0);
dd4f30895ebbddd77e000472fbadcb3128ae2883Timo Sirainen trans = new_trans;
dd4f30895ebbddd77e000472fbadcb3128ae2883Timo Sirainen }
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
dd4f30895ebbddd77e000472fbadcb3128ae2883Timo Sirainen hdr = mail_index_get_header(box->view);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (update != NULL && update->uid_validity != 0)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen uid_validity = update->uid_validity;
547e916f4e6f01af682f8b6e032c337f2a699364Timo Sirainen else if (hdr->uid_validity != 0)
547e916f4e6f01af682f8b6e032c337f2a699364Timo Sirainen uid_validity = hdr->uid_validity;
547e916f4e6f01af682f8b6e032c337f2a699364Timo Sirainen else {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen /* set uidvalidity */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen uid_validity = dbox_get_uidvalidity_next(box->list);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen }
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (hdr->uid_validity != uid_validity) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen mail_index_update_header(trans,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen offsetof(struct mail_index_header, uid_validity),
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen &uid_validity, sizeof(uid_validity), TRUE);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen }
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (update != NULL && hdr->next_uid < update->min_next_uid) {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen uid_next = update->min_next_uid;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen mail_index_update_header(trans,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen offsetof(struct mail_index_header, next_uid),
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen &uid_next, sizeof(uid_next), TRUE);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen }
ee9254cc7875519a9c71cc58a40610e6f320e907Timo Sirainen if (update != NULL && update->min_first_recent_uid != 0 &&
29337701451b9c9f9dd26b2aec23a31ab5203822Timo Sirainen hdr->first_recent_uid < update->min_first_recent_uid) {
29337701451b9c9f9dd26b2aec23a31ab5203822Timo Sirainen uint32_t first_recent_uid = update->min_first_recent_uid;
29337701451b9c9f9dd26b2aec23a31ab5203822Timo Sirainen
29337701451b9c9f9dd26b2aec23a31ab5203822Timo Sirainen mail_index_update_header(trans,
29337701451b9c9f9dd26b2aec23a31ab5203822Timo Sirainen offsetof(struct mail_index_header, first_recent_uid),
29337701451b9c9f9dd26b2aec23a31ab5203822Timo Sirainen &first_recent_uid, sizeof(first_recent_uid), FALSE);
29337701451b9c9f9dd26b2aec23a31ab5203822Timo Sirainen }
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (update != NULL && update->min_highest_modseq != 0 &&
ca98d6a1bbe73499da758a36bfab2963375c8d06Timo Sirainen mail_index_modseq_get_highest(box->view) <
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen update->min_highest_modseq) {
fa7c76955c6bc62689fbdf39318194f85905e6e2Timo Sirainen mail_index_modseq_enable(box->index);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen mail_index_update_highest_modseq(trans,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen update->min_highest_modseq);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen }
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
5bcac2212ca86e72289faa7db2c509841fbdbd59Timo Sirainen if (box->inbox_user && box->creating) {
5bcac2212ca86e72289faa7db2c509841fbdbd59Timo Sirainen /* initialize pop3-uidl header when creating mailbox
5bcac2212ca86e72289faa7db2c509841fbdbd59Timo Sirainen (not on mailbox_update()) */
a956137aa86d16e5a90b9d4a9e69d3854d824380Timo Sirainen index_pop3_uidl_set_max_uid(box, trans, 0);
5bcac2212ca86e72289faa7db2c509841fbdbd59Timo Sirainen }
a956137aa86d16e5a90b9d4a9e69d3854d824380Timo Sirainen
46219292a55094fa49aae33eee681ed075d30e17Timo Sirainen sdbox_update_header(mbox, trans, update);
dd4f30895ebbddd77e000472fbadcb3128ae2883Timo Sirainen if (new_trans != NULL) {
dd4f30895ebbddd77e000472fbadcb3128ae2883Timo Sirainen if (mail_index_transaction_commit(&new_trans) < 0) {
1f1ee8db68d9ae1604350801cd8dc33ebe29fe8aTimo Sirainen mailbox_set_index_error(box);
dd4f30895ebbddd77e000472fbadcb3128ae2883Timo Sirainen return -1;
dd4f30895ebbddd77e000472fbadcb3128ae2883Timo Sirainen }
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen }
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen return 0;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen}
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainenstatic const char *
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainensdbox_get_attachment_path_suffix(struct dbox_file *_file)
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen{
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen struct sdbox_file *file = (struct sdbox_file *)_file;
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen return t_strdup_printf("-%s-%u",
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen guid_128_to_string(file->mbox->mailbox_guid),
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen file->uid);
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen}
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen
0a7b04ec6441fdcf083392888b2e30844fc3e86dTimo Sirainenvoid sdbox_set_mailbox_corrupted(struct mailbox *box)
5b62dea2f88165f3f4d87bba9011343f3ff415ffTimo Sirainen{
df3963075d67f76ade66b6a74764f4eae31d9c87Josef 'Jeff' Sipek struct sdbox_mailbox *mbox = SDBOX_MAILBOX(box);
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen struct sdbox_index_header hdr;
9865d9e7c5713e41db939222ed9c0225a11fb99eTimo Sirainen bool need_resize;
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen
9865d9e7c5713e41db939222ed9c0225a11fb99eTimo Sirainen if (sdbox_read_header(mbox, &hdr, TRUE, &need_resize) < 0 ||
9865d9e7c5713e41db939222ed9c0225a11fb99eTimo Sirainen hdr.rebuild_count == 0)
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen mbox->corrupted_rebuild_count = 1;
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen else
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen mbox->corrupted_rebuild_count = hdr.rebuild_count;
5b62dea2f88165f3f4d87bba9011343f3ff415ffTimo Sirainen}
5b62dea2f88165f3f4d87bba9011343f3ff415ffTimo Sirainen
57bf90f66f393c2807b2fc543655013f61d1d9e4Timo Sirainenstatic void sdbox_set_file_corrupted(struct dbox_file *_file)
57bf90f66f393c2807b2fc543655013f61d1d9e4Timo Sirainen{
57bf90f66f393c2807b2fc543655013f61d1d9e4Timo Sirainen struct sdbox_file *file = (struct sdbox_file *)_file;
57bf90f66f393c2807b2fc543655013f61d1d9e4Timo Sirainen
57bf90f66f393c2807b2fc543655013f61d1d9e4Timo Sirainen sdbox_set_mailbox_corrupted(&file->mbox->box);
57bf90f66f393c2807b2fc543655013f61d1d9e4Timo Sirainen}
57bf90f66f393c2807b2fc543655013f61d1d9e4Timo Sirainen
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainenstatic int sdbox_mailbox_alloc_index(struct sdbox_mailbox *mbox)
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen{
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen struct sdbox_index_header hdr;
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen if (index_storage_mailbox_alloc_index(&mbox->box) < 0)
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen return -1;
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen mbox->hdr_ext_id =
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen mail_index_ext_register(mbox->box.index, "dbox-hdr",
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen sizeof(struct sdbox_index_header), 0, 0);
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen /* set the initialization data in case the mailbox is created */
efe78d3ba24fc866af1c79b9223dc0809ba26cadStephan Bosch i_zero(&hdr);
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen guid_128_generate(hdr.mailbox_guid);
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen mail_index_set_ext_init_data(mbox->box.index, mbox->hdr_ext_id,
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen &hdr, sizeof(hdr));
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen return 0;
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen}
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainenstatic int sdbox_mailbox_open(struct mailbox *box)
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen{
df3963075d67f76ade66b6a74764f4eae31d9c87Josef 'Jeff' Sipek struct sdbox_mailbox *mbox = SDBOX_MAILBOX(box);
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen struct sdbox_index_header hdr;
9865d9e7c5713e41db939222ed9c0225a11fb99eTimo Sirainen bool need_resize;
7b15788793354ca0fd4fdb4dda8e426ca1e9e9d7Timo Sirainen time_t path_ctime;
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen
7b15788793354ca0fd4fdb4dda8e426ca1e9e9d7Timo Sirainen if (dbox_mailbox_check_existence(box, &path_ctime) < 0)
50bf6cfe2aa6111fb38af37970f9551b2286638cTimo Sirainen return -1;
50bf6cfe2aa6111fb38af37970f9551b2286638cTimo Sirainen
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen if (sdbox_mailbox_alloc_index(mbox) < 0)
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen return -1;
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen
7b15788793354ca0fd4fdb4dda8e426ca1e9e9d7Timo Sirainen if (dbox_mailbox_open(box, path_ctime) < 0)
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen return -1;
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen
7baab0b0b60df7ce9093d0881cd322dff1e79491Timo Sirainen if (box->creating) {
7baab0b0b60df7ce9093d0881cd322dff1e79491Timo Sirainen /* wait for mailbox creation to initialize the index */
7baab0b0b60df7ce9093d0881cd322dff1e79491Timo Sirainen return 0;
7baab0b0b60df7ce9093d0881cd322dff1e79491Timo Sirainen }
7baab0b0b60df7ce9093d0881cd322dff1e79491Timo Sirainen
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen /* get/generate mailbox guid */
9865d9e7c5713e41db939222ed9c0225a11fb99eTimo Sirainen if (sdbox_read_header(mbox, &hdr, FALSE, &need_resize) < 0) {
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen /* looks like the mailbox is corrupted */
607a8bc27298b711270381364f2a9a999a810a87Timo Sirainen (void)sdbox_sync(mbox, SDBOX_SYNC_FLAG_FORCE);
9865d9e7c5713e41db939222ed9c0225a11fb99eTimo Sirainen if (sdbox_read_header(mbox, &hdr, TRUE, &need_resize) < 0)
efe78d3ba24fc866af1c79b9223dc0809ba26cadStephan Bosch i_zero(&hdr);
e22ac7474fb36e3e3dcfeb70ea5f54ea812aa2d0Timo Sirainen }
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen if (guid_128_is_empty(hdr.mailbox_guid)) {
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen /* regenerate it */
7baab0b0b60df7ce9093d0881cd322dff1e79491Timo Sirainen if (sdbox_mailbox_create_indexes(box, NULL, NULL) < 0 ||
9865d9e7c5713e41db939222ed9c0225a11fb99eTimo Sirainen sdbox_read_header(mbox, &hdr, TRUE, &need_resize) < 0)
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen return -1;
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen }
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen memcpy(mbox->mailbox_guid, hdr.mailbox_guid,
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen sizeof(mbox->mailbox_guid));
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen return 0;
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen}
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainenstatic void sdbox_mailbox_close(struct mailbox *box)
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen{
df3963075d67f76ade66b6a74764f4eae31d9c87Josef 'Jeff' Sipek struct sdbox_mailbox *mbox = SDBOX_MAILBOX(box);
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen if (mbox->corrupted_rebuild_count != 0)
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen (void)sdbox_sync(mbox, 0);
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen index_storage_mailbox_close(box);
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen}
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen
c72fbbe6328ceb0919b5146ecd43817dc294c314Timo Sirainenstatic int
c72fbbe6328ceb0919b5146ecd43817dc294c314Timo Sirainensdbox_mailbox_create(struct mailbox *box,
c72fbbe6328ceb0919b5146ecd43817dc294c314Timo Sirainen const struct mailbox_update *update, bool directory)
c72fbbe6328ceb0919b5146ecd43817dc294c314Timo Sirainen{
df3963075d67f76ade66b6a74764f4eae31d9c87Josef 'Jeff' Sipek struct sdbox_mailbox *mbox = SDBOX_MAILBOX(box);
c72fbbe6328ceb0919b5146ecd43817dc294c314Timo Sirainen struct sdbox_index_header hdr;
c72fbbe6328ceb0919b5146ecd43817dc294c314Timo Sirainen bool need_resize;
c72fbbe6328ceb0919b5146ecd43817dc294c314Timo Sirainen
c72fbbe6328ceb0919b5146ecd43817dc294c314Timo Sirainen if (dbox_mailbox_create(box, update, directory) < 0)
c72fbbe6328ceb0919b5146ecd43817dc294c314Timo Sirainen return -1;
c72fbbe6328ceb0919b5146ecd43817dc294c314Timo Sirainen if (directory || !guid_128_is_empty(mbox->mailbox_guid))
c72fbbe6328ceb0919b5146ecd43817dc294c314Timo Sirainen return 0;
c72fbbe6328ceb0919b5146ecd43817dc294c314Timo Sirainen
c72fbbe6328ceb0919b5146ecd43817dc294c314Timo Sirainen /* another process just created the mailbox. read the mailbox_guid. */
c72fbbe6328ceb0919b5146ecd43817dc294c314Timo Sirainen if (sdbox_read_header(mbox, &hdr, FALSE, &need_resize) < 0) {
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi mailbox_set_critical(box,
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi "sdbox: Failed to read newly created dbox header");
c72fbbe6328ceb0919b5146ecd43817dc294c314Timo Sirainen return -1;
c72fbbe6328ceb0919b5146ecd43817dc294c314Timo Sirainen }
c72fbbe6328ceb0919b5146ecd43817dc294c314Timo Sirainen memcpy(mbox->mailbox_guid, hdr.mailbox_guid,
c72fbbe6328ceb0919b5146ecd43817dc294c314Timo Sirainen sizeof(mbox->mailbox_guid));
c72fbbe6328ceb0919b5146ecd43817dc294c314Timo Sirainen i_assert(!guid_128_is_empty(mbox->mailbox_guid));
c72fbbe6328ceb0919b5146ecd43817dc294c314Timo Sirainen return 0;
c72fbbe6328ceb0919b5146ecd43817dc294c314Timo Sirainen}
c72fbbe6328ceb0919b5146ecd43817dc294c314Timo Sirainen
fbd918f47f591f8084fd52b207ef29515ddd11b9Timo Sirainenstatic int
51327f2489a4e0e615eb9f7d921473cf8512bb79Timo Sirainensdbox_mailbox_get_metadata(struct mailbox *box,
51327f2489a4e0e615eb9f7d921473cf8512bb79Timo Sirainen enum mailbox_metadata_items items,
51327f2489a4e0e615eb9f7d921473cf8512bb79Timo Sirainen struct mailbox_metadata *metadata_r)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen{
df3963075d67f76ade66b6a74764f4eae31d9c87Josef 'Jeff' Sipek struct sdbox_mailbox *mbox = SDBOX_MAILBOX(box);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
206ed2f6fa3a6fb291498627b2da626581c07a18Timo Sirainen if (index_mailbox_get_metadata(box, items, metadata_r) < 0)
206ed2f6fa3a6fb291498627b2da626581c07a18Timo Sirainen return -1;
51327f2489a4e0e615eb9f7d921473cf8512bb79Timo Sirainen if ((items & MAILBOX_METADATA_GUID) != 0) {
51327f2489a4e0e615eb9f7d921473cf8512bb79Timo Sirainen memcpy(metadata_r->guid, mbox->mailbox_guid,
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen sizeof(metadata_r->guid));
51327f2489a4e0e615eb9f7d921473cf8512bb79Timo Sirainen }
206ed2f6fa3a6fb291498627b2da626581c07a18Timo Sirainen return 0;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen}
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstatic int
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainendbox_mailbox_update(struct mailbox *box, const struct mailbox_update *update)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen{
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (!box->opened) {
84e49ad7d7a840d600a961daeca60802e3d69cd0Timo Sirainen if (mailbox_open(box) < 0)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen return -1;
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen }
f46885a5b78b15a8d2419f6e5d13b643bd85e41fTimo Sirainen if (sdbox_mailbox_create_indexes(box, update, NULL) < 0)
f46885a5b78b15a8d2419f6e5d13b643bd85e41fTimo Sirainen return -1;
f46885a5b78b15a8d2419f6e5d13b643bd85e41fTimo Sirainen return index_storage_mailbox_update_common(box, update);
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen}
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
2b9e49e4e65e3e2dca38f56971029a3051ccdb99Timo Sirainenstruct mail_storage sdbox_storage = {
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen .name = SDBOX_STORAGE_NAME,
8b31f966d9688e07672ef1958dcbdb7686523c04Timo Sirainen .class_flags = MAIL_STORAGE_CLASS_FLAG_FILE_PER_MSG |
9847ec56efa15fa063eea9988eee2d4ed9ec7d58Timo Sirainen MAIL_STORAGE_CLASS_FLAG_HAVE_MAIL_GUIDS |
d46a1e3f999dda802dc5137e883adcd7a6629cd3Timo Sirainen MAIL_STORAGE_CLASS_FLAG_HAVE_MAIL_SAVE_GUIDS |
08e9fd42eb8007e1f9db62c088eef74f906114a5Josef 'Jeff' Sipek MAIL_STORAGE_CLASS_FLAG_BINARY_DATA |
08e9fd42eb8007e1f9db62c088eef74f906114a5Josef 'Jeff' Sipek MAIL_STORAGE_CLASS_FLAG_STUBS,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen .v = {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen NULL,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen sdbox_storage_alloc,
c6227d7ecc972b6c58602d78602210f75190ae3fTimo Sirainen sdbox_storage_create,
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen dbox_storage_destroy,
dca6d617a23e3f93af3b8df59acb46478179fe55Timo Sirainen NULL,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen dbox_storage_get_list_settings,
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen sdbox_storage_autodetect,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen sdbox_mailbox_alloc,
857c471c13ca215f4be9dd4b336b742b8d434e31Timo Sirainen NULL,
857c471c13ca215f4be9dd4b336b742b8d434e31Timo Sirainen NULL,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen }
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen};
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
2b9e49e4e65e3e2dca38f56971029a3051ccdb99Timo Sirainenstruct mail_storage dbox_storage = {
2b9e49e4e65e3e2dca38f56971029a3051ccdb99Timo Sirainen .name = "dbox", /* alias */
e15b305e90c9834734ccf35ed78f0ad29d570ee9Timo Sirainen .class_flags = MAIL_STORAGE_CLASS_FLAG_FILE_PER_MSG,
2b9e49e4e65e3e2dca38f56971029a3051ccdb99Timo Sirainen
2b9e49e4e65e3e2dca38f56971029a3051ccdb99Timo Sirainen .v = {
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen NULL,
2b9e49e4e65e3e2dca38f56971029a3051ccdb99Timo Sirainen sdbox_storage_alloc,
c6227d7ecc972b6c58602d78602210f75190ae3fTimo Sirainen sdbox_storage_create,
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen dbox_storage_destroy,
2b9e49e4e65e3e2dca38f56971029a3051ccdb99Timo Sirainen NULL,
2b9e49e4e65e3e2dca38f56971029a3051ccdb99Timo Sirainen dbox_storage_get_list_settings,
eadbca0f2b78fcc36332678fee9ee08003df63deTimo Sirainen sdbox_storage_autodetect,
2b9e49e4e65e3e2dca38f56971029a3051ccdb99Timo Sirainen sdbox_mailbox_alloc,
857c471c13ca215f4be9dd4b336b742b8d434e31Timo Sirainen NULL,
857c471c13ca215f4be9dd4b336b742b8d434e31Timo Sirainen NULL,
2b9e49e4e65e3e2dca38f56971029a3051ccdb99Timo Sirainen }
2b9e49e4e65e3e2dca38f56971029a3051ccdb99Timo Sirainen};
2b9e49e4e65e3e2dca38f56971029a3051ccdb99Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstruct mailbox sdbox_mailbox = {
7bafda1813454621e03615e83d55bccfa7cc56bdTimo Sirainen .v = {
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen index_storage_is_readonly,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen index_storage_mailbox_enable,
306cfd77100131c08b243de10f6d40500f4c27c6Timo Sirainen index_storage_mailbox_exists,
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen sdbox_mailbox_open,
e5fb952c6d49d3b6bff1746551566202e92947daTimo Sirainen sdbox_mailbox_close,
438f12d7a776da695019114884b48188d94613efTimo Sirainen index_storage_mailbox_free,
c72fbbe6328ceb0919b5146ecd43817dc294c314Timo Sirainen sdbox_mailbox_create,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen dbox_mailbox_update,
af9edddbb704a640055898846add4e386e83fe43Timo Sirainen index_storage_mailbox_delete,
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen index_storage_mailbox_rename,
fbd918f47f591f8084fd52b207ef29515ddd11b9Timo Sirainen index_storage_get_status,
51327f2489a4e0e615eb9f7d921473cf8512bb79Timo Sirainen sdbox_mailbox_get_metadata,
6469cf211a57433335641725dc236ebb2b9fdd3bTimo Sirainen index_storage_set_subscribed,
62041dfb7d6ac6e9c633a557075999cdfcff7bd5Timo Sirainen index_storage_attribute_set,
62041dfb7d6ac6e9c633a557075999cdfcff7bd5Timo Sirainen index_storage_attribute_get,
62041dfb7d6ac6e9c633a557075999cdfcff7bd5Timo Sirainen index_storage_attribute_iter_init,
62041dfb7d6ac6e9c633a557075999cdfcff7bd5Timo Sirainen index_storage_attribute_iter_next,
62041dfb7d6ac6e9c633a557075999cdfcff7bd5Timo Sirainen index_storage_attribute_iter_deinit,
c0d069950af1dbc6a4e5c3de3bf2e437796e3ae0Timo Sirainen index_storage_list_index_has_changed,
c0d069950af1dbc6a4e5c3de3bf2e437796e3ae0Timo Sirainen index_storage_list_index_update_sync,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen sdbox_storage_sync_init,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen index_mailbox_sync_next,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen index_mailbox_sync_deinit,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen NULL,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen dbox_notify_changes,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen index_transaction_begin,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen index_transaction_commit,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen index_transaction_rollback,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen NULL,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen dbox_mail_alloc,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen index_storage_search_init,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen index_storage_search_deinit,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen index_storage_search_next_nonblock,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen index_storage_search_next_update_seq,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen sdbox_save_alloc,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen sdbox_save_begin,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen dbox_save_continue,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen sdbox_save_finish,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen sdbox_save_cancel,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen sdbox_copy,
1460ef7a18c53216ddb4a94bb62fba96076aae8eTimo Sirainen sdbox_transaction_save_commit_pre,
1460ef7a18c53216ddb4a94bb62fba96076aae8eTimo Sirainen sdbox_transaction_save_commit_post,
1460ef7a18c53216ddb4a94bb62fba96076aae8eTimo Sirainen sdbox_transaction_save_rollback,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen index_storage_is_inconsistent
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen }
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen};
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainenstruct dbox_storage_vfuncs sdbox_dbox_storage_vfuncs = {
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen sdbox_file_free,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen sdbox_file_create_fd,
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen sdbox_mail_open,
5b62dea2f88165f3f4d87bba9011343f3ff415ffTimo Sirainen sdbox_mailbox_create_indexes,
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen sdbox_get_attachment_path_suffix,
57bf90f66f393c2807b2fc543655013f61d1d9e4Timo Sirainen sdbox_set_mailbox_corrupted,
5b62dea2f88165f3f4d87bba9011343f3ff415ffTimo Sirainen sdbox_set_file_corrupted
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen};