maildir-sync.c revision 814565216099ac2b40c2862013d092d2f575802a
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* Copyright (C) 2002-2003 Timo Sirainen */
46c31f64b9f0949f00b7819f45b22f2d64b2ea27Timo Sirainenstatic int maildir_update_filename(struct mail_index *index,
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen index->update_field(update, DATA_FIELD_LOCATION, new_fname, 0);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenstatic int maildir_update_flags(struct mail_index *index,
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen flags = maildir_filename_get_flags(new_fname, rec->msg_flags);
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen if (!index->update_flags(index, rec, seq, flags, TRUE))
648d24583c1574441c4fa0331a90bd4d6e7996c5Timo Sirainenstatic int is_file_content_changed(struct mail_index *index,
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen#define DATA_HDR_SIZE (DATA_HDR_HEADER_SIZE | DATA_HDR_BODY_SIZE)
46c31f64b9f0949f00b7819f45b22f2d64b2ea27Timo Sirainen struct mail_index_data_record_header *data_hdr;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if ((rec->data_fields & DATA_HDR_INTERNAL_DATE) == 0 &&
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen (rec->data_fields & DATA_HDR_SIZE) != DATA_HDR_SIZE) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* nothing in cache, we can't know if it's changed */
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen index_file_set_syscall_error(index, path, "stat()");
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen data_hdr = mail_index_data_lookup_header(index->data, rec);
ffd9a1898a18fadfc5dce399162c25d50548f905Timo Sirainen if ((rec->data_fields & DATA_HDR_INTERNAL_DATE) != 0 &&
bbf796c17f02538058d7559bfe96d677e5b55015Timo Sirainen if ((rec->data_fields & DATA_HDR_SIZE) == DATA_HDR_SIZE &&
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen (uoff_t)st.st_size != data_hdr->body_size + data_hdr->header_size)
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen/* a char* hash function from ASU -- from glib */
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainenstatic unsigned int maildir_hash(const void *p)
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen const unsigned char *s = p;
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainen unsigned int g, h = 0;
7797aa2479e99aeb71057b7a2584b2cb72e4d3f8Timo Sirainen h = (h << 4) + *s;
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen if ((g = h & 0xf0000000UL)) {
5a07b37a9df398b5189c14872a600384208ab74bTimo Sirainen h = h ^ (g >> 24);
7d7b5c98f086ffa8ac9c90f21db17748ca607202Timo Sirainenstatic int maildir_cmp(const void *p1, const void *p2)
da985034a708db2f61394b30d117050ae6829ee5Timo Sirainen while (*s1 == *s2 && *s1 != ':' && *s1 != '\0') {
de12ff295bb3d0873b4dced5840612cbacd635efTimo Sirainenstatic void uidlist_hash_get_filenames(void *key, void *value, void *context)
6a19e109ee8c5a6f688da83a86a7f6abeb71abddTimo Sirainen if (hash_rec->action == MAILDIR_FILE_ACTION_NEW)
5626ae5e3316eced244adb6485c0927f1c7fdc41Timo Sirainen buffer_append(buf, (const void *) &key, sizeof(key));
c27f03fa8fd2ef4acd1db814fae7d90e0eb9d3aeTimo Sirainenstatic int maildir_sync_uidlist(struct mail_index *index, const char *dir,
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen if (maildir_uidlist_next(uidlist, &uid_rec) < 0)
f23298fea47eecbeded985ee2537a34c4c4ef56bTimo Sirainen /* skip over the expunged records in uidlist */
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen while (uid_rec.uid != 0 && uid_rec.uid < uid) {
519e0a461271843833a2b42626ad93f6e7ddc497Timo Sirainen index_set_corrupted(index, "Unexpectedly lost file "
6a04c5112961c5f4fb2d2f25192b3dc424d62ad0Timo Sirainen index_set_corrupted(index, "Filename mismatch for UID "
6825360d446542046757b06064282301c4c6b27cTimo Sirainen (hash_rec->action == MAILDIR_FILE_ACTION_UPDATE_FLAGS ||
6825360d446542046757b06064282301c4c6b27cTimo Sirainen hash_rec->action == MAILDIR_FILE_ACTION_NONE)) {
6825360d446542046757b06064282301c4c6b27cTimo Sirainen /* it's UID has changed */
6825360d446542046757b06064282301c4c6b27cTimo Sirainen hash_rec->action = MAILDIR_FILE_ACTION_UPDATE_CONTENT;
6825360d446542046757b06064282301c4c6b27cTimo Sirainen /* make sure filename is not invalidated by expunge */
6825360d446542046757b06064282301c4c6b27cTimo Sirainen hash_insert(files, p_strdup(pool, fname), hash_rec);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (!maildir_update_flags(index, rec, seq, fname))
89b548af722113acb5d63dfffb44423cb60f91e4Timo Sirainen if (maildir_uidlist_next(uidlist, &uid_rec) < 0)
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen index_set_corrupted(index, "Wrong messages_count in header "
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen /* if there's mails with UIDs in uidlist, write them */
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen if (!hash_lookup_full(files, uid_rec.filename,
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen /* expunged */
1e47cfede3a0b62654105daab00e97b5d660bc6bTimo Sirainen i_assert(hash_rec->action == MAILDIR_FILE_ACTION_NEW);
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen /* make sure we set the same UID for it. */
3697080532ccd9f51fac108be6079b616c7a2ddfTimo Sirainen "index.next_uid (%u) > "
14c474d9f4591c397ed0b5206af6537c7b52c924Timo Sirainen "uid_rec.uid (%u)",
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen if (!maildir_index_append_file(index, dir, orig_key))
d22390f33eedbd2413debabc0662dde5241b1aa6Timo Sirainen if (maildir_uidlist_next(uidlist, &uid_rec) < 0)
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen if (new_count == 0 || !INDEX_IS_UIDLIST_LOCKED(index)) {
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen /* all done (or can't do it since we don't have lock) */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* then there's the completely new mails. sort them by the filename
d22390f33eedbd2413debabc0662dde5241b1aa6Timo Sirainen so we should get them to same order as they were created. */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen buf = buffer_create_static_hard(pool, new_count * sizeof(const char *));
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen hash_foreach(files, uidlist_hash_get_filenames, buf);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen i_assert(buffer_get_used_size(buf) / sizeof(const char *) <= new_count);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen new_files = buffer_get_modifyable_data(buf, NULL);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen qsort(new_files, new_count, sizeof(const char *),
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen (int (*)(const void *, const void *)) strcmp);
1fd0d511885c30028aba388588151acf4ee85e75Timo Sirainen /* and finally write */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen for (i = 0; i < new_count; i++) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (!maildir_index_append_file(index, dir, new_files[i]))
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainenstatic int maildir_index_sync_dir(struct mail_index *index, const char *dir,
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen i_assert(index->lock_type == MAIL_LOCK_EXCLUSIVE);
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen if (index->header->messages_count >= INT_MAX/32) {
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen index_set_corrupted(index, "Header says %u messages",
af1e2b2ab5d1c5ca5afe482ef8c8161c17acc190Timo Sirainen /* read current messages in index into hash */
0b49cfeae91a4020a404714b11c99e8e955fb631Timo Sirainen size = nearest_power(index->header->messages_count *
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen pool = pool_alloconly_create("maildir sync", I_MAX(size, 16384));
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen files = hash_create(default_pool, pool, index->header->messages_count*2,
f239eb76f77afcbc0bfc97c9b52b4407d1bc3fe6Timo Sirainen hash_rec = p_new(pool, struct maildir_hash_rec, 1);
1fd0d511885c30028aba388588151acf4ee85e75Timo Sirainen hash_rec->action = MAILDIR_FILE_ACTION_EXPUNGE;
1fd0d511885c30028aba388588151acf4ee85e75Timo Sirainen /* Do we want to check changes in file contents? This slows down
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen things as we need to do extra stat() for all files. */
0c27b881989bc2b391281650ee89a8cc4d89f5e7Timo Sirainen check_content_changes = getenv("MAILDIR_CHECK_CONTENT_CHANGES") != NULL;
0c27b881989bc2b391281650ee89a8cc4d89f5e7Timo Sirainen return index_file_set_syscall_error(index, dir, "opendir()");
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen hash_rec = p_new(pool, struct maildir_hash_rec, 1);
1fd0d511885c30028aba388588151acf4ee85e75Timo Sirainen if (hash_rec->action != MAILDIR_FILE_ACTION_EXPUNGE) {
1fd0d511885c30028aba388588151acf4ee85e75Timo Sirainen /* FIXME: duplicate */
1fd0d511885c30028aba388588151acf4ee85e75Timo Sirainen /* new message */
1fd0d511885c30028aba388588151acf4ee85e75Timo Sirainen hash_insert(files, p_strdup(pool, d->d_name), hash_rec);
1fd0d511885c30028aba388588151acf4ee85e75Timo Sirainen /* file content changed, treat it as new message */
1fd0d511885c30028aba388588151acf4ee85e75Timo Sirainen hash_rec->action = MAILDIR_FILE_ACTION_UPDATE_CONTENT;
1fd0d511885c30028aba388588151acf4ee85e75Timo Sirainen /* make sure filename is not invalidated by expunge
1fd0d511885c30028aba388588151acf4ee85e75Timo Sirainen later. the file name may have changed also. */
1fd0d511885c30028aba388588151acf4ee85e75Timo Sirainen hash_insert(files, p_strdup(pool, d->d_name), hash_rec);
1fd0d511885c30028aba388588151acf4ee85e75Timo Sirainen } else if (strcmp(orig_key, d->d_name) != 0) {
1fd0d511885c30028aba388588151acf4ee85e75Timo Sirainen /* update filename now, flags later */
1fd0d511885c30028aba388588151acf4ee85e75Timo Sirainen hash_rec->action = MAILDIR_FILE_ACTION_UPDATE_FLAGS;
1fd0d511885c30028aba388588151acf4ee85e75Timo Sirainen if (!maildir_update_filename(index, hash_rec->rec,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index_file_set_syscall_error(index, dir, "closedir()");
da5d50534cfca45d0aaaf0bdac17b287b4588809Timo Sirainen failed = !maildir_sync_uidlist(index, dir, uidlist,
e670e1783fe4541dc3fc6109a181d45b0a9c2635Timo Sirainenstatic int maildir_new_scan_first_file(struct mail_index *index,
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen return index_file_set_syscall_error(index, dir, "opendir()");
b0be0bead3d6963149f7f2a9504b8ab5aced9af5Timo Sirainen /* find first file */
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen index_file_set_syscall_error(index, dir, "closedir()");
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainenstatic int maildir_index_lock_and_sync(struct mail_index *index, int *changes,
eb77c0a041648af50abc027811f6b7951c3a80cdTimo Sirainen /* anon-mmaped */
51b979b6414b940f04677a7e2d064be119345954Timo Sirainen return index_set_syscall_error(index, "fstat()");
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen cur_dir = t_strconcat(index->mailbox_path, "/cur", NULL);
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen return index_file_set_syscall_error(index, cur_dir, "stat()");
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen uidlist_path = t_strconcat(index->mailbox_path,
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen return index_file_set_syscall_error(index, uidlist_path,
e4b09b008ab544eb8994beecbfffefa21d855e43Timo Sirainen /* FIXME: save mtime into index header, so we don't
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen have to read it every time mailbox is opened */
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen /* we may or may not have succeeded. if we didn't,
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen just continue by syncing with existing uidlist file */
14c474d9f4591c397ed0b5206af6537c7b52c924Timo Sirainen if (!cur_changed && !INDEX_IS_UIDLIST_LOCKED(index)) {
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen /* just new mails in new/ dir, we can't sync them
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen if we can't get the lock. */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (!index->set_lock(index, MAIL_LOCK_EXCLUSIVE))
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen *uidlist_r = uidlist = maildir_uidlist_open(index);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen uidlist->uid_validity != index->header->uid_validity) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* uidvalidity changed */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "UIDVALIDITY changed in uidlist");
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen index->header->uid_validity = uidlist->uid_validity;
0d0451206a91e9f96e522075dce28a89adc2325dTimo Sirainen index->header->next_uid > uidlist->next_uid) {
a045c3aba2610c6ed0bf1c346df1c6d8f7b9fbfdTimo Sirainen index_set_corrupted(index, "index.next_uid (%u) > "
a045c3aba2610c6ed0bf1c346df1c6d8f7b9fbfdTimo Sirainen "uidlist.next_uid (%u)",
5cc772dc8b507be0bc1996b5717943ba13432e08Timo Sirainen /* move mail from new/ to cur/ */
27db4ce5fe399c981e09dcf9e885a1546afd34f4Timo Sirainen if (new_dirp != NULL && INDEX_IS_UIDLIST_LOCKED(index)) {
5cc772dc8b507be0bc1996b5717943ba13432e08Timo Sirainen new_dir = t_strconcat(index->mailbox_path, "/new", NULL);
5cc772dc8b507be0bc1996b5717943ba13432e08Timo Sirainen if (!maildir_index_build_dir(index, new_dir, cur_dir,
b0be0bead3d6963149f7f2a9504b8ab5aced9af5Timo Sirainen /* set cur/ directory's timestamp into past to make sure we
ccffbed92cb02c24fd717808a84138240bf1885bTimo Sirainen notice if new mail is moved there */
ae14dfd895881f9d1c6899b0c09f1a8b51447d61Timo Sirainen index_file_set_syscall_error(index, cur_dir, "utime()");
a045c3aba2610c6ed0bf1c346df1c6d8f7b9fbfdTimo Sirainen /* We have to always scan the cur/ directory to make
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen sure we don't miss any mails some other non-Dovecot
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen client may have moved there. FIXME: make it
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen optional, it's unnecessary with Dovecot-only setup */
31189eeac1ccaaf1201c60427f8c1087b0f5dfceTimo Sirainen /* set the cur/ directory's timestamp */
0d0451206a91e9f96e522075dce28a89adc2325dTimo Sirainen if (!maildir_index_sync_dir(index, cur_dir, uidlist))
87712707722ef7d73acb065546e61afa4455cd9eTimo Sirainen if (uidlist != NULL && uidlist->next_uid > index->header->next_uid)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* there's more new mails, but we need .lock file to
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen be able to sync them. */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (!maildir_uidlist_rewrite(index, &uidlist_mtime))
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* uidlist file synced */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* update sync stamp */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (index->lock_type == MAIL_LOCK_UNLOCK && !index->anon_mmap) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* no changes to index, we need to update it's timestamp
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ourself to get it changed */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return index_set_syscall_error(index, "utime()");
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenint maildir_index_sync(struct mail_index *index,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen enum mail_lock_type data_lock_type __attr_unused__,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen i_assert(index->lock_type != MAIL_LOCK_SHARED);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen new_dir = t_strconcat(index->mailbox_path, "/new", NULL);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (!maildir_new_scan_first_file(index, new_dir, &new_dirp, &new_dent))
2b498cb82aaad8a11adb5a27a29c55b9c334a1ecTimo Sirainen ret = maildir_index_lock_and_sync(index, changes, new_dirp, new_dent,
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen "closedir()");