Mercurial > dovecot > core-2.2
annotate src/dsync/dsync-brain.c @ 9644:16849c0931be HEAD
dsync: Handle missing mail GUIDs.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Fri, 17 Jul 2009 20:11:05 -0400 |
parents | fb91695ec0bf |
children | 4d5cc6ce68aa |
rev | line source |
---|---|
9559 | 1 /* Copyright (c) 2009 Dovecot authors, see the included COPYING file */ |
2 | |
3 #include "lib.h" | |
4 #include "array.h" | |
5 #include "hash.h" | |
9638
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
6 #include "hex-binary.h" |
9559 | 7 #include "master-service.h" |
8 #include "dsync-worker.h" | |
9 #include "dsync-brain-private.h" | |
10 | |
11 static void | |
12 dsync_brain_mailbox_list_deinit(struct dsync_brain_mailbox_list **list); | |
13 static void dsync_brain_msg_sync_deinit(struct dsync_brain_mailbox_sync **sync); | |
14 | |
15 struct dsync_brain *dsync_brain_init(struct dsync_worker *src_worker, | |
16 struct dsync_worker *dest_worker) | |
17 { | |
18 struct dsync_brain *brain; | |
19 | |
20 brain = i_new(struct dsync_brain, 1); | |
21 brain->src_worker = src_worker; | |
22 brain->dest_worker = dest_worker; | |
23 return brain; | |
24 } | |
25 | |
26 static void dsync_brain_fail(struct dsync_brain *brain) | |
27 { | |
28 brain->failed = TRUE; | |
29 master_service_stop(master_service); | |
30 } | |
31 | |
32 int dsync_brain_deinit(struct dsync_brain **_brain) | |
33 { | |
34 struct dsync_brain *brain = *_brain; | |
35 int ret = brain->failed ? -1 : 0; | |
36 | |
37 if (brain->mailbox_sync != NULL) | |
38 dsync_brain_msg_sync_deinit(&brain->mailbox_sync); | |
39 if (brain->src_mailbox_list != NULL) | |
40 dsync_brain_mailbox_list_deinit(&brain->src_mailbox_list); | |
41 if (brain->dest_mailbox_list != NULL) | |
42 dsync_brain_mailbox_list_deinit(&brain->dest_mailbox_list); | |
43 | |
44 *_brain = NULL; | |
45 i_free(brain); | |
46 return ret; | |
47 } | |
48 | |
49 static void dsync_brain_mailbox_list_finished(struct dsync_brain *brain) | |
50 { | |
51 if (brain->src_mailbox_list->iter != NULL || | |
52 brain->dest_mailbox_list->iter != NULL) | |
53 return; | |
54 | |
55 /* both lists are finished */ | |
56 brain->state++; | |
57 dsync_brain_sync(brain); | |
58 } | |
59 | |
60 static void dsync_worker_mailbox_input(void *context) | |
61 { | |
62 struct dsync_brain_mailbox_list *list = context; | |
63 struct dsync_mailbox dsync_box, *dup_box; | |
64 int ret; | |
65 | |
66 while ((ret = dsync_worker_mailbox_iter_next(list->iter, | |
67 &dsync_box)) > 0) { | |
68 dup_box = dsync_mailbox_dup(list->pool, &dsync_box); | |
69 array_append(&list->mailboxes, &dup_box, 1); | |
70 } | |
71 if (ret < 0) { | |
72 /* finished listing mailboxes */ | |
73 if (dsync_worker_mailbox_iter_deinit(&list->iter) < 0) | |
74 dsync_brain_fail(list->brain); | |
75 array_sort(&list->mailboxes, dsync_mailbox_p_guid_cmp); | |
76 dsync_brain_mailbox_list_finished(list->brain); | |
77 } | |
78 } | |
79 | |
80 static struct dsync_brain_mailbox_list * | |
81 dsync_brain_mailbox_list_init(struct dsync_brain *brain, | |
82 struct dsync_worker *worker) | |
83 { | |
84 struct dsync_brain_mailbox_list *list; | |
85 pool_t pool; | |
86 | |
87 pool = pool_alloconly_create("dsync brain mailbox list", 10240); | |
88 list = p_new(pool, struct dsync_brain_mailbox_list, 1); | |
89 list->pool = pool; | |
90 list->brain = brain; | |
91 list->worker = worker; | |
92 list->iter = dsync_worker_mailbox_iter_init(worker); | |
93 p_array_init(&list->mailboxes, pool, 128); | |
94 dsync_worker_set_input_callback(worker, dsync_worker_mailbox_input, | |
95 list); | |
96 return list; | |
97 } | |
98 | |
99 static void | |
100 dsync_brain_mailbox_list_deinit(struct dsync_brain_mailbox_list **_list) | |
101 { | |
102 struct dsync_brain_mailbox_list *list = *_list; | |
103 | |
104 *_list = NULL; | |
105 | |
106 if (list->iter != NULL) | |
107 (void)dsync_worker_mailbox_iter_deinit(&list->iter); | |
108 pool_unref(&list->pool); | |
109 } | |
110 | |
111 static void dsync_brain_create_missing_mailboxes(struct dsync_brain *brain) | |
112 { | |
113 struct dsync_mailbox *const *src_boxes, *const *dest_boxes, new_box; | |
114 unsigned int src, dest, src_count, dest_count; | |
115 int ret; | |
116 | |
117 /* FIXME: handle different hierarchy separators? */ | |
118 | |
119 memset(&new_box, 0, sizeof(new_box)); | |
120 | |
121 /* find mailboxes from source whose GUIDs don't exist in dest. | |
122 the mailboxes are sorted by GUID, so we can do this quickly. */ | |
123 src_boxes = array_get(&brain->src_mailbox_list->mailboxes, &src_count); | |
124 dest_boxes = array_get(&brain->dest_mailbox_list->mailboxes, &dest_count); | |
125 for (src = dest = 0; src < src_count && dest < dest_count; ) { | |
126 ret = dsync_mailbox_guid_cmp(src_boxes[src], dest_boxes[dest]); | |
127 if (ret == 0) { | |
128 src++; dest++; | |
129 } else if (ret < 0) { | |
130 /* exists only in source */ | |
131 new_box = *src_boxes[src]; | |
132 new_box.uid_next = 0; | |
133 new_box.highest_modseq = 0; | |
134 dsync_worker_create_mailbox(brain->dest_worker, | |
135 &new_box); | |
136 src++; | |
137 } else { | |
138 /* exists only in dest */ | |
139 dest++; | |
140 } | |
141 } | |
142 for (; src < src_count; src++) { | |
143 new_box = *src_boxes[src]; | |
144 new_box.uid_next = 0; | |
145 new_box.highest_modseq = 0; | |
146 dsync_worker_create_mailbox(brain->dest_worker, &new_box); | |
147 } | |
148 } | |
149 | |
150 static void dsync_brain_guid_add(struct dsync_brain_mailbox_sync *sync, | |
151 struct dsync_brain_msg_iter *iter) | |
152 { | |
153 struct dsync_brain_guid_instance *inst, *prev_inst; | |
154 | |
155 inst = p_new(sync->pool, struct dsync_brain_guid_instance, 1); | |
156 inst->mailbox_idx = iter->mailbox_idx; | |
157 inst->uid = iter->msg.uid; | |
158 | |
159 prev_inst = hash_table_lookup(sync->guid_hash, iter->msg.guid); | |
160 if (prev_inst == NULL) { | |
161 hash_table_insert(sync->guid_hash, | |
162 p_strdup(sync->pool, iter->msg.guid), inst); | |
163 } else { | |
164 inst->next = prev_inst->next; | |
165 prev_inst->next = inst; | |
166 } | |
167 } | |
168 | |
169 static int dsync_brain_msg_iter_next(struct dsync_brain_msg_iter *iter) | |
170 { | |
171 int ret = 1; | |
172 | |
173 if (iter->msg.guid == NULL) { | |
174 ret = dsync_worker_msg_iter_next(iter->iter, &iter->mailbox_idx, | |
175 &iter->msg); | |
176 if (ret > 0) { | |
177 if (iter->save_guids) | |
178 dsync_brain_guid_add(iter->sync, iter); | |
179 } | |
180 } | |
181 | |
182 if (iter->wanted_mailbox_idx != iter->mailbox_idx) { | |
183 /* finished with this mailbox */ | |
184 return -1; | |
185 } | |
186 return ret; | |
187 } | |
188 | |
189 static int dsync_brain_msg_iter_next_pair(struct dsync_brain_mailbox_sync *sync) | |
190 { | |
191 int ret; | |
192 | |
193 if ((ret = dsync_brain_msg_iter_next(sync->src_msg_iter)) <= 0) | |
194 return ret; | |
195 if ((ret = dsync_brain_msg_iter_next(sync->dest_msg_iter)) <= 0) | |
196 return ret; | |
197 return 1; | |
198 } | |
199 | |
200 static void | |
201 dsync_brain_msg_sync_save_source(struct dsync_brain_mailbox_sync *sync) | |
202 { | |
203 struct dsync_brain_new_msg *new_msg; | |
204 | |
205 new_msg = array_append_space(&sync->new_msgs); | |
206 new_msg->mailbox_idx = sync->src_msg_iter->mailbox_idx; | |
207 new_msg->msg = dsync_message_dup(sync->pool, &sync->src_msg_iter->msg); | |
208 } | |
209 | |
210 static void dsync_brain_msg_sync_existing(struct dsync_brain *brain, | |
211 struct dsync_message *src_msg, | |
212 struct dsync_message *dest_msg) | |
213 { | |
214 if (src_msg->flags != dest_msg->flags || | |
215 src_msg->modseq > dest_msg->modseq || | |
216 !dsync_keyword_list_equals(src_msg->keywords, dest_msg->keywords)) | |
217 dsync_worker_msg_update_metadata(brain->dest_worker, src_msg); | |
218 } | |
219 | |
9638
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
220 static const char * |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
221 get_guid_128_str(const char *guid, unsigned char *dest, unsigned int dest_len) |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
222 { |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
223 uint8_t guid_128[MAIL_GUID_128_SIZE]; |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
224 buffer_t guid_128_buf; |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
225 |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
226 buffer_create_data(&guid_128_buf, dest, dest_len); |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
227 mail_generate_guid_128_hash(guid, guid_128); |
9644
16849c0931be
dsync: Handle missing mail GUIDs.
Timo Sirainen <tss@iki.fi>
parents:
9642
diff
changeset
|
228 if (mail_guid_128_is_empty(guid_128)) |
16849c0931be
dsync: Handle missing mail GUIDs.
Timo Sirainen <tss@iki.fi>
parents:
9642
diff
changeset
|
229 return ""; |
9638
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
230 binary_to_hex_append(&guid_128_buf, guid_128, sizeof(guid_128)); |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
231 buffer_append_c(&guid_128_buf, '\0'); |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
232 return guid_128_buf.data; |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
233 } |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
234 |
9559 | 235 static int dsync_brain_msg_sync_pair(struct dsync_brain_mailbox_sync *sync) |
236 { | |
237 struct dsync_message *src_msg = &sync->src_msg_iter->msg; | |
238 struct dsync_message *dest_msg = &sync->dest_msg_iter->msg; | |
9592
7b0bcf423fca
dsync: Fixed uid conflict handling.
Timo Sirainen <tss@iki.fi>
parents:
9559
diff
changeset
|
239 struct dsync_mailbox *const *boxp; |
9559 | 240 struct dsync_brain_uid_conflict *conflict; |
9638
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
241 const char *src_guid, *dest_guid; |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
242 unsigned char guid_128_data[MAIL_GUID_128_SIZE * 2 + 1]; |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
243 bool src_expunged, dest_expunged; |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
244 |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
245 src_expunged = (src_msg->flags & DSYNC_MAIL_FLAG_EXPUNGED) != 0; |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
246 dest_expunged = (dest_msg->flags & DSYNC_MAIL_FLAG_EXPUNGED) != 0; |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
247 |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
248 if (src_expunged) { |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
249 src_guid = src_msg->guid; |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
250 dest_guid = get_guid_128_str(dest_msg->guid, guid_128_data, |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
251 sizeof(guid_128_data)); |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
252 } else if (dest_expunged) { |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
253 src_guid = get_guid_128_str(src_msg->guid, guid_128_data, |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
254 sizeof(guid_128_data)); |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
255 dest_guid = dest_msg->guid; |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
256 } else { |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
257 src_guid = src_msg->guid; |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
258 dest_guid = dest_msg->guid; |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
259 } |
9559 | 260 |
261 if (src_msg->uid < dest_msg->uid) { | |
262 /* message has been expunged from dest. ignore it, unless | |
263 we're in uid-conflict mode. */ | |
9638
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
264 if (sync->uid_conflict && !src_expunged) |
9559 | 265 dsync_brain_msg_sync_save_source(sync); |
266 src_msg->guid = NULL; | |
9638
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
267 return 0; |
9559 | 268 } else if (src_msg->uid > dest_msg->uid) { |
269 /* message has been expunged from src. expunge it from dest | |
270 too, unless we're in uid-conflict mode. */ | |
9638
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
271 if (!sync->uid_conflict && !dest_expunged) { |
9559 | 272 dsync_worker_msg_expunge(sync->brain->dest_worker, |
273 dest_msg->uid); | |
274 } | |
275 dest_msg->guid = NULL; | |
9638
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
276 return 0; |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
277 } |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
278 |
9644
16849c0931be
dsync: Handle missing mail GUIDs.
Timo Sirainen <tss@iki.fi>
parents:
9642
diff
changeset
|
279 /* UIDs match, but do GUIDs? If either of the GUIDs aren't set, it |
16849c0931be
dsync: Handle missing mail GUIDs.
Timo Sirainen <tss@iki.fi>
parents:
9642
diff
changeset
|
280 means that either the storage doesn't support GUIDs or we're |
16849c0931be
dsync: Handle missing mail GUIDs.
Timo Sirainen <tss@iki.fi>
parents:
9642
diff
changeset
|
281 handling an old-style expunge record. In that case just assume |
16849c0931be
dsync: Handle missing mail GUIDs.
Timo Sirainen <tss@iki.fi>
parents:
9642
diff
changeset
|
282 they match. */ |
16849c0931be
dsync: Handle missing mail GUIDs.
Timo Sirainen <tss@iki.fi>
parents:
9642
diff
changeset
|
283 if (strcmp(src_guid, dest_guid) != 0 && |
16849c0931be
dsync: Handle missing mail GUIDs.
Timo Sirainen <tss@iki.fi>
parents:
9642
diff
changeset
|
284 *src_guid != '\0' && *dest_guid != '\0') { |
9638
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
285 /* UID conflict. give new UIDs to messages in both src and |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
286 dest (if they're not expunged already) */ |
9559 | 287 sync->uid_conflict = TRUE; |
9638
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
288 if (!dest_expunged) { |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
289 conflict = array_append_space(&sync->uid_conflicts); |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
290 conflict->mailbox_idx = sync->src_msg_iter->mailbox_idx; |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
291 conflict->uid = dest_msg->uid; |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
292 } |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
293 if (!src_expunged) { |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
294 boxp = array_idx(&sync->brain->src_mailbox_list->mailboxes, |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
295 conflict->mailbox_idx); |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
296 src_msg->uid = (*boxp)->uid_next++; |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
297 dsync_brain_msg_sync_save_source(sync); |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
298 } |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
299 } else if (dest_expunged) { |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
300 /* message expunged from destination, we can skip this. */ |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
301 } else if (src_expunged) { |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
302 /* message expunged from source, expunge from destination too */ |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
303 dsync_worker_msg_expunge(sync->brain->dest_worker, |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
304 dest_msg->uid); |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
305 } else { |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
306 /* message exists in both source and dest, sync metadata */ |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
307 dsync_brain_msg_sync_existing(sync->brain, src_msg, dest_msg); |
9559 | 308 } |
9638
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
309 src_msg->guid = NULL; |
b11a3eda2477
dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents:
9592
diff
changeset
|
310 dest_msg->guid = NULL; |
9559 | 311 return 0; |
312 } | |
313 | |
314 static bool | |
315 dsync_brain_msg_sync_mailbox_more(struct dsync_brain_mailbox_sync *sync) | |
316 { | |
317 struct dsync_mailbox *const *boxp; | |
318 int ret; | |
319 | |
320 while ((ret = dsync_brain_msg_iter_next_pair(sync)) > 0) { | |
321 if (dsync_brain_msg_sync_pair(sync) < 0) | |
322 break; | |
323 if (dsync_worker_is_output_full(sync->brain->dest_worker)) | |
324 return FALSE; | |
325 } | |
326 if (ret == 0) | |
327 return FALSE; | |
328 | |
329 /* finished syncing messages in this mailbox that exist in both source | |
9642 | 330 and destination. if there are messages left in destination, |
331 we can't reliably know if they should be expunged, so don't. | |
332 Add their GUIDs to hash in any case. */ | |
9559 | 333 |
334 boxp = array_idx(&sync->brain->src_mailbox_list->mailboxes, | |
335 sync->src_msg_iter->wanted_mailbox_idx); | |
336 while ((ret = dsync_brain_msg_iter_next(sync->dest_msg_iter)) > 0) { | |
337 if (sync->dest_msg_iter->msg.uid >= (*boxp)->uid_next) | |
338 sync->uid_conflict = TRUE; | |
339 sync->dest_msg_iter->msg.guid = NULL; | |
340 } | |
341 if (ret == 0) | |
342 return FALSE; | |
343 | |
344 /* if there are any messages left in source, we'll copy all of them */ | |
345 while ((ret = dsync_brain_msg_iter_next(sync->src_msg_iter)) > 0) { | |
346 dsync_brain_msg_sync_save_source(sync); | |
347 sync->src_msg_iter->msg.guid = NULL; | |
348 } | |
349 if (ret == 0) | |
350 return FALSE; | |
351 /* done with this mailbox. the same iterator is still used for | |
352 getting messages from other mailboxes. */ | |
353 return TRUE; | |
354 } | |
355 | |
356 static void dsync_brain_msg_sync_finish(struct dsync_brain_mailbox_sync *sync) | |
357 { | |
358 /* synced all existing messages. now add the new messages. */ | |
359 if (dsync_worker_msg_iter_deinit(&sync->src_msg_iter->iter) < 0 || | |
360 dsync_worker_msg_iter_deinit(&sync->dest_msg_iter->iter)) | |
361 dsync_brain_fail(sync->brain); | |
362 | |
363 sync->brain->state++; | |
364 dsync_brain_sync(sync->brain); | |
365 } | |
366 | |
367 static void dsync_brain_msg_sync_more(struct dsync_brain_mailbox_sync *sync) | |
368 { | |
369 struct dsync_mailbox *const *mailboxes; | |
370 unsigned int count, mailbox_idx; | |
371 | |
372 mailboxes = array_get(&sync->brain->src_mailbox_list->mailboxes, | |
373 &count); | |
374 while (dsync_brain_msg_sync_mailbox_more(sync)) { | |
375 /* sync the next mailbox */ | |
9642 | 376 sync->uid_conflict = FALSE; |
9559 | 377 mailbox_idx = ++sync->src_msg_iter->wanted_mailbox_idx; |
378 sync->dest_msg_iter->wanted_mailbox_idx++; | |
379 if (mailbox_idx == count) { | |
380 dsync_brain_msg_sync_finish(sync); | |
381 return; | |
382 } | |
383 dsync_worker_select_mailbox(sync->brain->dest_worker, | |
384 &mailboxes[mailbox_idx]->guid); | |
385 } | |
386 } | |
387 | |
388 static void dsync_worker_msg_callback(void *context) | |
389 { | |
390 struct dsync_brain_mailbox_sync *sync = context; | |
391 | |
392 dsync_brain_msg_sync_more(sync); | |
393 } | |
394 | |
395 static struct dsync_brain_msg_iter * | |
396 dsync_brain_msg_iter_init(struct dsync_brain_mailbox_sync *sync, | |
397 struct dsync_worker *worker, | |
398 const mailbox_guid_t mailboxes[], | |
399 unsigned int mailbox_count) | |
400 { | |
401 struct dsync_brain_msg_iter *iter; | |
402 | |
403 iter = p_new(sync->pool, struct dsync_brain_msg_iter, 1); | |
404 iter->sync = sync; | |
405 iter->worker = worker; | |
406 iter->iter = dsync_worker_msg_iter_init(worker, mailboxes, | |
407 mailbox_count); | |
408 dsync_worker_set_input_callback(worker, | |
409 dsync_worker_msg_callback, sync); | |
410 dsync_worker_set_output_callback(worker, | |
411 dsync_worker_msg_callback, sync); | |
412 return iter; | |
413 } | |
414 | |
9639
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
415 static bool dsync_mailbox_has_changed_msgs(const struct dsync_mailbox *box1, |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
416 const struct dsync_mailbox *box2) |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
417 { |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
418 return box1->uid_validity != box2->uid_validity || |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
419 box1->uid_next != box2->uid_next || |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
420 box1->highest_modseq != box2->highest_modseq; |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
421 } |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
422 |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
423 static void |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
424 dsync_brain_get_changed_mailboxes(struct dsync_brain *brain, |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
425 ARRAY_TYPE(mailbox_guid) *guids) |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
426 { |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
427 struct dsync_mailbox *const *src_boxes, *const *dest_boxes; |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
428 unsigned int src, dest, src_count, dest_count; |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
429 int ret; |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
430 |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
431 src_boxes = array_get(&brain->src_mailbox_list->mailboxes, &src_count); |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
432 dest_boxes = array_get(&brain->dest_mailbox_list->mailboxes, &dest_count); |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
433 |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
434 for (src = dest = 0; src < src_count && dest < dest_count; ) { |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
435 ret = dsync_mailbox_guid_cmp(src_boxes[src], dest_boxes[dest]); |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
436 if (ret == 0) { |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
437 if (dsync_mailbox_has_changed_msgs(src_boxes[src], |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
438 dest_boxes[dest])) |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
439 array_append(guids, &src_boxes[src]->guid, 1); |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
440 src++; dest++; |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
441 } else if (ret < 0) { |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
442 /* exists only in source */ |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
443 array_append(guids, &src_boxes[src]->guid, 1); |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
444 src++; |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
445 } else { |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
446 /* exists only in dest */ |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
447 dest++; |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
448 } |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
449 } |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
450 for (; src < src_count; src++) |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
451 array_append(guids, &src_boxes[src]->guid, 1); |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
452 } |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
453 |
9559 | 454 static struct dsync_brain_mailbox_sync * |
455 dsync_brain_msg_sync_init(struct dsync_brain *brain) | |
456 { | |
457 struct dsync_brain_mailbox_sync *sync; | |
9639
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
458 ARRAY_TYPE(mailbox_guid) guids; |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
459 unsigned int count; |
9559 | 460 pool_t pool; |
461 | |
9639
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
462 t_array_init(&guids, array_count(&brain->src_mailbox_list->mailboxes)); |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
463 dsync_brain_get_changed_mailboxes(brain, &guids); |
9559 | 464 |
465 pool = pool_alloconly_create("dsync brain mailbox sync", 1024*256); | |
466 sync = p_new(pool, struct dsync_brain_mailbox_sync, 1); | |
467 sync->pool = pool; | |
468 sync->brain = brain; | |
469 | |
470 i_array_init(&sync->uid_conflicts, 128); | |
471 i_array_init(&sync->new_msgs, 128); | |
472 i_array_init(&sync->copy_retry_indexes, 32); | |
9639
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
473 |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
474 /* initialize message iteration on both workers */ |
ffda7bd92ebc
dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents:
9638
diff
changeset
|
475 count = array_count(&guids); |
9559 | 476 sync->src_msg_iter = |
477 dsync_brain_msg_iter_init(sync, brain->src_worker, | |
478 array_idx(&guids, 0), count); | |
479 sync->dest_msg_iter = | |
480 dsync_brain_msg_iter_init(sync, brain->dest_worker, | |
481 array_idx(&guids, 0), count); | |
482 | |
483 sync->guid_hash = hash_table_create(default_pool, pool, 10000, | |
484 strcase_hash, | |
485 (hash_cmp_callback_t *)strcasecmp); | |
486 sync->dest_msg_iter->save_guids = TRUE; | |
487 return sync; | |
488 } | |
489 | |
490 static void dsync_brain_msg_sync_deinit(struct dsync_brain_mailbox_sync **_sync) | |
491 { | |
492 struct dsync_brain_mailbox_sync *sync = *_sync; | |
493 | |
494 *_sync = NULL; | |
495 | |
496 if (sync->src_msg_iter->iter != NULL) | |
497 (void)dsync_worker_msg_iter_deinit(&sync->src_msg_iter->iter); | |
498 if (sync->dest_msg_iter->iter != NULL) | |
499 (void)dsync_worker_msg_iter_deinit(&sync->dest_msg_iter->iter); | |
500 | |
501 hash_table_destroy(&sync->guid_hash); | |
502 array_free(&sync->uid_conflicts); | |
503 array_free(&sync->new_msgs); | |
504 array_free(&sync->copy_retry_indexes); | |
505 pool_unref(&sync->pool); | |
506 } | |
507 | |
508 static void dsync_brain_sync_existing_mailboxes(struct dsync_brain *brain) | |
509 { | |
510 brain->mailbox_sync = dsync_brain_msg_sync_init(brain); | |
511 dsync_brain_msg_sync_more(brain->mailbox_sync); | |
512 } | |
513 | |
514 static int | |
515 dsync_brain_msg_sync_add_new_msg(struct dsync_brain_mailbox_sync *sync, | |
516 const struct dsync_mailbox *src_mailbox, | |
517 unsigned int msg_idx, | |
518 const struct dsync_message *msg) | |
519 { | |
520 const struct dsync_brain_guid_instance *inst; | |
521 struct dsync_mailbox *const *inst_box; | |
522 struct dsync_msg_static_data data; | |
523 int ret; | |
524 | |
525 inst = hash_table_lookup(sync->guid_hash, msg->guid); | |
526 if (inst != NULL) { | |
527 /* we can save this by copying an existing message */ | |
528 dsync_worker_select_mailbox(sync->brain->dest_worker, | |
529 &src_mailbox->guid); | |
530 dsync_worker_set_next_result_tag(sync->brain->dest_worker, | |
531 msg_idx+1); | |
532 inst_box = array_idx(&sync->brain->src_mailbox_list->mailboxes, | |
533 inst->mailbox_idx); | |
534 dsync_worker_msg_copy(sync->brain->dest_worker, | |
535 &(*inst_box)->guid, inst->uid, msg); | |
536 sync->copy_results_left++; | |
537 } else { | |
538 dsync_worker_select_mailbox(sync->brain->src_worker, | |
539 &src_mailbox->guid); | |
540 ret = dsync_worker_msg_get(sync->brain->src_worker, | |
541 msg->uid, &data); | |
542 if (ret <= 0) { | |
543 if (ret == 0) { | |
544 /* mail got expunged during sync. | |
545 just skip this. */ | |
546 return 1; | |
547 } else { | |
548 dsync_brain_fail(sync->brain); | |
549 return -1; | |
550 } | |
551 } | |
552 dsync_worker_select_mailbox(sync->brain->dest_worker, | |
553 &src_mailbox->guid); | |
554 dsync_worker_msg_save(sync->brain->dest_worker, msg, &data); | |
555 } | |
556 return dsync_worker_is_output_full(sync->brain->dest_worker) ? 0 : 1; | |
557 } | |
558 | |
559 static void | |
560 dsync_brain_msg_sync_add_new_msgs(struct dsync_brain_mailbox_sync *sync) | |
561 { | |
562 struct dsync_mailbox *const *mailboxes, *mailbox; | |
563 const struct dsync_brain_new_msg *msgs; | |
564 unsigned int i, mailbox_count, msg_count; | |
565 | |
566 mailboxes = array_get(&sync->brain->src_mailbox_list->mailboxes, | |
567 &mailbox_count); | |
568 msgs = array_get(&sync->new_msgs, &msg_count); | |
569 for (i = sync->next_new_msg; i < msg_count; i++) { | |
570 mailbox = mailboxes[msgs[i].mailbox_idx]; | |
571 if (dsync_brain_msg_sync_add_new_msg(sync, mailbox, i, | |
572 msgs[i].msg) <= 0) { | |
573 /* failed / continue later */ | |
574 sync->next_new_msg = i + 1; | |
575 return; | |
576 } | |
577 } | |
578 | |
579 /* all messages sent */ | |
580 if (sync->copy_results_left == 0) { | |
581 sync->brain->state++; | |
582 dsync_brain_sync(sync->brain); | |
583 } | |
584 } | |
585 | |
586 static void dsync_worker_copy_input(void *context) | |
587 { | |
588 struct dsync_brain_mailbox_sync *sync = context; | |
589 struct dsync_brain_guid_instance *inst; | |
590 const struct dsync_brain_new_msg *msgs; | |
591 unsigned int count; | |
592 uint32_t tag; | |
593 int result; | |
594 | |
595 msgs = array_get(&sync->new_msgs, &count); | |
596 while (dsync_worker_get_next_result(sync->brain->dest_worker, | |
597 &tag, &result)) { | |
598 if (tag == 0 || tag > count) { | |
599 i_error("Worker sent result with invalid tag %u", tag); | |
600 dsync_brain_fail(sync->brain); | |
601 return; | |
602 } | |
603 tag--; | |
604 if (sync->copy_results_left == 0) { | |
605 i_error("Worker sent unexpected result"); | |
606 dsync_brain_fail(sync->brain); | |
607 return; | |
608 } | |
609 sync->copy_results_left--; | |
610 if (result < 0) { | |
611 /* mark the guid instance invalid and try again later */ | |
612 inst = hash_table_lookup(sync->guid_hash, | |
613 msgs[tag].msg->guid); | |
614 inst->failed = TRUE; | |
615 array_append(&sync->copy_retry_indexes, &tag, 1); | |
616 } | |
617 } | |
618 if (sync->copy_results_left == 0) { | |
619 sync->brain->state++; | |
620 dsync_brain_sync(sync->brain); | |
621 } | |
622 } | |
623 | |
624 static void dsync_worker_new_msg_output(void *context) | |
625 { | |
626 struct dsync_brain_mailbox_sync *sync = context; | |
627 | |
628 dsync_brain_msg_sync_add_new_msgs(sync); | |
629 } | |
630 | |
631 static void | |
632 dsync_brain_msg_sync_new_msgs(struct dsync_brain_mailbox_sync *sync) | |
633 { | |
634 dsync_worker_set_input_callback(sync->brain->dest_worker, | |
635 dsync_worker_copy_input, sync); | |
636 dsync_worker_set_output_callback(sync->brain->dest_worker, | |
637 dsync_worker_new_msg_output, sync); | |
638 dsync_brain_msg_sync_add_new_msgs(sync); | |
639 } | |
640 | |
641 static void | |
642 dsync_brain_msg_sync_retry_copies(struct dsync_brain_mailbox_sync *sync) | |
643 { | |
644 const uint32_t *indexes; | |
645 struct dsync_mailbox *const *mailboxes, *mailbox; | |
646 const struct dsync_brain_new_msg *msgs; | |
647 unsigned int i, msg_idx, idx_count, msg_count, mailbox_count; | |
648 struct dsync_brain_guid_instance *inst; | |
649 const char *guid_str; | |
650 void *orig_key, *orig_value; | |
651 | |
652 /* first remove GUID instances that had failed. */ | |
653 msgs = array_get(&sync->new_msgs, &msg_count); | |
654 indexes = array_get(&sync->copy_retry_indexes, &idx_count); | |
655 for (i = 0; i < idx_count; i++) { | |
656 guid_str = msgs[indexes[i]].msg->guid; | |
657 if (hash_table_lookup_full(sync->guid_hash, guid_str, | |
658 &orig_key, &orig_value)) | |
659 inst = orig_value; | |
660 else | |
661 inst = NULL; | |
662 if (inst != NULL && inst->failed) { | |
663 inst = inst->next; | |
664 if (inst == NULL) | |
665 hash_table_remove(sync->guid_hash, guid_str); | |
666 else { | |
667 hash_table_update(sync->guid_hash, orig_key, | |
668 inst); | |
669 } | |
670 } | |
671 } | |
672 | |
673 /* try saving again. there probably weren't many of them, so don't | |
674 worry about filling output buffer. */ | |
675 mailboxes = array_get(&sync->brain->src_mailbox_list->mailboxes, | |
676 &mailbox_count); | |
677 for (i = 0; i < idx_count; i++) { | |
678 msg_idx = indexes[i]; | |
679 mailbox = mailboxes[msgs[msg_idx].mailbox_idx]; | |
680 (void)dsync_brain_msg_sync_add_new_msg(sync, mailbox, msg_idx, | |
681 msgs[msg_idx].msg); | |
682 } | |
683 | |
684 /* if we copied anything, we'll again have to wait for the results */ | |
685 array_clear(&sync->copy_retry_indexes); | |
686 dsync_worker_set_output_callback(sync->brain->dest_worker, NULL, NULL); | |
687 | |
688 if (sync->copy_results_left == 0) { | |
689 dsync_worker_set_input_callback(sync->brain->dest_worker, | |
690 NULL, NULL); | |
691 sync->brain->state++; | |
692 dsync_brain_sync(sync->brain); | |
693 } else { | |
694 /* temporarily move back the state. once copies have returned | |
695 success/failures, we'll get back to this function and see | |
696 if we need to retry again */ | |
697 sync->brain->state--; | |
698 } | |
699 } | |
700 | |
701 static void | |
702 dsync_brain_msg_sync_update_mailbox(struct dsync_brain *brain) | |
703 { | |
704 struct dsync_mailbox *const *mailboxes; | |
705 unsigned int i, count; | |
706 | |
707 mailboxes = array_get(&brain->src_mailbox_list->mailboxes, &count); | |
708 for (i = 0; i < count; i++) | |
709 dsync_worker_update_mailbox(brain->dest_worker, mailboxes[i]); | |
710 } | |
711 | |
712 static void | |
713 dsync_brain_msg_sync_resolve_uid_conflicts(struct dsync_brain_mailbox_sync *sync) | |
714 { | |
715 const struct dsync_brain_uid_conflict *conflicts; | |
716 struct dsync_mailbox *const *mailboxes, *mailbox; | |
717 unsigned int i, count, mailbox_count; | |
718 | |
719 mailboxes = array_get(&sync->brain->src_mailbox_list->mailboxes, | |
720 &mailbox_count); | |
721 conflicts = array_get(&sync->uid_conflicts, &count); | |
722 for (i = 0; i < count; i++) { | |
723 mailbox = mailboxes[conflicts[i].mailbox_idx]; | |
724 dsync_worker_select_mailbox(sync->brain->dest_worker, | |
725 &mailbox->guid); | |
726 dsync_worker_msg_update_uid(sync->brain->dest_worker, | |
727 conflicts[i].uid); | |
728 } | |
729 } | |
730 | |
731 static void dsync_worker_flush_callback(void *context) | |
732 { | |
733 struct dsync_brain *brain = context; | |
734 int ret; | |
735 | |
736 if ((ret = dsync_worker_output_flush(brain->dest_worker)) <= 0) { | |
737 if (ret < 0) | |
738 dsync_brain_fail(brain); | |
739 return; | |
740 } | |
741 brain->state++; | |
742 dsync_brain_sync(brain); | |
743 } | |
744 | |
745 void dsync_brain_sync(struct dsync_brain *brain) | |
746 { | |
747 switch (brain->state) { | |
748 case DSYNC_STATE_GET_MAILBOXES: | |
749 i_assert(brain->src_mailbox_list == NULL); | |
750 brain->src_mailbox_list = | |
751 dsync_brain_mailbox_list_init(brain, brain->src_worker); | |
752 brain->dest_mailbox_list = | |
753 dsync_brain_mailbox_list_init(brain, brain->dest_worker); | |
754 dsync_worker_mailbox_input(brain->src_mailbox_list); | |
755 dsync_worker_mailbox_input(brain->dest_mailbox_list); | |
756 break; | |
757 case DSYNC_STATE_CREATE_MAILBOXES: | |
758 if (array_count(&brain->src_mailbox_list->mailboxes) == 0) { | |
759 /* no mailboxes */ | |
760 i_error("No source mailboxes"); | |
761 dsync_brain_fail(brain); | |
762 } | |
763 | |
764 /* FIXME: maybe wait and verify that all mailboxes are | |
765 created successfully? */ | |
766 dsync_brain_create_missing_mailboxes(brain); | |
767 brain->state++; | |
768 /* fall through */ | |
769 case DSYNC_STATE_SYNC_EXISTING_MSGS: | |
770 dsync_brain_sync_existing_mailboxes(brain); | |
771 break; | |
772 case DSYNC_STATE_SYNC_NEW_MSGS: | |
773 dsync_brain_msg_sync_new_msgs(brain->mailbox_sync); | |
774 break; | |
775 case DSYNC_STATE_SYNC_RETRY_COPIES: | |
776 dsync_brain_msg_sync_retry_copies(brain->mailbox_sync); | |
777 break; | |
778 case DSYNC_STATE_SYNC_UPDATE_MAILBOX: | |
779 dsync_brain_msg_sync_update_mailbox(brain); | |
780 brain->state++; | |
781 /* fall through */ | |
782 case DSYNC_STATE_SYNC_RESOLVE_UID_CONFLICTS: | |
783 /* resolve uid conflicts after uid_nexts have been updated, | |
784 so that it won't again collide uids */ | |
785 dsync_brain_msg_sync_resolve_uid_conflicts(brain->mailbox_sync); | |
786 brain->state++; | |
787 /* fall through */ | |
788 case DSYNC_STATE_SYNC_FLUSH: | |
789 dsync_worker_set_output_callback(brain->dest_worker, | |
790 dsync_worker_flush_callback, | |
791 brain); | |
792 dsync_worker_flush_callback(brain); | |
793 break; | |
794 case DSYNC_STATE_SYNC_END: | |
795 master_service_stop(master_service); | |
796 break; | |
797 } | |
798 } |