Merge miscellaneous cleanups from camel-gobject.
[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-2008 Novell, Inc. (www.novell.com)
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 #include <inttypes.h>
35
36 #include <glib.h>
37 #include <glib/gi18n-lib.h>
38 #include <glib/gstdio.h>
39
40 #include "camel-mbox-folder.h"
41 #include "camel-mbox-store.h"
42 #include "camel-mbox-summary.h"
43
44 #ifndef O_BINARY
45 #define O_BINARY 0
46 #endif
47
48 #define d(x) /*(printf("%s(%d): ", __FILE__, __LINE__),(x))*/
49
50 static CamelLocalFolderClass *parent_class = NULL;
51
52 /* Returns the class for a CamelMboxFolder */
53 #define CMBOXF_CLASS(so) CAMEL_MBOX_FOLDER_CLASS (CAMEL_OBJECT_GET_CLASS(so))
54 #define CF_CLASS(so) CAMEL_FOLDER_CLASS (CAMEL_OBJECT_GET_CLASS(so))
55 #define CMBOXS_CLASS(so) CAMEL_STORE_CLASS (CAMEL_OBJECT_GET_CLASS(so))
56
57 static gint mbox_lock(CamelLocalFolder *lf, CamelLockType type, CamelException *ex);
58 static void mbox_unlock(CamelLocalFolder *lf);
59
60 static void mbox_append_message(CamelFolder *folder, CamelMimeMessage * message, const CamelMessageInfo * info, gchar **appended_uid, CamelException *ex);
61 static CamelMimeMessage *mbox_get_message(CamelFolder *folder, const gchar * uid, CamelException *ex);
62 static CamelLocalSummary *mbox_create_summary(CamelLocalFolder *lf, const gchar *path, const gchar *folder, CamelIndex *index);
63 static gchar * mbox_get_filename (CamelFolder *folder, const gchar *uid, CamelException *ex);
64 static gint mbox_cmp_uids (CamelFolder *folder, const gchar *uid1, const gchar *uid2);
65 static void mbox_sort_uids (CamelFolder *folder, GPtrArray *uids);
66
67 static void mbox_finalize(CamelObject * object);
68
69 static void
70 camel_mbox_folder_class_init(CamelMboxFolderClass * camel_mbox_folder_class)
71 {
72         CamelFolderClass *camel_folder_class = CAMEL_FOLDER_CLASS(camel_mbox_folder_class);
73         CamelLocalFolderClass *lclass = (CamelLocalFolderClass *)camel_mbox_folder_class;
74
75         parent_class = (CamelLocalFolderClass *)camel_type_get_global_classfuncs(camel_local_folder_get_type());
76
77         /* virtual method definition */
78
79         /* virtual method overload */
80         camel_folder_class->append_message = mbox_append_message;
81         camel_folder_class->get_message = mbox_get_message;
82         camel_folder_class->get_filename = mbox_get_filename;
83         camel_folder_class->cmp_uids = mbox_cmp_uids;
84         camel_folder_class->sort_uids = mbox_sort_uids;
85
86         lclass->create_summary = mbox_create_summary;
87         lclass->lock = mbox_lock;
88         lclass->unlock = mbox_unlock;
89 }
90
91 static void
92 mbox_init(gpointer object, gpointer klass)
93 {
94         /*CamelFolder *folder = object;*/
95         CamelMboxFolder *mbox_folder = object;
96
97         mbox_folder->lockfd = -1;
98 }
99
100 static void
101 mbox_finalize(CamelObject * object)
102 {
103         CamelMboxFolder *mbox_folder = (CamelMboxFolder *)object;
104
105         g_assert(mbox_folder->lockfd == -1);
106 }
107
108 CamelType camel_mbox_folder_get_type(void)
109 {
110         static CamelType camel_mbox_folder_type = CAMEL_INVALID_TYPE;
111
112         if (camel_mbox_folder_type == CAMEL_INVALID_TYPE) {
113                 camel_mbox_folder_type = camel_type_register(CAMEL_LOCAL_FOLDER_TYPE, "CamelMboxFolder",
114                                                              sizeof(CamelMboxFolder),
115                                                              sizeof(CamelMboxFolderClass),
116                                                              (CamelObjectClassInitFunc) camel_mbox_folder_class_init,
117                                                              NULL,
118                                                              (CamelObjectInitFunc) mbox_init,
119                                                              (CamelObjectFinalizeFunc) mbox_finalize);
120         }
121
122         return camel_mbox_folder_type;
123 }
124
125 CamelFolder *
126 camel_mbox_folder_new(CamelStore *parent_store, const gchar *full_name, guint32 flags, CamelException *ex)
127 {
128         CamelFolder *folder;
129
130         d(printf("Creating mbox folder: %s in %s\n", full_name, camel_local_store_get_toplevel_dir((CamelLocalStore *)parent_store)));
131
132         folder = (CamelFolder *)camel_object_new(CAMEL_MBOX_FOLDER_TYPE);
133         folder = (CamelFolder *)camel_local_folder_construct((CamelLocalFolder *)folder,
134                                                              parent_store, full_name, flags, ex);
135
136         return folder;
137 }
138
139 static CamelLocalSummary *mbox_create_summary(CamelLocalFolder *lf, const gchar *path, const gchar *folder, CamelIndex *index)
140 {
141         return (CamelLocalSummary *)camel_mbox_summary_new((CamelFolder *)lf, path, folder, index);
142 }
143
144 static gint mbox_lock(CamelLocalFolder *lf, CamelLockType type, CamelException *ex)
145 {
146 #ifndef G_OS_WIN32
147         CamelMboxFolder *mf = (CamelMboxFolder *)lf;
148
149         /* make sure we have matching unlocks for locks, camel-local-folder class should enforce this */
150         g_assert(mf->lockfd == -1);
151
152         mf->lockfd = open(lf->folder_path, O_RDWR|O_LARGEFILE, 0);
153         if (mf->lockfd == -1) {
154                 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
155                                       _("Cannot create folder lock on %s: %s"),
156                                       lf->folder_path, g_strerror (errno));
157                 return -1;
158         }
159
160         if (camel_lock_folder(lf->folder_path, mf->lockfd, type, ex) == -1) {
161                 close(mf->lockfd);
162                 mf->lockfd = -1;
163                 return -1;
164         }
165 #endif
166         return 0;
167 }
168
169 static void mbox_unlock(CamelLocalFolder *lf)
170 {
171 #ifndef G_OS_WIN32
172         CamelMboxFolder *mf = (CamelMboxFolder *)lf;
173
174         g_assert(mf->lockfd != -1);
175         camel_unlock_folder(lf->folder_path, mf->lockfd);
176         close(mf->lockfd);
177         mf->lockfd = -1;
178 #endif
179 }
180
181 static void
182 mbox_append_message(CamelFolder *folder, CamelMimeMessage * message, const CamelMessageInfo * info, gchar **appended_uid, CamelException *ex)
183 {
184         CamelLocalFolder *lf = (CamelLocalFolder *)folder;
185         CamelStream *output_stream = NULL, *filter_stream = NULL;
186         CamelMimeFilter *filter_from;
187         CamelMboxSummary *mbs = (CamelMboxSummary *)folder->summary;
188         CamelMessageInfo *mi;
189         gchar *fromline = NULL;
190         struct stat st;
191         gint retval;
192 #if 0
193         gchar *xev;
194 #endif
195         /* If we can't lock, dont do anything */
196         if (camel_local_folder_lock(lf, CAMEL_LOCK_WRITE, ex) == -1)
197                 return;
198
199         d(printf("Appending message\n"));
200
201         /* first, check the summary is correct (updates folder_size too) */
202         retval = camel_local_summary_check ((CamelLocalSummary *)folder->summary, lf->changes, ex);
203         if (retval == -1)
204                 goto fail;
205
206         /* add it to the summary/assign the uid, etc */
207         mi = camel_local_summary_add((CamelLocalSummary *)folder->summary, message, info, lf->changes, ex);
208         if (mi == NULL)
209                 goto fail;
210
211         d(printf("Appending message: uid is %s\n", camel_message_info_uid(mi)));
212
213         if ((camel_message_info_flags (mi) & CAMEL_MESSAGE_ATTACHMENTS) && !camel_mime_message_has_attachment (message))
214                 camel_message_info_set_flags (mi, CAMEL_MESSAGE_ATTACHMENTS, 0);
215
216         output_stream = camel_stream_fs_new_with_name(lf->folder_path, O_WRONLY | O_APPEND | O_LARGEFILE, 0666);
217         if (output_stream == NULL) {
218                 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
219                                       _("Cannot open mailbox: %s: %s\n"),
220                                       lf->folder_path, g_strerror (errno));
221                 goto fail;
222         }
223
224         /* and we need to set the frompos/XEV explicitly */
225         ((CamelMboxMessageInfo *)mi)->frompos = mbs->folder_size;
226 #if 0
227         xev = camel_local_summary_encode_x_evolution((CamelLocalSummary *)folder->summary, mi);
228         if (xev) {
229                 /* the x-ev header should match the 'current' flags, no problem, so store as much */
230                 camel_medium_set_header((CamelMedium *)message, "X-Evolution", xev);
231                 mi->flags &= ~ CAMEL_MESSAGE_FOLDER_NOXEV|CAMEL_MESSAGE_FOLDER_FLAGGED;
232                 g_free(xev);
233         }
234 #endif
235
236         /* we must write this to the non-filtered stream ... */
237         fromline = camel_mime_message_build_mbox_from(message);
238         if (camel_stream_write(output_stream, fromline, strlen(fromline)) == -1)
239                 goto fail_write;
240
241         /* and write the content to the filtering stream, that translates '\nFrom' into '\n>From' */
242         filter_stream = camel_stream_filter_new (output_stream);
243         filter_from = (CamelMimeFilter *) camel_mime_filter_from_new();
244         camel_stream_filter_add((CamelStreamFilter *) filter_stream, filter_from);
245         camel_object_unref (filter_from);
246
247         if (camel_data_wrapper_write_to_stream ((CamelDataWrapper *) message, filter_stream) == -1 ||
248             camel_stream_write (filter_stream, "\n", 1) == -1 ||
249             camel_stream_flush (filter_stream) == -1)
250                 goto fail_write;
251
252         /* filter stream ref's the output stream itself, so we need to unref it too */
253         camel_object_unref (filter_stream);
254         camel_object_unref (output_stream);
255         g_free(fromline);
256
257         if (!((CamelMessageInfoBase *)mi)->preview && camel_folder_summary_get_need_preview(folder->summary)) {
258                 if (camel_mime_message_build_preview ((CamelMimePart *)message, mi) && ((CamelMessageInfoBase *)mi)->preview)
259                         camel_folder_summary_add_preview (folder->summary, mi);
260         }
261
262         /* now we 'fudge' the summary  to tell it its uptodate, because its idea of uptodate has just changed */
263         /* the stat really shouldn't fail, we just wrote to it */
264         if (g_stat (lf->folder_path, &st) == 0) {
265                 ((CamelFolderSummary *) mbs)->time = st.st_mtime;
266                 mbs->folder_size = st.st_size;
267         }
268
269         /* unlock as soon as we can */
270         camel_local_folder_unlock(lf);
271
272         if (camel_folder_change_info_changed(lf->changes)) {
273                 camel_object_trigger_event((CamelObject *)folder, "folder_changed", lf->changes);
274                 camel_folder_change_info_clear(lf->changes);
275         }
276
277         if (appended_uid)
278                 *appended_uid = g_strdup(camel_message_info_uid(mi));
279
280         return;
281
282 fail_write:
283         if (errno == EINTR)
284                 camel_exception_set (ex, CAMEL_EXCEPTION_USER_CANCEL,
285                                      _("Mail append canceled"));
286         else
287                 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
288                                       _("Cannot append message to mbox file: %s: %s"),
289                                       lf->folder_path, g_strerror (errno));
290
291         if (output_stream) {
292                 /* reset the file to original size */
293                 do {
294                         retval = ftruncate (((CamelStreamFs *) output_stream)->fd, mbs->folder_size);
295                 } while (retval == -1 && errno == EINTR);
296
297                 camel_object_unref (output_stream);
298         }
299
300         if (filter_stream)
301                 camel_object_unref (filter_stream);
302
303         g_free(fromline);
304
305         /* remove the summary info so we are not out-of-sync with the mbox */
306         camel_folder_summary_remove_uid (CAMEL_FOLDER_SUMMARY (mbs), camel_message_info_uid (mi));
307
308         /* and tell the summary it's up-to-date */
309         if (g_stat (lf->folder_path, &st) == 0) {
310                 ((CamelFolderSummary *) mbs)->time = st.st_mtime;
311                 mbs->folder_size = st.st_size;
312         }
313
314 fail:
315         /* make sure we unlock the folder - before we start triggering events into appland */
316         camel_local_folder_unlock(lf);
317
318         /* cascade the changes through, anyway, if there are any outstanding */
319         if (camel_folder_change_info_changed(lf->changes)) {
320                 camel_object_trigger_event((CamelObject *)folder, "folder_changed", lf->changes);
321                 camel_folder_change_info_clear(lf->changes);
322         }
323 }
324
325 static gchar *
326 mbox_get_filename (CamelFolder *folder, const gchar *uid, CamelException *ex)
327 {
328         CamelLocalFolder *lf = (CamelLocalFolder *)folder;
329         CamelMboxMessageInfo *info;
330         off_t frompos;
331         gchar *filename = NULL;
332
333         d(printf("Getting message %s\n", uid));
334
335         /* lock the folder first, burn if we can't, need write lock for summary check */
336         if (camel_local_folder_lock(lf, CAMEL_LOCK_WRITE, ex) == -1)
337                 return NULL;
338
339         /* check for new messages always */
340         if (camel_local_summary_check((CamelLocalSummary *)folder->summary, lf->changes, ex) == -1) {
341                 camel_local_folder_unlock(lf);
342                 return NULL;
343         }
344
345         /* get the message summary info */
346         info = (CamelMboxMessageInfo *) camel_folder_summary_uid(folder->summary, uid);
347
348         if (info == NULL) {
349                 set_cannot_get_message_ex (ex, CAMEL_EXCEPTION_FOLDER_INVALID_UID,
350                                      uid, lf->folder_path, _("No such message"));
351                 goto fail;
352         }
353
354         if (info->frompos == -1) {
355                 camel_message_info_free((CamelMessageInfo *)info);
356                 goto fail;
357         }
358
359         frompos = info->frompos;
360         camel_message_info_free((CamelMessageInfo *)info);
361
362         filename = g_strdup_printf ("%s%s!%" PRId64, lf->folder_path, G_DIR_SEPARATOR_S, (gint64) frompos);
363
364 fail:
365         /* and unlock now we're finished with it */
366         camel_local_folder_unlock(lf);
367
368         return filename;
369 }
370
371 static CamelMimeMessage *
372 mbox_get_message(CamelFolder *folder, const gchar * uid, CamelException *ex)
373 {
374         CamelLocalFolder *lf = (CamelLocalFolder *)folder;
375         CamelMimeMessage *message = NULL;
376         CamelMboxMessageInfo *info;
377         CamelMimeParser *parser = NULL;
378         gint fd, retval;
379         gint retried = FALSE;
380         off_t frompos;
381
382         d(printf("Getting message %s\n", uid));
383
384         /* lock the folder first, burn if we can't, need write lock for summary check */
385         if (camel_local_folder_lock(lf, CAMEL_LOCK_WRITE, ex) == -1)
386                 return NULL;
387
388         /* check for new messages always */
389         if (camel_local_summary_check((CamelLocalSummary *)folder->summary, lf->changes, ex) == -1) {
390                 camel_local_folder_unlock(lf);
391                 return NULL;
392         }
393
394 retry:
395         /* get the message summary info */
396         info = (CamelMboxMessageInfo *) camel_folder_summary_uid(folder->summary, uid);
397
398         if (info == NULL) {
399                 set_cannot_get_message_ex (ex, CAMEL_EXCEPTION_FOLDER_INVALID_UID,
400                                      uid, lf->folder_path, _("No such message"));
401                 goto fail;
402         }
403
404         if (info->frompos == -1) {
405                 camel_message_info_free((CamelMessageInfo *)info);
406                 goto fail;
407         }
408
409         frompos = info->frompos;
410         camel_message_info_free((CamelMessageInfo *)info);
411
412         /* we use an fd instead of a normal stream here - the reason is subtle, camel_mime_part will cache
413            the whole message in memory if the stream is non-seekable (which it is when built from a parser
414            with no stream).  This means we dont have to lock the mbox for the life of the message, but only
415            while it is being created. */
416
417         fd = g_open(lf->folder_path, O_LARGEFILE | O_RDONLY | O_BINARY, 0);
418         if (fd == -1) {
419                 set_cannot_get_message_ex (ex, CAMEL_EXCEPTION_SYSTEM,
420                                       uid, lf->folder_path, g_strerror (errno));
421                 goto fail;
422         }
423
424         /* we use a parser to verify the message is correct, and in the correct position */
425         parser = camel_mime_parser_new();
426         camel_mime_parser_init_with_fd(parser, fd);
427         camel_mime_parser_scan_from(parser, TRUE);
428
429         camel_mime_parser_seek(parser, frompos, SEEK_SET);
430         if (camel_mime_parser_step(parser, NULL, NULL) != CAMEL_MIME_PARSER_STATE_FROM
431             || camel_mime_parser_tell_start_from(parser) != frompos) {
432
433                 g_warning("Summary doesn't match the folder contents!  eek!\n"
434                           "  expecting offset %ld got %ld, state = %d", (glong)frompos,
435                           (glong)camel_mime_parser_tell_start_from(parser),
436                           camel_mime_parser_state(parser));
437
438                 camel_object_unref((CamelObject *)parser);
439                 parser = NULL;
440
441                 if (!retried) {
442                         retried = TRUE;
443                         camel_local_summary_check_force((CamelLocalSummary *)folder->summary);
444                         retval = camel_local_summary_check((CamelLocalSummary *)folder->summary, lf->changes, ex);
445                         if (retval != -1)
446                                 goto retry;
447                 }
448
449                 set_cannot_get_message_ex (ex, CAMEL_EXCEPTION_FOLDER_INVALID,
450                                      uid, lf->folder_path, _("The folder appears to be irrecoverably corrupted."));
451                 goto fail;
452         }
453
454         message = camel_mime_message_new();
455         if (camel_mime_part_construct_from_parser((CamelMimePart *)message, parser) == -1) {
456                 set_cannot_get_message_ex (ex, errno==EINTR?CAMEL_EXCEPTION_USER_CANCEL:CAMEL_EXCEPTION_SYSTEM,
457                                      uid, lf->folder_path, _("Message construction failed."));
458                 camel_object_unref((CamelObject *)message);
459                 message = NULL;
460                 goto fail;
461         }
462
463         camel_medium_remove_header((CamelMedium *)message, "X-Evolution");
464
465 fail:
466         /* and unlock now we're finished with it */
467         camel_local_folder_unlock(lf);
468
469         if (parser)
470                 camel_object_unref((CamelObject *)parser);
471
472         /* use the opportunity to notify of changes (particularly if we had a rebuild) */
473         if (camel_folder_change_info_changed(lf->changes)) {
474                 camel_object_trigger_event((CamelObject *)folder, "folder_changed", lf->changes);
475                 camel_folder_change_info_clear(lf->changes);
476         }
477
478         return message;
479 }
480
481 static gint
482 mbox_cmp_uids (CamelFolder *folder, const gchar *uid1, const gchar *uid2)
483 {
484         CamelMboxMessageInfo *a, *b;
485
486         g_return_val_if_fail (folder != NULL, 0);
487         g_return_val_if_fail (folder->summary != NULL, 0);
488
489         a = (CamelMboxMessageInfo *) camel_folder_summary_uid (folder->summary, uid1);
490         b = (CamelMboxMessageInfo *) camel_folder_summary_uid (folder->summary, uid2);
491
492         g_return_val_if_fail (a != NULL, 0);
493         g_return_val_if_fail (b != NULL, 0);
494
495         return a->frompos < b->frompos ? -1 : a->frompos == b->frompos ? 0 : 1;
496 }
497
498 static void
499 mbox_sort_uids (CamelFolder *folder, GPtrArray *uids)
500 {
501         g_return_if_fail (parent_class != NULL);
502         g_return_if_fail (folder != NULL);
503
504         if (uids && uids->len > 1) {
505                 CamelException ex;
506
507                 camel_exception_init (&ex);
508
509                 camel_folder_summary_ensure_infos_loaded (folder->summary, uids->len, &ex);
510
511                 if (camel_exception_is_set (&ex))
512                         g_warning ("%s: %s", G_STRFUNC, camel_exception_get_description (&ex));
513
514                 camel_exception_clear (&ex);
515         }
516
517         CAMEL_FOLDER_CLASS (parent_class)->sort_uids (folder, uids);
518 }