Diffstat (limited to 'kmicromail/libetpan/generic/maildirdriver_message.c') (more/less context) (ignore whitespace changes)
-rw-r--r-- | kmicromail/libetpan/generic/maildirdriver_message.c | 58 |
1 files changed, 57 insertions, 1 deletions
diff --git a/kmicromail/libetpan/generic/maildirdriver_message.c b/kmicromail/libetpan/generic/maildirdriver_message.c index 7cf9dd1..613fc39 100644 --- a/kmicromail/libetpan/generic/maildirdriver_message.c +++ b/kmicromail/libetpan/generic/maildirdriver_message.c | |||
@@ -1,129 +1,133 @@ | |||
1 | /* | 1 | /* |
2 | * libEtPan! -- a mail stuff library | 2 | * libEtPan! -- a mail stuff library |
3 | * | 3 | * |
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | 4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa |
5 | * All rights reserved. | 5 | * All rights reserved. |
6 | * | 6 | * |
7 | * Redistribution and use in source and binary forms, with or without | 7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions | 8 | * modification, are permitted provided that the following conditions |
9 | * are met: | 9 | * are met: |
10 | * 1. Redistributions of source code must retain the above copyright | 10 | * 1. Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. | 11 | * notice, this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the | 13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. | 14 | * documentation and/or other materials provided with the distribution. |
15 | * 3. Neither the name of the libEtPan! project nor the names of its | 15 | * 3. Neither the name of the libEtPan! project nor the names of its |
16 | * contributors may be used to endorse or promote products derived | 16 | * contributors may be used to endorse or promote products derived |
17 | * from this software without specific prior written permission. | 17 | * from this software without specific prior written permission. |
18 | * | 18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | 19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
29 | * SUCH DAMAGE. | 29 | * SUCH DAMAGE. |
30 | */ | 30 | */ |
31 | 31 | ||
32 | /* | 32 | /* |
33 | * $Id$ | 33 | * $Id$ |
34 | */ | 34 | */ |
35 | 35 | ||
36 | #include "maildirdriver_message.h" | 36 | #include "maildirdriver_message.h" |
37 | #include "maildirdriver_tools.h" | ||
37 | 38 | ||
38 | #include "mailmessage_tools.h" | 39 | #include "mailmessage_tools.h" |
39 | #include "maildirdriver.h" | 40 | #include "maildirdriver.h" |
40 | #include "maildir.h" | 41 | #include "maildir.h" |
41 | #include "generic_cache.h" | 42 | #include "generic_cache.h" |
42 | 43 | ||
43 | #include <unistd.h> | 44 | #include <unistd.h> |
44 | #include <sys/mman.h> | 45 | #include <sys/mman.h> |
45 | #include <sys/types.h> | 46 | #include <sys/types.h> |
46 | #include <sys/stat.h> | 47 | #include <sys/stat.h> |
47 | #include <fcntl.h> | 48 | #include <fcntl.h> |
48 | #include <string.h> | 49 | #include <string.h> |
49 | #include <stdlib.h> | 50 | #include <stdlib.h> |
50 | 51 | ||
52 | static int get_flags(mailmessage * msg_info, | ||
53 | struct mail_flags ** result); | ||
54 | |||
51 | static int prefetch(mailmessage * msg_info); | 55 | static int prefetch(mailmessage * msg_info); |
52 | 56 | ||
53 | static void prefetch_free(struct generic_message_t * msg); | 57 | static void prefetch_free(struct generic_message_t * msg); |
54 | 58 | ||
55 | static int initialize(mailmessage * msg_info); | 59 | static int initialize(mailmessage * msg_info); |
56 | 60 | ||
57 | static void check(mailmessage * msg_info); | 61 | static void check(mailmessage * msg_info); |
58 | 62 | ||
59 | static mailmessage_driver local_maildir_message_driver = { | 63 | static mailmessage_driver local_maildir_message_driver = { |
60 | .msg_name = "maildir", | 64 | .msg_name = "maildir", |
61 | 65 | ||
62 | .msg_initialize = initialize, | 66 | .msg_initialize = initialize, |
63 | .msg_uninitialize = mailmessage_generic_uninitialize, | 67 | .msg_uninitialize = mailmessage_generic_uninitialize, |
64 | 68 | ||
65 | .msg_flush = mailmessage_generic_flush, | 69 | .msg_flush = mailmessage_generic_flush, |
66 | .msg_check = check, | 70 | .msg_check = check, |
67 | 71 | ||
68 | .msg_fetch_result_free = mailmessage_generic_fetch_result_free, | 72 | .msg_fetch_result_free = mailmessage_generic_fetch_result_free, |
69 | 73 | ||
70 | .msg_fetch = mailmessage_generic_fetch, | 74 | .msg_fetch = mailmessage_generic_fetch, |
71 | .msg_fetch_header = mailmessage_generic_fetch_header, | 75 | .msg_fetch_header = mailmessage_generic_fetch_header, |
72 | .msg_fetch_body = mailmessage_generic_fetch_header, | 76 | .msg_fetch_body = mailmessage_generic_fetch_header, |
73 | .msg_fetch_size = NULL, | 77 | .msg_fetch_size = NULL, |
74 | .msg_get_bodystructure = mailmessage_generic_get_bodystructure, | 78 | .msg_get_bodystructure = mailmessage_generic_get_bodystructure, |
75 | .msg_fetch_section = mailmessage_generic_fetch_section, | 79 | .msg_fetch_section = mailmessage_generic_fetch_section, |
76 | .msg_fetch_section_header = mailmessage_generic_fetch_section_header, | 80 | .msg_fetch_section_header = mailmessage_generic_fetch_section_header, |
77 | .msg_fetch_section_mime = mailmessage_generic_fetch_section_mime, | 81 | .msg_fetch_section_mime = mailmessage_generic_fetch_section_mime, |
78 | .msg_fetch_section_body = mailmessage_generic_fetch_section_body, | 82 | .msg_fetch_section_body = mailmessage_generic_fetch_section_body, |
79 | .msg_fetch_envelope = mailmessage_generic_fetch_envelope, | 83 | .msg_fetch_envelope = mailmessage_generic_fetch_envelope, |
80 | 84 | ||
81 | .msg_get_flags = NULL, | 85 | .msg_get_flags = get_flags, |
82 | }; | 86 | }; |
83 | 87 | ||
84 | mailmessage_driver * maildir_message_driver = &local_maildir_message_driver; | 88 | mailmessage_driver * maildir_message_driver = &local_maildir_message_driver; |
85 | 89 | ||
86 | struct maildir_msg_data { | 90 | struct maildir_msg_data { |
87 | int fd; | 91 | int fd; |
88 | }; | 92 | }; |
89 | 93 | ||
90 | static inline struct maildir_session_state_data * | 94 | static inline struct maildir_session_state_data * |
91 | get_session_data(mailmessage * msg) | 95 | get_session_data(mailmessage * msg) |
92 | { | 96 | { |
93 | return msg->msg_session->sess_data; | 97 | return msg->msg_session->sess_data; |
94 | } | 98 | } |
95 | 99 | ||
96 | static struct maildir * get_maildir_session(mailmessage * msg) | 100 | static struct maildir * get_maildir_session(mailmessage * msg) |
97 | { | 101 | { |
98 | return get_session_data(msg)->md_session; | 102 | return get_session_data(msg)->md_session; |
99 | } | 103 | } |
100 | 104 | ||
101 | static int prefetch(mailmessage * msg_info) | 105 | static int prefetch(mailmessage * msg_info) |
102 | { | 106 | { |
103 | struct generic_message_t * msg; | 107 | struct generic_message_t * msg; |
104 | int res; | 108 | int res; |
105 | struct maildir_msg_data * data; | 109 | struct maildir_msg_data * data; |
106 | char * filename; | 110 | char * filename; |
107 | int fd; | 111 | int fd; |
108 | char * mapping; | 112 | char * mapping; |
109 | struct maildir * md; | 113 | struct maildir * md; |
110 | 114 | ||
111 | md = get_maildir_session(msg_info); | 115 | md = get_maildir_session(msg_info); |
112 | 116 | ||
113 | if (msg_info->msg_uid == NULL) { | 117 | if (msg_info->msg_uid == NULL) { |
114 | res = MAIL_ERROR_INVAL; | 118 | res = MAIL_ERROR_INVAL; |
115 | goto err; | 119 | goto err; |
116 | } | 120 | } |
117 | 121 | ||
118 | filename = maildir_message_get(md, msg_info->msg_uid); | 122 | filename = maildir_message_get(md, msg_info->msg_uid); |
119 | if (filename == NULL) { | 123 | if (filename == NULL) { |
120 | res = MAIL_ERROR_MEMORY; | 124 | res = MAIL_ERROR_MEMORY; |
121 | goto err; | 125 | goto err; |
122 | } | 126 | } |
123 | 127 | ||
124 | fd = open(filename, O_RDONLY); | 128 | fd = open(filename, O_RDONLY); |
125 | free(filename); | 129 | free(filename); |
126 | if (fd == -1) { | 130 | if (fd == -1) { |
127 | res = MAIL_ERROR_FILE; | 131 | res = MAIL_ERROR_FILE; |
128 | goto err; | 132 | goto err; |
129 | } | 133 | } |
@@ -152,48 +156,100 @@ static int prefetch(mailmessage * msg_info) | |||
152 | 156 | ||
153 | unmap: | 157 | unmap: |
154 | munmap(mapping, msg_info->msg_size); | 158 | munmap(mapping, msg_info->msg_size); |
155 | close: | 159 | close: |
156 | close(fd); | 160 | close(fd); |
157 | err: | 161 | err: |
158 | return res; | 162 | return res; |
159 | } | 163 | } |
160 | 164 | ||
161 | static void prefetch_free(struct generic_message_t * msg) | 165 | static void prefetch_free(struct generic_message_t * msg) |
162 | { | 166 | { |
163 | if (msg->msg_message != NULL) { | 167 | if (msg->msg_message != NULL) { |
164 | struct maildir_msg_data * data; | 168 | struct maildir_msg_data * data; |
165 | 169 | ||
166 | munmap(msg->msg_message, msg->msg_length); | 170 | munmap(msg->msg_message, msg->msg_length); |
167 | msg->msg_message = NULL; | 171 | msg->msg_message = NULL; |
168 | data = msg->msg_data; | 172 | data = msg->msg_data; |
169 | close(data->fd); | 173 | close(data->fd); |
170 | free(data); | 174 | free(data); |
171 | } | 175 | } |
172 | } | 176 | } |
173 | 177 | ||
174 | static int initialize(mailmessage * msg_info) | 178 | static int initialize(mailmessage * msg_info) |
175 | { | 179 | { |
176 | struct generic_message_t * msg; | 180 | struct generic_message_t * msg; |
177 | int r; | 181 | int r; |
178 | 182 | ||
179 | r = mailmessage_generic_initialize(msg_info); | 183 | r = mailmessage_generic_initialize(msg_info); |
180 | if (r != MAIL_NO_ERROR) | 184 | if (r != MAIL_NO_ERROR) |
181 | return r; | 185 | return r; |
182 | 186 | ||
183 | msg = msg_info->msg_data; | 187 | msg = msg_info->msg_data; |
184 | msg->msg_prefetch = prefetch; | 188 | msg->msg_prefetch = prefetch; |
185 | msg->msg_prefetch_free = prefetch_free; | 189 | msg->msg_prefetch_free = prefetch_free; |
186 | 190 | ||
187 | return MAIL_NO_ERROR; | 191 | return MAIL_NO_ERROR; |
188 | } | 192 | } |
189 | 193 | ||
190 | static void check(mailmessage * msg_info) | 194 | static void check(mailmessage * msg_info) |
191 | { | 195 | { |
192 | int r; | 196 | int r; |
193 | 197 | ||
194 | if (msg_info->msg_flags != NULL) { | 198 | if (msg_info->msg_flags != NULL) { |
195 | r = mail_flags_store_set(get_session_data(msg_info)->md_flags_store, | 199 | r = mail_flags_store_set(get_session_data(msg_info)->md_flags_store, |
196 | msg_info); | 200 | msg_info); |
197 | /* ignore errors */ | 201 | /* ignore errors */ |
198 | } | 202 | } |
199 | } | 203 | } |
204 | |||
205 | static int get_flags(mailmessage * msg_info, | ||
206 | struct mail_flags ** result) | ||
207 | { | ||
208 | chashdatum key; | ||
209 | chashdatum value; | ||
210 | struct maildir * md; | ||
211 | struct mail_flags * flags; | ||
212 | struct maildir_session_state_data * data; | ||
213 | struct maildir_msg * md_msg; | ||
214 | int r; | ||
215 | uint32_t driver_flags; | ||
216 | clist * ext; | ||
217 | |||
218 | if (msg_info->msg_flags != NULL) { | ||
219 | * result = msg_info->msg_flags; | ||
220 | return MAIL_NO_ERROR; | ||
221 | } | ||
222 | |||
223 | data = get_session_data(msg_info); | ||
224 | flags = mail_flags_store_get(data->md_flags_store, | ||
225 | msg_info->msg_index); | ||
226 | if (flags != NULL) { | ||
227 | msg_info->msg_flags = flags; | ||
228 | * result = msg_info->msg_flags; | ||
229 | return MAIL_NO_ERROR; | ||
230 | } | ||
231 | |||
232 | md = get_maildir_session(msg_info); | ||
233 | if (md == NULL) | ||
234 | return MAIL_ERROR_BAD_STATE; | ||
235 | |||
236 | key.data = msg_info->msg_uid; | ||
237 | key.len = strlen(msg_info->msg_uid); | ||
238 | r = chash_get(md->mdir_msg_hash, &key, &value); | ||
239 | if (r < 0) | ||
240 | return MAIL_ERROR_MSG_NOT_FOUND; | ||
241 | |||
242 | md_msg = value.data; | ||
243 | |||
244 | driver_flags = maildirdriver_maildir_flags_to_flags(md_msg->msg_flags); | ||
245 | |||
246 | ext = clist_new(); | ||
247 | if (ext == NULL) | ||
248 | return MAIL_ERROR_MEMORY; | ||
249 | |||
250 | msg_info->msg_flags = mail_flags_new(driver_flags, ext); | ||
251 | |||
252 | * result = msg_info->msg_flags; | ||
253 | |||
254 | return MAIL_NO_ERROR; | ||
255 | } | ||