annotate src/lib-storage/index/maildir/maildir-sync.c @ 3472:db29cc6754d5 HEAD

Store new/ directory's timestamp in sync_size header in index (kludgy..). Fixed saving message into empty and non-synced mailboxes.
author Timo Sirainen <tss@iki.fi>
date Tue, 12 Jul 2005 15:45:06 +0300
parents 346a494c2feb
children e2fe8222449d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1955
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
1 /* Copyright (C) 2004 Timo Sirainen */
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
2
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
3 /*
1955
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
4 Here's a description of how we handle Maildir synchronization and
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
5 it's problems:
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
6
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
7 We want to be as efficient as we can. The most efficient way to
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
8 check if changes have occured is to stat() the new/ and cur/
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
9 directories and uidlist file - if their mtimes haven't changed,
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
10 there's no changes and we don't need to do anything.
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
11
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
12 Problem 1: Multiple changes can happen within a single second -
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
13 nothing guarantees that once we synced it, someone else didn't just
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
14 then make a modification. Such modifications wouldn't get noticed
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
15 until a new modification occured later.
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
16
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
17 Problem 2: Syncing cur/ directory is much more costly than syncing
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
18 new/. Moving mails from new/ to cur/ will always change mtime of
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
19 cur/ causing us to sync it as well.
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
20
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
21 Problem 3: We may not be able to move mail from new/ to cur/
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
22 because we're out of quota, or simply because we're accessing a
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
23 read-only mailbox.
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
24
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
25
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
26 MAILDIR_SYNC_SECS
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
27 -----------------
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
28
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
29 Several checks below use MAILDIR_SYNC_SECS, which should be maximum
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
30 clock drift between all computers accessing the maildir (eg. via
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
31 NFS), rounded up to next second. Our default is 1 second, since
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
32 everyone should be using NTP.
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
33
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
34 Note that setting it to 0 works only if there's only one computer
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
35 accessing the maildir. It's practically impossible to make two
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
36 clocks _exactly_ synchronized.
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
37
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
38 It might be possible to only use file server's clock by looking at
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
39 the atime field, but I don't know how well that would actually work.
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
40
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
41 cur directory
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
42 -------------
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
43
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
44 We have dirty_cur_time variable which is set to cur/ directory's
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
45 mtime when it's >= time() - MAILDIR_SYNC_SECS and we _think_ we have
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
46 synchronized the directory.
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
47
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
48 When dirty_cur_time is non-zero, we don't synchronize the cur/
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
49 directory until
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
50
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
51 a) cur/'s mtime changes
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
52 b) opening a mail fails with ENOENT
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
53 c) time() > dirty_cur_time + MAILDIR_SYNC_SECS
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
54
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
55 This allows us to modify the maildir multiple times without having
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
56 to sync it at every change. The sync will eventually be done to
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
57 make sure we didn't miss any external changes.
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
58
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
59 The dirty_cur_time is set when:
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
60
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
61 - we change message flags
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
62 - we expunge messages
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
63 - we move mail from new/ to cur/
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
64 - we sync cur/ directory and it's mtime is >= time() - MAILDIR_SYNC_SECS
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
65
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
66 It's unset when we do the final syncing, ie. when mtime is
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
67 older than time() - MAILDIR_SYNC_SECS.
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
68
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
69 new directory
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
70 -------------
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
71
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
72 If new/'s mtime is >= time() - MAILDIR_SYNC_SECS, always synchronize
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
73 it. dirty_cur_time-like feature might save us a few syncs, but
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
74 that might break a client which saves a mail in one connection and
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
75 tries to fetch it in another one. new/ directory is almost always
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
76 empty, so syncing it should be very fast anyway. Actually this can
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
77 still happen if we sync only new/ dir while another client is also
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
78 moving mails from it to cur/ - it takes us a while to see them.
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
79 That's pretty unlikely to happen however, and only way to fix it
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
80 would be to always synchronize cur/ after new/.
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
81
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
82 Normally we move all mails from new/ to cur/ whenever we sync it. If
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
83 it's not possible for some reason, we mark the mail with "probably
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
84 exists in new/ directory" flag.
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
85
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
86 If rename() still fails because of ENOSPC or EDQUOT, we still save
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
87 the flag changes in index with dirty-flag on. When moving the mail
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
88 to cur/ directory, or when we notice it's already moved there, we
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
89 apply the flag changes to the filename, rename it and remove the
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
90 dirty flag. If there's dirty flags, this should be tried every time
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
91 after expunge or when closing the mailbox.
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
92
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
93 uidlist
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
94 -------
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
95
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
96 This file contains UID <-> filename mappings. It's updated only when
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
97 new mail arrives, so it may contain filenames that have already been
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
98 deleted. Updating is done by getting uidlist.lock file, writing the
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
99 whole uidlist into it and rename()ing it over the old uidlist. This
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
100 means there's no need to lock the file for reading.
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
101
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
102 Whenever uidlist is rewritten, it's mtime must be larger than the old
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
103 one's. Use utime() before rename() if needed. Note that inode checking
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
104 wouldn't have been sufficient as inode numbers can be reused.
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
105
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
106 This file is usually read the first time you need to know filename for
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
107 given UID. After that it's not re-read unless new mails come that we
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
108 don't know about.
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
109
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
110 broken clients
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
111 --------------
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
112
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
113 Originally the middle identifier in Maildir filename was specified
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
114 only as <process id>_<delivery counter>. That however created a
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
115 problem with randomized PIDs which made it possible that the same
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
116 PID was reused within one second.
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
117
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
118 So if within one second a mail was delivered, MUA moved it to cur/
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
119 and another mail was delivered by a new process using same PID as
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
120 the first one, we likely ended up overwriting the first mail when
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
121 the second mail was moved over it.
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
122
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
123 Nowadays everyone should be giving a bit more specific identifier,
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
124 for example include microseconds in it which Dovecot does.
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
125
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
126 There's a simple way to prevent this from happening in some cases:
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
127 Don't move the mail from new/ to cur/ if it's mtime is >= time() -
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
128 MAILDIR_SYNC_SECS. The second delivery's link() call then fails
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
129 because the file is already in new/, and it will then use a
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
130 different filename. There's a few problems with this however:
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
131
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
132 - it requires extra stat() call which is unneeded extra I/O
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
133 - another MUA might still move the mail to cur/
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
134 - if first file's flags are modified by either Dovecot or another
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
135 MUA, it's moved to cur/ (you _could_ just do the dirty-flagging
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
136 but that'd be ugly)
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
137
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
138 Because this is useful only for very few people and it requires
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
139 extra I/O, I decided not to implement this. It should be however
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
140 quite easy to do since we need to be able to deal with files in new/
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
141 in any case.
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
142
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
143 It's also possible to never accidentally overwrite a mail by using
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
144 link() + unlink() rather than rename(). This however isn't very
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
145 good idea as it introduces potential race conditions when multiple
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
146 clients are accessing the mailbox:
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
147
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
148 Trying to move the same mail from new/ to cur/ at the same time:
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
149
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
150 a) Client 1 uses slightly different filename than client 2,
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
151 for example one sets read-flag on but the other doesn't.
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
152 You have the same mail duplicated now.
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
153
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
154 b) Client 3 sees the mail between Client 1's and 2's link() calls
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
155 and changes it's flag. You have the same mail duplicated now.
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
156
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
157 And it gets worse when they're unlink()ing in cur/ directory:
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
158
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
159 c) Client 1 changes mails's flag and client 2 changes it back
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
160 between 1's link() and unlink(). The mail is now expunged.
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
161
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
162 d) If you try to deal with the duplicates by unlink()ing another
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
163 one of them, you might end up unlinking both of them.
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
164
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
165 So, what should we do then if we notice a duplicate? First of all,
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
166 it might not be a duplicate at all, readdir() might have just
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
167 returned it twice because it was just renamed. What we should do is
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
168 create a completely new base name for it and rename() it to that.
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
169 If the call fails with ENOENT, it only means that it wasn't a
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
170 duplicate after all.
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
171 */
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
172
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
173 #include "lib.h"
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
174 #include "ioloop.h"
3470
346a494c2feb Moved array declaration to array-decl.h and include it in lib.h. So array.h
Timo Sirainen <tss@iki.fi>
parents: 3453
diff changeset
175 #include "array.h"
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
176 #include "buffer.h"
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
177 #include "hash.h"
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
178 #include "str.h"
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
179 #include "maildir-storage.h"
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
180 #include "maildir-uidlist.h"
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
181 #include "maildir-keywords.h"
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
182
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
183 #include <stdio.h>
2050
ee1095ccfd23 Index header changes now go through transaction log. Removed the kludgy
Timo Sirainen <tss@iki.fi>
parents: 2039
diff changeset
184 #include <stddef.h>
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
185 #include <unistd.h>
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
186 #include <dirent.h>
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
187 #include <sys/stat.h>
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
188
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
189 #define MAILDIR_SYNC_SECS 1
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
190
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
191 #define MAILDIR_FILENAME_FLAG_FOUND 128
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
192
3435
6f7ce690358e If we have rename()d more than 5 files from new/ to cur/, rescan the
Timo Sirainen <tss@iki.fi>
parents: 3417
diff changeset
193 /* When rename()ing many files from new/ to cur/, it's possible that next
6f7ce690358e If we have rename()d more than 5 files from new/ to cur/, rescan the
Timo Sirainen <tss@iki.fi>
parents: 3417
diff changeset
194 readdir() skips some files. we don't of course wish to lose them, so we
6f7ce690358e If we have rename()d more than 5 files from new/ to cur/, rescan the
Timo Sirainen <tss@iki.fi>
parents: 3417
diff changeset
195 go and rescan the new/ directory again from beginning until no files are
6f7ce690358e If we have rename()d more than 5 files from new/ to cur/, rescan the
Timo Sirainen <tss@iki.fi>
parents: 3417
diff changeset
196 left. This value is just an optimization to avoid checking the directory
6f7ce690358e If we have rename()d more than 5 files from new/ to cur/, rescan the
Timo Sirainen <tss@iki.fi>
parents: 3417
diff changeset
197 twice unneededly. usually only NFS is the problem case. 1 is the safest
6f7ce690358e If we have rename()d more than 5 files from new/ to cur/, rescan the
Timo Sirainen <tss@iki.fi>
parents: 3417
diff changeset
198 bet here, but I guess 5 will do just fine too. */
6f7ce690358e If we have rename()d more than 5 files from new/ to cur/, rescan the
Timo Sirainen <tss@iki.fi>
parents: 3417
diff changeset
199 #define MAILDIR_RENAME_RESCAN_COUNT 5
6f7ce690358e If we have rename()d more than 5 files from new/ to cur/, rescan the
Timo Sirainen <tss@iki.fi>
parents: 3417
diff changeset
200
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
201 struct maildir_sync_context {
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
202 struct maildir_mailbox *mbox;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
203 const char *new_dir, *cur_dir;
1954
2f6e137cdc44 Syncing optimizations.
Timo Sirainen <tss@iki.fi>
parents: 1947
diff changeset
204 int partial;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
205
2818
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
206 struct maildir_uidlist_sync_ctx *uidlist_sync_ctx;
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
207 struct maildir_index_sync_context *index_sync_ctx;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
208 };
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
209
1956
d6941cd8afdc Added support for setting dirty flags for messages (TODO: undirty..)
Timo Sirainen <tss@iki.fi>
parents: 1955
diff changeset
210 struct maildir_index_sync_context {
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
211 struct maildir_mailbox *mbox;
2037
8763032d31bd Set dirty flags through transaction log, not directly. Some other flag
Timo Sirainen <tss@iki.fi>
parents: 2034
diff changeset
212 struct mail_index_view *view;
1956
d6941cd8afdc Added support for setting dirty flags for messages (TODO: undirty..)
Timo Sirainen <tss@iki.fi>
parents: 1955
diff changeset
213 struct mail_index_sync_ctx *sync_ctx;
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
214 struct maildir_keywords_sync_ctx *keywords_sync_ctx;
2037
8763032d31bd Set dirty flags through transaction log, not directly. Some other flag
Timo Sirainen <tss@iki.fi>
parents: 2034
diff changeset
215 struct mail_index_transaction *trans;
1956
d6941cd8afdc Added support for setting dirty flags for messages (TODO: undirty..)
Timo Sirainen <tss@iki.fi>
parents: 1955
diff changeset
216
3446
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
217 array_t ARRAY_DEFINE(sync_recs, struct mail_index_sync_rec);
1956
d6941cd8afdc Added support for setting dirty flags for messages (TODO: undirty..)
Timo Sirainen <tss@iki.fi>
parents: 1955
diff changeset
218 uint32_t seq;
2272
ced88553af0b mail_index_sync_sort_flags() now merges flag changes so mail storage
Timo Sirainen <tss@iki.fi>
parents: 2258
diff changeset
219 int dirty_state;
1956
d6941cd8afdc Added support for setting dirty flags for messages (TODO: undirty..)
Timo Sirainen <tss@iki.fi>
parents: 1955
diff changeset
220 };
d6941cd8afdc Added support for setting dirty flags for messages (TODO: undirty..)
Timo Sirainen <tss@iki.fi>
parents: 1955
diff changeset
221
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
222 int maildir_filename_get_flags(struct maildir_index_sync_context *ctx,
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
223 const char *fname,
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
224 enum mail_flags *flags_r,
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
225 array_t *keywords_r)
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
226 {
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
227 ARRAY_SET_TYPE(keywords_r, unsigned int);
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
228 const char *info;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
229
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
230 array_clear(keywords_r);
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
231 *flags_r = 0;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
232
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
233 info = strchr(fname, MAILDIR_INFO_SEP);
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
234 if (info == NULL || info[1] != '2' || info[2] != MAILDIR_FLAGS_SEP)
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
235 return 0;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
236
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
237 for (info += 3; *info != '\0' && *info != MAILDIR_FLAGS_SEP; info++) {
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
238 switch (*info) {
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
239 case 'R': /* replied */
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
240 *flags_r |= MAIL_ANSWERED;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
241 break;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
242 case 'S': /* seen */
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
243 *flags_r |= MAIL_SEEN;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
244 break;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
245 case 'T': /* trashed */
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
246 *flags_r |= MAIL_DELETED;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
247 break;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
248 case 'D': /* draft */
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
249 *flags_r |= MAIL_DRAFT;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
250 break;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
251 case 'F': /* flagged */
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
252 *flags_r |= MAIL_FLAGGED;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
253 break;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
254 default:
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
255 if (*info >= MAILDIR_KEYWORD_FIRST &&
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
256 *info <= MAILDIR_KEYWORD_LAST) {
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
257 int idx;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
258
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
259 idx = maildir_keywords_char_idx(
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
260 ctx->keywords_sync_ctx, *info);
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
261 if (idx < 0) {
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
262 /* unknown keyword. */
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
263 break;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
264 }
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
265
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
266 array_append(keywords_r, &idx, 1);
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
267 break;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
268 }
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
269
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
270 /* unknown flag - ignore */
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
271 break;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
272 }
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
273 }
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
274
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
275 return 1;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
276 }
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
277
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
278 static void
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
279 maildir_filename_append_keywords(struct maildir_keywords_sync_ctx *ctx,
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
280 array_t *keywords, string_t *str)
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
281 {
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
282 ARRAY_SET_TYPE(keywords, unsigned int);
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
283 const unsigned int *indexes;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
284 unsigned int i, count;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
285 char chr;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
286
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
287 indexes = array_get(keywords, &count);
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
288 for (i = 0; i < count; i++) {
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
289 chr = maildir_keywords_idx_char(ctx, indexes[i]);
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
290 if (chr != '\0')
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
291 str_append_c(str, chr);
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
292 }
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
293 }
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
294
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
295 const char *maildir_filename_set_flags(struct maildir_index_sync_context *ctx,
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
296 const char *fname, enum mail_flags flags,
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
297 array_t *keywords)
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
298 {
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
299 string_t *flags_str;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
300 enum mail_flags flags_left;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
301 const char *info, *oldflags;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
302 int nextflag;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
303
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
304 /* remove the old :info from file name, and get the old flags */
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
305 info = strrchr(fname, MAILDIR_INFO_SEP);
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
306 if (info != NULL && strrchr(fname, '/') > info)
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
307 info = NULL;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
308
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
309 oldflags = "";
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
310 if (info != NULL) {
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
311 fname = t_strdup_until(fname, info);
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
312 if (info[1] == '2' && info[2] == MAILDIR_FLAGS_SEP)
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
313 oldflags = info+3;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
314 }
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
315
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
316 /* insert the new flags between old flags. flags must be sorted by
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
317 their ASCII code. unknown flags are kept. */
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
318 flags_str = t_str_new(256);
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
319 str_append(flags_str, fname);
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
320 str_append(flags_str, MAILDIR_FLAGS_FULL_SEP);
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
321 flags_left = flags;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
322 for (;;) {
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
323 /* skip all known flags */
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
324 while (*oldflags == 'D' || *oldflags == 'F' ||
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
325 *oldflags == 'R' || *oldflags == 'S' ||
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
326 *oldflags == 'T' ||
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
327 (*oldflags >= MAILDIR_KEYWORD_FIRST &&
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
328 *oldflags <= MAILDIR_KEYWORD_LAST))
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
329 oldflags++;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
330
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
331 nextflag = *oldflags == '\0' || *oldflags == MAILDIR_FLAGS_SEP ?
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
332 256 : (unsigned char) *oldflags;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
333
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
334 if ((flags_left & MAIL_DRAFT) && nextflag > 'D') {
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
335 str_append_c(flags_str, 'D');
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
336 flags_left &= ~MAIL_DRAFT;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
337 }
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
338 if ((flags_left & MAIL_FLAGGED) && nextflag > 'F') {
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
339 str_append_c(flags_str, 'F');
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
340 flags_left &= ~MAIL_FLAGGED;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
341 }
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
342 if ((flags_left & MAIL_ANSWERED) && nextflag > 'R') {
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
343 str_append_c(flags_str, 'R');
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
344 flags_left &= ~MAIL_ANSWERED;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
345 }
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
346 if ((flags_left & MAIL_SEEN) && nextflag > 'S') {
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
347 str_append_c(flags_str, 'S');
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
348 flags_left &= ~MAIL_SEEN;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
349 }
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
350 if ((flags_left & MAIL_DELETED) && nextflag > 'T') {
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
351 str_append_c(flags_str, 'T');
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
352 flags_left &= ~MAIL_DELETED;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
353 }
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
354
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
355 if (keywords != NULL && array_is_created(keywords) &&
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
356 nextflag > MAILDIR_KEYWORD_FIRST) {
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
357 maildir_filename_append_keywords(ctx->keywords_sync_ctx,
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
358 keywords, flags_str);
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
359 keywords = NULL;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
360 }
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
361
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
362 if (*oldflags == '\0' || *oldflags == MAILDIR_FLAGS_SEP)
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
363 break;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
364
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
365 str_append_c(flags_str, *oldflags);
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
366 oldflags++;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
367 }
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
368
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
369 if (*oldflags == MAILDIR_FLAGS_SEP) {
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
370 /* another flagset, we don't know about these, just keep them */
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
371 while (*oldflags != '\0')
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
372 str_append_c(flags_str, *oldflags++);
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
373 }
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
374
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
375 return str_c(flags_str);
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
376 }
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
377
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
378 static int maildir_expunge(struct maildir_mailbox *mbox, const char *path,
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
379 void *context __attr_unused__)
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
380 {
1955
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
381 if (unlink(path) == 0) {
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
382 mbox->dirty_cur_time = ioloop_time;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
383 return 1;
1955
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
384 }
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
385 if (errno == ENOENT)
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
386 return 0;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
387
3280
2c72492dfd91 Created mbox_storage and maildir_storage.
Timo Sirainen <tss@iki.fi>
parents: 3279
diff changeset
388 mail_storage_set_critical(STORAGE(mbox->storage),
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
389 "unlink(%s) failed: %m", path);
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
390 return -1;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
391 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
392
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
393 static int maildir_sync_flags(struct maildir_mailbox *mbox, const char *path,
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
394 void *context)
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
395 {
1956
d6941cd8afdc Added support for setting dirty flags for messages (TODO: undirty..)
Timo Sirainen <tss@iki.fi>
parents: 1955
diff changeset
396 struct maildir_index_sync_context *ctx = context;
3446
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
397 const struct mail_index_sync_rec *recs;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
398 const char *newpath;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
399 enum mail_flags flags;
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
400 array_t ARRAY_DEFINE(keywords, unsigned int);
3446
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
401 unsigned int i, count;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
402 uint8_t flags8;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
403
2272
ced88553af0b mail_index_sync_sort_flags() now merges flag changes so mail storage
Timo Sirainen <tss@iki.fi>
parents: 2258
diff changeset
404 ctx->dirty_state = 0;
2037
8763032d31bd Set dirty flags through transaction log, not directly. Some other flag
Timo Sirainen <tss@iki.fi>
parents: 2034
diff changeset
405
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
406 ARRAY_CREATE(&keywords, pool_datastack_create(), unsigned int, 16);
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
407 (void)maildir_filename_get_flags(ctx, path, &flags, &keywords);
3446
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
408 flags8 = flags;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
409
3446
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
410 recs = array_get_modifyable(&ctx->sync_recs, &count);
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
411 for (i = 0; i < count; i++) {
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
412 if (recs[i].uid1 != ctx->seq)
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
413 break;
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
414
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
415 switch (recs[i].type) {
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
416 case MAIL_INDEX_SYNC_TYPE_FLAGS:
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
417 mail_index_sync_flags_apply(&recs[i], &flags8);
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
418 break;
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
419 case MAIL_INDEX_SYNC_TYPE_KEYWORD_ADD:
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
420 case MAIL_INDEX_SYNC_TYPE_KEYWORD_REMOVE:
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
421 case MAIL_INDEX_SYNC_TYPE_KEYWORD_RESET:
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
422 mail_index_sync_keywords_apply(&recs[i], &keywords);
3446
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
423 break;
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
424 case MAIL_INDEX_SYNC_TYPE_APPEND:
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
425 case MAIL_INDEX_SYNC_TYPE_EXPUNGE:
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
426 i_unreached();
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
427 break;
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
428 }
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
429 }
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
430
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
431 newpath = maildir_filename_set_flags(ctx, path, flags8, &keywords);
1955
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
432 if (rename(path, newpath) == 0) {
2272
ced88553af0b mail_index_sync_sort_flags() now merges flag changes so mail storage
Timo Sirainen <tss@iki.fi>
parents: 2258
diff changeset
433 if ((flags8 & MAIL_INDEX_MAIL_FLAG_DIRTY) != 0)
ced88553af0b mail_index_sync_sort_flags() now merges flag changes so mail storage
Timo Sirainen <tss@iki.fi>
parents: 2258
diff changeset
434 ctx->dirty_state = -1;
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
435 mbox->dirty_cur_time = ioloop_time;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
436 return 1;
1955
0f0128b4af5d More syncing changes
Timo Sirainen <tss@iki.fi>
parents: 1954
diff changeset
437 }
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
438 if (errno == ENOENT)
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
439 return 0;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
440
2037
8763032d31bd Set dirty flags through transaction log, not directly. Some other flag
Timo Sirainen <tss@iki.fi>
parents: 2034
diff changeset
441 if (ENOSPACE(errno) || errno == EACCES) {
8763032d31bd Set dirty flags through transaction log, not directly. Some other flag
Timo Sirainen <tss@iki.fi>
parents: 2034
diff changeset
442 mail_index_update_flags(ctx->trans, ctx->seq, MODIFY_ADD,
3016
61c8d205d887 Initial support for keywords. Syncing to mbox/maildir doesn't work yet.
Timo Sirainen <tss@iki.fi>
parents: 2936
diff changeset
443 MAIL_INDEX_MAIL_FLAG_DIRTY);
2272
ced88553af0b mail_index_sync_sort_flags() now merges flag changes so mail storage
Timo Sirainen <tss@iki.fi>
parents: 2258
diff changeset
444 ctx->dirty_state = 1;
1956
d6941cd8afdc Added support for setting dirty flags for messages (TODO: undirty..)
Timo Sirainen <tss@iki.fi>
parents: 1955
diff changeset
445 return 1;
d6941cd8afdc Added support for setting dirty flags for messages (TODO: undirty..)
Timo Sirainen <tss@iki.fi>
parents: 1955
diff changeset
446 }
d6941cd8afdc Added support for setting dirty flags for messages (TODO: undirty..)
Timo Sirainen <tss@iki.fi>
parents: 1955
diff changeset
447
3280
2c72492dfd91 Created mbox_storage and maildir_storage.
Timo Sirainen <tss@iki.fi>
parents: 3279
diff changeset
448 mail_storage_set_critical(STORAGE(mbox->storage),
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
449 "rename(%s, %s) failed: %m", path, newpath);
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
450 return -1;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
451 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
452
3446
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
453 static int
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
454 maildir_sync_record_commit_until(struct maildir_index_sync_context *ctx,
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
455 uint32_t last_seq)
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
456 {
3446
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
457 struct mail_index_sync_rec *recs;
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
458 unsigned int i, count;
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
459 uint32_t seq, uid;
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
460 int expunged, flag_changed;
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
461
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
462 recs = array_get_modifyable(&ctx->sync_recs, &count);
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
463 for (seq = recs[0].uid1; seq <= last_seq; seq++) {
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
464 expunged = flag_changed = FALSE;
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
465 for (i = 0; i < count; i++) {
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
466 if (recs[i].uid1 > seq)
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
467 break;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
468
3446
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
469 i_assert(recs[i].uid1 == seq);
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
470 switch (recs[i].type) {
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
471 case MAIL_INDEX_SYNC_TYPE_EXPUNGE:
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
472 expunged = TRUE;
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
473 break;
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
474 case MAIL_INDEX_SYNC_TYPE_FLAGS:
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
475 case MAIL_INDEX_SYNC_TYPE_KEYWORD_ADD:
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
476 case MAIL_INDEX_SYNC_TYPE_KEYWORD_REMOVE:
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
477 case MAIL_INDEX_SYNC_TYPE_KEYWORD_RESET:
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
478 flag_changed = TRUE;
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
479 break;
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
480 case MAIL_INDEX_SYNC_TYPE_APPEND:
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
481 i_unreached();
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
482 break;
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
483 }
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
484 }
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
485
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
486 if (mail_index_lookup_uid(ctx->view, seq, &uid) < 0)
2033
4f6b1118a53d Transaction log contains only UIDs now, no more sequences which just mess up
Timo Sirainen <tss@iki.fi>
parents: 2009
diff changeset
487 return -1;
4f6b1118a53d Transaction log contains only UIDs now, no more sequences which just mess up
Timo Sirainen <tss@iki.fi>
parents: 2009
diff changeset
488
3446
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
489 ctx->seq = seq;
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
490 if (expunged) {
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
491 if (maildir_file_do(ctx->mbox, uid,
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
492 maildir_expunge, ctx) < 0)
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
493 return -1;
3446
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
494 } else if (flag_changed) {
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
495 if (maildir_file_do(ctx->mbox, uid,
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
496 maildir_sync_flags, ctx) < 0)
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
497 return -1;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
498 }
3446
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
499
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
500 for (i = count; i > 0; i--) {
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
501 if (++recs[i-1].uid1 > recs[i-1].uid2) {
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
502 array_delete(&ctx->sync_recs, i-1, 1);
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
503 recs = array_get_modifyable(&ctx->sync_recs,
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
504 &count);
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
505 if (count == 0) {
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
506 /* all sync_recs committed */
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
507 return 0;
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
508 }
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
509 }
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
510 }
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
511 }
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
512 return 0;
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
513 }
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
514
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
515 static int maildir_sync_record(struct maildir_index_sync_context *ctx,
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
516 const struct mail_index_sync_rec *sync_rec)
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
517 {
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
518 struct mail_index_view *view = ctx->view;
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
519 struct mail_index_sync_rec sync_copy;
2033
4f6b1118a53d Transaction log contains only UIDs now, no more sequences which just mess up
Timo Sirainen <tss@iki.fi>
parents: 2009
diff changeset
520
3446
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
521 if (sync_rec == NULL) {
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
522 /* deinit */
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
523 while (array_count(&ctx->sync_recs) > 0) {
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
524 if (maildir_sync_record_commit_until(ctx,
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
525 (uint32_t)-1) < 0)
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
526 return -1;
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
527 }
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
528 return 0;
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
529 }
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
530
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
531 if (sync_rec->type == MAIL_INDEX_SYNC_TYPE_APPEND)
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
532 return 0; /* ignore */
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
533
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
534 /* convert to sequences to avoid looping through huge holes in
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
535 UID range */
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
536 sync_copy = *sync_rec;
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
537 if (mail_index_lookup_uid_range(view, sync_rec->uid1,
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
538 sync_rec->uid2,
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
539 &sync_copy.uid1,
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
540 &sync_copy.uid2) < 0)
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
541 return -1;
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
542
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
543 if (sync_copy.uid1 == 0) {
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
544 /* UIDs were expunged */
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
545 return 0;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
546 }
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
547
3446
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
548 while (array_count(&ctx->sync_recs) > 0) {
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
549 const struct mail_index_sync_rec *rec =
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
550 array_idx(&ctx->sync_recs, 0);
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
551
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
552 i_assert(rec->uid1 <= sync_copy.uid1);
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
553 if (rec->uid1 == sync_copy.uid1)
2033
4f6b1118a53d Transaction log contains only UIDs now, no more sequences which just mess up
Timo Sirainen <tss@iki.fi>
parents: 2009
diff changeset
554 break;
4f6b1118a53d Transaction log contains only UIDs now, no more sequences which just mess up
Timo Sirainen <tss@iki.fi>
parents: 2009
diff changeset
555
3446
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
556 if (maildir_sync_record_commit_until(ctx, sync_copy.uid1-1) < 0)
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
557 return -1;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
558 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
559
3446
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
560 array_append(&ctx->sync_recs, &sync_copy, 1);
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
561 return 0;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
562 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
563
3446
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
564 static int maildir_sync_index_records(struct maildir_index_sync_context *ctx)
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
565 {
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
566 struct mail_index_sync_rec sync_rec;
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
567 int ret;
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
568
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
569 ret = mail_index_sync_next(ctx->sync_ctx, &sync_rec);
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
570 if (ret <= 0)
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
571 return ret;
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
572
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
573 ARRAY_CREATE(&ctx->sync_recs, pool_datastack_create(),
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
574 struct mail_index_sync_rec, 32);
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
575 do {
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
576 if (maildir_sync_record(ctx, &sync_rec) < 0)
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
577 return -1;
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
578
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
579 ret = mail_index_sync_next(ctx->sync_ctx, &sync_rec);
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
580 } while (ret > 0);
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
581
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
582 if (maildir_sync_record(ctx, NULL) < 0)
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
583 return -1;
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
584 return ret;
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
585 }
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
586
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
587 static struct maildir_sync_context *
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
588 maildir_sync_context_new(struct maildir_mailbox *mbox)
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
589 {
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
590 struct maildir_sync_context *ctx;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
591
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
592 ctx = t_new(struct maildir_sync_context, 1);
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
593 ctx->mbox = mbox;
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
594 ctx->new_dir = t_strconcat(mbox->path, "/new", NULL);
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
595 ctx->cur_dir = t_strconcat(mbox->path, "/cur", NULL);
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
596 return ctx;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
597 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
598
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
599 static void maildir_sync_deinit(struct maildir_sync_context *ctx)
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
600 {
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
601 if (ctx->uidlist_sync_ctx != NULL)
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
602 (void)maildir_uidlist_sync_deinit(ctx->uidlist_sync_ctx);
2818
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
603 if (ctx->index_sync_ctx != NULL)
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
604 maildir_sync_index_abort(ctx->index_sync_ctx);
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
605 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
606
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
607 static int maildir_fix_duplicate(struct maildir_mailbox *mbox, const char *dir,
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
608 const char *old_fname)
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
609 {
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
610 const char *new_fname, *old_path, *new_path;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
611 int ret = 0;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
612
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
613 t_push();
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
614
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
615 old_path = t_strconcat(dir, "/", old_fname, NULL);
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
616 new_fname = maildir_generate_tmp_filename(&ioloop_timeval);
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
617 new_path = t_strconcat(mbox->path, "/new/", new_fname, NULL);
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
618
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
619 if (rename(old_path, new_path) == 0) {
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
620 i_warning("Fixed duplicate in %s: %s -> %s",
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
621 mbox->path, old_fname, new_fname);
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
622 } else if (errno != ENOENT) {
3280
2c72492dfd91 Created mbox_storage and maildir_storage.
Timo Sirainen <tss@iki.fi>
parents: 3279
diff changeset
623 mail_storage_set_critical(STORAGE(mbox->storage),
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
624 "rename(%s, %s) failed: %m", old_path, new_path);
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
625 ret = -1;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
626 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
627 t_pop();
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
628
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
629 return ret;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
630 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
631
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
632 static int maildir_scan_dir(struct maildir_sync_context *ctx, int new_dir)
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
633 {
3280
2c72492dfd91 Created mbox_storage and maildir_storage.
Timo Sirainen <tss@iki.fi>
parents: 3279
diff changeset
634 struct mail_storage *storage = STORAGE(ctx->mbox->storage);
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
635 const char *dir;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
636 DIR *dirp;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
637 string_t *src, *dest;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
638 struct dirent *dp;
3435
6f7ce690358e If we have rename()d more than 5 files from new/ to cur/, rescan the
Timo Sirainen <tss@iki.fi>
parents: 3417
diff changeset
639 enum maildir_uidlist_rec_flag flags;
6f7ce690358e If we have rename()d more than 5 files from new/ to cur/, rescan the
Timo Sirainen <tss@iki.fi>
parents: 3417
diff changeset
640 unsigned int moves = 0;
1947
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
641 int move_new, ret = 1;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
642
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
643 dir = new_dir ? ctx->new_dir : ctx->cur_dir;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
644 dirp = opendir(dir);
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
645 if (dirp == NULL) {
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
646 mail_storage_set_critical(storage,
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
647 "opendir(%s) failed: %m", dir);
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
648 return -1;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
649 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
650
2121
0840edf34f37 Locking fixes. use less memory
Timo Sirainen <tss@iki.fi>
parents: 2067
diff changeset
651 t_push();
0840edf34f37 Locking fixes. use less memory
Timo Sirainen <tss@iki.fi>
parents: 2067
diff changeset
652 src = t_str_new(1024);
0840edf34f37 Locking fixes. use less memory
Timo Sirainen <tss@iki.fi>
parents: 2067
diff changeset
653 dest = t_str_new(1024);
0840edf34f37 Locking fixes. use less memory
Timo Sirainen <tss@iki.fi>
parents: 2067
diff changeset
654
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
655 move_new = new_dir && !mailbox_is_readonly(&ctx->mbox->ibox.box) &&
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
656 !ctx->mbox->ibox.keep_recent;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
657 while ((dp = readdir(dirp)) != NULL) {
2258
087a43e29492 No maildir filename checking after all.
Timo Sirainen <tss@iki.fi>
parents: 2256
diff changeset
658 if (dp->d_name[0] == '.')
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
659 continue;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
660
1978
6303ef092c5b mbox code compiles again, but syncing is only partially implemented so
Timo Sirainen <tss@iki.fi>
parents: 1970
diff changeset
661 ret = maildir_uidlist_sync_next_pre(ctx->uidlist_sync_ctx,
6303ef092c5b mbox code compiles again, but syncing is only partially implemented so
Timo Sirainen <tss@iki.fi>
parents: 1970
diff changeset
662 dp->d_name);
1984
9c159272f721 syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
663 if (ret == 0) {
2038
df504dad3aec Recent flag fixes. Should work perfectly now with maildir.
Timo Sirainen <tss@iki.fi>
parents: 2037
diff changeset
664 /* new file and we couldn't lock uidlist, check this
df504dad3aec Recent flag fixes. Should work perfectly now with maildir.
Timo Sirainen <tss@iki.fi>
parents: 2037
diff changeset
665 later in next sync. */
1984
9c159272f721 syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
666 if (new_dir)
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
667 ctx->mbox->last_new_mtime = 0;
1984
9c159272f721 syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
668 else
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
669 ctx->mbox->dirty_cur_time = ioloop_time;
1978
6303ef092c5b mbox code compiles again, but syncing is only partially implemented so
Timo Sirainen <tss@iki.fi>
parents: 1970
diff changeset
670 continue;
1984
9c159272f721 syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
671 }
1978
6303ef092c5b mbox code compiles again, but syncing is only partially implemented so
Timo Sirainen <tss@iki.fi>
parents: 1970
diff changeset
672 if (ret < 0)
6303ef092c5b mbox code compiles again, but syncing is only partially implemented so
Timo Sirainen <tss@iki.fi>
parents: 1970
diff changeset
673 break;
6303ef092c5b mbox code compiles again, but syncing is only partially implemented so
Timo Sirainen <tss@iki.fi>
parents: 1970
diff changeset
674
1947
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
675 flags = 0;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
676 if (move_new) {
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
677 str_truncate(src, 0);
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
678 str_truncate(dest, 0);
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
679 str_printfa(src, "%s/%s", ctx->new_dir, dp->d_name);
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
680 str_printfa(dest, "%s/%s", ctx->cur_dir, dp->d_name);
3417
b0bdf32564b7 Replaced ':' and ',' character usages with #defines, so they can be changed
Timo Sirainen <tss@iki.fi>
parents: 3340
diff changeset
681 if (strchr(dp->d_name, MAILDIR_INFO_SEP) == NULL) {
b0bdf32564b7 Replaced ':' and ',' character usages with #defines, so they can be changed
Timo Sirainen <tss@iki.fi>
parents: 3340
diff changeset
682 str_append(dest, MAILDIR_FLAGS_FULL_SEP);
b0bdf32564b7 Replaced ':' and ',' character usages with #defines, so they can be changed
Timo Sirainen <tss@iki.fi>
parents: 3340
diff changeset
683 }
1947
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
684 if (rename(str_c(src), str_c(dest)) == 0) {
1984
9c159272f721 syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
685 /* we moved it - it's \Recent for us */
3435
6f7ce690358e If we have rename()d more than 5 files from new/ to cur/, rescan the
Timo Sirainen <tss@iki.fi>
parents: 3417
diff changeset
686 moves++;
6f7ce690358e If we have rename()d more than 5 files from new/ to cur/, rescan the
Timo Sirainen <tss@iki.fi>
parents: 3417
diff changeset
687 ctx->mbox->dirty_cur_time = ioloop_time;
1947
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
688 flags |= MAILDIR_UIDLIST_REC_FLAG_MOVED |
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
689 MAILDIR_UIDLIST_REC_FLAG_RECENT;
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
690 } else if (ENOTFOUND(errno)) {
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
691 /* someone else moved it already */
3435
6f7ce690358e If we have rename()d more than 5 files from new/ to cur/, rescan the
Timo Sirainen <tss@iki.fi>
parents: 3417
diff changeset
692 moves++;
1947
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
693 flags |= MAILDIR_UIDLIST_REC_FLAG_MOVED;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
694 } else if (ENOSPACE(errno)) {
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
695 /* not enough disk space, leave here */
1947
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
696 flags |= MAILDIR_UIDLIST_REC_FLAG_NEW_DIR |
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
697 MAILDIR_UIDLIST_REC_FLAG_RECENT;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
698 move_new = FALSE;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
699 } else {
1947
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
700 flags |= MAILDIR_UIDLIST_REC_FLAG_NEW_DIR |
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
701 MAILDIR_UIDLIST_REC_FLAG_RECENT;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
702 mail_storage_set_critical(storage,
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
703 "rename(%s, %s) failed: %m",
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
704 str_c(src), str_c(dest));
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
705 }
1947
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
706 } else if (new_dir) {
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
707 flags |= MAILDIR_UIDLIST_REC_FLAG_NEW_DIR |
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
708 MAILDIR_UIDLIST_REC_FLAG_RECENT;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
709 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
710
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
711 ret = maildir_uidlist_sync_next(ctx->uidlist_sync_ctx,
1947
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
712 dp->d_name, flags);
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
713 if (ret <= 0) {
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
714 if (ret < 0)
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
715 break;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
716
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
717 /* possibly duplicate - try fixing it */
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
718 if (maildir_fix_duplicate(ctx->mbox,
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
719 dir, dp->d_name) < 0) {
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
720 ret = -1;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
721 break;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
722 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
723 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
724 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
725
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
726 if (closedir(dirp) < 0) {
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
727 mail_storage_set_critical(storage,
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
728 "closedir(%s) failed: %m", dir);
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
729 }
2121
0840edf34f37 Locking fixes. use less memory
Timo Sirainen <tss@iki.fi>
parents: 2067
diff changeset
730
0840edf34f37 Locking fixes. use less memory
Timo Sirainen <tss@iki.fi>
parents: 2067
diff changeset
731 t_pop();
3435
6f7ce690358e If we have rename()d more than 5 files from new/ to cur/, rescan the
Timo Sirainen <tss@iki.fi>
parents: 3417
diff changeset
732 return ret < 0 ? -1 : (moves <= MAILDIR_RENAME_RESCAN_COUNT ? 0 : 1);
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
733 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
734
3472
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
735 static void
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
736 maildir_sync_update_from_header(struct maildir_mailbox *mbox)
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
737 {
3472
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
738 uint64_t value;
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
739
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
740 /* FIXME: ugly, replace with extension header */
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
741 value = mail_index_get_header(mbox->ibox.view)->sync_size;
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
742 mbox->last_new_mtime = value & 0xffffffff;
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
743 mbox->last_new_sync_time = value >> 32;
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
744
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
745 mbox->last_cur_mtime =
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
746 mail_index_get_header(mbox->ibox.view)->sync_stamp;
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
747 }
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
748
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
749 static int
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
750 maildir_sync_quick_check(struct maildir_mailbox *mbox,
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
751 const char *new_dir, const char *cur_dir,
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
752 int *new_changed_r, int *cur_changed_r)
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
753 {
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
754 struct stat st;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
755 time_t new_mtime, cur_mtime;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
756
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
757 *new_changed_r = *cur_changed_r = FALSE;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
758
3472
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
759 if (stat(new_dir, &st) < 0) {
3280
2c72492dfd91 Created mbox_storage and maildir_storage.
Timo Sirainen <tss@iki.fi>
parents: 3279
diff changeset
760 mail_storage_set_critical(STORAGE(mbox->storage),
3472
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
761 "stat(%s) failed: %m", new_dir);
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
762 return -1;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
763 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
764 new_mtime = st.st_mtime;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
765
3472
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
766 if (stat(cur_dir, &st) < 0) {
3280
2c72492dfd91 Created mbox_storage and maildir_storage.
Timo Sirainen <tss@iki.fi>
parents: 3279
diff changeset
767 mail_storage_set_critical(STORAGE(mbox->storage),
3472
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
768 "stat(%s) failed: %m", cur_dir);
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
769 return -1;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
770 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
771 cur_mtime = st.st_mtime;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
772
2893
fd431866c674 mail_index_refresh() isn't public anymore, mail_index_view_open_locked()
Timo Sirainen <tss@iki.fi>
parents: 2892
diff changeset
773 /* cur stamp is kept in index, we don't have to sync if
3472
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
774 someone else has done it and updated the index.
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
775
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
776 FIXME: For now we're using sync_size field as the new/ dir's stamp.
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
777 Pretty ugly.. */
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
778 maildir_sync_update_from_header(mbox);
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
779 if ((mbox->dirty_cur_time == 0 && cur_mtime != mbox->last_cur_mtime) ||
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
780 (new_mtime != mbox->last_new_mtime)) {
2893
fd431866c674 mail_index_refresh() isn't public anymore, mail_index_view_open_locked()
Timo Sirainen <tss@iki.fi>
parents: 2892
diff changeset
781 /* check if the index has been updated.. */
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
782 if (mail_index_refresh(mbox->ibox.index) < 0) {
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
783 mail_storage_set_index_error(&mbox->ibox);
2064
5cfdc99fab69 Don't use internal last_cur_mtime stamp - it's index-specific so always get
Timo Sirainen <tss@iki.fi>
parents: 2053
diff changeset
784 return -1;
5cfdc99fab69 Don't use internal last_cur_mtime stamp - it's index-specific so always get
Timo Sirainen <tss@iki.fi>
parents: 2053
diff changeset
785 }
5cfdc99fab69 Don't use internal last_cur_mtime stamp - it's index-specific so always get
Timo Sirainen <tss@iki.fi>
parents: 2053
diff changeset
786
3472
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
787 maildir_sync_update_from_header(mbox);
1954
2f6e137cdc44 Syncing optimizations.
Timo Sirainen <tss@iki.fi>
parents: 1947
diff changeset
788 }
2f6e137cdc44 Syncing optimizations.
Timo Sirainen <tss@iki.fi>
parents: 1947
diff changeset
789
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
790 if (new_mtime != mbox->last_new_mtime ||
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
791 new_mtime >= mbox->last_new_sync_time - MAILDIR_SYNC_SECS) {
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
792 *new_changed_r = TRUE;
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
793 mbox->last_new_mtime = new_mtime;
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
794 mbox->last_new_sync_time = ioloop_time;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
795 }
1954
2f6e137cdc44 Syncing optimizations.
Timo Sirainen <tss@iki.fi>
parents: 1947
diff changeset
796
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
797 if (cur_mtime != mbox->last_cur_mtime ||
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
798 (mbox->dirty_cur_time != 0 &&
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
799 ioloop_time - mbox->dirty_cur_time > MAILDIR_SYNC_SECS)) {
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
800 /* cur/ changed, or delayed cur/ check */
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
801 *cur_changed_r = TRUE;
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
802 mbox->last_cur_mtime = cur_mtime;
1984
9c159272f721 syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
803
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
804 mbox->dirty_cur_time =
1984
9c159272f721 syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
805 cur_mtime >= ioloop_time - MAILDIR_SYNC_SECS ?
9c159272f721 syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
806 cur_mtime : 0;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
807 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
808
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
809 return 0;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
810 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
811
2818
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
812 struct maildir_index_sync_context *
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
813 maildir_sync_index_begin(struct maildir_mailbox *mbox)
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
814 {
2818
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
815 struct maildir_index_sync_context *sync_ctx;
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
816
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
817 sync_ctx = i_new(struct maildir_index_sync_context, 1);
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
818 sync_ctx->mbox = mbox;
2818
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
819
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
820 if (mail_index_sync_begin(mbox->ibox.index, &sync_ctx->sync_ctx,
2818
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
821 &sync_ctx->view, (uint32_t)-1, (uoff_t)-1,
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
822 FALSE, FALSE) <= 0) {
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
823 mail_storage_set_index_error(&mbox->ibox);
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
824 i_free(sync_ctx);
2818
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
825 return NULL;
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
826 }
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
827
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
828 sync_ctx->keywords_sync_ctx =
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
829 maildir_keywords_sync_init(mbox->keywords, mbox->ibox.index);
2818
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
830 return sync_ctx;
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
831 }
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
832
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
833 void maildir_sync_index_abort(struct maildir_index_sync_context *sync_ctx)
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
834 {
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
835 mail_index_sync_rollback(sync_ctx->sync_ctx);
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
836 maildir_keywords_sync_deinit(sync_ctx->keywords_sync_ctx);
2818
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
837 i_free(sync_ctx);
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
838 }
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
839
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
840 int maildir_sync_index_finish(struct maildir_index_sync_context *sync_ctx,
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
841 int partial)
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
842 {
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
843 struct maildir_mailbox *mbox = sync_ctx->mbox;
2818
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
844 struct mail_index_view *view = sync_ctx->view;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
845 struct maildir_uidlist_iter_ctx *iter;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
846 struct mail_index_transaction *trans;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
847 const struct mail_index_header *hdr;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
848 const struct mail_index_record *rec;
1954
2f6e137cdc44 Syncing optimizations.
Timo Sirainen <tss@iki.fi>
parents: 1947
diff changeset
849 uint32_t seq, uid;
2f6e137cdc44 Syncing optimizations.
Timo Sirainen <tss@iki.fi>
parents: 1947
diff changeset
850 enum maildir_uidlist_rec_flag uflags;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
851 const char *filename;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
852 enum mail_flags flags;
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
853 array_t ARRAY_DEFINE(keywords, unsigned int);
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
854 array_t ARRAY_DEFINE(idx_keywords, unsigned int);
2050
ee1095ccfd23 Index header changes now go through transaction log. Removed the kludgy
Timo Sirainen <tss@iki.fi>
parents: 2039
diff changeset
855 uint32_t uid_validity, next_uid;
3472
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
856 uint64_t value;
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
857 time_t old_new_sync_time;
3446
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
858 int ret = 0, full_rescan = FALSE;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
859
3453
f140acbe46b4 Assert/cleanup
Timo Sirainen <tss@iki.fi>
parents: 3447
diff changeset
860 i_assert(maildir_uidlist_is_locked(sync_ctx->mbox->uidlist));
f140acbe46b4 Assert/cleanup
Timo Sirainen <tss@iki.fi>
parents: 3447
diff changeset
861
3322
49071cc19102 If UIDVALIDITY changes, don't invalidate the whole index. Just expunge all
Timo Sirainen <tss@iki.fi>
parents: 3280
diff changeset
862 trans = mail_index_transaction_begin(view, FALSE, TRUE);
49071cc19102 If UIDVALIDITY changes, don't invalidate the whole index. Just expunge all
Timo Sirainen <tss@iki.fi>
parents: 3280
diff changeset
863 sync_ctx->trans = trans;
49071cc19102 If UIDVALIDITY changes, don't invalidate the whole index. Just expunge all
Timo Sirainen <tss@iki.fi>
parents: 3280
diff changeset
864
2892
62d53b49110d Changed mail_index_get_header() to return the header as return value because
Timo Sirainen <tss@iki.fi>
parents: 2877
diff changeset
865 hdr = mail_index_get_header(view);
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
866 uid_validity = maildir_uidlist_get_uid_validity(mbox->uidlist);
2051
596267d8e2e2 Some more UIDVALIDITY issues fixed.
Timo Sirainen <tss@iki.fi>
parents: 2050
diff changeset
867 if (uid_validity != hdr->uid_validity &&
596267d8e2e2 Some more UIDVALIDITY issues fixed.
Timo Sirainen <tss@iki.fi>
parents: 2050
diff changeset
868 uid_validity != 0 && hdr->uid_validity != 0) {
2050
ee1095ccfd23 Index header changes now go through transaction log. Removed the kludgy
Timo Sirainen <tss@iki.fi>
parents: 2039
diff changeset
869 /* uidvalidity changed and mailbox isn't being initialized,
3322
49071cc19102 If UIDVALIDITY changes, don't invalidate the whole index. Just expunge all
Timo Sirainen <tss@iki.fi>
parents: 3280
diff changeset
870 reset mailbox so we can add all messages as new */
3280
2c72492dfd91 Created mbox_storage and maildir_storage.
Timo Sirainen <tss@iki.fi>
parents: 3279
diff changeset
871 mail_storage_set_critical(STORAGE(mbox->storage),
2067
3f97439ba59e Path was missing from Maildir sync-errors.
Timo Sirainen <tss@iki.fi>
parents: 2064
diff changeset
872 "Maildir %s sync: UIDVALIDITY changed (%u -> %u)",
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
873 mbox->path, hdr->uid_validity, uid_validity);
3322
49071cc19102 If UIDVALIDITY changes, don't invalidate the whole index. Just expunge all
Timo Sirainen <tss@iki.fi>
parents: 3280
diff changeset
874
3340
9c8220dfde7c Don't try to handle UIDVALIDITY changes nicely after all. It causes
Timo Sirainen <tss@iki.fi>
parents: 3322
diff changeset
875 mail_index_mark_corrupted(mbox->ibox.index);
9c8220dfde7c Don't try to handle UIDVALIDITY changes nicely after all. It causes
Timo Sirainen <tss@iki.fi>
parents: 3322
diff changeset
876 return -1;
2050
ee1095ccfd23 Index header changes now go through transaction log. Removed the kludgy
Timo Sirainen <tss@iki.fi>
parents: 2039
diff changeset
877 }
ee1095ccfd23 Index header changes now go through transaction log. Removed the kludgy
Timo Sirainen <tss@iki.fi>
parents: 2039
diff changeset
878
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
879 seq = 0;
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
880 ARRAY_CREATE(&keywords, pool_datastack_create(),
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
881 unsigned int, MAILDIR_MAX_KEYWORDS);
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
882 ARRAY_CREATE(&idx_keywords, pool_datastack_create(),
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
883 unsigned int, MAILDIR_MAX_KEYWORDS);
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
884 iter = maildir_uidlist_iter_init(mbox->uidlist);
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
885 while (maildir_uidlist_iter_next(iter, &uid, &uflags, &filename)) {
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
886 maildir_filename_get_flags(sync_ctx, filename,
3016
61c8d205d887 Initial support for keywords. Syncing to mbox/maildir doesn't work yet.
Timo Sirainen <tss@iki.fi>
parents: 2936
diff changeset
887 &flags, &keywords);
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
888
2038
df504dad3aec Recent flag fixes. Should work perfectly now with maildir.
Timo Sirainen <tss@iki.fi>
parents: 2037
diff changeset
889 if ((uflags & MAILDIR_UIDLIST_REC_FLAG_RECENT) != 0 &&
df504dad3aec Recent flag fixes. Should work perfectly now with maildir.
Timo Sirainen <tss@iki.fi>
parents: 2037
diff changeset
890 (uflags & MAILDIR_UIDLIST_REC_FLAG_NEW_DIR) != 0 &&
df504dad3aec Recent flag fixes. Should work perfectly now with maildir.
Timo Sirainen <tss@iki.fi>
parents: 2037
diff changeset
891 (uflags & MAILDIR_UIDLIST_REC_FLAG_MOVED) == 0) {
df504dad3aec Recent flag fixes. Should work perfectly now with maildir.
Timo Sirainen <tss@iki.fi>
parents: 2037
diff changeset
892 /* mail is recent for next session as well */
df504dad3aec Recent flag fixes. Should work perfectly now with maildir.
Timo Sirainen <tss@iki.fi>
parents: 2037
diff changeset
893 flags |= MAIL_RECENT;
df504dad3aec Recent flag fixes. Should work perfectly now with maildir.
Timo Sirainen <tss@iki.fi>
parents: 2037
diff changeset
894 }
df504dad3aec Recent flag fixes. Should work perfectly now with maildir.
Timo Sirainen <tss@iki.fi>
parents: 2037
diff changeset
895
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
896 __again:
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
897 seq++;
1954
2f6e137cdc44 Syncing optimizations.
Timo Sirainen <tss@iki.fi>
parents: 1947
diff changeset
898
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
899 if (seq > hdr->messages_count) {
1984
9c159272f721 syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
900 if (uid < hdr->next_uid) {
2053
66f5d28f9b27 race condition fixes
Timo Sirainen <tss@iki.fi>
parents: 2051
diff changeset
901 /* most likely a race condition: we read the
66f5d28f9b27 race condition fixes
Timo Sirainen <tss@iki.fi>
parents: 2051
diff changeset
902 maildir, then someone else expunged messages
66f5d28f9b27 race condition fixes
Timo Sirainen <tss@iki.fi>
parents: 2051
diff changeset
903 and committed changes to index. so, this
66f5d28f9b27 race condition fixes
Timo Sirainen <tss@iki.fi>
parents: 2051
diff changeset
904 message shouldn't actually exist. mark it
66f5d28f9b27 race condition fixes
Timo Sirainen <tss@iki.fi>
parents: 2051
diff changeset
905 racy and check in next sync.
66f5d28f9b27 race condition fixes
Timo Sirainen <tss@iki.fi>
parents: 2051
diff changeset
906
66f5d28f9b27 race condition fixes
Timo Sirainen <tss@iki.fi>
parents: 2051
diff changeset
907 the difference between this and the later
66f5d28f9b27 race condition fixes
Timo Sirainen <tss@iki.fi>
parents: 2051
diff changeset
908 check is that this one happens when messages
66f5d28f9b27 race condition fixes
Timo Sirainen <tss@iki.fi>
parents: 2051
diff changeset
909 are expunged from the end */
66f5d28f9b27 race condition fixes
Timo Sirainen <tss@iki.fi>
parents: 2051
diff changeset
910 if ((uflags &
2228
d19ba01fb5cd partial syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 2173
diff changeset
911 MAILDIR_UIDLIST_REC_FLAG_NONSYNCED) != 0) {
d19ba01fb5cd partial syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 2173
diff changeset
912 /* partial syncing */
d19ba01fb5cd partial syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 2173
diff changeset
913 continue;
d19ba01fb5cd partial syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 2173
diff changeset
914 }
d19ba01fb5cd partial syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 2173
diff changeset
915 if ((uflags &
2053
66f5d28f9b27 race condition fixes
Timo Sirainen <tss@iki.fi>
parents: 2051
diff changeset
916 MAILDIR_UIDLIST_REC_FLAG_RACING) != 0) {
66f5d28f9b27 race condition fixes
Timo Sirainen <tss@iki.fi>
parents: 2051
diff changeset
917 mail_storage_set_critical(
3280
2c72492dfd91 Created mbox_storage and maildir_storage.
Timo Sirainen <tss@iki.fi>
parents: 3279
diff changeset
918 STORAGE(mbox->storage),
2067
3f97439ba59e Path was missing from Maildir sync-errors.
Timo Sirainen <tss@iki.fi>
parents: 2064
diff changeset
919 "Maildir %s sync: "
3f97439ba59e Path was missing from Maildir sync-errors.
Timo Sirainen <tss@iki.fi>
parents: 2064
diff changeset
920 "UID < next_uid "
2053
66f5d28f9b27 race condition fixes
Timo Sirainen <tss@iki.fi>
parents: 2051
diff changeset
921 "(%u < %u, file = %s)",
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
922 mbox->path, uid, hdr->next_uid,
2067
3f97439ba59e Path was missing from Maildir sync-errors.
Timo Sirainen <tss@iki.fi>
parents: 2064
diff changeset
923 filename);
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
924 mail_index_mark_corrupted(
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
925 mbox->ibox.index);
2053
66f5d28f9b27 race condition fixes
Timo Sirainen <tss@iki.fi>
parents: 2051
diff changeset
926 ret = -1;
66f5d28f9b27 race condition fixes
Timo Sirainen <tss@iki.fi>
parents: 2051
diff changeset
927 break;
66f5d28f9b27 race condition fixes
Timo Sirainen <tss@iki.fi>
parents: 2051
diff changeset
928 }
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
929 mbox->dirty_cur_time = ioloop_time;
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
930 maildir_uidlist_add_flags(mbox->uidlist,
2053
66f5d28f9b27 race condition fixes
Timo Sirainen <tss@iki.fi>
parents: 2051
diff changeset
931 filename,
66f5d28f9b27 race condition fixes
Timo Sirainen <tss@iki.fi>
parents: 2051
diff changeset
932 MAILDIR_UIDLIST_REC_FLAG_RACING);
66f5d28f9b27 race condition fixes
Timo Sirainen <tss@iki.fi>
parents: 2051
diff changeset
933
66f5d28f9b27 race condition fixes
Timo Sirainen <tss@iki.fi>
parents: 2051
diff changeset
934 seq--;
66f5d28f9b27 race condition fixes
Timo Sirainen <tss@iki.fi>
parents: 2051
diff changeset
935 continue;
1984
9c159272f721 syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
936 }
9c159272f721 syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
937
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
938 mail_index_append(trans, uid, &seq);
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
939 mail_index_update_flags(trans, seq, MODIFY_REPLACE,
3016
61c8d205d887 Initial support for keywords. Syncing to mbox/maildir doesn't work yet.
Timo Sirainen <tss@iki.fi>
parents: 2936
diff changeset
940 flags);
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
941
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
942 if (array_count(&keywords) > 0) {
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
943 struct mail_keywords *kw;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
944
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
945 kw = mail_index_keywords_create_from_indexes(
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
946 trans, &keywords);
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
947 mail_index_update_keywords(trans, seq,
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
948 MODIFY_REPLACE, kw);
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
949 mail_index_keywords_free(kw);
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
950 }
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
951 continue;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
952 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
953
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
954 if (mail_index_lookup(view, seq, &rec) < 0) {
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
955 ret = -1;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
956 break;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
957 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
958
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
959 if (rec->uid < uid) {
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
960 /* expunged */
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
961 mail_index_expunge(trans, seq);
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
962 goto __again;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
963 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
964
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
965 if (rec->uid > uid) {
2034
8078400fe561 Fix bogus "UID inserted in the middle of mailbox" errors
Timo Sirainen <tss@iki.fi>
parents: 2033
diff changeset
966 /* most likely a race condition: we read the
8078400fe561 Fix bogus "UID inserted in the middle of mailbox" errors
Timo Sirainen <tss@iki.fi>
parents: 2033
diff changeset
967 maildir, then someone else expunged messages and
8078400fe561 Fix bogus "UID inserted in the middle of mailbox" errors
Timo Sirainen <tss@iki.fi>
parents: 2033
diff changeset
968 committed changes to index. so, this message
2053
66f5d28f9b27 race condition fixes
Timo Sirainen <tss@iki.fi>
parents: 2051
diff changeset
969 shouldn't actually exist. mark it racy and check
66f5d28f9b27 race condition fixes
Timo Sirainen <tss@iki.fi>
parents: 2051
diff changeset
970 in next sync. */
2228
d19ba01fb5cd partial syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 2173
diff changeset
971 if ((uflags &
d19ba01fb5cd partial syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 2173
diff changeset
972 MAILDIR_UIDLIST_REC_FLAG_NONSYNCED) != 0) {
d19ba01fb5cd partial syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 2173
diff changeset
973 /* partial syncing */
d19ba01fb5cd partial syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 2173
diff changeset
974 seq--;
d19ba01fb5cd partial syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 2173
diff changeset
975 continue;
d19ba01fb5cd partial syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 2173
diff changeset
976 }
2053
66f5d28f9b27 race condition fixes
Timo Sirainen <tss@iki.fi>
parents: 2051
diff changeset
977 if ((uflags & MAILDIR_UIDLIST_REC_FLAG_RACING) != 0) {
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
978 mail_storage_set_critical(
3280
2c72492dfd91 Created mbox_storage and maildir_storage.
Timo Sirainen <tss@iki.fi>
parents: 3279
diff changeset
979 STORAGE(mbox->storage),
2570
372d4b90c076 cleanup
Timo Sirainen <tss@iki.fi>
parents: 2533
diff changeset
980 "Maildir %s sync: "
372d4b90c076 cleanup
Timo Sirainen <tss@iki.fi>
parents: 2533
diff changeset
981 "UID inserted in the middle of mailbox "
2034
8078400fe561 Fix bogus "UID inserted in the middle of mailbox" errors
Timo Sirainen <tss@iki.fi>
parents: 2033
diff changeset
982 "(%u > %u, file = %s)",
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
983 mbox->path, rec->uid, uid, filename);
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
984 mail_index_mark_corrupted(mbox->ibox.index);
2034
8078400fe561 Fix bogus "UID inserted in the middle of mailbox" errors
Timo Sirainen <tss@iki.fi>
parents: 2033
diff changeset
985 ret = -1;
8078400fe561 Fix bogus "UID inserted in the middle of mailbox" errors
Timo Sirainen <tss@iki.fi>
parents: 2033
diff changeset
986 break;
2064
5cfdc99fab69 Don't use internal last_cur_mtime stamp - it's index-specific so always get
Timo Sirainen <tss@iki.fi>
parents: 2053
diff changeset
987 }
2053
66f5d28f9b27 race condition fixes
Timo Sirainen <tss@iki.fi>
parents: 2051
diff changeset
988
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
989 mbox->dirty_cur_time = ioloop_time;
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
990 maildir_uidlist_add_flags(mbox->uidlist, filename,
2053
66f5d28f9b27 race condition fixes
Timo Sirainen <tss@iki.fi>
parents: 2051
diff changeset
991 MAILDIR_UIDLIST_REC_FLAG_RACING);
2034
8078400fe561 Fix bogus "UID inserted in the middle of mailbox" errors
Timo Sirainen <tss@iki.fi>
parents: 2033
diff changeset
992
8078400fe561 Fix bogus "UID inserted in the middle of mailbox" errors
Timo Sirainen <tss@iki.fi>
parents: 2033
diff changeset
993 seq--;
8078400fe561 Fix bogus "UID inserted in the middle of mailbox" errors
Timo Sirainen <tss@iki.fi>
parents: 2033
diff changeset
994 continue;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
995 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
996
2320
8a6666a9ac98 Handle recent flags in index file correctly. Fixes recent flag losing when
Timo Sirainen <tss@iki.fi>
parents: 2272
diff changeset
997 if ((rec->flags & MAIL_RECENT) != 0) {
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
998 index_mailbox_set_recent(&mbox->ibox, seq);
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
999 if (mbox->ibox.keep_recent) {
2320
8a6666a9ac98 Handle recent flags in index file correctly. Fixes recent flag losing when
Timo Sirainen <tss@iki.fi>
parents: 2272
diff changeset
1000 flags |= MAIL_RECENT;
8a6666a9ac98 Handle recent flags in index file correctly. Fixes recent flag losing when
Timo Sirainen <tss@iki.fi>
parents: 2272
diff changeset
1001 } else {
8a6666a9ac98 Handle recent flags in index file correctly. Fixes recent flag losing when
Timo Sirainen <tss@iki.fi>
parents: 2272
diff changeset
1002 mail_index_update_flags(trans, seq,
8a6666a9ac98 Handle recent flags in index file correctly. Fixes recent flag losing when
Timo Sirainen <tss@iki.fi>
parents: 2272
diff changeset
1003 MODIFY_REMOVE,
3016
61c8d205d887 Initial support for keywords. Syncing to mbox/maildir doesn't work yet.
Timo Sirainen <tss@iki.fi>
parents: 2936
diff changeset
1004 MAIL_RECENT);
2320
8a6666a9ac98 Handle recent flags in index file correctly. Fixes recent flag losing when
Timo Sirainen <tss@iki.fi>
parents: 2272
diff changeset
1005 }
8a6666a9ac98 Handle recent flags in index file correctly. Fixes recent flag losing when
Timo Sirainen <tss@iki.fi>
parents: 2272
diff changeset
1006 }
8a6666a9ac98 Handle recent flags in index file correctly. Fixes recent flag losing when
Timo Sirainen <tss@iki.fi>
parents: 2272
diff changeset
1007
2228
d19ba01fb5cd partial syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 2173
diff changeset
1008 if ((uflags & MAILDIR_UIDLIST_REC_FLAG_NONSYNCED) != 0) {
d19ba01fb5cd partial syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 2173
diff changeset
1009 /* partial syncing */
3436
3c51658d6846 We didn't notice if messages were deleted directly from new/.
Timo Sirainen <tss@iki.fi>
parents: 3435
diff changeset
1010 if ((flags & MAIL_RECENT) != 0) {
3c51658d6846 We didn't notice if messages were deleted directly from new/.
Timo Sirainen <tss@iki.fi>
parents: 3435
diff changeset
1011 /* we last saw this mail in new/, but it's
3c51658d6846 We didn't notice if messages were deleted directly from new/.
Timo Sirainen <tss@iki.fi>
parents: 3435
diff changeset
1012 not there anymore. possibly expunged,
3c51658d6846 We didn't notice if messages were deleted directly from new/.
Timo Sirainen <tss@iki.fi>
parents: 3435
diff changeset
1013 make sure. */
3c51658d6846 We didn't notice if messages were deleted directly from new/.
Timo Sirainen <tss@iki.fi>
parents: 3435
diff changeset
1014 full_rescan = TRUE;
3c51658d6846 We didn't notice if messages were deleted directly from new/.
Timo Sirainen <tss@iki.fi>
parents: 3435
diff changeset
1015 }
2228
d19ba01fb5cd partial syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 2173
diff changeset
1016 continue;
d19ba01fb5cd partial syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 2173
diff changeset
1017 }
d19ba01fb5cd partial syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 2173
diff changeset
1018
1956
d6941cd8afdc Added support for setting dirty flags for messages (TODO: undirty..)
Timo Sirainen <tss@iki.fi>
parents: 1955
diff changeset
1019 if ((rec->flags & MAIL_INDEX_MAIL_FLAG_DIRTY) != 0) {
d6941cd8afdc Added support for setting dirty flags for messages (TODO: undirty..)
Timo Sirainen <tss@iki.fi>
parents: 1955
diff changeset
1020 /* we haven't been able to update maildir with this
d6941cd8afdc Added support for setting dirty flags for messages (TODO: undirty..)
Timo Sirainen <tss@iki.fi>
parents: 1955
diff changeset
1021 record's flag changes. don't sync them. */
d6941cd8afdc Added support for setting dirty flags for messages (TODO: undirty..)
Timo Sirainen <tss@iki.fi>
parents: 1955
diff changeset
1022 continue;
d6941cd8afdc Added support for setting dirty flags for messages (TODO: undirty..)
Timo Sirainen <tss@iki.fi>
parents: 1955
diff changeset
1023 }
d6941cd8afdc Added support for setting dirty flags for messages (TODO: undirty..)
Timo Sirainen <tss@iki.fi>
parents: 1955
diff changeset
1024
2038
df504dad3aec Recent flag fixes. Should work perfectly now with maildir.
Timo Sirainen <tss@iki.fi>
parents: 2037
diff changeset
1025 if (((uint8_t)flags & ~MAIL_RECENT) !=
3016
61c8d205d887 Initial support for keywords. Syncing to mbox/maildir doesn't work yet.
Timo Sirainen <tss@iki.fi>
parents: 2936
diff changeset
1026 (rec->flags & (MAIL_FLAGS_MASK^MAIL_RECENT))) {
2038
df504dad3aec Recent flag fixes. Should work perfectly now with maildir.
Timo Sirainen <tss@iki.fi>
parents: 2037
diff changeset
1027 /* FIXME: this is wrong if there's pending changes in
df504dad3aec Recent flag fixes. Should work perfectly now with maildir.
Timo Sirainen <tss@iki.fi>
parents: 2037
diff changeset
1028 transaction log already. it gets fixed in next sync
df504dad3aec Recent flag fixes. Should work perfectly now with maildir.
Timo Sirainen <tss@iki.fi>
parents: 2037
diff changeset
1029 however.. */
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1030 mail_index_update_flags(trans, seq, MODIFY_REPLACE,
3016
61c8d205d887 Initial support for keywords. Syncing to mbox/maildir doesn't work yet.
Timo Sirainen <tss@iki.fi>
parents: 2936
diff changeset
1031 flags);
2038
df504dad3aec Recent flag fixes. Should work perfectly now with maildir.
Timo Sirainen <tss@iki.fi>
parents: 2037
diff changeset
1032 } else if ((flags & MAIL_RECENT) == 0 &&
df504dad3aec Recent flag fixes. Should work perfectly now with maildir.
Timo Sirainen <tss@iki.fi>
parents: 2037
diff changeset
1033 (rec->flags & MAIL_RECENT) != 0) {
df504dad3aec Recent flag fixes. Should work perfectly now with maildir.
Timo Sirainen <tss@iki.fi>
parents: 2037
diff changeset
1034 /* just remove recent flag */
df504dad3aec Recent flag fixes. Should work perfectly now with maildir.
Timo Sirainen <tss@iki.fi>
parents: 2037
diff changeset
1035 mail_index_update_flags(trans, seq, MODIFY_REMOVE,
3016
61c8d205d887 Initial support for keywords. Syncing to mbox/maildir doesn't work yet.
Timo Sirainen <tss@iki.fi>
parents: 2936
diff changeset
1036 MAIL_RECENT);
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1037 }
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1038
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1039 /* update keywords if they have changed */
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1040 array_clear(&idx_keywords);
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1041 if (mail_index_lookup_keywords(view, seq, &idx_keywords) < 0) {
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1042 ret = -1;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1043 break;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1044 }
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1045 if (!array_cmp(&keywords, &idx_keywords)) {
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1046 struct mail_keywords *kw;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1047
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1048 kw = mail_index_keywords_create_from_indexes(
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1049 trans, &keywords);
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1050 mail_index_update_keywords(trans, seq,
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1051 MODIFY_REPLACE, kw);
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1052 mail_index_keywords_free(kw);
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1053 }
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1054 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1055 maildir_uidlist_iter_deinit(iter);
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1056 array_free(&keywords);
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1057
2533
89911d2a9126 Don't break if saving messages to maildir without index files.
Timo Sirainen <tss@iki.fi>
parents: 2393
diff changeset
1058 if (!partial) {
1954
2f6e137cdc44 Syncing optimizations.
Timo Sirainen <tss@iki.fi>
parents: 1947
diff changeset
1059 /* expunge the rest */
2f6e137cdc44 Syncing optimizations.
Timo Sirainen <tss@iki.fi>
parents: 1947
diff changeset
1060 for (seq++; seq <= hdr->messages_count; seq++)
2f6e137cdc44 Syncing optimizations.
Timo Sirainen <tss@iki.fi>
parents: 1947
diff changeset
1061 mail_index_expunge(trans, seq);
2f6e137cdc44 Syncing optimizations.
Timo Sirainen <tss@iki.fi>
parents: 1947
diff changeset
1062 }
2f6e137cdc44 Syncing optimizations.
Timo Sirainen <tss@iki.fi>
parents: 1947
diff changeset
1063
2037
8763032d31bd Set dirty flags through transaction log, not directly. Some other flag
Timo Sirainen <tss@iki.fi>
parents: 2034
diff changeset
1064 /* now, sync the index */
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
1065 mbox->syncing_commit = TRUE;
3446
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
1066 if (maildir_sync_index_records(sync_ctx) < 0)
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
1067 ret = -1;
113c888cdca1 Merge changes from multiple index sync records into one before actually
Timo Sirainen <tss@iki.fi>
parents: 3436
diff changeset
1068 mbox->syncing_commit = FALSE;
2037
8763032d31bd Set dirty flags through transaction log, not directly. Some other flag
Timo Sirainen <tss@iki.fi>
parents: 2034
diff changeset
1069
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
1070 if (mbox->dirty_cur_time == 0 &&
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
1071 mbox->last_cur_mtime != (time_t)hdr->sync_stamp) {
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
1072 uint32_t sync_stamp = mbox->last_cur_mtime;
2050
ee1095ccfd23 Index header changes now go through transaction log. Removed the kludgy
Timo Sirainen <tss@iki.fi>
parents: 2039
diff changeset
1073
ee1095ccfd23 Index header changes now go through transaction log. Removed the kludgy
Timo Sirainen <tss@iki.fi>
parents: 2039
diff changeset
1074 mail_index_update_header(trans,
ee1095ccfd23 Index header changes now go through transaction log. Removed the kludgy
Timo Sirainen <tss@iki.fi>
parents: 2039
diff changeset
1075 offsetof(struct mail_index_header, sync_stamp),
3322
49071cc19102 If UIDVALIDITY changes, don't invalidate the whole index. Just expunge all
Timo Sirainen <tss@iki.fi>
parents: 3280
diff changeset
1076 &sync_stamp, sizeof(sync_stamp), TRUE);
2050
ee1095ccfd23 Index header changes now go through transaction log. Removed the kludgy
Timo Sirainen <tss@iki.fi>
parents: 2039
diff changeset
1077 }
ee1095ccfd23 Index header changes now go through transaction log. Removed the kludgy
Timo Sirainen <tss@iki.fi>
parents: 2039
diff changeset
1078
3472
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
1079 /* FIXME: use a header extension instead of sync_size.. */
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
1080 value = mbox->last_new_mtime;
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
1081 old_new_sync_time = hdr->sync_size >> 32;
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
1082 if (mbox->last_new_mtime >= old_new_sync_time - MAILDIR_SYNC_SECS) {
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
1083 value |= (uint64_t)mbox->last_new_sync_time << 32;
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
1084 } else {
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
1085 value |= (uint64_t)old_new_sync_time << 32;
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
1086 }
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
1087 if (value != hdr->sync_size) {
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
1088 uint64_t sync_stamp = mbox->last_new_mtime |
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
1089 ((uint64_t)mbox->last_new_sync_time << 32);
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
1090
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
1091 mail_index_update_header(trans,
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
1092 offsetof(struct mail_index_header, sync_size),
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
1093 &sync_stamp, sizeof(sync_stamp), TRUE);
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
1094 }
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
1095
2051
596267d8e2e2 Some more UIDVALIDITY issues fixed.
Timo Sirainen <tss@iki.fi>
parents: 2050
diff changeset
1096 if (hdr->uid_validity == 0) {
596267d8e2e2 Some more UIDVALIDITY issues fixed.
Timo Sirainen <tss@iki.fi>
parents: 2050
diff changeset
1097 /* get the initial uidvalidity */
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
1098 if (maildir_uidlist_update(mbox->uidlist) < 0)
2051
596267d8e2e2 Some more UIDVALIDITY issues fixed.
Timo Sirainen <tss@iki.fi>
parents: 2050
diff changeset
1099 ret = -1;
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
1100 uid_validity = maildir_uidlist_get_uid_validity(mbox->uidlist);
2051
596267d8e2e2 Some more UIDVALIDITY issues fixed.
Timo Sirainen <tss@iki.fi>
parents: 2050
diff changeset
1101 if (uid_validity == 0) {
596267d8e2e2 Some more UIDVALIDITY issues fixed.
Timo Sirainen <tss@iki.fi>
parents: 2050
diff changeset
1102 uid_validity = ioloop_time;
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
1103 maildir_uidlist_set_uid_validity(mbox->uidlist,
2051
596267d8e2e2 Some more UIDVALIDITY issues fixed.
Timo Sirainen <tss@iki.fi>
parents: 2050
diff changeset
1104 uid_validity);
596267d8e2e2 Some more UIDVALIDITY issues fixed.
Timo Sirainen <tss@iki.fi>
parents: 2050
diff changeset
1105 }
596267d8e2e2 Some more UIDVALIDITY issues fixed.
Timo Sirainen <tss@iki.fi>
parents: 2050
diff changeset
1106 } else if (uid_validity == 0) {
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
1107 maildir_uidlist_set_uid_validity(mbox->uidlist,
2051
596267d8e2e2 Some more UIDVALIDITY issues fixed.
Timo Sirainen <tss@iki.fi>
parents: 2050
diff changeset
1108 hdr->uid_validity);
596267d8e2e2 Some more UIDVALIDITY issues fixed.
Timo Sirainen <tss@iki.fi>
parents: 2050
diff changeset
1109 }
596267d8e2e2 Some more UIDVALIDITY issues fixed.
Timo Sirainen <tss@iki.fi>
parents: 2050
diff changeset
1110
596267d8e2e2 Some more UIDVALIDITY issues fixed.
Timo Sirainen <tss@iki.fi>
parents: 2050
diff changeset
1111 if (uid_validity != hdr->uid_validity && uid_validity != 0) {
2050
ee1095ccfd23 Index header changes now go through transaction log. Removed the kludgy
Timo Sirainen <tss@iki.fi>
parents: 2039
diff changeset
1112 mail_index_update_header(trans,
ee1095ccfd23 Index header changes now go through transaction log. Removed the kludgy
Timo Sirainen <tss@iki.fi>
parents: 2039
diff changeset
1113 offsetof(struct mail_index_header, uid_validity),
3322
49071cc19102 If UIDVALIDITY changes, don't invalidate the whole index. Just expunge all
Timo Sirainen <tss@iki.fi>
parents: 3280
diff changeset
1114 &uid_validity, sizeof(uid_validity), TRUE);
2050
ee1095ccfd23 Index header changes now go through transaction log. Removed the kludgy
Timo Sirainen <tss@iki.fi>
parents: 2039
diff changeset
1115 }
ee1095ccfd23 Index header changes now go through transaction log. Removed the kludgy
Timo Sirainen <tss@iki.fi>
parents: 2039
diff changeset
1116
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
1117 next_uid = maildir_uidlist_get_next_uid(mbox->uidlist);
2050
ee1095ccfd23 Index header changes now go through transaction log. Removed the kludgy
Timo Sirainen <tss@iki.fi>
parents: 2039
diff changeset
1118 if (next_uid != 0 && hdr->next_uid != next_uid) {
ee1095ccfd23 Index header changes now go through transaction log. Removed the kludgy
Timo Sirainen <tss@iki.fi>
parents: 2039
diff changeset
1119 mail_index_update_header(trans,
ee1095ccfd23 Index header changes now go through transaction log. Removed the kludgy
Timo Sirainen <tss@iki.fi>
parents: 2039
diff changeset
1120 offsetof(struct mail_index_header, next_uid),
3322
49071cc19102 If UIDVALIDITY changes, don't invalidate the whole index. Just expunge all
Timo Sirainen <tss@iki.fi>
parents: 3280
diff changeset
1121 &next_uid, sizeof(next_uid), FALSE);
2050
ee1095ccfd23 Index header changes now go through transaction log. Removed the kludgy
Timo Sirainen <tss@iki.fi>
parents: 2039
diff changeset
1122 }
ee1095ccfd23 Index header changes now go through transaction log. Removed the kludgy
Timo Sirainen <tss@iki.fi>
parents: 2039
diff changeset
1123
2393
d598c82a3c22 Renamed mail_index_sync_end() to _commit() and added _rollback(). Fixed mbox
Timo Sirainen <tss@iki.fi>
parents: 2373
diff changeset
1124 if (ret < 0) {
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1125 mail_index_transaction_rollback(trans);
2818
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1126 mail_index_sync_rollback(sync_ctx->sync_ctx);
2393
d598c82a3c22 Renamed mail_index_sync_end() to _commit() and added _rollback(). Fixed mbox
Timo Sirainen <tss@iki.fi>
parents: 2373
diff changeset
1127 } else {
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1128 uint32_t seq;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1129 uoff_t offset;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1130
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1131 if (mail_index_transaction_commit(trans, &seq, &offset) < 0)
2050
ee1095ccfd23 Index header changes now go through transaction log. Removed the kludgy
Timo Sirainen <tss@iki.fi>
parents: 2039
diff changeset
1132 ret = -1;
2009
2e77ee652e2e When committing with no changes, don't try to sync them later.
Timo Sirainen <tss@iki.fi>
parents: 1988
diff changeset
1133 else if (seq != 0) {
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
1134 mbox->ibox.commit_log_file_seq = seq;
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
1135 mbox->ibox.commit_log_file_offset = offset;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1136 }
2818
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1137 if (mail_index_sync_commit(sync_ctx->sync_ctx) < 0)
2393
d598c82a3c22 Renamed mail_index_sync_end() to _commit() and added _rollback(). Fixed mbox
Timo Sirainen <tss@iki.fi>
parents: 2373
diff changeset
1138 ret = -1;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1139 }
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1140 maildir_keywords_sync_deinit(sync_ctx->keywords_sync_ctx);
3453
f140acbe46b4 Assert/cleanup
Timo Sirainen <tss@iki.fi>
parents: 3447
diff changeset
1141 sync_ctx->keywords_sync_ctx = NULL;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1142
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1143 if (ret == 0) {
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
1144 mbox->ibox.commit_log_file_seq = 0;
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
1145 mbox->ibox.commit_log_file_offset = 0;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1146 } else {
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
1147 mail_storage_set_index_error(&mbox->ibox);
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1148 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1149
2818
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1150 i_free(sync_ctx);
3436
3c51658d6846 We didn't notice if messages were deleted directly from new/.
Timo Sirainen <tss@iki.fi>
parents: 3435
diff changeset
1151 return ret < 0 ? -1 : (full_rescan ? 0 : 1);
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1152 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1153
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1154 static int maildir_sync_context(struct maildir_sync_context *ctx, int forced,
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1155 int sync_last_commit)
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1156 {
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1157 int ret, new_changed, cur_changed;
3436
3c51658d6846 We didn't notice if messages were deleted directly from new/.
Timo Sirainen <tss@iki.fi>
parents: 3435
diff changeset
1158 int full_rescan = FALSE;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1159
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1160 if (sync_last_commit) {
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1161 new_changed = cur_changed = FALSE;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1162 } else if (!forced) {
3472
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
1163 if (maildir_sync_quick_check(ctx->mbox,
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
1164 ctx->new_dir, ctx->cur_dir,
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
1165 &new_changed, &cur_changed) < 0)
2140
e2cd51b99359 "readonly sync" -> "forced sync"
Timo Sirainen <tss@iki.fi>
parents: 2123
diff changeset
1166 return -1;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1167
2140
e2cd51b99359 "readonly sync" -> "forced sync"
Timo Sirainen <tss@iki.fi>
parents: 2123
diff changeset
1168 if (!new_changed && !cur_changed)
e2cd51b99359 "readonly sync" -> "forced sync"
Timo Sirainen <tss@iki.fi>
parents: 2123
diff changeset
1169 return 0;
e2cd51b99359 "readonly sync" -> "forced sync"
Timo Sirainen <tss@iki.fi>
parents: 2123
diff changeset
1170 } else {
e2cd51b99359 "readonly sync" -> "forced sync"
Timo Sirainen <tss@iki.fi>
parents: 2123
diff changeset
1171 new_changed = cur_changed = TRUE;
e2cd51b99359 "readonly sync" -> "forced sync"
Timo Sirainen <tss@iki.fi>
parents: 2123
diff changeset
1172 }
1947
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
1173
2818
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1174 /*
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1175 Locking, locking, locking.. Wasn't maildir supposed to be lockless?
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1176
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1177 We can get here either as beginning a real maildir sync, or when
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1178 committing changes to maildir but a file was lost (maybe renamed).
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1179
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1180 So, we're going to need two locks. One for index and one for
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1181 uidlist. To avoid deadlocking do the index lock first.
2121
0840edf34f37 Locking fixes. use less memory
Timo Sirainen <tss@iki.fi>
parents: 2067
diff changeset
1182
2818
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1183 uidlist is needed only for figuring out UIDs for newly seen files,
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1184 so theoretically we wouldn't need to lock it unless there are new
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1185 files. It has a few problems though, assuming the index lock didn't
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1186 already protect it (eg. in-memory indexes):
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1187
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1188 1. Just because you see a new file which doesn't exist in uidlist
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1189 file, doesn't mean that the file really exists anymore, or that
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1190 your readdir() lists all new files. Meaning that this is possible:
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1191
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1192 A: opendir(), readdir() -> new file ...
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1193 -- new files are written to the maildir --
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1194 B: opendir(), readdir() -> new file, lock uidlist,
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1195 readdir() -> another new file, rewrite uidlist, unlock
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1196 A: ... lock uidlist, readdir() -> nothing left, rewrite uidlist,
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1197 unlock
2173
9438951e243f Don't crash if we're syncing last commit to maildir, but some file was lost
Timo Sirainen <tss@iki.fi>
parents: 2140
diff changeset
1198
2818
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1199 The second time running A didn't see the two new files. To handle
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1200 this correctly, it must not remove the new unseen files from
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1201 uidlist. This is possible to do, but adds extra complexity.
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1202
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1203 2. If another process is rename()ing files while we are
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1204 readdir()ing, it's possible that readdir() never lists some files,
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1205 causing Dovecot to assume they were expunged. In next sync they
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1206 would show up again, but client could have already been notified of
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1207 that and they would show up under new UIDs, so the damage is
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1208 already done.
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1209
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1210 Both of the problems can be avoided if we simply lock the uidlist
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1211 before syncing and keep it until sync is finished. Typically this
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1212 would happen in any case, as there is the index lock..
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1213
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1214 The second case is still a problem with external changes though,
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1215 because maildir doesn't require any kind of locking. Luckily this
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1216 problem rarely happens except under high amount of modifications.
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1217 */
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1218
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
1219 if (!ctx->mbox->syncing_commit) {
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
1220 ctx->index_sync_ctx = maildir_sync_index_begin(ctx->mbox);
2818
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1221 if (ctx->index_sync_ctx == NULL)
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1222 return -1;
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1223 }
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1224
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
1225 if ((ret = maildir_uidlist_lock(ctx->mbox->uidlist)) <= 0) {
2818
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1226 /* failure / timeout. if forced is TRUE, we could still go
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1227 forward and check only for renamed files, but is it worth
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1228 the trouble? .. */
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1229 return ret;
2140
e2cd51b99359 "readonly sync" -> "forced sync"
Timo Sirainen <tss@iki.fi>
parents: 2123
diff changeset
1230 }
2123
e01de478882f locking fixes
Timo Sirainen <tss@iki.fi>
parents: 2121
diff changeset
1231
e01de478882f locking fixes
Timo Sirainen <tss@iki.fi>
parents: 2121
diff changeset
1232 ctx->partial = !cur_changed;
e01de478882f locking fixes
Timo Sirainen <tss@iki.fi>
parents: 2121
diff changeset
1233 ctx->uidlist_sync_ctx =
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
1234 maildir_uidlist_sync_init(ctx->mbox->uidlist, ctx->partial);
2121
0840edf34f37 Locking fixes. use less memory
Timo Sirainen <tss@iki.fi>
parents: 2067
diff changeset
1235
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1236 if (new_changed || cur_changed) {
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1237 /* if we're going to check cur/ dir our current logic requires
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1238 that new/ dir is checked as well. it's a good idea anyway. */
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1239 while ((ret = maildir_scan_dir(ctx, TRUE)) > 0) {
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1240 /* rename()d at least some files, which might have
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1241 caused some other files to be missed. check again
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1242 (see MAILDIR_RENAME_RESCAN_COUNT). */
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1243 }
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1244 if (ret < 0)
1954
2f6e137cdc44 Syncing optimizations.
Timo Sirainen <tss@iki.fi>
parents: 1947
diff changeset
1245 return -1;
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1246
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1247 if (cur_changed) {
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1248 if (maildir_scan_dir(ctx, FALSE) < 0)
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1249 return -1;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1250 }
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1251
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1252 /* finish uidlist syncing, but keep it still locked */
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1253 maildir_uidlist_sync_finish(ctx->uidlist_sync_ctx);
1954
2f6e137cdc44 Syncing optimizations.
Timo Sirainen <tss@iki.fi>
parents: 1947
diff changeset
1254 }
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1255
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
1256 if (!ctx->mbox->syncing_commit) {
3436
3c51658d6846 We didn't notice if messages were deleted directly from new/.
Timo Sirainen <tss@iki.fi>
parents: 3435
diff changeset
1257 ret = maildir_sync_index_finish(ctx->index_sync_ctx,
3c51658d6846 We didn't notice if messages were deleted directly from new/.
Timo Sirainen <tss@iki.fi>
parents: 3435
diff changeset
1258 ctx->partial);
3c51658d6846 We didn't notice if messages were deleted directly from new/.
Timo Sirainen <tss@iki.fi>
parents: 3435
diff changeset
1259 if (ret < 0) {
2818
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1260 ctx->index_sync_ctx = NULL;
2173
9438951e243f Don't crash if we're syncing last commit to maildir, but some file was lost
Timo Sirainen <tss@iki.fi>
parents: 2140
diff changeset
1261 return -1;
2818
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1262 }
3436
3c51658d6846 We didn't notice if messages were deleted directly from new/.
Timo Sirainen <tss@iki.fi>
parents: 3435
diff changeset
1263 if (ret == 0)
3c51658d6846 We didn't notice if messages were deleted directly from new/.
Timo Sirainen <tss@iki.fi>
parents: 3435
diff changeset
1264 full_rescan = TRUE;
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1265
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1266 i_assert(maildir_uidlist_is_locked(ctx->mbox->uidlist));
2818
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
1267 ctx->index_sync_ctx = NULL;
2173
9438951e243f Don't crash if we're syncing last commit to maildir, but some file was lost
Timo Sirainen <tss@iki.fi>
parents: 2140
diff changeset
1268 }
1984
9c159272f721 syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
1269
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1270 ret = maildir_uidlist_sync_deinit(ctx->uidlist_sync_ctx);
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1271 ctx->uidlist_sync_ctx = NULL;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1272
3436
3c51658d6846 We didn't notice if messages were deleted directly from new/.
Timo Sirainen <tss@iki.fi>
parents: 3435
diff changeset
1273 return ret < 0 ? -1 : (full_rescan ? 0 : 1);
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1274 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1275
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
1276 int maildir_storage_sync_force(struct maildir_mailbox *mbox)
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1277 {
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1278 struct maildir_sync_context *ctx;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1279 int ret;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1280
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
1281 ctx = maildir_sync_context_new(mbox);
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1282 ret = maildir_sync_context(ctx, TRUE, FALSE);
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1283 maildir_sync_deinit(ctx);
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1284 return ret < 0 ? -1 : 0;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1285 }
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1286
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1287 int maildir_sync_last_commit(struct maildir_mailbox *mbox)
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1288 {
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1289 struct maildir_sync_context *ctx;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1290 int ret;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1291
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1292 if (mbox->ibox.commit_log_file_seq == 0)
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1293 return 0;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1294
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1295 ctx = maildir_sync_context_new(mbox);
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1296 ret = maildir_sync_context(ctx, FALSE, TRUE);
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1297 maildir_sync_deinit(ctx);
3436
3c51658d6846 We didn't notice if messages were deleted directly from new/.
Timo Sirainen <tss@iki.fi>
parents: 3435
diff changeset
1298 return ret < 0 ? -1 : 0;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1299 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1300
2322
aae574ed7f4c Broke mailbox_sync() into iterator.
Timo Sirainen <tss@iki.fi>
parents: 2320
diff changeset
1301 struct mailbox_sync_context *
aae574ed7f4c Broke mailbox_sync() into iterator.
Timo Sirainen <tss@iki.fi>
parents: 2320
diff changeset
1302 maildir_storage_sync_init(struct mailbox *box, enum mailbox_sync_flags flags)
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1303 {
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
1304 struct maildir_mailbox *mbox = (struct maildir_mailbox *)box;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1305 struct maildir_sync_context *ctx;
2322
aae574ed7f4c Broke mailbox_sync() into iterator.
Timo Sirainen <tss@iki.fi>
parents: 2320
diff changeset
1306 int ret = 0;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1307
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1308 if ((flags & MAILBOX_SYNC_FLAG_FAST) == 0 ||
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
1309 mbox->ibox.sync_last_check + MAILBOX_FULL_SYNC_INTERVAL <=
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
1310 ioloop_time) {
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
1311 mbox->ibox.sync_last_check = ioloop_time;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1312
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
1313 ctx = maildir_sync_context_new(mbox);
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1314 ret = maildir_sync_context(ctx, FALSE, FALSE);
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1315 maildir_sync_deinit(ctx);
3436
3c51658d6846 We didn't notice if messages were deleted directly from new/.
Timo Sirainen <tss@iki.fi>
parents: 3435
diff changeset
1316
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1317 i_assert(!maildir_uidlist_is_locked(mbox->uidlist));
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
1318
3436
3c51658d6846 We didn't notice if messages were deleted directly from new/.
Timo Sirainen <tss@iki.fi>
parents: 3435
diff changeset
1319 if (ret == 0) {
3c51658d6846 We didn't notice if messages were deleted directly from new/.
Timo Sirainen <tss@iki.fi>
parents: 3435
diff changeset
1320 /* lost some files from new/, see if thery're in cur/ */
3c51658d6846 We didn't notice if messages were deleted directly from new/.
Timo Sirainen <tss@iki.fi>
parents: 3435
diff changeset
1321 ret = maildir_storage_sync_force(mbox);
3c51658d6846 We didn't notice if messages were deleted directly from new/.
Timo Sirainen <tss@iki.fi>
parents: 3435
diff changeset
1322 }
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1323 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1324
2322
aae574ed7f4c Broke mailbox_sync() into iterator.
Timo Sirainen <tss@iki.fi>
parents: 2320
diff changeset
1325 return index_mailbox_sync_init(box, flags, ret < 0);
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1326 }
3472
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
1327
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
1328 int maildir_sync_is_synced(struct maildir_mailbox *mbox)
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
1329 {
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
1330 const char *new_dir, *cur_dir;
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
1331 int ret, new_changed, cur_changed;
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
1332
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
1333 t_push();
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
1334 new_dir = t_strconcat(mbox->path, "/new", NULL);
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
1335 cur_dir = t_strconcat(mbox->path, "/cur", NULL);
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
1336
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
1337 ret = maildir_sync_quick_check(mbox, new_dir, cur_dir,
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
1338 &new_changed, &cur_changed);
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
1339 t_pop();
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
1340 return ret < 0 ? -1 : (!new_changed && !cur_changed);
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
1341 }