Mercurial > dovecot > core-2.2
annotate src/doveadm/dsync/dsync-slave-io.c @ 14629:c93ca5e46a8a
Marked functions parameters that are allowed to be NULL. Some APIs were also changed.
The non-obvious APIs where NULL parameter was changed to "" are
master_service_init() and auth_master_user_list_init().
These checks can currently be enabled only on a patched clang:
http://llvm.org/bugs/show_bug.cgi?id=6786
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Sun, 24 Jun 2012 00:52:57 +0300 |
parents | b2076acc3715 |
children | ca37d1577291 |
rev | line source |
---|---|
14584 | 1 /* Copyright (c) 2012 Dovecot authors, see the included COPYING file */ |
2 | |
3 #include "lib.h" | |
4 #include "array.h" | |
5 #include "close-keep-errno.h" | |
6 #include "fd-set-nonblock.h" | |
7 #include "safe-mkstemp.h" | |
8 #include "ioloop.h" | |
9 #include "istream.h" | |
10 #include "istream-seekable.h" | |
11 #include "istream-dot.h" | |
12 #include "ostream.h" | |
13 #include "str.h" | |
14 #include "strescape.h" | |
15 #include "master-service.h" | |
16 #include "mail-cache.h" | |
17 #include "mail-storage-private.h" | |
18 #include "dsync-serializer.h" | |
19 #include "dsync-deserializer.h" | |
20 #include "dsync-mail.h" | |
21 #include "dsync-mailbox.h" | |
22 #include "dsync-mailbox-state.h" | |
23 #include "dsync-mailbox-tree.h" | |
24 #include "dsync-slave-private.h" | |
25 | |
26 #include <stdlib.h> | |
27 | |
28 #define DSYNC_SLAVE_IO_TIMEOUT_MSECS (60*10*1000) | |
29 #define DSYNC_SLAVE_IO_OUTBUF_THROTTLE_SIZE (1024*128) | |
30 | |
31 #define DSYNC_PROTOCOL_VERSION_MAJOR 3 | |
32 #define DSYNC_HANDSHAKE_VERSION "VERSION\tdsync\t3\t0\n" | |
33 | |
34 enum item_type { | |
35 ITEM_NONE, | |
36 | |
37 ITEM_HANDSHAKE, | |
38 ITEM_MAILBOX_STATE, | |
39 ITEM_MAILBOX_TREE_NODE, | |
40 ITEM_MAILBOX_DELETE, | |
41 ITEM_MAILBOX, | |
42 | |
43 ITEM_MAIL_CHANGE, | |
44 ITEM_MAIL_REQUEST, | |
45 ITEM_MAIL, | |
46 | |
47 ITEM_MAILBOX_CACHE_FIELD, | |
48 | |
49 ITEM_END_OF_LIST | |
50 }; | |
51 | |
52 #define END_OF_LIST_LINE "." | |
53 static const struct { | |
54 /* full human readable name of the item */ | |
55 const char *name; | |
56 /* unique character identifying the item */ | |
57 char chr; | |
58 const char *required_keys; | |
59 const char *optional_keys; | |
60 } items[ITEM_END_OF_LIST+1] = { | |
61 { NULL, '\0', NULL, NULL }, | |
62 { .name = "handshake", | |
63 .chr = 'H', | |
64 .optional_keys = "sync_ns_prefix sync_type " | |
65 "guid_requests mails_have_guids" | |
66 }, | |
67 { .name = "mailbox_state", | |
68 .chr = 'S', | |
69 .required_keys = "mailbox_guid last_uidvalidity last_common_uid " | |
70 "last_common_modseq" | |
71 }, | |
72 { .name = "mailbox_tree_node", | |
73 .chr = 'N', | |
74 .required_keys = "name existence", | |
75 .optional_keys = "mailbox_guid uid_validity " | |
76 "last_renamed subscribed last_subscription_change" | |
77 }, | |
78 { .name = "mailbox_delete", | |
79 .chr = 'D', | |
80 .required_keys = "hierarchy_sep", | |
81 .optional_keys = "mailboxes dirs" | |
82 }, | |
83 { .name = "mailbox", | |
84 .chr = 'B', | |
85 .required_keys = "mailbox_guid uid_validity uid_next " | |
86 "messages_count first_recent_uid highest_modseq", | |
87 .optional_keys = "cache_fields" | |
88 }, | |
89 { .name = "mail_change", | |
90 .chr = 'C', | |
91 .required_keys = "type uid", | |
92 .optional_keys = "guid hdr_hash modseq save_timestamp " | |
93 "add_flags remove_flags final_flags " | |
94 "keywords_reset keyword_changes" | |
95 }, | |
96 { .name = "mail_request", | |
97 .chr = 'R', | |
98 .optional_keys = "guid uid" | |
99 }, | |
100 { .name = "mail", | |
101 .chr = 'M', | |
102 .optional_keys = "guid uid pop3_uidl pop3_order received_date stream" | |
103 }, | |
104 { .name = "mailbox_cache_field", | |
105 .chr = 'c', | |
106 .required_keys = "name decision", | |
107 .optional_keys = "last_used" | |
108 }, | |
109 | |
110 { "end_of_list", '\0', NULL, NULL } | |
111 }; | |
112 | |
113 struct dsync_slave_io { | |
114 struct dsync_slave slave; | |
115 | |
116 char *name, *temp_path_prefix; | |
117 int fd_in, fd_out; | |
118 struct istream *input; | |
119 struct ostream *output; | |
120 struct io *io; | |
121 struct timeout *to; | |
122 | |
123 struct dsync_serializer *serializers[ITEM_END_OF_LIST]; | |
124 struct dsync_deserializer *deserializers[ITEM_END_OF_LIST]; | |
125 | |
126 pool_t ret_pool; | |
127 struct dsync_deserializer_decoder *cur_decoder; | |
128 | |
129 struct istream *mail_output, *mail_input; | |
130 struct dsync_mail *cur_mail; | |
131 char mail_output_last; | |
132 | |
133 unsigned int version_received:1; | |
134 unsigned int handshake_received:1; | |
135 unsigned int has_pending_data:1; | |
136 }; | |
137 | |
138 static void dsync_slave_io_stop(struct dsync_slave_io *slave) | |
139 { | |
140 i_stream_close(slave->input); | |
141 o_stream_close(slave->output); | |
142 io_loop_stop(current_ioloop); | |
143 } | |
144 | |
145 static int dsync_slave_io_read_mail_stream(struct dsync_slave_io *slave) | |
146 { | |
147 size_t size; | |
148 | |
149 if (i_stream_read(slave->mail_input) < 0) { | |
150 if (slave->mail_input->stream_errno != 0) { | |
151 errno = slave->mail_input->stream_errno; | |
152 i_error("dsync(%s): read() failed: %m", slave->name); | |
153 dsync_slave_io_stop(slave); | |
154 return -1; | |
155 } | |
156 /* finished reading the mail stream */ | |
157 i_assert(slave->mail_input->eof); | |
158 i_stream_seek(slave->mail_input, 0); | |
159 slave->mail_input = NULL; | |
160 return 1; | |
161 } | |
162 (void)i_stream_get_data(slave->mail_input, &size); | |
163 i_stream_skip(slave->mail_input, size); | |
164 return 0; | |
165 } | |
166 | |
167 static void dsync_slave_io_input(struct dsync_slave_io *slave) | |
168 { | |
169 if (slave->mail_input != NULL) { | |
170 if (dsync_slave_io_read_mail_stream(slave) == 0) | |
171 return; | |
172 } | |
173 slave->slave.io_callback(slave->slave.io_context); | |
174 } | |
175 | |
176 static int dsync_slave_io_send_mail_stream(struct dsync_slave_io *slave) | |
177 { | |
178 const unsigned char *data; | |
179 unsigned char add; | |
180 size_t i, size; | |
181 int ret; | |
182 | |
183 while ((ret = i_stream_read_data(slave->mail_output, | |
184 &data, &size, 0)) > 0) { | |
185 add = '\0'; | |
186 for (i = 0; i < size; i++) { | |
187 if (data[i] == '\n') { | |
188 if ((i == 0 && slave->mail_output_last != '\r') || | |
189 (i > 0 && data[i-1] != '\r')) { | |
190 /* missing CR */ | |
191 add = '\r'; | |
192 break; | |
193 } | |
194 } else if (data[i] == '.' && | |
195 ((i == 0 && slave->mail_output_last == '\n') || | |
196 (i > 0 && data[i-1] == '\n'))) { | |
197 /* escape the dot */ | |
198 add = '.'; | |
199 break; | |
200 } | |
201 } | |
202 | |
203 if (i > 0) { | |
204 (void)o_stream_send(slave->output, data, i); | |
205 slave->mail_output_last = data[i-1]; | |
206 i_stream_skip(slave->mail_output, i); | |
207 } | |
208 | |
209 if (o_stream_get_buffer_used_size(slave->output) >= 4096) { | |
210 if ((ret = o_stream_flush(slave->output)) < 0) { | |
211 dsync_slave_io_stop(slave); | |
212 return -1; | |
213 } | |
214 if (ret == 0) { | |
215 /* continue later */ | |
216 o_stream_set_flush_pending(slave->output, TRUE); | |
217 return 0; | |
218 } | |
219 } | |
220 | |
221 if (add != '\0') { | |
222 (void)o_stream_send(slave->output, &add, 1); | |
223 slave->mail_output_last = add; | |
224 } | |
225 } | |
226 i_assert(ret == -1); | |
227 | |
228 if (slave->mail_output->stream_errno != 0) { | |
229 i_error("dsync(%s): read(%s) failed: %m", | |
230 slave->name, i_stream_get_name(slave->mail_output)); | |
231 dsync_slave_io_stop(slave); | |
232 return -1; | |
233 } | |
234 | |
235 /* finished sending the stream */ | |
236 (void)o_stream_send_str(slave->output, "\r\n.\r\n"); | |
237 i_stream_unref(&slave->mail_output); | |
238 return 1; | |
239 } | |
240 | |
241 static int dsync_slave_io_output(struct dsync_slave_io *slave) | |
242 { | |
243 struct ostream *output = slave->output; | |
244 int ret; | |
245 | |
246 if ((ret = o_stream_flush(output)) < 0) | |
247 ret = 1; | |
248 else if (slave->mail_output != NULL) { | |
249 if (dsync_slave_io_send_mail_stream(slave) < 0) | |
250 ret = 1; | |
251 } | |
252 timeout_reset(slave->to); | |
253 | |
254 if (!dsync_slave_is_send_queue_full(&slave->slave)) | |
255 slave->slave.io_callback(slave->slave.io_context); | |
256 return ret; | |
257 } | |
258 | |
259 static void dsync_slave_io_timeout(struct dsync_slave_io *slave) | |
260 { | |
261 i_error("dsync(%s): I/O has stalled, no activity for %u seconds", | |
262 slave->name, DSYNC_SLAVE_IO_TIMEOUT_MSECS/1000); | |
263 dsync_slave_io_stop(slave); | |
264 } | |
265 | |
266 static void dsync_slave_io_init(struct dsync_slave_io *slave) | |
267 { | |
268 unsigned int i; | |
269 | |
270 fd_set_nonblock(slave->fd_in, TRUE); | |
271 fd_set_nonblock(slave->fd_out, TRUE); | |
272 | |
273 slave->input = i_stream_create_fd(slave->fd_in, (size_t)-1, FALSE); | |
274 slave->output = o_stream_create_fd(slave->fd_out, (size_t)-1, FALSE); | |
275 slave->io = io_add(slave->fd_in, IO_READ, dsync_slave_io_input, slave); | |
276 o_stream_set_flush_callback(slave->output, dsync_slave_io_output, slave); | |
277 slave->to = timeout_add(DSYNC_SLAVE_IO_TIMEOUT_MSECS, | |
14629
c93ca5e46a8a
Marked functions parameters that are allowed to be NULL. Some APIs were also changed.
Timo Sirainen <tss@iki.fi>
parents:
14584
diff
changeset
|
278 dsync_slave_io_timeout, slave); |
14584 | 279 o_stream_cork(slave->output); |
280 o_stream_send_str(slave->output, DSYNC_HANDSHAKE_VERSION); | |
281 | |
282 /* initialize serializers and send their headers to remote */ | |
283 for (i = 1; i < ITEM_END_OF_LIST; i++) T_BEGIN { | |
284 const char *keys; | |
285 | |
286 keys = items[i].required_keys == NULL ? items[i].optional_keys : | |
287 t_strconcat(items[i].required_keys, " ", | |
288 items[i].optional_keys, NULL); | |
289 if (keys != NULL) { | |
290 i_assert(items[i].chr != '\0'); | |
291 | |
292 slave->serializers[i] = | |
293 dsync_serializer_init(t_strsplit_spaces(keys, " ")); | |
294 o_stream_send(slave->output, &items[i].chr, 1); | |
295 o_stream_send_str(slave->output, | |
296 dsync_serializer_encode_header_line(slave->serializers[i])); | |
297 } | |
298 } T_END; | |
299 o_stream_send_str(slave->output, ".\n"); | |
300 | |
301 dsync_slave_flush(&slave->slave); | |
302 } | |
303 | |
304 static void dsync_slave_io_deinit(struct dsync_slave *_slave) | |
305 { | |
306 struct dsync_slave_io *slave = (struct dsync_slave_io *)_slave; | |
307 | |
308 if (slave->cur_decoder != NULL) | |
309 dsync_deserializer_decode_finish(&slave->cur_decoder); | |
310 if (slave->mail_output != NULL) | |
311 i_stream_unref(&slave->mail_output); | |
312 | |
313 timeout_remove(&slave->to); | |
314 if (slave->io != NULL) | |
315 io_remove(&slave->io); | |
316 i_stream_destroy(&slave->input); | |
317 o_stream_destroy(&slave->output); | |
318 if (close(slave->fd_in) < 0) | |
319 i_error("close(%s) failed: %m", slave->name); | |
320 if (slave->fd_in != slave->fd_out) { | |
321 if (close(slave->fd_out) < 0) | |
322 i_error("close(%s) failed: %m", slave->name); | |
323 } | |
324 pool_unref(&slave->ret_pool); | |
325 i_free(slave->temp_path_prefix); | |
326 i_free(slave->name); | |
327 i_free(slave); | |
328 } | |
329 | |
330 static int dsync_slave_io_next_line(struct dsync_slave_io *slave, | |
331 const char **line_r) | |
332 { | |
333 const char *line; | |
334 | |
335 line = i_stream_next_line(slave->input); | |
336 if (line != NULL) { | |
337 *line_r = line; | |
338 return 1; | |
339 } | |
340 | |
341 /* try reading some */ | |
342 switch (i_stream_read(slave->input)) { | |
343 case -1: | |
344 if (slave->input->stream_errno != 0) { | |
345 errno = slave->input->stream_errno; | |
346 i_error("read(%s) failed: %m", slave->name); | |
347 } else { | |
348 i_assert(slave->input->eof); | |
349 i_error("read(%s) failed: EOF", slave->name); | |
350 } | |
351 dsync_slave_io_stop(slave); | |
352 return -1; | |
353 case 0: | |
354 return 0; | |
355 } | |
356 *line_r = i_stream_next_line(slave->input); | |
357 if (*line_r == NULL) { | |
358 slave->has_pending_data = FALSE; | |
359 return 0; | |
360 } | |
361 slave->has_pending_data = TRUE; | |
362 return 1; | |
363 } | |
364 | |
14629
c93ca5e46a8a
Marked functions parameters that are allowed to be NULL. Some APIs were also changed.
Timo Sirainen <tss@iki.fi>
parents:
14584
diff
changeset
|
365 static void ATTR_FORMAT(3, 4) ATTR_NULL(2) |
14584 | 366 dsync_slave_input_error(struct dsync_slave_io *slave, |
367 struct dsync_deserializer_decoder *decoder, | |
368 const char *fmt, ...) | |
369 { | |
370 va_list args; | |
371 const char *error; | |
372 | |
373 va_start(args, fmt); | |
374 error = t_strdup_vprintf(fmt, args); | |
375 if (decoder == NULL) | |
376 i_error("dsync(%s): %s", slave->name, error); | |
377 else { | |
378 i_error("dsync(%s): %s: %s", slave->name, | |
379 dsync_deserializer_decoder_get_name(decoder), error); | |
380 } | |
381 va_end(args); | |
382 | |
383 dsync_slave_io_stop(slave); | |
384 } | |
385 | |
386 static void | |
387 dsync_slave_io_send_string(struct dsync_slave_io *slave, const string_t *str) | |
388 { | |
389 i_assert(slave->mail_output == NULL); | |
390 o_stream_send(slave->output, str_data(str), str_len(str)); | |
391 } | |
392 | |
393 static int dsync_slave_check_missing_deserializers(struct dsync_slave_io *slave) | |
394 { | |
395 unsigned int i; | |
396 int ret = 0; | |
397 | |
398 for (i = 1; i < ITEM_END_OF_LIST; i++) { | |
399 if (slave->deserializers[i] == NULL && | |
400 (items[i].required_keys != NULL || | |
401 items[i].optional_keys != NULL)) { | |
402 dsync_slave_input_error(slave, NULL, | |
403 "Remote didn't handshake deserializer for %s", | |
404 items[i].name); | |
405 ret = -1; | |
406 } | |
407 } | |
408 return ret; | |
409 } | |
410 | |
411 static bool | |
412 dsync_slave_io_handshake(struct dsync_slave_io *slave, const char *line) | |
413 { | |
414 enum item_type item = ITEM_NONE; | |
415 const char *const *required_keys, *error; | |
416 unsigned int i; | |
417 | |
418 if (slave->handshake_received) | |
419 return TRUE; | |
420 | |
421 if (!slave->version_received) { | |
422 if (!version_string_verify(line, "dsync", | |
423 DSYNC_PROTOCOL_VERSION_MAJOR)) { | |
424 dsync_slave_input_error(slave, NULL, | |
425 "Remote dsync doesn't use compatible protocol"); | |
426 return DSYNC_SLAVE_RECV_RET_TRYAGAIN; | |
427 } | |
428 slave->version_received = TRUE; | |
429 return FALSE; | |
430 } | |
431 | |
432 if (strcmp(line, END_OF_LIST_LINE) == 0) { | |
433 /* finished handshaking */ | |
434 if (dsync_slave_check_missing_deserializers(slave) < 0) | |
435 return FALSE; | |
436 slave->handshake_received = TRUE; | |
437 return FALSE; | |
438 } | |
439 | |
440 for (i = 1; i < ITEM_END_OF_LIST; i++) { | |
441 if (items[i].chr == line[0]) { | |
442 item = i; | |
443 break; | |
444 } | |
445 } | |
446 if (item == ITEM_NONE) { | |
447 /* unknown deserializer, ignore */ | |
448 return FALSE; | |
449 } | |
450 | |
451 required_keys = items[item].required_keys == NULL ? NULL : | |
452 t_strsplit(items[item].required_keys, " "); | |
453 if (dsync_deserializer_init(items[item].name, | |
454 required_keys, line + 1, | |
455 &slave->deserializers[item], &error) < 0) { | |
456 dsync_slave_input_error(slave, NULL, | |
457 "Remote sent invalid handshake for %s: %s", | |
458 items[item].name, error); | |
459 } | |
460 return FALSE; | |
461 } | |
462 | |
463 static enum dsync_slave_recv_ret | |
464 dsync_slave_io_input_next(struct dsync_slave_io *slave, enum item_type item, | |
465 struct dsync_deserializer_decoder **decoder_r) | |
466 { | |
467 enum item_type line_item = ITEM_NONE; | |
468 const char *line, *error; | |
469 unsigned int i; | |
470 | |
471 i_assert(slave->mail_input == NULL); | |
472 | |
473 timeout_reset(slave->to); | |
474 | |
475 do { | |
476 if (dsync_slave_io_next_line(slave, &line) <= 0) | |
477 return DSYNC_SLAVE_RECV_RET_TRYAGAIN; | |
478 } while (!dsync_slave_io_handshake(slave, line)); | |
479 | |
480 if (strcmp(line, END_OF_LIST_LINE) == 0) { | |
481 /* end of this list */ | |
482 return DSYNC_SLAVE_RECV_RET_FINISHED; | |
483 } | |
484 for (i = 1; i < ITEM_END_OF_LIST; i++) { | |
485 if (*line == items[i].chr) { | |
486 line_item = i; | |
487 break; | |
488 } | |
489 } | |
490 if (line_item != item) { | |
491 dsync_slave_input_error(slave, NULL, | |
492 "Received unexpected input %c != %c", | |
493 *line, items[item].chr); | |
494 return DSYNC_SLAVE_RECV_RET_TRYAGAIN; | |
495 } | |
496 | |
497 if (slave->cur_decoder != NULL) | |
498 dsync_deserializer_decode_finish(&slave->cur_decoder); | |
499 if (dsync_deserializer_decode_begin(slave->deserializers[item], | |
500 line+1, &slave->cur_decoder, | |
501 &error) < 0) { | |
502 dsync_slave_input_error(slave, NULL, "Invalid input to %s: %s", | |
503 items[item].name, error); | |
504 return DSYNC_SLAVE_RECV_RET_TRYAGAIN; | |
505 } | |
506 *decoder_r = slave->cur_decoder; | |
507 return DSYNC_SLAVE_RECV_RET_OK; | |
508 } | |
509 | |
510 static void | |
511 dsync_slave_io_send_handshake(struct dsync_slave *_slave, | |
512 const struct dsync_slave_settings *set) | |
513 { | |
514 struct dsync_slave_io *slave = (struct dsync_slave_io *)_slave; | |
515 struct dsync_serializer_encoder *encoder; | |
516 string_t *str = t_str_new(128); | |
517 char sync_type[2]; | |
518 | |
519 str_append_c(str, items[ITEM_HANDSHAKE].chr); | |
520 encoder = dsync_serializer_encode_begin(slave->serializers[ITEM_HANDSHAKE]); | |
521 if (set->sync_ns_prefix != NULL) { | |
522 dsync_serializer_encode_add(encoder, "sync_ns_prefix", | |
523 set->sync_ns_prefix); | |
524 } | |
525 | |
526 sync_type[0] = sync_type[1] = '\0'; | |
527 switch (set->sync_type) { | |
528 case DSYNC_BRAIN_SYNC_TYPE_UNKNOWN: | |
529 break; | |
530 case DSYNC_BRAIN_SYNC_TYPE_FULL: | |
531 sync_type[0] = 'f'; | |
532 break; | |
533 case DSYNC_BRAIN_SYNC_TYPE_CHANGED: | |
534 sync_type[0] = 'c'; | |
535 break; | |
536 case DSYNC_BRAIN_SYNC_TYPE_STATE: | |
537 sync_type[0] = 's'; | |
538 break; | |
539 } | |
540 i_assert(sync_type[0] != '\0'); | |
541 dsync_serializer_encode_add(encoder, "sync_type", sync_type); | |
542 if (set->guid_requests) | |
543 dsync_serializer_encode_add(encoder, "guid_requests", ""); | |
544 if (set->mails_have_guids) | |
545 dsync_serializer_encode_add(encoder, "mails_have_guids", ""); | |
546 | |
547 dsync_serializer_encode_finish(&encoder, str); | |
548 dsync_slave_io_send_string(slave, str); | |
549 } | |
550 | |
551 static enum dsync_slave_recv_ret | |
552 dsync_slave_io_recv_handshake(struct dsync_slave *_slave, | |
553 const struct dsync_slave_settings **set_r) | |
554 { | |
555 struct dsync_slave_io *slave = (struct dsync_slave_io *)_slave; | |
556 struct dsync_deserializer_decoder *decoder; | |
557 struct dsync_slave_settings *set; | |
558 const char *value; | |
559 pool_t pool = slave->ret_pool; | |
560 enum dsync_slave_recv_ret ret; | |
561 | |
562 ret = dsync_slave_io_input_next(slave, ITEM_HANDSHAKE, &decoder); | |
563 if (ret != DSYNC_SLAVE_RECV_RET_OK) { | |
564 if (ret != DSYNC_SLAVE_RECV_RET_TRYAGAIN) { | |
565 i_error("dsync(%s): Unexpected input in handshake", | |
566 slave->name); | |
567 dsync_slave_io_stop(slave); | |
568 } | |
569 return DSYNC_SLAVE_RECV_RET_TRYAGAIN; | |
570 } | |
571 | |
572 p_clear(pool); | |
573 set = p_new(pool, struct dsync_slave_settings, 1); | |
574 | |
575 if (dsync_deserializer_decode_try(decoder, "sync_ns_prefix", &value)) | |
576 set->sync_ns_prefix = p_strdup(pool, value); | |
577 if (dsync_deserializer_decode_try(decoder, "sync_type", &value)) { | |
578 switch (value[0]) { | |
579 case 'f': | |
580 set->sync_type = DSYNC_BRAIN_SYNC_TYPE_FULL; | |
581 break; | |
582 case 'c': | |
583 set->sync_type = DSYNC_BRAIN_SYNC_TYPE_CHANGED; | |
584 break; | |
585 case 's': | |
586 set->sync_type = DSYNC_BRAIN_SYNC_TYPE_STATE; | |
587 break; | |
588 default: | |
589 dsync_slave_input_error(slave, decoder, | |
590 "Unknown sync_type: %s", value); | |
591 return DSYNC_SLAVE_RECV_RET_TRYAGAIN; | |
592 } | |
593 } | |
594 if (dsync_deserializer_decode_try(decoder, "guid_requests", &value)) | |
595 set->guid_requests = TRUE; | |
596 if (dsync_deserializer_decode_try(decoder, "mails_have_guids", &value)) | |
597 set->mails_have_guids = TRUE; | |
598 | |
599 *set_r = set; | |
600 return DSYNC_SLAVE_RECV_RET_OK; | |
601 } | |
602 | |
603 static void | |
604 dsync_slave_io_send_end_of_list(struct dsync_slave *_slave) | |
605 { | |
606 struct dsync_slave_io *slave = (struct dsync_slave_io *)_slave; | |
607 | |
608 i_assert(slave->mail_output == NULL); | |
609 | |
610 o_stream_send_str(slave->output, END_OF_LIST_LINE"\n"); | |
611 } | |
612 | |
613 static void | |
614 dsync_slave_io_send_mailbox_state(struct dsync_slave *_slave, | |
615 const struct dsync_mailbox_state *state) | |
616 { | |
617 struct dsync_slave_io *slave = (struct dsync_slave_io *)_slave; | |
618 struct dsync_serializer_encoder *encoder; | |
619 string_t *str = t_str_new(128); | |
620 | |
621 str_append_c(str, items[ITEM_MAILBOX_STATE].chr); | |
622 encoder = dsync_serializer_encode_begin(slave->serializers[ITEM_MAILBOX_STATE]); | |
623 dsync_serializer_encode_add(encoder, "mailbox_guid", | |
624 guid_128_to_string(state->mailbox_guid)); | |
625 dsync_serializer_encode_add(encoder, "last_uidvalidity", | |
626 dec2str(state->last_uidvalidity)); | |
627 dsync_serializer_encode_add(encoder, "last_common_uid", | |
628 dec2str(state->last_common_uid)); | |
629 dsync_serializer_encode_add(encoder, "last_common_modseq", | |
630 dec2str(state->last_common_modseq)); | |
631 | |
632 dsync_serializer_encode_finish(&encoder, str); | |
633 dsync_slave_io_send_string(slave, str); | |
634 } | |
635 | |
636 static enum dsync_slave_recv_ret | |
637 dsync_slave_io_recv_mailbox_state(struct dsync_slave *_slave, | |
638 struct dsync_mailbox_state *state_r) | |
639 { | |
640 struct dsync_slave_io *slave = (struct dsync_slave_io *)_slave; | |
641 struct dsync_deserializer_decoder *decoder; | |
642 const char *value; | |
643 enum dsync_slave_recv_ret ret; | |
644 | |
645 memset(state_r, 0, sizeof(*state_r)); | |
646 | |
647 ret = dsync_slave_io_input_next(slave, ITEM_MAILBOX_STATE, &decoder); | |
648 if (ret != DSYNC_SLAVE_RECV_RET_OK) | |
649 return ret; | |
650 | |
651 value = dsync_deserializer_decode_get(decoder, "mailbox_guid"); | |
652 if (guid_128_from_string(value, state_r->mailbox_guid) < 0) { | |
653 dsync_slave_input_error(slave, decoder, "Invalid mailbox_guid"); | |
654 return DSYNC_SLAVE_RECV_RET_TRYAGAIN; | |
655 } | |
656 value = dsync_deserializer_decode_get(decoder, "last_uidvalidity"); | |
657 if (str_to_uint32(value, &state_r->last_uidvalidity) < 0) { | |
658 dsync_slave_input_error(slave, decoder, "Invalid last_uidvalidity"); | |
659 return DSYNC_SLAVE_RECV_RET_TRYAGAIN; | |
660 } | |
661 value = dsync_deserializer_decode_get(decoder, "last_common_uid"); | |
662 if (str_to_uint32(value, &state_r->last_uidvalidity) < 0) { | |
663 dsync_slave_input_error(slave, decoder, "Invalid last_common_uid"); | |
664 return DSYNC_SLAVE_RECV_RET_TRYAGAIN; | |
665 } | |
666 value = dsync_deserializer_decode_get(decoder, "last_common_modseq"); | |
667 if (str_to_uint32(value, &state_r->last_uidvalidity) < 0) { | |
668 dsync_slave_input_error(slave, decoder, "Invalid last_common_modseq"); | |
669 return DSYNC_SLAVE_RECV_RET_TRYAGAIN; | |
670 } | |
671 return DSYNC_SLAVE_RECV_RET_OK; | |
672 } | |
673 | |
674 static void | |
675 dsync_slave_io_send_mailbox_tree_node(struct dsync_slave *_slave, | |
676 const char *const *name, | |
677 const struct dsync_mailbox_node *node) | |
678 { | |
679 struct dsync_slave_io *slave = (struct dsync_slave_io *)_slave; | |
680 struct dsync_serializer_encoder *encoder; | |
681 string_t *str, *namestr; | |
682 | |
683 i_assert(*name != NULL); | |
684 | |
685 str = t_str_new(128); | |
686 str_append_c(str, items[ITEM_MAILBOX_TREE_NODE].chr); | |
687 | |
688 /* convert all hierarchy separators to tabs. mailbox names really | |
689 aren't supposed to have any tabs, but escape them anyway if there | |
690 are. */ | |
691 namestr = t_str_new(128); | |
692 for (; *name != NULL; name++) { | |
693 str_tabescape_write(namestr, *name); | |
694 str_append_c(namestr, '\t'); | |
695 } | |
696 str_truncate(namestr, str_len(namestr)-1); | |
697 | |
698 encoder = dsync_serializer_encode_begin(slave->serializers[ITEM_MAILBOX_TREE_NODE]); | |
699 dsync_serializer_encode_add(encoder, "name", str_c(namestr)); | |
700 switch (node->existence) { | |
701 case DSYNC_MAILBOX_NODE_NONEXISTENT: | |
702 dsync_serializer_encode_add(encoder, "existence", "n"); | |
703 break; | |
704 case DSYNC_MAILBOX_NODE_EXISTS: | |
705 dsync_serializer_encode_add(encoder, "existence", "y"); | |
706 break; | |
707 case DSYNC_MAILBOX_NODE_DELETED: | |
708 dsync_serializer_encode_add(encoder, "existence", "d"); | |
709 break; | |
710 } | |
711 | |
712 if (!guid_128_is_empty(node->mailbox_guid)) { | |
713 dsync_serializer_encode_add(encoder, "mailbox_guid", | |
714 guid_128_to_string(node->mailbox_guid)); | |
715 } | |
716 if (node->uid_validity != 0) { | |
717 dsync_serializer_encode_add(encoder, "uid_validity", | |
718 dec2str(node->uid_validity)); | |
719 } | |
720 if (node->last_renamed != 0) { | |
721 dsync_serializer_encode_add(encoder, "last_renamed", | |
722 dec2str(node->last_renamed)); | |
723 } | |
724 if (node->last_subscription_change != 0) { | |
725 dsync_serializer_encode_add(encoder, "last_subscription_change", | |
726 dec2str(node->last_subscription_change)); | |
727 } | |
728 if (node->subscribed) | |
729 dsync_serializer_encode_add(encoder, "subscribed", ""); | |
730 dsync_serializer_encode_finish(&encoder, str); | |
731 dsync_slave_io_send_string(slave, str); | |
732 } | |
733 | |
734 static enum dsync_slave_recv_ret | |
735 dsync_slave_io_recv_mailbox_tree_node(struct dsync_slave *_slave, | |
736 const char *const **name_r, | |
737 const struct dsync_mailbox_node **node_r) | |
738 { | |
739 struct dsync_slave_io *slave = (struct dsync_slave_io *)_slave; | |
740 struct dsync_deserializer_decoder *decoder; | |
741 struct dsync_mailbox_node *node; | |
742 const char *value; | |
743 enum dsync_slave_recv_ret ret; | |
744 | |
745 ret = dsync_slave_io_input_next(slave, ITEM_MAILBOX_TREE_NODE, &decoder); | |
746 if (ret != DSYNC_SLAVE_RECV_RET_OK) | |
747 return ret; | |
748 | |
749 p_clear(slave->ret_pool); | |
750 node = p_new(slave->ret_pool, struct dsync_mailbox_node, 1); | |
751 | |
752 value = dsync_deserializer_decode_get(decoder, "name"); | |
753 if (*value == '\0') { | |
754 dsync_slave_input_error(slave, decoder, "Empty name"); | |
755 return DSYNC_SLAVE_RECV_RET_TRYAGAIN; | |
756 } | |
757 *name_r = (void *)p_strsplit_tabescaped(slave->ret_pool, value); | |
758 | |
759 value = dsync_deserializer_decode_get(decoder, "existence"); | |
760 switch (*value) { | |
761 case 'n': | |
762 node->existence = DSYNC_MAILBOX_NODE_NONEXISTENT; | |
763 break; | |
764 case 'y': | |
765 node->existence = DSYNC_MAILBOX_NODE_EXISTS; | |
766 break; | |
767 case 'd': | |
768 node->existence = DSYNC_MAILBOX_NODE_DELETED; | |
769 break; | |
770 } | |
771 | |
772 if (dsync_deserializer_decode_try(decoder, "mailbox_guid", &value) && | |
773 guid_128_from_string(value, node->mailbox_guid) < 0) { | |
774 dsync_slave_input_error(slave, decoder, "Invalid mailbox_guid"); | |
775 return DSYNC_SLAVE_RECV_RET_TRYAGAIN; | |
776 } | |
777 if (dsync_deserializer_decode_try(decoder, "uid_validity", &value) && | |
778 str_to_uint32(value, &node->uid_validity) < 0) { | |
779 dsync_slave_input_error(slave, decoder, "Invalid uid_validity"); | |
780 return DSYNC_SLAVE_RECV_RET_TRYAGAIN; | |
781 } | |
782 if (dsync_deserializer_decode_try(decoder, "last_renamed", &value) && | |
783 str_to_time(value, &node->last_renamed) < 0) { | |
784 dsync_slave_input_error(slave, decoder, "Invalid last_renamed"); | |
785 return DSYNC_SLAVE_RECV_RET_TRYAGAIN; | |
786 } | |
787 if (dsync_deserializer_decode_try(decoder, "last_subscription_change", &value) && | |
788 str_to_time(value, &node->last_subscription_change) < 0) { | |
789 dsync_slave_input_error(slave, decoder, "Invalid last_subscription_change"); | |
790 return DSYNC_SLAVE_RECV_RET_TRYAGAIN; | |
791 } | |
792 if (dsync_deserializer_decode_try(decoder, "subscribed", &value)) | |
793 node->subscribed = TRUE; | |
794 | |
795 *node_r = node; | |
796 return DSYNC_SLAVE_RECV_RET_OK; | |
797 } | |
798 | |
799 static void | |
800 dsync_slave_io_send_mailbox_deletes(struct dsync_slave *_slave, | |
801 const struct dsync_mailbox_delete *deletes, | |
802 unsigned int count, char hierarchy_sep) | |
803 { | |
804 struct dsync_slave_io *slave = (struct dsync_slave_io *)_slave; | |
805 struct dsync_serializer_encoder *encoder; | |
806 string_t *str, *guidstr; | |
807 char sep[2]; | |
808 unsigned int i; | |
809 | |
810 str = t_str_new(128); | |
811 str_append_c(str, items[ITEM_MAILBOX_DELETE].chr); | |
812 | |
813 encoder = dsync_serializer_encode_begin(slave->serializers[ITEM_MAILBOX_DELETE]); | |
814 sep[0] = hierarchy_sep; sep[1] = '\0'; | |
815 dsync_serializer_encode_add(encoder, "hierarchy_sep", sep); | |
816 | |
817 guidstr = t_str_new(128); | |
818 for (i = 0; i < count; i++) { | |
819 if (deletes[i].delete_mailbox) { | |
820 str_append(guidstr, guid_128_to_string(deletes[i].guid)); | |
821 str_append_c(guidstr, ' '); | |
822 } | |
823 } | |
824 if (str_len(guidstr) > 0) { | |
825 str_truncate(guidstr, str_len(guidstr)-1); | |
826 dsync_serializer_encode_add(encoder, "mailboxes", | |
827 str_c(guidstr)); | |
828 } | |
829 | |
830 str_truncate(guidstr, 0); | |
831 for (i = 0; i < count; i++) { | |
832 if (!deletes[i].delete_mailbox) { | |
833 str_append(guidstr, guid_128_to_string(deletes[i].guid)); | |
834 str_append_c(guidstr, ' '); | |
835 } | |
836 } | |
837 if (str_len(guidstr) > 0) { | |
838 str_truncate(guidstr, str_len(guidstr)-1); | |
839 dsync_serializer_encode_add(encoder, "dirs", str_c(guidstr)); | |
840 } | |
841 dsync_serializer_encode_finish(&encoder, str); | |
842 dsync_slave_io_send_string(slave, str); | |
843 } | |
844 | |
845 ARRAY_DEFINE_TYPE(dsync_mailbox_delete, struct dsync_mailbox_delete); | |
846 static int | |
847 decode_mailbox_deletes(ARRAY_TYPE(dsync_mailbox_delete) *deletes, | |
848 const char *value, bool delete_mailbox) | |
849 { | |
850 struct dsync_mailbox_delete *del; | |
851 const char *const *guid_strings; | |
852 unsigned int i; | |
853 | |
854 guid_strings = t_strsplit(value, " "); | |
855 for (i = 0; guid_strings[i] != NULL; i++) { | |
856 del = array_append_space(deletes); | |
857 del->delete_mailbox = delete_mailbox; | |
858 if (guid_128_from_string(guid_strings[i], del->guid) < 0) | |
859 return -1; | |
860 } | |
861 return 0; | |
862 } | |
863 | |
864 static enum dsync_slave_recv_ret | |
865 dsync_slave_io_recv_mailbox_deletes(struct dsync_slave *_slave, | |
866 const struct dsync_mailbox_delete **deletes_r, | |
867 unsigned int *count_r, char *hierarchy_sep_r) | |
868 { | |
869 struct dsync_slave_io *slave = (struct dsync_slave_io *)_slave; | |
870 struct dsync_deserializer_decoder *decoder; | |
871 ARRAY_TYPE(dsync_mailbox_delete) deletes; | |
872 const char *value; | |
873 enum dsync_slave_recv_ret ret; | |
874 | |
875 ret = dsync_slave_io_input_next(slave, ITEM_MAILBOX_DELETE, &decoder); | |
876 if (ret != DSYNC_SLAVE_RECV_RET_OK) | |
877 return ret; | |
878 | |
879 p_clear(slave->ret_pool); | |
880 p_array_init(&deletes, slave->ret_pool, 16); | |
881 | |
882 value = dsync_deserializer_decode_get(decoder, "hierarchy_sep"); | |
883 if (strlen(value) != 1) { | |
884 dsync_slave_input_error(slave, decoder, "Invalid hierarchy_sep"); | |
885 return DSYNC_SLAVE_RECV_RET_TRYAGAIN; | |
886 } | |
887 *hierarchy_sep_r = value[0]; | |
888 | |
889 if (dsync_deserializer_decode_try(decoder, "mailboxes", &value) && | |
890 decode_mailbox_deletes(&deletes, value, TRUE) < 0) { | |
891 dsync_slave_input_error(slave, decoder, "Invalid mailboxes"); | |
892 return DSYNC_SLAVE_RECV_RET_TRYAGAIN; | |
893 } | |
894 if (dsync_deserializer_decode_try(decoder, "dirs", &value) && | |
895 decode_mailbox_deletes(&deletes, value, FALSE) < 0) { | |
896 dsync_slave_input_error(slave, decoder, "Invalid dirs"); | |
897 return DSYNC_SLAVE_RECV_RET_TRYAGAIN; | |
898 } | |
899 *deletes_r = array_get(&deletes, count_r); | |
900 return DSYNC_SLAVE_RECV_RET_OK; | |
901 } | |
902 | |
903 static const char * | |
904 get_cache_fields(struct dsync_slave_io *slave, | |
905 const struct dsync_mailbox *dsync_box) | |
906 { | |
907 struct dsync_serializer_encoder *encoder; | |
908 string_t *str; | |
909 const struct mailbox_cache_field *cache_fields; | |
910 unsigned int i, count; | |
911 char decision[3]; | |
912 | |
913 cache_fields = array_get(&dsync_box->cache_fields, &count); | |
914 if (count == 0) | |
915 return ""; | |
916 | |
917 str = t_str_new(128); | |
918 for (i = 0; i < count; i++) { | |
919 const struct mailbox_cache_field *field = &cache_fields[i]; | |
920 | |
921 encoder = dsync_serializer_encode_begin(slave->serializers[ITEM_MAILBOX_CACHE_FIELD]); | |
922 dsync_serializer_encode_add(encoder, "name", field->name); | |
923 | |
924 memset(decision, 0, sizeof(decision)); | |
925 switch (field->decision & ~MAIL_CACHE_DECISION_FORCED) { | |
926 case MAIL_CACHE_DECISION_NO: | |
927 decision[0] = 'n'; | |
928 break; | |
929 case MAIL_CACHE_DECISION_TEMP: | |
930 decision[0] = 't'; | |
931 break; | |
932 case MAIL_CACHE_DECISION_YES: | |
933 decision[0] = 'y'; | |
934 break; | |
935 } | |
936 i_assert(decision[0] != '\0'); | |
937 if ((field->decision & MAIL_CACHE_DECISION_FORCED) != 0) | |
938 decision[1] = 'F'; | |
939 dsync_serializer_encode_add(encoder, "decision", decision); | |
940 if (field->last_used != 0) { | |
941 dsync_serializer_encode_add(encoder, "last_used", | |
942 dec2str(field->last_used)); | |
943 } | |
944 dsync_serializer_encode_finish(&encoder, str); | |
945 } | |
946 if (i > 0) { | |
947 /* remove the trailing LF */ | |
948 str_truncate(str, str_len(str)-1); | |
949 } | |
950 return str_c(str); | |
951 } | |
952 | |
953 static void | |
954 dsync_slave_io_send_mailbox(struct dsync_slave *_slave, | |
955 const struct dsync_mailbox *dsync_box) | |
956 { | |
957 struct dsync_slave_io *slave = (struct dsync_slave_io *)_slave; | |
958 struct dsync_serializer_encoder *encoder; | |
959 string_t *str = t_str_new(128); | |
960 const char *value; | |
961 | |
962 str_append_c(str, items[ITEM_MAILBOX].chr); | |
963 encoder = dsync_serializer_encode_begin(slave->serializers[ITEM_MAILBOX]); | |
964 dsync_serializer_encode_add(encoder, "mailbox_guid", | |
965 guid_128_to_string(dsync_box->mailbox_guid)); | |
966 | |
967 if (dsync_box->mailbox_lost) | |
968 dsync_serializer_encode_add(encoder, "mailbox_lost", ""); | |
969 dsync_serializer_encode_add(encoder, "uid_validity", | |
970 dec2str(dsync_box->uid_validity)); | |
971 dsync_serializer_encode_add(encoder, "uid_next", | |
972 dec2str(dsync_box->uid_next)); | |
973 dsync_serializer_encode_add(encoder, "messages_count", | |
974 dec2str(dsync_box->messages_count)); | |
975 dsync_serializer_encode_add(encoder, "first_recent_uid", | |
976 dec2str(dsync_box->first_recent_uid)); | |
977 dsync_serializer_encode_add(encoder, "highest_modseq", | |
978 dec2str(dsync_box->highest_modseq)); | |
979 | |
980 value = get_cache_fields(slave, dsync_box); | |
981 if (value != NULL) | |
982 dsync_serializer_encode_add(encoder, "cache_fields", value); | |
983 | |
984 dsync_serializer_encode_finish(&encoder, str); | |
985 dsync_slave_io_send_string(slave, str); | |
986 } | |
987 | |
988 static int | |
989 parse_cache_field(struct dsync_slave_io *slave, struct dsync_mailbox *box, | |
990 const char *value) | |
991 { | |
992 struct dsync_deserializer_decoder *decoder; | |
993 struct mailbox_cache_field field; | |
994 const char *error; | |
995 int ret = 0; | |
996 | |
997 if (dsync_deserializer_decode_begin(slave->deserializers[ITEM_MAILBOX_CACHE_FIELD], | |
998 value, &decoder, &error) < 0) { | |
999 dsync_slave_input_error(slave, NULL, | |
1000 "cache_field: Invalid input: %s", error); | |
1001 return -1; | |
1002 } | |
1003 | |
1004 memset(&field, 0, sizeof(field)); | |
1005 value = dsync_deserializer_decode_get(decoder, "name"); | |
1006 field.name = p_strdup(slave->ret_pool, value); | |
1007 | |
1008 value = dsync_deserializer_decode_get(decoder, "decision"); | |
1009 switch (*value) { | |
1010 case 'n': | |
1011 field.decision = MAIL_CACHE_DECISION_NO; | |
1012 break; | |
1013 case 't': | |
1014 field.decision = MAIL_CACHE_DECISION_TEMP; | |
1015 break; | |
1016 case 'y': | |
1017 field.decision = MAIL_CACHE_DECISION_YES; | |
1018 break; | |
1019 default: | |
1020 dsync_slave_input_error(slave, decoder, "Invalid decision: %s", | |
1021 value); | |
1022 ret = -1; | |
1023 break; | |
1024 } | |
1025 if (value[1] == 'F') | |
1026 field.decision |= MAIL_CACHE_DECISION_FORCED; | |
1027 | |
1028 if (dsync_deserializer_decode_try(decoder, "last_used", &value) && | |
1029 str_to_time(value, &field.last_used) < 0) { | |
1030 dsync_slave_input_error(slave, decoder, "Invalid last_used"); | |
1031 ret = -1; | |
1032 } | |
1033 array_append(&box->cache_fields, &field, 1); | |
1034 | |
1035 dsync_deserializer_decode_finish(&decoder); | |
1036 return ret; | |
1037 } | |
1038 | |
1039 static enum dsync_slave_recv_ret | |
1040 dsync_slave_io_recv_mailbox(struct dsync_slave *_slave, | |
1041 const struct dsync_mailbox **dsync_box_r) | |
1042 { | |
1043 struct dsync_slave_io *slave = (struct dsync_slave_io *)_slave; | |
1044 pool_t pool = slave->ret_pool; | |
1045 struct dsync_deserializer_decoder *decoder; | |
1046 struct dsync_mailbox *box; | |
1047 const char *value; | |
1048 enum dsync_slave_recv_ret ret; | |
1049 | |
1050 p_clear(pool); | |
1051 box = p_new(pool, struct dsync_mailbox, 1); | |
1052 | |
1053 ret = dsync_slave_io_input_next(slave, ITEM_MAILBOX, &decoder); | |
1054 if (ret != DSYNC_SLAVE_RECV_RET_OK) | |
1055 return ret; | |
1056 | |
1057 value = dsync_deserializer_decode_get(decoder, "mailbox_guid"); | |
1058 if (guid_128_from_string(value, box->mailbox_guid) < 0) { | |
1059 dsync_slave_input_error(slave, decoder, "Invalid mailbox_guid"); | |
1060 return DSYNC_SLAVE_RECV_RET_TRYAGAIN; | |
1061 } | |
1062 | |
1063 if (dsync_deserializer_decode_try(decoder, "mailbox_lost", &value)) | |
1064 box->mailbox_lost = TRUE; | |
1065 value = dsync_deserializer_decode_get(decoder, "uid_validity"); | |
1066 if (str_to_uint32(value, &box->uid_validity) < 0) { | |
1067 dsync_slave_input_error(slave, decoder, "Invalid uid_validity"); | |
1068 return DSYNC_SLAVE_RECV_RET_TRYAGAIN; | |
1069 } | |
1070 value = dsync_deserializer_decode_get(decoder, "uid_next"); | |
1071 if (str_to_uint32(value, &box->uid_next) < 0) { | |
1072 dsync_slave_input_error(slave, decoder, "Invalid uid_next"); | |
1073 return DSYNC_SLAVE_RECV_RET_TRYAGAIN; | |
1074 } | |
1075 value = dsync_deserializer_decode_get(decoder, "messages_count"); | |
1076 if (str_to_uint32(value, &box->messages_count) < 0) { | |
1077 dsync_slave_input_error(slave, decoder, "Invalid messages_count"); | |
1078 return DSYNC_SLAVE_RECV_RET_TRYAGAIN; | |
1079 } | |
1080 value = dsync_deserializer_decode_get(decoder, "first_recent_uid"); | |
1081 if (str_to_uint32(value, &box->first_recent_uid) < 0) { | |
1082 dsync_slave_input_error(slave, decoder, "Invalid first_recent_uid"); | |
1083 return DSYNC_SLAVE_RECV_RET_TRYAGAIN; | |
1084 } | |
1085 value = dsync_deserializer_decode_get(decoder, "highest_modseq"); | |
1086 if (str_to_uint64(value, &box->highest_modseq) < 0) { | |
1087 dsync_slave_input_error(slave, decoder, "Invalid highest_modseq"); | |
1088 return DSYNC_SLAVE_RECV_RET_TRYAGAIN; | |
1089 } | |
1090 | |
1091 p_array_init(&box->cache_fields, pool, 32); | |
1092 if (dsync_deserializer_decode_try(decoder, "cache_fields", &value)) { | |
1093 const char *const *fields = t_strsplit(value, "\n"); | |
1094 for (; *fields != NULL; fields++) { | |
1095 if (parse_cache_field(slave, box, *fields) < 0) | |
1096 return DSYNC_SLAVE_RECV_RET_TRYAGAIN; | |
1097 } | |
1098 } | |
1099 | |
1100 *dsync_box_r = box; | |
1101 return DSYNC_SLAVE_RECV_RET_OK; | |
1102 } | |
1103 | |
1104 static void | |
1105 dsync_slave_io_send_change(struct dsync_slave *_slave, | |
1106 const struct dsync_mail_change *change) | |
1107 { | |
1108 struct dsync_slave_io *slave = (struct dsync_slave_io *)_slave; | |
1109 struct dsync_serializer_encoder *encoder; | |
1110 string_t *str = t_str_new(128); | |
1111 char type[2]; | |
1112 | |
1113 str_append_c(str, items[ITEM_MAIL_CHANGE].chr); | |
1114 encoder = dsync_serializer_encode_begin(slave->serializers[ITEM_MAIL_CHANGE]); | |
1115 | |
1116 type[0] = type[1] = '\0'; | |
1117 switch (change->type) { | |
1118 case DSYNC_MAIL_CHANGE_TYPE_SAVE: | |
1119 type[0] = 's'; | |
1120 break; | |
1121 case DSYNC_MAIL_CHANGE_TYPE_EXPUNGE: | |
1122 type[0] = 'e'; | |
1123 break; | |
1124 case DSYNC_MAIL_CHANGE_TYPE_FLAG_CHANGE: | |
1125 type[0] = 'f'; | |
1126 break; | |
1127 } | |
1128 i_assert(type[0] != '\0'); | |
1129 dsync_serializer_encode_add(encoder, "type", type); | |
1130 dsync_serializer_encode_add(encoder, "uid", dec2str(change->uid)); | |
1131 if (change->guid != NULL) | |
1132 dsync_serializer_encode_add(encoder, "guid", change->guid); | |
1133 if (change->hdr_hash != NULL) { | |
1134 dsync_serializer_encode_add(encoder, "hdr_hash", | |
1135 change->hdr_hash); | |
1136 } | |
1137 if (change->modseq != 0) { | |
1138 dsync_serializer_encode_add(encoder, "modseq", | |
1139 dec2str(change->modseq)); | |
1140 } | |
1141 if (change->save_timestamp != 0) { | |
1142 dsync_serializer_encode_add(encoder, "save_timestamp", | |
1143 dec2str(change->save_timestamp)); | |
1144 } | |
1145 if (change->add_flags != 0) { | |
1146 dsync_serializer_encode_add(encoder, "add_flags", | |
1147 t_strdup_printf("%x", change->add_flags)); | |
1148 } | |
1149 if (change->remove_flags != 0) { | |
1150 dsync_serializer_encode_add(encoder, "remove_flags", | |
1151 t_strdup_printf("%x", change->remove_flags)); | |
1152 } | |
1153 if (change->final_flags != 0) { | |
1154 dsync_serializer_encode_add(encoder, "final_flags", | |
1155 t_strdup_printf("%x", change->final_flags)); | |
1156 } | |
1157 if (change->keywords_reset) | |
1158 dsync_serializer_encode_add(encoder, "keywords_reset", ""); | |
1159 | |
1160 if (array_is_created(&change->keyword_changes) && | |
1161 array_count(&change->keyword_changes) > 0) { | |
1162 string_t *kw_str = t_str_new(128); | |
1163 const char *const *changes; | |
1164 unsigned int i, count; | |
1165 | |
1166 changes = array_get(&change->keyword_changes, &count); | |
1167 str_tabescape_write(kw_str, changes[0]); | |
1168 for (i = 1; i < count; i++) { | |
1169 str_append_c(kw_str, '\t'); | |
1170 str_tabescape_write(kw_str, changes[i]); | |
1171 } | |
1172 dsync_serializer_encode_add(encoder, "keyword_changes", | |
1173 str_c(kw_str)); | |
1174 } | |
1175 | |
1176 dsync_serializer_encode_finish(&encoder, str); | |
1177 dsync_slave_io_send_string(slave, str); | |
1178 } | |
1179 | |
1180 static enum dsync_slave_recv_ret | |
1181 dsync_slave_io_recv_change(struct dsync_slave *_slave, | |
1182 const struct dsync_mail_change **change_r) | |
1183 { | |
1184 struct dsync_slave_io *slave = (struct dsync_slave_io *)_slave; | |
1185 pool_t pool = slave->ret_pool; | |
1186 struct dsync_deserializer_decoder *decoder; | |
1187 struct dsync_mail_change *change; | |
1188 const char *value; | |
1189 enum dsync_slave_recv_ret ret; | |
1190 | |
1191 p_clear(pool); | |
1192 change = p_new(pool, struct dsync_mail_change, 1); | |
1193 | |
1194 ret = dsync_slave_io_input_next(slave, ITEM_MAIL_CHANGE, &decoder); | |
1195 if (ret != DSYNC_SLAVE_RECV_RET_OK) | |
1196 return ret; | |
1197 | |
1198 value = dsync_deserializer_decode_get(decoder, "type"); | |
1199 switch (*value) { | |
1200 case 's': | |
1201 change->type = DSYNC_MAIL_CHANGE_TYPE_SAVE; | |
1202 break; | |
1203 case 'e': | |
1204 change->type = DSYNC_MAIL_CHANGE_TYPE_EXPUNGE; | |
1205 break; | |
1206 case 'f': | |
1207 change->type = DSYNC_MAIL_CHANGE_TYPE_FLAG_CHANGE; | |
1208 break; | |
1209 default: | |
1210 dsync_slave_input_error(slave, decoder, | |
1211 "Invalid type: %s", value); | |
1212 return DSYNC_SLAVE_RECV_RET_TRYAGAIN; | |
1213 } | |
1214 | |
1215 value = dsync_deserializer_decode_get(decoder, "uid"); | |
1216 if (str_to_uint32(value, &change->uid) < 0) { | |
1217 dsync_slave_input_error(slave, decoder, "Invalid uid"); | |
1218 return DSYNC_SLAVE_RECV_RET_TRYAGAIN; | |
1219 } | |
1220 | |
1221 if (dsync_deserializer_decode_try(decoder, "guid", &value)) | |
1222 change->guid = p_strdup(pool, value); | |
1223 if (dsync_deserializer_decode_try(decoder, "hdr_hash", &value)) | |
1224 change->hdr_hash = p_strdup(pool, value); | |
1225 if (dsync_deserializer_decode_try(decoder, "modseq", &value) && | |
1226 str_to_uint64(value, &change->modseq) < 0) { | |
1227 dsync_slave_input_error(slave, decoder, "Invalid modseq"); | |
1228 return DSYNC_SLAVE_RECV_RET_TRYAGAIN; | |
1229 } | |
1230 if (dsync_deserializer_decode_try(decoder, "save_timestamp", &value) && | |
1231 str_to_time(value, &change->save_timestamp) < 0) { | |
1232 dsync_slave_input_error(slave, decoder, "Invalid save_timestamp"); | |
1233 return DSYNC_SLAVE_RECV_RET_TRYAGAIN; | |
1234 } | |
1235 | |
1236 if (dsync_deserializer_decode_try(decoder, "add_flags", &value)) | |
1237 change->add_flags = strtoul(value, NULL, 16); | |
1238 if (dsync_deserializer_decode_try(decoder, "remove_flags", &value)) | |
1239 change->remove_flags = strtoul(value, NULL, 16); | |
1240 if (dsync_deserializer_decode_try(decoder, "final_flags", &value)) | |
1241 change->final_flags = strtoul(value, NULL, 16); | |
1242 if (dsync_deserializer_decode_try(decoder, "keywords_reset", &value)) | |
1243 change->keywords_reset = TRUE; | |
1244 | |
1245 if (dsync_deserializer_decode_try(decoder, "keyword_changes", &value) && | |
1246 *value != '\0') { | |
1247 const char *const *changes = t_strsplit_tab(value); | |
1248 unsigned int i, count = str_array_length(changes); | |
1249 | |
1250 p_array_init(&change->keyword_changes, pool, count); | |
1251 for (i = 0; i < count; i++) { | |
1252 value = p_strdup(pool, changes[i]); | |
1253 array_append(&change->keyword_changes, &value, 1); | |
1254 } | |
1255 } | |
1256 | |
1257 *change_r = change; | |
1258 return DSYNC_SLAVE_RECV_RET_OK; | |
1259 } | |
1260 | |
1261 static void | |
1262 dsync_slave_io_send_mail_request(struct dsync_slave *_slave, | |
1263 const struct dsync_mail_request *request) | |
1264 { | |
1265 struct dsync_slave_io *slave = (struct dsync_slave_io *)_slave; | |
1266 struct dsync_serializer_encoder *encoder; | |
1267 string_t *str = t_str_new(128); | |
1268 | |
1269 str_append_c(str, items[ITEM_MAIL_REQUEST].chr); | |
1270 encoder = dsync_serializer_encode_begin(slave->serializers[ITEM_MAIL_REQUEST]); | |
1271 if (request->guid != NULL) | |
1272 dsync_serializer_encode_add(encoder, "guid", request->guid); | |
1273 if (request->uid != 0) { | |
1274 dsync_serializer_encode_add(encoder, "uid", | |
1275 dec2str(request->uid)); | |
1276 } | |
1277 dsync_serializer_encode_finish(&encoder, str); | |
1278 dsync_slave_io_send_string(slave, str); | |
1279 } | |
1280 | |
1281 static enum dsync_slave_recv_ret | |
1282 dsync_slave_io_recv_mail_request(struct dsync_slave *_slave, | |
1283 const struct dsync_mail_request **request_r) | |
1284 { | |
1285 struct dsync_slave_io *slave = (struct dsync_slave_io *)_slave; | |
1286 struct dsync_deserializer_decoder *decoder; | |
1287 struct dsync_mail_request *request; | |
1288 const char *value; | |
1289 enum dsync_slave_recv_ret ret; | |
1290 | |
1291 p_clear(slave->ret_pool); | |
1292 request = p_new(slave->ret_pool, struct dsync_mail_request, 1); | |
1293 | |
1294 ret = dsync_slave_io_input_next(slave, ITEM_MAIL_REQUEST, &decoder); | |
1295 if (ret != DSYNC_SLAVE_RECV_RET_OK) | |
1296 return ret; | |
1297 | |
1298 if (dsync_deserializer_decode_try(decoder, "guid", &value)) | |
1299 request->guid = p_strdup(slave->ret_pool, value); | |
1300 if (dsync_deserializer_decode_try(decoder, "uid", &value) && | |
1301 str_to_uint32(value, &request->uid) < 0) { | |
1302 dsync_slave_input_error(slave, decoder, "Invalid uid"); | |
1303 return DSYNC_SLAVE_RECV_RET_TRYAGAIN; | |
1304 } | |
1305 | |
1306 *request_r = request; | |
1307 return DSYNC_SLAVE_RECV_RET_OK; | |
1308 } | |
1309 | |
1310 static void | |
1311 dsync_slave_io_send_mail(struct dsync_slave *_slave, | |
1312 const struct dsync_mail *mail) | |
1313 { | |
1314 struct dsync_slave_io *slave = (struct dsync_slave_io *)_slave; | |
1315 struct dsync_serializer_encoder *encoder; | |
1316 string_t *str = t_str_new(128); | |
1317 | |
1318 i_assert(slave->mail_output == NULL); | |
1319 | |
1320 str_append_c(str, items[ITEM_MAIL].chr); | |
1321 encoder = dsync_serializer_encode_begin(slave->serializers[ITEM_MAIL]); | |
1322 if (mail->guid != NULL) | |
1323 dsync_serializer_encode_add(encoder, "guid", mail->guid); | |
1324 if (mail->uid != 0) | |
1325 dsync_serializer_encode_add(encoder, "uid", dec2str(mail->uid)); | |
1326 if (mail->pop3_uidl != NULL) { | |
1327 dsync_serializer_encode_add(encoder, "pop3_uidl", | |
1328 mail->pop3_uidl); | |
1329 } | |
1330 if (mail->pop3_order > 0) { | |
1331 dsync_serializer_encode_add(encoder, "pop3_order", | |
1332 dec2str(mail->pop3_order)); | |
1333 } | |
1334 if (mail->received_date > 0) { | |
1335 dsync_serializer_encode_add(encoder, "received_date", | |
1336 dec2str(mail->received_date)); | |
1337 } | |
1338 if (mail->input != NULL) | |
1339 dsync_serializer_encode_add(encoder, "stream", ""); | |
1340 | |
1341 dsync_serializer_encode_finish(&encoder, str); | |
1342 dsync_slave_io_send_string(slave, str); | |
1343 | |
1344 if (mail->input != NULL) { | |
1345 slave->mail_output_last = '\0'; | |
1346 slave->mail_output = mail->input; | |
1347 i_stream_ref(slave->mail_output); | |
1348 (void)dsync_slave_io_send_mail_stream(slave); | |
1349 } | |
1350 } | |
1351 | |
1352 static int seekable_fd_callback(const char **path_r, void *context) | |
1353 { | |
1354 struct dsync_slave_io *slave = context; | |
1355 string_t *path; | |
1356 int fd; | |
1357 | |
1358 path = t_str_new(128); | |
1359 str_append(path, slave->temp_path_prefix); | |
1360 fd = safe_mkstemp(path, 0600, (uid_t)-1, (gid_t)-1); | |
1361 if (fd == -1) { | |
1362 i_error("safe_mkstemp(%s) failed: %m", str_c(path)); | |
1363 return -1; | |
1364 } | |
1365 | |
1366 /* we just want the fd, unlink it */ | |
1367 if (unlink(str_c(path)) < 0) { | |
1368 /* shouldn't happen.. */ | |
1369 i_error("unlink(%s) failed: %m", str_c(path)); | |
1370 close_keep_errno(fd); | |
1371 return -1; | |
1372 } | |
1373 | |
1374 *path_r = str_c(path); | |
1375 return fd; | |
1376 } | |
1377 | |
1378 static enum dsync_slave_recv_ret | |
1379 dsync_slave_io_recv_mail(struct dsync_slave *_slave, | |
1380 struct dsync_mail **mail_r) | |
1381 { | |
1382 struct dsync_slave_io *slave = (struct dsync_slave_io *)_slave; | |
1383 pool_t pool = slave->ret_pool; | |
1384 struct dsync_deserializer_decoder *decoder; | |
1385 struct dsync_mail *mail; | |
1386 struct istream *inputs[2]; | |
1387 const char *value; | |
1388 enum dsync_slave_recv_ret ret; | |
1389 | |
1390 if (slave->mail_input != NULL) { | |
1391 /* wait until the mail's stream has been read */ | |
1392 return DSYNC_SLAVE_RECV_RET_TRYAGAIN; | |
1393 } | |
1394 if (slave->cur_mail != NULL) { | |
1395 /* finished reading the stream, return the mail now */ | |
1396 *mail_r = slave->cur_mail; | |
1397 slave->cur_mail = NULL; | |
1398 return DSYNC_SLAVE_RECV_RET_OK; | |
1399 } | |
1400 | |
1401 p_clear(pool); | |
1402 mail = p_new(pool, struct dsync_mail, 1); | |
1403 | |
1404 ret = dsync_slave_io_input_next(slave, ITEM_MAIL, &decoder); | |
1405 if (ret != DSYNC_SLAVE_RECV_RET_OK) | |
1406 return ret; | |
1407 | |
1408 if (dsync_deserializer_decode_try(decoder, "guid", &value)) | |
1409 mail->guid = p_strdup(pool, value); | |
1410 if (dsync_deserializer_decode_try(decoder, "uid", &value) && | |
1411 str_to_uint32(value, &mail->uid) < 0) { | |
1412 dsync_slave_input_error(slave, decoder, "Invalid uid"); | |
1413 return DSYNC_SLAVE_RECV_RET_TRYAGAIN; | |
1414 } | |
1415 if (dsync_deserializer_decode_try(decoder, "pop3_uidl", &value)) | |
1416 mail->pop3_uidl = p_strdup(pool, value); | |
1417 if (dsync_deserializer_decode_try(decoder, "pop3_order", &value) && | |
1418 str_to_uint(value, &mail->pop3_order) < 0) { | |
1419 dsync_slave_input_error(slave, decoder, "Invalid pop3_order"); | |
1420 return DSYNC_SLAVE_RECV_RET_TRYAGAIN; | |
1421 } | |
1422 if (dsync_deserializer_decode_try(decoder, "received_date", &value) && | |
1423 str_to_time(value, &mail->received_date) < 0) { | |
1424 dsync_slave_input_error(slave, decoder, "Invalid received_date"); | |
1425 return DSYNC_SLAVE_RECV_RET_TRYAGAIN; | |
1426 } | |
1427 if (dsync_deserializer_decode_try(decoder, "stream", &value)) { | |
1428 inputs[0] = i_stream_create_dot(slave->input, FALSE); | |
1429 inputs[1] = NULL; | |
1430 mail->input = i_stream_create_seekable(inputs, | |
1431 MAIL_READ_FULL_BLOCK_SIZE, seekable_fd_callback, slave); | |
1432 i_stream_unref(&inputs[0]); | |
1433 | |
1434 slave->mail_input = mail->input; | |
1435 if (dsync_slave_io_read_mail_stream(slave) <= 0) { | |
1436 slave->cur_mail = mail; | |
1437 return DSYNC_SLAVE_RECV_RET_TRYAGAIN; | |
1438 } | |
1439 /* already finished reading the stream */ | |
1440 i_assert(slave->mail_input == NULL); | |
1441 } | |
1442 | |
1443 *mail_r = mail; | |
1444 return DSYNC_SLAVE_RECV_RET_OK; | |
1445 } | |
1446 | |
1447 static void dsync_slave_io_flush(struct dsync_slave *_slave) | |
1448 { | |
1449 struct dsync_slave_io *slave = (struct dsync_slave_io *)_slave; | |
1450 | |
1451 o_stream_uncork(slave->output); | |
1452 o_stream_cork(slave->output); | |
1453 } | |
1454 | |
1455 static bool dsync_slave_io_is_send_queue_full(struct dsync_slave *_slave) | |
1456 { | |
1457 struct dsync_slave_io *slave = (struct dsync_slave_io *)_slave; | |
1458 size_t bytes; | |
1459 | |
1460 if (slave->mail_output != NULL) | |
1461 return TRUE; | |
1462 | |
1463 bytes = o_stream_get_buffer_used_size(slave->output); | |
1464 if (bytes < DSYNC_SLAVE_IO_OUTBUF_THROTTLE_SIZE) | |
1465 return FALSE; | |
1466 | |
1467 o_stream_set_flush_pending(slave->output, TRUE); | |
1468 return TRUE; | |
1469 } | |
1470 | |
1471 static bool dsync_slave_io_has_pending_data(struct dsync_slave *_slave) | |
1472 { | |
1473 struct dsync_slave_io *slave = (struct dsync_slave_io *)_slave; | |
1474 | |
1475 return slave->has_pending_data; | |
1476 } | |
1477 | |
1478 static const struct dsync_slave_vfuncs dsync_slave_io_vfuncs = { | |
1479 dsync_slave_io_deinit, | |
1480 dsync_slave_io_send_handshake, | |
1481 dsync_slave_io_recv_handshake, | |
1482 dsync_slave_io_send_end_of_list, | |
1483 dsync_slave_io_send_mailbox_state, | |
1484 dsync_slave_io_recv_mailbox_state, | |
1485 dsync_slave_io_send_mailbox_tree_node, | |
1486 dsync_slave_io_recv_mailbox_tree_node, | |
1487 dsync_slave_io_send_mailbox_deletes, | |
1488 dsync_slave_io_recv_mailbox_deletes, | |
1489 dsync_slave_io_send_mailbox, | |
1490 dsync_slave_io_recv_mailbox, | |
1491 dsync_slave_io_send_change, | |
1492 dsync_slave_io_recv_change, | |
1493 dsync_slave_io_send_mail_request, | |
1494 dsync_slave_io_recv_mail_request, | |
1495 dsync_slave_io_send_mail, | |
1496 dsync_slave_io_recv_mail, | |
1497 dsync_slave_io_flush, | |
1498 dsync_slave_io_is_send_queue_full, | |
1499 dsync_slave_io_has_pending_data | |
1500 }; | |
1501 | |
1502 struct dsync_slave * | |
1503 dsync_slave_init_io(int fd_in, int fd_out, const char *name, | |
1504 const char *temp_path_prefix) | |
1505 { | |
1506 struct dsync_slave_io *slave; | |
1507 | |
1508 slave = i_new(struct dsync_slave_io, 1); | |
1509 slave->slave.v = dsync_slave_io_vfuncs; | |
1510 slave->fd_in = fd_in; | |
1511 slave->fd_out = fd_out; | |
1512 slave->name = i_strdup(name); | |
1513 slave->temp_path_prefix = i_strdup(temp_path_prefix); | |
1514 slave->ret_pool = pool_alloconly_create("slave io data", 2048); | |
1515 dsync_slave_io_init(slave); | |
1516 return &slave->slave; | |
1517 } |