Diffstat (limited to 'libetpan/src/driver/implementation/db/dbdriver_message.c') (more/less context) (ignore whitespace changes)
-rw-r--r-- | libetpan/src/driver/implementation/db/dbdriver_message.c | 308 |
1 files changed, 308 insertions, 0 deletions
diff --git a/libetpan/src/driver/implementation/db/dbdriver_message.c b/libetpan/src/driver/implementation/db/dbdriver_message.c new file mode 100644 index 0000000..70e9dca --- a/dev/null +++ b/libetpan/src/driver/implementation/db/dbdriver_message.c | |||
@@ -0,0 +1,308 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2005 - DINH Viet Hoa | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
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 | ||
17 | * from this software without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
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 | ||
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 | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * $Id$ | ||
34 | */ | ||
35 | |||
36 | #include "dbdriver_message.h" | ||
37 | #include "dbdriver.h" | ||
38 | #include "mail_cache_db.h" | ||
39 | |||
40 | #include "mailmessage_tools.h" | ||
41 | #include "generic_cache.h" | ||
42 | |||
43 | #include <unistd.h> | ||
44 | #include <sys/mman.h> | ||
45 | #include <sys/types.h> | ||
46 | #include <sys/stat.h> | ||
47 | #include <fcntl.h> | ||
48 | #include <string.h> | ||
49 | #include <stdlib.h> | ||
50 | |||
51 | static int fetch_envelope(mailmessage * msg_info, | ||
52 | struct mailimf_fields ** result); | ||
53 | |||
54 | static int get_flags(mailmessage * msg_info, | ||
55 | struct mail_flags ** result); | ||
56 | |||
57 | static int prefetch(mailmessage * msg_info); | ||
58 | |||
59 | static void prefetch_free(struct generic_message_t * msg); | ||
60 | |||
61 | static int initialize(mailmessage * msg_info); | ||
62 | |||
63 | static void check(mailmessage * msg_info); | ||
64 | |||
65 | static mailmessage_driver local_db_message_driver = { | ||
66 | .msg_name = "db", | ||
67 | |||
68 | .msg_initialize = initialize, | ||
69 | .msg_uninitialize = mailmessage_generic_uninitialize, | ||
70 | |||
71 | .msg_flush = mailmessage_generic_flush, | ||
72 | .msg_check = check, | ||
73 | |||
74 | .msg_fetch_result_free = mailmessage_generic_fetch_result_free, | ||
75 | |||
76 | .msg_fetch = mailmessage_generic_fetch, | ||
77 | .msg_fetch_header = mailmessage_generic_fetch_header, | ||
78 | .msg_fetch_body = mailmessage_generic_fetch_header, | ||
79 | .msg_fetch_size = NULL, | ||
80 | .msg_get_bodystructure = mailmessage_generic_get_bodystructure, | ||
81 | .msg_fetch_section = mailmessage_generic_fetch_section, | ||
82 | .msg_fetch_section_header = mailmessage_generic_fetch_section_header, | ||
83 | .msg_fetch_section_mime = mailmessage_generic_fetch_section_mime, | ||
84 | .msg_fetch_section_body = mailmessage_generic_fetch_section_body, | ||
85 | .msg_fetch_envelope = fetch_envelope, | ||
86 | |||
87 | .msg_get_flags = get_flags, | ||
88 | }; | ||
89 | |||
90 | mailmessage_driver * db_message_driver = &local_db_message_driver; | ||
91 | |||
92 | struct db_msg_data { | ||
93 | MMAPString * msg_content; | ||
94 | }; | ||
95 | |||
96 | static inline struct db_session_state_data * | ||
97 | get_session_data(mailmessage * msg) | ||
98 | { | ||
99 | return msg->msg_session->sess_data; | ||
100 | } | ||
101 | |||
102 | static int prefetch(mailmessage * msg_info) | ||
103 | { | ||
104 | struct generic_message_t * msg; | ||
105 | int res; | ||
106 | struct db_msg_data * data; | ||
107 | struct db_session_state_data * sess_data; | ||
108 | MMAPString * msg_content; | ||
109 | struct mail_cache_db * maildb; | ||
110 | int r; | ||
111 | char key[PATH_MAX]; | ||
112 | void * msg_data; | ||
113 | size_t msg_data_len; | ||
114 | |||
115 | sess_data = get_session_data(msg_info); | ||
116 | |||
117 | r = mail_cache_db_open_lock(sess_data->db_filename, &maildb); | ||
118 | if (r < 0) { | ||
119 | res = MAIL_ERROR_FILE; | ||
120 | goto err; | ||
121 | } | ||
122 | |||
123 | snprintf(key, sizeof(key), "%lu", (unsigned long) msg_info->msg_index); | ||
124 | r = mail_cache_db_get(maildb, key, strlen(key), &msg_data, &msg_data_len); | ||
125 | if (r < 0) { | ||
126 | res = MAIL_ERROR_MSG_NOT_FOUND; | ||
127 | goto close_db; | ||
128 | } | ||
129 | |||
130 | msg_content = mmap_string_new_len(msg_data, msg_data_len); | ||
131 | if (msg_content == NULL) { | ||
132 | res = MAIL_ERROR_MEMORY; | ||
133 | goto close_db; | ||
134 | } | ||
135 | |||
136 | data = malloc(sizeof(* data)); | ||
137 | if (data == NULL) { | ||
138 | res = MAIL_ERROR_MEMORY; | ||
139 | goto free_mmapstr; | ||
140 | } | ||
141 | |||
142 | data->msg_content = msg_content; | ||
143 | |||
144 | msg = msg_info->msg_data; | ||
145 | |||
146 | msg->msg_data = data; | ||
147 | msg->msg_message = msg_content->str; | ||
148 | msg->msg_length = msg_content->len; | ||
149 | |||
150 | mail_cache_db_close_unlock(sess_data->db_filename, maildb); | ||
151 | |||
152 | return MAIL_NO_ERROR; | ||
153 | |||
154 | free_mmapstr: | ||
155 | mmap_string_free(msg_content); | ||
156 | close_db: | ||
157 | mail_cache_db_close_unlock(sess_data->db_filename, maildb); | ||
158 | err: | ||
159 | return res; | ||
160 | } | ||
161 | |||
162 | static void prefetch_free(struct generic_message_t * msg) | ||
163 | { | ||
164 | if (msg->msg_message != NULL) { | ||
165 | struct db_msg_data * data; | ||
166 | |||
167 | data = msg->msg_data; | ||
168 | mmap_string_free(data->msg_content); | ||
169 | data->msg_content = NULL; | ||
170 | free(data); | ||
171 | msg->msg_message = NULL; | ||
172 | } | ||
173 | } | ||
174 | |||
175 | static int initialize(mailmessage * msg_info) | ||
176 | { | ||
177 | struct generic_message_t * msg; | ||
178 | int r; | ||
179 | char key[PATH_MAX]; | ||
180 | |||
181 | snprintf(key, sizeof(key), "%lu", (unsigned long) msg_info->msg_index); | ||
182 | msg_info->msg_uid = strdup(key); | ||
183 | if (msg_info->msg_uid == NULL) | ||
184 | return MAIL_ERROR_MEMORY; | ||
185 | |||
186 | r = mailmessage_generic_initialize(msg_info); | ||
187 | if (r != MAIL_NO_ERROR) | ||
188 | return r; | ||
189 | |||
190 | msg = msg_info->msg_data; | ||
191 | msg->msg_prefetch = prefetch; | ||
192 | msg->msg_prefetch_free = prefetch_free; | ||
193 | |||
194 | return MAIL_NO_ERROR; | ||
195 | } | ||
196 | |||
197 | static void check(mailmessage * msg_info) | ||
198 | { | ||
199 | int r; | ||
200 | |||
201 | if (msg_info->msg_flags != NULL) { | ||
202 | r = mail_flags_store_set(get_session_data(msg_info)->db_flags_store, | ||
203 | msg_info); | ||
204 | /* ignore errors */ | ||
205 | } | ||
206 | } | ||
207 | |||
208 | static int fetch_envelope(mailmessage * msg_info, | ||
209 | struct mailimf_fields ** result) | ||
210 | { | ||
211 | char key[PATH_MAX]; | ||
212 | int r; | ||
213 | struct db_session_state_data * sess_data; | ||
214 | struct mail_cache_db * maildb; | ||
215 | int res; | ||
216 | struct mailimf_fields * fields; | ||
217 | MMAPString * mmapstr; | ||
218 | |||
219 | sess_data = get_session_data(msg_info); | ||
220 | |||
221 | r = mail_cache_db_open_lock(sess_data->db_filename, &maildb); | ||
222 | if (r < 0) { | ||
223 | res = MAIL_ERROR_FILE; | ||
224 | goto err; | ||
225 | } | ||
226 | |||
227 | snprintf(key, sizeof(key), "%lu-envelope", | ||
228 | (unsigned long) msg_info->msg_index); | ||
229 | |||
230 | mmapstr = mmap_string_new(""); | ||
231 | if (mmapstr == NULL) { | ||
232 | res = MAIL_ERROR_MEMORY; | ||
233 | goto close_db; | ||
234 | } | ||
235 | |||
236 | r = generic_cache_fields_read(maildb, mmapstr, | ||
237 | key, &fields); | ||
238 | |||
239 | mmap_string_free(mmapstr); | ||
240 | |||
241 | if (r != MAIL_NO_ERROR) { | ||
242 | res = MAIL_ERROR_MSG_NOT_FOUND; | ||
243 | goto close_db; | ||
244 | } | ||
245 | |||
246 | mail_cache_db_close_unlock(sess_data->db_filename, maildb); | ||
247 | |||
248 | * result = fields; | ||
249 | |||
250 | return MAIL_NO_ERROR; | ||
251 | |||
252 | close_db: | ||
253 | mail_cache_db_close_unlock(sess_data->db_filename, maildb); | ||
254 | err: | ||
255 | return res; | ||
256 | } | ||
257 | |||
258 | static int get_flags(mailmessage * msg_info, | ||
259 | struct mail_flags ** result) | ||
260 | { | ||
261 | char key[PATH_MAX]; | ||
262 | int r; | ||
263 | struct db_session_state_data * sess_data; | ||
264 | struct mail_cache_db * maildb; | ||
265 | int res; | ||
266 | MMAPString * mmapstr; | ||
267 | |||
268 | sess_data = get_session_data(msg_info); | ||
269 | |||
270 | r = mail_cache_db_open_lock(sess_data->db_filename, &maildb); | ||
271 | if (r < 0) { | ||
272 | res = MAIL_ERROR_FILE; | ||
273 | goto err; | ||
274 | } | ||
275 | |||
276 | snprintf(key, sizeof(key), "%lu-flags", (unsigned long) msg_info->msg_index); | ||
277 | |||
278 | mmapstr = mmap_string_new(""); | ||
279 | if (mmapstr == NULL) { | ||
280 | res = MAIL_ERROR_MEMORY; | ||
281 | goto close_db; | ||
282 | } | ||
283 | |||
284 | r = generic_cache_flags_read(maildb, mmapstr, | ||
285 | key, &msg_info->msg_flags); | ||
286 | |||
287 | mmap_string_free(mmapstr); | ||
288 | |||
289 | if (r != MAIL_NO_ERROR) { | ||
290 | msg_info->msg_flags = mail_flags_new_empty(); | ||
291 | if (msg_info->msg_flags == NULL) { | ||
292 | res = MAIL_ERROR_MEMORY; | ||
293 | goto close_db; | ||
294 | } | ||
295 | } | ||
296 | |||
297 | mail_cache_db_close_unlock(sess_data->db_filename, maildb); | ||
298 | |||
299 | * result = msg_info->msg_flags; | ||
300 | |||
301 | return MAIL_NO_ERROR; | ||
302 | |||
303 | close_db: | ||
304 | mail_cache_db_close_unlock(sess_data->db_filename, maildb); | ||
305 | err: | ||
306 | return res; | ||
307 | } | ||
308 | |||