Fix use after free
[claws.git] / src / msgcache.c
index 493bd4170253d50dc6f876f9bcdd0ff86c519b35..82480ed9c10b7d02babd720098ac9633860599e9 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2007 Hiroyuki Yamamoto & The Claws Mail Team
+ * Copyright (C) 1999-2012 Hiroyuki Yamamoto & The Claws Mail Team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -19,6 +19,7 @@
 
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
+#include "claws-features.h"
 #endif
 
 #include "defs.h"
@@ -45,6 +46,7 @@
 #include "codeconv.h"
 #include "timing.h"
 #include "tags.h"
+#include "prefs_common.h"
 
 #ifdef HAVE_FWRITE_UNLOCKED
 #define SC_FWRITE fwrite_unlocked
@@ -70,7 +72,6 @@
         ((x[3]&0xff) << 24))
 
 static gboolean msgcache_use_mmap_read = TRUE;
-static gboolean msgcache_use_mmap_write = FALSE;
 
 #else
 #define bswap_32(x) (x)
@@ -88,7 +89,6 @@ static gboolean msgcache_use_mmap_write = FALSE;
         ((x[3]&0xff) << 24))
 
 static gboolean msgcache_use_mmap_read = TRUE;
-static gboolean msgcache_use_mmap_write = FALSE;
 #endif
 
 static gboolean swapping = TRUE;
@@ -146,7 +146,7 @@ static gboolean msgcache_msginfo_free_func(gpointer num, gpointer msginfo, gpoin
 
 void msgcache_destroy(MsgCache *cache)
 {
-       g_return_if_fail(cache != NULL);
+       cm_return_if_fail(cache != NULL);
 
        g_hash_table_foreach_remove(cache->msgnum_table, msgcache_msginfo_free_func, NULL);
        g_hash_table_destroy(cache->msgid_table);
@@ -158,8 +158,8 @@ void msgcache_add_msg(MsgCache *cache, MsgInfo *msginfo)
 {
        MsgInfo *newmsginfo;
 
-       g_return_if_fail(cache != NULL);
-       g_return_if_fail(msginfo != NULL);
+       cm_return_if_fail(cache != NULL);
+       cm_return_if_fail(msginfo != NULL);
 
        newmsginfo = procmsg_msginfo_new_ref(msginfo);
        g_hash_table_insert(cache->msgnum_table, &newmsginfo->msgnum, newmsginfo);
@@ -168,15 +168,16 @@ void msgcache_add_msg(MsgCache *cache, MsgInfo *msginfo)
        cache->memusage += procmsg_msginfo_memusage(msginfo);
        cache->last_access = time(NULL);
 
-       debug_print("Cache size: %d messages, %d bytes\n", g_hash_table_size(cache->msgnum_table), cache->memusage);
+       msginfo->folder->cache_dirty = TRUE;
+
+       debug_print("Cache size: %d messages, %u bytes\n", g_hash_table_size(cache->msgnum_table), cache->memusage);
 }
 
 void msgcache_remove_msg(MsgCache *cache, guint msgnum)
 {
        MsgInfo *msginfo;
 
-       g_return_if_fail(cache != NULL);
-       g_return_if_fail(msgnum > 0);
+       cm_return_if_fail(cache != NULL);
 
        msginfo = (MsgInfo *) g_hash_table_lookup(cache->msgnum_table, &msgnum);
        if(!msginfo)
@@ -186,18 +187,22 @@ void msgcache_remove_msg(MsgCache *cache, guint msgnum)
        if(msginfo->msgid)
                g_hash_table_remove(cache->msgid_table, msginfo->msgid);
        g_hash_table_remove(cache->msgnum_table, &msginfo->msgnum);
+
+       msginfo->folder->cache_dirty = TRUE;
+
        procmsg_msginfo_free(msginfo);
        cache->last_access = time(NULL);
 
-       debug_print("Cache size: %d messages, %d byte\n", g_hash_table_size(cache->msgnum_table), cache->memusage);
+
+       debug_print("Cache size: %d messages, %u bytes\n", g_hash_table_size(cache->msgnum_table), cache->memusage);
 }
 
 void msgcache_update_msg(MsgCache *cache, MsgInfo *msginfo)
 {
        MsgInfo *oldmsginfo, *newmsginfo;
        
-       g_return_if_fail(cache != NULL);
-       g_return_if_fail(msginfo != NULL);
+       cm_return_if_fail(cache != NULL);
+       cm_return_if_fail(msginfo != NULL);
 
        oldmsginfo = g_hash_table_lookup(cache->msgnum_table, &msginfo->msgnum);
        if(oldmsginfo && oldmsginfo->msgid) 
@@ -215,7 +220,9 @@ void msgcache_update_msg(MsgCache *cache, MsgInfo *msginfo)
        cache->memusage += procmsg_msginfo_memusage(newmsginfo);
        cache->last_access = time(NULL);
        
-       debug_print("Cache size: %d messages, %d byte\n", g_hash_table_size(cache->msgnum_table), cache->memusage);
+       debug_print("Cache size: %d messages, %u bytes\n", g_hash_table_size(cache->msgnum_table), cache->memusage);
+
+       msginfo->folder->cache_dirty = TRUE;
 
        return;
 }
@@ -224,7 +231,7 @@ MsgInfo *msgcache_get_msg(MsgCache *cache, guint num)
 {
        MsgInfo *msginfo;
 
-       g_return_val_if_fail(cache != NULL, NULL);
+       cm_return_val_if_fail(cache != NULL, NULL);
 
        msginfo = g_hash_table_lookup(cache->msgnum_table, &num);
        if(!msginfo)
@@ -238,8 +245,8 @@ MsgInfo *msgcache_get_msg_by_id(MsgCache *cache, const gchar *msgid)
 {
        MsgInfo *msginfo;
        
-       g_return_val_if_fail(cache != NULL, NULL);
-       g_return_val_if_fail(msgid != NULL, NULL);
+       cm_return_val_if_fail(cache != NULL, NULL);
+       cm_return_val_if_fail(msgid != NULL, NULL);
 
        msginfo = g_hash_table_lookup(cache->msgid_table, msgid);
        if(!msginfo)
@@ -261,7 +268,7 @@ MsgInfoList *msgcache_get_msg_list(MsgCache *cache)
 {
        MsgInfoList *msg_list = NULL;
        START_TIMING("");
-       g_return_val_if_fail(cache != NULL, NULL);
+       cm_return_val_if_fail(cache != NULL, NULL);
 
        g_hash_table_foreach((GHashTable *)cache->msgnum_table, msgcache_get_msg_list_func, (gpointer)&msg_list);       
        cache->last_access = time(NULL);
@@ -273,14 +280,14 @@ MsgInfoList *msgcache_get_msg_list(MsgCache *cache)
 
 time_t msgcache_get_last_access_time(MsgCache *cache)
 {
-       g_return_val_if_fail(cache != NULL, 0);
+       cm_return_val_if_fail(cache != NULL, 0);
        
        return cache->last_access;
 }
 
 gint msgcache_get_memory_usage(MsgCache *cache)
 {
-       g_return_val_if_fail(cache != NULL, 0);
+       cm_return_val_if_fail(cache != NULL, 0);
 
        return cache->memusage;
 }
@@ -305,7 +312,7 @@ gint msgcache_get_memory_usage(MsgCache *cache)
        size_t ni; \
  \
        if ((ni = fread(&idata, sizeof(idata), 1, fp)) != 1) { \
-               g_warning("read_int: Cache data corrupted, read %d of %d at " \
+               g_warning("read_int: Cache data corrupted, read %zd of %zd at " \
                          "offset %ld\n", ni, sizeof(idata), ftell(fp)); \
                procmsg_msginfo_free(msginfo); \
                error = TRUE; \
@@ -314,17 +321,27 @@ gint msgcache_get_memory_usage(MsgCache *cache)
                n = swapping ? bswap_32(idata) : (idata);\
 }
 
-#define GET_CACHE_DATA_INT(n) \
-{ \
-       n = (swapping ? (MMAP_TO_GUINT32_SWAPPED(walk_data)):(MMAP_TO_GUINT32(walk_data))); \
-       walk_data += 4; rem_len -= 4;                   \
+#define GET_CACHE_DATA_INT(n)                                                                  \
+{                                                                                              \
+       if (rem_len < 4) {                                                                      \
+               g_print("error at rem_len:%d\n", rem_len);                                      \
+               error = TRUE;                                                                   \
+               goto bail_err;                                                                  \
+       }                                                                                       \
+       n = (swapping ? (MMAP_TO_GUINT32_SWAPPED(walk_data)):(MMAP_TO_GUINT32(walk_data)));     \
+       walk_data += 4; rem_len -= 4;                                                           \
 }
 
 #define GET_CACHE_DATA(data, total_len) \
 { \
        GET_CACHE_DATA_INT(tmp_len);    \
+       if (rem_len < tmp_len) {                                                                \
+               g_print("error at rem_len:%d (tmp_len %d)\n", rem_len, tmp_len);                \
+               error = TRUE;                                                                   \
+               goto bail_err;                                                                  \
+       }                                                                                       \
        if ((tmp_len = msgcache_get_cache_data_str(walk_data, &data, tmp_len, conv)) < 0) { \
-               printf("error at rem_len:%d\n", rem_len);\
+               g_print("error at rem_len:%d\n", rem_len);\
                procmsg_msginfo_free(msginfo); \
                error = TRUE; \
                goto bail_err; \
@@ -391,11 +408,11 @@ static FILE *msgcache_open_data_file(const gchar *file, guint version,
        FILE *fp;
        gint32 data_ver;
 
-       g_return_val_if_fail(file != NULL, NULL);
+       cm_return_val_if_fail(file != NULL, NULL);
 
        if (mode == DATA_WRITE) {
                int w_err = 0, wrote = 0;
-               if ((fp = g_fopen(file, "w+")) == NULL) {
+               if ((fp = g_fopen(file, "wb")) == NULL) {
                        FILE_OP_ERROR(file, "fopen");
                        return NULL;
                }
@@ -456,16 +473,16 @@ static gint msgcache_read_cache_data_str(FILE *fp, gchar **str,
        if (!swapping) {
                if ((ni = fread(&len, sizeof(len), 1, fp) != 1) ||
                    len > G_MAXINT) {
-                       g_warning("read_data_str: Cache data (len) corrupted, read %d "
-                                 "of %d bytes at offset %ld\n", ni, sizeof(len), 
+                       g_warning("read_data_str: Cache data (len) corrupted, read %zd "
+                                 "of %zd bytes at offset %ld\n", ni, sizeof(len), 
                                  ftell(fp));
                        return -1;
                }
        } else {
                if ((ni = fread(&len, sizeof(len), 1, fp) != 1) ||
                    bswap_32(len) > G_MAXINT) {
-                       g_warning("read_data_str: Cache data (len) corrupted, read %d "
-                                 "of %d bytes at offset %ld\n", ni, sizeof(len), 
+                       g_warning("read_data_str: Cache data (len) corrupted, read %zd "
+                                 "of %zd bytes at offset %ld\n", ni, sizeof(len), 
                                  ftell(fp));
                        return -1;
                }
@@ -478,12 +495,11 @@ static gint msgcache_read_cache_data_str(FILE *fp, gchar **str,
        tmpstr = g_try_malloc(len + 1);
 
        if(tmpstr == NULL) {
-               g_warning("read_data_str: can't g_malloc %d bytes - cache data probably corrupted.\n", len);
                return -1;
        }
 
        if ((ni = fread(tmpstr, 1, len, fp)) != len) {
-               g_warning("read_data_str: Cache data corrupted, read %d of %d "
+               g_warning("read_data_str: Cache data corrupted, read %zd of %u "
                          "bytes at offset %ld\n", 
                          ni, len, ftell(fp));
                g_free(tmpstr);
@@ -518,12 +534,10 @@ static gint msgcache_get_cache_data_str(gchar *src, gchar **str, gint len,
        tmpstr = g_try_malloc(len + 1);
 
        if(tmpstr == NULL) {
-               g_warning("read_data_str: can't g_malloc %d bytes - cache data probably corrupted.\n", len);
                return -1;
        }
 
-       strncpy(tmpstr, src, len);
-
+       memcpy(tmpstr, src, len);
        tmpstr[len] = 0;
 
        if (conv != NULL) {
@@ -569,8 +583,8 @@ MsgCache *msgcache_read_cache(FolderItem *item, const gchar *cache_file)
        char *cache_data = NULL;
        struct stat st;
 
-       g_return_val_if_fail(cache_file != NULL, NULL);
-       g_return_val_if_fail(item != NULL, NULL);
+       cm_return_val_if_fail(cache_file != NULL, NULL);
+       cm_return_val_if_fail(item != NULL, NULL);
 
        swapping = TRUE;
 
@@ -596,8 +610,10 @@ MsgCache *msgcache_read_cache(FolderItem *item, const gchar *cache_file)
                tmp_flags |= MSG_DRAFT;
        }
 
-       if (msgcache_read_cache_data_str(fp, &srccharset, NULL) < 0)
+       if (msgcache_read_cache_data_str(fp, &srccharset, NULL) < 0) {
+               fclose(fp);
                return NULL;
+       }
        dstcharset = CS_UTF_8;
        if (srccharset == NULL || dstcharset == NULL) {
                conv = NULL;
@@ -638,6 +654,7 @@ MsgCache *msgcache_read_cache(FolderItem *item, const gchar *cache_file)
                        if (!hMapping)
                                goto w32_fail;
                        cache_data = (unsigned char *)MapViewOfFile(hMapping, FILE_MAP_COPY, 0, 0, 0);
+                       CloseHandle (hMapping);
                w32_fail:
                        ;
 #else
@@ -699,12 +716,6 @@ MsgCache *msgcache_read_cache(FolderItem *item, const gchar *cache_file)
                        if(msginfo->msgid)
                                g_hash_table_insert(cache->msgid_table, msginfo->msgid, msginfo);
                }
-
-#ifdef G_OS_WIN32
-               UnmapViewOfFile((void*) cache_data);
-#else
-               munmap(cache_data, map_len);
-#endif
        } else {
                while (fread(&num, sizeof(num), 1, fp) == 1) {
                        if (swapping)
@@ -757,8 +768,14 @@ MsgCache *msgcache_read_cache(FolderItem *item, const gchar *cache_file)
                }
        }
 bail_err:
+       if (cache_data != NULL && cache_data != MAP_FAILED) {
+#ifdef G_OS_WIN32
+               UnmapViewOfFile((void*) cache_data);
+#else
+               munmap(cache_data, map_len);
+#endif
+       }
        fclose(fp);
-
        if (conv != NULL) {
                if (conv->free != NULL)
                        conv->free(conv);
@@ -774,7 +791,8 @@ bail_err:
        cache->memusage = memusage;
 
        debug_print("done. (%d items read)\n", g_hash_table_size(cache->msgnum_table));
-       debug_print("Cache size: %d messages, %d byte\n", g_hash_table_size(cache->msgnum_table), cache->memusage);
+       debug_print("Cache size: %d messages, %u bytes\n", g_hash_table_size(cache->msgnum_table), cache->memusage);
+
        return cache;
 }
 
@@ -787,7 +805,8 @@ void msgcache_read_mark(MsgCache *cache, const gchar *mark_file)
        gint map_len = -1;
        char *cache_data = NULL;
        struct stat st;
-       
+       gboolean error = FALSE;
+
        swapping = TRUE;
 
        /* In case we can't open the mark file with MARK_VERSION, check if we can open it with the
@@ -820,6 +839,7 @@ void msgcache_read_mark(MsgCache *cache, const gchar *mark_file)
                        if (!hMapping)
                                goto w32_fail2;
                        cache_data = (unsigned char *)MapViewOfFile(hMapping, FILE_MAP_COPY, 0, 0, 0);
+                       CloseHandle (hMapping);
                w32_fail2:
                        ;
 #else
@@ -841,16 +861,14 @@ void msgcache_read_mark(MsgCache *cache, const gchar *mark_file)
                                msginfo->flags.perm_flags = perm_flags;
                        }
                }
-#ifdef G_OS_WIN32
-               UnmapViewOfFile((void*) cache_data);
-#else
-               munmap(cache_data, map_len);
-#endif
        } else {
                while (fread(&num, sizeof(num), 1, fp) == 1) {
                        if (swapping)
                                num = bswap_32(num);
-                       if (fread(&perm_flags, sizeof(perm_flags), 1, fp) != 1) break;
+                       if (fread(&perm_flags, sizeof(perm_flags), 1, fp) != 1) {
+                               error = TRUE;
+                               break;
+                       }
                        if (swapping)
                                perm_flags = bswap_32(perm_flags);
                        msginfo = g_hash_table_lookup(cache->msgnum_table, &num);
@@ -859,7 +877,18 @@ void msgcache_read_mark(MsgCache *cache, const gchar *mark_file)
                        }
                }       
        }
+bail_err:
+       if (cache_data != NULL && cache_data != MAP_FAILED) {
+#ifdef G_OS_WIN32
+               UnmapViewOfFile((void*) cache_data);
+#else
+               munmap(cache_data, map_len);
+#endif
+       }
        fclose(fp);
+       if (error) {
+               debug_print("error reading cache mark from %s\n", mark_file);
+       }
 }
 
 void msgcache_read_tags(MsgCache *cache, const gchar *tags_file)
@@ -870,7 +899,8 @@ void msgcache_read_tags(MsgCache *cache, const gchar *tags_file)
        gint map_len = -1;
        char *cache_data = NULL;
        struct stat st;
-       
+       gboolean error = FALSE;
+
        swapping = TRUE;
 
        /* In case we can't open the mark file with MARK_VERSION, check if we can open it with the
@@ -903,6 +933,7 @@ void msgcache_read_tags(MsgCache *cache, const gchar *tags_file)
                        if (!hMapping)
                                goto w32_fail6;
                        cache_data = (unsigned char *)MapViewOfFile(hMapping, FILE_MAP_COPY, 0, 0, 0);
+                       CloseHandle (hMapping);
                w32_fail6:
                        ;
 #else
@@ -934,11 +965,6 @@ void msgcache_read_tags(MsgCache *cache, const gchar *tags_file)
                                msginfo->tags = g_slist_reverse(msginfo->tags);
                        }
                }
-#ifdef G_OS_WIN32
-               UnmapViewOfFile((void*) cache_data);
-#else
-               munmap(cache_data, map_len);
-#endif
        } else {
                while (fread(&num, sizeof(num), 1, fp) == 1) {
                        gint id = -1;
@@ -963,7 +989,18 @@ void msgcache_read_tags(MsgCache *cache, const gchar *tags_file)
                        }
                }
        }
+bail_err:
+       if (cache_data != NULL && cache_data != MAP_FAILED) {
+#ifdef G_OS_WIN32
+               UnmapViewOfFile((void*) cache_data);
+#else
+               munmap(cache_data, map_len);
+#endif
+       }
        fclose(fp);
+       if (error) {
+               debug_print("error reading cache tags from %s\n", tags_file);
+       }
 }
 
 static int msgcache_write_cache(MsgInfo *msginfo, FILE *fp)
@@ -1000,39 +1037,6 @@ static int msgcache_write_cache(MsgInfo *msginfo, FILE *fp)
        return w_err ? -1 : wrote;
 }
 
-static int msgcache_write_mmap_cache(MsgInfo *msginfo, char *walk_data)
-{
-       MsgTmpFlags flags = msginfo->flags.tmp_flags & MSG_CACHED_FLAG_MASK;
-       GSList *cur;
-       int wrote = 0;
-
-       PUT_CACHE_DATA_INT(msginfo->msgnum);
-       PUT_CACHE_DATA_INT(msginfo->size);
-       PUT_CACHE_DATA_INT(msginfo->mtime);
-       PUT_CACHE_DATA_INT(msginfo->date_t);
-       PUT_CACHE_DATA_INT(flags);
-       PUT_CACHE_DATA(msginfo->fromname);
-
-       PUT_CACHE_DATA(msginfo->date);
-       PUT_CACHE_DATA(msginfo->from);
-       PUT_CACHE_DATA(msginfo->to);
-       PUT_CACHE_DATA(msginfo->cc);
-       PUT_CACHE_DATA(msginfo->newsgroups);
-       PUT_CACHE_DATA(msginfo->subject);
-       PUT_CACHE_DATA(msginfo->msgid);
-       PUT_CACHE_DATA(msginfo->inreplyto);
-       PUT_CACHE_DATA(msginfo->xref);
-       PUT_CACHE_DATA_INT(msginfo->planned_download);
-       PUT_CACHE_DATA_INT(msginfo->total_size);
-        
-       PUT_CACHE_DATA_INT(g_slist_length(msginfo->references));
-
-       for (cur = msginfo->references; cur != NULL; cur = cur->next) {
-               PUT_CACHE_DATA((gchar *)cur->data);
-       }
-       return wrote;
-}
-
 static int msgcache_write_flags(MsgInfo *msginfo, FILE *fp)
 {
        MsgPermFlags flags = msginfo->flags.perm_flags;
@@ -1059,40 +1063,11 @@ static int msgcache_write_tags(MsgInfo *msginfo, FILE *fp)
        return w_err ? -1 : wrote;
 }
 
-static int msgcache_write_mmap_flags(MsgInfo *msginfo, char *walk_data)
-{
-       MsgPermFlags flags = msginfo->flags.perm_flags;
-       int wrote = 0;
-
-       PUT_CACHE_DATA_INT(msginfo->msgnum);
-       PUT_CACHE_DATA_INT(flags);
-       return wrote;
-}
-
-static int msgcache_write_mmap_tags(MsgInfo *msginfo, char *walk_data)
-{
-       GSList *cur = msginfo->tags;
-       int wrote = 0;
-       
-       PUT_CACHE_DATA_INT(msginfo->msgnum);
-       for (; cur; cur = cur->next) {
-               gint id = GPOINTER_TO_INT(cur->data);
-               if (tags_get_tag(id) != NULL) {
-                       PUT_CACHE_DATA_INT(id);
-               }
-       }
-       PUT_CACHE_DATA_INT(-1);
-       return wrote;
-}
-
 struct write_fps
 {
        FILE *cache_fp;
        FILE *mark_fp;
        FILE *tags_fp;
-       char *cache_data;
-       char *mark_data;
-       char *tags_data;
        int error;
        guint cache_size;
        guint mark_size;
@@ -1108,41 +1083,27 @@ static void msgcache_write_func(gpointer key, gpointer value, gpointer user_data
        msginfo = (MsgInfo *)value;
        write_fps = user_data;
 
-       tmp = msgcache_write_cache(msginfo, write_fps->cache_fp);
-       if (tmp < 0)
-               write_fps->error = 1;
-       else
-               write_fps->cache_size += tmp;
+       if (write_fps->cache_fp) {
+               tmp = msgcache_write_cache(msginfo, write_fps->cache_fp);
+               if (tmp < 0)
+                       write_fps->error = 1;
+               else
+                       write_fps->cache_size += tmp;
+       }
+       if (write_fps->mark_fp) {
        tmp= msgcache_write_flags(msginfo, write_fps->mark_fp);
-       if (tmp < 0)
-               write_fps->error = 1;
-       else
-               write_fps->mark_size += tmp;
-       tmp = msgcache_write_tags(msginfo, write_fps->tags_fp);
-       if (tmp < 0)
-               write_fps->error = 1;
-       else
-               write_fps->tags_size += tmp;
-}
-
-static void msgcache_write_mmap_func(gpointer key, gpointer value, gpointer user_data)
-{
-       MsgInfo *msginfo;
-       struct write_fps *write_fps;
-       int tmp;
-
-       msginfo = (MsgInfo *)value;
-       write_fps = user_data;
-
-       tmp = msgcache_write_mmap_cache(msginfo, write_fps->cache_data);
-       write_fps->cache_size += tmp;
-       write_fps->cache_data += tmp;
-       tmp = msgcache_write_mmap_flags(msginfo, write_fps->mark_data);
-       write_fps->mark_size += tmp;
-       write_fps->mark_data += tmp;
-       tmp = msgcache_write_mmap_tags(msginfo, write_fps->tags_data);
-       write_fps->tags_size += tmp;
-       write_fps->tags_data += tmp;
+               if (tmp < 0)
+                       write_fps->error = 1;
+               else
+                       write_fps->mark_size += tmp;
+               }
+       if (write_fps->tags_fp) {
+               tmp = msgcache_write_tags(msginfo, write_fps->tags_fp);
+               if (tmp < 0)
+                       write_fps->error = 1;
+               else
+                       write_fps->tags_size += tmp;
+       }
 }
 
 gint msgcache_write(const gchar *cache_file, const gchar *mark_file, const gchar *tags_file, MsgCache *cache)
@@ -1150,16 +1111,9 @@ gint msgcache_write(const gchar *cache_file, const gchar *mark_file, const gchar
        struct write_fps write_fps;
        gchar *new_cache, *new_mark, *new_tags;
        int w_err = 0, wrote = 0;
-       gint map_len = -1;
-       char *cache_data = NULL;
-       char *mark_data = NULL;
-       char *tags_data = NULL;
 
        START_TIMING("");
-       g_return_val_if_fail(cache_file != NULL, -1);
-       g_return_val_if_fail(mark_file != NULL, -1);
-       g_return_val_if_fail(tags_file != NULL, -1);
-       g_return_val_if_fail(cache != NULL, -1);
+       cm_return_val_if_fail(cache != NULL, -1);
 
        new_cache = g_strconcat(cache_file, ".new", NULL);
        new_mark  = g_strconcat(mark_file, ".new", NULL);
@@ -1170,198 +1124,143 @@ gint msgcache_write(const gchar *cache_file, const gchar *mark_file, const gchar
        write_fps.mark_size = 0;
        write_fps.tags_size = 0;
 
-       write_fps.cache_fp = msgcache_open_data_file(new_cache, CACHE_VERSION,
-               DATA_WRITE, NULL, 0);
-       if (write_fps.cache_fp == NULL) {
-               g_free(new_cache);
-               g_free(new_mark);
-               return -1;
-       }
+       /* open files and write headers */
 
-       WRITE_CACHE_DATA(CS_UTF_8, write_fps.cache_fp);
+       if (cache_file) {
+               write_fps.cache_fp = msgcache_open_data_file(new_cache, CACHE_VERSION,
+                       DATA_WRITE, NULL, 0);
+               if (write_fps.cache_fp == NULL) {
+                       g_free(new_cache);
+                       g_free(new_mark);
+                       g_free(new_tags);
+                       return -1;
+               }
+               WRITE_CACHE_DATA(CS_UTF_8, write_fps.cache_fp);
+       } else {
+               write_fps.cache_fp = NULL;
+       }
 
        if (w_err != 0) {
                g_warning("failed to write charset\n");
-               fclose(write_fps.cache_fp);
-               g_unlink(new_cache);
+               if (write_fps.cache_fp)
+                       fclose(write_fps.cache_fp);
+               claws_unlink(new_cache);
                g_free(new_cache);
                g_free(new_mark);
                g_free(new_tags);
                return -1;
        }
 
-       write_fps.mark_fp = msgcache_open_data_file(new_mark, MARK_VERSION,
-               DATA_WRITE, NULL, 0);
-       if (write_fps.mark_fp == NULL) {
-               fclose(write_fps.cache_fp);
-               g_unlink(new_cache);
-               g_free(new_cache);
-               g_free(new_mark);
-               g_free(new_tags);
-               return -1;
+       if (mark_file) {
+               write_fps.mark_fp = msgcache_open_data_file(new_mark, MARK_VERSION,
+                       DATA_WRITE, NULL, 0);
+               if (write_fps.mark_fp == NULL) {
+                       if (write_fps.cache_fp)
+                               fclose(write_fps.cache_fp);
+                       claws_unlink(new_cache);
+                       g_free(new_cache);
+                       g_free(new_mark);
+                       g_free(new_tags);
+                       return -1;
+               }
+       } else {
+               write_fps.mark_fp = NULL;
        }
 
-       write_fps.tags_fp = msgcache_open_data_file(new_tags, TAGS_VERSION,
-               DATA_WRITE, NULL, 0);
-       if (write_fps.tags_fp == NULL) {
-               fclose(write_fps.cache_fp);
-               fclose(write_fps.mark_fp);
-               g_unlink(new_cache);
-               g_unlink(new_mark);
-               g_free(new_cache);
-               g_free(new_mark);
-               g_free(new_tags);
-               return -1;
+       if (tags_file) {
+               write_fps.tags_fp = msgcache_open_data_file(new_tags, TAGS_VERSION,
+                       DATA_WRITE, NULL, 0);
+               if (write_fps.tags_fp == NULL) {
+                       if (write_fps.cache_fp)
+                               fclose(write_fps.cache_fp);
+                       if (write_fps.mark_fp)
+                               fclose(write_fps.mark_fp);
+                       claws_unlink(new_cache);
+                       claws_unlink(new_mark);
+                       g_free(new_cache);
+                       g_free(new_mark);
+                       g_free(new_tags);
+                       return -1;
+               }
+       } else {
+               write_fps.tags_fp = NULL;
        }
 
        debug_print("\tWriting message cache to %s and %s...\n", new_cache, new_mark);
 
-       if (change_file_mode_rw(write_fps.cache_fp, new_cache) < 0)
+       if (write_fps.cache_fp && change_file_mode_rw(write_fps.cache_fp, new_cache) < 0)
                FILE_OP_ERROR(new_cache, "chmod");
 
-       write_fps.cache_size = ftell(write_fps.cache_fp);
-       write_fps.mark_size = ftell(write_fps.mark_fp);
-       write_fps.tags_size = ftell(write_fps.tags_fp);
+       /* headers written, note file size */
+       if (write_fps.cache_fp)
+               write_fps.cache_size = ftell(write_fps.cache_fp);
+       if (write_fps.mark_fp)
+               write_fps.mark_size = ftell(write_fps.mark_fp);
+       if (write_fps.tags_fp)
+               write_fps.tags_size = ftell(write_fps.tags_fp);
 
-       if (msgcache_use_mmap_write && cache->memusage > 0) {
-               map_len = cache->memusage;
-               if (ftruncate(fileno(write_fps.cache_fp), (off_t)map_len) == 0) {
-
-#ifdef G_OS_WIN32
-                       cache_data = NULL;
-                       HANDLE hFile, hMapping;
-                       hFile = (HANDLE) _get_osfhandle (fileno(write_fps.cache_fp));
-                       if (hFile == (HANDLE) -1)
-                               goto w32_fail3;
-                       hMapping = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, NULL);
-                       if (!hMapping)
-                               goto w32_fail3;
-                       cache_data = (unsigned char *)MapViewOfFile(hMapping, FILE_MAP_COPY, 0, 0, 0);
-               w32_fail3:
-                       ;
-#else
-                       cache_data = mmap(NULL, map_len, PROT_WRITE, MAP_SHARED, 
-                               fileno(write_fps.cache_fp), 0);
-#endif
-               }
-               if (cache_data != NULL && cache_data != MAP_FAILED) {
-                       if (ftruncate(fileno(write_fps.mark_fp), (off_t)map_len) == 0) {
-#ifdef G_OS_WIN32
-                               mark_data = NULL;
-                               HANDLE hFile, hMapping;
-                               hFile = (HANDLE) _get_osfhandle (fileno(write_fps.mark_fp));
-                               if (hFile == (HANDLE) -1)
-                                       goto w32_fail4;
-                               hMapping = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, NULL);
-                               if (!hMapping)
-                                       goto w32_fail4;
-                               mark_data = (unsigned char *)MapViewOfFile(hMapping, FILE_MAP_COPY, 0, 0, 0);
-                       w32_fail4:
-                               ;
-#else
-                               mark_data = mmap(NULL, map_len, PROT_WRITE, MAP_SHARED, 
-                                       fileno(write_fps.mark_fp), 0);
-#endif
-                       } 
-                       if (mark_data == NULL || mark_data == MAP_FAILED) {
-#ifdef G_OS_WIN32
-                               UnmapViewOfFile((void*) cache_data);
-#else
-                               munmap(cache_data, map_len);
-#endif
-                               cache_data = NULL;
-                       } else {
-                               if (ftruncate(fileno(write_fps.tags_fp), (off_t)map_len) == 0) {
-#ifdef G_OS_WIN32
-                                               tags_data = NULL;
-                                               HANDLE hFile, hMapping;
-                                               hFile = (HANDLE) _get_osfhandle (fileno(write_fps.tags_fp));
-                                               if (hFile == (HANDLE) -1)
-                                                       goto w32_fail5;
-                                               hMapping = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, NULL);
-                                               if (!hMapping)
-                                                       goto w32_fail5;
-                                               tags_data = (unsigned char *)MapViewOfFile(hMapping, FILE_MAP_COPY, 0, 0, 0);
-                                       w32_fail5:
-                                               ;
-#else
-                                       tags_data = mmap(NULL, map_len, PROT_WRITE, MAP_SHARED, 
-                                               fileno(write_fps.tags_fp), 0);
-#endif
-                               } 
-                               if (tags_data == NULL || tags_data == MAP_FAILED) {
-#ifdef G_OS_WIN32
-                                       UnmapViewOfFile((void*) cache_data);
-                                       UnmapViewOfFile((void*) mark_data);
-#else
-                                       munmap(cache_data, map_len);
-                                       munmap(mark_data, map_len);
-#endif
-                                       cache_data = NULL;
-                                       mark_data = NULL;
-                               } 
-                       }
-               }
-       }
-
-       if (cache_data != NULL && cache_data != MAP_FAILED) {
-               write_fps.cache_data = cache_data + ftell(write_fps.cache_fp);
-               write_fps.mark_data = mark_data + ftell(write_fps.mark_fp);
-               write_fps.tags_data = mark_data + ftell(write_fps.tags_fp);
-               g_hash_table_foreach(cache->msgnum_table, msgcache_write_mmap_func, (gpointer)&write_fps);
-#ifdef G_OS_WIN32
-               UnmapViewOfFile((void*) cache_data);
-               UnmapViewOfFile((void*) mark_data);
-               UnmapViewOfFile((void*) tags_data);
-#else
-               munmap(cache_data, map_len);
-               munmap(mark_data, map_len);
-               munmap(tags_data, map_len);
-#endif
-               ftruncate(fileno(write_fps.cache_fp), write_fps.cache_size);
-               ftruncate(fileno(write_fps.mark_fp), write_fps.mark_size);
-               ftruncate(fileno(write_fps.tags_fp), write_fps.tags_size);
-       } else {
 #ifdef HAVE_FWRITE_UNLOCKED
+       /* lock files for write once (instead of once per fwrite) */
+       if (write_fps.cache_fp)
                flockfile(write_fps.cache_fp);
+       if (write_fps.mark_fp)
                flockfile(write_fps.mark_fp);
+       if (write_fps.tags_fp)
                flockfile(write_fps.tags_fp);
 #endif
-               g_hash_table_foreach(cache->msgnum_table, msgcache_write_func, (gpointer)&write_fps);
+       /* write data to the files */
+       g_hash_table_foreach(cache->msgnum_table, msgcache_write_func, (gpointer)&write_fps);
 #ifdef HAVE_FWRITE_UNLOCKED
-               funlockfile(write_fps.mark_fp);
+       /* unlock files */
+       if (write_fps.cache_fp)
                funlockfile(write_fps.cache_fp);
+       if (write_fps.mark_fp)
+               funlockfile(write_fps.mark_fp);
+       if (write_fps.tags_fp)
                funlockfile(write_fps.tags_fp);
 #endif
-       }
-       
-       fflush(write_fps.cache_fp);
-       fflush(write_fps.mark_fp);
-       fflush(write_fps.tags_fp);
-
-#if 0
-       fsync(fileno(write_fps.cache_fp));
-       fsync(fileno(write_fps.mark_fp));
-       fsync(fileno(write_fps.tags_fp));
-#endif
-
-       fclose(write_fps.cache_fp);
-       fclose(write_fps.mark_fp);
-       fclose(write_fps.tags_fp);
+       /* flush buffers */
+       if (write_fps.cache_fp)
+               write_fps.error |= (fflush(write_fps.cache_fp) != 0);
+       if (write_fps.mark_fp)
+               write_fps.error |= (fflush(write_fps.mark_fp) != 0);
+       if (write_fps.tags_fp)
+               write_fps.error |= (fflush(write_fps.tags_fp) != 0);
+
+       /* sync to filesystem */
+       if (prefs_common.flush_metadata && write_fps.cache_fp)
+               write_fps.error |= (fsync(fileno(write_fps.cache_fp)) != 0);
+       if (prefs_common.flush_metadata && write_fps.mark_fp)
+               write_fps.error |= (fsync(fileno(write_fps.mark_fp)) != 0);
+       if (prefs_common.flush_metadata && write_fps.tags_fp)
+               write_fps.error |= (fsync(fileno(write_fps.tags_fp)) != 0);
+
+       /* close files */
+       if (write_fps.cache_fp)
+               write_fps.error |= (fclose(write_fps.cache_fp) != 0);
+       if (write_fps.mark_fp)
+               write_fps.error |= (fclose(write_fps.mark_fp) != 0);
+       if (write_fps.tags_fp)
+               write_fps.error |= (fclose(write_fps.tags_fp) != 0);
 
 
        if (write_fps.error != 0) {
-               g_unlink(new_cache);
-               g_unlink(new_mark);
-               g_unlink(new_tags);
+               /* in case of error, forget all */
+               claws_unlink(new_cache);
+               claws_unlink(new_mark);
+               claws_unlink(new_tags);
                g_free(new_cache);
                g_free(new_mark);
                g_free(new_tags);
                return -1;
        } else {
-               move_file(new_cache, cache_file, TRUE);
-               move_file(new_mark, mark_file, TRUE);
-               move_file(new_tags, tags_file, TRUE);
+               /* switch files */
+               if (cache_file)
+                       move_file(new_cache, cache_file, TRUE);
+               if (mark_file)
+                       move_file(new_mark, mark_file, TRUE);
+               if (tags_file)
+                       move_file(new_tags, tags_file, TRUE);
                cache->last_access = time(NULL);
        }