annotate src/lib-storage/index/maildir/maildir-sync.c @ 7226:e6693a0ec8e1 HEAD

Renamed T_FRAME_BEGIN/END to T_BEGIN/END. Removed T_FRAME() macro and replaced them with T_BEGIN/END calls. T_FRAME() made it difficult to debug code with gdb.
author Timo Sirainen <tss@iki.fi>
date Mon, 11 Feb 2008 20:17:00 +0200
parents 7ed926ed7aa4
children db65d921b0e1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7086
7ed926ed7aa4 Updated copyright notices to include year 2008.
Timo Sirainen <tss@iki.fi>
parents: 7066
diff changeset
1 /* Copyright (c) 2004-2008 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);
7046
e74a1d1dca07 When doing a forced sync, we're not necessarily locked. If it takes a while,
Timo Sirainen <tss@iki.fi>
parents: 6940
diff changeset
233 if (now - ctx->last_touch > MAILDIR_LOCK_TOUCH_SECS && ctx->locked) {
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
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
5910
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
282 fname1 = maildir_uidlist_sync_get_full_filename(ctx->uidlist_sync_ctx,
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
283 fname2);
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
284 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
285
5910
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
286 path1 = t_strconcat(dir, "/", fname1, NULL);
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
287 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
288
5910
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
289 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
290 /* 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
291 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
292 return 0;
5cbabd4ccd9c Don't go fixing duplicate maildir filenames without properly checking that
Timo Sirainen <tss@iki.fi>
parents: 4238
diff changeset
293 }
5910
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
294 if (st1.st_ino == st2.st_ino &&
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
295 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
296 /* 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
297 between stat() calls, or that the files were link()ed. */
5910
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
298 if (st1.st_nlink > 1 && st2.st_nlink == st1.st_nlink &&
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
299 st1.st_ctime == st2.st_ctime &&
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
300 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
301 /* 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
302 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
303 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
304
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 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
306 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
307 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
308 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
309 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
310 happening) */
5910
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
311 if (unlink(path2) == 0)
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
312 i_warning("Unlinked a duplicate: %s", path2);
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
313 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
314 mail_storage_set_critical(
5910
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
315 &ctx->mbox->storage->storage,
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
316 "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
317 }
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 }
4397
5cbabd4ccd9c Don't go fixing duplicate maildir filenames without properly checking that
Timo Sirainen <tss@iki.fi>
parents: 4238
diff changeset
319 return 0;
5cbabd4ccd9c Don't go fixing duplicate maildir filenames without properly checking that
Timo Sirainen <tss@iki.fi>
parents: 4238
diff changeset
320 }
5cbabd4ccd9c Don't go fixing duplicate maildir filenames without properly checking that
Timo Sirainen <tss@iki.fi>
parents: 4238
diff changeset
321
5904
62ceb6b2b20d Renamed maildir_generate_tmp_filename() to maildir_filename_generate(). Also
Timo Sirainen <tss@iki.fi>
parents: 5901
diff changeset
322 new_fname = maildir_filename_generate();
5910
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
323 new_path = t_strconcat(ctx->mbox->path, "/new/", new_fname, NULL);
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
324
5910
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
325 if (rename(path2, new_path) == 0)
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
326 i_warning("Fixed a duplicate: %s -> %s", path2, new_fname);
5392
9a8402768b42 If duplicate links are found, rename() one over the other to get rid of
Timo Sirainen <tss@iki.fi>
parents: 5391
diff changeset
327 else if (errno != ENOENT) {
5910
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
328 mail_storage_set_critical(&ctx->mbox->storage->storage,
5582
1c4fd25893bd Updated error message.
Timo Sirainen <tss@iki.fi>
parents: 5564
diff changeset
329 "Couldn't fix a duplicate: rename(%s, %s) failed: %m",
5910
289f828591f7 Code cleanup
Timo Sirainen <tss@iki.fi>
parents: 5904
diff changeset
330 path2, new_path);
6940
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6933
diff changeset
331 return -1;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
332 }
6940
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6933
diff changeset
333 return 0;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
334 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
335
5926
095b3adc537b Handle mailbox deletions while syncing silently.
Timo Sirainen <tss@iki.fi>
parents: 5920
diff changeset
336 static int
095b3adc537b Handle mailbox deletions while syncing silently.
Timo Sirainen <tss@iki.fi>
parents: 5920
diff changeset
337 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
338 {
6777
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
339 int i;
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
340
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
341 for (i = 0;; i++) {
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
342 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
343 return 0;
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
344 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
345 break;
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
346
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
347 if (maildir_set_deleted(mbox))
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
348 return -1;
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
349 /* try again */
5926
095b3adc537b Handle mailbox deletions while syncing silently.
Timo Sirainen <tss@iki.fi>
parents: 5920
diff changeset
350 }
095b3adc537b Handle mailbox deletions while syncing silently.
Timo Sirainen <tss@iki.fi>
parents: 5920
diff changeset
351
095b3adc537b Handle mailbox deletions while syncing silently.
Timo Sirainen <tss@iki.fi>
parents: 5920
diff changeset
352 mail_storage_set_critical(mbox->ibox.box.storage,
095b3adc537b Handle mailbox deletions while syncing silently.
Timo Sirainen <tss@iki.fi>
parents: 5920
diff changeset
353 "stat(%s) failed: %m", path);
095b3adc537b Handle mailbox deletions while syncing silently.
Timo Sirainen <tss@iki.fi>
parents: 5920
diff changeset
354 return -1;
095b3adc537b Handle mailbox deletions while syncing silently.
Timo Sirainen <tss@iki.fi>
parents: 5920
diff changeset
355 }
095b3adc537b Handle mailbox deletions while syncing silently.
Timo Sirainen <tss@iki.fi>
parents: 5920
diff changeset
356
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
357 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
358 {
5459
78eaf595359c Removed struct index_storage abstraction. It's pointless.
Timo Sirainen <tss@iki.fi>
parents: 5443
diff changeset
359 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
360 const char *path;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
361 DIR *dirp;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
362 string_t *src, *dest;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
363 struct dirent *dp;
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
364 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
365 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
366 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
367 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
368 int ret = 1;
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
369 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
370
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
371 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
372 for (i = 0;; i++) {
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
373 dirp = opendir(path);
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
374 if (dirp != NULL)
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
375 break;
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
376
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
377 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
378 mail_storage_set_critical(storage,
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
379 "opendir(%s) failed: %m", path);
5926
095b3adc537b Handle mailbox deletions while syncing silently.
Timo Sirainen <tss@iki.fi>
parents: 5920
diff changeset
380 return -1;
095b3adc537b Handle mailbox deletions while syncing silently.
Timo Sirainen <tss@iki.fi>
parents: 5920
diff changeset
381 }
6777
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
382
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
383 if (maildir_set_deleted(ctx->mbox))
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
384 return -1;
186b164a9579 Delay creating directories until we really need them.
Timo Sirainen <tss@iki.fi>
parents: 6771
diff changeset
385 /* try again */
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
386 }
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
387
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
388 #ifdef HAVE_DIRFD
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
389 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
390 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
391 "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
392 (void)closedir(dirp);
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
393 return -1;
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 #else
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
396 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
397 (void)closedir(dirp);
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
398 return -1;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
399 }
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
400 #endif
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 now = time(NULL);
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
403 if (new_dir) {
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
404 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
405 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
406 #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
407 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
408 #else
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
409 ctx->mbox->maildir_hdr.new_mtime_nsecs = 0;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
410 #endif
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
411 } else {
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.cur_check_time = now;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
413 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
414 #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
415 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
416 #else
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
417 ctx->mbox->maildir_hdr.cur_mtime_nsecs = 0;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
418 #endif
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
419 }
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
420
2121
0840edf34f37 Locking fixes. use less memory
Timo Sirainen <tss@iki.fi>
parents: 2067
diff changeset
421 src = t_str_new(1024);
0840edf34f37 Locking fixes. use less memory
Timo Sirainen <tss@iki.fi>
parents: 2067
diff changeset
422 dest = t_str_new(1024);
0840edf34f37 Locking fixes. use less memory
Timo Sirainen <tss@iki.fi>
parents: 2067
diff changeset
423
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
424 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
425 !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
426
8bb5c7a69405 If readdir() or closedir() fails, return failure.
Timo Sirainen <tss@iki.fi>
parents: 5910
diff changeset
427 errno = 0;
8bb5c7a69405 If readdir() or closedir() fails, return failure.
Timo Sirainen <tss@iki.fi>
parents: 5910
diff changeset
428 for (; (dp = readdir(dirp)) != NULL; errno = 0) {
2258
087a43e29492 No maildir filename checking after all.
Timo Sirainen <tss@iki.fi>
parents: 2256
diff changeset
429 if (dp->d_name[0] == '.')
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
430 continue;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
431
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
432 check_touch = FALSE;
1947
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
433 flags = 0;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
434 if (move_new) {
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
435 str_truncate(src, 0);
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
436 str_truncate(dest, 0);
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
437 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
438 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
439 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
440 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
441 }
1947
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
442 if (rename(str_c(src), str_c(dest)) == 0) {
1984
9c159272f721 syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
443 /* 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
444 dir_changed = TRUE;
5914
ae731dbf3a6f Sync index changes while iterating through uidlist entries. This avoids
Timo Sirainen <tss@iki.fi>
parents: 5912
diff changeset
445 move_count++;
1947
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
446 flags |= MAILDIR_UIDLIST_REC_FLAG_MOVED |
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
447 MAILDIR_UIDLIST_REC_FLAG_RECENT;
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
448 } else if (ENOTFOUND(errno)) {
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
449 /* 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
450 dir_changed = TRUE;
5914
ae731dbf3a6f Sync index changes while iterating through uidlist entries. This avoids
Timo Sirainen <tss@iki.fi>
parents: 5912
diff changeset
451 move_count++;
6037
d911d943438e Recent flag handling rewrite. Still not perfect with maildir.
Timo Sirainen <tss@iki.fi>
parents: 6027
diff changeset
452 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
453 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
454 } 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
455 /* 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
456 leave here */
6037
d911d943438e Recent flag handling rewrite. Still not perfect with maildir.
Timo Sirainen <tss@iki.fi>
parents: 6027
diff changeset
457 flags |= MAILDIR_UIDLIST_REC_FLAG_NEW_DIR;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
458 move_new = FALSE;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
459 } else {
6037
d911d943438e Recent flag handling rewrite. Still not perfect with maildir.
Timo Sirainen <tss@iki.fi>
parents: 6027
diff changeset
460 flags |= MAILDIR_UIDLIST_REC_FLAG_NEW_DIR;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
461 mail_storage_set_critical(storage,
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
462 "rename(%s, %s) failed: %m",
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
463 str_c(src), str_c(dest));
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
464 }
5914
ae731dbf3a6f Sync index changes while iterating through uidlist entries. This avoids
Timo Sirainen <tss@iki.fi>
parents: 5912
diff changeset
465 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
466 maildir_sync_notify(ctx);
1947
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
467 } else if (new_dir) {
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
468 flags |= MAILDIR_UIDLIST_REC_FLAG_NEW_DIR |
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
469 MAILDIR_UIDLIST_REC_FLAG_RECENT;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
470 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
471
5914
ae731dbf3a6f Sync index changes while iterating through uidlist entries. This avoids
Timo Sirainen <tss@iki.fi>
parents: 5912
diff changeset
472 i++;
ae731dbf3a6f Sync index changes while iterating through uidlist entries. This avoids
Timo Sirainen <tss@iki.fi>
parents: 5912
diff changeset
473 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
474 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
475
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
476 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
477 dp->d_name, flags);
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
478 if (ret <= 0) {
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
479 if (ret < 0)
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
480 break;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
481
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
482 /* possibly duplicate - try fixing it */
7226
e6693a0ec8e1 Renamed T_FRAME_BEGIN/END to T_BEGIN/END. Removed T_FRAME() macro and
Timo Sirainen <tss@iki.fi>
parents: 7086
diff changeset
483 T_BEGIN {
6940
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6933
diff changeset
484 ret = maildir_fix_duplicate(ctx, path,
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6933
diff changeset
485 dp->d_name);
7226
e6693a0ec8e1 Renamed T_FRAME_BEGIN/END to T_BEGIN/END. Removed T_FRAME() macro and
Timo Sirainen <tss@iki.fi>
parents: 7086
diff changeset
486 } T_END;
6940
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6933
diff changeset
487 if (ret < 0)
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
488 break;
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 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
491
5911
8bb5c7a69405 If readdir() or closedir() fails, return failure.
Timo Sirainen <tss@iki.fi>
parents: 5910
diff changeset
492 if (errno != 0) {
8bb5c7a69405 If readdir() or closedir() fails, return failure.
Timo Sirainen <tss@iki.fi>
parents: 5910
diff changeset
493 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
494 "readdir(%s) failed: %m", path);
5911
8bb5c7a69405 If readdir() or closedir() fails, return failure.
Timo Sirainen <tss@iki.fi>
parents: 5910
diff changeset
495 ret = -1;
8bb5c7a69405 If readdir() or closedir() fails, return failure.
Timo Sirainen <tss@iki.fi>
parents: 5910
diff changeset
496 }
8bb5c7a69405 If readdir() or closedir() fails, return failure.
Timo Sirainen <tss@iki.fi>
parents: 5910
diff changeset
497
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
498 if (closedir(dirp) < 0) {
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
499 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
500 "closedir(%s) failed: %m", path);
5911
8bb5c7a69405 If readdir() or closedir() fails, return failure.
Timo Sirainen <tss@iki.fi>
parents: 5910
diff changeset
501 ret = -1;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
502 }
2121
0840edf34f37 Locking fixes. use less memory
Timo Sirainen <tss@iki.fi>
parents: 2067
diff changeset
503
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
504 if (dir_changed) {
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
505 if (new_dir)
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
506 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
507 else
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
508 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
509 }
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
510
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
511 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
512 (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
513 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
514
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
515 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
516 {
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
517 const void *data;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
518 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
519
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
520 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
521 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
522 return -1;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
523 }
3472
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
524
6275
913b188f4dd4 Removed explicit locking from views and maps. They were already locked all
Timo Sirainen <tss@iki.fi>
parents: 6037
diff changeset
525 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
526 &data, &data_size);
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
527 if (data_size == 0) {
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
528 /* doesn't exist */
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
529 return 0;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
530 }
4848
967de900c73a Mailbox list indexing and related changes. Currently works only with
Timo Sirainen <tss@iki.fi>
parents: 4774
diff changeset
531
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
532 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
533 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
534 else
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
535 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
536 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
537 }
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
538
7066
048325df1ec4 If MAILBOX_SYNC_FLAG_FULL_READ flag is set (SELECT and CHECK IMAP commands),
Timo Sirainen <tss@iki.fi>
parents: 7046
diff changeset
539 static int maildir_sync_quick_check(struct maildir_mailbox *mbox, bool undirty,
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
540 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
541 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
542 {
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
543 #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
544 # define DIR_NSECS_CHANGED(st, hdr, name) \
5983
272796cfee66 compiler warning fixes
Timo Sirainen <tss@iki.fi>
parents: 5950
diff changeset
545 ((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
546 #else
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
547 # 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
548 #endif
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
549
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
550 #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
551 ((hdr)->name ## _check_time <= \
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
552 (hdr)->name ## _mtime + MAILDIR_SYNC_SECS && \
7066
048325df1ec4 If MAILBOX_SYNC_FLAG_FULL_READ flag is set (SELECT and CHECK IMAP commands),
Timo Sirainen <tss@iki.fi>
parents: 7046
diff changeset
553 (undirty || \
048325df1ec4 If MAILBOX_SYNC_FLAG_FULL_READ flag is set (SELECT and CHECK IMAP commands),
Timo Sirainen <tss@iki.fi>
parents: 7046
diff changeset
554 (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
555
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
556 #define DIR_MTIME_CHANGED(st, hdr, name) \
5983
272796cfee66 compiler warning fixes
Timo Sirainen <tss@iki.fi>
parents: 5950
diff changeset
557 ((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
558 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
559
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
560 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
561 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
562 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
563
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
564 if (mbox->maildir_hdr.new_mtime == 0) {
6328
29d249982282 Error handling fixes.
Timo Sirainen <tss@iki.fi>
parents: 6275
diff changeset
565 if (maildir_header_refresh(mbox) < 0)
29d249982282 Error handling fixes.
Timo Sirainen <tss@iki.fi>
parents: 6275
diff changeset
566 return -1;
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
567 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
568 /* first sync */
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
569 *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
570 return 0;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
571 }
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
572 }
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
573
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
574 *new_changed_r = *cur_changed_r = FALSE;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
575
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
576 /* 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
577 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
578 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
579 /* refresh index and try again */
6328
29d249982282 Error handling fixes.
Timo Sirainen <tss@iki.fi>
parents: 6275
diff changeset
580 if (maildir_header_refresh(mbox) < 0)
29d249982282 Error handling fixes.
Timo Sirainen <tss@iki.fi>
parents: 6275
diff changeset
581 return -1;
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
582 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
583
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
584 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
585 *new_changed_r = TRUE;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
586 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
587 *cur_changed_r = TRUE;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
588 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
589 return 0;
1954
2f6e137cdc44 Syncing optimizations.
Timo Sirainen <tss@iki.fi>
parents: 1947
diff changeset
590 }
2f6e137cdc44 Syncing optimizations.
Timo Sirainen <tss@iki.fi>
parents: 1947
diff changeset
591
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
592 if (!*new_changed_r) {
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
593 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
594 return -1;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
595 check_new = TRUE;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
596 }
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
597 if (!*cur_changed_r) {
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
598 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
599 return -1;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
600 check_cur = TRUE;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
601 }
1954
2f6e137cdc44 Syncing optimizations.
Timo Sirainen <tss@iki.fi>
parents: 1947
diff changeset
602
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
603 for (;;) {
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
604 if (check_new)
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
605 *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
606 if (check_cur)
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
607 *cur_changed_r = DIR_MTIME_CHANGED(cur_st, hdr, cur);
1984
9c159272f721 syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 1978
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 && !*cur_changed_r) || refreshed)
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
610 break;
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
611
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
612 /* refresh index and try again */
6328
29d249982282 Error handling fixes.
Timo Sirainen <tss@iki.fi>
parents: 6275
diff changeset
613 if (maildir_header_refresh(mbox) < 0)
29d249982282 Error handling fixes.
Timo Sirainen <tss@iki.fi>
parents: 6275
diff changeset
614 return -1;
5927
b9865213da42 Store syncing information to maildir extension header instead of kludging
Timo Sirainen <tss@iki.fi>
parents: 5926
diff changeset
615 refreshed = TRUE;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
616 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
617
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
618 return 0;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
619 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
620
6027
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
621 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
622 {
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
623 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
624 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
625
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
626 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
627 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
628 return;
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
629
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
630 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
631 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
632
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
633 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
634 /* 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
635 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
636 deleted. */
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
637 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
638 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
639 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
640 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
641 }
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
6037
d911d943438e Recent flag handling rewrite. Still not perfect with maildir.
Timo Sirainen <tss@iki.fi>
parents: 6027
diff changeset
644 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
645 {
d911d943438e Recent flag handling rewrite. Still not perfect with maildir.
Timo Sirainen <tss@iki.fi>
parents: 6027
diff changeset
646 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
647
d911d943438e Recent flag handling rewrite. Still not perfect with maildir.
Timo Sirainen <tss@iki.fi>
parents: 6027
diff changeset
648 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
649 return FALSE;
d911d943438e Recent flag handling rewrite. Still not perfect with maildir.
Timo Sirainen <tss@iki.fi>
parents: 6027
diff changeset
650
6859
64e0f45e8dc3 Delayed NFS attribute cache flushing had some bug, so removed it for now.
Timo Sirainen <tss@iki.fi>
parents: 6800
diff changeset
651 (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
652
d911d943438e Recent flag handling rewrite. Still not perfect with maildir.
Timo Sirainen <tss@iki.fi>
parents: 6027
diff changeset
653 /* 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
654 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
655 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
656 return hdr->first_recent_uid <
d911d943438e Recent flag handling rewrite. Still not perfect with maildir.
Timo Sirainen <tss@iki.fi>
parents: 6027
diff changeset
657 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
658 }
d911d943438e Recent flag handling rewrite. Still not perfect with maildir.
Timo Sirainen <tss@iki.fi>
parents: 6027
diff changeset
659
6452
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
660 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
661 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
662 {
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
663 enum mail_index_sync_flags flags = 0;
7066
048325df1ec4 If MAILBOX_SYNC_FLAG_FULL_READ flag is set (SELECT and CHECK IMAP commands),
Timo Sirainen <tss@iki.fi>
parents: 7046
diff changeset
664 bool undirty = (ctx->flags & MAILBOX_SYNC_FLAG_FULL_READ) != 0;
6452
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
665
7066
048325df1ec4 If MAILBOX_SYNC_FLAG_FULL_READ flag is set (SELECT and CHECK IMAP commands),
Timo Sirainen <tss@iki.fi>
parents: 7046
diff changeset
666 if (maildir_sync_quick_check(ctx->mbox, undirty,
048325df1ec4 If MAILBOX_SYNC_FLAG_FULL_READ flag is set (SELECT and CHECK IMAP commands),
Timo Sirainen <tss@iki.fi>
parents: 7046
diff changeset
667 ctx->new_dir, ctx->cur_dir,
6452
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
668 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
669 return -1;
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
670
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
671 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
672 return 1;
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
673
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
674 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
675 *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
676 return 1;
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
677 }
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
678
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
679 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
680 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
681
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
682 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
683 }
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
684
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
685 static int maildir_sync_context(struct maildir_sync_context *ctx, bool forced,
6886
82420ed23379 If doing a lockless forced sync, track the UID we're trying to find. If we
Timo Sirainen <tss@iki.fi>
parents: 6884
diff changeset
686 uint32_t *find_uid, bool *lost_files_r)
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
687 {
6878
0c3ab0aef33c maildir_uidlist_sync_init() was called wrong, although it just happened to
Timo Sirainen <tss@iki.fi>
parents: 6859
diff changeset
688 enum maildir_uidlist_sync_flags sync_flags;
6886
82420ed23379 If doing a lockless forced sync, track the UID we're trying to find. If we
Timo Sirainen <tss@iki.fi>
parents: 6884
diff changeset
689 enum maildir_uidlist_rec_flag flags;
6452
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
690 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
691 int ret;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
692
6452
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
693 *lost_files_r = FALSE;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
694
6452
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
695 if (forced)
2140
e2cd51b99359 "readonly sync" -> "forced sync"
Timo Sirainen <tss@iki.fi>
parents: 2123
diff changeset
696 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
697 else {
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
698 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
699 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
700 return ret;
2140
e2cd51b99359 "readonly sync" -> "forced sync"
Timo Sirainen <tss@iki.fi>
parents: 2123
diff changeset
701 }
1947
777da553d1d3 Recent-flag should work now
Timo Sirainen <tss@iki.fi>
parents: 1944
diff changeset
702
2818
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
703 /*
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
704 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
705
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
706 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
707 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
708
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
709 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
710 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
711
2818
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
712 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
713 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
714 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
715 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
716
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
717 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
718 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
719 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
720
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
721 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
722 -- 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
723 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
724 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
725 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
726 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
727
2818
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
728 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
729 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
730 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
731
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
732 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
733 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
734 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
735 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
736 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
737 already done.
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
738
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
739 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
740 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
741 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
742
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
743 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
744 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
745 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
746 */
a758a5b542bb Always protect maildir syncing with uidlist lock. Before we only tried to
Timo Sirainen <tss@iki.fi>
parents: 2816
diff changeset
747
6886
82420ed23379 If doing a lockless forced sync, track the UID we're trying to find. If we
Timo Sirainen <tss@iki.fi>
parents: 6884
diff changeset
748 if (!cur_changed) {
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
749 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
750 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
751 } 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
752 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
753 sync_flags = 0;
6886
82420ed23379 If doing a lockless forced sync, track the UID we're trying to find. If we
Timo Sirainen <tss@iki.fi>
parents: 6884
diff changeset
754 if (forced)
82420ed23379 If doing a lockless forced sync, track the UID we're trying to find. If we
Timo Sirainen <tss@iki.fi>
parents: 6884
diff changeset
755 sync_flags |= MAILDIR_UIDLIST_SYNC_FORCE;
6881
b1554dea8ee8 If we're doing a fast sync, try only once to lock uidlist.
Timo Sirainen <tss@iki.fi>
parents: 6880
diff changeset
756 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
757 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
758 }
6878
0c3ab0aef33c maildir_uidlist_sync_init() was called wrong, although it just happened to
Timo Sirainen <tss@iki.fi>
parents: 6859
diff changeset
759 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
760 &ctx->uidlist_sync_ctx);
e9695ec7925b Recursive maildir uidlist syncs caused assert crashes. Also did some
Timo Sirainen <tss@iki.fi>
parents: 3520
diff changeset
761 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
762 /* 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
763 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
764 return ret;
2140
e2cd51b99359 "readonly sync" -> "forced sync"
Timo Sirainen <tss@iki.fi>
parents: 2123
diff changeset
765 }
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
766 ctx->locked = maildir_uidlist_is_locked(ctx->mbox->uidlist);
6886
82420ed23379 If doing a lockless forced sync, track the UID we're trying to find. If we
Timo Sirainen <tss@iki.fi>
parents: 6884
diff changeset
767 if (!ctx->locked)
82420ed23379 If doing a lockless forced sync, track the UID we're trying to find. If we
Timo Sirainen <tss@iki.fi>
parents: 6884
diff changeset
768 ctx->partial = TRUE;
2123
e01de478882f locking fixes
Timo Sirainen <tss@iki.fi>
parents: 2121
diff changeset
769
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
770 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
771 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
772 &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
773 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
774 }
3c8b191b0019 Adding mail to index while saving it had a race condition. Fixing it
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4199
diff changeset
775
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
776 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
777 /* 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
778 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
779 unsigned int count = 0;
0c8705aad54c Avoid infinite looping when buggy filesystems.
Timo Sirainen <tss@iki.fi>
parents: 5390
diff changeset
780
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
781 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
782 /* 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
783 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
784 (see MAILDIR_RENAME_RESCAN_COUNT). */
5391
0c8705aad54c Avoid infinite looping when buggy filesystems.
Timo Sirainen <tss@iki.fi>
parents: 5390
diff changeset
785 if (++count > MAILDIR_SCAN_DIR_MAX_COUNT)
0c8705aad54c Avoid infinite looping when buggy filesystems.
Timo Sirainen <tss@iki.fi>
parents: 5390
diff changeset
786 break;
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
787 }
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
788 if (ret < 0)
1954
2f6e137cdc44 Syncing optimizations.
Timo Sirainen <tss@iki.fi>
parents: 1947
diff changeset
789 return -1;
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
790
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
791 if (cur_changed) {
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
792 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
793 return -1;
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
794 }
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
795
6027
3b260c6d9207 If uidlist gets deleted but indexes not, give UIDs to messages beginning
Timo Sirainen <tss@iki.fi>
parents: 5983
diff changeset
796 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
797
3447
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
798 /* 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
799 maildir_uidlist_sync_finish(ctx->uidlist_sync_ctx);
1954
2f6e137cdc44 Syncing optimizations.
Timo Sirainen <tss@iki.fi>
parents: 1947
diff changeset
800 }
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
801
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
802 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
803 /* 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
804 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
805 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
806 }
15df8bb27d7a If we did unlocked maildir sync, make sure we sync it later with locking.
Timo Sirainen <tss@iki.fi>
parents: 6881
diff changeset
807
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
808 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
809 /* 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
810 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
811 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
812 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
813 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
814 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
815 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
816
3c8b191b0019 Adding mail to index while saving it had a race condition. Fixing it
Timo Sirainen <timo.sirainen@movial.fi>
parents: 4199
diff changeset
817 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
818 return -1;
3436
3c51658d6846 We didn't notice if messages were deleted directly from new/.
Timo Sirainen <tss@iki.fi>
parents: 3435
diff changeset
819 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
820 *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
821
96de02ea7482 Keywords are stored in maildir filename and maildir-keywords file
Timo Sirainen <tss@iki.fi>
parents: 3446
diff changeset
822 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
823 }
1984
9c159272f721 syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
824
6886
82420ed23379 If doing a lockless forced sync, track the UID we're trying to find. If we
Timo Sirainen <tss@iki.fi>
parents: 6884
diff changeset
825 if (find_uid != NULL && *find_uid != 0) {
6933
ad16a4582033 If file is lost, don't try to look for it until stack overflows.
Timo Sirainen <tss@iki.fi>
parents: 6886
diff changeset
826 if (maildir_uidlist_lookup_nosync(ctx->mbox->uidlist, *find_uid,
ad16a4582033 If file is lost, don't try to look for it until stack overflows.
Timo Sirainen <tss@iki.fi>
parents: 6886
diff changeset
827 &flags) == NULL) {
6886
82420ed23379 If doing a lockless forced sync, track the UID we're trying to find. If we
Timo Sirainen <tss@iki.fi>
parents: 6884
diff changeset
828 /* UID is expunged */
82420ed23379 If doing a lockless forced sync, track the UID we're trying to find. If we
Timo Sirainen <tss@iki.fi>
parents: 6884
diff changeset
829 *find_uid = 0;
82420ed23379 If doing a lockless forced sync, track the UID we're trying to find. If we
Timo Sirainen <tss@iki.fi>
parents: 6884
diff changeset
830 } else if ((flags & MAILDIR_UIDLIST_REC_FLAG_NONSYNCED) == 0) {
82420ed23379 If doing a lockless forced sync, track the UID we're trying to find. If we
Timo Sirainen <tss@iki.fi>
parents: 6884
diff changeset
831 /* we didn't find it, possibly expunged? */
82420ed23379 If doing a lockless forced sync, track the UID we're trying to find. If we
Timo Sirainen <tss@iki.fi>
parents: 6884
diff changeset
832 *find_uid = 0;
82420ed23379 If doing a lockless forced sync, track the UID we're trying to find. If we
Timo Sirainen <tss@iki.fi>
parents: 6884
diff changeset
833 }
82420ed23379 If doing a lockless forced sync, track the UID we're trying to find. If we
Timo Sirainen <tss@iki.fi>
parents: 6884
diff changeset
834 }
82420ed23379 If doing a lockless forced sync, track the UID we're trying to find. If we
Timo Sirainen <tss@iki.fi>
parents: 6884
diff changeset
835
6452
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
836 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
837 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
838
6886
82420ed23379 If doing a lockless forced sync, track the UID we're trying to find. If we
Timo Sirainen <tss@iki.fi>
parents: 6884
diff changeset
839 int maildir_storage_sync_force(struct maildir_mailbox *mbox, uint32_t uid)
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
840 {
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
841 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
842 bool lost_files;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
843 int ret;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
844
7226
e6693a0ec8e1 Renamed T_FRAME_BEGIN/END to T_BEGIN/END. Removed T_FRAME() macro and
Timo Sirainen <tss@iki.fi>
parents: 7086
diff changeset
845 T_BEGIN {
6940
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6933
diff changeset
846 ctx = maildir_sync_context_new(mbox, MAILBOX_SYNC_FLAG_FAST);
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6933
diff changeset
847 ret = maildir_sync_context(ctx, TRUE, &uid, &lost_files);
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6933
diff changeset
848 maildir_sync_deinit(ctx);
7226
e6693a0ec8e1 Renamed T_FRAME_BEGIN/END to T_BEGIN/END. Removed T_FRAME() macro and
Timo Sirainen <tss@iki.fi>
parents: 7086
diff changeset
849 } T_END;
6886
82420ed23379 If doing a lockless forced sync, track the UID we're trying to find. If we
Timo Sirainen <tss@iki.fi>
parents: 6884
diff changeset
850
82420ed23379 If doing a lockless forced sync, track the UID we're trying to find. If we
Timo Sirainen <tss@iki.fi>
parents: 6884
diff changeset
851 if (uid != 0) {
82420ed23379 If doing a lockless forced sync, track the UID we're trying to find. If we
Timo Sirainen <tss@iki.fi>
parents: 6884
diff changeset
852 /* maybe it's expunged. check again. */
7226
e6693a0ec8e1 Renamed T_FRAME_BEGIN/END to T_BEGIN/END. Removed T_FRAME() macro and
Timo Sirainen <tss@iki.fi>
parents: 7086
diff changeset
853 T_BEGIN {
6940
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6933
diff changeset
854 ctx = maildir_sync_context_new(mbox, 0);
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6933
diff changeset
855 ret = maildir_sync_context(ctx, TRUE, NULL,
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6933
diff changeset
856 &lost_files);
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6933
diff changeset
857 maildir_sync_deinit(ctx);
7226
e6693a0ec8e1 Renamed T_FRAME_BEGIN/END to T_BEGIN/END. Removed T_FRAME() macro and
Timo Sirainen <tss@iki.fi>
parents: 7086
diff changeset
858 } T_END;
6886
82420ed23379 If doing a lockless forced sync, track the UID we're trying to find. If we
Timo Sirainen <tss@iki.fi>
parents: 6884
diff changeset
859 }
6452
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
860 return ret;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
861 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
862
2322
aae574ed7f4c Broke mailbox_sync() into iterator.
Timo Sirainen <tss@iki.fi>
parents: 2320
diff changeset
863 struct mailbox_sync_context *
aae574ed7f4c Broke mailbox_sync() into iterator.
Timo Sirainen <tss@iki.fi>
parents: 2320
diff changeset
864 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
865 {
3279
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
866 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
867 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
868 bool lost_files;
2322
aae574ed7f4c Broke mailbox_sync() into iterator.
Timo Sirainen <tss@iki.fi>
parents: 2320
diff changeset
869 int ret = 0;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
870
4894
24afafbfe47b Make sure the mailbox is opened when transaction is started (fixes deliver).
Timo Sirainen <tss@iki.fi>
parents: 4848
diff changeset
871 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
872 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
873
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
874 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
875 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
876 ioloop_time) {
b698ae839a18 Moved mbox/maildir-specific variables from struct index_mailbox to
Timo Sirainen <tss@iki.fi>
parents: 3203
diff changeset
877 mbox->ibox.sync_last_check = ioloop_time;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
878
7226
e6693a0ec8e1 Renamed T_FRAME_BEGIN/END to T_BEGIN/END. Removed T_FRAME() macro and
Timo Sirainen <tss@iki.fi>
parents: 7086
diff changeset
879 T_BEGIN {
6940
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6933
diff changeset
880 ctx = maildir_sync_context_new(mbox, flags);
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6933
diff changeset
881 ret = maildir_sync_context(ctx, FALSE, NULL,
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6933
diff changeset
882 &lost_files);
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6933
diff changeset
883 maildir_sync_deinit(ctx);
7226
e6693a0ec8e1 Renamed T_FRAME_BEGIN/END to T_BEGIN/END. Removed T_FRAME() macro and
Timo Sirainen <tss@iki.fi>
parents: 7086
diff changeset
884 } T_END;
3436
3c51658d6846 We didn't notice if messages were deleted directly from new/.
Timo Sirainen <tss@iki.fi>
parents: 3435
diff changeset
885
4152
e2edd333c473 Added MAILBOX_OPEN_KEEP_LOCKED flag to mailbox opening and implemented it
Timo Sirainen <tss@iki.fi>
parents: 4126
diff changeset
886 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
887 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
888
6452
a62923d3c969 Don't sync mailbox immediately when committing transactions. When it's done
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
889 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
890 /* lost some files from new/, see if thery're in cur/ */
6886
82420ed23379 If doing a lockless forced sync, track the UID we're trying to find. If we
Timo Sirainen <tss@iki.fi>
parents: 6884
diff changeset
891 ret = maildir_storage_sync_force(mbox, 0);
3436
3c51658d6846 We didn't notice if messages were deleted directly from new/.
Timo Sirainen <tss@iki.fi>
parents: 3435
diff changeset
892 }
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
893 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
894
2322
aae574ed7f4c Broke mailbox_sync() into iterator.
Timo Sirainen <tss@iki.fi>
parents: 2320
diff changeset
895 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
896 }
3472
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
897
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
898 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
899 {
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
900 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
901 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
902
7226
e6693a0ec8e1 Renamed T_FRAME_BEGIN/END to T_BEGIN/END. Removed T_FRAME() macro and
Timo Sirainen <tss@iki.fi>
parents: 7086
diff changeset
903 T_BEGIN {
6940
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6933
diff changeset
904 const char *new_dir, *cur_dir;
3472
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
905
6940
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6933
diff changeset
906 new_dir = t_strconcat(mbox->path, "/new", NULL);
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6933
diff changeset
907 cur_dir = t_strconcat(mbox->path, "/cur", NULL);
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6933
diff changeset
908
7066
048325df1ec4 If MAILBOX_SYNC_FLAG_FULL_READ flag is set (SELECT and CHECK IMAP commands),
Timo Sirainen <tss@iki.fi>
parents: 7046
diff changeset
909 ret = maildir_sync_quick_check(mbox, FALSE, new_dir, cur_dir,
6940
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6933
diff changeset
910 &new_changed, &cur_changed);
7226
e6693a0ec8e1 Renamed T_FRAME_BEGIN/END to T_BEGIN/END. Removed T_FRAME() macro and
Timo Sirainen <tss@iki.fi>
parents: 7086
diff changeset
911 } T_END;
3472
db29cc6754d5 Store new/ directory's timestamp in sync_size header in index (kludgy..).
Timo Sirainen <tss@iki.fi>
parents: 3470
diff changeset
912 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
913 }