maildir-sync.c revision b142deb9a831c89b1bb9129ada655f3e56b9d4cc
bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (C) 2004 Timo Sirainen */
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody/*
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody Here's a description of how we handle Maildir synchronization and
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody it's problems:
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody We want to be as efficient as we can. The most efficient way to
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody check if changes have occurred is to stat() the new/ and cur/
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody directories and uidlist file - if their mtimes haven't changed,
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody there's no changes and we don't need to do anything.
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody
87b4215acbf020aa5b8dea686b23fc664140cda0Stephan Bosch Problem 1: Multiple changes can happen within a single second -
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody nothing guarantees that once we synced it, someone else didn't just
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody then make a modification. Such modifications wouldn't get noticed
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody until a new modification occurred later.
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody Problem 2: Syncing cur/ directory is much more costly than syncing
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody new/. Moving mails from new/ to cur/ will always change mtime of
87b4215acbf020aa5b8dea686b23fc664140cda0Stephan Bosch cur/ causing us to sync it as well.
87b4215acbf020aa5b8dea686b23fc664140cda0Stephan Bosch
87b4215acbf020aa5b8dea686b23fc664140cda0Stephan Bosch Problem 3: We may not be able to move mail from new/ to cur/
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody because we're out of quota, or simply because we're accessing a
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody read-only mailbox.
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody MAILDIR_SYNC_SECS
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody -----------------
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody
191153d1a5b0eb0c129139570e3aa5212f28d2acJosef 'Jeff' Sipek Several checks below use MAILDIR_SYNC_SECS, which should be maximum
62461eb609e1d852e027cf4e07d30d51288678a2Aki Tuomi clock drift between all computers accessing the maildir (eg. via
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody NFS), rounded up to next second. Our default is 1 second, since
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody everyone should be using NTP.
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody Note that setting it to 0 works only if there's only one computer
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody accessing the maildir. It's practically impossible to make two
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody clocks _exactly_ synchronized.
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody It might be possible to only use file server's clock by looking at
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody the atime field, but I don't know how well that would actually work.
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody
191153d1a5b0eb0c129139570e3aa5212f28d2acJosef 'Jeff' Sipek cur directory
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody -------------
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody We have dirty_cur_time variable which is set to cur/ directory's
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody mtime when it's >= time() - MAILDIR_SYNC_SECS and we _think_ we have
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody synchronized the directory.
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody When dirty_cur_time is non-zero, we don't synchronize the cur/
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody directory until
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody a) cur/'s mtime changes
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody b) opening a mail fails with ENOENT
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody c) time() > dirty_cur_time + MAILDIR_SYNC_SECS
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody This allows us to modify the maildir multiple times without having
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody to sync it at every change. The sync will eventually be done to
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody make sure we didn't miss any external changes.
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody The dirty_cur_time is set when:
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody - we change message flags
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody - we expunge messages
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody - we move mail from new/ to cur/
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody - we sync cur/ directory and it's mtime is >= time() - MAILDIR_SYNC_SECS
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody It's unset when we do the final syncing, ie. when mtime is
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody older than time() - MAILDIR_SYNC_SECS.
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody new directory
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody -------------
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody If new/'s mtime is >= time() - MAILDIR_SYNC_SECS, always synchronize
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody it. dirty_cur_time-like feature might save us a few syncs, but
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody that might break a client which saves a mail in one connection and
d6bbf85809664a810726b5c711c7213874d8df57Phil Carmody tries to fetch it in another one. new/ directory is almost always
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody empty, so syncing it should be very fast anyway. Actually this can
d6bbf85809664a810726b5c711c7213874d8df57Phil Carmody still happen if we sync only new/ dir while another client is also
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody moving mails from it to cur/ - it takes us a while to see them.
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody That's pretty unlikely to happen however, and only way to fix it
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody would be to always synchronize cur/ after new/.
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch Normally we move all mails from new/ to cur/ whenever we sync it. If
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch it's not possible for some reason, we mark the mail with "probably
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch exists in new/ directory" flag.
87b4215acbf020aa5b8dea686b23fc664140cda0Stephan Bosch
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch If rename() still fails because of ENOSPC or EDQUOT, we still save
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch the flag changes in index with dirty-flag on. When moving the mail
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch to cur/ directory, or when we notice it's already moved there, we
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch apply the flag changes to the filename, rename it and remove the
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch dirty flag. If there's dirty flags, this should be tried every time
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch after expunge or when closing the mailbox.
87b4215acbf020aa5b8dea686b23fc664140cda0Stephan Bosch
87b4215acbf020aa5b8dea686b23fc664140cda0Stephan Bosch uidlist
87b4215acbf020aa5b8dea686b23fc664140cda0Stephan Bosch -------
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch This file contains UID <-> filename mappings. It's updated only when
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch new mail arrives, so it may contain filenames that have already been
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch deleted. Updating is done by getting uidlist.lock file, writing the
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch whole uidlist into it and rename()ing it over the old uidlist. This
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch means there's no need to lock the file for reading.
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch
191153d1a5b0eb0c129139570e3aa5212f28d2acJosef 'Jeff' Sipek Whenever uidlist is rewritten, it's mtime must be larger than the old
62461eb609e1d852e027cf4e07d30d51288678a2Aki Tuomi one's. Use utime() before rename() if needed. Note that inode checking
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch wouldn't have been sufficient as inode numbers can be reused.
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch This file is usually read the first time you need to know filename for
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch given UID. After that it's not re-read unless new mails come that we
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch don't know about.
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch broken clients
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch --------------
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch Originally the middle identifier in Maildir filename was specified
191153d1a5b0eb0c129139570e3aa5212f28d2acJosef 'Jeff' Sipek only as <process id>_<delivery counter>. That however created a
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch problem with randomized PIDs which made it possible that the same
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch PID was reused within one second.
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch So if within one second a mail was delivered, MUA moved it to cur/
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch and another mail was delivered by a new process using same PID as
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch the first one, we likely ended up overwriting the first mail when
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch the second mail was moved over it.
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch Nowadays everyone should be giving a bit more specific identifier,
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch for example include microseconds in it which Dovecot does.
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch There's a simple way to prevent this from happening in some cases:
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch Don't move the mail from new/ to cur/ if it's mtime is >= time() -
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch MAILDIR_SYNC_SECS. The second delivery's link() call then fails
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch because the file is already in new/, and it will then use a
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch different filename. There's a few problems with this however:
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch - it requires extra stat() call which is unneeded extra I/O
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch - another MUA might still move the mail to cur/
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch - if first file's flags are modified by either Dovecot or another
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch MUA, it's moved to cur/ (you _could_ just do the dirty-flagging
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch but that'd be ugly)
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch Because this is useful only for very few people and it requires
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch extra I/O, I decided not to implement this. It should be however
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch quite easy to do since we need to be able to deal with files in new/
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch in any case.
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch It's also possible to never accidentally overwrite a mail by using
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch link() + unlink() rather than rename(). This however isn't very
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch good idea as it introduces potential race conditions when multiple
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch clients are accessing the mailbox:
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch Trying to move the same mail from new/ to cur/ at the same time:
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch a) Client 1 uses slightly different filename than client 2,
d6bbf85809664a810726b5c711c7213874d8df57Phil Carmody for example one sets read-flag on but the other doesn't.
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch You have the same mail duplicated now.
d6bbf85809664a810726b5c711c7213874d8df57Phil Carmody
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch b) Client 3 sees the mail between Client 1's and 2's link() calls
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch and changes it's flag. You have the same mail duplicated now.
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch And it gets worse when they're unlink()ing in cur/ directory:
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch c) Client 1 changes mails's flag and client 2 changes it back
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch between 1's link() and unlink(). The mail is now expunged.
87b4215acbf020aa5b8dea686b23fc664140cda0Stephan Bosch
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch d) If you try to deal with the duplicates by unlink()ing another
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch one of them, you might end up unlinking both of them.
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch So, what should we do then if we notice a duplicate? First of all,
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch it might not be a duplicate at all, readdir() might have just
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch returned it twice because it was just renamed. What we should do is
87b4215acbf020aa5b8dea686b23fc664140cda0Stephan Bosch create a completely new base name for it and rename() it to that.
87b4215acbf020aa5b8dea686b23fc664140cda0Stephan Bosch If the call fails with ENOENT, it only means that it wasn't a
87b4215acbf020aa5b8dea686b23fc664140cda0Stephan Bosch duplicate after all.
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch*/
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch#include "lib.h"
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch#include "ioloop.h"
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch#include "array.h"
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch#include "buffer.h"
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch#include "hash.h"
191153d1a5b0eb0c129139570e3aa5212f28d2acJosef 'Jeff' Sipek#include "str.h"
62461eb609e1d852e027cf4e07d30d51288678a2Aki Tuomi#include "maildir-storage.h"
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch#include "maildir-uidlist.h"
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch#include "maildir-keywords.h"
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch#include <stdio.h>
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch#include <stddef.h>
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch#include <unistd.h>
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch#include <dirent.h>
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch#include <sys/stat.h>
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch#define MAILDIR_SYNC_SECS 1
191153d1a5b0eb0c129139570e3aa5212f28d2acJosef 'Jeff' Sipek
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch#define MAILDIR_FILENAME_FLAG_FOUND 128
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch/* When rename()ing many files from new/ to cur/, it's possible that next
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch readdir() skips some files. we don't of course wish to lose them, so we
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch go and rescan the new/ directory again from beginning until no files are
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch left. This value is just an optimization to avoid checking the directory
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch twice unneededly. usually only NFS is the problem case. 1 is the safest
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch bet here, but I guess 5 will do just fine too. */
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch#define MAILDIR_RENAME_RESCAN_COUNT 5
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Boschstruct maildir_sync_context {
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch struct maildir_mailbox *mbox;
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch const char *new_dir, *cur_dir;
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch bool partial;
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch struct maildir_uidlist_sync_ctx *uidlist_sync_ctx;
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch struct maildir_index_sync_context *index_sync_ctx;
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch};
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Boschstruct maildir_index_sync_context {
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch struct maildir_mailbox *mbox;
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch struct mail_index_view *view;
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch struct mail_index_sync_ctx *sync_ctx;
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch struct maildir_keywords_sync_ctx *keywords_sync_ctx;
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch struct mail_index_transaction *trans;
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch array_t ARRAY_DEFINE(sync_recs, struct mail_index_sync_rec);
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch uint32_t seq;
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch int dirty_state;
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch};
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Boschstruct maildir_keywords_sync_ctx *
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Boschmaildir_sync_get_keywords_sync_ctx(struct maildir_index_sync_context *ctx)
d6bbf85809664a810726b5c711c7213874d8df57Phil Carmody{
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch return ctx->keywords_sync_ctx;
d6bbf85809664a810726b5c711c7213874d8df57Phil Carmody}
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Boschint maildir_filename_get_flags(struct maildir_keywords_sync_ctx *ctx,
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch const char *fname, enum mail_flags *flags_r,
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch array_t *keywords_r)
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody{
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody ARRAY_SET_TYPE(keywords_r, unsigned int);
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody const char *info;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody array_clear(keywords_r);
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody *flags_r = 0;
bee4935fb14a078052e4524070910fd0b8135ef7Phil Carmody
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody info = strchr(fname, MAILDIR_INFO_SEP);
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody if (info == NULL || info[1] != '2' || info[2] != MAILDIR_FLAGS_SEP)
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody return 0;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody for (info += 3; *info != '\0' && *info != MAILDIR_FLAGS_SEP; info++) {
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody switch (*info) {
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody case 'R': /* replied */
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody *flags_r |= MAIL_ANSWERED;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody break;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody case 'S': /* seen */
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody *flags_r |= MAIL_SEEN;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody break;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody case 'T': /* trashed */
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody *flags_r |= MAIL_DELETED;
bee4935fb14a078052e4524070910fd0b8135ef7Phil Carmody break;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody case 'D': /* draft */
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody *flags_r |= MAIL_DRAFT;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody break;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody case 'F': /* flagged */
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody *flags_r |= MAIL_FLAGGED;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody break;
47ca7a853a116b1be3d73b46301d97e572f2f806Phil Carmody default:
47ca7a853a116b1be3d73b46301d97e572f2f806Phil Carmody if (*info >= MAILDIR_KEYWORD_FIRST &&
47ca7a853a116b1be3d73b46301d97e572f2f806Phil Carmody *info <= MAILDIR_KEYWORD_LAST) {
47ca7a853a116b1be3d73b46301d97e572f2f806Phil Carmody int idx;
bee4935fb14a078052e4524070910fd0b8135ef7Phil Carmody
47ca7a853a116b1be3d73b46301d97e572f2f806Phil Carmody idx = maildir_keywords_char_idx(ctx, *info);
47ca7a853a116b1be3d73b46301d97e572f2f806Phil Carmody if (idx < 0) {
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody /* unknown keyword. */
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody break;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody }
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody array_append(keywords_r,
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody (unsigned int *)&idx, 1);
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody break;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody }
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody /* unknown flag - ignore */
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody break;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody }
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody }
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody return 1;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody}
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmodystatic void
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmodymaildir_filename_append_keywords(struct maildir_keywords_sync_ctx *ctx,
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody array_t *keywords, string_t *str)
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody{
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody ARRAY_SET_TYPE(keywords, unsigned int);
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody const unsigned int *indexes;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody unsigned int i, count;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody char chr;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody indexes = array_get(keywords, &count);
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody for (i = 0; i < count; i++) {
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody chr = maildir_keywords_idx_char(ctx, indexes[i]);
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody if (chr != '\0')
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody str_append_c(str, chr);
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody }
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody}
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmodyconst char *maildir_filename_set_flags(struct maildir_keywords_sync_ctx *ctx,
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody const char *fname, enum mail_flags flags,
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody array_t *keywords)
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody{
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody string_t *flags_str;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody enum mail_flags flags_left;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody const char *info, *oldflags;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody int nextflag;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody /* remove the old :info from file name, and get the old flags */
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody info = strrchr(fname, MAILDIR_INFO_SEP);
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody if (info != NULL && strrchr(fname, '/') > info)
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody info = NULL;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody oldflags = "";
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody if (info != NULL) {
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody fname = t_strdup_until(fname, info);
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody if (info[1] == '2' && info[2] == MAILDIR_FLAGS_SEP)
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody oldflags = info+3;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody }
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody /* insert the new flags between old flags. flags must be sorted by
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody their ASCII code. unknown flags are kept. */
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody flags_str = t_str_new(256);
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody str_append(flags_str, fname);
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody str_append(flags_str, MAILDIR_FLAGS_FULL_SEP);
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody flags_left = flags;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody for (;;) {
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody /* skip all known flags */
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody while (*oldflags == 'D' || *oldflags == 'F' ||
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody *oldflags == 'R' || *oldflags == 'S' ||
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody *oldflags == 'T' ||
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody (*oldflags >= MAILDIR_KEYWORD_FIRST &&
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody *oldflags <= MAILDIR_KEYWORD_LAST))
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody oldflags++;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody nextflag = *oldflags == '\0' || *oldflags == MAILDIR_FLAGS_SEP ?
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody 256 : (unsigned char) *oldflags;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody if ((flags_left & MAIL_DRAFT) && nextflag > 'D') {
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody str_append_c(flags_str, 'D');
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody flags_left &= ~MAIL_DRAFT;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody }
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody if ((flags_left & MAIL_FLAGGED) && nextflag > 'F') {
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody str_append_c(flags_str, 'F');
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody flags_left &= ~MAIL_FLAGGED;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody }
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody if ((flags_left & MAIL_ANSWERED) && nextflag > 'R') {
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody str_append_c(flags_str, 'R');
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody flags_left &= ~MAIL_ANSWERED;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody }
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody if ((flags_left & MAIL_SEEN) && nextflag > 'S') {
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody str_append_c(flags_str, 'S');
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody flags_left &= ~MAIL_SEEN;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody }
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody if ((flags_left & MAIL_DELETED) && nextflag > 'T') {
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody str_append_c(flags_str, 'T');
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody flags_left &= ~MAIL_DELETED;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody }
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody if (keywords != NULL && array_is_created(keywords) &&
d3a430481a1e072fb55fee8803c16d075bf7bd91Aki Tuomi nextflag > MAILDIR_KEYWORD_FIRST) {
d3a430481a1e072fb55fee8803c16d075bf7bd91Aki Tuomi maildir_filename_append_keywords(ctx, keywords,
d3a430481a1e072fb55fee8803c16d075bf7bd91Aki Tuomi flags_str);
d3a430481a1e072fb55fee8803c16d075bf7bd91Aki Tuomi keywords = NULL;
d3a430481a1e072fb55fee8803c16d075bf7bd91Aki Tuomi }
d3a430481a1e072fb55fee8803c16d075bf7bd91Aki Tuomi
d3a430481a1e072fb55fee8803c16d075bf7bd91Aki Tuomi if (*oldflags == '\0' || *oldflags == MAILDIR_FLAGS_SEP)
d3a430481a1e072fb55fee8803c16d075bf7bd91Aki Tuomi break;
d3a430481a1e072fb55fee8803c16d075bf7bd91Aki Tuomi
d3a430481a1e072fb55fee8803c16d075bf7bd91Aki Tuomi str_append_c(flags_str, *oldflags);
d3a430481a1e072fb55fee8803c16d075bf7bd91Aki Tuomi oldflags++;
d3a430481a1e072fb55fee8803c16d075bf7bd91Aki Tuomi }
d3a430481a1e072fb55fee8803c16d075bf7bd91Aki Tuomi
d3a430481a1e072fb55fee8803c16d075bf7bd91Aki Tuomi if (*oldflags == MAILDIR_FLAGS_SEP) {
d3a430481a1e072fb55fee8803c16d075bf7bd91Aki Tuomi /* another flagset, we don't know about these, just keep them */
d3a430481a1e072fb55fee8803c16d075bf7bd91Aki Tuomi while (*oldflags != '\0')
d3a430481a1e072fb55fee8803c16d075bf7bd91Aki Tuomi str_append_c(flags_str, *oldflags++);
d3a430481a1e072fb55fee8803c16d075bf7bd91Aki Tuomi }
d3a430481a1e072fb55fee8803c16d075bf7bd91Aki Tuomi
d3a430481a1e072fb55fee8803c16d075bf7bd91Aki Tuomi return str_c(flags_str);
d3a430481a1e072fb55fee8803c16d075bf7bd91Aki Tuomi}
d3a430481a1e072fb55fee8803c16d075bf7bd91Aki Tuomi
d3a430481a1e072fb55fee8803c16d075bf7bd91Aki Tuomistatic int maildir_expunge(struct maildir_mailbox *mbox, const char *path,
d3a430481a1e072fb55fee8803c16d075bf7bd91Aki Tuomi void *context __attr_unused__)
d3a430481a1e072fb55fee8803c16d075bf7bd91Aki Tuomi{
d3a430481a1e072fb55fee8803c16d075bf7bd91Aki Tuomi if (unlink(path) == 0) {
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody mbox->dirty_cur_time = ioloop_time;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody return 1;
47ca7a853a116b1be3d73b46301d97e572f2f806Phil Carmody }
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody if (errno == ENOENT)
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch return 0;
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody mail_storage_set_critical(STORAGE(mbox->storage),
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody "unlink(%s) failed: %m", path);
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody return -1;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody}
d3a430481a1e072fb55fee8803c16d075bf7bd91Aki Tuomi
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmodystatic int maildir_sync_flags(struct maildir_mailbox *mbox, const char *path,
void *context)
{
struct maildir_index_sync_context *ctx = context;
const struct mail_index_sync_rec *recs;
const char *dir, *fname, *newfname, *newpath;
enum mail_flags flags;
array_t ARRAY_DEFINE(keywords, unsigned int);
unsigned int i, count;
uint8_t flags8;
ctx->dirty_state = 0;
fname = strrchr(path, '/');
i_assert(fname != NULL);
fname++;
dir = t_strdup_until(path, fname);
ARRAY_CREATE(&keywords, pool_datastack_create(), unsigned int, 16);
(void)maildir_filename_get_flags(ctx->keywords_sync_ctx,
fname, &flags, &keywords);
flags8 = flags;
recs = array_get_modifyable(&ctx->sync_recs, &count);
for (i = 0; i < count; i++) {
if (recs[i].uid1 != ctx->seq)
break;
switch (recs[i].type) {
case MAIL_INDEX_SYNC_TYPE_FLAGS:
mail_index_sync_flags_apply(&recs[i], &flags8);
break;
case MAIL_INDEX_SYNC_TYPE_KEYWORD_ADD:
case MAIL_INDEX_SYNC_TYPE_KEYWORD_REMOVE:
case MAIL_INDEX_SYNC_TYPE_KEYWORD_RESET:
mail_index_sync_keywords_apply(&recs[i], &keywords);
break;
case MAIL_INDEX_SYNC_TYPE_APPEND:
case MAIL_INDEX_SYNC_TYPE_EXPUNGE:
i_unreached();
break;
}
}
newfname = maildir_filename_set_flags(ctx->keywords_sync_ctx,
fname, flags8, &keywords);
newpath = t_strconcat(dir, newfname, NULL);
if (rename(path, newpath) == 0) {
if ((flags8 & MAIL_INDEX_MAIL_FLAG_DIRTY) != 0)
ctx->dirty_state = -1;
mbox->dirty_cur_time = ioloop_time;
return 1;
}
if (errno == ENOENT)
return 0;
if (ENOSPACE(errno) || errno == EACCES) {
mail_index_update_flags(ctx->trans, ctx->seq, MODIFY_ADD,
MAIL_INDEX_MAIL_FLAG_DIRTY);
ctx->dirty_state = 1;
return 1;
}
mail_storage_set_critical(STORAGE(mbox->storage),
"rename(%s, %s) failed: %m", path, newpath);
return -1;
}
static int
maildir_sync_record_commit_until(struct maildir_index_sync_context *ctx,
uint32_t last_seq)
{
struct mail_index_sync_rec *recs;
unsigned int i, count;
uint32_t seq, uid;
bool expunged, flag_changed;
recs = array_get_modifyable(&ctx->sync_recs, &count);
for (seq = recs[0].uid1; seq <= last_seq; seq++) {
expunged = flag_changed = FALSE;
for (i = 0; i < count; i++) {
if (recs[i].uid1 > seq)
break;
i_assert(recs[i].uid1 == seq);
switch (recs[i].type) {
case MAIL_INDEX_SYNC_TYPE_EXPUNGE:
expunged = TRUE;
break;
case MAIL_INDEX_SYNC_TYPE_FLAGS:
case MAIL_INDEX_SYNC_TYPE_KEYWORD_ADD:
case MAIL_INDEX_SYNC_TYPE_KEYWORD_REMOVE:
case MAIL_INDEX_SYNC_TYPE_KEYWORD_RESET:
flag_changed = TRUE;
break;
case MAIL_INDEX_SYNC_TYPE_APPEND:
i_unreached();
break;
}
}
if (mail_index_lookup_uid(ctx->view, seq, &uid) < 0)
return -1;
ctx->seq = seq;
if (expunged) {
if (maildir_file_do(ctx->mbox, uid,
maildir_expunge, ctx) < 0)
return -1;
} else if (flag_changed) {
if (maildir_file_do(ctx->mbox, uid,
maildir_sync_flags, ctx) < 0)
return -1;
}
for (i = count; i > 0; i--) {
if (++recs[i-1].uid1 > recs[i-1].uid2) {
array_delete(&ctx->sync_recs, i-1, 1);
recs = array_get_modifyable(&ctx->sync_recs,
&count);
if (count == 0) {
/* all sync_recs committed */
return 0;
}
}
}
}
return 0;
}
static int maildir_sync_record(struct maildir_index_sync_context *ctx,
const struct mail_index_sync_rec *sync_rec)
{
struct mail_index_view *view = ctx->view;
struct mail_index_sync_rec sync_copy;
if (sync_rec == NULL) {
/* deinit */
while (array_count(&ctx->sync_recs) > 0) {
if (maildir_sync_record_commit_until(ctx,
(uint32_t)-1) < 0)
return -1;
}
return 0;
}
if (sync_rec->type == MAIL_INDEX_SYNC_TYPE_APPEND)
return 0; /* ignore */
/* convert to sequences to avoid looping through huge holes in
UID range */
sync_copy = *sync_rec;
if (mail_index_lookup_uid_range(view, sync_rec->uid1,
sync_rec->uid2,
&sync_copy.uid1,
&sync_copy.uid2) < 0)
return -1;
if (sync_copy.uid1 == 0) {
/* UIDs were expunged */
return 0;
}
while (array_count(&ctx->sync_recs) > 0) {
const struct mail_index_sync_rec *rec =
array_idx(&ctx->sync_recs, 0);
i_assert(rec->uid1 <= sync_copy.uid1);
if (rec->uid1 == sync_copy.uid1)
break;
if (maildir_sync_record_commit_until(ctx, sync_copy.uid1-1) < 0)
return -1;
}
array_append(&ctx->sync_recs, &sync_copy, 1);
return 0;
}
static int maildir_sync_index_records(struct maildir_index_sync_context *ctx)
{
struct mail_index_sync_rec sync_rec;
int ret;
ret = mail_index_sync_next(ctx->sync_ctx, &sync_rec);
if (ret <= 0)
return ret;
ARRAY_CREATE(&ctx->sync_recs, pool_datastack_create(),
struct mail_index_sync_rec, 32);
do {
if (maildir_sync_record(ctx, &sync_rec) < 0)
return -1;
ret = mail_index_sync_next(ctx->sync_ctx, &sync_rec);
} while (ret > 0);
if (maildir_sync_record(ctx, NULL) < 0)
return -1;
return ret;
}
static struct maildir_sync_context *
maildir_sync_context_new(struct maildir_mailbox *mbox)
{
struct maildir_sync_context *ctx;
ctx = t_new(struct maildir_sync_context, 1);
ctx->mbox = mbox;
ctx->new_dir = t_strconcat(mbox->path, "/new", NULL);
ctx->cur_dir = t_strconcat(mbox->path, "/cur", NULL);
return ctx;
}
static void maildir_sync_deinit(struct maildir_sync_context *ctx)
{
if (ctx->uidlist_sync_ctx != NULL)
(void)maildir_uidlist_sync_deinit(&ctx->uidlist_sync_ctx);
if (ctx->index_sync_ctx != NULL)
(void)maildir_sync_index_finish(&ctx->index_sync_ctx, TRUE);
}
static int maildir_fix_duplicate(struct maildir_sync_context *ctx,
const char *dir, const char *old_fname)
{
struct maildir_mailbox *mbox = ctx->mbox;
const char *existing_fname, *existing_path;
const char *new_fname, *old_path, *new_path;
struct stat st, st2;
int ret = 0;
existing_fname =
maildir_uidlist_sync_get_full_filename(ctx->uidlist_sync_ctx,
old_fname);
i_assert(existing_fname != NULL);
t_push();
existing_path = t_strconcat(dir, "/", existing_fname, NULL);
old_path = t_strconcat(dir, "/", old_fname, NULL);
if (stat(existing_path, &st) < 0 ||
stat(old_path, &st2) < 0) {
/* most likely the files just don't exist anymore.
don't really care about other errors much. */
t_pop();
return 0;
}
if (st.st_ino == st2.st_ino && CMP_DEV_T(st.st_dev, st2.st_dev)) {
/* files are the same. this means either a race condition
between stat() calls, or someone has started link()ing the
files. either way there's no data loss if we just leave it
there. */
t_pop();
return 0;
}
new_fname = maildir_generate_tmp_filename(&ioloop_timeval);
new_path = t_strconcat(mbox->path, "/new/", new_fname, NULL);
if (rename(old_path, new_path) == 0) {
i_warning("Fixed duplicate in %s: %s -> %s",
mbox->path, old_fname, new_fname);
} else if (errno != ENOENT) {
mail_storage_set_critical(STORAGE(mbox->storage),
"rename(%s, %s) failed: %m", old_path, new_path);
ret = -1;
}
t_pop();
return ret;
}
static int maildir_scan_dir(struct maildir_sync_context *ctx, bool new_dir)
{
struct mail_storage *storage = STORAGE(ctx->mbox->storage);
const char *dir;
DIR *dirp;
string_t *src, *dest;
struct dirent *dp;
enum maildir_uidlist_rec_flag flags;
unsigned int moves = 0;
int ret = 1;
bool move_new;
dir = new_dir ? ctx->new_dir : ctx->cur_dir;
dirp = opendir(dir);
if (dirp == NULL) {
mail_storage_set_critical(storage,
"opendir(%s) failed: %m", dir);
return -1;
}
t_push();
src = t_str_new(1024);
dest = t_str_new(1024);
move_new = new_dir && !mailbox_is_readonly(&ctx->mbox->ibox.box) &&
!ctx->mbox->ibox.keep_recent;
while ((dp = readdir(dirp)) != NULL) {
if (dp->d_name[0] == '.')
continue;
ret = maildir_uidlist_sync_next_pre(ctx->uidlist_sync_ctx,
dp->d_name);
if (ret == 0) {
/* new file and we couldn't lock uidlist, check this
later in next sync. */
if (new_dir)
ctx->mbox->last_new_mtime = 0;
else
ctx->mbox->dirty_cur_time = ioloop_time;
continue;
}
if (ret < 0)
break;
flags = 0;
if (move_new) {
str_truncate(src, 0);
str_truncate(dest, 0);
str_printfa(src, "%s/%s", ctx->new_dir, dp->d_name);
str_printfa(dest, "%s/%s", ctx->cur_dir, dp->d_name);
if (strchr(dp->d_name, MAILDIR_INFO_SEP) == NULL) {
str_append(dest, MAILDIR_FLAGS_FULL_SEP);
}
if (rename(str_c(src), str_c(dest)) == 0) {
/* we moved it - it's \Recent for us */
moves++;
ctx->mbox->dirty_cur_time = ioloop_time;
flags |= MAILDIR_UIDLIST_REC_FLAG_MOVED |
MAILDIR_UIDLIST_REC_FLAG_RECENT;
} else if (ENOTFOUND(errno)) {
/* someone else moved it already */
moves++;
flags |= MAILDIR_UIDLIST_REC_FLAG_MOVED;
} else if (ENOSPACE(errno) || errno == EACCES) {
/* not enough disk space / read-only maildir,
leave here */
flags |= MAILDIR_UIDLIST_REC_FLAG_NEW_DIR |
MAILDIR_UIDLIST_REC_FLAG_RECENT;
move_new = FALSE;
} else {
flags |= MAILDIR_UIDLIST_REC_FLAG_NEW_DIR |
MAILDIR_UIDLIST_REC_FLAG_RECENT;
mail_storage_set_critical(storage,
"rename(%s, %s) failed: %m",
str_c(src), str_c(dest));
}
} else if (new_dir) {
flags |= MAILDIR_UIDLIST_REC_FLAG_NEW_DIR |
MAILDIR_UIDLIST_REC_FLAG_RECENT;
}
ret = maildir_uidlist_sync_next(ctx->uidlist_sync_ctx,
dp->d_name, flags);
if (ret <= 0) {
if (ret < 0)
break;
/* possibly duplicate - try fixing it */
if (maildir_fix_duplicate(ctx, dir, dp->d_name) < 0) {
ret = -1;
break;
}
}
}
if (closedir(dirp) < 0) {
mail_storage_set_critical(storage,
"closedir(%s) failed: %m", dir);
}
t_pop();
return ret < 0 ? -1 : (moves <= MAILDIR_RENAME_RESCAN_COUNT ? 0 : 1);
}
static void
maildir_sync_update_from_header(struct maildir_mailbox *mbox)
{
uint64_t value;
/* FIXME: ugly, replace with extension header */
value = mail_index_get_header(mbox->ibox.view)->sync_size;
mbox->last_new_mtime = value & 0xffffffff;
mbox->last_new_sync_time = value >> 32;
mbox->last_cur_mtime =
mail_index_get_header(mbox->ibox.view)->sync_stamp;
}
static int
maildir_sync_quick_check(struct maildir_mailbox *mbox,
const char *new_dir, const char *cur_dir,
bool *new_changed_r, bool *cur_changed_r)
{
struct index_mailbox *ibox = &mbox->ibox;
struct stat st;
time_t new_mtime, cur_mtime;
*new_changed_r = *cur_changed_r = FALSE;
if (stat(new_dir, &st) < 0) {
mail_storage_set_critical(STORAGE(mbox->storage),
"stat(%s) failed: %m", new_dir);
return -1;
}
new_mtime = st.st_mtime;
if (stat(cur_dir, &st) < 0) {
mail_storage_set_critical(STORAGE(mbox->storage),
"stat(%s) failed: %m", cur_dir);
return -1;
}
cur_mtime = st.st_mtime;
/* cur stamp is kept in index, we don't have to sync if
someone else has done it and updated the index.
FIXME: For now we're using sync_size field as the new/ dir's stamp.
Pretty ugly.. */
maildir_sync_update_from_header(mbox);
if ((mbox->dirty_cur_time == 0 && cur_mtime != mbox->last_cur_mtime) ||
(new_mtime != mbox->last_new_mtime)) {
/* check if the index has been updated.. */
if (mail_index_refresh(ibox->index) < 0) {
mail_storage_set_index_error(ibox);
return -1;
}
maildir_sync_update_from_header(mbox);
}
/* If we're removing recent flags, always sync new/ directory if
it has mails. */
if (new_mtime != mbox->last_new_mtime ||
new_mtime >= mbox->last_new_sync_time - MAILDIR_SYNC_SECS ||
(!ibox->keep_recent &&
mail_index_get_header(ibox->view)->recent_messages_count > 0)) {
*new_changed_r = TRUE;
mbox->last_new_mtime = new_mtime;
mbox->last_new_sync_time = ioloop_time;
}
if (cur_mtime != mbox->last_cur_mtime ||
(mbox->dirty_cur_time != 0 &&
ioloop_time - mbox->dirty_cur_time > MAILDIR_SYNC_SECS)) {
/* cur/ changed, or delayed cur/ check */
*cur_changed_r = TRUE;
mbox->last_cur_mtime = cur_mtime;
mbox->dirty_cur_time =
cur_mtime >= ioloop_time - MAILDIR_SYNC_SECS ?
cur_mtime : 0;
}
return 0;
}
int maildir_sync_index_begin(struct maildir_mailbox *mbox,
struct maildir_index_sync_context **ctx_r)
{
struct maildir_index_sync_context *ctx;
struct mail_index_sync_ctx *sync_ctx;
struct mail_index_view *view;
if (mail_index_sync_begin(mbox->ibox.index, &sync_ctx, &view,
(uint32_t)-1, (uoff_t)-1,
FALSE, FALSE) <= 0) {
mail_storage_set_index_error(&mbox->ibox);
return -1;
}
ctx = i_new(struct maildir_index_sync_context, 1);
ctx->mbox = mbox;
ctx->sync_ctx = sync_ctx;
ctx->view = view;
ctx->keywords_sync_ctx =
maildir_keywords_sync_init(mbox->keywords, mbox->ibox.index);
*ctx_r = ctx;
return 0;
}
int maildir_sync_index_finish(struct maildir_index_sync_context **_sync_ctx,
bool failed)
{
struct maildir_index_sync_context *sync_ctx = *_sync_ctx;
struct maildir_mailbox *mbox = sync_ctx->mbox;
uint32_t seq;
uoff_t offset;
int ret = failed ? -1 : 0;
*_sync_ctx = NULL;
if (sync_ctx->trans != NULL) {
if (ret < 0)
mail_index_transaction_rollback(&sync_ctx->trans);
else {
if (mail_index_transaction_commit(&sync_ctx->trans,
&seq, &offset) < 0)
ret = -1;
else if (seq != 0) {
mbox->ibox.commit_log_file_seq = seq;
mbox->ibox.commit_log_file_offset = offset;
}
}
}
if (ret < 0)
mail_index_sync_rollback(&sync_ctx->sync_ctx);
else {
if (mail_index_sync_commit(&sync_ctx->sync_ctx) < 0)
ret = -1;
else {
mbox->ibox.commit_log_file_seq = 0;
mbox->ibox.commit_log_file_offset = 0;
}
}
if (ret < 0)
mail_storage_set_index_error(&mbox->ibox);
maildir_keywords_sync_deinit(sync_ctx->keywords_sync_ctx);
sync_ctx->keywords_sync_ctx = NULL;
i_free(sync_ctx);
return ret;
}
int maildir_sync_index(struct maildir_index_sync_context *sync_ctx,
bool partial)
{
struct maildir_mailbox *mbox = sync_ctx->mbox;
struct mail_index_view *view = sync_ctx->view;
struct maildir_uidlist_iter_ctx *iter;
struct mail_index_transaction *trans;
const struct mail_index_header *hdr;
const struct mail_index_record *rec;
uint32_t seq, uid;
enum maildir_uidlist_rec_flag uflags;
const char *filename;
enum mail_flags flags;
array_t ARRAY_DEFINE(keywords, unsigned int);
array_t ARRAY_DEFINE(idx_keywords, unsigned int);
uint32_t uid_validity, next_uid;
uint64_t value;
time_t old_new_sync_time;
int ret = 0;
bool full_rescan = FALSE;
i_assert(maildir_uidlist_is_locked(sync_ctx->mbox->uidlist));
hdr = mail_index_get_header(view);
uid_validity = maildir_uidlist_get_uid_validity(mbox->uidlist);
if (uid_validity != hdr->uid_validity &&
uid_validity != 0 && hdr->uid_validity != 0) {
/* uidvalidity changed and mailbox isn't being initialized,
reset mailbox so we can add all messages as new */
mail_storage_set_critical(STORAGE(mbox->storage),
"Maildir %s sync: UIDVALIDITY changed (%u -> %u)",
mbox->path, hdr->uid_validity, uid_validity);
mail_index_mark_corrupted(mbox->ibox.index);
return -1;
}
sync_ctx->trans = trans =
mail_index_transaction_begin(sync_ctx->view, FALSE, TRUE);
seq = 0;
ARRAY_CREATE(&keywords, pool_datastack_create(),
unsigned int, MAILDIR_MAX_KEYWORDS);
ARRAY_CREATE(&idx_keywords, pool_datastack_create(),
unsigned int, MAILDIR_MAX_KEYWORDS);
iter = maildir_uidlist_iter_init(mbox->uidlist);
while (maildir_uidlist_iter_next(iter, &uid, &uflags, &filename)) {
maildir_filename_get_flags(sync_ctx->keywords_sync_ctx,
filename, &flags, &keywords);
/* the private flags are kept only in indexes. don't use them
at all even for newly seen mails */
flags &= ~mbox->private_flags_mask;
if ((uflags & MAILDIR_UIDLIST_REC_FLAG_RECENT) != 0 &&
(uflags & MAILDIR_UIDLIST_REC_FLAG_NEW_DIR) != 0 &&
(uflags & MAILDIR_UIDLIST_REC_FLAG_MOVED) == 0) {
/* mail is recent for next session as well */
flags |= MAIL_RECENT;
}
__again:
seq++;
if (seq > hdr->messages_count) {
if (uid < hdr->next_uid) {
/* most likely a race condition: we read the
maildir, then someone else expunged messages
and committed changes to index. so, this
message shouldn't actually exist. mark it
racy and check in next sync.
the difference between this and the later
check is that this one happens when messages
are expunged from the end */
if ((uflags &
MAILDIR_UIDLIST_REC_FLAG_NONSYNCED) != 0) {
/* partial syncing */
continue;
}
if ((uflags &
MAILDIR_UIDLIST_REC_FLAG_RACING) != 0) {
mail_storage_set_critical(
STORAGE(mbox->storage),
"Maildir %s sync: "
"UID < next_uid "
"(%u < %u, file = %s)",
mbox->path, uid, hdr->next_uid,
filename);
mail_index_mark_corrupted(
mbox->ibox.index);
ret = -1;
break;
}
mbox->dirty_cur_time = ioloop_time;
maildir_uidlist_add_flags(mbox->uidlist,
filename,
MAILDIR_UIDLIST_REC_FLAG_RACING);
seq--;
continue;
}
mail_index_append(trans, uid, &seq);
mail_index_update_flags(trans, seq, MODIFY_REPLACE,
flags);
if (array_count(&keywords) > 0) {
struct mail_keywords *kw;
kw = mail_index_keywords_create_from_indexes(
trans, &keywords);
mail_index_update_keywords(trans, seq,
MODIFY_REPLACE, kw);
mail_index_keywords_free(&kw);
}
continue;
}
if (mail_index_lookup(view, seq, &rec) < 0) {
ret = -1;
break;
}
if (rec->uid < uid) {
/* expunged */
mail_index_expunge(trans, seq);
goto __again;
}
if (rec->uid > uid) {
/* most likely a race condition: we read the
maildir, then someone else expunged messages and
committed changes to index. so, this message
shouldn't actually exist. mark it racy and check
in next sync. */
if ((uflags &
MAILDIR_UIDLIST_REC_FLAG_NONSYNCED) != 0) {
/* partial syncing */
seq--;
continue;
}
if ((uflags & MAILDIR_UIDLIST_REC_FLAG_RACING) != 0) {
mail_storage_set_critical(
STORAGE(mbox->storage),
"Maildir %s sync: "
"UID inserted in the middle of mailbox "
"(%u > %u, file = %s)",
mbox->path, rec->uid, uid, filename);
mail_index_mark_corrupted(mbox->ibox.index);
ret = -1;
break;
}
mbox->dirty_cur_time = ioloop_time;
maildir_uidlist_add_flags(mbox->uidlist, filename,
MAILDIR_UIDLIST_REC_FLAG_RACING);
seq--;
continue;
}
/* the private flags are stored only in indexes, keep them */
flags |= rec->flags & mbox->private_flags_mask;
if ((rec->flags & MAIL_RECENT) != 0) {
index_mailbox_set_recent(&mbox->ibox, seq);
if (mbox->ibox.keep_recent) {
flags |= MAIL_RECENT;
} else {
mail_index_update_flags(trans, seq,
MODIFY_REMOVE,
MAIL_RECENT);
}
}
if ((uflags & MAILDIR_UIDLIST_REC_FLAG_NONSYNCED) != 0) {
/* partial syncing */
if ((flags & MAIL_RECENT) != 0) {
/* we last saw this mail in new/, but it's
not there anymore. possibly expunged,
make sure. */
full_rescan = TRUE;
}
continue;
}
if ((rec->flags & MAIL_INDEX_MAIL_FLAG_DIRTY) != 0) {
/* we haven't been able to update maildir with this
record's flag changes. don't sync them. */
continue;
}
if (((uint8_t)flags & ~MAIL_RECENT) !=
(rec->flags & (MAIL_FLAGS_MASK^MAIL_RECENT))) {
/* FIXME: this is wrong if there's pending changes in
transaction log already. it gets fixed in next sync
however.. */
mail_index_update_flags(trans, seq, MODIFY_REPLACE,
flags);
} else if ((flags & MAIL_RECENT) == 0 &&
(rec->flags & MAIL_RECENT) != 0) {
/* just remove recent flag */
mail_index_update_flags(trans, seq, MODIFY_REMOVE,
MAIL_RECENT);
}
/* update keywords if they have changed */
if (mail_index_lookup_keywords(view, seq, &idx_keywords) < 0) {
ret = -1;
break;
}
if (!index_keyword_array_cmp(&keywords, &idx_keywords)) {
struct mail_keywords *kw;
kw = mail_index_keywords_create_from_indexes(
trans, &keywords);
mail_index_update_keywords(trans, seq,
MODIFY_REPLACE, kw);
mail_index_keywords_free(&kw);
}
}
maildir_uidlist_iter_deinit(iter);
array_free(&keywords);
if (!partial) {
/* expunge the rest */
for (seq++; seq <= hdr->messages_count; seq++)
mail_index_expunge(trans, seq);
/* next_uid must be updated only in non-partial syncs since
partial syncs don't add the new mails to index. also we'll
have to do it here before syncing index records, since after
that the uidlist's next_uid value may have changed. */
next_uid = maildir_uidlist_get_next_uid(mbox->uidlist);
if (next_uid != 0 && hdr->next_uid != next_uid) {
mail_index_update_header(trans,
offsetof(struct mail_index_header, next_uid),
&next_uid, sizeof(next_uid), FALSE);
}
}
if (!mbox->syncing_commit) {
/* now, sync the index. NOTE: may recurse back to here with
partial syncs */
mbox->syncing_commit = TRUE;
if (maildir_sync_index_records(sync_ctx) < 0)
ret = -1;
mbox->syncing_commit = FALSE;
}
if (mbox->dirty_cur_time == 0 &&
mbox->last_cur_mtime != (time_t)hdr->sync_stamp) {
uint32_t sync_stamp = mbox->last_cur_mtime;
mail_index_update_header(trans,
offsetof(struct mail_index_header, sync_stamp),
&sync_stamp, sizeof(sync_stamp), TRUE);
}
/* FIXME: use a header extension instead of sync_size.. */
value = mbox->last_new_mtime;
old_new_sync_time = hdr->sync_size >> 32;
if (mbox->last_new_mtime >= old_new_sync_time - MAILDIR_SYNC_SECS) {
value |= (uint64_t)mbox->last_new_sync_time << 32;
} else {
value |= (uint64_t)old_new_sync_time << 32;
}
if (value != hdr->sync_size) {
uint64_t sync_stamp = mbox->last_new_mtime |
((uint64_t)mbox->last_new_sync_time << 32);
mail_index_update_header(trans,
offsetof(struct mail_index_header, sync_size),
&sync_stamp, sizeof(sync_stamp), TRUE);
}
if (hdr->uid_validity == 0) {
/* get the initial uidvalidity */
if (maildir_uidlist_update(mbox->uidlist) < 0)
ret = -1;
uid_validity = maildir_uidlist_get_uid_validity(mbox->uidlist);
if (uid_validity == 0) {
uid_validity = ioloop_time;
maildir_uidlist_set_uid_validity(mbox->uidlist,
uid_validity);
}
} else if (uid_validity == 0) {
maildir_uidlist_set_uid_validity(mbox->uidlist,
hdr->uid_validity);
}
if (uid_validity != hdr->uid_validity && uid_validity != 0) {
mail_index_update_header(trans,
offsetof(struct mail_index_header, uid_validity),
&uid_validity, sizeof(uid_validity), TRUE);
}
return ret < 0 ? -1 : (full_rescan ? 0 : 1);
}
static int maildir_sync_context(struct maildir_sync_context *ctx, bool forced,
bool sync_last_commit)
{
bool new_changed, cur_changed, full_rescan = FALSE;
int ret;
if (sync_last_commit) {
new_changed = cur_changed = FALSE;
} else if (!forced) {
if (maildir_sync_quick_check(ctx->mbox,
ctx->new_dir, ctx->cur_dir,
&new_changed, &cur_changed) < 0)
return -1;
if (!new_changed && !cur_changed)
return 1;
} else {
new_changed = cur_changed = TRUE;
}
/*
Locking, locking, locking.. Wasn't maildir supposed to be lockless?
We can get here either as beginning a real maildir sync, or when
committing changes to maildir but a file was lost (maybe renamed).
So, we're going to need two locks. One for index and one for
uidlist. To avoid deadlocking do the uidlist lock always first.
uidlist is needed only for figuring out UIDs for newly seen files,
so theoretically we wouldn't need to lock it unless there are new
files. It has a few problems though, assuming the index lock didn't
already protect it (eg. in-memory indexes):
1. Just because you see a new file which doesn't exist in uidlist
file, doesn't mean that the file really exists anymore, or that
your readdir() lists all new files. Meaning that this is possible:
A: opendir(), readdir() -> new file ...
-- new files are written to the maildir --
B: opendir(), readdir() -> new file, lock uidlist,
readdir() -> another new file, rewrite uidlist, unlock
A: ... lock uidlist, readdir() -> nothing left, rewrite uidlist,
unlock
The second time running A didn't see the two new files. To handle
this correctly, it must not remove the new unseen files from
uidlist. This is possible to do, but adds extra complexity.
2. If another process is rename()ing files while we are
readdir()ing, it's possible that readdir() never lists some files,
causing Dovecot to assume they were expunged. In next sync they
would show up again, but client could have already been notified of
that and they would show up under new UIDs, so the damage is
already done.
Both of the problems can be avoided if we simply lock the uidlist
before syncing and keep it until sync is finished. Typically this
would happen in any case, as there is the index lock..
The second case is still a problem with external changes though,
because maildir doesn't require any kind of locking. Luckily this
problem rarely happens except under high amount of modifications.
*/
ctx->partial = !cur_changed;
ret = maildir_uidlist_sync_init(ctx->mbox->uidlist, ctx->partial,
&ctx->uidlist_sync_ctx);
if (ret <= 0) {
/* failure / timeout. if forced is TRUE, we could still go
forward and check only for renamed files, but is it worth
the trouble? .. */
return ret;
}
if (!ctx->mbox->syncing_commit) {
if (maildir_sync_index_begin(ctx->mbox,
&ctx->index_sync_ctx) < 0)
return -1;
}
if (new_changed || cur_changed) {
/* if we're going to check cur/ dir our current logic requires
that new/ dir is checked as well. it's a good idea anyway. */
while ((ret = maildir_scan_dir(ctx, TRUE)) > 0) {
/* rename()d at least some files, which might have
caused some other files to be missed. check again
(see MAILDIR_RENAME_RESCAN_COUNT). */
}
if (ret < 0)
return -1;
if (cur_changed) {
if (maildir_scan_dir(ctx, FALSE) < 0)
return -1;
}
/* finish uidlist syncing, but keep it still locked */
maildir_uidlist_sync_finish(ctx->uidlist_sync_ctx);
}
if (!ctx->mbox->syncing_commit) {
/* NOTE: index syncing here might cause a re-sync due to
files getting lost, so this function might be called
re-entrantly. FIXME: and that breaks in
maildir_uidlist_sync_deinit() */
ret = maildir_sync_index(ctx->index_sync_ctx, ctx->partial);
if (maildir_sync_index_finish(&ctx->index_sync_ctx,
ret < 0) < 0)
return -1;
if (ret < 0)
return -1;
if (ret == 0)
full_rescan = TRUE;
i_assert(maildir_uidlist_is_locked(ctx->mbox->uidlist));
}
ret = maildir_uidlist_sync_deinit(&ctx->uidlist_sync_ctx);
return ret < 0 ? -1 : (full_rescan ? 0 : 1);
}
int maildir_storage_sync_force(struct maildir_mailbox *mbox)
{
struct maildir_sync_context *ctx;
int ret;
ctx = maildir_sync_context_new(mbox);
ret = maildir_sync_context(ctx, TRUE, FALSE);
maildir_sync_deinit(ctx);
return ret < 0 ? -1 : 0;
}
int maildir_sync_last_commit(struct maildir_mailbox *mbox)
{
struct maildir_sync_context *ctx;
int ret;
if (mbox->ibox.commit_log_file_seq == 0)
return 0;
ctx = maildir_sync_context_new(mbox);
ret = maildir_sync_context(ctx, FALSE, TRUE);
maildir_sync_deinit(ctx);
return ret < 0 ? -1 : 0;
}
struct mailbox_sync_context *
maildir_storage_sync_init(struct mailbox *box, enum mailbox_sync_flags flags)
{
struct maildir_mailbox *mbox = (struct maildir_mailbox *)box;
struct maildir_sync_context *ctx;
int ret = 0;
if ((flags & MAILBOX_SYNC_FLAG_FAST) == 0 ||
mbox->ibox.sync_last_check + MAILBOX_FULL_SYNC_INTERVAL <=
ioloop_time) {
mbox->ibox.sync_last_check = ioloop_time;
ctx = maildir_sync_context_new(mbox);
ret = maildir_sync_context(ctx, FALSE, FALSE);
maildir_sync_deinit(ctx);
i_assert(!maildir_uidlist_is_locked(mbox->uidlist) ||
mbox->ibox.keep_locked);
if (ret == 0) {
/* lost some files from new/, see if thery're in cur/ */
ret = maildir_storage_sync_force(mbox);
}
}
return index_mailbox_sync_init(box, flags, ret < 0);
}
int maildir_sync_is_synced(struct maildir_mailbox *mbox)
{
const char *new_dir, *cur_dir;
bool new_changed, cur_changed;
int ret;
t_push();
new_dir = t_strconcat(mbox->path, "/new", NULL);
cur_dir = t_strconcat(mbox->path, "/cur", NULL);
ret = maildir_sync_quick_check(mbox, new_dir, cur_dir,
&new_changed, &cur_changed);
t_pop();
return ret < 0 ? -1 : (!new_changed && !cur_changed);
}