annotate src/lib-storage/index/maildir/maildir-sync.c @ 6859:64e0f45e8dc3 HEAD

Delayed NFS attribute cache flushing had some bug, so removed it for now.
author Timo Sirainen <tss@iki.fi>
date Mon, 26 Nov 2007 12:15:57 +0200
parents ca849e28a062
children 0c3ab0aef33c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6429
65c69a53a7be Replaced my Copyright notices. The year range always ends with 2007 now.
Timo Sirainen <tss@iki.fi>
parents: 6328
diff changeset
1 /* Copyright (c) 2004-2007 Dovecot authors, see the included COPYING file */
1955
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
3520
e2fe8222449d s/occured/occurred/
Timo Sirainen <tss@iki.fi>
parents: 3472
diff changeset
8 check if changes have occurred is to stat() the new/ and cur/
1955
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
3520
e2fe8222449d s/occured/occurred/
Timo Sirainen <tss@iki.fi>
parents: 3472
diff changeset
15 until a new modification occurred later.
1955
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"
5926
095b3adc537b Handle mailbox deletions while syncing silently.
Timo Sirainen <tss@iki.fi>
parents: 5920
diff changeset
179 #include "nfs-workarounds.h"
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
180 #include "maildir-storage.h"
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
181 #include "maildir-uidlist.h"
5899
f29b93c0519c Moved maildir filename related functions to maildir-filename.c
Timo Sirainen <tss@iki.fi>
parents: 5850
diff changeset
182 #include "maildir-filename.h"
4848
967de900c73a Mailbox list indexing and related changes. Currently works only with
Timo Sirainen <tss@iki.fi>
parents: 4774
diff changeset
183 #include "maildir-sync.h"
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
184
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
185 #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
186 #include <stddef.h>
5588
6c89106dee21 Keyword characters weren't sorted in the maildir filename.
Timo Sirainen <tss@iki.fi>
parents: 5582
diff changeset
187 #include <stdlib.h>
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
188 #include <unistd.h>
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
189 #include <dirent.h>
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
190 #include <sys/stat.h>
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
191
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
192 #define MAILDIR_FILENAME_FLAG_FOUND 128
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
193
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
194 /* 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
195 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
196 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
197 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
198 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
199 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
200 #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
201
5397
3a0964ac3a5c comment/duplicate link handling fixes.
Timo Sirainen <tss@iki.fi>
parents: 5392
diff changeset
202 /* This is mostly to avoid infinite looping when rename() destination already
3a0964ac3a5c comment/duplicate link handling fixes.
Timo Sirainen <tss@iki.fi>
parents: 5392
diff changeset
203 exists as the hard link of the file itself. */
5391
0c8705aad54c Avoid infinite looping when buggy filesystems.
Timo Sirainen <tss@iki.fi>
parents: 5390
diff changeset
204 #define MAILDIR_SCAN_DIR_MAX_COUNT 5
0c8705aad54c Avoid infinite looping when buggy filesystems.
Timo Sirainen <tss@iki.fi>
parents: 5390
diff changeset
205
5443
293bc7d1062f If we find duplicate hard links which haven't changed for 30 secs, unlink()
Timo Sirainen <tss@iki.fi>
parents: 5397
diff changeset
206 #define DUPE_LINKS_DELETE_SECS 30
293bc7d1062f If we find duplicate hard links which haven't changed for 30 secs, unlink()
Timo Sirainen <tss@iki.fi>
parents: 5397
diff changeset
207
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
208 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
209 struct maildir_mailbox *mbox;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
210 const char *new_dir, *cur_dir;
3863
55df57c028d4 Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents: 3821
diff changeset
211 bool partial;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
212
5368
7d45edb81fe4 When copying/syncing a lot of mails, send "* OK Hang in there" replies to
Timo Sirainen <tss@iki.fi>
parents: 5307
diff changeset
213 time_t last_touch, last_notify;
7d45edb81fe4 When copying/syncing a lot of mails, send "* OK Hang in there" replies to
Timo Sirainen <tss@iki.fi>
parents: 5307
diff changeset
214
2818
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
215 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
216 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
217 };
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
218
5920
00c5e3cbeaf0 Moved index syncing code to its own file.
Timo Sirainen <tss@iki.fi>
parents: 5918
diff changeset
219 void maildir_sync_notify(struct maildir_sync_context *ctx)
5368
7d45edb81fe4 When copying/syncing a lot of mails, send "* OK Hang in there" replies to
Timo Sirainen <tss@iki.fi>
parents: 5307
diff changeset
220 {
7d45edb81fe4 When copying/syncing a lot of mails, send "* OK Hang in there" replies to
Timo Sirainen <tss@iki.fi>
parents: 5307
diff changeset
221 time_t now;
7d45edb81fe4 When copying/syncing a lot of mails, send "* OK Hang in there" replies to
Timo Sirainen <tss@iki.fi>
parents: 5307
diff changeset
222
5390
4eeec560df01 If saving to maildir causes flag/expunge syncs, don't crash.
Timo Sirainen <tss@iki.fi>
parents: 5388
diff changeset
223 if (ctx == NULL) {
4eeec560df01 If saving to maildir causes flag/expunge syncs, don't crash.
Timo Sirainen <tss@iki.fi>
parents: 5388
diff changeset
224 /* we got here from maildir-save.c. it has no
4eeec560df01 If saving to maildir causes flag/expunge syncs, don't crash.
Timo Sirainen <tss@iki.fi>
parents: 5388
diff changeset
225 maildir_sync_context, */
4eeec560df01 If saving to maildir causes flag/expunge syncs, don't crash.
Timo Sirainen <tss@iki.fi>
parents: 5388
diff changeset
226 return;
4eeec560df01 If saving to maildir causes flag/expunge syncs, don't crash.
Timo Sirainen <tss@iki.fi>
parents: 5388
diff changeset
227 }
4eeec560df01 If saving to maildir causes flag/expunge syncs, don't crash.
Timo Sirainen <tss@iki.fi>
parents: 5388
diff changeset
228
5368
7d45edb81fe4 When copying/syncing a lot of mails, send "* OK Hang in there" replies to
Timo Sirainen <tss@iki.fi>
parents: 5307
diff changeset
229 now = time(NULL);
7d45edb81fe4 When copying/syncing a lot of mails, send "* OK Hang in there" replies to
Timo Sirainen <tss@iki.fi>
parents: 5307
diff changeset
230 if (now - ctx->last_touch > MAILDIR_LOCK_TOUCH_SECS) {
7d45edb81fe4 When copying/syncing a lot of mails, send "* OK Hang in there" replies to
Timo Sirainen <tss@iki.fi>
parents: 5307
diff changeset
231 (void)maildir_uidlist_lock_touch(ctx->mbox->uidlist);
7d45edb81fe4 When copying/syncing a lot of mails, send "* OK Hang in there" replies to
Timo Sirainen <tss@iki.fi>
parents: 5307
diff changeset
232 ctx->last_touch = now;
7d45edb81fe4 When copying/syncing a lot of mails, send "* OK Hang in there" replies to
Timo Sirainen <tss@iki.fi>
parents: 5307
diff changeset
233 }
7d45edb81fe4 When copying/syncing a lot of mails, send "* OK Hang in there" replies to
Timo Sirainen <tss@iki.fi>
parents: 5307
diff changeset
234 if (now - ctx->last_notify > MAIL_STORAGE_STAYALIVE_SECS) {
7d45edb81fe4 When copying/syncing a lot of mails, send "* OK Hang in there" replies to
Timo Sirainen <tss@iki.fi>
parents: 5307
diff changeset
235 struct mailbox *box = &ctx->mbox->ibox.box;
7d45edb81fe4 When copying/syncing a lot of mails, send "* OK Hang in there" replies to
Timo Sirainen <tss@iki.fi>
parents: 5307
diff changeset
236
7d45edb81fe4 When copying/syncing a lot of mails, send "* OK Hang in there" replies to
Timo Sirainen <tss@iki.fi>
parents: 5307
diff changeset
237 if (box->storage->callbacks->notify_ok != NULL) {
7d45edb81fe4 When copying/syncing a lot of mails, send "* OK Hang in there" replies to
Timo Sirainen <tss@iki.fi>
parents: 5307
diff changeset
238 box->storage->callbacks->
7d45edb81fe4 When copying/syncing a lot of mails, send "* OK Hang in there" replies to
Timo Sirainen <tss@iki.fi>
parents: 5307
diff changeset
239 notify_ok(box, "Hang in there..",
7d45edb81fe4 When copying/syncing a lot of mails, send "* OK Hang in there" replies to
Timo Sirainen <tss@iki.fi>
parents: 5307
diff changeset
240 box->storage->callback_context);
7d45edb81fe4 When copying/syncing a lot of mails, send "* OK Hang in there" replies to
Timo Sirainen <tss@iki.fi>
parents: 5307
diff changeset
241 }
7d45edb81fe4 When copying/syncing a lot of mails, send "* OK Hang in there" replies to
Timo Sirainen <tss@iki.fi>
parents: 5307
diff changeset
242 ctx->last_notify = now;
7d45edb81fe4 When copying/syncing a lot of mails, send "* OK Hang in there" replies to
Timo Sirainen <tss@iki.fi>
parents: 5307
diff changeset
243 }
7d45edb81fe4 When copying/syncing a lot of mails, send "* OK Hang in there" replies to
Timo Sirainen <tss@iki.fi>
parents: 5307
diff changeset
244 }
7d45edb81fe4 When copying/syncing a lot of mails, send "* OK Hang in there" replies to
Timo Sirainen <tss@iki.fi>
parents: 5307
diff changeset
245
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
246 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
247 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
248 {
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
249 struct maildir_sync_context *ctx;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
250
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
251 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
252 ctx->mbox = mbox;
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
253 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
254 ctx->cur_dir = t_strconcat(mbox->path, "/cur", NULL);
5368
7d45edb81fe4 When copying/syncing a lot of mails, send "* OK Hang in there" replies to
Timo Sirainen <tss@iki.fi>
parents: 5307
diff changeset
255 ctx->last_touch = ioloop_time;
7d45edb81fe4 When copying/syncing a lot of mails, send "* OK Hang in there" replies to
Timo Sirainen <tss@iki.fi>
parents: 5307
diff changeset
256 ctx->last_notify = ioloop_time;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
257 return ctx;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
258 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
259
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
260 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
261 {
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
262 if (ctx->uidlist_sync_ctx != NULL)
4238
3c8b191b0019 Adding mail to index while saving it had a race condition. Fixing it
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4199
diff changeset
263 (void)maildir_uidlist_sync_deinit(&ctx->uidlist_sync_ctx);
4774
615b7738a62f Saving mails could have skipped over transactions, which caused different
Timo Sirainen <tss@iki.fi>
parents: 4612
diff changeset
264 if (ctx->index_sync_ctx != NULL) {
615b7738a62f Saving mails could have skipped over transactions, which caused different
Timo Sirainen <tss@iki.fi>
parents: 4612
diff changeset
265 (void)maildir_sync_index_finish(&ctx->index_sync_ctx,
615b7738a62f Saving mails could have skipped over transactions, which caused different
Timo Sirainen <tss@iki.fi>
parents: 4612
diff changeset
266 TRUE, FALSE);
615b7738a62f Saving mails could have skipped over transactions, which caused different
Timo Sirainen <tss@iki.fi>
parents: 4612
diff changeset
267 }
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
268 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
269
4397
5cbabd4ccd9c Don't go fixing duplicate maildir filenames without properly checking that
Timo Sirainen <tss@iki.fi>
parents: 4238
diff changeset
270 static int maildir_fix_duplicate(struct maildir_sync_context *ctx,
5910
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
271 const char *dir, const char *fname2)
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
272 {
5910
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
273 const char *fname1, *path1, *path2;
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
274 const char *new_fname, *new_path;
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
275 struct stat st1, st2;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
276 int ret = 0;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
277
5910
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
278 fname1 = maildir_uidlist_sync_get_full_filename(ctx->uidlist_sync_ctx,
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
279 fname2);
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
280 i_assert(fname1 != NULL);
4397
5cbabd4ccd9c Don't go fixing duplicate maildir filenames without properly checking that
Timo Sirainen <tss@iki.fi>
parents: 4238
diff changeset
281
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
282 t_push();
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
283
5910
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
284 path1 = t_strconcat(dir, "/", fname1, NULL);
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
285 path2 = t_strconcat(dir, "/", fname2, NULL);
4397
5cbabd4ccd9c Don't go fixing duplicate maildir filenames without properly checking that
Timo Sirainen <tss@iki.fi>
parents: 4238
diff changeset
286
5910
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
287 if (stat(path1, &st1) < 0 || stat(path2, &st2) < 0) {
4397
5cbabd4ccd9c Don't go fixing duplicate maildir filenames without properly checking that
Timo Sirainen <tss@iki.fi>
parents: 4238
diff changeset
288 /* most likely the files just don't exist anymore.
5cbabd4ccd9c Don't go fixing duplicate maildir filenames without properly checking that
Timo Sirainen <tss@iki.fi>
parents: 4238
diff changeset
289 don't really care about other errors much. */
5cbabd4ccd9c Don't go fixing duplicate maildir filenames without properly checking that
Timo Sirainen <tss@iki.fi>
parents: 4238
diff changeset
290 t_pop();
5cbabd4ccd9c Don't go fixing duplicate maildir filenames without properly checking that
Timo Sirainen <tss@iki.fi>
parents: 4238
diff changeset
291 return 0;
5cbabd4ccd9c Don't go fixing duplicate maildir filenames without properly checking that
Timo Sirainen <tss@iki.fi>
parents: 4238
diff changeset
292 }
5910
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
293 if (st1.st_ino == st2.st_ino &&
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
294 CMP_DEV_T(st1.st_dev, st2.st_dev)) {
5397
3a0964ac3a5c comment/duplicate link handling fixes.
Timo Sirainen <tss@iki.fi>
parents: 5392
diff changeset
295 /* Files are the same. this means either a race condition
5443
293bc7d1062f If we find duplicate hard links which haven't changed for 30 secs, unlink()
Timo Sirainen <tss@iki.fi>
parents: 5397
diff changeset
296 between stat() calls, or that the files were link()ed. */
5910
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
297 if (st1.st_nlink > 1 && st2.st_nlink == st1.st_nlink &&
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
298 st1.st_ctime == st2.st_ctime &&
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
299 st1.st_ctime < ioloop_time - DUPE_LINKS_DELETE_SECS) {
5443
293bc7d1062f If we find duplicate hard links which haven't changed for 30 secs, unlink()
Timo Sirainen <tss@iki.fi>
parents: 5397
diff changeset
300 /* The file has hard links and it hasn't had any
293bc7d1062f If we find duplicate hard links which haven't changed for 30 secs, unlink()
Timo Sirainen <tss@iki.fi>
parents: 5397
diff changeset
301 changes (such as renames) for a while, so this
293bc7d1062f If we find duplicate hard links which haven't changed for 30 secs, unlink()
Timo Sirainen <tss@iki.fi>
parents: 5397
diff changeset
302 isn't a race condition.
5392
9a8402768b42 If duplicate links are found, rename() one over the other to get rid of
Timo Sirainen <tss@iki.fi>
parents: 5391
diff changeset
303
5443
293bc7d1062f If we find duplicate hard links which haven't changed for 30 secs, unlink()
Timo Sirainen <tss@iki.fi>
parents: 5397
diff changeset
304 rename()ing one file on top of the other would fix
293bc7d1062f If we find duplicate hard links which haven't changed for 30 secs, unlink()
Timo Sirainen <tss@iki.fi>
parents: 5397
diff changeset
305 this safely, except POSIX decided that rename()
293bc7d1062f If we find duplicate hard links which haven't changed for 30 secs, unlink()
Timo Sirainen <tss@iki.fi>
parents: 5397
diff changeset
306 doesn't work that way. So we'll have unlink() one
293bc7d1062f If we find duplicate hard links which haven't changed for 30 secs, unlink()
Timo Sirainen <tss@iki.fi>
parents: 5397
diff changeset
307 and hope that another process didn't just decide to
293bc7d1062f If we find duplicate hard links which haven't changed for 30 secs, unlink()
Timo Sirainen <tss@iki.fi>
parents: 5397
diff changeset
308 unlink() the other (uidlist lock prevents this from
293bc7d1062f If we find duplicate hard links which haven't changed for 30 secs, unlink()
Timo Sirainen <tss@iki.fi>
parents: 5397
diff changeset
309 happening) */
5910
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
310 if (unlink(path2) == 0)
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
311 i_warning("Unlinked a duplicate: %s", path2);
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
312 else {
5443
293bc7d1062f If we find duplicate hard links which haven't changed for 30 secs, unlink()
Timo Sirainen <tss@iki.fi>
parents: 5397
diff changeset
313 mail_storage_set_critical(
5910
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
314 &ctx->mbox->storage->storage,
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
315 "unlink(%s) failed: %m", path2);
5443
293bc7d1062f If we find duplicate hard links which haven't changed for 30 secs, unlink()
Timo Sirainen <tss@iki.fi>
parents: 5397
diff changeset
316 }
293bc7d1062f If we find duplicate hard links which haven't changed for 30 secs, unlink()
Timo Sirainen <tss@iki.fi>
parents: 5397
diff changeset
317 }
4397
5cbabd4ccd9c Don't go fixing duplicate maildir filenames without properly checking that
Timo Sirainen <tss@iki.fi>
parents: 4238
diff changeset
318 t_pop();
5cbabd4ccd9c Don't go fixing duplicate maildir filenames without properly checking that
Timo Sirainen <tss@iki.fi>
parents: 4238
diff changeset
319 return 0;
5cbabd4ccd9c Don't go fixing duplicate maildir filenames without properly checking that
Timo Sirainen <tss@iki.fi>
parents: 4238
diff changeset
320 }
5cbabd4ccd9c Don't go fixing duplicate maildir filenames without properly checking that
Timo Sirainen <tss@iki.fi>
parents: 4238
diff changeset
321
5904
62ceb6b2b20d Renamed maildir_generate_tmp_filename() to maildir_filename_generate(). Also
Timo Sirainen <tss@iki.fi>
parents: 5901
diff changeset
322 new_fname = maildir_filename_generate();
5910
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
323 new_path = t_strconcat(ctx->mbox->path, "/new/", new_fname, NULL);
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
324
5910
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
325 if (rename(path2, new_path) == 0)
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
326 i_warning("Fixed a duplicate: %s -> %s", path2, new_fname);
5392
9a8402768b42 If duplicate links are found, rename() one over the other to get rid of
Timo Sirainen <tss@iki.fi>
parents: 5391
diff changeset
327 else if (errno != ENOENT) {
5910
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
328 mail_storage_set_critical(&ctx->mbox->storage->storage,
5582
1c4fd25893bd Updated error message.
Timo Sirainen <tss@iki.fi>
parents: 5564
diff changeset
329 "Couldn't fix a duplicate: rename(%s, %s) failed: %m",
5910
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
330 path2, new_path);
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
331 ret = -1;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
332 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
333 t_pop();
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
334
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
335 return ret;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
336 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
337
5926
095b3adc537b Handle mailbox deletions while syncing silently.
Timo Sirainen <tss@iki.fi>
parents: 5920
diff changeset
338 static int
095b3adc537b Handle mailbox deletions while syncing silently.
Timo Sirainen <tss@iki.fi>
parents: 5920
diff changeset
339 maildir_stat(struct maildir_mailbox *mbox, const char *path, struct stat *st_r)
095b3adc537b Handle mailbox deletions while syncing silently.
Timo Sirainen <tss@iki.fi>
parents: 5920
diff changeset
340 {
6777
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
341 int i;
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
342
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
343 for (i = 0;; i++) {
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
344 if (nfs_safe_stat(path, st_r) == 0)
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
345 return 0;
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
346 if (errno != ENOENT || i == MAILDIR_DELETE_RETRY_COUNT)
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
347 break;
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
348
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
349 if (maildir_set_deleted(mbox))
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
350 return -1;
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
351 /* try again */
5926
095b3adc537b Handle mailbox deletions while syncing silently.
Timo Sirainen <tss@iki.fi>
parents: 5920
diff changeset
352 }
095b3adc537b Handle mailbox deletions while syncing silently.
Timo Sirainen <tss@iki.fi>
parents: 5920
diff changeset
353
095b3adc537b Handle mailbox deletions while syncing silently.
Timo Sirainen <tss@iki.fi>
parents: 5920
diff changeset
354 mail_storage_set_critical(mbox->ibox.box.storage,
095b3adc537b Handle mailbox deletions while syncing silently.
Timo Sirainen <tss@iki.fi>
parents: 5920
diff changeset
355 "stat(%s) failed: %m", path);
095b3adc537b Handle mailbox deletions while syncing silently.
Timo Sirainen <tss@iki.fi>
parents: 5920
diff changeset
356 return -1;
095b3adc537b Handle mailbox deletions while syncing silently.
Timo Sirainen <tss@iki.fi>
parents: 5920
diff changeset
357 }
095b3adc537b Handle mailbox deletions while syncing silently.
Timo Sirainen <tss@iki.fi>
parents: 5920
diff changeset
358
3863
55df57c028d4 Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents: 3821
diff changeset
359 static int maildir_scan_dir(struct maildir_sync_context *ctx, bool new_dir)
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
360 {
5459
78eaf595359c Removed struct index_storage abstraction. It's pointless.
Timo Sirainen <tss@iki.fi>
parents: 5443
diff changeset
361 struct mail_storage *storage = &ctx->mbox->storage->storage;
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
362 const char *path;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
363 DIR *dirp;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
364 string_t *src, *dest;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
365 struct dirent *dp;
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
366 struct stat st;
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
367 enum maildir_uidlist_rec_flag flags;
5914
ae731dbf3a6f Sync index changes while iterating through uidlist entries. This avoids
Timo Sirainen <tss@iki.fi>
parents: 5912
diff changeset
368 unsigned int i = 0, move_count = 0;
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
369 time_t now;
3863
55df57c028d4 Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents: 3821
diff changeset
370 int ret = 1;
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
371 bool move_new, check_touch, dir_changed = FALSE;
5214
4e9d345df846 When syncing huge maildirs check once in a while if we need to update
Timo Sirainen <tss@iki.fi>
parents: 5080
diff changeset
372
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
373 path = new_dir ? ctx->new_dir : ctx->cur_dir;
6777
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
374 for (i = 0;; i++) {
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
375 dirp = opendir(path);
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
376 if (dirp != NULL)
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
377 break;
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
378
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
379 if (errno != ENOENT || i == MAILDIR_DELETE_RETRY_COUNT) {
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
380 mail_storage_set_critical(storage,
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
381 "opendir(%s) failed: %m", path);
5926
095b3adc537b Handle mailbox deletions while syncing silently.
Timo Sirainen <tss@iki.fi>
parents: 5920
diff changeset
382 return -1;
095b3adc537b Handle mailbox deletions while syncing silently.
Timo Sirainen <tss@iki.fi>
parents: 5920
diff changeset
383 }
6777
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
384
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
385 if (maildir_set_deleted(ctx->mbox))
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
386 return -1;
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
387 /* try again */
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
388 }
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
389
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
390 #ifdef HAVE_DIRFD
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
391 if (fstat(dirfd(dirp), &st) < 0) {
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
392 mail_storage_set_critical(storage,
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
393 "fstat(%s) failed: %m", path);
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
394 (void)closedir(dirp);
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
395 return -1;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
396 }
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
397 #else
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
398 if (maildir_stat(ctx->mbox, path, &st) < 0) {
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
399 (void)closedir(dirp);
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
400 return -1;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
401 }
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
402 #endif
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
403
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
404 now = time(NULL);
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
405 if (new_dir) {
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
406 ctx->mbox->maildir_hdr.new_check_time = now;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
407 ctx->mbox->maildir_hdr.new_mtime = st.st_mtime;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
408 #ifdef HAVE_STAT_TV_NSEC
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
409 ctx->mbox->maildir_hdr.new_mtime_nsecs = st.st_mtim.tv_nsec;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
410 #else
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
411 ctx->mbox->maildir_hdr.new_mtime_nsecs = 0;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
412 #endif
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
413 } else {
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
414 ctx->mbox->maildir_hdr.cur_check_time = now;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
415 ctx->mbox->maildir_hdr.cur_mtime = st.st_mtime;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
416 #ifdef HAVE_STAT_TV_NSEC
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
417 ctx->mbox->maildir_hdr.cur_mtime_nsecs = st.st_mtim.tv_nsec;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
418 #else
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
419 ctx->mbox->maildir_hdr.cur_mtime_nsecs = 0;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
420 #endif
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
421 }
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
422
2121
0840edf34f37 Locking fixes. use less memory
Timo Sirainen <tss@iki.fi>
parents: 2067
diff changeset
423 t_push();
0840edf34f37 Locking fixes. use less memory
Timo Sirainen <tss@iki.fi>
parents: 2067
diff changeset
424 src = t_str_new(1024);
0840edf34f37 Locking fixes. use less memory
Timo Sirainen <tss@iki.fi>
parents: 2067
diff changeset
425 dest = t_str_new(1024);
0840edf34f37 Locking fixes. use less memory
Timo Sirainen <tss@iki.fi>
parents: 2067
diff changeset
426
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
427 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
428 !ctx->mbox->ibox.keep_recent;
5911
8bb5c7a69405 If readdir() or closedir() fails, return failure.
Timo Sirainen <tss@iki.fi>
parents: 5910
diff changeset
429
8bb5c7a69405 If readdir() or closedir() fails, return failure.
Timo Sirainen <tss@iki.fi>
parents: 5910
diff changeset
430 errno = 0;
8bb5c7a69405 If readdir() or closedir() fails, return failure.
Timo Sirainen <tss@iki.fi>
parents: 5910
diff changeset
431 for (; (dp = readdir(dirp)) != NULL; errno = 0) {
2258
087a43e29492 No maildir filename checking after all.
Timo Sirainen <tss@iki.fi>
parents: 2256
diff changeset
432 if (dp->d_name[0] == '.')
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
433 continue;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
434
1978
6303ef092c5b mbox code compiles again, but syncing is only partially implemented so
Timo Sirainen <tss@iki.fi>
parents: 1970
diff changeset
435 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
436 dp->d_name);
1984
9c159272f721 syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
437 if (ret == 0) {
2038
df504dad3aec Recent flag fixes. Should work perfectly now with maildir.
Timo Sirainen <tss@iki.fi>
parents: 2037
diff changeset
438 /* 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
439 later in next sync. */
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
440 dir_changed = TRUE;
1978
6303ef092c5b mbox code compiles again, but syncing is only partially implemented so
Timo Sirainen <tss@iki.fi>
parents: 1970
diff changeset
441 continue;
1984
9c159272f721 syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
442 }
1978
6303ef092c5b mbox code compiles again, but syncing is only partially implemented so
Timo Sirainen <tss@iki.fi>
parents: 1970
diff changeset
443 if (ret < 0)
6303ef092c5b mbox code compiles again, but syncing is only partially implemented so
Timo Sirainen <tss@iki.fi>
parents: 1970
diff changeset
444 break;
6303ef092c5b mbox code compiles again, but syncing is only partially implemented so
Timo Sirainen <tss@iki.fi>
parents: 1970
diff changeset
445
5214
4e9d345df846 When syncing huge maildirs check once in a while if we need to update
Timo Sirainen <tss@iki.fi>
parents: 5080
diff changeset
446 check_touch = FALSE;
1947
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
447 flags = 0;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
448 if (move_new) {
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
449 str_truncate(src, 0);
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
450 str_truncate(dest, 0);
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
451 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
452 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
453 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
454 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
455 }
1947
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
456 if (rename(str_c(src), str_c(dest)) == 0) {
1984
9c159272f721 syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
457 /* we moved it - it's \Recent for us */
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
458 dir_changed = TRUE;
5914
ae731dbf3a6f Sync index changes while iterating through uidlist entries. This avoids
Timo Sirainen <tss@iki.fi>
parents: 5912
diff changeset
459 move_count++;
1947
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
460 flags |= MAILDIR_UIDLIST_REC_FLAG_MOVED |
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
461 MAILDIR_UIDLIST_REC_FLAG_RECENT;
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
462 } else if (ENOTFOUND(errno)) {
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
463 /* someone else moved it already */
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
464 dir_changed = TRUE;
5914
ae731dbf3a6f Sync index changes while iterating through uidlist entries. This avoids
Timo Sirainen <tss@iki.fi>
parents: 5912
diff changeset
465 move_count++;
6037
d911d943438e Recent flag handling rewrite. Still not perfect with maildir.
Timo Sirainen <tss@iki.fi>
parents: 6027
diff changeset
466 flags |= MAILDIR_UIDLIST_REC_FLAG_MOVED |
d911d943438e Recent flag handling rewrite. Still not perfect with maildir.
Timo Sirainen <tss@iki.fi>
parents: 6027
diff changeset
467 MAILDIR_UIDLIST_REC_FLAG_RECENT;
4199
fa135b615b01 If maildir is readonly, don't complain about rename() failing to move mails
Timo Sirainen <tss@iki.fi>
parents: 4152
diff changeset
468 } else if (ENOSPACE(errno) || errno == EACCES) {
fa135b615b01 If maildir is readonly, don't complain about rename() failing to move mails
Timo Sirainen <tss@iki.fi>
parents: 4152
diff changeset
469 /* not enough disk space / read-only maildir,
fa135b615b01 If maildir is readonly, don't complain about rename() failing to move mails
Timo Sirainen <tss@iki.fi>
parents: 4152
diff changeset
470 leave here */
6037
d911d943438e Recent flag handling rewrite. Still not perfect with maildir.
Timo Sirainen <tss@iki.fi>
parents: 6027
diff changeset
471 flags |= MAILDIR_UIDLIST_REC_FLAG_NEW_DIR;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
472 move_new = FALSE;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
473 } else {
6037
d911d943438e Recent flag handling rewrite. Still not perfect with maildir.
Timo Sirainen <tss@iki.fi>
parents: 6027
diff changeset
474 flags |= MAILDIR_UIDLIST_REC_FLAG_NEW_DIR;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
475 mail_storage_set_critical(storage,
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
476 "rename(%s, %s) failed: %m",
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
477 str_c(src), str_c(dest));
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
478 }
5914
ae731dbf3a6f Sync index changes while iterating through uidlist entries. This avoids
Timo Sirainen <tss@iki.fi>
parents: 5912
diff changeset
479 if ((move_count % MAILDIR_SLOW_MOVE_COUNT) == 0)
ae731dbf3a6f Sync index changes while iterating through uidlist entries. This avoids
Timo Sirainen <tss@iki.fi>
parents: 5912
diff changeset
480 maildir_sync_notify(ctx);
1947
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
481 } else if (new_dir) {
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
482 flags |= MAILDIR_UIDLIST_REC_FLAG_NEW_DIR |
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
483 MAILDIR_UIDLIST_REC_FLAG_RECENT;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
484 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
485
5914
ae731dbf3a6f Sync index changes while iterating through uidlist entries. This avoids
Timo Sirainen <tss@iki.fi>
parents: 5912
diff changeset
486 i++;
ae731dbf3a6f Sync index changes while iterating through uidlist entries. This avoids
Timo Sirainen <tss@iki.fi>
parents: 5912
diff changeset
487 if ((i % MAILDIR_SLOW_CHECK_COUNT) == 0)
ae731dbf3a6f Sync index changes while iterating through uidlist entries. This avoids
Timo Sirainen <tss@iki.fi>
parents: 5912
diff changeset
488 maildir_sync_notify(ctx);
5214
4e9d345df846 When syncing huge maildirs check once in a while if we need to update
Timo Sirainen <tss@iki.fi>
parents: 5080
diff changeset
489
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
490 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
491 dp->d_name, flags);
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
492 if (ret <= 0) {
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
493 if (ret < 0)
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
494 break;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
495
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
496 /* possibly duplicate - try fixing it */
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
497 if (maildir_fix_duplicate(ctx, path, dp->d_name) < 0) {
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
498 ret = -1;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
499 break;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
500 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
501 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
502 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
503
5911
8bb5c7a69405 If readdir() or closedir() fails, return failure.
Timo Sirainen <tss@iki.fi>
parents: 5910
diff changeset
504 if (errno != 0) {
8bb5c7a69405 If readdir() or closedir() fails, return failure.
Timo Sirainen <tss@iki.fi>
parents: 5910
diff changeset
505 mail_storage_set_critical(storage,
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
506 "readdir(%s) failed: %m", path);
5911
8bb5c7a69405 If readdir() or closedir() fails, return failure.
Timo Sirainen <tss@iki.fi>
parents: 5910
diff changeset
507 ret = -1;
8bb5c7a69405 If readdir() or closedir() fails, return failure.
Timo Sirainen <tss@iki.fi>
parents: 5910
diff changeset
508 }
8bb5c7a69405 If readdir() or closedir() fails, return failure.
Timo Sirainen <tss@iki.fi>
parents: 5910
diff changeset
509
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
510 if (closedir(dirp) < 0) {
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
511 mail_storage_set_critical(storage,
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
512 "closedir(%s) failed: %m", path);
5911
8bb5c7a69405 If readdir() or closedir() fails, return failure.
Timo Sirainen <tss@iki.fi>
parents: 5910
diff changeset
513 ret = -1;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
514 }
2121
0840edf34f37 Locking fixes. use less memory
Timo Sirainen <tss@iki.fi>
parents: 2067
diff changeset
515
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
516 if (dir_changed) {
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
517 if (new_dir)
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
518 ctx->mbox->maildir_hdr.new_mtime = now;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
519 else
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
520 ctx->mbox->maildir_hdr.cur_mtime = now;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
521 }
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
522
2121
0840edf34f37 Locking fixes. use less memory
Timo Sirainen <tss@iki.fi>
parents: 2067
diff changeset
523 t_pop();
5368
7d45edb81fe4 When copying/syncing a lot of mails, send "* OK Hang in there" replies to
Timo Sirainen <tss@iki.fi>
parents: 5307
diff changeset
524 return ret < 0 ? -1 :
5914
ae731dbf3a6f Sync index changes while iterating through uidlist entries. This avoids
Timo Sirainen <tss@iki.fi>
parents: 5912
diff changeset
525 (move_count <= MAILDIR_RENAME_RESCAN_COUNT ? 0 : 1);
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
526 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
527
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
528 static int maildir_header_refresh(struct maildir_mailbox *mbox)
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
529 {
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
530 const void *data;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
531 size_t data_size;
4848
967de900c73a Mailbox list indexing and related changes. Currently works only with
Timo Sirainen <tss@iki.fi>
parents: 4774
diff changeset
532
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
533 if (mail_index_refresh(mbox->ibox.index) < 0) {
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
534 mail_storage_set_index_error(&mbox->ibox);
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
535 return -1;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
536 }
3472
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
537
6275
913b188f4dd4 Removed explicit locking from views and maps. They were already locked all
Timo Sirainen <tss@iki.fi>
parents: 6037
diff changeset
538 mail_index_get_header_ext(mbox->ibox.view, mbox->maildir_ext_id,
913b188f4dd4 Removed explicit locking from views and maps. They were already locked all
Timo Sirainen <tss@iki.fi>
parents: 6037
diff changeset
539 &data, &data_size);
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
540 if (data_size == 0) {
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
541 /* doesn't exist */
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
542 return 0;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
543 }
4848
967de900c73a Mailbox list indexing and related changes. Currently works only with
Timo Sirainen <tss@iki.fi>
parents: 4774
diff changeset
544
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
545 if (data_size != sizeof(mbox->maildir_hdr))
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
546 i_warning("Maildir %s: Invalid header record size", mbox->path);
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
547 else
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
548 memcpy(&mbox->maildir_hdr, data, sizeof(mbox->maildir_hdr));
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
549 return 0;
3472
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
550 }
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
551
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
552 static int maildir_sync_quick_check(struct maildir_mailbox *mbox,
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
553 const char *new_dir, const char *cur_dir,
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
554 bool *new_changed_r, bool *cur_changed_r)
3472
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
555 {
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
556 #ifdef HAVE_STAT_TV_NSEC
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
557 # define DIR_NSECS_CHANGED(st, hdr, name) \
5983
272796cfee66 compiler warning fixes
Timo Sirainen <tss@iki.fi>
parents: 5950
diff changeset
558 ((unsigned int)(st).st_mtim.tv_nsec != (hdr)->name ## _mtime_nsecs)
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
559 #else
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
560 # define DIR_NSECS_CHANGED(st, hdr, name) 0
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
561 #endif
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
562
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
563 #define DIR_DELAYED_REFRESH(hdr, name) \
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
564 ((hdr)->name ## _check_time <= \
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
565 (hdr)->name ## _mtime + MAILDIR_SYNC_SECS && \
5983
272796cfee66 compiler warning fixes
Timo Sirainen <tss@iki.fi>
parents: 5950
diff changeset
566 (time_t)(hdr)->name ## _check_time < ioloop_time - MAILDIR_SYNC_SECS)
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
567
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
568 #define DIR_MTIME_CHANGED(st, hdr, name) \
5983
272796cfee66 compiler warning fixes
Timo Sirainen <tss@iki.fi>
parents: 5950
diff changeset
569 ((st).st_mtime != (time_t)(hdr)->name ## _mtime || \
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
570 DIR_NSECS_CHANGED(st, hdr, name))
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
571
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
572 struct maildir_index_header *hdr = &mbox->maildir_hdr;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
573 struct stat new_st, cur_st;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
574 bool refreshed = FALSE, check_new = FALSE, check_cur = FALSE;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
575
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
576 if (mbox->maildir_hdr.new_mtime == 0) {
6328
29d249982282 Error handling fixes.
Timo Sirainen <tss@iki.fi>
parents: 6275
diff changeset
577 if (maildir_header_refresh(mbox) < 0)
29d249982282 Error handling fixes.
Timo Sirainen <tss@iki.fi>
parents: 6275
diff changeset
578 return -1;
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
579 if (mbox->maildir_hdr.new_mtime == 0) {
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
580 /* first sync */
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
581 *new_changed_r = *cur_changed_r = TRUE;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
582 return 0;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
583 }
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
584 }
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
585
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
586 *new_changed_r = *cur_changed_r = FALSE;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
587
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
588 /* try to avoid stat()ing by first checking delayed changes */
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
589 if (DIR_DELAYED_REFRESH(hdr, new) ||
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
590 DIR_DELAYED_REFRESH(hdr, cur)) {
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
591 /* refresh index and try again */
6328
29d249982282 Error handling fixes.
Timo Sirainen <tss@iki.fi>
parents: 6275
diff changeset
592 if (maildir_header_refresh(mbox) < 0)
29d249982282 Error handling fixes.
Timo Sirainen <tss@iki.fi>
parents: 6275
diff changeset
593 return -1;
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
594 refreshed = TRUE;
3472
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
595
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
596 if (DIR_DELAYED_REFRESH(hdr, new))
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
597 *new_changed_r = TRUE;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
598 if (DIR_DELAYED_REFRESH(hdr, cur))
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
599 *cur_changed_r = TRUE;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
600 if (*new_changed_r && *cur_changed_r)
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
601 return 0;
1954
2f6e137cdc44 Syncing optimizations.
Timo Sirainen <tss@iki.fi>
parents: 1947
diff changeset
602 }
2f6e137cdc44 Syncing optimizations.
Timo Sirainen <tss@iki.fi>
parents: 1947
diff changeset
603
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
604 if (!*new_changed_r) {
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
605 if (maildir_stat(mbox, new_dir, &new_st) < 0)
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
606 return -1;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
607 check_new = TRUE;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
608 }
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
609 if (!*cur_changed_r) {
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
610 if (maildir_stat(mbox, cur_dir, &cur_st) < 0)
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
611 return -1;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
612 check_cur = TRUE;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
613 }
1954
2f6e137cdc44 Syncing optimizations.
Timo Sirainen <tss@iki.fi>
parents: 1947
diff changeset
614
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
615 for (;;) {
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
616 if (check_new)
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
617 *new_changed_r = DIR_MTIME_CHANGED(new_st, hdr, new);
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
618 if (check_cur)
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
619 *cur_changed_r = DIR_MTIME_CHANGED(cur_st, hdr, cur);
1984
9c159272f721 syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
620
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
621 if ((!*new_changed_r && !*cur_changed_r) || refreshed)
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
622 break;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
623
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
624 /* refresh index and try again */
6328
29d249982282 Error handling fixes.
Timo Sirainen <tss@iki.fi>
parents: 6275
diff changeset
625 if (maildir_header_refresh(mbox) < 0)
29d249982282 Error handling fixes.
Timo Sirainen <tss@iki.fi>
parents: 6275
diff changeset
626 return -1;
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
627 refreshed = TRUE;
1915
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
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
630 return 0;
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
6027
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
633 static void maildir_sync_update_next_uid(struct maildir_mailbox *mbox)
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
634 {
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
635 const struct mail_index_header *hdr;
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
636 uint32_t uid_validity, next_uid;
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
637
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
638 hdr = mail_index_get_header(mbox->ibox.view);
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
639 if (hdr->uid_validity == 0)
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
640 return;
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
641
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
642 uid_validity = maildir_uidlist_get_uid_validity(mbox->uidlist);
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
643 next_uid = maildir_uidlist_get_next_uid(mbox->uidlist);
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
644
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
645 if (uid_validity == hdr->uid_validity || uid_validity == 0) {
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
646 /* make sure uidlist's next_uid is at least as large as
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
647 index file's. typically this happens only if uidlist gets
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
648 deleted. */
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
649 maildir_uidlist_set_uid_validity(mbox->uidlist,
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
650 hdr->uid_validity);
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
651 maildir_uidlist_set_next_uid(mbox->uidlist,
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
652 hdr->next_uid, FALSE);
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
653 }
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
654 }
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
655
6037
d911d943438e Recent flag handling rewrite. Still not perfect with maildir.
Timo Sirainen <tss@iki.fi>
parents: 6027
diff changeset
656 static bool move_recent_messages(struct maildir_sync_context *ctx)
d911d943438e Recent flag handling rewrite. Still not perfect with maildir.
Timo Sirainen <tss@iki.fi>
parents: 6027
diff changeset
657 {
d911d943438e Recent flag handling rewrite. Still not perfect with maildir.
Timo Sirainen <tss@iki.fi>
parents: 6027
diff changeset
658 const struct mail_index_header *hdr;
d911d943438e Recent flag handling rewrite. Still not perfect with maildir.
Timo Sirainen <tss@iki.fi>
parents: 6027
diff changeset
659
d911d943438e Recent flag handling rewrite. Still not perfect with maildir.
Timo Sirainen <tss@iki.fi>
parents: 6027
diff changeset
660 if (ctx->mbox->ibox.keep_recent)
d911d943438e Recent flag handling rewrite. Still not perfect with maildir.
Timo Sirainen <tss@iki.fi>
parents: 6027
diff changeset
661 return FALSE;
d911d943438e Recent flag handling rewrite. Still not perfect with maildir.
Timo Sirainen <tss@iki.fi>
parents: 6027
diff changeset
662
6859
64e0f45e8dc3 Delayed NFS attribute cache flushing had some bug, so removed it for now.
Timo Sirainen <tss@iki.fi>
parents: 6800
diff changeset
663 (void)maildir_uidlist_refresh(ctx->mbox->uidlist);
6037
d911d943438e Recent flag handling rewrite. Still not perfect with maildir.
Timo Sirainen <tss@iki.fi>
parents: 6027
diff changeset
664
d911d943438e Recent flag handling rewrite. Still not perfect with maildir.
Timo Sirainen <tss@iki.fi>
parents: 6027
diff changeset
665 /* if there are files in new/, we'll need to move them. we'll check
d911d943438e Recent flag handling rewrite. Still not perfect with maildir.
Timo Sirainen <tss@iki.fi>
parents: 6027
diff changeset
666 this by checking if we have any recent messages */
d911d943438e Recent flag handling rewrite. Still not perfect with maildir.
Timo Sirainen <tss@iki.fi>
parents: 6027
diff changeset
667 hdr = mail_index_get_header(ctx->mbox->ibox.view);
d911d943438e Recent flag handling rewrite. Still not perfect with maildir.
Timo Sirainen <tss@iki.fi>
parents: 6027
diff changeset
668 return hdr->first_recent_uid <
d911d943438e Recent flag handling rewrite. Still not perfect with maildir.
Timo Sirainen <tss@iki.fi>
parents: 6027
diff changeset
669 maildir_uidlist_get_next_uid(ctx->mbox->uidlist);
d911d943438e Recent flag handling rewrite. Still not perfect with maildir.
Timo Sirainen <tss@iki.fi>
parents: 6027
diff changeset
670 }
d911d943438e Recent flag handling rewrite. Still not perfect with maildir.
Timo Sirainen <tss@iki.fi>
parents: 6027
diff changeset
671
6452
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
672 static int maildir_sync_get_changes(struct maildir_sync_context *ctx,
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
673 bool *new_changed_r, bool *cur_changed_r)
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
674 {
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
675 enum mail_index_sync_flags flags = 0;
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
676
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
677 if (maildir_sync_quick_check(ctx->mbox, ctx->new_dir, ctx->cur_dir,
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
678 new_changed_r, cur_changed_r) < 0)
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
679 return -1;
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
680
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
681 if (*new_changed_r || *cur_changed_r)
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
682 return 1;
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
683
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
684 if (move_recent_messages(ctx)) {
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
685 *new_changed_r = TRUE;
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
686 return 1;
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
687 }
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
688
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
689 if (!ctx->mbox->ibox.keep_recent)
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
690 flags |= MAIL_INDEX_SYNC_FLAG_DROP_RECENT;
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
691
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
692 return mail_index_sync_have_any(ctx->mbox->ibox.index, flags) ? 1 : 0;
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
693 }
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
694
3863
55df57c028d4 Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents: 3821
diff changeset
695 static int maildir_sync_context(struct maildir_sync_context *ctx, bool forced,
6452
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
696 bool *lost_files_r)
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
697 {
6452
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
698 bool new_changed, cur_changed;
3863
55df57c028d4 Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents: 3821
diff changeset
699 int ret;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
700
6452
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
701 *lost_files_r = FALSE;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
702
6452
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
703 if (forced)
2140
e2cd51b99359 "readonly sync" -> "forced sync"
Timo Sirainen <tss@iki.fi>
parents: 2123
diff changeset
704 new_changed = cur_changed = TRUE;
6452
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
705 else {
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
706 ret = maildir_sync_get_changes(ctx, &new_changed, &cur_changed);
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
707 if (ret <= 0)
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
708 return ret;
2140
e2cd51b99359 "readonly sync" -> "forced sync"
Timo Sirainen <tss@iki.fi>
parents: 2123
diff changeset
709 }
1947
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
710
2818
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
711 /*
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
712 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
713
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
714 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
715 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
716
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
717 So, we're going to need two locks. One for index and one for
4238
3c8b191b0019 Adding mail to index while saving it had a race condition. Fixing it
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4199
diff changeset
718 uidlist. To avoid deadlocking do the uidlist lock always first.
2121
0840edf34f37 Locking fixes. use less memory
Timo Sirainen <tss@iki.fi>
parents: 2067
diff changeset
719
2818
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
720 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
721 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
722 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
723 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
724
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
725 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
726 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
727 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
728
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
729 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
730 -- 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
731 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
732 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
733 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
734 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
735
2818
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
736 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
737 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
738 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
739
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
740 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
741 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
742 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
743 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
744 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
745 already done.
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
746
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
747 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
748 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
749 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
750
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
751 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
752 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
753 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
754 */
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
755
3530
e9695ec7925b Recursive maildir uidlist syncs caused assert crashes. Also did some
Timo Sirainen <tss@iki.fi>
parents: 3520
diff changeset
756 ctx->partial = !cur_changed;
e9695ec7925b Recursive maildir uidlist syncs caused assert crashes. Also did some
Timo Sirainen <tss@iki.fi>
parents: 3520
diff changeset
757 ret = maildir_uidlist_sync_init(ctx->mbox->uidlist, ctx->partial,
e9695ec7925b Recursive maildir uidlist syncs caused assert crashes. Also did some
Timo Sirainen <tss@iki.fi>
parents: 3520
diff changeset
758 &ctx->uidlist_sync_ctx);
e9695ec7925b Recursive maildir uidlist syncs caused assert crashes. Also did some
Timo Sirainen <tss@iki.fi>
parents: 3520
diff changeset
759 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
760 /* 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
761 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
762 the trouble? .. */
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
763 return ret;
2140
e2cd51b99359 "readonly sync" -> "forced sync"
Timo Sirainen <tss@iki.fi>
parents: 2123
diff changeset
764 }
2123
e01de478882f locking fixes
Timo Sirainen <tss@iki.fi>
parents: 2121
diff changeset
765
4238
3c8b191b0019 Adding mail to index while saving it had a race condition. Fixing it
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4199
diff changeset
766 if (!ctx->mbox->syncing_commit) {
5920
00c5e3cbeaf0 Moved index syncing code to its own file.
Timo Sirainen <tss@iki.fi>
parents: 5918
diff changeset
767 if (maildir_sync_index_begin(ctx->mbox, ctx,
4238
3c8b191b0019 Adding mail to index while saving it had a race condition. Fixing it
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4199
diff changeset
768 &ctx->index_sync_ctx) < 0)
3c8b191b0019 Adding mail to index while saving it had a race condition. Fixing it
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4199
diff changeset
769 return -1;
3c8b191b0019 Adding mail to index while saving it had a race condition. Fixing it
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4199
diff changeset
770 }
3c8b191b0019 Adding mail to index while saving it had a race condition. Fixing it
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4199
diff changeset
771
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
772 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
773 /* 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
774 that new/ dir is checked as well. it's a good idea anyway. */
5391
0c8705aad54c Avoid infinite looping when buggy filesystems.
Timo Sirainen <tss@iki.fi>
parents: 5390
diff changeset
775 unsigned int count = 0;
0c8705aad54c Avoid infinite looping when buggy filesystems.
Timo Sirainen <tss@iki.fi>
parents: 5390
diff changeset
776
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
777 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
778 /* 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
779 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
780 (see MAILDIR_RENAME_RESCAN_COUNT). */
5391
0c8705aad54c Avoid infinite looping when buggy filesystems.
Timo Sirainen <tss@iki.fi>
parents: 5390
diff changeset
781 if (++count > MAILDIR_SCAN_DIR_MAX_COUNT)
0c8705aad54c Avoid infinite looping when buggy filesystems.
Timo Sirainen <tss@iki.fi>
parents: 5390
diff changeset
782 break;
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
783 }
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
784 if (ret < 0)
1954
2f6e137cdc44 Syncing optimizations.
Timo Sirainen <tss@iki.fi>
parents: 1947
diff changeset
785 return -1;
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
786
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
787 if (cur_changed) {
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
788 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
789 return -1;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
790 }
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
791
6027
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
792 maildir_sync_update_next_uid(ctx->mbox);
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
793
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
794 /* 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
795 maildir_uidlist_sync_finish(ctx->uidlist_sync_ctx);
1954
2f6e137cdc44 Syncing optimizations.
Timo Sirainen <tss@iki.fi>
parents: 1947
diff changeset
796 }
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
797
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
798 if (!ctx->mbox->syncing_commit) {
3530
e9695ec7925b Recursive maildir uidlist syncs caused assert crashes. Also did some
Timo Sirainen <tss@iki.fi>
parents: 3520
diff changeset
799 /* NOTE: index syncing here might cause a re-sync due to
e9695ec7925b Recursive maildir uidlist syncs caused assert crashes. Also did some
Timo Sirainen <tss@iki.fi>
parents: 3520
diff changeset
800 files getting lost, so this function might be called
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
801 re-entrantly. */
4238
3c8b191b0019 Adding mail to index while saving it had a race condition. Fixing it
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4199
diff changeset
802 ret = maildir_sync_index(ctx->index_sync_ctx, ctx->partial);
3c8b191b0019 Adding mail to index while saving it had a race condition. Fixing it
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4199
diff changeset
803 if (maildir_sync_index_finish(&ctx->index_sync_ctx,
4774
615b7738a62f Saving mails could have skipped over transactions, which caused different
Timo Sirainen <tss@iki.fi>
parents: 4612
diff changeset
804 ret < 0, FALSE) < 0)
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
805 return -1;
4238
3c8b191b0019 Adding mail to index while saving it had a race condition. Fixing it
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4199
diff changeset
806
3c8b191b0019 Adding mail to index while saving it had a race condition. Fixing it
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4199
diff changeset
807 if (ret < 0)
3c8b191b0019 Adding mail to index while saving it had a race condition. Fixing it
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4199
diff changeset
808 return -1;
3436
3c51658d6846 We didn't notice if messages were deleted directly from new/.
Timo Sirainen <tss@iki.fi>
parents: 3435
diff changeset
809 if (ret == 0)
6452
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
810 *lost_files_r = TRUE;
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
811
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
812 i_assert(maildir_uidlist_is_locked(ctx->mbox->uidlist));
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
813 }
1984
9c159272f721 syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
814
6452
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
815 return maildir_uidlist_sync_deinit(&ctx->uidlist_sync_ctx);
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
816 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
817
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
818 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
819 {
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
820 struct maildir_sync_context *ctx;
6452
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
821 bool lost_files;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
822 int ret;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
823
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
824 ctx = maildir_sync_context_new(mbox);
6452
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
825 ret = maildir_sync_context(ctx, TRUE, &lost_files);
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
826 maildir_sync_deinit(ctx);
6452
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
827 return ret;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
828 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
829
2322
aae574ed7f4c Broke mailbox_sync() into iterator.
Timo Sirainen <tss@iki.fi>
parents: 2320
diff changeset
830 struct mailbox_sync_context *
aae574ed7f4c Broke mailbox_sync() into iterator.
Timo Sirainen <tss@iki.fi>
parents: 2320
diff changeset
831 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
832 {
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
833 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
834 struct maildir_sync_context *ctx;
6452
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
835 bool lost_files;
2322
aae574ed7f4c Broke mailbox_sync() into iterator.
Timo Sirainen <tss@iki.fi>
parents: 2320
diff changeset
836 int ret = 0;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
837
4894
24afafbfe47b Make sure the mailbox is opened when transaction is started (fixes deliver).
Timo Sirainen <tss@iki.fi>
parents: 4848
diff changeset
838 if (!box->opened)
24afafbfe47b Make sure the mailbox is opened when transaction is started (fixes deliver).
Timo Sirainen <tss@iki.fi>
parents: 4848
diff changeset
839 index_storage_mailbox_open(&mbox->ibox);
4848
967de900c73a Mailbox list indexing and related changes. Currently works only with
Timo Sirainen <tss@iki.fi>
parents: 4774
diff changeset
840
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
841 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
842 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
843 ioloop_time) {
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
844 mbox->ibox.sync_last_check = ioloop_time;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
845
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
846 ctx = maildir_sync_context_new(mbox);
6452
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
847 ret = maildir_sync_context(ctx, FALSE, &lost_files);
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
848 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
849
4152
e2edd333c473 Added MAILBOX_OPEN_KEEP_LOCKED flag to mailbox opening and implemented it
Timo Sirainen <tss@iki.fi>
parents: 4126
diff changeset
850 i_assert(!maildir_uidlist_is_locked(mbox->uidlist) ||
e2edd333c473 Added MAILBOX_OPEN_KEEP_LOCKED flag to mailbox opening and implemented it
Timo Sirainen <tss@iki.fi>
parents: 4126
diff changeset
851 mbox->ibox.keep_locked);
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
852
6452
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
853 if (lost_files) {
3436
3c51658d6846 We didn't notice if messages were deleted directly from new/.
Timo Sirainen <tss@iki.fi>
parents: 3435
diff changeset
854 /* 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
855 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
856 }
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
857 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
858
2322
aae574ed7f4c Broke mailbox_sync() into iterator.
Timo Sirainen <tss@iki.fi>
parents: 2320
diff changeset
859 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
860 }
3472
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
861
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
862 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
863 {
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
864 const char *new_dir, *cur_dir;
3863
55df57c028d4 Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents: 3821
diff changeset
865 bool new_changed, cur_changed;
55df57c028d4 Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents: 3821
diff changeset
866 int ret;
3472
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
867
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
868 t_push();
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
869 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
870 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
871
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
872 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
873 &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
874 t_pop();
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
875 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
876 }