1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; fill-column: 160 -*-
3 * Authors: Michael Zucchi <notzed@ximian.com>
4 * Jeffrey Stedfast <fejj@ximian.com>
6 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
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.
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.
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
33 #include <sys/types.h>
37 #include <glib/gi18n-lib.h>
38 #include <glib/gstdio.h>
40 #include "camel-mbox-folder.h"
41 #include "camel-mbox-store.h"
42 #include "camel-mbox-summary.h"
48 #define d(x) /*(printf("%s(%d): ", __FILE__, __LINE__),(x))*/
50 static CamelLocalFolderClass *parent_class = NULL;
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))
57 static gint mbox_lock(CamelLocalFolder *lf, CamelLockType type, CamelException *ex);
58 static void mbox_unlock(CamelLocalFolder *lf);
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);
67 static void mbox_finalize(CamelObject * object);
70 camel_mbox_folder_class_init(CamelMboxFolderClass * camel_mbox_folder_class)
72 CamelFolderClass *camel_folder_class = CAMEL_FOLDER_CLASS(camel_mbox_folder_class);
73 CamelLocalFolderClass *lclass = (CamelLocalFolderClass *)camel_mbox_folder_class;
75 parent_class = (CamelLocalFolderClass *)camel_type_get_global_classfuncs(camel_local_folder_get_type());
77 /* virtual method definition */
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;
86 lclass->create_summary = mbox_create_summary;
87 lclass->lock = mbox_lock;
88 lclass->unlock = mbox_unlock;
92 mbox_init(gpointer object, gpointer klass)
94 /*CamelFolder *folder = object;*/
95 CamelMboxFolder *mbox_folder = object;
97 mbox_folder->lockfd = -1;
101 mbox_finalize(CamelObject * object)
103 CamelMboxFolder *mbox_folder = (CamelMboxFolder *)object;
105 g_assert(mbox_folder->lockfd == -1);
108 CamelType camel_mbox_folder_get_type(void)
110 static CamelType camel_mbox_folder_type = CAMEL_INVALID_TYPE;
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,
118 (CamelObjectInitFunc) mbox_init,
119 (CamelObjectFinalizeFunc) mbox_finalize);
122 return camel_mbox_folder_type;
126 camel_mbox_folder_new(CamelStore *parent_store, const gchar *full_name, guint32 flags, CamelException *ex)
130 d(printf("Creating mbox folder: %s in %s\n", full_name, camel_local_store_get_toplevel_dir((CamelLocalStore *)parent_store)));
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);
139 static CamelLocalSummary *mbox_create_summary(CamelLocalFolder *lf, const gchar *path, const gchar *folder, CamelIndex *index)
141 return (CamelLocalSummary *)camel_mbox_summary_new((CamelFolder *)lf, path, folder, index);
144 static gint mbox_lock(CamelLocalFolder *lf, CamelLockType type, CamelException *ex)
147 CamelMboxFolder *mf = (CamelMboxFolder *)lf;
149 /* make sure we have matching unlocks for locks, camel-local-folder class should enforce this */
150 g_assert(mf->lockfd == -1);
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));
160 if (camel_lock_folder(lf->folder_path, mf->lockfd, type, ex) == -1) {
169 static void mbox_unlock(CamelLocalFolder *lf)
172 CamelMboxFolder *mf = (CamelMboxFolder *)lf;
174 g_assert(mf->lockfd != -1);
175 camel_unlock_folder(lf->folder_path, mf->lockfd);
182 mbox_append_message(CamelFolder *folder, CamelMimeMessage * message, const CamelMessageInfo * info, gchar **appended_uid, CamelException *ex)
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;
195 /* If we can't lock, dont do anything */
196 if (camel_local_folder_lock(lf, CAMEL_LOCK_WRITE, ex) == -1)
199 d(printf("Appending message\n"));
201 /* first, check the summary is correct (updates folder_size too) */
202 retval = camel_local_summary_check ((CamelLocalSummary *)folder->summary, lf->changes, ex);
206 /* add it to the summary/assign the uid, etc */
207 mi = camel_local_summary_add((CamelLocalSummary *)folder->summary, message, info, lf->changes, ex);
211 d(printf("Appending message: uid is %s\n", camel_message_info_uid(mi)));
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);
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));
224 /* and we need to set the frompos/XEV explicitly */
225 ((CamelMboxMessageInfo *)mi)->frompos = mbs->folder_size;
227 xev = camel_local_summary_encode_x_evolution((CamelLocalSummary *)folder->summary, mi);
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;
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)
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);
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)
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);
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);
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;
269 /* unlock as soon as we can */
270 camel_local_folder_unlock(lf);
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);
278 *appended_uid = g_strdup(camel_message_info_uid(mi));
284 camel_exception_set (ex, CAMEL_EXCEPTION_USER_CANCEL,
285 _("Mail append canceled"));
287 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
288 _("Cannot append message to mbox file: %s: %s"),
289 lf->folder_path, g_strerror (errno));
292 /* reset the file to original size */
294 retval = ftruncate (((CamelStreamFs *) output_stream)->fd, mbs->folder_size);
295 } while (retval == -1 && errno == EINTR);
297 camel_object_unref (output_stream);
301 camel_object_unref (filter_stream);
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));
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;
315 /* make sure we unlock the folder - before we start triggering events into appland */
316 camel_local_folder_unlock(lf);
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);
326 mbox_get_filename (CamelFolder *folder, const gchar *uid, CamelException *ex)
328 CamelLocalFolder *lf = (CamelLocalFolder *)folder;
329 CamelMboxMessageInfo *info;
331 gchar *filename = NULL;
333 d(printf("Getting message %s\n", uid));
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)
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);
345 /* get the message summary info */
346 info = (CamelMboxMessageInfo *) camel_folder_summary_uid(folder->summary, uid);
349 set_cannot_get_message_ex (ex, CAMEL_EXCEPTION_FOLDER_INVALID_UID,
350 uid, lf->folder_path, _("No such message"));
354 if (info->frompos == -1) {
355 camel_message_info_free((CamelMessageInfo *)info);
359 frompos = info->frompos;
360 camel_message_info_free((CamelMessageInfo *)info);
362 filename = g_strdup_printf ("%s%s!%" PRId64, lf->folder_path, G_DIR_SEPARATOR_S, (gint64) frompos);
365 /* and unlock now we're finished with it */
366 camel_local_folder_unlock(lf);
371 static CamelMimeMessage *
372 mbox_get_message(CamelFolder *folder, const gchar * uid, CamelException *ex)
374 CamelLocalFolder *lf = (CamelLocalFolder *)folder;
375 CamelMimeMessage *message = NULL;
376 CamelMboxMessageInfo *info;
377 CamelMimeParser *parser = NULL;
379 gint retried = FALSE;
382 d(printf("Getting message %s\n", uid));
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)
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);
395 /* get the message summary info */
396 info = (CamelMboxMessageInfo *) camel_folder_summary_uid(folder->summary, uid);
399 set_cannot_get_message_ex (ex, CAMEL_EXCEPTION_FOLDER_INVALID_UID,
400 uid, lf->folder_path, _("No such message"));
404 if (info->frompos == -1) {
405 camel_message_info_free((CamelMessageInfo *)info);
409 frompos = info->frompos;
410 camel_message_info_free((CamelMessageInfo *)info);
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. */
417 fd = g_open(lf->folder_path, O_LARGEFILE | O_RDONLY | O_BINARY, 0);
419 set_cannot_get_message_ex (ex, CAMEL_EXCEPTION_SYSTEM,
420 uid, lf->folder_path, g_strerror (errno));
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);
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) {
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));
438 camel_object_unref((CamelObject *)parser);
443 camel_local_summary_check_force((CamelLocalSummary *)folder->summary);
444 retval = camel_local_summary_check((CamelLocalSummary *)folder->summary, lf->changes, ex);
449 set_cannot_get_message_ex (ex, CAMEL_EXCEPTION_FOLDER_INVALID,
450 uid, lf->folder_path, _("The folder appears to be irrecoverably corrupted."));
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);
463 camel_medium_remove_header((CamelMedium *)message, "X-Evolution");
466 /* and unlock now we're finished with it */
467 camel_local_folder_unlock(lf);
470 camel_object_unref((CamelObject *)parser);
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);
482 mbox_cmp_uids (CamelFolder *folder, const gchar *uid1, const gchar *uid2)
484 CamelMboxMessageInfo *a, *b;
486 g_return_val_if_fail (folder != NULL, 0);
487 g_return_val_if_fail (folder->summary != NULL, 0);
489 a = (CamelMboxMessageInfo *) camel_folder_summary_uid (folder->summary, uid1);
490 b = (CamelMboxMessageInfo *) camel_folder_summary_uid (folder->summary, uid2);
492 g_return_val_if_fail (a != NULL, 0);
493 g_return_val_if_fail (b != NULL, 0);
495 return a->frompos < b->frompos ? -1 : a->frompos == b->frompos ? 0 : 1;
499 mbox_sort_uids (CamelFolder *folder, GPtrArray *uids)
501 g_return_if_fail (parent_class != NULL);
502 g_return_if_fail (folder != NULL);
504 if (uids && uids->len > 1) {
507 camel_exception_init (&ex);
509 camel_folder_summary_ensure_infos_loaded (folder->summary, uids->len, &ex);
511 if (camel_exception_is_set (&ex))
512 g_warning ("%s: %s", G_STRFUNC, camel_exception_get_description (&ex));
514 camel_exception_clear (&ex);
517 CAMEL_FOLDER_CLASS (parent_class)->sort_uids (folder, uids);