bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2008-2018 Dovecot authors, see the included COPYING file */
109c39ab66c5aa85bf37d5bbf4ce91c6f966268bAki Tuomi MODULE_CONTEXT_REQUIRE(obj, virtual_backend_storage_module)
499fec3443374cc89fb8c83b8027c1614097d7a3Timo Sirainenextern struct virtual_mailbox_vfuncs virtual_mailbox_vfuncs;
f5a7396b31762a1f876517e13ce9065820139f7cTimo Sirainenstruct virtual_storage_module virtual_storage_module =
f5a7396b31762a1f876517e13ce9065820139f7cTimo Sirainen MODULE_CONTEXT_INIT(&mail_storage_module_register);
a18335e18aac7fc219b6f18dde083359155cc524Timo Sirainenstatic MODULE_CONTEXT_DEFINE_INIT(virtual_backend_storage_module,
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainenstatic bool ns_is_visible(struct mail_namespace *ns)
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainen return (ns->flags & NAMESPACE_FLAG_LIST_PREFIX) != 0 ||
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainen (ns->flags & NAMESPACE_FLAG_LIST_CHILDREN) != 0 ||
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainenstatic const char *get_user_visible_mailbox_name(struct mailbox *box)
942302b0247403645394d848b3c620ead262a2a5Timo Sirainenvoid virtual_box_copy_error(struct mailbox *dest, struct mailbox *src)
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainen str = t_strdup_printf("%s (for backend mailbox %s)", str, name);
942302b0247403645394d848b3c620ead262a2a5Timo Sirainen mail_storage_set_error(dest->storage, error, str);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainenstatic struct mail_storage *virtual_storage_alloc(void)
cd8507179823de33d6e8242e10dbc15d136245b5Timo Sirainen pool = pool_alloconly_create("virtual storage", 1024);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen storage = p_new(pool, struct virtual_storage, 1);
212e9e43a7d49242446331fd43ba519eda936d60Timo Sirainenvirtual_storage_create(struct mail_storage *_storage,
212e9e43a7d49242446331fd43ba519eda936d60Timo Sirainen const char **error_r)
212e9e43a7d49242446331fd43ba519eda936d60Timo Sirainen struct virtual_storage *storage = (struct virtual_storage *)_storage;
212e9e43a7d49242446331fd43ba519eda936d60Timo Sirainen value = mail_user_plugin_getenv(_storage->user, "virtual_max_open_mailboxes");
212e9e43a7d49242446331fd43ba519eda936d60Timo Sirainen storage->max_open_mailboxes = VIRTUAL_DEFAULT_MAX_OPEN_MAILBOXES;
212e9e43a7d49242446331fd43ba519eda936d60Timo Sirainen else if (str_to_uint(value, &storage->max_open_mailboxes) < 0) {
212e9e43a7d49242446331fd43ba519eda936d60Timo Sirainen *error_r = "Invalid virtual_max_open_mailboxes setting";
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainenvirtual_storage_get_list_settings(const struct mail_namespace *ns ATTR_UNUSED,
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen set->subscription_fname = VIRTUAL_SUBSCRIPTION_FILE_NAME;
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainenvirtual_backend_box_lookup_name(struct virtual_mailbox *mbox, const char *name)
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen bboxes = array_get(&mbox->backend_boxes, &count);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen for (i = 0; i < count; i++) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenvirtual_backend_box_lookup(struct virtual_mailbox *mbox, uint32_t mailbox_id)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen bboxes = array_get(&mbox->backend_boxes, &count);
4ece61edd7c266a4b8f3b290a7f0a3cb3d13ca0fTimo Sirainen for (i = 0; i < count; i++) {
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainenstatic bool virtual_mailbox_is_in_open_stack(struct virtual_storage *storage,
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen names = array_get(&storage->open_stack, &count);
c93ff0433cc3d348116f75a64f9988fedb86fd18Timo Sirainen for (i = 0; i < count; i++) {
132487b9a47c2eb6fc80cfa2b0aaf82c6dc3af56Timo Sirainenstatic int virtual_backend_box_open_failed(struct virtual_mailbox *mbox,
18a41cbd38f83429b790414c1159c097af4a59b8Timo Sirainen "Virtual mailbox open failed because of mailbox %s: %s",
18a41cbd38f83429b790414c1159c097af4a59b8Timo Sirainen mail_storage_set_error(mbox->box.storage, error, str);
755aea84bbe2b15ed7fe991f6462a93333ff571fTimo Sirainen if (error == MAIL_ERROR_PERM && bbox->wildcard) {
755aea84bbe2b15ed7fe991f6462a93333ff571fTimo Sirainen /* this mailbox wasn't explicitly specified. just skip it. */
212e9e43a7d49242446331fd43ba519eda936d60Timo Sirainenstatic int virtual_backend_box_alloc(struct virtual_mailbox *mbox,
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen struct mail_user *user = mbox->storage->storage.user;
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen ns = mail_namespace_find(user->namespaces, mailbox);
e5fd6dfd0a492e4708d4dbb7971d7fc5d7b8fd85Timo Sirainen bbox->box = mailbox_alloc(ns->list, mailbox, flags);
dd37e2ff291fbebac1b94e8aad50f3bdf7531049Timo Sirainen MODULE_CONTEXT_SET(bbox->box, virtual_storage_module, bbox);
15a2661c25010c7397f224ac83fa30433cb718ddAki Tuomi mailbox_set_reason(bbox->box, mbox->box.reason == NULL ?
15a2661c25010c7397f224ac83fa30433cb718ddAki Tuomi t_strdup_printf("virtual mailbox %s", mailbox_get_vname(&mbox->box)) :
15a2661c25010c7397f224ac83fa30433cb718ddAki Tuomi t_strdup_printf("virtual mailbox %s: %s", mailbox_get_vname(&mbox->box), mbox->box.reason));
ca0cfbcd16563416b37e3c3e9062349a9d1c9cf7Timo Sirainen /* Assume that the save_bbox exists, whether or not it truly
ca0cfbcd16563416b37e3c3e9062349a9d1c9cf7Timo Sirainen does. This at least gives a better error message than crash
ca0cfbcd16563416b37e3c3e9062349a9d1c9cf7Timo Sirainen if (mailbox_exists(bbox->box, TRUE, &existence) < 0)
ca0cfbcd16563416b37e3c3e9062349a9d1c9cf7Timo Sirainen return virtual_backend_box_open_failed(mbox, bbox);
18a41cbd38f83429b790414c1159c097af4a59b8Timo Sirainen /* ignore this. it could be intentional. */
18a41cbd38f83429b790414c1159c097af4a59b8Timo Sirainen if (mbox->storage->storage.user->mail_debug) {
18a41cbd38f83429b790414c1159c097af4a59b8Timo Sirainen "Skipping non-existing mailbox %s",
6b399f555c9c5c722d4cd5eab8faa02b2a4731d3Timo Sirainen i_array_init(&bbox->sync_pending_removes, 64);
074055dadbca01626437cc4724853a374acab6a8Timo Sirainen /* we use modseqs for being able to check quickly if backend mailboxes
074055dadbca01626437cc4724853a374acab6a8Timo Sirainen have changed. make sure the backend has them enabled. */
963842c00ef1714db2855c8952f1b46d78cba1caTimo Sirainen (void)mailbox_enable(bbox->box, MAILBOX_FEATURE_CONDSTORE);
6b399f555c9c5c722d4cd5eab8faa02b2a4731d3Timo Sirainenstatic int virtual_mailboxes_open(struct virtual_mailbox *mbox,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen bboxes = array_get(&mbox->backend_boxes, &count);
7f3b826a89bcb7a72759912e99f574b28309fe1bTimo Sirainen for (i = 0; i < count; ) {
212e9e43a7d49242446331fd43ba519eda936d60Timo Sirainen ret = virtual_backend_box_alloc(mbox, bboxes[i], flags);
6b399f555c9c5c722d4cd5eab8faa02b2a4731d3Timo Sirainen bboxes = array_get(&mbox->backend_boxes, &count);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen for (; i > 0; i--) {
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainenstatic struct mailbox *
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainenvirtual_mailbox_alloc(struct mail_storage *_storage, struct mailbox_list *list,
18ccd19c244f49665fe03cda785efa066d2c38dfTimo Sirainen struct virtual_storage *storage = (struct virtual_storage *)_storage;
c529313e1cbc22244d4528e80aa3e485f8806cd3Timo Sirainen pool = pool_alloconly_create("virtual mailbox", 2048);
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen mbox = p_new(pool, struct virtual_mailbox, 1);
aa41b2e17912d6cad3151babea6a85dd88539d28Timo Sirainen mbox->box.virtual_vfuncs = &virtual_mailbox_vfuncs;
8c909e451d14075c05d90382cf8eebc4e354f569Timo Sirainen index_storage_mailbox_alloc(&mbox->box, vname, flags, MAIL_INDEX_PREFIX);
212e9e43a7d49242446331fd43ba519eda936d60Timo Sirainenvoid virtual_backend_box_sync_mail_unset(struct virtual_backend_box *bbox)
ce930f99c6a78f2c74b00df1ad2337095978a9dbTimo Sirainenstatic bool virtual_backend_box_can_close(struct virtual_backend_box *bbox)
43dfd44ec2e993e175c4b50e9804c90a50f56d66Aki Tuomi /* we can close it if notify is set
43dfd44ec2e993e175c4b50e9804c90a50f56d66Aki Tuomi because we have no need to keep it open
43dfd44ec2e993e175c4b50e9804c90a50f56d66Aki Tuomi for tracking changes */
ce930f99c6a78f2c74b00df1ad2337095978a9dbTimo Sirainen if (array_count(&bbox->sync_pending_removes) > 0) {
ce930f99c6a78f2c74b00df1ad2337095978a9dbTimo Sirainen /* FIXME: we could probably close this by making
ce930f99c6a78f2c74b00df1ad2337095978a9dbTimo Sirainen syncing support it? */
212e9e43a7d49242446331fd43ba519eda936d60Timo Sirainenvirtual_backend_box_close_any_except(struct virtual_mailbox *mbox,
212e9e43a7d49242446331fd43ba519eda936d60Timo Sirainen /* first try to close a mailbox without any transactions.
212e9e43a7d49242446331fd43ba519eda936d60Timo Sirainen we'll also skip any mailbox that has notifications enabled (ideally
212e9e43a7d49242446331fd43ba519eda936d60Timo Sirainen these would be handled by mailbox list index) */
212e9e43a7d49242446331fd43ba519eda936d60Timo Sirainen for (bbox = mbox->open_backend_boxes_head; bbox != NULL; bbox = bbox->next_open) {
212e9e43a7d49242446331fd43ba519eda936d60Timo Sirainen /* next try to close a mailbox that has sync_mail, but no
212e9e43a7d49242446331fd43ba519eda936d60Timo Sirainen other transactions */
212e9e43a7d49242446331fd43ba519eda936d60Timo Sirainen for (bbox = mbox->open_backend_boxes_head; bbox != NULL; bbox = bbox->next_open) {
a18335e18aac7fc219b6f18dde083359155cc524Timo Sirainenstatic void virtual_backend_mailbox_close(struct mailbox *box)
a18335e18aac7fc219b6f18dde083359155cc524Timo Sirainen struct virtual_backend_box *bbox = VIRTUAL_CONTEXT(box);
a18335e18aac7fc219b6f18dde083359155cc524Timo Sirainen struct virtual_backend_mailbox *vbox = VIRTUAL_BACKEND_CONTEXT(box);
a18335e18aac7fc219b6f18dde083359155cc524Timo Sirainen /* we could have gotten here from e.g. mailbox_autocreate()
a18335e18aac7fc219b6f18dde083359155cc524Timo Sirainen without going through virtual_mailbox_close() */
a18335e18aac7fc219b6f18dde083359155cc524Timo Sirainen virtual_backend_box_close(bbox->virtual_mbox, bbox);
a18335e18aac7fc219b6f18dde083359155cc524Timo Sirainenvoid virtual_backend_mailbox_allocated(struct mailbox *box)
a18335e18aac7fc219b6f18dde083359155cc524Timo Sirainen vbox = p_new(box->pool, struct virtual_backend_mailbox, 1);
a18335e18aac7fc219b6f18dde083359155cc524Timo Sirainen MODULE_CONTEXT_SET(box, virtual_backend_storage_module, vbox);
a18335e18aac7fc219b6f18dde083359155cc524Timo Sirainenvoid virtual_backend_mailbox_opened(struct mailbox *box)
dd37e2ff291fbebac1b94e8aad50f3bdf7531049Timo Sirainen struct virtual_backend_box *bbox = VIRTUAL_CONTEXT(box);
dd37e2ff291fbebac1b94e8aad50f3bdf7531049Timo Sirainen /* not a backend for a virtual mailbox */
212e9e43a7d49242446331fd43ba519eda936d60Timo Sirainen /* the backend mailbox was already opened. if we didn't get here
212e9e43a7d49242446331fd43ba519eda936d60Timo Sirainen from virtual_backend_box_open() we may need to close a mailbox */
613d69eb09ed2b4d20720e7023b25f55c2215fbeTimo Sirainen while (mbox->backends_open_count >= mbox->storage->max_open_mailboxes &&
212e9e43a7d49242446331fd43ba519eda936d60Timo Sirainen virtual_backend_box_close_any_except(mbox, bbox))
212e9e43a7d49242446331fd43ba519eda936d60Timo Sirainen DLLIST2_APPEND_FULL(&mbox->open_backend_boxes_head,
212e9e43a7d49242446331fd43ba519eda936d60Timo Sirainenint virtual_backend_box_open(struct virtual_mailbox *mbox,
212e9e43a7d49242446331fd43ba519eda936d60Timo Sirainen /* try to keep the number of open mailboxes below the threshold
212e9e43a7d49242446331fd43ba519eda936d60Timo Sirainen before opening the mailbox */
212e9e43a7d49242446331fd43ba519eda936d60Timo Sirainen while (mbox->backends_open_count >= mbox->storage->max_open_mailboxes &&
212e9e43a7d49242446331fd43ba519eda936d60Timo Sirainen virtual_backend_box_close_any_except(mbox, bbox))
212e9e43a7d49242446331fd43ba519eda936d60Timo Sirainenvoid virtual_backend_box_close(struct virtual_mailbox *mbox,
212e9e43a7d49242446331fd43ba519eda936d60Timo Sirainen mailbox_search_result_free(&bbox->search_result);
dd37e2ff291fbebac1b94e8aad50f3bdf7531049Timo Sirainen DLLIST2_REMOVE_FULL(&mbox->open_backend_boxes_head,
0d318fe07ea8846330b6fe6b6b281ef0ccfbaacdAki Tuomi /* stop receiving notifications */
212e9e43a7d49242446331fd43ba519eda936d60Timo Sirainenvoid virtual_backend_box_accessed(struct virtual_mailbox *mbox,
212e9e43a7d49242446331fd43ba519eda936d60Timo Sirainen DLLIST2_REMOVE_FULL(&mbox->open_backend_boxes_head,
212e9e43a7d49242446331fd43ba519eda936d60Timo Sirainen DLLIST2_APPEND_FULL(&mbox->open_backend_boxes_head,
eca30f1fe8556c46abc75c94d03f59b2e89d4162Timo Sirainenstatic void virtual_mailbox_close_internal(struct virtual_mailbox *mbox)
eca30f1fe8556c46abc75c94d03f59b2e89d4162Timo Sirainen bboxes = array_get_modifiable(&mbox->backend_boxes, &count);
eca30f1fe8556c46abc75c94d03f59b2e89d4162Timo Sirainen for (i = 0; i < count; i++) {
eca30f1fe8556c46abc75c94d03f59b2e89d4162Timo Sirainen if (array_is_created(&bboxes[i]->sync_outside_expunges))
eca30f1fe8556c46abc75c94d03f59b2e89d4162Timo Sirainen array_free(&bboxes[i]->sync_outside_expunges);
c4bb0320ab43ea35fa6df88fc745fdad906cee44Timo Sirainenvirtual_mailbox_exists(struct mailbox *box, bool auto_boxes ATTR_UNUSED,
c4bb0320ab43ea35fa6df88fc745fdad906cee44Timo Sirainen return index_storage_mailbox_exists_full(box, VIRTUAL_CONFIG_FNAME,
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainenstatic int virtual_mailbox_open(struct mailbox *box)
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen struct virtual_mailbox *mbox = (struct virtual_mailbox *)box;
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen if (virtual_mailbox_is_in_open_stack(mbox->storage, box->name)) {
eca30f1fe8556c46abc75c94d03f59b2e89d4162Timo Sirainen array_append(&mbox->storage->open_stack, &box->name, 1);
eca30f1fe8556c46abc75c94d03f59b2e89d4162Timo Sirainen ret = virtual_mailboxes_open(mbox, box->flags);
eca30f1fe8556c46abc75c94d03f59b2e89d4162Timo Sirainen array_count(&mbox->storage->open_stack)-1, 1);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen if (index_storage_mailbox_open(box, FALSE) < 0)
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen mail_index_ext_register(mbox->box.index, "virtual", 0,
825b0e819a7c48a366ddca23ec78b87e8c30e9b4Aki Tuomi mail_index_ext_register(mbox->box.index, "virtual-guid", GUID_128_SIZE,
d85a1a9d9af4a36ded4d30cb277905c807de2ec5Timo Sirainen if (virtual_mailbox_ext_header_read(mbox, box->view, &broken) < 0) {
825b0e819a7c48a366ddca23ec78b87e8c30e9b4Aki Tuomi /* if GUID is missing write it here */
825b0e819a7c48a366ddca23ec78b87e8c30e9b4Aki Tuomi mail_index_update_header_ext(t, mbox->virtual_guid_ext_id,
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi "Cannot write GUID for virtual mailbox to index");
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainenstatic void virtual_mailbox_close(struct mailbox *box)
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen struct virtual_mailbox *mbox = (struct virtual_mailbox *)box;
eca30f1fe8556c46abc75c94d03f59b2e89d4162Timo Sirainenstatic void virtual_mailbox_free(struct mailbox *box)
eca30f1fe8556c46abc75c94d03f59b2e89d4162Timo Sirainen struct virtual_mailbox *mbox = (struct virtual_mailbox *)box;
17ad2164c747cedbf81dae1893063e71a3df0356Timo Sirainen const struct mailbox_update *update ATTR_UNUSED,
17ad2164c747cedbf81dae1893063e71a3df0356Timo Sirainen mail_storage_set_error(box->storage, MAIL_ERROR_NOTPOSSIBLE,
24e5e4526d8f5cbc056ab97fd0d154d0936d7a5eTimo Sirainen "Can't create virtual mailboxes");
17ad2164c747cedbf81dae1893063e71a3df0356Timo Sirainen const struct mailbox_update *update ATTR_UNUSED)
17ad2164c747cedbf81dae1893063e71a3df0356Timo Sirainen mail_storage_set_error(box->storage, MAIL_ERROR_NOTPOSSIBLE,
17ad2164c747cedbf81dae1893063e71a3df0356Timo Sirainen "Can't update virtual mailboxes");
18a41cbd38f83429b790414c1159c097af4a59b8Timo Sirainenstatic int virtual_storage_set_have_guid_flags(struct virtual_mailbox *mbox)
18a41cbd38f83429b790414c1159c097af4a59b8Timo Sirainen bboxes = array_get(&mbox->backend_boxes, &count);
18a41cbd38f83429b790414c1159c097af4a59b8Timo Sirainen for (i = 0; i < count; i++) {
18a41cbd38f83429b790414c1159c097af4a59b8Timo Sirainen if (mailbox_get_status(bboxes[i]->box, 0, &status) < 0) {
d5282594ec73c953ffb23096a2cddf373cc15f46Timo Sirainen errstr = mailbox_get_last_error(bboxes[i]->box, &error);
d5282594ec73c953ffb23096a2cddf373cc15f46Timo Sirainen /* backend mailbox was just lost - skip it */
d5282594ec73c953ffb23096a2cddf373cc15f46Timo Sirainen /* Not expected to happen, but we can't return failure
d5282594ec73c953ffb23096a2cddf373cc15f46Timo Sirainen since this could be called from
d5282594ec73c953ffb23096a2cddf373cc15f46Timo Sirainen mailbox_get_open_status() and it would panic.
d5282594ec73c953ffb23096a2cddf373cc15f46Timo Sirainen So just log the error and skip the mailbox. */
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi "Virtual mailbox: Failed to get have_guid existence for backend mailbox %s: %s",
421d30619384e72a27e2a5d13ff6525aff4d17feTimo Sirainenvirtual_storage_get_status(struct mailbox *box,
8b31f966d9688e07672ef1958dcbdb7686523c04Timo Sirainen struct virtual_mailbox *mbox = (struct virtual_mailbox *)box;
421d30619384e72a27e2a5d13ff6525aff4d17feTimo Sirainen if (index_storage_get_status(box, items, status_r) < 0)
421d30619384e72a27e2a5d13ff6525aff4d17feTimo Sirainen /* Virtual mailboxes have no cached data of their own, so the
421d30619384e72a27e2a5d13ff6525aff4d17feTimo Sirainen current value is always 0. The most important use for this
421d30619384e72a27e2a5d13ff6525aff4d17feTimo Sirainen functionality is for "doveadm index" to do FTS indexing and
421d30619384e72a27e2a5d13ff6525aff4d17feTimo Sirainen it doesn't really matter there if we set this value
421d30619384e72a27e2a5d13ff6525aff4d17feTimo Sirainen correctly or not. So for now just assume that everything is
421d30619384e72a27e2a5d13ff6525aff4d17feTimo Sirainen status_r->last_cached_seq = status_r->messages;
18a41cbd38f83429b790414c1159c097af4a59b8Timo Sirainen if (virtual_storage_set_have_guid_flags(mbox) < 0)
51327f2489a4e0e615eb9f7d921473cf8512bb79Timo Sirainenvirtual_mailbox_get_metadata(struct mailbox *box,
825b0e819a7c48a366ddca23ec78b87e8c30e9b4Aki Tuomi struct virtual_mailbox *mbox = (struct virtual_mailbox *)box;
206ed2f6fa3a6fb291498627b2da626581c07a18Timo Sirainen if (index_mailbox_get_metadata(box, items, metadata_r) < 0)
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi mailbox_set_critical(box, "GUID missing for virtual folder");
77d8223da3da23b731257596abefa77e4485b77dTimo Sirainenvirtual_notify_callback(struct mailbox *bbox ATTR_UNUSED, struct mailbox *box)
77d8223da3da23b731257596abefa77e4485b77dTimo Sirainen box->notify_callback(box, box->notify_context);
43dfd44ec2e993e175c4b50e9804c90a50f56d66Aki Tuomistatic void virtual_backend_box_changed(struct virtual_backend_box *bbox)
43dfd44ec2e993e175c4b50e9804c90a50f56d66Aki Tuomi virtual_notify_callback(bbox->box, &bbox->virtual_mbox->box);
43dfd44ec2e993e175c4b50e9804c90a50f56d66Aki Tuomistatic int virtual_notify_start(struct virtual_backend_box *bbox)
43dfd44ec2e993e175c4b50e9804c90a50f56d66Aki Tuomi if (mailbox_list_notify_init(bbox->box->list, MAILBOX_LIST_NOTIFY_STATUS,
43dfd44ec2e993e175c4b50e9804c90a50f56d66Aki Tuomi /* did not support notifications */
43dfd44ec2e993e175c4b50e9804c90a50f56d66Aki Tuomi mailbox_list_notify_wait(bbox->notify, virtual_backend_box_changed, bbox);
77d8223da3da23b731257596abefa77e4485b77dTimo Sirainenstatic void virtual_notify_changes(struct mailbox *box)
77d8223da3da23b731257596abefa77e4485b77dTimo Sirainen struct virtual_mailbox *mbox = (struct virtual_mailbox *)box;
43dfd44ec2e993e175c4b50e9804c90a50f56d66Aki Tuomi array_foreach_modifiable(&mbox->backend_boxes, bboxp) {
43dfd44ec2e993e175c4b50e9804c90a50f56d66Aki Tuomi array_foreach_modifiable(&mbox->backend_boxes, bboxp) {
81e832796cdc6af790ed7be8a6c150889f03171cTimo Sirainen /* There's only a single backend mailbox and its
81e832796cdc6af790ed7be8a6c150889f03171cTimo Sirainen indexes are already opened. Might as well use the
81e832796cdc6af790ed7be8a6c150889f03171cTimo Sirainen backend directly for notifications. */
81e832796cdc6af790ed7be8a6c150889f03171cTimo Sirainen /* we are already waiting for notifications */
81e832796cdc6af790ed7be8a6c150889f03171cTimo Sirainen /* wait for notifications */
81e832796cdc6af790ed7be8a6c150889f03171cTimo Sirainen /* it did not work, so open the mailbox and use
81e832796cdc6af790ed7be8a6c150889f03171cTimo Sirainen alternative method */
a385399497bdb50d4dfce729ffc852b75ed46a36Timo Sirainen /* we can't report error in here, so do it later */
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen struct virtual_mailbox *mbox = (struct virtual_mailbox *)box;
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen strcmp(mbox->lookup_prev_bbox->box->vname, backend_mailbox->vname) == 0)
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen bbox = virtual_backend_box_lookup_name(mbox, backend_mailbox->vname);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen seq_range_array_iter_init(&iter, backend_uids); n = 0;
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen while (seq_range_array_iter_nth(&iter, n++, &uid)) {
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen while (i < count && uids[i].real_uid < uid) i++;
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainenvirtual_get_virtual_uid_map(struct mailbox *box,
ecdce39e5ef4b62eefa9f5818f17d153fd5d710aTimo Sirainen struct virtual_mailbox *mbox = (struct virtual_mailbox *)box;
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen strcmp(mbox->lookup_prev_bbox->box->vname, backend_mailbox->vname) == 0)
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen bbox = virtual_backend_box_lookup_name(mbox, backend_mailbox->vname);
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen seq_range_array_iter_init(&iter, backend_uids); n = 0;
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen while (seq_range_array_iter_nth(&iter, n++, &uid)) {
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen while (i < count && uids[i].real_uid < uid) i++;
8d587838c414c48a331f0b54cd7ffd97e5024abdTimo Sirainen array_append(virtual_uids_r, &uids[i].virtual_uid, 1);
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainenvirtual_get_virtual_backend_boxes(struct mailbox *box,
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen struct virtual_mailbox *mbox = (struct virtual_mailbox *)box;
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen bboxes = array_get(&mbox->backend_boxes, &count);
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen for (i = 0; i < count; i++) {
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen if (!only_with_msgs || array_count(&bboxes[i]->uids) > 0)
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainenstatic bool virtual_is_inconsistent(struct mailbox *box)
4cf5f0934a25f1fd58f2780108f9d6498c455a1fTimo Sirainen struct virtual_mailbox *mbox = (struct virtual_mailbox *)box;
013c0431b58809e16fd0afea0429d0b7bb1ef8a1Timo Sirainenvirtual_list_index_has_changed(struct mailbox *box ATTR_UNUSED,
013c0431b58809e16fd0afea0429d0b7bb1ef8a1Timo Sirainen struct mail_index_view *list_view ATTR_UNUSED,
22d6302b54300d87a877675ca8fd97a1da4e5fb8Timo Sirainen uint32_t seq ATTR_UNUSED, bool quick ATTR_UNUSED)
013c0431b58809e16fd0afea0429d0b7bb1ef8a1Timo Sirainen /* we don't have any quick and easy optimizations for tracking
013c0431b58809e16fd0afea0429d0b7bb1ef8a1Timo Sirainen virtual folders. ideally we'd completely disable mailbox list
013c0431b58809e16fd0afea0429d0b7bb1ef8a1Timo Sirainen indexes for them, but this is the easiest way to do it for now. */
013c0431b58809e16fd0afea0429d0b7bb1ef8a1Timo Sirainenvirtual_list_index_update_sync(struct mailbox *box ATTR_UNUSED,
013c0431b58809e16fd0afea0429d0b7bb1ef8a1Timo Sirainen struct mail_index_transaction *trans ATTR_UNUSED,
4ba9a1d3facc515b3feb5238a16bcf91f76fac61Timo Sirainen .class_flags = MAIL_STORAGE_CLASS_FLAG_NOQUOTA,