annotate src/lib-storage/index/mbox/mbox-sync-rewrite.c @ 2164:e5f9d66d6ced HEAD

fixes
author Timo Sirainen <tss@iki.fi>
date Sat, 19 Jun 2004 03:19:48 +0300
parents 282e993b92c4
children a0173f504707
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1 #include "lib.h"
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
2 #include "buffer.h"
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
3 #include "istream.h"
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
4 #include "ostream.h"
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
5 #include "str.h"
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
6 #include "write-full.h"
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
7 #include "message-parser.h"
1981
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
8 #include "mbox-storage.h"
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
9 #include "mbox-sync-private.h"
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
10 #include "istream-raw-mbox.h"
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
11
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
12 int mbox_move(struct mbox_sync_context *sync_ctx,
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
13 uoff_t dest, uoff_t source, uoff_t size)
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
14 {
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
15 struct istream *input;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
16 struct ostream *output;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
17 off_t ret;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
18
2151
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
19 i_assert(size < OFF_T_MAX);
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
20
2107
8266299b92f5 expunging is somewhat working
Timo Sirainen <tss@iki.fi>
parents: 2041
diff changeset
21 if (size == 0 || source == dest)
8266299b92f5 expunging is somewhat working
Timo Sirainen <tss@iki.fi>
parents: 2041
diff changeset
22 return 0;
8266299b92f5 expunging is somewhat working
Timo Sirainen <tss@iki.fi>
parents: 2041
diff changeset
23
1981
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
24 istream_raw_mbox_flush(sync_ctx->input);
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
25
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
26 output = o_stream_create_file(sync_ctx->fd, default_pool, 4096, FALSE);
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
27 i_stream_seek(sync_ctx->file_input, source);
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
28 o_stream_seek(output, dest);
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
29
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
30 if (size == (uoff_t)-1) {
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
31 input = sync_ctx->file_input;
2107
8266299b92f5 expunging is somewhat working
Timo Sirainen <tss@iki.fi>
parents: 2041
diff changeset
32 ret = o_stream_send_istream(output, input) < 0 ? -1 : 0;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
33 } else {
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
34 input = i_stream_create_limit(default_pool,
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
35 sync_ctx->file_input,
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
36 source, size);
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
37 ret = o_stream_send_istream(output, input);
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
38 i_stream_unref(input);
2107
8266299b92f5 expunging is somewhat working
Timo Sirainen <tss@iki.fi>
parents: 2041
diff changeset
39 ret = ret == (off_t)size ? 0 : -1;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
40 }
2107
8266299b92f5 expunging is somewhat working
Timo Sirainen <tss@iki.fi>
parents: 2041
diff changeset
41
2124
24651c3ac7f3 major syncing code cleanups. the code finally looks almost readable. logic
Timo Sirainen <tss@iki.fi>
parents: 2119
diff changeset
42 if (ret < 0) {
24651c3ac7f3 major syncing code cleanups. the code finally looks almost readable. logic
Timo Sirainen <tss@iki.fi>
parents: 2119
diff changeset
43 errno = output->stream_errno;
24651c3ac7f3 major syncing code cleanups. the code finally looks almost readable. logic
Timo Sirainen <tss@iki.fi>
parents: 2119
diff changeset
44 mbox_set_syscall_error(sync_ctx->ibox,
24651c3ac7f3 major syncing code cleanups. the code finally looks almost readable. logic
Timo Sirainen <tss@iki.fi>
parents: 2119
diff changeset
45 "o_stream_send_istream()");
24651c3ac7f3 major syncing code cleanups. the code finally looks almost readable. logic
Timo Sirainen <tss@iki.fi>
parents: 2119
diff changeset
46 }
24651c3ac7f3 major syncing code cleanups. the code finally looks almost readable. logic
Timo Sirainen <tss@iki.fi>
parents: 2119
diff changeset
47
2107
8266299b92f5 expunging is somewhat working
Timo Sirainen <tss@iki.fi>
parents: 2041
diff changeset
48 o_stream_unref(output);
8266299b92f5 expunging is somewhat working
Timo Sirainen <tss@iki.fi>
parents: 2041
diff changeset
49 return (int)ret;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
50 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
51
2164
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
52 static int mbox_fill_space(struct mbox_sync_context *sync_ctx,
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
53 uoff_t offset, uoff_t size)
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
54 {
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
55 unsigned char space[1024];
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
56
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
57 memset(space, ' ', sizeof(space));
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
58 while (size > sizeof(space)) {
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
59 if (pwrite_full(sync_ctx->fd, space,
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
60 sizeof(space), offset) < 0) {
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
61 mbox_set_syscall_error(sync_ctx->ibox, "pwrite_full()");
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
62 return -1;
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
63 }
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
64 size -= sizeof(space);
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
65 }
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
66
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
67 if (pwrite_full(sync_ctx->fd, space, size, offset) < 0) {
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
68 mbox_set_syscall_error(sync_ctx->ibox, "pwrite_full()");
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
69 return -1;
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
70 }
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
71 return 0;
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
72 }
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
73
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
74 static void mbox_sync_headers_add_space(struct mbox_sync_mail_context *ctx,
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
75 size_t size)
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
76 {
2151
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
77 size_t data_size, pos, start_pos;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
78 const unsigned char *data;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
79 void *p;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
80
2146
3a33250e6a2d Complain if file isn't in mbox format. Complain if From-line wasn't found
Timo Sirainen <tss@iki.fi>
parents: 2126
diff changeset
81 i_assert(size < SSIZE_T_MAX);
3a33250e6a2d Complain if file isn't in mbox format. Complain if From-line wasn't found
Timo Sirainen <tss@iki.fi>
parents: 2126
diff changeset
82
2164
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
83 /* Append at the end of X-Keywords header,
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
84 or X-UID if it doesn't exist */
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
85 start_pos = ctx->hdr_pos[MBOX_HDR_X_KEYWORDS] != (size_t)-1 ?
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
86 ctx->hdr_pos[MBOX_HDR_X_KEYWORDS] :
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
87 ctx->hdr_pos[MBOX_HDR_X_UID];
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
88
2151
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
89 data = str_data(ctx->header);
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
90 data_size = str_len(ctx->header);
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
91
2151
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
92 for (pos = start_pos; pos < data_size; pos++) {
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
93 if (data[pos] == '\n') {
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
94 /* possibly continues in next line */
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
95 if (pos+1 == data_size || !IS_LWSP(data[pos+1]))
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
96 break;
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
97 start_pos = pos+1;
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
98 } else if (!IS_LWSP(data[pos])) {
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
99 start_pos = pos+1;
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
100 }
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
101 }
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
102
2164
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
103 /* pos points to end of headers now, and start_pos to beginning
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
104 of whitespace. */
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
105 buffer_copy(ctx->header, pos + size,
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
106 ctx->header, pos, (size_t)-1);
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
107 p = buffer_get_space_unsafe(ctx->header, pos, size);
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
108 memset(p, ' ', size);
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
109
2151
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
110 ctx->mail.offset = ctx->hdr_offset + start_pos;
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
111 ctx->mail.space = (pos - start_pos) + size;
1981
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
112
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
113 if (ctx->header_first_change > pos)
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
114 ctx->header_first_change = pos;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
115 ctx->header_last_change = (size_t)-1;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
116 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
117
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
118 static void mbox_sync_header_remove_space(struct mbox_sync_mail_context *ctx,
2151
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
119 size_t start_pos, size_t *size)
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
120 {
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
121 const unsigned char *data;
2151
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
122 size_t data_size, pos, last_line_pos;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
123
2151
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
124 /* find the end of the LWSP */
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
125 data = str_data(ctx->header);
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
126 data_size = str_len(ctx->header);
2151
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
127
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
128 for (pos = last_line_pos = start_pos; pos < data_size; pos++) {
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
129 if (data[pos] == '\n') {
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
130 /* possibly continues in next line */
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
131 if (pos+1 == data_size || !IS_LWSP(data[pos+1])) {
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
132 data_size = pos;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
133 break;
2151
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
134 }
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
135 last_line_pos = pos+1;
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
136 } else if (!IS_LWSP(data[pos])) {
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
137 start_pos = last_line_pos = pos+1;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
138 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
139 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
140
2151
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
141 if (start_pos == data_size)
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
142 return;
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
143
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
144 /* and remove what we can */
2151
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
145 if (ctx->header_first_change > start_pos)
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
146 ctx->header_first_change = start_pos;
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
147 ctx->header_last_change = (size_t)-1;
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
148
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
149 if (data_size - start_pos <= *size) {
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
150 /* remove it all */
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
151 str_delete(ctx->header, start_pos, data_size - start_pos);
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
152 *size -= data_size - start_pos;
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
153 return;
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
154 }
1981
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
155
2151
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
156 /* we have more space than needed. since we're removing from
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
157 the beginning of header instead of end, we don't have to
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
158 worry about multiline-headers. */
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
159 str_delete(ctx->header, start_pos, *size);
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
160 last_line_pos = last_line_pos <= *size ?
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
161 start_pos : last_line_pos - *size;
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
162
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
163 data_size -= *size;
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
164 *size = 0;
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
165
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
166 if (ctx->mail.space < data_size - last_line_pos) {
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
167 ctx->mail.space = data_size - last_line_pos;
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
168 ctx->mail.offset = ctx->hdr_offset + last_line_pos;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
169 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
170 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
171
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
172 static void mbox_sync_headers_remove_space(struct mbox_sync_mail_context *ctx,
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
173 size_t size)
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
174 {
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
175 static enum header_position space_positions[] = {
2151
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
176 MBOX_HDR_X_UID,
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
177 MBOX_HDR_X_KEYWORDS,
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
178 MBOX_HDR_X_IMAPBASE
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
179 };
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
180 enum header_position pos;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
181 int i;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
182
1981
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
183 ctx->mail.space = 0;
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
184 ctx->mail.offset = ctx->hdr_offset;
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
185
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
186 for (i = 0; i < 3 && size > 0; i++) {
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
187 pos = space_positions[i];
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
188 if (ctx->hdr_pos[pos] != (size_t)-1) {
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
189 mbox_sync_header_remove_space(ctx, ctx->hdr_pos[pos],
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
190 &size);
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
191 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
192 }
2153
53288223de6c Create X-Keywords header if possible.
Timo Sirainen <tss@iki.fi>
parents: 2151
diff changeset
193
53288223de6c Create X-Keywords header if possible.
Timo Sirainen <tss@iki.fi>
parents: 2151
diff changeset
194 /* FIXME: see if we could remove X-Keywords header completely */
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
195 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
196
2107
8266299b92f5 expunging is somewhat working
Timo Sirainen <tss@iki.fi>
parents: 2041
diff changeset
197 int mbox_sync_try_rewrite(struct mbox_sync_mail_context *ctx, off_t move_diff)
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
198 {
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
199 size_t old_hdr_size, new_hdr_size;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
200
1983
933cab442392 mbox growing and locking works now
Timo Sirainen <tss@iki.fi>
parents: 1981
diff changeset
201 i_assert(ctx->sync_ctx->ibox->mbox_lock_type == F_WRLCK);
933cab442392 mbox growing and locking works now
Timo Sirainen <tss@iki.fi>
parents: 1981
diff changeset
202
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
203 old_hdr_size = ctx->body_offset - ctx->hdr_offset;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
204 new_hdr_size = str_len(ctx->header);
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
205
2164
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
206 if (new_hdr_size <= old_hdr_size) {
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
207 /* add space. note that we must call add_space() even if we're
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
208 not adding anything so mail.offset gets fixed. */
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
209 mbox_sync_headers_add_space(ctx, old_hdr_size - new_hdr_size);
1983
933cab442392 mbox growing and locking works now
Timo Sirainen <tss@iki.fi>
parents: 1981
diff changeset
210 } else if (new_hdr_size > old_hdr_size) {
2151
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
211 /* try removing the space where we can */
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
212 mbox_sync_headers_remove_space(ctx,
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
213 new_hdr_size - old_hdr_size);
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
214 new_hdr_size = str_len(ctx->header);
2107
8266299b92f5 expunging is somewhat working
Timo Sirainen <tss@iki.fi>
parents: 2041
diff changeset
215
2151
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
216 if (new_hdr_size <= old_hdr_size) {
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
217 /* good, we removed enough. */
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
218 i_assert(new_hdr_size == old_hdr_size);
2164
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
219 ctx->mail.space =
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
220 -(ssize_t)(new_hdr_size - old_hdr_size);
2151
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
221 } else if (move_diff < 0 &&
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
222 new_hdr_size - old_hdr_size <= -move_diff) {
2107
8266299b92f5 expunging is somewhat working
Timo Sirainen <tss@iki.fi>
parents: 2041
diff changeset
223 /* moving backwards - we can use the extra space from
8266299b92f5 expunging is somewhat working
Timo Sirainen <tss@iki.fi>
parents: 2041
diff changeset
224 it, just update expunged_space accordingly */
2151
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
225 i_assert(ctx->mail.space == 0);
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
226 i_assert(ctx->sync_ctx->expunged_space >=
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
227 new_hdr_size - old_hdr_size);
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
228 ctx->sync_ctx->expunged_space -=
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
229 new_hdr_size - old_hdr_size;
2107
8266299b92f5 expunging is somewhat working
Timo Sirainen <tss@iki.fi>
parents: 2041
diff changeset
230 } else {
2151
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
231 /* couldn't get enough space */
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
232 i_assert(ctx->mail.space == 0);
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
233 ctx->mail.space =
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
234 -(ssize_t)(new_hdr_size - old_hdr_size);
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
235 return 0;
2107
8266299b92f5 expunging is somewhat working
Timo Sirainen <tss@iki.fi>
parents: 2041
diff changeset
236 }
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
237 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
238
2151
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
239 i_assert(ctx->mail.space >= 0);
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
240
2126
57078e1f2bfc more syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 2124
diff changeset
241 if (ctx->header_first_change == (size_t)-1 && move_diff == 0) {
57078e1f2bfc more syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 2124
diff changeset
242 /* no changes actually. we get here if index sync record told
57078e1f2bfc more syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 2124
diff changeset
243 us to do something that was already there */
57078e1f2bfc more syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 2124
diff changeset
244 return 1;
57078e1f2bfc more syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 2124
diff changeset
245 }
2107
8266299b92f5 expunging is somewhat working
Timo Sirainen <tss@iki.fi>
parents: 2041
diff changeset
246
2151
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
247 if (move_diff != 0 || ctx->no_partial_rewrite) {
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
248 /* forget about partial write optimizations */
2107
8266299b92f5 expunging is somewhat working
Timo Sirainen <tss@iki.fi>
parents: 2041
diff changeset
249 ctx->header_first_change = 0;
8266299b92f5 expunging is somewhat working
Timo Sirainen <tss@iki.fi>
parents: 2041
diff changeset
250 ctx->header_last_change = 0;
8266299b92f5 expunging is somewhat working
Timo Sirainen <tss@iki.fi>
parents: 2041
diff changeset
251 }
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
252
1983
933cab442392 mbox growing and locking works now
Timo Sirainen <tss@iki.fi>
parents: 1981
diff changeset
253 if (ctx->header_last_change != (size_t)-1 &&
933cab442392 mbox growing and locking works now
Timo Sirainen <tss@iki.fi>
parents: 1981
diff changeset
254 ctx->header_last_change != 0)
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
255 str_truncate(ctx->header, ctx->header_last_change);
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
256
2151
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
257 if (pwrite_full(ctx->sync_ctx->fd,
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
258 str_data(ctx->header) + ctx->header_first_change,
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
259 str_len(ctx->header) - ctx->header_first_change,
2107
8266299b92f5 expunging is somewhat working
Timo Sirainen <tss@iki.fi>
parents: 2041
diff changeset
260 ctx->hdr_offset + move_diff +
8266299b92f5 expunging is somewhat working
Timo Sirainen <tss@iki.fi>
parents: 2041
diff changeset
261 ctx->header_first_change) < 0) {
2124
24651c3ac7f3 major syncing code cleanups. the code finally looks almost readable. logic
Timo Sirainen <tss@iki.fi>
parents: 2119
diff changeset
262 mbox_set_syscall_error(ctx->sync_ctx->ibox, "pwrite_full()");
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
263 return -1;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
264 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
265 istream_raw_mbox_flush(ctx->sync_ctx->input);
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
266 return 1;
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
267 }
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
268
1981
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
269 static int mbox_sync_read_and_move(struct mbox_sync_context *sync_ctx,
2109
8c0168d19148 expunges should pretty much work now.
Timo Sirainen <tss@iki.fi>
parents: 2107
diff changeset
270 struct mbox_sync_mail *mails,
8c0168d19148 expunges should pretty much work now.
Timo Sirainen <tss@iki.fi>
parents: 2107
diff changeset
271 uint32_t seq, uint32_t idx,
2164
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
272 uoff_t space_diff, uoff_t end_offset)
1981
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
273 {
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
274 struct mbox_sync_mail_context mail_ctx;
2109
8c0168d19148 expunges should pretty much work now.
Timo Sirainen <tss@iki.fi>
parents: 2107
diff changeset
275 uint32_t old_prev_msg_uid;
2164
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
276 uoff_t hdr_offset, offset, dest_offset;
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
277 size_t old_hdr_size, need_space;
1981
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
278
2155
Timo Sirainen <tss@iki.fi>
parents: 2153
diff changeset
279 if (mbox_sync_seek(sync_ctx, mails[idx].from_offset) < 0)
Timo Sirainen <tss@iki.fi>
parents: 2153
diff changeset
280 return -1;
1981
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
281
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
282 memset(&mail_ctx, 0, sizeof(mail_ctx));
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
283 mail_ctx.sync_ctx = sync_ctx;
2109
8c0168d19148 expunges should pretty much work now.
Timo Sirainen <tss@iki.fi>
parents: 2107
diff changeset
284 mail_ctx.seq = seq;
1981
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
285 mail_ctx.header = sync_ctx->header;
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
286
2164
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
287 hdr_offset = mails[idx].offset;
1981
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
288 mail_ctx.mail.offset = mails[idx].offset;
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
289 mail_ctx.mail.body_size = mails[idx].body_size;
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
290
2109
8c0168d19148 expunges should pretty much work now.
Timo Sirainen <tss@iki.fi>
parents: 2107
diff changeset
291 /* mbox_sync_parse_next_mail() checks that UIDs are growing,
8c0168d19148 expunges should pretty much work now.
Timo Sirainen <tss@iki.fi>
parents: 2107
diff changeset
292 so we have to fool it. */
2146
3a33250e6a2d Complain if file isn't in mbox format. Complain if From-line wasn't found
Timo Sirainen <tss@iki.fi>
parents: 2126
diff changeset
293 old_prev_msg_uid = sync_ctx->prev_msg_uid;
3a33250e6a2d Complain if file isn't in mbox format. Complain if From-line wasn't found
Timo Sirainen <tss@iki.fi>
parents: 2126
diff changeset
294 sync_ctx->prev_msg_uid = mails[idx].uid-1;
2151
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
295 sync_ctx->dest_first_mail = seq == 1;
2109
8c0168d19148 expunges should pretty much work now.
Timo Sirainen <tss@iki.fi>
parents: 2107
diff changeset
296
2146
3a33250e6a2d Complain if file isn't in mbox format. Complain if From-line wasn't found
Timo Sirainen <tss@iki.fi>
parents: 2126
diff changeset
297 mbox_sync_parse_next_mail(sync_ctx->input, &mail_ctx, TRUE);
2109
8c0168d19148 expunges should pretty much work now.
Timo Sirainen <tss@iki.fi>
parents: 2107
diff changeset
298 if (mails[idx].space != 0)
8c0168d19148 expunges should pretty much work now.
Timo Sirainen <tss@iki.fi>
parents: 2107
diff changeset
299 mbox_sync_update_header_from(&mail_ctx, &mails[idx]);
8c0168d19148 expunges should pretty much work now.
Timo Sirainen <tss@iki.fi>
parents: 2107
diff changeset
300 else {
8c0168d19148 expunges should pretty much work now.
Timo Sirainen <tss@iki.fi>
parents: 2107
diff changeset
301 /* updating might just try to add headers and mess up our
8c0168d19148 expunges should pretty much work now.
Timo Sirainen <tss@iki.fi>
parents: 2107
diff changeset
302 calculations completely. so only add the EOH here. */
8c0168d19148 expunges should pretty much work now.
Timo Sirainen <tss@iki.fi>
parents: 2107
diff changeset
303 if (mail_ctx.have_eoh)
8c0168d19148 expunges should pretty much work now.
Timo Sirainen <tss@iki.fi>
parents: 2107
diff changeset
304 str_append_c(mail_ctx.header, '\n');
8c0168d19148 expunges should pretty much work now.
Timo Sirainen <tss@iki.fi>
parents: 2107
diff changeset
305 }
1981
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
306
2151
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
307 sync_ctx->prev_msg_uid = old_prev_msg_uid;
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
308 sync_ctx->dest_first_mail = FALSE;
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
309
2164
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
310 old_hdr_size = mail_ctx.body_offset - mail_ctx.hdr_offset;
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
311 need_space = str_len(mail_ctx.header) - mail_ctx.mail.space -
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
312 old_hdr_size;
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
313 i_assert(need_space == -mails[idx].space);
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
314 i_assert(space_diff >= need_space);
1981
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
315
2164
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
316 if (space_diff - need_space < (uoff_t)mail_ctx.mail.space) {
1981
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
317 mbox_sync_headers_remove_space(&mail_ctx, mail_ctx.mail.space -
2164
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
318 (space_diff - need_space));
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
319 } else {
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
320 mbox_sync_headers_add_space(&mail_ctx, space_diff - need_space -
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
321 mail_ctx.mail.space);
1981
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
322 }
2164
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
323 mails[idx].offset = mail_ctx.mail.offset;
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
324 mails[idx].space = mail_ctx.mail.space;
1981
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
325
2164
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
326 /* move the body of this message and headers of next message forward,
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
327 then write the headers */
2146
3a33250e6a2d Complain if file isn't in mbox format. Complain if From-line wasn't found
Timo Sirainen <tss@iki.fi>
parents: 2126
diff changeset
328 offset = sync_ctx->input->v_offset;
2164
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
329 dest_offset = offset + space_diff;
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
330 if (mbox_move(sync_ctx, dest_offset, offset,
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
331 end_offset - dest_offset) < 0)
1981
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
332 return -1;
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
333
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
334 if (pwrite_full(sync_ctx->fd, str_data(mail_ctx.header),
2164
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
335 str_len(mail_ctx.header), hdr_offset) < 0) {
2124
24651c3ac7f3 major syncing code cleanups. the code finally looks almost readable. logic
Timo Sirainen <tss@iki.fi>
parents: 2119
diff changeset
336 mbox_set_syscall_error(sync_ctx->ibox, "pwrite_full()");
1981
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
337 return -1;
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
338 }
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
339
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
340 return 0;
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
341 }
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
342
2164
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
343 /* extra_space specifies how many bytes from last_seq's space will be left
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
344 over after all the rewrites. */
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
345 int mbox_sync_rewrite(struct mbox_sync_context *sync_ctx, uoff_t extra_space,
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
346 uint32_t first_seq, uint32_t last_seq)
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
347 {
1978
6303ef092c5b mbox code compiles again, but syncing is only partially implemented so
Timo Sirainen <tss@iki.fi>
parents: 1915
diff changeset
348 struct mbox_sync_mail *mails;
2164
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
349 uoff_t offset, end_offset, dest_offset, space_diff;
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
350 uint32_t idx, extra_per_mail;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
351 size_t size;
1981
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
352 int ret = 0;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
353
2107
8266299b92f5 expunging is somewhat working
Timo Sirainen <tss@iki.fi>
parents: 2041
diff changeset
354 i_assert(first_seq != last_seq);
1983
933cab442392 mbox growing and locking works now
Timo Sirainen <tss@iki.fi>
parents: 1981
diff changeset
355 i_assert(sync_ctx->ibox->mbox_lock_type == F_WRLCK);
933cab442392 mbox growing and locking works now
Timo Sirainen <tss@iki.fi>
parents: 1981
diff changeset
356
2124
24651c3ac7f3 major syncing code cleanups. the code finally looks almost readable. logic
Timo Sirainen <tss@iki.fi>
parents: 2119
diff changeset
357 mails = buffer_get_modifyable_data(sync_ctx->mails, &size);
2118
11febaee5e9b Save mbox offsets to index file using extra_records. Some other fixes.
Timo Sirainen <tss@iki.fi>
parents: 2111
diff changeset
358 i_assert(size / sizeof(*mails) == last_seq - first_seq + 1);
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
359
2109
8c0168d19148 expunges should pretty much work now.
Timo Sirainen <tss@iki.fi>
parents: 2107
diff changeset
360 /* if there's expunges in mails[], we would get more correct balancing
8c0168d19148 expunges should pretty much work now.
Timo Sirainen <tss@iki.fi>
parents: 2107
diff changeset
361 by counting only them here. however, that might make us overwrite
8c0168d19148 expunges should pretty much work now.
Timo Sirainen <tss@iki.fi>
parents: 2107
diff changeset
362 data which hasn't yet been copied backwards. to avoid too much
8c0168d19148 expunges should pretty much work now.
Timo Sirainen <tss@iki.fi>
parents: 2107
diff changeset
363 complexity, we just leave all the rest of the extra space to first
8c0168d19148 expunges should pretty much work now.
Timo Sirainen <tss@iki.fi>
parents: 2107
diff changeset
364 mail */
8c0168d19148 expunges should pretty much work now.
Timo Sirainen <tss@iki.fi>
parents: 2107
diff changeset
365 idx = last_seq - first_seq;
2164
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
366 extra_per_mail = extra_space / (idx + 1);
1981
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
367
2151
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
368 /* after expunge the next mail must have been missing space, or we
16287320d080 Several fixes in space/offset logic. Should be much more robust now.
Timo Sirainen <tss@iki.fi>
parents: 2146
diff changeset
369 would have moved it backwards already */
2109
8c0168d19148 expunges should pretty much work now.
Timo Sirainen <tss@iki.fi>
parents: 2107
diff changeset
370 i_assert(mails[0].space < 0 || mails[0].uid == 0);
2164
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
371
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
372 /* start moving backwards. */
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
373 do {
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
374 /* this message's body is always moved space_diff bytes
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
375 forward along with next message's headers, so current
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
376 message gets temporarily space_diff amount of extra
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
377 whitespace.
2109
8c0168d19148 expunges should pretty much work now.
Timo Sirainen <tss@iki.fi>
parents: 2107
diff changeset
378
2164
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
379 the moving stops at next message's beginning of extra
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
380 space. each message gets left extra_per_mail bytes of
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
381 space. what gets left over is given to first message */
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
382 i_assert(mails[idx].space > 0);
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
383 space_diff = mails[idx].space;
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
384 end_offset = mails[idx].offset + mails[idx].space;
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
385
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
386 if (mails[idx].uid != 0) {
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
387 space_diff -= extra_per_mail;
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
388 end_offset -= extra_per_mail;
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
389 mails[idx].space = extra_per_mail;
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
390 }
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
391
2107
8266299b92f5 expunging is somewhat working
Timo Sirainen <tss@iki.fi>
parents: 2041
diff changeset
392 idx--;
2109
8c0168d19148 expunges should pretty much work now.
Timo Sirainen <tss@iki.fi>
parents: 2107
diff changeset
393 if (mails[idx].space <= 0 && mails[idx].uid != 0) {
1981
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
394 /* offset points to beginning of headers. read the
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
395 header again, update it and give enough space to
2164
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
396 fill space_diff */
2109
8c0168d19148 expunges should pretty much work now.
Timo Sirainen <tss@iki.fi>
parents: 2107
diff changeset
397 if (mbox_sync_read_and_move(sync_ctx, mails,
8c0168d19148 expunges should pretty much work now.
Timo Sirainen <tss@iki.fi>
parents: 2107
diff changeset
398 first_seq + idx, idx,
2164
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
399 space_diff,
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
400 end_offset) < 0) {
1981
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
401 ret = -1;
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
402 break;
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
403 }
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
404 } else {
2164
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
405 /* X-Keywords: xx [offset]\n
1981
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
406 ...
2164
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
407 X-Keywords: xx [end_offset] \n
1981
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
408
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
409 move data forward so mails before us gets the extra
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
410 space (ie. we temporarily get more space to us) */
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
411 offset = mails[idx].offset + mails[idx].space;
2164
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
412 dest_offset = offset + space_diff;
2041
2240a38138db mbox syncing fixes
Timo Sirainen <tss@iki.fi>
parents: 1983
diff changeset
413 if (mbox_move(sync_ctx, dest_offset, offset,
2109
8c0168d19148 expunges should pretty much work now.
Timo Sirainen <tss@iki.fi>
parents: 2107
diff changeset
414 end_offset - dest_offset) < 0) {
1981
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
415 ret = -1;
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
416 break;
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
417 }
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
418
2164
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
419 if (mbox_fill_space(sync_ctx, offset,
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
420 dest_offset - offset) < 0) {
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
421 ret = -1;
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
422 break;
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
423 }
1981
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
424
2164
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
425 mails[idx].space += space_diff;
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
426 }
2111
eb26aa46686d Fixes for expunging first message
Timo Sirainen <tss@iki.fi>
parents: 2110
diff changeset
427
2164
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
428 mails[idx+1].from_offset += space_diff;
Timo Sirainen <tss@iki.fi>
parents: 2155
diff changeset
429 } while (idx > 0);
2109
8c0168d19148 expunges should pretty much work now.
Timo Sirainen <tss@iki.fi>
parents: 2107
diff changeset
430
1981
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
431 istream_raw_mbox_flush(sync_ctx->input);
eb282832fc75 mbox rewriting is almost working - the hard part is done.
Timo Sirainen <tss@iki.fi>
parents: 1978
diff changeset
432 return ret;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
433 }