2007-01-05 [paul] 2.6.1cvs110
[claws.git] / src / msgcache.c
index ae8f4f97af80872a79281ea538b58230f0c68328..24556d03d561e012cd23f367f0d27bb72d3a1f07 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2006 Hiroyuki Yamamoto & The Sylpheed Claws Team
+ * Copyright (C) 1999-2007 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
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#endif
+
 #include "defs.h"
 
+#define _GNU_SOURCE
+#include <stdio.h>
+
 #include <glib.h>
 #include <glib/gi18n.h>
 #include <sys/mman.h>
 #include "codeconv.h"
 #include "timing.h"
 
+#ifdef HAVE_FWRITE_UNLOCKED
+#define SC_FWRITE fwrite_unlocked
+#else
+#define SC_FWRITE fwrite
+#endif
+
 #if G_BYTE_ORDER == G_BIG_ENDIAN
 #define bswap_32(x) \
      ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) | \
@@ -320,7 +333,7 @@ gint msgcache_get_memory_usage(MsgCache *cache)
        guint32 idata;                                  \
                                                        \
        idata = (guint32)bswap_32(n);                   \
-       if (fwrite(&idata, sizeof(idata), 1, fp) != 1)  \
+       if (SC_FWRITE(&idata, sizeof(idata), 1, fp) != 1)       \
                w_err = 1;                              \
        wrote += 4;                                     \
 }
@@ -344,7 +357,7 @@ gint msgcache_get_memory_usage(MsgCache *cache)
                len = strlen(data);                     \
        WRITE_CACHE_DATA_INT(len, fp);                  \
        if (w_err == 0 && len > 0) {                    \
-               if (fwrite(data, 1, len, fp) != len)    \
+               if (SC_FWRITE(data, 1, len, fp) != len) \
                        w_err = 1;                      \
                wrote += len;                           \
        } \
@@ -491,6 +504,11 @@ static gint msgcache_get_cache_data_str(gchar *src, gchar **str, gint len,
        if (len == 0)
                return 0;
 
+       if(len > 2*1024*1024) {
+               g_warning("read_data_str: refusing to allocate %d bytes.\n", len);
+               return -1;
+       }
+
        tmpstr = g_try_malloc(len + 1);
 
        if(tmpstr == NULL) {
@@ -1019,11 +1037,28 @@ gint msgcache_write(const gchar *cache_file, const gchar *mark_file, MsgCache *c
                g_hash_table_foreach(cache->msgnum_table, msgcache_write_mmap_func, (gpointer)&write_fps);
                munmap(cache_data, map_len);
                munmap(mark_data, map_len);
+               ftruncate(fileno(write_fps.cache_fp), write_fps.cache_size);
+               ftruncate(fileno(write_fps.mark_fp), write_fps.mark_size);
        } else {
+#ifdef HAVE_FWRITE_UNLOCKED
+               flockfile(write_fps.cache_fp);
+               flockfile(write_fps.mark_fp);
+#endif
                g_hash_table_foreach(cache->msgnum_table, msgcache_write_func, (gpointer)&write_fps);
+#ifdef HAVE_FWRITE_UNLOCKED
+               funlockfile(write_fps.mark_fp);
+               funlockfile(write_fps.cache_fp);
+#endif
        }
-       ftruncate(fileno(write_fps.cache_fp), write_fps.cache_size);
-       ftruncate(fileno(write_fps.mark_fp), write_fps.mark_size);
+       
+       fflush(write_fps.cache_fp);
+       fflush(write_fps.mark_fp);
+
+#if 0
+       fsync(fileno(write_fps.cache_fp));
+       fsync(fileno(write_fps.mark_fp));
+#endif
+
        fclose(write_fps.cache_fp);
        fclose(write_fps.mark_fp);