maildir-sync.c revision 88553367d677170a4b703b9d52aac9eabf91c656
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen/* Copyright (C) 2004 Timo Sirainen */
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/*
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen Here's a description of how we handle Maildir synchronization and
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen it's problems:
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen We want to be as efficient as we can. The most efficient way to
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen check if changes have occured is to stat() the new/ and cur/
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen directories and uidlist file - if their mtimes haven't changed,
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen there's no changes and we don't need to do anything.
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen Problem 1: Multiple changes can happen within a single second -
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen nothing guarantees that once we synced it, someone else didn't just
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen then make a modification. Such modifications wouldn't get noticed
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen until a new modification occured later.
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen Problem 2: Syncing cur/ directory is much more costly than syncing
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen new/. Moving mails from new/ to cur/ will always change mtime of
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen cur/ causing us to sync it as well.
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen Problem 3: We may not be able to move mail from new/ to cur/
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen because we're out of quota, or simply because we're accessing a
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen read-only mailbox.
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen MAILDIR_SYNC_SECS
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen -----------------
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen Several checks below use MAILDIR_SYNC_SECS, which should be maximum
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen clock drift between all computers accessing the maildir (eg. via
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen NFS), rounded up to next second. Our default is 1 second, since
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen everyone should be using NTP.
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen Note that setting it to 0 works only if there's only one computer
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen accessing the maildir. It's practically impossible to make two
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen clocks _exactly_ synchronized.
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen It might be possible to only use file server's clock by looking at
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen the atime field, but I don't know how well that would actually work.
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen cur directory
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen -------------
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen We have dirty_cur_time variable which is set to cur/ directory's
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen mtime when it's >= time() - MAILDIR_SYNC_SECS and we _think_ we have
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen synchronized the directory.
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen When dirty_cur_time is non-zero, we don't synchronize the cur/
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen directory until
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen a) cur/'s mtime changes
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen b) opening a mail fails with ENOENT
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen c) time() > dirty_cur_time + MAILDIR_SYNC_SECS
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen This allows us to modify the maildir multiple times without having
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen to sync it at every change. The sync will eventually be done to
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen make sure we didn't miss any external changes.
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen The dirty_cur_time is set when:
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen - we change message flags
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen - we expunge messages
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen - we move mail from new/ to cur/
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen - we sync cur/ directory and it's mtime is >= time() - MAILDIR_SYNC_SECS
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen It's unset when we do the final syncing, ie. when mtime is
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen older than time() - MAILDIR_SYNC_SECS.
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen new directory
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen -------------
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen If new/'s mtime is >= time() - MAILDIR_SYNC_SECS, always synchronize
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen it. dirty_cur_time-like feature might save us a few syncs, but
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen that might break a client which saves a mail in one connection and
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen tries to fetch it in another one. new/ directory is almost always
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen empty, so syncing it should be very fast anyway. Actually this can
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen still happen if we sync only new/ dir while another client is also
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen moving mails from it to cur/ - it takes us a while to see them.
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen That's pretty unlikely to happen however, and only way to fix it
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen would be to always synchronize cur/ after new/.
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen Normally we move all mails from new/ to cur/ whenever we sync it. If
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen it's not possible for some reason, we mark the mail with "probably
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen exists in new/ directory" flag.
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen If rename() still fails because of ENOSPC or EDQUOT, we still save
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen the flag changes in index with dirty-flag on. When moving the mail
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen to cur/ directory, or when we notice it's already moved there, we
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen apply the flag changes to the filename, rename it and remove the
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen dirty flag. If there's dirty flags, this should be tried every time
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen after expunge or when closing the mailbox.
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen uidlist
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen -------
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen This file contains UID <-> filename mappings. It's updated only when
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen new mail arrives, so it may contain filenames that have already been
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen deleted. Updating is done by getting uidlist.lock file, writing the
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen whole uidlist into it and rename()ing it over the old uidlist. This
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen means there's no need to lock the file for reading.
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen Whenever uidlist is rewritten, it's mtime must be larger than the old
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen one's. Use utime() before rename() if needed. Note that inode checking
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen wouldn't have been sufficient as inode numbers can be reused.
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen This file is usually read the first time you need to know filename for
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen given UID. After that it's not re-read unless new mails come that we
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen don't know about.
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen broken clients
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen --------------
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen Originally the middle identifier in Maildir filename was specified
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen only as <process id>_<delivery counter>. That however created a
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen problem with randomized PIDs which made it possible that the same
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen PID was reused within one second.
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen So if within one second a mail was delivered, MUA moved it to cur/
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen and another mail was delivered by a new process using same PID as
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen the first one, we likely ended up overwriting the first mail when
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen the second mail was moved over it.
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen Nowadays everyone should be giving a bit more specific identifier,
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen for example include microseconds in it which Dovecot does.
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen There's a simple way to prevent this from happening in some cases:
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen Don't move the mail from new/ to cur/ if it's mtime is >= time() -
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen MAILDIR_SYNC_SECS. The second delivery's link() call then fails
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen because the file is already in new/, and it will then use a
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen different filename. There's a few problems with this however:
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen - it requires extra stat() call which is unneeded extra I/O
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen - another MUA might still move the mail to cur/
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen - if first file's flags are modified by either Dovecot or another
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen MUA, it's moved to cur/ (you _could_ just do the dirty-flagging
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen but that'd be ugly)
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen Because this is useful only for very few people and it requires
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen extra I/O, I decided not to implement this. It should be however
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen quite easy to do since we need to be able to deal with files in new/
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen in any case.
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen It's also possible to never accidentally overwrite a mail by using
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen link() + unlink() rather than rename(). This however isn't very
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen good idea as it introduces potential race conditions when multiple
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen clients are accessing the mailbox:
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen Trying to move the same mail from new/ to cur/ at the same time:
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen a) Client 1 uses slightly different filename than client 2,
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen for example one sets read-flag on but the other doesn't.
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen You have the same mail duplicated now.
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen b) Client 3 sees the mail between Client 1's and 2's link() calls
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen and changes it's flag. You have the same mail duplicated now.
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen And it gets worse when they're unlink()ing in cur/ directory:
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen c) Client 1 changes mails's flag and client 2 changes it back
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen between 1's link() and unlink(). The mail is now expunged.
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen d) If you try to deal with the duplicates by unlink()ing another
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen one of them, you might end up unlinking both of them.
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen So, what should we do then if we notice a duplicate? First of all,
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen it might not be a duplicate at all, readdir() might have just
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen returned it twice because it was just renamed. What we should do is
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen create a completely new base name for it and rename() it to that.
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen If the call fails with ENOENT, it only means that it wasn't a
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen duplicate after all.
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen*/
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "lib.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "ioloop.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "buffer.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "hash.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "str.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "maildir-storage.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "maildir-uidlist.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include <stdio.h>
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen#include <stddef.h>
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include <unistd.h>
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include <dirent.h>
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include <sys/stat.h>
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#define MAILDIR_SYNC_SECS 1
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#define MAILDIR_FILENAME_FLAG_FOUND 128
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstruct maildir_sync_context {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct index_mailbox *ibox;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char *new_dir, *cur_dir;
b7b81543899e306c71e6152516d8698416162bcbTimo Sirainen int partial;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct maildir_uidlist_sync_ctx *uidlist_sync_ctx;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen};
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainenstruct maildir_index_sync_context {
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen struct index_mailbox *ibox;
b35f7104715edee0cfac6d46ab0b342033867eb7Timo Sirainen struct mail_index_view *view;
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen struct mail_index_sync_ctx *sync_ctx;
b35f7104715edee0cfac6d46ab0b342033867eb7Timo Sirainen struct mail_index_transaction *trans;
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen struct mail_index_sync_rec sync_rec;
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen uint32_t seq;
b92813e2f96d4b28f989528ed5dd6115da7d9bdbTimo Sirainen int dirty_state;
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen};
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic int maildir_expunge(struct index_mailbox *ibox, const char *path,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen void *context __attr_unused__)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen if (unlink(path) == 0) {
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen ibox->dirty_cur_time = ioloop_time;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return 1;
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (errno == ENOENT)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_storage_set_critical(ibox->box.storage,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "unlink(%s) failed: %m", path);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic int maildir_sync_flags(struct index_mailbox *ibox, const char *path,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen void *context)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen struct maildir_index_sync_context *ctx = context;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char *newpath;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen enum mail_flags flags;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen uint8_t flags8;
d8b77aef97e89f1ccc5cbdaef77be9052279e35fTimo Sirainen keywords_mask_t keywords;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
b92813e2f96d4b28f989528ed5dd6115da7d9bdbTimo Sirainen ctx->dirty_state = 0;
b35f7104715edee0cfac6d46ab0b342033867eb7Timo Sirainen
d8b77aef97e89f1ccc5cbdaef77be9052279e35fTimo Sirainen (void)maildir_filename_get_flags(path, &flags, keywords);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen flags8 = flags;
d8b77aef97e89f1ccc5cbdaef77be9052279e35fTimo Sirainen mail_index_sync_flags_apply(&ctx->sync_rec, &flags8, keywords);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
d8b77aef97e89f1ccc5cbdaef77be9052279e35fTimo Sirainen newpath = maildir_filename_set_flags(path, flags8, keywords);
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen if (rename(path, newpath) == 0) {
b92813e2f96d4b28f989528ed5dd6115da7d9bdbTimo Sirainen if ((flags8 & MAIL_INDEX_MAIL_FLAG_DIRTY) != 0)
b92813e2f96d4b28f989528ed5dd6115da7d9bdbTimo Sirainen ctx->dirty_state = -1;
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen ibox->dirty_cur_time = ioloop_time;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return 1;
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (errno == ENOENT)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
b35f7104715edee0cfac6d46ab0b342033867eb7Timo Sirainen if (ENOSPACE(errno) || errno == EACCES) {
b35f7104715edee0cfac6d46ab0b342033867eb7Timo Sirainen memset(keywords, 0, sizeof(keywords));
b35f7104715edee0cfac6d46ab0b342033867eb7Timo Sirainen mail_index_update_flags(ctx->trans, ctx->seq, MODIFY_ADD,
b35f7104715edee0cfac6d46ab0b342033867eb7Timo Sirainen MAIL_INDEX_MAIL_FLAG_DIRTY, keywords);
b92813e2f96d4b28f989528ed5dd6115da7d9bdbTimo Sirainen ctx->dirty_state = 1;
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen return 1;
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen }
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_storage_set_critical(ibox->box.storage,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "rename(%s, %s) failed: %m", path, newpath);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic int maildir_sync_record(struct index_mailbox *ibox,
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen struct maildir_index_sync_context *ctx)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen struct mail_index_sync_rec *sync_rec = &ctx->sync_rec;
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen struct mail_index_view *view = ctx->view;
659fe5d24825b160cae512538088020d97a60239Timo Sirainen uint32_t seq, seq1, seq2, uid;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen switch (sync_rec->type) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen case MAIL_INDEX_SYNC_TYPE_APPEND:
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen break;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen case MAIL_INDEX_SYNC_TYPE_EXPUNGE:
659fe5d24825b160cae512538088020d97a60239Timo Sirainen /* make it go through sequences to avoid looping through huge
659fe5d24825b160cae512538088020d97a60239Timo Sirainen holes in UID range */
659fe5d24825b160cae512538088020d97a60239Timo Sirainen if (mail_index_lookup_uid_range(view, sync_rec->uid1,
659fe5d24825b160cae512538088020d97a60239Timo Sirainen sync_rec->uid2,
659fe5d24825b160cae512538088020d97a60239Timo Sirainen &seq1, &seq2) < 0)
659fe5d24825b160cae512538088020d97a60239Timo Sirainen return -1;
659fe5d24825b160cae512538088020d97a60239Timo Sirainen
659fe5d24825b160cae512538088020d97a60239Timo Sirainen if (seq1 == 0)
659fe5d24825b160cae512538088020d97a60239Timo Sirainen break;
659fe5d24825b160cae512538088020d97a60239Timo Sirainen
659fe5d24825b160cae512538088020d97a60239Timo Sirainen for (seq = seq1; seq <= seq2; seq++) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (mail_index_lookup_uid(view, seq, &uid) < 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (maildir_file_do(ibox, uid, maildir_expunge,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen NULL) < 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen break;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen case MAIL_INDEX_SYNC_TYPE_FLAGS:
659fe5d24825b160cae512538088020d97a60239Timo Sirainen if (mail_index_lookup_uid_range(view, sync_rec->uid1,
659fe5d24825b160cae512538088020d97a60239Timo Sirainen sync_rec->uid2,
659fe5d24825b160cae512538088020d97a60239Timo Sirainen &seq1, &seq2) < 0)
659fe5d24825b160cae512538088020d97a60239Timo Sirainen return -1;
659fe5d24825b160cae512538088020d97a60239Timo Sirainen
659fe5d24825b160cae512538088020d97a60239Timo Sirainen if (seq1 == 0)
659fe5d24825b160cae512538088020d97a60239Timo Sirainen break;
659fe5d24825b160cae512538088020d97a60239Timo Sirainen
659fe5d24825b160cae512538088020d97a60239Timo Sirainen for (ctx->seq = seq1; ctx->seq <= seq2; ctx->seq++) {
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen if (mail_index_lookup_uid(view, ctx->seq, &uid) < 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return -1;
b35f7104715edee0cfac6d46ab0b342033867eb7Timo Sirainen if (maildir_file_do(ibox, uid,
b35f7104715edee0cfac6d46ab0b342033867eb7Timo Sirainen maildir_sync_flags, ctx) < 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return -1;
b92813e2f96d4b28f989528ed5dd6115da7d9bdbTimo Sirainen if (ctx->dirty_state < 0) {
b92813e2f96d4b28f989528ed5dd6115da7d9bdbTimo Sirainen /* flag isn't dirty anymore */
b92813e2f96d4b28f989528ed5dd6115da7d9bdbTimo Sirainen keywords_mask_t keywords;
b92813e2f96d4b28f989528ed5dd6115da7d9bdbTimo Sirainen
b92813e2f96d4b28f989528ed5dd6115da7d9bdbTimo Sirainen memset(keywords, 0, sizeof(keywords));
b92813e2f96d4b28f989528ed5dd6115da7d9bdbTimo Sirainen mail_index_update_flags(ctx->trans, ctx->seq,
b92813e2f96d4b28f989528ed5dd6115da7d9bdbTimo Sirainen MODIFY_REMOVE,
b92813e2f96d4b28f989528ed5dd6115da7d9bdbTimo Sirainen MAIL_INDEX_MAIL_FLAG_DIRTY, keywords);
b35f7104715edee0cfac6d46ab0b342033867eb7Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen break;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenint maildir_sync_last_commit(struct index_mailbox *ibox)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen struct maildir_index_sync_context ctx;
b35f7104715edee0cfac6d46ab0b342033867eb7Timo Sirainen uint32_t seq;
b35f7104715edee0cfac6d46ab0b342033867eb7Timo Sirainen uoff_t offset;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen int ret;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (ibox->commit_log_file_seq == 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen memset(&ctx, 0, sizeof(ctx));
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen ctx.ibox = ibox;
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen
65d6ca3fb5450b81df0190d9e9aa62c00fed5116Timo Sirainen ibox->syncing_commit = TRUE;
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen ret = mail_index_sync_begin(ibox->index, &ctx.sync_ctx, &ctx.view,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ibox->commit_log_file_seq,
88553367d677170a4b703b9d52aac9eabf91c656Timo Sirainen ibox->commit_log_file_offset, FALSE);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (ret > 0) {
b35f7104715edee0cfac6d46ab0b342033867eb7Timo Sirainen ctx.trans = mail_index_transaction_begin(ctx.view, FALSE);
b35f7104715edee0cfac6d46ab0b342033867eb7Timo Sirainen
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen while ((ret = mail_index_sync_next(ctx.sync_ctx,
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen &ctx.sync_rec)) > 0) {
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen if (maildir_sync_record(ibox, &ctx) < 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen break;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
b35f7104715edee0cfac6d46ab0b342033867eb7Timo Sirainen if (mail_index_transaction_commit(ctx.trans, &seq, &offset) < 0)
b35f7104715edee0cfac6d46ab0b342033867eb7Timo Sirainen ret = -1;
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen if (mail_index_sync_end(ctx.sync_ctx) < 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
65d6ca3fb5450b81df0190d9e9aa62c00fed5116Timo Sirainen ibox->syncing_commit = FALSE;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (ret == 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ibox->commit_log_file_seq = 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ibox->commit_log_file_offset = 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen } else {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_storage_set_index_error(ibox);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return ret;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic struct maildir_sync_context *
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenmaildir_sync_context_new(struct index_mailbox *ibox)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct maildir_sync_context *ctx;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ctx = t_new(struct maildir_sync_context, 1);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ctx->ibox = ibox;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ctx->new_dir = t_strconcat(ibox->path, "/new", NULL);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ctx->cur_dir = t_strconcat(ibox->path, "/cur", NULL);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return ctx;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic void maildir_sync_deinit(struct maildir_sync_context *ctx)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (ctx->uidlist_sync_ctx != NULL)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen (void)maildir_uidlist_sync_deinit(ctx->uidlist_sync_ctx);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic int maildir_fix_duplicate(struct index_mailbox *ibox, const char *dir,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char *old_fname)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char *new_fname, *old_path, *new_path;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen int ret = 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen t_push();
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen old_path = t_strconcat(dir, "/", old_fname, NULL);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen new_fname = maildir_generate_tmp_filename(&ioloop_timeval);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen new_path = t_strconcat(ibox->path, "/new/", new_fname, NULL);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (rename(old_path, new_path) == 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen i_warning("Fixed duplicate in %s: %s -> %s",
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ibox->path, old_fname, new_fname);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen } else if (errno != ENOENT) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_storage_set_critical(ibox->box.storage,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "rename(%s, %s) failed: %m", old_path, new_path);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen t_pop();
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return ret;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic int maildir_scan_dir(struct maildir_sync_context *ctx, int new_dir)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct mail_storage *storage = ctx->ibox->box.storage;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char *dir;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen DIR *dirp;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen string_t *src, *dest;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct dirent *dp;
641f0c0900ee6e7cf9667f4b40ed95cec7d0cdcaTimo Sirainen enum maildir_uidlist_rec_flag flags;
641f0c0900ee6e7cf9667f4b40ed95cec7d0cdcaTimo Sirainen int move_new, ret = 1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen dir = new_dir ? ctx->new_dir : ctx->cur_dir;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen dirp = opendir(dir);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (dirp == NULL) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_storage_set_critical(storage,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "opendir(%s) failed: %m", dir);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
d77c309fccbc6a7594f8cb08fb01009fa613c568Timo Sirainen t_push();
d77c309fccbc6a7594f8cb08fb01009fa613c568Timo Sirainen src = t_str_new(1024);
d77c309fccbc6a7594f8cb08fb01009fa613c568Timo Sirainen dest = t_str_new(1024);
d77c309fccbc6a7594f8cb08fb01009fa613c568Timo Sirainen
c282435b57b6f9696fc12d99ea70468b7bdfe24cTimo Sirainen move_new = new_dir && !mailbox_is_readonly(&ctx->ibox->box) &&
c282435b57b6f9696fc12d99ea70468b7bdfe24cTimo Sirainen !ctx->ibox->keep_recent;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen while ((dp = readdir(dirp)) != NULL) {
7bd6001d84ecc1792ddfd54fe8efa63c509d90b1Timo Sirainen if (dp->d_name[0] == '.')
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen continue;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen ret = maildir_uidlist_sync_next_pre(ctx->uidlist_sync_ctx,
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen dp->d_name);
d67f54632110cfb6aafe2d7cd1f99b031c0b208aTimo Sirainen if (ret == 0) {
d482b35af87f5fd872bad007da0475813a401a49Timo Sirainen /* new file and we couldn't lock uidlist, check this
d482b35af87f5fd872bad007da0475813a401a49Timo Sirainen later in next sync. */
d67f54632110cfb6aafe2d7cd1f99b031c0b208aTimo Sirainen if (new_dir)
d67f54632110cfb6aafe2d7cd1f99b031c0b208aTimo Sirainen ctx->ibox->last_new_mtime = 0;
d67f54632110cfb6aafe2d7cd1f99b031c0b208aTimo Sirainen else
d67f54632110cfb6aafe2d7cd1f99b031c0b208aTimo Sirainen ctx->ibox->dirty_cur_time = ioloop_time;
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen continue;
d67f54632110cfb6aafe2d7cd1f99b031c0b208aTimo Sirainen }
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen if (ret < 0)
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen break;
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen
641f0c0900ee6e7cf9667f4b40ed95cec7d0cdcaTimo Sirainen flags = 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (move_new) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen str_truncate(src, 0);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen str_truncate(dest, 0);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen str_printfa(src, "%s/%s", ctx->new_dir, dp->d_name);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen str_printfa(dest, "%s/%s", ctx->cur_dir, dp->d_name);
ead79af955bccc360de08c150918474c1809748eTimo Sirainen if (strchr(dp->d_name, ':') == NULL)
ead79af955bccc360de08c150918474c1809748eTimo Sirainen str_append(dest, ":2,");
641f0c0900ee6e7cf9667f4b40ed95cec7d0cdcaTimo Sirainen if (rename(str_c(src), str_c(dest)) == 0) {
d67f54632110cfb6aafe2d7cd1f99b031c0b208aTimo Sirainen /* we moved it - it's \Recent for us */
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen ctx->ibox->dirty_cur_time = ioloop_time;
641f0c0900ee6e7cf9667f4b40ed95cec7d0cdcaTimo Sirainen flags |= MAILDIR_UIDLIST_REC_FLAG_MOVED |
641f0c0900ee6e7cf9667f4b40ed95cec7d0cdcaTimo Sirainen MAILDIR_UIDLIST_REC_FLAG_RECENT;
641f0c0900ee6e7cf9667f4b40ed95cec7d0cdcaTimo Sirainen } else if (ENOTFOUND(errno)) {
641f0c0900ee6e7cf9667f4b40ed95cec7d0cdcaTimo Sirainen /* someone else moved it already */
641f0c0900ee6e7cf9667f4b40ed95cec7d0cdcaTimo Sirainen flags |= MAILDIR_UIDLIST_REC_FLAG_MOVED;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen } else if (ENOSPACE(errno)) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* not enough disk space, leave here */
641f0c0900ee6e7cf9667f4b40ed95cec7d0cdcaTimo Sirainen flags |= MAILDIR_UIDLIST_REC_FLAG_NEW_DIR |
641f0c0900ee6e7cf9667f4b40ed95cec7d0cdcaTimo Sirainen MAILDIR_UIDLIST_REC_FLAG_RECENT;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen move_new = FALSE;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen } else {
641f0c0900ee6e7cf9667f4b40ed95cec7d0cdcaTimo Sirainen flags |= MAILDIR_UIDLIST_REC_FLAG_NEW_DIR |
641f0c0900ee6e7cf9667f4b40ed95cec7d0cdcaTimo Sirainen MAILDIR_UIDLIST_REC_FLAG_RECENT;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_storage_set_critical(storage,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "rename(%s, %s) failed: %m",
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen str_c(src), str_c(dest));
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
641f0c0900ee6e7cf9667f4b40ed95cec7d0cdcaTimo Sirainen } else if (new_dir) {
641f0c0900ee6e7cf9667f4b40ed95cec7d0cdcaTimo Sirainen flags |= MAILDIR_UIDLIST_REC_FLAG_NEW_DIR |
641f0c0900ee6e7cf9667f4b40ed95cec7d0cdcaTimo Sirainen MAILDIR_UIDLIST_REC_FLAG_RECENT;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = maildir_uidlist_sync_next(ctx->uidlist_sync_ctx,
641f0c0900ee6e7cf9667f4b40ed95cec7d0cdcaTimo Sirainen dp->d_name, flags);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (ret <= 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (ret < 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen break;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* possibly duplicate - try fixing it */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (maildir_fix_duplicate(ctx->ibox,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen dir, dp->d_name) < 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen break;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (closedir(dirp) < 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_storage_set_critical(storage,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "closedir(%s) failed: %m", dir);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
d77c309fccbc6a7594f8cb08fb01009fa613c568Timo Sirainen
d77c309fccbc6a7594f8cb08fb01009fa613c568Timo Sirainen t_pop();
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return ret < 0 ? -1 : 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic int maildir_sync_quick_check(struct maildir_sync_context *ctx,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen int *new_changed_r, int *cur_changed_r)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct index_mailbox *ibox = ctx->ibox;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct stat st;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen time_t new_mtime, cur_mtime;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen *new_changed_r = *cur_changed_r = FALSE;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (stat(ctx->new_dir, &st) < 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_storage_set_critical(ibox->box.storage,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "stat(%s) failed: %m", ctx->new_dir);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen new_mtime = st.st_mtime;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (stat(ctx->cur_dir, &st) < 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_storage_set_critical(ibox->box.storage,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "stat(%s) failed: %m", ctx->cur_dir);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen cur_mtime = st.st_mtime;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
bde6382cf65fba6165dc3603f5419e194d87f404Timo Sirainen if (ibox->dirty_cur_time == 0) {
bde6382cf65fba6165dc3603f5419e194d87f404Timo Sirainen /* cur stamp is kept in index, we don't have to sync if
bde6382cf65fba6165dc3603f5419e194d87f404Timo Sirainen someone else has done it and updated the index. make sure
bde6382cf65fba6165dc3603f5419e194d87f404Timo Sirainen we have a fresh index with latest sync_stamp. */
bde6382cf65fba6165dc3603f5419e194d87f404Timo Sirainen struct mail_index_view *view;
bde6382cf65fba6165dc3603f5419e194d87f404Timo Sirainen const struct mail_index_header *hdr;
bde6382cf65fba6165dc3603f5419e194d87f404Timo Sirainen
bde6382cf65fba6165dc3603f5419e194d87f404Timo Sirainen if (mail_index_refresh(ibox->index) < 0) {
bde6382cf65fba6165dc3603f5419e194d87f404Timo Sirainen mail_storage_set_index_error(ibox);
bde6382cf65fba6165dc3603f5419e194d87f404Timo Sirainen return -1;
bde6382cf65fba6165dc3603f5419e194d87f404Timo Sirainen }
bde6382cf65fba6165dc3603f5419e194d87f404Timo Sirainen
bde6382cf65fba6165dc3603f5419e194d87f404Timo Sirainen view = mail_index_view_open(ibox->index);
bde6382cf65fba6165dc3603f5419e194d87f404Timo Sirainen if (mail_index_get_header(view, &hdr) < 0) {
bde6382cf65fba6165dc3603f5419e194d87f404Timo Sirainen mail_index_view_close(view);
bde6382cf65fba6165dc3603f5419e194d87f404Timo Sirainen mail_storage_set_index_error(ibox);
bde6382cf65fba6165dc3603f5419e194d87f404Timo Sirainen return -1;
bde6382cf65fba6165dc3603f5419e194d87f404Timo Sirainen }
bde6382cf65fba6165dc3603f5419e194d87f404Timo Sirainen ibox->last_cur_mtime = hdr->sync_stamp;
bde6382cf65fba6165dc3603f5419e194d87f404Timo Sirainen mail_index_view_close(view);
b7b81543899e306c71e6152516d8698416162bcbTimo Sirainen }
b7b81543899e306c71e6152516d8698416162bcbTimo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (new_mtime != ibox->last_new_mtime ||
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen new_mtime >= ibox->last_new_sync_time - MAILDIR_SYNC_SECS) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen *new_changed_r = TRUE;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ibox->last_new_mtime = new_mtime;
0c17af9d3f9323136a94e66605776ed4462a172dTimo Sirainen ibox->last_new_sync_time = ioloop_time;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
b7b81543899e306c71e6152516d8698416162bcbTimo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (cur_mtime != ibox->last_cur_mtime ||
033557e1c8ebec5ae31f2f24fab90226a1945168Timo Sirainen (ibox->dirty_cur_time != 0 &&
033557e1c8ebec5ae31f2f24fab90226a1945168Timo Sirainen ioloop_time - ibox->dirty_cur_time > MAILDIR_SYNC_SECS)) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* cur/ changed, or delayed cur/ check */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen *cur_changed_r = TRUE;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ibox->last_cur_mtime = cur_mtime;
d67f54632110cfb6aafe2d7cd1f99b031c0b208aTimo Sirainen
d67f54632110cfb6aafe2d7cd1f99b031c0b208aTimo Sirainen ibox->dirty_cur_time =
d67f54632110cfb6aafe2d7cd1f99b031c0b208aTimo Sirainen cur_mtime >= ioloop_time - MAILDIR_SYNC_SECS ?
d67f54632110cfb6aafe2d7cd1f99b031c0b208aTimo Sirainen cur_mtime : 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic int maildir_sync_index(struct maildir_sync_context *ctx)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct index_mailbox *ibox = ctx->ibox;
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen struct maildir_index_sync_context sync_ctx;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct maildir_uidlist_iter_ctx *iter;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct mail_index_transaction *trans;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct mail_index_view *view;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const struct mail_index_header *hdr;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const struct mail_index_record *rec;
b7b81543899e306c71e6152516d8698416162bcbTimo Sirainen uint32_t seq, uid;
b7b81543899e306c71e6152516d8698416162bcbTimo Sirainen enum maildir_uidlist_rec_flag uflags;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char *filename;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen enum mail_flags flags;
d8b77aef97e89f1ccc5cbdaef77be9052279e35fTimo Sirainen keywords_mask_t keywords;
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen uint32_t uid_validity, next_uid;
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen int ret;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen memset(&sync_ctx, 0, sizeof(sync_ctx));
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen sync_ctx.ibox = ibox;
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen if (mail_index_sync_begin(ibox->index, &sync_ctx.sync_ctx, &view,
88553367d677170a4b703b9d52aac9eabf91c656Timo Sirainen (uint32_t)-1, (uoff_t)-1, FALSE) <= 0) {
0add8c99ca65e56dbf613595fc37c41aafff3f7fTimo Sirainen mail_storage_set_index_error(ibox);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen sync_ctx.view = view;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen ret = mail_index_get_header(view, &hdr);
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen i_assert(ret == 0); /* view is locked, can't happen */
31ddc75584c5cde53d2e78a737587f2e7fdcb0d2Timo Sirainen
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen uid_validity = maildir_uidlist_get_uid_validity(ibox->uidlist);
92888ef30960c30ccc9e030fe7eab5d4d04a7d1cTimo Sirainen if (uid_validity != hdr->uid_validity &&
92888ef30960c30ccc9e030fe7eab5d4d04a7d1cTimo Sirainen uid_validity != 0 && hdr->uid_validity != 0) {
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen /* uidvalidity changed and mailbox isn't being initialized,
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen index must be rebuilt */
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen mail_storage_set_critical(ibox->box.storage,
6969f13267ad8495a132ece39d34be36b9883f37Timo Sirainen "Maildir %s sync: UIDVALIDITY changed (%u -> %u)",
6969f13267ad8495a132ece39d34be36b9883f37Timo Sirainen ibox->path, hdr->uid_validity, uid_validity);
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen mail_index_mark_corrupted(ibox->index);
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen (void)mail_index_sync_end(sync_ctx.sync_ctx);
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen return -1;
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen }
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen trans = mail_index_transaction_begin(view, FALSE);
b35f7104715edee0cfac6d46ab0b342033867eb7Timo Sirainen sync_ctx.trans = trans;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen seq = 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen iter = maildir_uidlist_iter_init(ibox->uidlist);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen while (maildir_uidlist_iter_next(iter, &uid, &uflags, &filename)) {
d8b77aef97e89f1ccc5cbdaef77be9052279e35fTimo Sirainen maildir_filename_get_flags(filename, &flags, keywords);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
d482b35af87f5fd872bad007da0475813a401a49Timo Sirainen if ((uflags & MAILDIR_UIDLIST_REC_FLAG_RECENT) != 0 &&
d482b35af87f5fd872bad007da0475813a401a49Timo Sirainen (uflags & MAILDIR_UIDLIST_REC_FLAG_NEW_DIR) != 0 &&
d482b35af87f5fd872bad007da0475813a401a49Timo Sirainen (uflags & MAILDIR_UIDLIST_REC_FLAG_MOVED) == 0) {
d482b35af87f5fd872bad007da0475813a401a49Timo Sirainen /* mail is recent for next session as well */
d482b35af87f5fd872bad007da0475813a401a49Timo Sirainen flags |= MAIL_RECENT;
d482b35af87f5fd872bad007da0475813a401a49Timo Sirainen }
d482b35af87f5fd872bad007da0475813a401a49Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen __again:
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen seq++;
b7b81543899e306c71e6152516d8698416162bcbTimo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (seq > hdr->messages_count) {
d67f54632110cfb6aafe2d7cd1f99b031c0b208aTimo Sirainen if (uid < hdr->next_uid) {
316689d0ef55e2fa4e2fb4ac5b1ea35ce65688d3Timo Sirainen /* most likely a race condition: we read the
316689d0ef55e2fa4e2fb4ac5b1ea35ce65688d3Timo Sirainen maildir, then someone else expunged messages
316689d0ef55e2fa4e2fb4ac5b1ea35ce65688d3Timo Sirainen and committed changes to index. so, this
316689d0ef55e2fa4e2fb4ac5b1ea35ce65688d3Timo Sirainen message shouldn't actually exist. mark it
316689d0ef55e2fa4e2fb4ac5b1ea35ce65688d3Timo Sirainen racy and check in next sync.
316689d0ef55e2fa4e2fb4ac5b1ea35ce65688d3Timo Sirainen
316689d0ef55e2fa4e2fb4ac5b1ea35ce65688d3Timo Sirainen the difference between this and the later
316689d0ef55e2fa4e2fb4ac5b1ea35ce65688d3Timo Sirainen check is that this one happens when messages
316689d0ef55e2fa4e2fb4ac5b1ea35ce65688d3Timo Sirainen are expunged from the end */
a33fde36c57839342dafdf3c8d4b5c009e3ef4bcTimo Sirainen if ((uflags &
a33fde36c57839342dafdf3c8d4b5c009e3ef4bcTimo Sirainen MAILDIR_UIDLIST_REC_FLAG_NONSYNCED) != 0) {
a33fde36c57839342dafdf3c8d4b5c009e3ef4bcTimo Sirainen /* partial syncing */
a33fde36c57839342dafdf3c8d4b5c009e3ef4bcTimo Sirainen continue;
a33fde36c57839342dafdf3c8d4b5c009e3ef4bcTimo Sirainen }
316689d0ef55e2fa4e2fb4ac5b1ea35ce65688d3Timo Sirainen if ((uflags &
316689d0ef55e2fa4e2fb4ac5b1ea35ce65688d3Timo Sirainen MAILDIR_UIDLIST_REC_FLAG_RACING) != 0) {
316689d0ef55e2fa4e2fb4ac5b1ea35ce65688d3Timo Sirainen mail_storage_set_critical(
316689d0ef55e2fa4e2fb4ac5b1ea35ce65688d3Timo Sirainen ibox->box.storage,
6969f13267ad8495a132ece39d34be36b9883f37Timo Sirainen "Maildir %s sync: "
6969f13267ad8495a132ece39d34be36b9883f37Timo Sirainen "UID < next_uid "
316689d0ef55e2fa4e2fb4ac5b1ea35ce65688d3Timo Sirainen "(%u < %u, file = %s)",
6969f13267ad8495a132ece39d34be36b9883f37Timo Sirainen ibox->path, uid, hdr->next_uid,
6969f13267ad8495a132ece39d34be36b9883f37Timo Sirainen filename);
316689d0ef55e2fa4e2fb4ac5b1ea35ce65688d3Timo Sirainen mail_index_mark_corrupted(ibox->index);
316689d0ef55e2fa4e2fb4ac5b1ea35ce65688d3Timo Sirainen ret = -1;
316689d0ef55e2fa4e2fb4ac5b1ea35ce65688d3Timo Sirainen break;
316689d0ef55e2fa4e2fb4ac5b1ea35ce65688d3Timo Sirainen }
316689d0ef55e2fa4e2fb4ac5b1ea35ce65688d3Timo Sirainen ibox->dirty_cur_time = ioloop_time;
316689d0ef55e2fa4e2fb4ac5b1ea35ce65688d3Timo Sirainen maildir_uidlist_add_flags(ibox->uidlist,
316689d0ef55e2fa4e2fb4ac5b1ea35ce65688d3Timo Sirainen filename,
316689d0ef55e2fa4e2fb4ac5b1ea35ce65688d3Timo Sirainen MAILDIR_UIDLIST_REC_FLAG_RACING);
316689d0ef55e2fa4e2fb4ac5b1ea35ce65688d3Timo Sirainen
316689d0ef55e2fa4e2fb4ac5b1ea35ce65688d3Timo Sirainen seq--;
316689d0ef55e2fa4e2fb4ac5b1ea35ce65688d3Timo Sirainen continue;
d67f54632110cfb6aafe2d7cd1f99b031c0b208aTimo Sirainen }
d67f54632110cfb6aafe2d7cd1f99b031c0b208aTimo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_index_append(trans, uid, &seq);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_index_update_flags(trans, seq, MODIFY_REPLACE,
d8b77aef97e89f1ccc5cbdaef77be9052279e35fTimo Sirainen flags, keywords);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen continue;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (mail_index_lookup(view, seq, &rec) < 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen break;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (rec->uid < uid) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* expunged */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_index_expunge(trans, seq);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen goto __again;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (rec->uid > uid) {
a038139a470d2942759b9b86a9852aee7b460996Timo Sirainen /* most likely a race condition: we read the
a038139a470d2942759b9b86a9852aee7b460996Timo Sirainen maildir, then someone else expunged messages and
a038139a470d2942759b9b86a9852aee7b460996Timo Sirainen committed changes to index. so, this message
316689d0ef55e2fa4e2fb4ac5b1ea35ce65688d3Timo Sirainen shouldn't actually exist. mark it racy and check
316689d0ef55e2fa4e2fb4ac5b1ea35ce65688d3Timo Sirainen in next sync. */
a33fde36c57839342dafdf3c8d4b5c009e3ef4bcTimo Sirainen if ((uflags &
a33fde36c57839342dafdf3c8d4b5c009e3ef4bcTimo Sirainen MAILDIR_UIDLIST_REC_FLAG_NONSYNCED) != 0) {
a33fde36c57839342dafdf3c8d4b5c009e3ef4bcTimo Sirainen /* partial syncing */
a33fde36c57839342dafdf3c8d4b5c009e3ef4bcTimo Sirainen seq--;
a33fde36c57839342dafdf3c8d4b5c009e3ef4bcTimo Sirainen continue;
a33fde36c57839342dafdf3c8d4b5c009e3ef4bcTimo Sirainen }
316689d0ef55e2fa4e2fb4ac5b1ea35ce65688d3Timo Sirainen if ((uflags & MAILDIR_UIDLIST_REC_FLAG_RACING) != 0) {
a038139a470d2942759b9b86a9852aee7b460996Timo Sirainen mail_storage_set_critical(ibox->box.storage,
6969f13267ad8495a132ece39d34be36b9883f37Timo Sirainen "Maildir %s sync: UID inserted in the "
a038139a470d2942759b9b86a9852aee7b460996Timo Sirainen "middle of mailbox "
a038139a470d2942759b9b86a9852aee7b460996Timo Sirainen "(%u > %u, file = %s)",
6969f13267ad8495a132ece39d34be36b9883f37Timo Sirainen ibox->path, rec->uid, uid, filename);
a038139a470d2942759b9b86a9852aee7b460996Timo Sirainen mail_index_mark_corrupted(ibox->index);
a038139a470d2942759b9b86a9852aee7b460996Timo Sirainen ret = -1;
a038139a470d2942759b9b86a9852aee7b460996Timo Sirainen break;
bde6382cf65fba6165dc3603f5419e194d87f404Timo Sirainen }
316689d0ef55e2fa4e2fb4ac5b1ea35ce65688d3Timo Sirainen
316689d0ef55e2fa4e2fb4ac5b1ea35ce65688d3Timo Sirainen ibox->dirty_cur_time = ioloop_time;
316689d0ef55e2fa4e2fb4ac5b1ea35ce65688d3Timo Sirainen maildir_uidlist_add_flags(ibox->uidlist, filename,
316689d0ef55e2fa4e2fb4ac5b1ea35ce65688d3Timo Sirainen MAILDIR_UIDLIST_REC_FLAG_RACING);
a038139a470d2942759b9b86a9852aee7b460996Timo Sirainen
a038139a470d2942759b9b86a9852aee7b460996Timo Sirainen seq--;
a038139a470d2942759b9b86a9852aee7b460996Timo Sirainen continue;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0aa66a8e037c08604e71446d6ba52150a17dfc21Timo Sirainen if ((rec->flags & MAIL_RECENT) != 0) {
0aa66a8e037c08604e71446d6ba52150a17dfc21Timo Sirainen index_mailbox_set_recent(ibox, seq);
0aa66a8e037c08604e71446d6ba52150a17dfc21Timo Sirainen if (ibox->keep_recent) {
0aa66a8e037c08604e71446d6ba52150a17dfc21Timo Sirainen flags |= MAIL_RECENT;
0aa66a8e037c08604e71446d6ba52150a17dfc21Timo Sirainen } else {
0aa66a8e037c08604e71446d6ba52150a17dfc21Timo Sirainen mail_index_update_flags(trans, seq,
0aa66a8e037c08604e71446d6ba52150a17dfc21Timo Sirainen MODIFY_REMOVE,
0aa66a8e037c08604e71446d6ba52150a17dfc21Timo Sirainen MAIL_RECENT, keywords);
0aa66a8e037c08604e71446d6ba52150a17dfc21Timo Sirainen }
0aa66a8e037c08604e71446d6ba52150a17dfc21Timo Sirainen }
0aa66a8e037c08604e71446d6ba52150a17dfc21Timo Sirainen
a33fde36c57839342dafdf3c8d4b5c009e3ef4bcTimo Sirainen if ((uflags & MAILDIR_UIDLIST_REC_FLAG_NONSYNCED) != 0) {
a33fde36c57839342dafdf3c8d4b5c009e3ef4bcTimo Sirainen /* partial syncing */
a33fde36c57839342dafdf3c8d4b5c009e3ef4bcTimo Sirainen continue;
a33fde36c57839342dafdf3c8d4b5c009e3ef4bcTimo Sirainen }
a33fde36c57839342dafdf3c8d4b5c009e3ef4bcTimo Sirainen
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen if ((rec->flags & MAIL_INDEX_MAIL_FLAG_DIRTY) != 0) {
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen /* we haven't been able to update maildir with this
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen record's flag changes. don't sync them. */
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen continue;
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen }
fd3d711f219fd6813492acbe051e04327f0ca0f0Timo Sirainen
d482b35af87f5fd872bad007da0475813a401a49Timo Sirainen if (((uint8_t)flags & ~MAIL_RECENT) !=
d482b35af87f5fd872bad007da0475813a401a49Timo Sirainen (rec->flags & (MAIL_FLAGS_MASK^MAIL_RECENT)) ||
d8b77aef97e89f1ccc5cbdaef77be9052279e35fTimo Sirainen memcmp(keywords, rec->keywords,
d8b77aef97e89f1ccc5cbdaef77be9052279e35fTimo Sirainen INDEX_KEYWORDS_BYTE_COUNT) != 0) {
d482b35af87f5fd872bad007da0475813a401a49Timo Sirainen /* FIXME: this is wrong if there's pending changes in
d482b35af87f5fd872bad007da0475813a401a49Timo Sirainen transaction log already. it gets fixed in next sync
d482b35af87f5fd872bad007da0475813a401a49Timo Sirainen however.. */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_index_update_flags(trans, seq, MODIFY_REPLACE,
d8b77aef97e89f1ccc5cbdaef77be9052279e35fTimo Sirainen flags, keywords);
d482b35af87f5fd872bad007da0475813a401a49Timo Sirainen } else if ((flags & MAIL_RECENT) == 0 &&
d482b35af87f5fd872bad007da0475813a401a49Timo Sirainen (rec->flags & MAIL_RECENT) != 0) {
d482b35af87f5fd872bad007da0475813a401a49Timo Sirainen /* just remove recent flag */
d482b35af87f5fd872bad007da0475813a401a49Timo Sirainen memset(keywords, 0, sizeof(keywords));
d482b35af87f5fd872bad007da0475813a401a49Timo Sirainen mail_index_update_flags(trans, seq, MODIFY_REMOVE,
d482b35af87f5fd872bad007da0475813a401a49Timo Sirainen MAIL_RECENT, keywords);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen maildir_uidlist_iter_deinit(iter);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
b7b81543899e306c71e6152516d8698416162bcbTimo Sirainen if (!ctx->partial) {
b7b81543899e306c71e6152516d8698416162bcbTimo Sirainen /* expunge the rest */
b7b81543899e306c71e6152516d8698416162bcbTimo Sirainen for (seq++; seq <= hdr->messages_count; seq++)
b7b81543899e306c71e6152516d8698416162bcbTimo Sirainen mail_index_expunge(trans, seq);
b7b81543899e306c71e6152516d8698416162bcbTimo Sirainen }
b7b81543899e306c71e6152516d8698416162bcbTimo Sirainen
b35f7104715edee0cfac6d46ab0b342033867eb7Timo Sirainen /* now, sync the index */
714dfaac096e6190898aaa7cce315c2fa247f05aTimo Sirainen ibox->syncing_commit = TRUE;
b35f7104715edee0cfac6d46ab0b342033867eb7Timo Sirainen while ((ret = mail_index_sync_next(sync_ctx.sync_ctx,
b35f7104715edee0cfac6d46ab0b342033867eb7Timo Sirainen &sync_ctx.sync_rec)) > 0) {
b35f7104715edee0cfac6d46ab0b342033867eb7Timo Sirainen if (maildir_sync_record(ibox, &sync_ctx) < 0) {
b35f7104715edee0cfac6d46ab0b342033867eb7Timo Sirainen ret = -1;
b35f7104715edee0cfac6d46ab0b342033867eb7Timo Sirainen break;
b35f7104715edee0cfac6d46ab0b342033867eb7Timo Sirainen }
b35f7104715edee0cfac6d46ab0b342033867eb7Timo Sirainen }
714dfaac096e6190898aaa7cce315c2fa247f05aTimo Sirainen ibox->syncing_commit = FALSE;
b35f7104715edee0cfac6d46ab0b342033867eb7Timo Sirainen
bde6382cf65fba6165dc3603f5419e194d87f404Timo Sirainen if (ibox->dirty_cur_time == 0 &&
bde6382cf65fba6165dc3603f5419e194d87f404Timo Sirainen ibox->last_cur_mtime != (time_t)hdr->sync_stamp) {
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen uint32_t sync_stamp = ibox->last_cur_mtime;
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen mail_index_update_header(trans,
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen offsetof(struct mail_index_header, sync_stamp),
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen &sync_stamp, sizeof(sync_stamp));
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen }
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen
92888ef30960c30ccc9e030fe7eab5d4d04a7d1cTimo Sirainen if (hdr->uid_validity == 0) {
92888ef30960c30ccc9e030fe7eab5d4d04a7d1cTimo Sirainen /* get the initial uidvalidity */
92888ef30960c30ccc9e030fe7eab5d4d04a7d1cTimo Sirainen if (maildir_uidlist_update(ibox->uidlist) < 0)
92888ef30960c30ccc9e030fe7eab5d4d04a7d1cTimo Sirainen ret = -1;
92888ef30960c30ccc9e030fe7eab5d4d04a7d1cTimo Sirainen uid_validity = maildir_uidlist_get_uid_validity(ibox->uidlist);
92888ef30960c30ccc9e030fe7eab5d4d04a7d1cTimo Sirainen if (uid_validity == 0) {
92888ef30960c30ccc9e030fe7eab5d4d04a7d1cTimo Sirainen uid_validity = ioloop_time;
92888ef30960c30ccc9e030fe7eab5d4d04a7d1cTimo Sirainen maildir_uidlist_set_uid_validity(ibox->uidlist,
92888ef30960c30ccc9e030fe7eab5d4d04a7d1cTimo Sirainen uid_validity);
92888ef30960c30ccc9e030fe7eab5d4d04a7d1cTimo Sirainen }
92888ef30960c30ccc9e030fe7eab5d4d04a7d1cTimo Sirainen } else if (uid_validity == 0) {
92888ef30960c30ccc9e030fe7eab5d4d04a7d1cTimo Sirainen maildir_uidlist_set_uid_validity(ibox->uidlist,
92888ef30960c30ccc9e030fe7eab5d4d04a7d1cTimo Sirainen hdr->uid_validity);
92888ef30960c30ccc9e030fe7eab5d4d04a7d1cTimo Sirainen }
92888ef30960c30ccc9e030fe7eab5d4d04a7d1cTimo Sirainen
92888ef30960c30ccc9e030fe7eab5d4d04a7d1cTimo Sirainen if (uid_validity != hdr->uid_validity && uid_validity != 0) {
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen mail_index_update_header(trans,
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen offsetof(struct mail_index_header, uid_validity),
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen &uid_validity, sizeof(uid_validity));
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen }
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen next_uid = maildir_uidlist_get_next_uid(ibox->uidlist);
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen if (next_uid != 0 && hdr->next_uid != next_uid) {
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen mail_index_update_header(trans,
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen offsetof(struct mail_index_header, next_uid),
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen &next_uid, sizeof(next_uid));
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen }
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (ret < 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_index_transaction_rollback(trans);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen else {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen uint32_t seq;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen uoff_t offset;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (mail_index_transaction_commit(trans, &seq, &offset) < 0)
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen ret = -1;
8a3d609fdd84f5938c82e8e7eeb84a24ab41b317Timo Sirainen else if (seq != 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ibox->commit_log_file_seq = seq;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ibox->commit_log_file_offset = offset;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen if (mail_index_sync_end(sync_ctx.sync_ctx) < 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (ret == 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ibox->commit_log_file_seq = 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ibox->commit_log_file_offset = 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen } else {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail_storage_set_index_error(ibox);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return ret;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
a38ef15060e45e5060bb24c403a580b9a57a818cTimo Sirainenstatic int maildir_sync_context(struct maildir_sync_context *ctx, int forced)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen int ret, new_changed, cur_changed;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
a38ef15060e45e5060bb24c403a580b9a57a818cTimo Sirainen if (!forced) {
a38ef15060e45e5060bb24c403a580b9a57a818cTimo Sirainen if (maildir_sync_quick_check(ctx, &new_changed, &cur_changed) < 0)
a38ef15060e45e5060bb24c403a580b9a57a818cTimo Sirainen return -1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
a38ef15060e45e5060bb24c403a580b9a57a818cTimo Sirainen if (!new_changed && !cur_changed)
a38ef15060e45e5060bb24c403a580b9a57a818cTimo Sirainen return 0;
a38ef15060e45e5060bb24c403a580b9a57a818cTimo Sirainen } else {
a38ef15060e45e5060bb24c403a580b9a57a818cTimo Sirainen new_changed = cur_changed = TRUE;
a38ef15060e45e5060bb24c403a580b9a57a818cTimo Sirainen }
641f0c0900ee6e7cf9667f4b40ed95cec7d0cdcaTimo Sirainen
d77c309fccbc6a7594f8cb08fb01009fa613c568Timo Sirainen /* we have to lock uidlist immediately, otherwise there's race
d77c309fccbc6a7594f8cb08fb01009fa613c568Timo Sirainen conditions with other processes who might write older maildir
d77c309fccbc6a7594f8cb08fb01009fa613c568Timo Sirainen file list into uidlist.
d77c309fccbc6a7594f8cb08fb01009fa613c568Timo Sirainen
d77c309fccbc6a7594f8cb08fb01009fa613c568Timo Sirainen alternative would be to lock it when new files are found, but
65d6ca3fb5450b81df0190d9e9aa62c00fed5116Timo Sirainen the directory scans _must_ be restarted then.
65d6ca3fb5450b81df0190d9e9aa62c00fed5116Timo Sirainen
65d6ca3fb5450b81df0190d9e9aa62c00fed5116Timo Sirainen if we got here through maildir_sync_last_commit(), we can't sync
65d6ca3fb5450b81df0190d9e9aa62c00fed5116Timo Sirainen index as it's already being synced. so, don't try locking uidlist
65d6ca3fb5450b81df0190d9e9aa62c00fed5116Timo Sirainen either, we only want to find new filename for some mail.
65d6ca3fb5450b81df0190d9e9aa62c00fed5116Timo Sirainen */
65d6ca3fb5450b81df0190d9e9aa62c00fed5116Timo Sirainen if (!ctx->ibox->syncing_commit) {
65d6ca3fb5450b81df0190d9e9aa62c00fed5116Timo Sirainen if ((ret = maildir_uidlist_try_lock(ctx->ibox->uidlist)) < 0)
65d6ca3fb5450b81df0190d9e9aa62c00fed5116Timo Sirainen return ret;
65d6ca3fb5450b81df0190d9e9aa62c00fed5116Timo Sirainen if (ret == 0 && !forced) {
65d6ca3fb5450b81df0190d9e9aa62c00fed5116Timo Sirainen /* we didn't get a lock, don't do syncing unless we
65d6ca3fb5450b81df0190d9e9aa62c00fed5116Timo Sirainen really want to check for expunges or renames. new
65d6ca3fb5450b81df0190d9e9aa62c00fed5116Timo Sirainen files won't be added. */
65d6ca3fb5450b81df0190d9e9aa62c00fed5116Timo Sirainen return 0;
65d6ca3fb5450b81df0190d9e9aa62c00fed5116Timo Sirainen }
a38ef15060e45e5060bb24c403a580b9a57a818cTimo Sirainen }
73c76fa7340a107229c530196d026aadeae979c7Timo Sirainen
73c76fa7340a107229c530196d026aadeae979c7Timo Sirainen ctx->partial = !cur_changed;
73c76fa7340a107229c530196d026aadeae979c7Timo Sirainen ctx->uidlist_sync_ctx =
73c76fa7340a107229c530196d026aadeae979c7Timo Sirainen maildir_uidlist_sync_init(ctx->ibox->uidlist, ctx->partial);
d77c309fccbc6a7594f8cb08fb01009fa613c568Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (maildir_scan_dir(ctx, TRUE) < 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return -1;
b7b81543899e306c71e6152516d8698416162bcbTimo Sirainen if (cur_changed) {
b7b81543899e306c71e6152516d8698416162bcbTimo Sirainen if (maildir_scan_dir(ctx, FALSE) < 0)
b7b81543899e306c71e6152516d8698416162bcbTimo Sirainen return -1;
b7b81543899e306c71e6152516d8698416162bcbTimo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
d67f54632110cfb6aafe2d7cd1f99b031c0b208aTimo Sirainen /* finish uidlist syncing, but keep it still locked */
73c76fa7340a107229c530196d026aadeae979c7Timo Sirainen maildir_uidlist_sync_finish(ctx->uidlist_sync_ctx);
65d6ca3fb5450b81df0190d9e9aa62c00fed5116Timo Sirainen if (!ctx->ibox->syncing_commit) {
65d6ca3fb5450b81df0190d9e9aa62c00fed5116Timo Sirainen if (maildir_sync_index(ctx) < 0)
65d6ca3fb5450b81df0190d9e9aa62c00fed5116Timo Sirainen return -1;
65d6ca3fb5450b81df0190d9e9aa62c00fed5116Timo Sirainen }
d67f54632110cfb6aafe2d7cd1f99b031c0b208aTimo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = maildir_uidlist_sync_deinit(ctx->uidlist_sync_ctx);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ctx->uidlist_sync_ctx = NULL;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return ret;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
a38ef15060e45e5060bb24c403a580b9a57a818cTimo Sirainenint maildir_storage_sync_force(struct index_mailbox *ibox)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct maildir_sync_context *ctx;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen int ret;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ctx = maildir_sync_context_new(ibox);
a38ef15060e45e5060bb24c403a580b9a57a818cTimo Sirainen ret = maildir_sync_context(ctx, TRUE);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen maildir_sync_deinit(ctx);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return ret;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
d756ebcfa96bd7cff02097c8f26df9df368b81b1Timo Sirainenstruct mailbox_sync_context *
d756ebcfa96bd7cff02097c8f26df9df368b81b1Timo Sirainenmaildir_storage_sync_init(struct mailbox *box, enum mailbox_sync_flags flags)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct index_mailbox *ibox = (struct index_mailbox *)box;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen struct maildir_sync_context *ctx;
d756ebcfa96bd7cff02097c8f26df9df368b81b1Timo Sirainen int ret = 0;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if ((flags & MAILBOX_SYNC_FLAG_FAST) == 0 ||
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ibox->sync_last_check + MAILBOX_FULL_SYNC_INTERVAL <= ioloop_time) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ibox->sync_last_check = ioloop_time;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ctx = maildir_sync_context_new(ibox);
a38ef15060e45e5060bb24c403a580b9a57a818cTimo Sirainen ret = maildir_sync_context(ctx, FALSE);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen maildir_sync_deinit(ctx);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
d756ebcfa96bd7cff02097c8f26df9df368b81b1Timo Sirainen return index_mailbox_sync_init(box, flags, ret < 0);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}