summaryrefslogtreecommitdiffabout
path: root/kmicromail/libetpan/generic/maildirdriver_cached_message.c
authorzautrix <zautrix>2004-07-03 16:33:12 (UTC)
committer zautrix <zautrix>2004-07-03 16:33:12 (UTC)
commite3b89230f065c48c84b48c88edb6eb088374c487 (patch) (unidiff)
tree162ea2ef909a6f82ccfcedf45d80d6c821174912 /kmicromail/libetpan/generic/maildirdriver_cached_message.c
parent2dd6ac0b2d24c91d35ce674a6c26351352df2b15 (diff)
downloadkdepimpi-e3b89230f065c48c84b48c88edb6eb088374c487.zip
kdepimpi-e3b89230f065c48c84b48c88edb6eb088374c487.tar.gz
kdepimpi-e3b89230f065c48c84b48c88edb6eb088374c487.tar.bz2
Initial revision
Diffstat (limited to 'kmicromail/libetpan/generic/maildirdriver_cached_message.c') (more/less context) (ignore whitespace changes)
-rw-r--r--kmicromail/libetpan/generic/maildirdriver_cached_message.c248
1 files changed, 248 insertions, 0 deletions
diff --git a/kmicromail/libetpan/generic/maildirdriver_cached_message.c b/kmicromail/libetpan/generic/maildirdriver_cached_message.c
new file mode 100644
index 0000000..51866aa
--- a/dev/null
+++ b/kmicromail/libetpan/generic/maildirdriver_cached_message.c
@@ -0,0 +1,248 @@
1/*
2 * libEtPan! -- a mail stuff library
3 *
4 * Copyright (C) 2001, 2002 - 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 REGENTS 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 REGENTS 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 "maildirdriver_message.h"
37
38#include "mailmessage_tools.h"
39#include "maildirdriver.h"
40#include "maildir.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
51static int prefetch(mailmessage * msg_info);
52
53static void prefetch_free(struct generic_message_t * msg);
54
55static int initialize(mailmessage * msg_info);
56
57static void check(mailmessage * msg_info);
58
59static mailmessage_driver local_maildir_cached_message_driver = {
60 .msg_name = "maildir-cached",
61
62 .msg_initialize = initialize,
63 .msg_uninitialize = mailmessage_generic_uninitialize,
64
65 .msg_flush = mailmessage_generic_flush,
66 .msg_check = check,
67
68 .msg_fetch_result_free = mailmessage_generic_fetch_result_free,
69
70 .msg_fetch = mailmessage_generic_fetch,
71 .msg_fetch_header = mailmessage_generic_fetch_header,
72 .msg_fetch_body = mailmessage_generic_fetch_header,
73 .msg_fetch_size = NULL,
74 .msg_get_bodystructure = mailmessage_generic_get_bodystructure,
75 .msg_fetch_section = mailmessage_generic_fetch_section,
76 .msg_fetch_section_header = mailmessage_generic_fetch_section_header,
77 .msg_fetch_section_mime = mailmessage_generic_fetch_section_mime,
78 .msg_fetch_section_body = mailmessage_generic_fetch_section_body,
79 .msg_fetch_envelope = mailmessage_generic_fetch_envelope,
80
81 .msg_get_flags = NULL,
82};
83
84mailmessage_driver * maildir_cached_message_driver =
85&local_maildir_cached_message_driver;
86
87struct maildir_msg_data {
88 int fd;
89};
90
91#if 0
92static inline struct maildir_cached_session_state_data *
93get_cached_session_data(mailmessage * msg)
94{
95 return msg->session->data;
96}
97
98static inline mailsession * cached_session_get_ancestor(mailsession * session)
99{
100 return get_data(session)->session;
101}
102
103static inline struct maildir_session_state_data *
104cached_session_get_ancestor_data(mailsession * session)
105{
106 return get_ancestor(session)->data;
107}
108
109static struct maildir * get_maildir_session(mailmessage * msg)
110{
111 return cached_session_get_ancestor_data(msg->session)->session;
112}
113#endif
114static inline struct maildir_cached_session_state_data *
115get_cached_session_data(mailmessage * msg)
116{
117 return msg->msg_session->sess_data;
118}
119
120static inline struct maildir_cached_session_state_data *
121cached_session_get_data(mailsession * s)
122{
123 return s->sess_data;
124}
125
126static inline mailsession * cached_session_get_ancestor(mailsession * s)
127{
128 return cached_session_get_data(s)->md_ancestor;
129}
130
131static inline struct maildir_session_state_data *
132cached_session_get_ancestor_data(mailsession * s)
133{
134 return cached_session_get_ancestor(s)->sess_data;
135}
136
137static inline struct maildir_session_state_data *
138get_session_ancestor_data(mailmessage * msg)
139{
140 return cached_session_get_ancestor_data(msg->msg_session);
141}
142
143static inline struct maildir *
144cached_session_get_maildir_session(mailsession * session)
145{
146 return cached_session_get_ancestor_data(session)->md_session;
147}
148
149static inline struct maildir * get_maildir_session(mailmessage * msg)
150{
151 return cached_session_get_maildir_session(msg->msg_session);
152}
153
154static int prefetch(mailmessage * msg_info)
155{
156 struct generic_message_t * msg;
157 int res;
158 struct maildir_msg_data * data;
159 char * filename;
160 int fd;
161 char * mapping;
162 struct maildir * md;
163
164 md = get_maildir_session(msg_info);
165
166 filename = maildir_message_get(md, msg_info->msg_uid);
167 if (filename == NULL) {
168 res = MAIL_ERROR_MEMORY;
169 goto err;
170 }
171
172 fd = open(filename, O_RDONLY);
173 free(filename);
174 if (fd == -1) {
175 res = MAIL_ERROR_FILE;
176 goto err;
177 }
178
179 mapping = mmap(NULL, msg_info->msg_size, PROT_READ, MAP_PRIVATE, fd, 0);
180 if (mapping == MAP_FAILED) {
181 res = MAIL_ERROR_FILE;
182 goto close;
183 }
184
185 data = malloc(sizeof(* data));
186 if (data == NULL) {
187 res = MAIL_ERROR_MEMORY;
188 goto unmap;
189 }
190
191 data->fd = fd;
192
193 msg = msg_info->msg_data;
194
195 msg->msg_data = data;
196 msg->msg_message = mapping;
197 msg->msg_length = msg_info->msg_size;
198
199 return MAIL_NO_ERROR;
200
201 unmap:
202 munmap(mapping, msg_info->msg_size);
203 close:
204 close(fd);
205 err:
206 return res;
207}
208
209static void prefetch_free(struct generic_message_t * msg)
210{
211 if (msg->msg_message != NULL) {
212 struct maildir_msg_data * data;
213
214 munmap(msg->msg_message, msg->msg_length);
215 msg->msg_message = NULL;
216 data = msg->msg_data;
217 close(data->fd);
218 free(data);
219 }
220}
221
222static int initialize(mailmessage * msg_info)
223{
224 struct generic_message_t * msg;
225 int r;
226
227 r = mailmessage_generic_initialize(msg_info);
228 if (r != MAIL_NO_ERROR)
229 return r;
230
231 msg = msg_info->msg_data;
232 msg->msg_prefetch = prefetch;
233 msg->msg_prefetch_free = prefetch_free;
234
235 return MAIL_NO_ERROR;
236}
237
238static void check(mailmessage * msg_info)
239{
240 int r;
241
242 if (msg_info->msg_flags != NULL) {
243 r = mail_flags_store_set(get_session_ancestor_data(msg_info)->md_flags_store, msg_info);
244
245 r = mail_flags_store_set(get_cached_session_data(msg_info)->md_flags_store, msg_info);
246 /* ignore errors */
247 }
248}