bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2005-2018 Dovecot authors, see the included COPYING file */
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen/* note that everything here depends on uidlist file being locked the whole
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen time. that's why we don't have any locking of our own, or that we do things
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen that would be racy otherwise. */
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen#include "lib.h"
16f816d3f3c32ae3351834253f52ddd0212bcbf3Timo Sirainen#include "array.h"
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen#include "ioloop.h"
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen#include "hash.h"
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen#include "str.h"
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen#include "istream.h"
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen#include "eacces-error.h"
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen#include "file-dotlock.h"
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen#include "write-full.h"
181c1aff950e6f8e0556f8974e79d0747845ac0fTimo Sirainen#include "nfs-workarounds.h"
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen#include "maildir-storage.h"
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen#include "maildir-uidlist.h"
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen#include "maildir-keywords.h"
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen#include <sys/stat.h>
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen#include <utime.h>
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
6e1e9e341ffe21a69a23229c2b896d03066a071eTimo Sirainen/* how many seconds to wait before overriding dovecot-keywords.lock */
6e1e9e341ffe21a69a23229c2b896d03066a071eTimo Sirainen#define KEYWORDS_LOCK_STALE_TIMEOUT (60*2)
6e1e9e341ffe21a69a23229c2b896d03066a071eTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstruct maildir_keywords {
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen struct maildir_mailbox *mbox;
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainen struct mail_storage *storage;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen char *path;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen pool_t pool;
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen ARRAY_TYPE(keywords) list;
a75d470c9223a75801418fcdda258885c36317e0Timo Sirainen HASH_TABLE(char *, void *) hash; /* name -> idx+1 */
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen struct dotlock_settings dotlock_settings;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen time_t synced_mtime;
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen bool synced:1;
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen bool changed:1;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen};
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstruct maildir_keywords_sync_ctx {
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen struct maildir_keywords *mk;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen struct mail_index *index;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen const ARRAY_TYPE(keywords) *keywords;
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainen ARRAY(char) idx_to_chr;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen unsigned int chridx_to_idx[MAILDIR_MAX_KEYWORDS];
785d9cca224d33ca3938e9166784f6483e8a27d7Timo Sirainen bool readonly;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen};
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstruct maildir_keywords *maildir_keywords_init(struct maildir_mailbox *mbox)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen{
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen struct maildir_keywords *mk;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
d22301419109ed4a38351715e6760011421dadecTimo Sirainen mk = maildir_keywords_init_readonly(&mbox->box);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen mk->mbox = mbox;
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainen return mk;
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainen}
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainen
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainenstruct maildir_keywords *
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainenmaildir_keywords_init_readonly(struct mailbox *box)
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainen{
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainen struct maildir_keywords *mk;
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainen const char *dir;
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainen
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen if (mailbox_get_path_to(box, MAILBOX_LIST_PATH_TYPE_CONTROL, &dir) <= 0)
9fc97c8aa8190df87624d214bcc5d0b5362bec93Timo Sirainen i_unreached();
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainen
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainen mk = i_new(struct maildir_keywords, 1);
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainen mk->storage = box->storage;
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainen mk->path = i_strconcat(dir, "/" MAILDIR_KEYWORDS_NAME, NULL);
0449e50c99f3fab4485f9b983e3936aaa003c79aTimo Sirainen mk->pool = pool_alloconly_create("maildir keywords", 512);
91dca97b367c54a139c268b56a0c67f564bd9197Timo Sirainen i_array_init(&mk->list, MAILDIR_MAX_KEYWORDS);
678d0463849ba777106eb7875f27db07a5d8e3dfTimo Sirainen hash_table_create(&mk->hash, mk->pool, 0, strcase_hash, strcasecmp);
6e1e9e341ffe21a69a23229c2b896d03066a071eTimo Sirainen
6e1e9e341ffe21a69a23229c2b896d03066a071eTimo Sirainen mk->dotlock_settings.use_excl_lock =
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen box->storage->set->dotlock_use_excl;
8da095519878426b012058e6f331a669f327f47fTimo Sirainen mk->dotlock_settings.nfs_flush =
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen box->storage->set->mail_nfs_storage;
036626b19f14bef582f96e556913ae91b1d67881Timo Sirainen mk->dotlock_settings.timeout =
036626b19f14bef582f96e556913ae91b1d67881Timo Sirainen mail_storage_get_lock_timeout(box->storage,
036626b19f14bef582f96e556913ae91b1d67881Timo Sirainen KEYWORDS_LOCK_STALE_TIMEOUT + 2);
6e1e9e341ffe21a69a23229c2b896d03066a071eTimo Sirainen mk->dotlock_settings.stale_timeout = KEYWORDS_LOCK_STALE_TIMEOUT;
0ea17cea21df405e6e74a167d08e1ff0ee95dd13Timo Sirainen mk->dotlock_settings.temp_prefix =
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen mailbox_list_get_temp_prefix(box->list);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen return mk;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen}
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
9740d55b228a670047c854484d5cc979a056a9afTimo Sirainenvoid maildir_keywords_deinit(struct maildir_keywords **_mk)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen{
9740d55b228a670047c854484d5cc979a056a9afTimo Sirainen struct maildir_keywords *mk = *_mk;
9740d55b228a670047c854484d5cc979a056a9afTimo Sirainen
9740d55b228a670047c854484d5cc979a056a9afTimo Sirainen *_mk = NULL;
3ccab0bac68040f179a7de45c516cec258e28fdbTimo Sirainen hash_table_destroy(&mk->hash);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen array_free(&mk->list);
648d24583c1574441c4fa0331a90bd4d6e7996c5Timo Sirainen pool_unref(&mk->pool);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen i_free(mk->path);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen i_free(mk);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen}
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstatic void maildir_keywords_clear(struct maildir_keywords *mk)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen{
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen array_clear(&mk->list);
208028f268ac67be00590267315bf7743db02a52Timo Sirainen hash_table_clear(mk->hash, TRUE);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen p_clear(mk->pool);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen}
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstatic int maildir_keywords_sync(struct maildir_keywords *mk)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen{
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen struct istream *input;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen struct stat st;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen char *line, *p, *new_name;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen const char **strp;
9261dbf0675204898c6557591c7aa376e23a52b2Timo Sirainen unsigned int idx;
9261dbf0675204898c6557591c7aa376e23a52b2Timo Sirainen int fd;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen /* Remember that we rely on uidlist file locking in here. That's why
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen we rely on stat()'s timestamp and don't bother handling ESTALE
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen errors. */
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen if (mk->storage->set->mail_nfs_storage) {
04b8a90af181cc4c7959266855e8ed50a22ed413Timo Sirainen /* file is updated only by replacing it, no need to flush
04b8a90af181cc4c7959266855e8ed50a22ed413Timo Sirainen attribute cache */
04b8a90af181cc4c7959266855e8ed50a22ed413Timo Sirainen nfs_flush_file_handle_cache(mk->path);
04b8a90af181cc4c7959266855e8ed50a22ed413Timo Sirainen }
181c1aff950e6f8e0556f8974e79d0747845ac0fTimo Sirainen
181c1aff950e6f8e0556f8974e79d0747845ac0fTimo Sirainen if (nfs_safe_stat(mk->path, &st) < 0) {
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen if (errno == ENOENT) {
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen maildir_keywords_clear(mk);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen mk->synced = TRUE;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen return 0;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen }
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi mailbox_set_critical(&mk->mbox->box,
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi "stat(%s) failed: %m", mk->path);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen return -1;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen }
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen if (st.st_mtime == mk->synced_mtime) {
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen /* hasn't changed */
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen mk->synced = TRUE;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen return 0;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen }
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen mk->synced_mtime = st.st_mtime;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen fd = open(mk->path, O_RDONLY);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen if (fd == -1) {
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen if (errno == ENOENT) {
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen maildir_keywords_clear(mk);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen mk->synced = TRUE;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen return 0;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen }
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi mailbox_set_critical(&mk->mbox->box,
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi "open(%s) failed: %m", mk->path);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen return -1;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen }
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen maildir_keywords_clear(mk);
e93184a9055c2530366dfe617e07199603c399ddMartti Rannanjärvi input = i_stream_create_fd(fd, 1024);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen while ((line = i_stream_read_next_line(input)) != NULL) {
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen p = strchr(line, ' ');
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen if (p == NULL) {
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen /* note that when converting .customflags file this
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen case happens in the first line. */
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen continue;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen }
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen *p++ = '\0';
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
9261dbf0675204898c6557591c7aa376e23a52b2Timo Sirainen if (str_to_uint(line, &idx) < 0 ||
32d69cb0794acf725c47e6d17145ec4eb0cd6e7fTimo Sirainen idx >= MAILDIR_MAX_KEYWORDS || *p == '\0' ||
32d69cb0794acf725c47e6d17145ec4eb0cd6e7fTimo Sirainen hash_table_lookup(mk->hash, p) != NULL) {
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen /* shouldn't happen */
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen continue;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen }
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen /* save it */
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen new_name = p_strdup(mk->pool, p);
a75d470c9223a75801418fcdda258885c36317e0Timo Sirainen hash_table_insert(mk->hash, new_name, POINTER_CAST(idx + 1));
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
139143f1b798472438b813343a48601f1c564060Sergey Kitov strp = array_idx_get_space(&mk->list, idx);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen *strp = new_name;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen }
cd56a23e21f1df3f79648cf07e2f4385e2fadebbTimo Sirainen i_stream_destroy(&input);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen if (close(fd) < 0) {
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi mailbox_set_critical(&mk->mbox->box,
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi "close(%s) failed: %m", mk->path);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen return -1;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen }
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen mk->synced = TRUE;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen return 0;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen}
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstatic int
008c010ed99d23ff601c8aec48c27781dd1c6015Timo Sirainenmaildir_keywords_lookup(struct maildir_keywords *mk, const char *name,
008c010ed99d23ff601c8aec48c27781dd1c6015Timo Sirainen unsigned int *chridx_r)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen{
a75d470c9223a75801418fcdda258885c36317e0Timo Sirainen void *value;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
a75d470c9223a75801418fcdda258885c36317e0Timo Sirainen value = hash_table_lookup(mk->hash, name);
8d35582f2577c64517b2341c5d6477c7010e0a0cPhil Carmody if (value == NULL) {
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen if (mk->synced)
008c010ed99d23ff601c8aec48c27781dd1c6015Timo Sirainen return 0;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen if (maildir_keywords_sync(mk) < 0)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen return -1;
008c010ed99d23ff601c8aec48c27781dd1c6015Timo Sirainen i_assert(mk->synced);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
a75d470c9223a75801418fcdda258885c36317e0Timo Sirainen value = hash_table_lookup(mk->hash, name);
8d35582f2577c64517b2341c5d6477c7010e0a0cPhil Carmody if (value == NULL)
008c010ed99d23ff601c8aec48c27781dd1c6015Timo Sirainen return 0;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen }
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
a75d470c9223a75801418fcdda258885c36317e0Timo Sirainen *chridx_r = POINTER_CAST_TO(value, unsigned int)-1;
008c010ed99d23ff601c8aec48c27781dd1c6015Timo Sirainen return 1;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen}
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstatic void
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenmaildir_keywords_create(struct maildir_keywords *mk, const char *name,
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen unsigned int chridx)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen{
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen const char **strp;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen char *new_name;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen i_assert(chridx < MAILDIR_MAX_KEYWORDS);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen new_name = p_strdup(mk->pool, name);
a75d470c9223a75801418fcdda258885c36317e0Timo Sirainen hash_table_insert(mk->hash, new_name, POINTER_CAST(chridx + 1));
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
139143f1b798472438b813343a48601f1c564060Sergey Kitov strp = array_idx_get_space(&mk->list, chridx);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen *strp = new_name;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen mk->changed = TRUE;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen}
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstatic int
008c010ed99d23ff601c8aec48c27781dd1c6015Timo Sirainenmaildir_keywords_lookup_or_create(struct maildir_keywords *mk, const char *name,
008c010ed99d23ff601c8aec48c27781dd1c6015Timo Sirainen unsigned int *chridx_r)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen{
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen const char *const *keywords;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen unsigned int i, count;
008c010ed99d23ff601c8aec48c27781dd1c6015Timo Sirainen int ret;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
008c010ed99d23ff601c8aec48c27781dd1c6015Timo Sirainen if ((ret = maildir_keywords_lookup(mk, name, chridx_r)) != 0)
008c010ed99d23ff601c8aec48c27781dd1c6015Timo Sirainen return ret;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen /* see if we are full */
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen keywords = array_get(&mk->list, &count);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen for (i = 0; i < count; i++) {
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen if (keywords[i] == NULL)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen break;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen }
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen if (i == count && count >= MAILDIR_MAX_KEYWORDS)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen return -1;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
ba3d9eeb0bec6ed8465d68fa2480ad085559b580Timo Sirainen if (!maildir_uidlist_is_locked(mk->mbox->uidlist))
ba3d9eeb0bec6ed8465d68fa2480ad085559b580Timo Sirainen return -1;
ba3d9eeb0bec6ed8465d68fa2480ad085559b580Timo Sirainen
93c0be0b2c92501de882c2daf79105130501c988Timo Sirainen maildir_keywords_create(mk, name, i);
93c0be0b2c92501de882c2daf79105130501c988Timo Sirainen *chridx_r = i;
93c0be0b2c92501de882c2daf79105130501c988Timo Sirainen return 1;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen}
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstatic const char *
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenmaildir_keywords_idx(struct maildir_keywords *mk, unsigned int idx)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen{
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen const char *const *keywords;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen unsigned int count;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen keywords = array_get(&mk->list, &count);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen if (idx >= count) {
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen if (mk->synced)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen return NULL;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen if (maildir_keywords_sync(mk) < 0)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen return NULL;
008c010ed99d23ff601c8aec48c27781dd1c6015Timo Sirainen i_assert(mk->synced);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen keywords = array_get(&mk->list, &count);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen }
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen return idx >= count ? NULL : keywords[idx];
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen}
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainenstatic int maildir_keywords_write_fd(struct maildir_keywords *mk,
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen const char *path, int fd)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen{
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen struct maildir_mailbox *mbox = mk->mbox;
d22301419109ed4a38351715e6760011421dadecTimo Sirainen struct mailbox *box = &mbox->box;
ca98892a6b8a30ffc1fe26fcf02c7d59e3204e7eTimo Sirainen const struct mailbox_permissions *perm = mailbox_get_permissions(box);
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen const char *const *keywords;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen unsigned int i, count;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen string_t *str;
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen struct stat st;
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen str = t_str_new(256);
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen keywords = array_get(&mk->list, &count);
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen for (i = 0; i < count; i++) {
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen if (keywords[i] != NULL)
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen str_printfa(str, "%u %s\n", i, keywords[i]);
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen }
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen if (write_full(fd, str_data(str), str_len(str)) < 0) {
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi mailbox_set_critical(&mk->mbox->box,
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi "write_full(%s) failed: %m", path);
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen return -1;
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen }
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen if (fstat(fd, &st) < 0) {
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi mailbox_set_critical(&mk->mbox->box,
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi "fstat(%s) failed: %m", path);
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen return -1;
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen }
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen
ca98892a6b8a30ffc1fe26fcf02c7d59e3204e7eTimo Sirainen if (st.st_gid != perm->file_create_gid &&
ca98892a6b8a30ffc1fe26fcf02c7d59e3204e7eTimo Sirainen perm->file_create_gid != (gid_t)-1) {
ca98892a6b8a30ffc1fe26fcf02c7d59e3204e7eTimo Sirainen if (fchown(fd, (uid_t)-1, perm->file_create_gid) < 0) {
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen if (errno == EPERM) {
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi mailbox_set_critical(&mk->mbox->box, "%s",
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen eperm_error_get_chgrp("fchown", path,
ca98892a6b8a30ffc1fe26fcf02c7d59e3204e7eTimo Sirainen perm->file_create_gid,
ca98892a6b8a30ffc1fe26fcf02c7d59e3204e7eTimo Sirainen perm->file_create_gid_origin));
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen } else {
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi mailbox_set_critical(&mk->mbox->box,
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen "fchown(%s) failed: %m", path);
e156adefc1260d31a145df2f5e9b3c82050d4163Timo Sirainen }
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen }
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen }
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen /* mtime must grow every time */
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen if (st.st_mtime <= mk->synced_mtime) {
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen struct utimbuf ut;
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen mk->synced_mtime = ioloop_time <= mk->synced_mtime ?
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen mk->synced_mtime + 1 : ioloop_time;
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen ut.actime = ioloop_time;
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen ut.modtime = mk->synced_mtime;
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen if (utime(path, &ut) < 0) {
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi mailbox_set_critical(&mk->mbox->box,
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen "utime(%s) failed: %m", path);
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen return -1;
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen }
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen }
728a9c39b63b48f17600e6a1e5ebfc93d0896a24Timo Sirainen
728a9c39b63b48f17600e6a1e5ebfc93d0896a24Timo Sirainen if (fsync(fd) < 0) {
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi mailbox_set_critical(&mk->mbox->box,
728a9c39b63b48f17600e6a1e5ebfc93d0896a24Timo Sirainen "fsync(%s) failed: %m", path);
728a9c39b63b48f17600e6a1e5ebfc93d0896a24Timo Sirainen return -1;
728a9c39b63b48f17600e6a1e5ebfc93d0896a24Timo Sirainen }
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen return 0;
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen}
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainenstatic int maildir_keywords_commit(struct maildir_keywords *mk)
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen{
ca98892a6b8a30ffc1fe26fcf02c7d59e3204e7eTimo Sirainen const struct mailbox_permissions *perm;
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen struct dotlock *dotlock;
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen const char *lock_path;
e34d4597fc4995a067caafdc27676f6cebb0aa28Timo Sirainen mode_t old_mask;
32ee977e189266744ef69ac4e832fd3111d6f949Timo Sirainen int i, fd;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen mk->synced = FALSE;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
5667b85ee2cc32b5e0320ed02d9f548c291538aaTimo Sirainen if (!mk->changed || mk->mbox == NULL)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen return 0;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen lock_path = t_strconcat(mk->path, ".lock", NULL);
46b823ac3bce2c0f9f0fc73911e48d3a77b04fbeTimo Sirainen i_unlink_if_exists(lock_path);
32ee977e189266744ef69ac4e832fd3111d6f949Timo Sirainen
ca98892a6b8a30ffc1fe26fcf02c7d59e3204e7eTimo Sirainen perm = mailbox_get_permissions(&mk->mbox->box);
32ee977e189266744ef69ac4e832fd3111d6f949Timo Sirainen for (i = 0;; i++) {
32ee977e189266744ef69ac4e832fd3111d6f949Timo Sirainen /* we could just create the temp file directly, but doing it
32ee977e189266744ef69ac4e832fd3111d6f949Timo Sirainen this ways avoids potential problems with overwriting
32ee977e189266744ef69ac4e832fd3111d6f949Timo Sirainen contents in malicious symlinks */
ca98892a6b8a30ffc1fe26fcf02c7d59e3204e7eTimo Sirainen old_mask = umask(0777 & ~perm->file_create_mode);
32ee977e189266744ef69ac4e832fd3111d6f949Timo Sirainen fd = file_dotlock_open(&mk->dotlock_settings, mk->path,
32ee977e189266744ef69ac4e832fd3111d6f949Timo Sirainen DOTLOCK_CREATE_FLAG_NONBLOCK, &dotlock);
32ee977e189266744ef69ac4e832fd3111d6f949Timo Sirainen umask(old_mask);
32ee977e189266744ef69ac4e832fd3111d6f949Timo Sirainen if (fd != -1)
32ee977e189266744ef69ac4e832fd3111d6f949Timo Sirainen break;
32ee977e189266744ef69ac4e832fd3111d6f949Timo Sirainen
32ee977e189266744ef69ac4e832fd3111d6f949Timo Sirainen if (errno != ENOENT || i == MAILDIR_DELETE_RETRY_COUNT) {
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi mailbox_set_critical(&mk->mbox->box,
32ee977e189266744ef69ac4e832fd3111d6f949Timo Sirainen "file_dotlock_open(%s) failed: %m", mk->path);
32ee977e189266744ef69ac4e832fd3111d6f949Timo Sirainen return -1;
32ee977e189266744ef69ac4e832fd3111d6f949Timo Sirainen }
32ee977e189266744ef69ac4e832fd3111d6f949Timo Sirainen /* the control dir doesn't exist. create it unless the whole
32ee977e189266744ef69ac4e832fd3111d6f949Timo Sirainen mailbox was just deleted. */
d22301419109ed4a38351715e6760011421dadecTimo Sirainen if (!maildir_set_deleted(&mk->mbox->box))
32ee977e189266744ef69ac4e832fd3111d6f949Timo Sirainen return -1;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen }
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen if (maildir_keywords_write_fd(mk, lock_path, fd) < 0) {
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen file_dotlock_delete(&dotlock);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen return -1;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen }
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen if (file_dotlock_replace(&dotlock, 0) < 0) {
d4002fe1f64d25a792f76fb102ef7dc519cd4e24Martti Rannanjärvi mailbox_set_critical(&mk->mbox->box,
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen "file_dotlock_replace(%s) failed: %m", mk->path);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen return -1;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen }
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen mk->changed = FALSE;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen return 0;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen}
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenstruct maildir_keywords_sync_ctx *
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenmaildir_keywords_sync_init(struct maildir_keywords *mk,
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen struct mail_index *index)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen{
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen struct maildir_keywords_sync_ctx *ctx;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen ctx = i_new(struct maildir_keywords_sync_ctx, 1);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen ctx->mk = mk;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen ctx->index = index;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen ctx->keywords = mail_index_get_keywords(index);
91dca97b367c54a139c268b56a0c67f564bd9197Timo Sirainen i_array_init(&ctx->idx_to_chr, MAILDIR_MAX_KEYWORDS);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen return ctx;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen}
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
785d9cca224d33ca3938e9166784f6483e8a27d7Timo Sirainenstruct maildir_keywords_sync_ctx *
785d9cca224d33ca3938e9166784f6483e8a27d7Timo Sirainenmaildir_keywords_sync_init_readonly(struct maildir_keywords *mk,
785d9cca224d33ca3938e9166784f6483e8a27d7Timo Sirainen struct mail_index *index)
785d9cca224d33ca3938e9166784f6483e8a27d7Timo Sirainen{
785d9cca224d33ca3938e9166784f6483e8a27d7Timo Sirainen struct maildir_keywords_sync_ctx *ctx;
785d9cca224d33ca3938e9166784f6483e8a27d7Timo Sirainen
785d9cca224d33ca3938e9166784f6483e8a27d7Timo Sirainen ctx = maildir_keywords_sync_init(mk, index);
785d9cca224d33ca3938e9166784f6483e8a27d7Timo Sirainen ctx->readonly = TRUE;
785d9cca224d33ca3938e9166784f6483e8a27d7Timo Sirainen return ctx;
785d9cca224d33ca3938e9166784f6483e8a27d7Timo Sirainen}
785d9cca224d33ca3938e9166784f6483e8a27d7Timo Sirainen
9740d55b228a670047c854484d5cc979a056a9afTimo Sirainenvoid maildir_keywords_sync_deinit(struct maildir_keywords_sync_ctx **_ctx)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen{
9740d55b228a670047c854484d5cc979a056a9afTimo Sirainen struct maildir_keywords_sync_ctx *ctx = *_ctx;
9740d55b228a670047c854484d5cc979a056a9afTimo Sirainen
9740d55b228a670047c854484d5cc979a056a9afTimo Sirainen *_ctx = NULL;
9740d55b228a670047c854484d5cc979a056a9afTimo Sirainen
19e8adccba16ff419f5675b1575358c2956dce83Timo Sirainen T_BEGIN {
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen (void)maildir_keywords_commit(ctx->mk);
19e8adccba16ff419f5675b1575358c2956dce83Timo Sirainen } T_END;
8a6fc472a12c5ec09e2bd19f1b3acd553d75ff91Timo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen array_free(&ctx->idx_to_chr);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen i_free(ctx);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen}
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenunsigned int maildir_keywords_char_idx(struct maildir_keywords_sync_ctx *ctx,
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen char keyword)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen{
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen const char *name;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen unsigned int chridx, idx;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen i_assert(keyword >= MAILDIR_KEYWORD_FIRST &&
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen keyword <= MAILDIR_KEYWORD_LAST);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen chridx = keyword - MAILDIR_KEYWORD_FIRST;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen if (ctx->chridx_to_idx[chridx] != 0)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen return ctx->chridx_to_idx[chridx];
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen /* lookup / create */
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen name = maildir_keywords_idx(ctx->mk, chridx);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen if (name == NULL) {
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen /* name is lost. just generate one ourself. */
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen name = t_strdup_printf("unknown-%u", chridx);
008c010ed99d23ff601c8aec48c27781dd1c6015Timo Sirainen while (maildir_keywords_lookup(ctx->mk, name, &idx) > 0) {
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen /* don't create a duplicate name.
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen keep changing the name until it doesn't exist */
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen name = t_strconcat(name, "?", NULL);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen }
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen maildir_keywords_create(ctx->mk, name, chridx);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen }
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
6825360d446542046757b06064282301c4c6b27cTimo Sirainen mail_index_keyword_lookup_or_create(ctx->index, name, &idx);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen ctx->chridx_to_idx[chridx] = idx;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen return idx;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen}
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainenchar maildir_keywords_idx_char(struct maildir_keywords_sync_ctx *ctx,
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen unsigned int idx)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen{
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen const char *const *name_p;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen char *chr_p;
008c010ed99d23ff601c8aec48c27781dd1c6015Timo Sirainen unsigned int chridx;
785d9cca224d33ca3938e9166784f6483e8a27d7Timo Sirainen int ret;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
139143f1b798472438b813343a48601f1c564060Sergey Kitov chr_p = array_idx_get_space(&ctx->idx_to_chr, idx);
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen if (*chr_p != '\0')
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen return *chr_p;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen name_p = array_idx(ctx->keywords, idx);
785d9cca224d33ca3938e9166784f6483e8a27d7Timo Sirainen ret = !ctx->readonly ?
785d9cca224d33ca3938e9166784f6483e8a27d7Timo Sirainen maildir_keywords_lookup_or_create(ctx->mk, *name_p, &chridx) :
785d9cca224d33ca3938e9166784f6483e8a27d7Timo Sirainen maildir_keywords_lookup(ctx->mk, *name_p, &chridx);
785d9cca224d33ca3938e9166784f6483e8a27d7Timo Sirainen if (ret <= 0)
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen return '\0';
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen *chr_p = chridx + MAILDIR_KEYWORD_FIRST;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen return *chr_p;
cce169a321c9c629e4f2db1a69dae3b75bbcb27aTimo Sirainen}