annotate src/lib-storage/index/maildir/maildir-sync.c @ 6883:15df8bb27d7a HEAD

If we did unlocked maildir sync, make sure we sync it later with locking.
author Timo Sirainen <tss@iki.fi>
date Sat, 01 Dec 2007 09:51:43 +0200
parents b1554dea8ee8
children 2bef19ae5233
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;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
211
6881
b1554dea8ee8 If we're doing a fast sync, try only once to lock uidlist.
Timo Sirainen <tss@iki.fi>
parents: 6880
diff changeset
212 enum mailbox_sync_flags flags;
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;
6880
724c8f12eed2 maildir_storage_sync_force(): Don't wait on trying to lock uidlist. Just try
Timo Sirainen <tss@iki.fi>
parents: 6878
diff changeset
216 struct maildir_index_sync_context *index_sync_ctx;
724c8f12eed2 maildir_storage_sync_force(): Don't wait on trying to lock uidlist. Just try
Timo Sirainen <tss@iki.fi>
parents: 6878
diff changeset
217
724c8f12eed2 maildir_storage_sync_force(): Don't wait on trying to lock uidlist. Just try
Timo Sirainen <tss@iki.fi>
parents: 6878
diff changeset
218 unsigned int partial:1;
724c8f12eed2 maildir_storage_sync_force(): Don't wait on trying to lock uidlist. Just try
Timo Sirainen <tss@iki.fi>
parents: 6878
diff changeset
219 unsigned int locked:1;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
220 };
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
221
5920
00c5e3cbeaf0 Moved index syncing code to its own file.
Timo Sirainen <tss@iki.fi>
parents: 5918
diff changeset
222 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
223 {
7d45edb81fe4 When copying/syncing a lot of mails, send "* OK Hang in there" replies to
Timo Sirainen <tss@iki.fi>
parents: 5307
diff changeset
224 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
225
5390
4eeec560df01 If saving to maildir causes flag/expunge syncs, don't crash.
Timo Sirainen <tss@iki.fi>
parents: 5388
diff changeset
226 if (ctx == NULL) {
4eeec560df01 If saving to maildir causes flag/expunge syncs, don't crash.
Timo Sirainen <tss@iki.fi>
parents: 5388
diff changeset
227 /* 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
228 maildir_sync_context, */
4eeec560df01 If saving to maildir causes flag/expunge syncs, don't crash.
Timo Sirainen <tss@iki.fi>
parents: 5388
diff changeset
229 return;
4eeec560df01 If saving to maildir causes flag/expunge syncs, don't crash.
Timo Sirainen <tss@iki.fi>
parents: 5388
diff changeset
230 }
4eeec560df01 If saving to maildir causes flag/expunge syncs, don't crash.
Timo Sirainen <tss@iki.fi>
parents: 5388
diff changeset
231
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
232 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
233 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
234 (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
235 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
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 (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
238 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
239
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 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
241 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
242 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
243 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
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 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
246 }
7d45edb81fe4 When copying/syncing a lot of mails, send "* OK Hang in there" replies to
Timo Sirainen <tss@iki.fi>
parents: 5307
diff changeset
247 }
7d45edb81fe4 When copying/syncing a lot of mails, send "* OK Hang in there" replies to
Timo Sirainen <tss@iki.fi>
parents: 5307
diff changeset
248
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
249 static struct maildir_sync_context *
6881
b1554dea8ee8 If we're doing a fast sync, try only once to lock uidlist.
Timo Sirainen <tss@iki.fi>
parents: 6880
diff changeset
250 maildir_sync_context_new(struct maildir_mailbox *mbox,
b1554dea8ee8 If we're doing a fast sync, try only once to lock uidlist.
Timo Sirainen <tss@iki.fi>
parents: 6880
diff changeset
251 enum mailbox_sync_flags flags)
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
252 {
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
253 struct maildir_sync_context *ctx;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
254
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
255 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
256 ctx->mbox = mbox;
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
257 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
258 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
259 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
260 ctx->last_notify = ioloop_time;
6881
b1554dea8ee8 If we're doing a fast sync, try only once to lock uidlist.
Timo Sirainen <tss@iki.fi>
parents: 6880
diff changeset
261 ctx->flags = flags;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
262 return ctx;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
263 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
264
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
265 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
266 {
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
267 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
268 (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
269 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
270 (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
271 TRUE, FALSE);
615b7738a62f Saving mails could have skipped over transactions, which caused different
Timo Sirainen <tss@iki.fi>
parents: 4612
diff changeset
272 }
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
273 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
274
4397
5cbabd4ccd9c Don't go fixing duplicate maildir filenames without properly checking that
Timo Sirainen <tss@iki.fi>
parents: 4238
diff changeset
275 static int maildir_fix_duplicate(struct maildir_sync_context *ctx,
5910
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
276 const char *dir, const char *fname2)
1915
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 const char *fname1, *path1, *path2;
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
279 const char *new_fname, *new_path;
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
280 struct stat st1, st2;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
281 int ret = 0;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
282
5910
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
283 fname1 = maildir_uidlist_sync_get_full_filename(ctx->uidlist_sync_ctx,
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
284 fname2);
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
285 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
286
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
287 t_push();
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
288
5910
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
289 path1 = t_strconcat(dir, "/", fname1, NULL);
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
290 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
291
5910
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
292 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
293 /* 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
294 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
295 t_pop();
5cbabd4ccd9c Don't go fixing duplicate maildir filenames without properly checking that
Timo Sirainen <tss@iki.fi>
parents: 4238
diff changeset
296 return 0;
5cbabd4ccd9c Don't go fixing duplicate maildir filenames without properly checking that
Timo Sirainen <tss@iki.fi>
parents: 4238
diff changeset
297 }
5910
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
298 if (st1.st_ino == st2.st_ino &&
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
299 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
300 /* 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
301 between stat() calls, or that the files were link()ed. */
5910
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
302 if (st1.st_nlink > 1 && st2.st_nlink == st1.st_nlink &&
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
303 st1.st_ctime == st2.st_ctime &&
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
304 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
305 /* 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
306 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
307 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
308
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
309 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
310 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
311 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
312 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
313 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
314 happening) */
5910
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
315 if (unlink(path2) == 0)
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
316 i_warning("Unlinked a duplicate: %s", path2);
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
317 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
318 mail_storage_set_critical(
5910
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
319 &ctx->mbox->storage->storage,
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
320 "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
321 }
293bc7d1062f If we find duplicate hard links which haven't changed for 30 secs, unlink()
Timo Sirainen <tss@iki.fi>
parents: 5397
diff changeset
322 }
4397
5cbabd4ccd9c Don't go fixing duplicate maildir filenames without properly checking that
Timo Sirainen <tss@iki.fi>
parents: 4238
diff changeset
323 t_pop();
5cbabd4ccd9c Don't go fixing duplicate maildir filenames without properly checking that
Timo Sirainen <tss@iki.fi>
parents: 4238
diff changeset
324 return 0;
5cbabd4ccd9c Don't go fixing duplicate maildir filenames without properly checking that
Timo Sirainen <tss@iki.fi>
parents: 4238
diff changeset
325 }
5cbabd4ccd9c Don't go fixing duplicate maildir filenames without properly checking that
Timo Sirainen <tss@iki.fi>
parents: 4238
diff changeset
326
5904
62ceb6b2b20d Renamed maildir_generate_tmp_filename() to maildir_filename_generate(). Also
Timo Sirainen <tss@iki.fi>
parents: 5901
diff changeset
327 new_fname = maildir_filename_generate();
5910
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
328 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
329
5910
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
330 if (rename(path2, new_path) == 0)
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
331 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
332 else if (errno != ENOENT) {
5910
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
333 mail_storage_set_critical(&ctx->mbox->storage->storage,
5582
1c4fd25893bd Updated error message.
Timo Sirainen <tss@iki.fi>
parents: 5564
diff changeset
334 "Couldn't fix a duplicate: rename(%s, %s) failed: %m",
5910
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
335 path2, new_path);
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
336 ret = -1;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
337 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
338 t_pop();
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
339
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
340 return ret;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
341 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
342
5926
095b3adc537b Handle mailbox deletions while syncing silently.
Timo Sirainen <tss@iki.fi>
parents: 5920
diff changeset
343 static int
095b3adc537b Handle mailbox deletions while syncing silently.
Timo Sirainen <tss@iki.fi>
parents: 5920
diff changeset
344 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
345 {
6777
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
346 int i;
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
347
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
348 for (i = 0;; i++) {
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
349 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
350 return 0;
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
351 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
352 break;
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
353
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
354 if (maildir_set_deleted(mbox))
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
355 return -1;
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
356 /* try again */
5926
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
095b3adc537b Handle mailbox deletions while syncing silently.
Timo Sirainen <tss@iki.fi>
parents: 5920
diff changeset
359 mail_storage_set_critical(mbox->ibox.box.storage,
095b3adc537b Handle mailbox deletions while syncing silently.
Timo Sirainen <tss@iki.fi>
parents: 5920
diff changeset
360 "stat(%s) failed: %m", path);
095b3adc537b Handle mailbox deletions while syncing silently.
Timo Sirainen <tss@iki.fi>
parents: 5920
diff changeset
361 return -1;
095b3adc537b Handle mailbox deletions while syncing silently.
Timo Sirainen <tss@iki.fi>
parents: 5920
diff changeset
362 }
095b3adc537b Handle mailbox deletions while syncing silently.
Timo Sirainen <tss@iki.fi>
parents: 5920
diff changeset
363
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
364 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
365 {
5459
78eaf595359c Removed struct index_storage abstraction. It's pointless.
Timo Sirainen <tss@iki.fi>
parents: 5443
diff changeset
366 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
367 const char *path;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
368 DIR *dirp;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
369 string_t *src, *dest;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
370 struct dirent *dp;
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
371 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
372 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
373 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
374 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
375 int ret = 1;
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
376 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
377
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
378 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
379 for (i = 0;; i++) {
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
380 dirp = opendir(path);
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
381 if (dirp != NULL)
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
382 break;
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
383
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
384 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
385 mail_storage_set_critical(storage,
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
386 "opendir(%s) failed: %m", path);
5926
095b3adc537b Handle mailbox deletions while syncing silently.
Timo Sirainen <tss@iki.fi>
parents: 5920
diff changeset
387 return -1;
095b3adc537b Handle mailbox deletions while syncing silently.
Timo Sirainen <tss@iki.fi>
parents: 5920
diff changeset
388 }
6777
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
389
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
390 if (maildir_set_deleted(ctx->mbox))
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
391 return -1;
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
392 /* try again */
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
393 }
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
394
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
395 #ifdef HAVE_DIRFD
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
396 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
397 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
398 "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
399 (void)closedir(dirp);
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
400 return -1;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
401 }
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
402 #else
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
403 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
404 (void)closedir(dirp);
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
405 return -1;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
406 }
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
407 #endif
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
408
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
409 now = time(NULL);
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
410 if (new_dir) {
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_check_time = now;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
412 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
413 #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
414 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
415 #else
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
416 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
417 #endif
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_check_time = now;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
420 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
421 #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
422 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
423 #else
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
424 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
425 #endif
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
426 }
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
427
2121
0840edf34f37 Locking fixes. use less memory
Timo Sirainen <tss@iki.fi>
parents: 2067
diff changeset
428 t_push();
0840edf34f37 Locking fixes. use less memory
Timo Sirainen <tss@iki.fi>
parents: 2067
diff changeset
429 src = t_str_new(1024);
0840edf34f37 Locking fixes. use less memory
Timo Sirainen <tss@iki.fi>
parents: 2067
diff changeset
430 dest = t_str_new(1024);
0840edf34f37 Locking fixes. use less memory
Timo Sirainen <tss@iki.fi>
parents: 2067
diff changeset
431
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
432 move_new = new_dir && !mailbox_is_readonly(&ctx->mbox->ibox.box) &&
6880
724c8f12eed2 maildir_storage_sync_force(): Don't wait on trying to lock uidlist. Just try
Timo Sirainen <tss@iki.fi>
parents: 6878
diff changeset
433 !ctx->mbox->ibox.keep_recent && ctx->locked;
5911
8bb5c7a69405 If readdir() or closedir() fails, return failure.
Timo Sirainen <tss@iki.fi>
parents: 5910
diff changeset
434
8bb5c7a69405 If readdir() or closedir() fails, return failure.
Timo Sirainen <tss@iki.fi>
parents: 5910
diff changeset
435 errno = 0;
8bb5c7a69405 If readdir() or closedir() fails, return failure.
Timo Sirainen <tss@iki.fi>
parents: 5910
diff changeset
436 for (; (dp = readdir(dirp)) != NULL; errno = 0) {
2258
087a43e29492 No maildir filename checking after all.
Timo Sirainen <tss@iki.fi>
parents: 2256
diff changeset
437 if (dp->d_name[0] == '.')
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
438 continue;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
439
1978
6303ef092c5b mbox code compiles again, but syncing is only partially implemented so
Timo Sirainen <tss@iki.fi>
parents: 1970
diff changeset
440 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
441 dp->d_name);
1984
9c159272f721 syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
442 if (ret == 0) {
2038
df504dad3aec Recent flag fixes. Should work perfectly now with maildir.
Timo Sirainen <tss@iki.fi>
parents: 2037
diff changeset
443 /* 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
444 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
445 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
446 continue;
1984
9c159272f721 syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
447 }
1978
6303ef092c5b mbox code compiles again, but syncing is only partially implemented so
Timo Sirainen <tss@iki.fi>
parents: 1970
diff changeset
448 if (ret < 0)
6303ef092c5b mbox code compiles again, but syncing is only partially implemented so
Timo Sirainen <tss@iki.fi>
parents: 1970
diff changeset
449 break;
6303ef092c5b mbox code compiles again, but syncing is only partially implemented so
Timo Sirainen <tss@iki.fi>
parents: 1970
diff changeset
450
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
451 check_touch = FALSE;
1947
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
452 flags = 0;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
453 if (move_new) {
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
454 str_truncate(src, 0);
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
455 str_truncate(dest, 0);
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
456 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
457 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
458 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
459 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
460 }
1947
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
461 if (rename(str_c(src), str_c(dest)) == 0) {
1984
9c159272f721 syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
462 /* 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
463 dir_changed = TRUE;
5914
ae731dbf3a6f Sync index changes while iterating through uidlist entries. This avoids
Timo Sirainen <tss@iki.fi>
parents: 5912
diff changeset
464 move_count++;
1947
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
465 flags |= MAILDIR_UIDLIST_REC_FLAG_MOVED |
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
466 MAILDIR_UIDLIST_REC_FLAG_RECENT;
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
467 } else if (ENOTFOUND(errno)) {
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
468 /* 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
469 dir_changed = TRUE;
5914
ae731dbf3a6f Sync index changes while iterating through uidlist entries. This avoids
Timo Sirainen <tss@iki.fi>
parents: 5912
diff changeset
470 move_count++;
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_MOVED |
d911d943438e Recent flag handling rewrite. Still not perfect with maildir.
Timo Sirainen <tss@iki.fi>
parents: 6027
diff changeset
472 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
473 } 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
474 /* 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
475 leave here */
6037
d911d943438e Recent flag handling rewrite. Still not perfect with maildir.
Timo Sirainen <tss@iki.fi>
parents: 6027
diff changeset
476 flags |= MAILDIR_UIDLIST_REC_FLAG_NEW_DIR;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
477 move_new = FALSE;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
478 } else {
6037
d911d943438e Recent flag handling rewrite. Still not perfect with maildir.
Timo Sirainen <tss@iki.fi>
parents: 6027
diff changeset
479 flags |= MAILDIR_UIDLIST_REC_FLAG_NEW_DIR;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
480 mail_storage_set_critical(storage,
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
481 "rename(%s, %s) failed: %m",
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
482 str_c(src), str_c(dest));
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
483 }
5914
ae731dbf3a6f Sync index changes while iterating through uidlist entries. This avoids
Timo Sirainen <tss@iki.fi>
parents: 5912
diff changeset
484 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
485 maildir_sync_notify(ctx);
1947
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
486 } else if (new_dir) {
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
487 flags |= MAILDIR_UIDLIST_REC_FLAG_NEW_DIR |
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
488 MAILDIR_UIDLIST_REC_FLAG_RECENT;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
489 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
490
5914
ae731dbf3a6f Sync index changes while iterating through uidlist entries. This avoids
Timo Sirainen <tss@iki.fi>
parents: 5912
diff changeset
491 i++;
ae731dbf3a6f Sync index changes while iterating through uidlist entries. This avoids
Timo Sirainen <tss@iki.fi>
parents: 5912
diff changeset
492 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
493 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
494
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
495 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
496 dp->d_name, flags);
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
497 if (ret <= 0) {
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
498 if (ret < 0)
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 /* 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
502 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
503 ret = -1;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
504 break;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
505 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
506 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
507 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
508
5911
8bb5c7a69405 If readdir() or closedir() fails, return failure.
Timo Sirainen <tss@iki.fi>
parents: 5910
diff changeset
509 if (errno != 0) {
8bb5c7a69405 If readdir() or closedir() fails, return failure.
Timo Sirainen <tss@iki.fi>
parents: 5910
diff changeset
510 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
511 "readdir(%s) failed: %m", path);
5911
8bb5c7a69405 If readdir() or closedir() fails, return failure.
Timo Sirainen <tss@iki.fi>
parents: 5910
diff changeset
512 ret = -1;
8bb5c7a69405 If readdir() or closedir() fails, return failure.
Timo Sirainen <tss@iki.fi>
parents: 5910
diff changeset
513 }
8bb5c7a69405 If readdir() or closedir() fails, return failure.
Timo Sirainen <tss@iki.fi>
parents: 5910
diff changeset
514
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
515 if (closedir(dirp) < 0) {
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
516 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
517 "closedir(%s) failed: %m", path);
5911
8bb5c7a69405 If readdir() or closedir() fails, return failure.
Timo Sirainen <tss@iki.fi>
parents: 5910
diff changeset
518 ret = -1;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
519 }
2121
0840edf34f37 Locking fixes. use less memory
Timo Sirainen <tss@iki.fi>
parents: 2067
diff changeset
520
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
521 if (dir_changed) {
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
522 if (new_dir)
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
523 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
524 else
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
525 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
526 }
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
527
2121
0840edf34f37 Locking fixes. use less memory
Timo Sirainen <tss@iki.fi>
parents: 2067
diff changeset
528 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
529 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
530 (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
531 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
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 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
534 {
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
535 const void *data;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
536 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
537
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
538 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
539 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
540 return -1;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
541 }
3472
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
542
6275
913b188f4dd4 Removed explicit locking from views and maps. They were already locked all
Timo Sirainen <tss@iki.fi>
parents: 6037
diff changeset
543 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
544 &data, &data_size);
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 == 0) {
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
546 /* doesn't exist */
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
547 return 0;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
548 }
4848
967de900c73a Mailbox list indexing and related changes. Currently works only with
Timo Sirainen <tss@iki.fi>
parents: 4774
diff changeset
549
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
550 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
551 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
552 else
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
553 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
554 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
555 }
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
556
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
557 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
558 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
559 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
560 {
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
561 #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
562 # define DIR_NSECS_CHANGED(st, hdr, name) \
5983
272796cfee66 compiler warning fixes
Timo Sirainen <tss@iki.fi>
parents: 5950
diff changeset
563 ((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
564 #else
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
565 # 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
566 #endif
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_DELAYED_REFRESH(hdr, name) \
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
569 ((hdr)->name ## _check_time <= \
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
570 (hdr)->name ## _mtime + MAILDIR_SYNC_SECS && \
5983
272796cfee66 compiler warning fixes
Timo Sirainen <tss@iki.fi>
parents: 5950
diff changeset
571 (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
572
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
573 #define DIR_MTIME_CHANGED(st, hdr, name) \
5983
272796cfee66 compiler warning fixes
Timo Sirainen <tss@iki.fi>
parents: 5950
diff changeset
574 ((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
575 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
576
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
577 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
578 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
579 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
580
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
581 if (mbox->maildir_hdr.new_mtime == 0) {
6328
29d249982282 Error handling fixes.
Timo Sirainen <tss@iki.fi>
parents: 6275
diff changeset
582 if (maildir_header_refresh(mbox) < 0)
29d249982282 Error handling fixes.
Timo Sirainen <tss@iki.fi>
parents: 6275
diff changeset
583 return -1;
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
584 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
585 /* first sync */
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
586 *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
587 return 0;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
588 }
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
589 }
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
590
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
591 *new_changed_r = *cur_changed_r = FALSE;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
592
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
593 /* 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
594 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
595 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
596 /* refresh index and try again */
6328
29d249982282 Error handling fixes.
Timo Sirainen <tss@iki.fi>
parents: 6275
diff changeset
597 if (maildir_header_refresh(mbox) < 0)
29d249982282 Error handling fixes.
Timo Sirainen <tss@iki.fi>
parents: 6275
diff changeset
598 return -1;
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
599 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
600
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
601 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
602 *new_changed_r = TRUE;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
603 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
604 *cur_changed_r = TRUE;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
605 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
606 return 0;
1954
2f6e137cdc44 Syncing optimizations.
Timo Sirainen <tss@iki.fi>
parents: 1947
diff changeset
607 }
2f6e137cdc44 Syncing optimizations.
Timo Sirainen <tss@iki.fi>
parents: 1947
diff changeset
608
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
609 if (!*new_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, 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
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_new = TRUE;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
613 }
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
614 if (!*cur_changed_r) {
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
615 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
616 return -1;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
617 check_cur = TRUE;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
618 }
1954
2f6e137cdc44 Syncing optimizations.
Timo Sirainen <tss@iki.fi>
parents: 1947
diff changeset
619
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
620 for (;;) {
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
621 if (check_new)
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
622 *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
623 if (check_cur)
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
624 *cur_changed_r = DIR_MTIME_CHANGED(cur_st, hdr, cur);
1984
9c159272f721 syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
625
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
626 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
627 break;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
628
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
629 /* refresh index and try again */
6328
29d249982282 Error handling fixes.
Timo Sirainen <tss@iki.fi>
parents: 6275
diff changeset
630 if (maildir_header_refresh(mbox) < 0)
29d249982282 Error handling fixes.
Timo Sirainen <tss@iki.fi>
parents: 6275
diff changeset
631 return -1;
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
632 refreshed = TRUE;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
633 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
634
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
635 return 0;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
636 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
637
6027
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
638 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
639 {
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
640 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
641 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
642
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
643 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
644 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
645 return;
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
646
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
647 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
648 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
649
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
650 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
651 /* 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
652 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
653 deleted. */
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
654 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
655 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
656 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
657 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
658 }
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
659 }
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
660
6037
d911d943438e Recent flag handling rewrite. Still not perfect with maildir.
Timo Sirainen <tss@iki.fi>
parents: 6027
diff changeset
661 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
662 {
d911d943438e Recent flag handling rewrite. Still not perfect with maildir.
Timo Sirainen <tss@iki.fi>
parents: 6027
diff changeset
663 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
664
d911d943438e Recent flag handling rewrite. Still not perfect with maildir.
Timo Sirainen <tss@iki.fi>
parents: 6027
diff changeset
665 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
666 return FALSE;
d911d943438e Recent flag handling rewrite. Still not perfect with maildir.
Timo Sirainen <tss@iki.fi>
parents: 6027
diff changeset
667
6859
64e0f45e8dc3 Delayed NFS attribute cache flushing had some bug, so removed it for now.
Timo Sirainen <tss@iki.fi>
parents: 6800
diff changeset
668 (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
669
d911d943438e Recent flag handling rewrite. Still not perfect with maildir.
Timo Sirainen <tss@iki.fi>
parents: 6027
diff changeset
670 /* 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
671 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
672 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
673 return hdr->first_recent_uid <
d911d943438e Recent flag handling rewrite. Still not perfect with maildir.
Timo Sirainen <tss@iki.fi>
parents: 6027
diff changeset
674 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
675 }
d911d943438e Recent flag handling rewrite. Still not perfect with maildir.
Timo Sirainen <tss@iki.fi>
parents: 6027
diff changeset
676
6452
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
677 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
678 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
679 {
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
680 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
681
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
682 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
683 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
684 return -1;
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
685
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
686 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
687 return 1;
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 (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
690 *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
691 return 1;
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
692 }
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 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
695 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
696
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
697 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
698 }
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
699
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
700 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
701 bool *lost_files_r)
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
702 {
6878
0c3ab0aef33c maildir_uidlist_sync_init() was called wrong, although it just happened to
Timo Sirainen <tss@iki.fi>
parents: 6859
diff changeset
703 enum maildir_uidlist_sync_flags sync_flags;
6452
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
704 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
705 int ret;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
706
6452
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
707 *lost_files_r = FALSE;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
708
6452
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
709 if (forced)
2140
e2cd51b99359 "readonly sync" -> "forced sync"
Timo Sirainen <tss@iki.fi>
parents: 2123
diff changeset
710 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
711 else {
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
712 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
713 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
714 return ret;
2140
e2cd51b99359 "readonly sync" -> "forced sync"
Timo Sirainen <tss@iki.fi>
parents: 2123
diff changeset
715 }
1947
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
716
2818
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
717 /*
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
718 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
719
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
720 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
721 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
722
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
723 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
724 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
725
2818
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
726 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
727 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
728 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
729 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
730
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
731 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
732 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
733 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
734
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
735 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
736 -- 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
737 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
738 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
739 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
740 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
741
2818
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
742 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
743 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
744 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
745
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
746 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
747 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
748 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
749 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
750 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
751 already done.
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
752
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
753 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
754 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
755 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
756
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
757 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
758 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
759 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
760 */
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
761
6880
724c8f12eed2 maildir_storage_sync_force(): Don't wait on trying to lock uidlist. Just try
Timo Sirainen <tss@iki.fi>
parents: 6878
diff changeset
762 if (!cur_changed || forced) {
724c8f12eed2 maildir_storage_sync_force(): Don't wait on trying to lock uidlist. Just try
Timo Sirainen <tss@iki.fi>
parents: 6878
diff changeset
763 ctx->partial = TRUE;
724c8f12eed2 maildir_storage_sync_force(): Don't wait on trying to lock uidlist. Just try
Timo Sirainen <tss@iki.fi>
parents: 6878
diff changeset
764 sync_flags = MAILDIR_UIDLIST_SYNC_PARTIAL;
724c8f12eed2 maildir_storage_sync_force(): Don't wait on trying to lock uidlist. Just try
Timo Sirainen <tss@iki.fi>
parents: 6878
diff changeset
765 if (forced)
724c8f12eed2 maildir_storage_sync_force(): Don't wait on trying to lock uidlist. Just try
Timo Sirainen <tss@iki.fi>
parents: 6878
diff changeset
766 sync_flags |= MAILDIR_UIDLIST_SYNC_FORCE;
724c8f12eed2 maildir_storage_sync_force(): Don't wait on trying to lock uidlist. Just try
Timo Sirainen <tss@iki.fi>
parents: 6878
diff changeset
767 } else {
724c8f12eed2 maildir_storage_sync_force(): Don't wait on trying to lock uidlist. Just try
Timo Sirainen <tss@iki.fi>
parents: 6878
diff changeset
768 ctx->partial = FALSE;
724c8f12eed2 maildir_storage_sync_force(): Don't wait on trying to lock uidlist. Just try
Timo Sirainen <tss@iki.fi>
parents: 6878
diff changeset
769 sync_flags = 0;
6881
b1554dea8ee8 If we're doing a fast sync, try only once to lock uidlist.
Timo Sirainen <tss@iki.fi>
parents: 6880
diff changeset
770 if ((ctx->flags & MAILBOX_SYNC_FLAG_FAST) != 0)
b1554dea8ee8 If we're doing a fast sync, try only once to lock uidlist.
Timo Sirainen <tss@iki.fi>
parents: 6880
diff changeset
771 sync_flags |= MAILDIR_UIDLIST_SYNC_TRYLOCK;
6880
724c8f12eed2 maildir_storage_sync_force(): Don't wait on trying to lock uidlist. Just try
Timo Sirainen <tss@iki.fi>
parents: 6878
diff changeset
772 }
6878
0c3ab0aef33c maildir_uidlist_sync_init() was called wrong, although it just happened to
Timo Sirainen <tss@iki.fi>
parents: 6859
diff changeset
773 ret = maildir_uidlist_sync_init(ctx->mbox->uidlist, sync_flags,
3530
e9695ec7925b Recursive maildir uidlist syncs caused assert crashes. Also did some
Timo Sirainen <tss@iki.fi>
parents: 3520
diff changeset
774 &ctx->uidlist_sync_ctx);
e9695ec7925b Recursive maildir uidlist syncs caused assert crashes. Also did some
Timo Sirainen <tss@iki.fi>
parents: 3520
diff changeset
775 if (ret <= 0) {
6880
724c8f12eed2 maildir_storage_sync_force(): Don't wait on trying to lock uidlist. Just try
Timo Sirainen <tss@iki.fi>
parents: 6878
diff changeset
776 /* failure / timeout */
724c8f12eed2 maildir_storage_sync_force(): Don't wait on trying to lock uidlist. Just try
Timo Sirainen <tss@iki.fi>
parents: 6878
diff changeset
777 i_assert(ret < 0 || !forced);
2818
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
778 return ret;
2140
e2cd51b99359 "readonly sync" -> "forced sync"
Timo Sirainen <tss@iki.fi>
parents: 2123
diff changeset
779 }
6880
724c8f12eed2 maildir_storage_sync_force(): Don't wait on trying to lock uidlist. Just try
Timo Sirainen <tss@iki.fi>
parents: 6878
diff changeset
780 ctx->locked = maildir_uidlist_is_locked(ctx->mbox->uidlist);
2123
e01de478882f locking fixes
Timo Sirainen <tss@iki.fi>
parents: 2121
diff changeset
781
6880
724c8f12eed2 maildir_storage_sync_force(): Don't wait on trying to lock uidlist. Just try
Timo Sirainen <tss@iki.fi>
parents: 6878
diff changeset
782 if (!ctx->mbox->syncing_commit && ctx->locked) {
5920
00c5e3cbeaf0 Moved index syncing code to its own file.
Timo Sirainen <tss@iki.fi>
parents: 5918
diff changeset
783 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
784 &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
785 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
786 }
3c8b191b0019 Adding mail to index while saving it had a race condition. Fixing it
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4199
diff changeset
787
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
788 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
789 /* 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
790 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
791 unsigned int count = 0;
0c8705aad54c Avoid infinite looping when buggy filesystems.
Timo Sirainen <tss@iki.fi>
parents: 5390
diff changeset
792
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
793 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
794 /* 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
795 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
796 (see MAILDIR_RENAME_RESCAN_COUNT). */
5391
0c8705aad54c Avoid infinite looping when buggy filesystems.
Timo Sirainen <tss@iki.fi>
parents: 5390
diff changeset
797 if (++count > MAILDIR_SCAN_DIR_MAX_COUNT)
0c8705aad54c Avoid infinite looping when buggy filesystems.
Timo Sirainen <tss@iki.fi>
parents: 5390
diff changeset
798 break;
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
799 }
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
800 if (ret < 0)
1954
2f6e137cdc44 Syncing optimizations.
Timo Sirainen <tss@iki.fi>
parents: 1947
diff changeset
801 return -1;
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
802
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
803 if (cur_changed) {
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
804 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
805 return -1;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
806 }
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
807
6027
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
808 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
809
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
810 /* 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
811 maildir_uidlist_sync_finish(ctx->uidlist_sync_ctx);
1954
2f6e137cdc44 Syncing optimizations.
Timo Sirainen <tss@iki.fi>
parents: 1947
diff changeset
812 }
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
813
6883
15df8bb27d7a If we did unlocked maildir sync, make sure we sync it later with locking.
Timo Sirainen <tss@iki.fi>
parents: 6881
diff changeset
814 if (!ctx->locked) {
15df8bb27d7a If we did unlocked maildir sync, make sure we sync it later with locking.
Timo Sirainen <tss@iki.fi>
parents: 6881
diff changeset
815 /* make sure we sync the maildir later */
15df8bb27d7a If we did unlocked maildir sync, make sure we sync it later with locking.
Timo Sirainen <tss@iki.fi>
parents: 6881
diff changeset
816 ctx->mbox->maildir_hdr.new_mtime = 0;
15df8bb27d7a If we did unlocked maildir sync, make sure we sync it later with locking.
Timo Sirainen <tss@iki.fi>
parents: 6881
diff changeset
817 ctx->mbox->maildir_hdr.cur_mtime = 0;
15df8bb27d7a If we did unlocked maildir sync, make sure we sync it later with locking.
Timo Sirainen <tss@iki.fi>
parents: 6881
diff changeset
818 }
15df8bb27d7a If we did unlocked maildir sync, make sure we sync it later with locking.
Timo Sirainen <tss@iki.fi>
parents: 6881
diff changeset
819
6880
724c8f12eed2 maildir_storage_sync_force(): Don't wait on trying to lock uidlist. Just try
Timo Sirainen <tss@iki.fi>
parents: 6878
diff changeset
820 if (!ctx->mbox->syncing_commit && ctx->locked) {
3530
e9695ec7925b Recursive maildir uidlist syncs caused assert crashes. Also did some
Timo Sirainen <tss@iki.fi>
parents: 3520
diff changeset
821 /* 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
822 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
823 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
824 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
825 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
826 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
827 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
828
3c8b191b0019 Adding mail to index while saving it had a race condition. Fixing it
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4199
diff changeset
829 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
830 return -1;
3436
3c51658d6846 We didn't notice if messages were deleted directly from new/.
Timo Sirainen <tss@iki.fi>
parents: 3435
diff changeset
831 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
832 *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
833
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
834 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
835 }
1984
9c159272f721 syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
836
6452
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
837 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
838 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
839
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
840 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
841 {
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
842 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
843 bool lost_files;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
844 int ret;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
845
6881
b1554dea8ee8 If we're doing a fast sync, try only once to lock uidlist.
Timo Sirainen <tss@iki.fi>
parents: 6880
diff changeset
846 ctx = maildir_sync_context_new(mbox, 0);
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, TRUE, &lost_files);
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
848 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
849 return ret;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
850 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
851
2322
aae574ed7f4c Broke mailbox_sync() into iterator.
Timo Sirainen <tss@iki.fi>
parents: 2320
diff changeset
852 struct mailbox_sync_context *
aae574ed7f4c Broke mailbox_sync() into iterator.
Timo Sirainen <tss@iki.fi>
parents: 2320
diff changeset
853 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
854 {
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
855 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
856 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
857 bool lost_files;
2322
aae574ed7f4c Broke mailbox_sync() into iterator.
Timo Sirainen <tss@iki.fi>
parents: 2320
diff changeset
858 int ret = 0;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
859
4894
24afafbfe47b Make sure the mailbox is opened when transaction is started (fixes deliver).
Timo Sirainen <tss@iki.fi>
parents: 4848
diff changeset
860 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
861 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
862
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
863 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
864 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
865 ioloop_time) {
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
866 mbox->ibox.sync_last_check = ioloop_time;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
867
6881
b1554dea8ee8 If we're doing a fast sync, try only once to lock uidlist.
Timo Sirainen <tss@iki.fi>
parents: 6880
diff changeset
868 ctx = maildir_sync_context_new(mbox, flags);
6452
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
869 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
870 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
871
4152
e2edd333c473 Added MAILBOX_OPEN_KEEP_LOCKED flag to mailbox opening and implemented it
Timo Sirainen <tss@iki.fi>
parents: 4126
diff changeset
872 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
873 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
874
6452
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
875 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
876 /* 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
877 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
878 }
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
879 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
880
2322
aae574ed7f4c Broke mailbox_sync() into iterator.
Timo Sirainen <tss@iki.fi>
parents: 2320
diff changeset
881 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
882 }
3472
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
883
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
884 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
885 {
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
886 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
887 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
888 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
889
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
890 t_push();
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
891 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
892 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
893
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
894 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
895 &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
896 t_pop();
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
897 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
898 }