Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / camel / providers / local / camel-mbox-folder.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; fill-column: 160 -*-
2  *
3  * Authors: Michael Zucchi <notzed@ximian.com>
4  *          Jeffrey Stedfast <fejj@ximian.com>
5  *
6  * Copyright (C) 1999, 2003 Ximian Inc.
7  *
8  * This program is free software; you can redistribute it and/or 
9  * modify it under the terms of version 2 of the GNU Lesser General Public 
10  * License as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
20  * USA
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34
35 #include <glib.h>
36 #include <glib/gi18n-lib.h>
37 #include <glib/gstdio.h>
38
39 #include "camel/camel-data-wrapper.h"
40 #include "camel/camel-exception.h"
41 #include "camel/camel-mime-filter-from.h"
42 #include "camel/camel-mime-message.h"
43 #include "camel/camel-private.h"
44 #include "camel/camel-stream-filter.h"
45 #include "camel/camel-stream-fs.h"
46
47 #include "camel-mbox-folder.h"
48 #include "camel-mbox-store.h"
49 #include "camel-mbox-summary.h"
50
51 #ifndef O_BINARY
52 #define O_BINARY 0
53 #endif
54
55 #define d(x) /*(printf("%s(%d): ", __FILE__, __LINE__),(x))*/
56
57 static CamelLocalFolderClass *parent_class = NULL;
58
59 /* Returns the class for a CamelMboxFolder */
60 #define CMBOXF_CLASS(so) CAMEL_MBOX_FOLDER_CLASS (CAMEL_OBJECT_GET_CLASS(so))
61 #define CF_CLASS(so) CAMEL_FOLDER_CLASS (CAMEL_OBJECT_GET_CLASS(so))
62 #define CMBOXS_CLASS(so) CAMEL_STORE_CLASS (CAMEL_OBJECT_GET_CLASS(so))
63
64 static int mbox_lock(CamelLocalFolder *lf, CamelLockType type, CamelException *ex);
65 static void mbox_unlock(CamelLocalFolder *lf);
66
67 static void mbox_append_message(CamelFolder *folder, CamelMimeMessage * message, const CamelMessageInfo * info, char **appended_uid, CamelException *ex);
68 static CamelMimeMessage *mbox_get_message(CamelFolder *folder, const gchar * uid, CamelException *ex);
69 static CamelLocalSummary *mbox_create_summary(CamelLocalFolder *lf, const char *path, const char *folder, CamelIndex *index);
70
71 static void mbox_finalise(CamelObject * object);
72
73 static void
74 camel_mbox_folder_class_init(CamelMboxFolderClass * camel_mbox_folder_class)
75 {
76         CamelFolderClass *camel_folder_class = CAMEL_FOLDER_CLASS(camel_mbox_folder_class);
77         CamelLocalFolderClass *lclass = (CamelLocalFolderClass *)camel_mbox_folder_class;
78
79         parent_class = (CamelLocalFolderClass *)camel_type_get_global_classfuncs(camel_local_folder_get_type());
80
81         /* virtual method definition */
82
83         /* virtual method overload */
84         camel_folder_class->append_message = mbox_append_message;
85         camel_folder_class->get_message = mbox_get_message;
86
87         lclass->create_summary = mbox_create_summary;
88         lclass->lock = mbox_lock;
89         lclass->unlock = mbox_unlock;
90 }
91
92 static void
93 mbox_init(gpointer object, gpointer klass)
94 {
95         /*CamelFolder *folder = object;*/
96         CamelMboxFolder *mbox_folder = object;
97
98         mbox_folder->lockfd = -1;
99 }
100
101 static void
102 mbox_finalise(CamelObject * object)
103 {
104         CamelMboxFolder *mbox_folder = (CamelMboxFolder *)object;
105
106         g_assert(mbox_folder->lockfd == -1);
107 }
108
109 CamelType camel_mbox_folder_get_type(void)
110 {
111         static CamelType camel_mbox_folder_type = CAMEL_INVALID_TYPE;
112
113         if (camel_mbox_folder_type == CAMEL_INVALID_TYPE) {
114                 camel_mbox_folder_type = camel_type_register(CAMEL_LOCAL_FOLDER_TYPE, "CamelMboxFolder",
115                                                              sizeof(CamelMboxFolder),
116                                                              sizeof(CamelMboxFolderClass),
117                                                              (CamelObjectClassInitFunc) camel_mbox_folder_class_init,
118                                                              NULL,
119                                                              (CamelObjectInitFunc) mbox_init,
120                                                              (CamelObjectFinalizeFunc) mbox_finalise);
121         }
122
123         return camel_mbox_folder_type;
124 }
125
126 CamelFolder *
127 camel_mbox_folder_new(CamelStore *parent_store, const char *full_name, guint32 flags, CamelException *ex)
128 {
129         CamelFolder *folder;
130
131         d(printf("Creating mbox folder: %s in %s\n", full_name, camel_local_store_get_toplevel_dir((CamelLocalStore *)parent_store)));
132
133         folder = (CamelFolder *)camel_object_new(CAMEL_MBOX_FOLDER_TYPE);
134         folder = (CamelFolder *)camel_local_folder_construct((CamelLocalFolder *)folder,
135                                                              parent_store, full_name, flags, ex);
136
137         return folder;
138 }
139
140 static CamelLocalSummary *mbox_create_summary(CamelLocalFolder *lf, const char *path, const char *folder, CamelIndex *index)
141 {
142         return (CamelLocalSummary *)camel_mbox_summary_new((CamelFolder *)lf, path, folder, index);
143 }
144
145 static int mbox_lock(CamelLocalFolder *lf, CamelLockType type, CamelException *ex)
146 {
147 #ifndef G_OS_WIN32
148         CamelMboxFolder *mf = (CamelMboxFolder *)lf;
149
150         /* make sure we have matching unlocks for locks, camel-local-folder class should enforce this */
151         g_assert(mf->lockfd == -1);
152
153         mf->lockfd = open(lf->folder_path, O_RDWR, 0);
154         if (mf->lockfd == -1) {
155                 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
156                                       _("Cannot create folder lock on %s: %s"),
157                                       lf->folder_path, g_strerror (errno));
158                 return -1;
159         }
160
161         if (camel_lock_folder(lf->folder_path, mf->lockfd, type, ex) == -1) {
162                 close(mf->lockfd);
163                 mf->lockfd = -1;
164                 return -1;
165         }
166 #endif
167         return 0;
168 }
169
170 static void mbox_unlock(CamelLocalFolder *lf)
171 {
172 #ifndef G_OS_WIN32
173         CamelMboxFolder *mf = (CamelMboxFolder *)lf;
174
175         g_assert(mf->lockfd != -1);
176         camel_unlock_folder(lf->folder_path, mf->lockfd);
177         close(mf->lockfd);
178         mf->lockfd = -1;
179 #endif
180 }
181
182 static void
183 mbox_append_message(CamelFolder *folder, CamelMimeMessage * message, const CamelMessageInfo * info, char **appended_uid, CamelException *ex)
184 {
185         CamelLocalFolder *lf = (CamelLocalFolder *)folder;
186         CamelStream *output_stream = NULL, *filter_stream = NULL;
187         CamelMimeFilter *filter_from;
188         CamelMboxSummary *mbs = (CamelMboxSummary *)folder->summary;
189         CamelMessageInfo *mi;
190         char *fromline = NULL;
191         struct stat st;
192         int retval;
193 #if 0
194         char *xev;
195 #endif
196         /* If we can't lock, dont do anything */
197         if (camel_local_folder_lock(lf, CAMEL_LOCK_WRITE, ex) == -1)
198                 return;
199
200         d(printf("Appending message\n"));
201
202         /* first, check the summary is correct (updates folder_size too) */
203         retval = camel_local_summary_check ((CamelLocalSummary *)folder->summary, lf->changes, ex);
204         if (retval == -1)
205                 goto fail;
206
207         /* add it to the summary/assign the uid, etc */
208         mi = camel_local_summary_add((CamelLocalSummary *)folder->summary, message, info, lf->changes, ex);
209         if (mi == NULL)
210                 goto fail;
211
212         d(printf("Appending message: uid is %s\n", camel_message_info_uid(mi)));
213
214         output_stream = camel_stream_fs_new_with_name(lf->folder_path, O_WRONLY | O_APPEND | O_LARGEFILE, 0666);
215         if (output_stream == NULL) {
216                 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
217                                       _("Cannot open mailbox: %s: %s\n"),
218                                       lf->folder_path, g_strerror (errno));
219                 goto fail;
220         }
221
222         /* and we need to set the frompos/XEV explicitly */
223         ((CamelMboxMessageInfo *)mi)->frompos = mbs->folder_size;
224 #if 0
225         xev = camel_local_summary_encode_x_evolution((CamelLocalSummary *)folder->summary, mi);
226         if (xev) {
227                 /* the x-ev header should match the 'current' flags, no problem, so store as much */
228                 camel_medium_set_header((CamelMedium *)message, "X-Evolution", xev);
229                 mi->flags &= ~ CAMEL_MESSAGE_FOLDER_NOXEV|CAMEL_MESSAGE_FOLDER_FLAGGED;
230                 g_free(xev);
231         }
232 #endif
233
234         /* we must write this to the non-filtered stream ... */
235         fromline = camel_mime_message_build_mbox_from(message);
236         if (camel_stream_write(output_stream, fromline, strlen(fromline)) == -1)
237                 goto fail_write;
238
239         /* and write the content to the filtering stream, that translates '\nFrom' into '\n>From' */
240         filter_stream = (CamelStream *) camel_stream_filter_new_with_stream(output_stream);
241         filter_from = (CamelMimeFilter *) camel_mime_filter_from_new();
242         camel_stream_filter_add((CamelStreamFilter *) filter_stream, filter_from);
243         camel_object_unref (filter_from);
244         
245         if (camel_data_wrapper_write_to_stream ((CamelDataWrapper *) message, filter_stream) == -1 ||
246             camel_stream_write (filter_stream, "\n", 1) == -1 ||
247             camel_stream_flush (filter_stream) == -1)
248                 goto fail_write;
249         
250         /* filter stream ref's the output stream itself, so we need to unref it too */
251         camel_object_unref (filter_stream);
252         camel_object_unref (output_stream);
253         g_free(fromline);
254         
255         /* now we 'fudge' the summary  to tell it its uptodate, because its idea of uptodate has just changed */
256         /* the stat really shouldn't fail, we just wrote to it */
257         if (g_stat (lf->folder_path, &st) == 0) {
258                 ((CamelFolderSummary *) mbs)->time = st.st_mtime;
259                 mbs->folder_size = st.st_size;
260         }
261         
262         /* unlock as soon as we can */
263         camel_local_folder_unlock(lf);
264
265         if (camel_folder_change_info_changed(lf->changes)) {
266                 camel_object_trigger_event((CamelObject *)folder, "folder_changed", lf->changes);
267                 camel_folder_change_info_clear(lf->changes);
268         }
269
270         if (appended_uid)
271                 *appended_uid = g_strdup(camel_message_info_uid(mi));
272
273         return;
274
275 fail_write:
276         if (errno == EINTR)
277                 camel_exception_set (ex, CAMEL_EXCEPTION_USER_CANCEL,
278                                      _("Mail append canceled"));
279         else
280                 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
281                                       _("Cannot append message to mbox file: %s: %s"),
282                                       lf->folder_path, g_strerror (errno));
283         
284         if (output_stream) {
285                 /* reset the file to original size */
286                 do {
287                         retval = ftruncate (((CamelStreamFs *) output_stream)->fd, mbs->folder_size);
288                 } while (retval == -1 && errno == EINTR);
289                 
290                 camel_object_unref (output_stream);
291         }
292         
293         if (filter_stream)
294                 camel_object_unref (filter_stream);
295         
296         g_free(fromline);
297         
298         /* remove the summary info so we are not out-of-sync with the mbox */
299         camel_folder_summary_remove_uid (CAMEL_FOLDER_SUMMARY (mbs), camel_message_info_uid (mi));
300         
301         /* and tell the summary it's up-to-date */
302         if (g_stat (lf->folder_path, &st) == 0) {
303                 ((CamelFolderSummary *) mbs)->time = st.st_mtime;
304                 mbs->folder_size = st.st_size;
305         }
306         
307 fail:
308         /* make sure we unlock the folder - before we start triggering events into appland */
309         camel_local_folder_unlock(lf);
310
311         /* cascade the changes through, anyway, if there are any outstanding */
312         if (camel_folder_change_info_changed(lf->changes)) {
313                 camel_object_trigger_event((CamelObject *)folder, "folder_changed", lf->changes);
314                 camel_folder_change_info_clear(lf->changes);
315         }
316 }
317
318 static CamelMimeMessage *
319 mbox_get_message(CamelFolder *folder, const gchar * uid, CamelException *ex)
320 {
321         CamelLocalFolder *lf = (CamelLocalFolder *)folder;
322         CamelMimeMessage *message = NULL;
323         CamelMboxMessageInfo *info;
324         CamelMimeParser *parser = NULL;
325         int fd, retval;
326         int retried = FALSE;
327         off_t frompos;
328
329         d(printf("Getting message %s\n", uid));
330
331         /* lock the folder first, burn if we can't, need write lock for summary check */
332         if (camel_local_folder_lock(lf, CAMEL_LOCK_WRITE, ex) == -1)
333                 return NULL;
334
335         /* check for new messages always */
336         if (camel_local_summary_check((CamelLocalSummary *)folder->summary, lf->changes, ex) == -1) {
337                 camel_local_folder_unlock(lf);
338                 return NULL;
339         }
340         
341 retry:
342         /* get the message summary info */
343         info = (CamelMboxMessageInfo *) camel_folder_summary_uid(folder->summary, uid);
344
345         if (info == NULL) {
346                 camel_exception_setv(ex, CAMEL_EXCEPTION_FOLDER_INVALID_UID,
347                                      _("Cannot get message: %s from folder %s\n  %s"),
348                                      uid, lf->folder_path, _("No such message"));
349                 goto fail;
350         }
351
352         /* no frompos, its an error in the library (and we can't do anything with it) */
353         g_assert(info->frompos != -1);
354
355         frompos = info->frompos;
356         camel_message_info_free((CamelMessageInfo *)info);
357         
358         /* we use an fd instead of a normal stream here - the reason is subtle, camel_mime_part will cache
359            the whole message in memory if the stream is non-seekable (which it is when built from a parser
360            with no stream).  This means we dont have to lock the mbox for the life of the message, but only
361            while it is being created. */
362
363         fd = g_open(lf->folder_path, O_LARGEFILE | O_RDONLY | O_BINARY, 0);
364         if (fd == -1) {
365                 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
366                                       _("Cannot get message: %s from folder %s\n  %s"),
367                                       uid, lf->folder_path, g_strerror (errno));
368                 goto fail;
369         }
370
371         /* we use a parser to verify the message is correct, and in the correct position */
372         parser = camel_mime_parser_new();
373         camel_mime_parser_init_with_fd(parser, fd);
374         camel_mime_parser_scan_from(parser, TRUE);
375
376         camel_mime_parser_seek(parser, frompos, SEEK_SET);
377         if (camel_mime_parser_step(parser, NULL, NULL) != CAMEL_MIME_PARSER_STATE_FROM
378             || camel_mime_parser_tell_start_from(parser) != frompos) {
379
380                 g_warning("Summary doesn't match the folder contents!  eek!\n"
381                           "  expecting offset %ld got %ld, state = %d", (long int)frompos,
382                           (long int)camel_mime_parser_tell_start_from(parser),
383                           camel_mime_parser_state(parser));
384
385                 camel_object_unref((CamelObject *)parser);
386                 parser = NULL;
387
388                 if (!retried) {
389                         retried = TRUE;
390                         camel_local_summary_check_force((CamelLocalSummary *)folder->summary);
391                         retval = camel_local_summary_check((CamelLocalSummary *)folder->summary, lf->changes, ex);
392                         if (retval != -1)
393                                 goto retry;
394                 }
395
396                 camel_exception_setv(ex, CAMEL_EXCEPTION_FOLDER_INVALID,
397                                      _("Cannot get message: %s from folder %s\n  %s"), uid, lf->folder_path,
398                                      _("The folder appears to be irrecoverably corrupted."));
399                 goto fail;
400         }
401         
402         message = camel_mime_message_new();
403         if (camel_mime_part_construct_from_parser((CamelMimePart *)message, parser) == -1) {
404                 camel_exception_setv(ex, errno==EINTR?CAMEL_EXCEPTION_USER_CANCEL:CAMEL_EXCEPTION_SYSTEM,
405                                      _("Cannot get message: %s from folder %s\n  %s"), uid, lf->folder_path,
406                                      _("Message construction failed."));
407                 camel_object_unref((CamelObject *)message);
408                 message = NULL;
409                 goto fail;
410         }
411
412         camel_medium_remove_header((CamelMedium *)message, "X-Evolution");
413 fail:
414         /* and unlock now we're finished with it */
415         camel_local_folder_unlock(lf);
416
417         if (parser)
418                 camel_object_unref((CamelObject *)parser);
419         
420         /* use the opportunity to notify of changes (particularly if we had a rebuild) */
421         if (camel_folder_change_info_changed(lf->changes)) {
422                 camel_object_trigger_event((CamelObject *)folder, "folder_changed", lf->changes);
423                 camel_folder_change_info_clear(lf->changes);
424         }
425         
426         return message;
427 }