2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto & The Sylpheed Claws Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include <glib/gi18n.h>
25 #include <sys/types.h>
36 #if G_BYTE_ORDER == G_BIG_ENDIAN
38 ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
39 (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
41 #define MMAP_TO_GUINT32(x) \
43 ((x[2]&0xff) << 8) | \
44 ((x[1]&0xff) << 16) | \
47 #define MMAP_TO_GUINT32_SWAPPED(x) \
49 ((x[1]&0xff) << 8) | \
50 ((x[2]&0xff) << 16) | \
53 static gboolean msgcache_use_mmap = FALSE;
56 #define bswap_32(x) (x)
58 #define MMAP_TO_GUINT32(x) \
60 ((x[1]&0xff) << 8) | \
61 ((x[2]&0xff) << 16) | \
64 #define MMAP_TO_GUINT32_SWAPPED(x) \
66 ((x[1]&0xff) << 8) | \
67 ((x[2]&0xff) << 16) | \
70 static gboolean msgcache_use_mmap = FALSE;
73 static gboolean swapping = TRUE;
83 GHashTable *msgnum_table;
84 GHashTable *msgid_table;
89 typedef struct _StringConverter StringConverter;
90 struct _StringConverter {
91 gchar *(*convert) (StringConverter *converter, gchar *srcstr);
92 void (*free) (StringConverter *converter);
95 typedef struct _StrdupConverter StrdupConverter;
96 struct _StrdupConverter {
97 StringConverter converter;
100 typedef struct _CharsetConverter CharsetConverter;
101 struct _CharsetConverter {
102 StringConverter converter;
108 MsgCache *msgcache_new(void)
112 cache = g_new0(MsgCache, 1),
113 cache->msgnum_table = g_hash_table_new(g_int_hash, g_int_equal);
114 cache->msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
115 cache->last_access = time(NULL);
120 static gboolean msgcache_msginfo_free_func(gpointer num, gpointer msginfo, gpointer user_data)
122 procmsg_msginfo_free((MsgInfo *)msginfo);
126 void msgcache_destroy(MsgCache *cache)
128 g_return_if_fail(cache != NULL);
130 g_hash_table_foreach_remove(cache->msgnum_table, msgcache_msginfo_free_func, NULL);
131 g_hash_table_destroy(cache->msgid_table);
132 g_hash_table_destroy(cache->msgnum_table);
136 void msgcache_add_msg(MsgCache *cache, MsgInfo *msginfo)
140 g_return_if_fail(cache != NULL);
141 g_return_if_fail(msginfo != NULL);
143 newmsginfo = procmsg_msginfo_new_ref(msginfo);
144 g_hash_table_insert(cache->msgnum_table, &newmsginfo->msgnum, newmsginfo);
145 if(newmsginfo->msgid != NULL)
146 g_hash_table_insert(cache->msgid_table, newmsginfo->msgid, newmsginfo);
147 cache->memusage += procmsg_msginfo_memusage(msginfo);
148 cache->last_access = time(NULL);
150 debug_print("Cache size: %d messages, %d bytes\n", g_hash_table_size(cache->msgnum_table), cache->memusage);
153 void msgcache_remove_msg(MsgCache *cache, guint msgnum)
157 g_return_if_fail(cache != NULL);
158 g_return_if_fail(msgnum > 0);
160 msginfo = (MsgInfo *) g_hash_table_lookup(cache->msgnum_table, &msgnum);
164 cache->memusage -= procmsg_msginfo_memusage(msginfo);
166 g_hash_table_remove(cache->msgid_table, msginfo->msgid);
167 g_hash_table_remove(cache->msgnum_table, &msginfo->msgnum);
168 procmsg_msginfo_free(msginfo);
169 cache->last_access = time(NULL);
171 debug_print("Cache size: %d messages, %d byte\n", g_hash_table_size(cache->msgnum_table), cache->memusage);
174 void msgcache_update_msg(MsgCache *cache, MsgInfo *msginfo)
176 MsgInfo *oldmsginfo, *newmsginfo;
178 g_return_if_fail(cache != NULL);
179 g_return_if_fail(msginfo != NULL);
181 oldmsginfo = g_hash_table_lookup(cache->msgnum_table, &msginfo->msgnum);
182 if(oldmsginfo && oldmsginfo->msgid)
183 g_hash_table_remove(cache->msgid_table, oldmsginfo->msgid);
185 g_hash_table_remove(cache->msgnum_table, &oldmsginfo->msgnum);
186 cache->memusage -= procmsg_msginfo_memusage(oldmsginfo);
187 procmsg_msginfo_free(oldmsginfo);
190 newmsginfo = procmsg_msginfo_new_ref(msginfo);
191 g_hash_table_insert(cache->msgnum_table, &newmsginfo->msgnum, newmsginfo);
192 if(newmsginfo->msgid)
193 g_hash_table_insert(cache->msgid_table, newmsginfo->msgid, newmsginfo);
194 cache->memusage += procmsg_msginfo_memusage(newmsginfo);
195 cache->last_access = time(NULL);
197 debug_print("Cache size: %d messages, %d byte\n", g_hash_table_size(cache->msgnum_table), cache->memusage);
202 MsgInfo *msgcache_get_msg(MsgCache *cache, guint num)
206 g_return_val_if_fail(cache != NULL, NULL);
208 msginfo = g_hash_table_lookup(cache->msgnum_table, &num);
211 cache->last_access = time(NULL);
213 return procmsg_msginfo_new_ref(msginfo);
216 MsgInfo *msgcache_get_msg_by_id(MsgCache *cache, const gchar *msgid)
220 g_return_val_if_fail(cache != NULL, NULL);
221 g_return_val_if_fail(msgid != NULL, NULL);
223 msginfo = g_hash_table_lookup(cache->msgid_table, msgid);
226 cache->last_access = time(NULL);
228 return procmsg_msginfo_new_ref(msginfo);
231 static void msgcache_get_msg_list_func(gpointer key, gpointer value, gpointer user_data)
233 MsgInfoList **listptr = user_data;
234 MsgInfo *msginfo = value;
236 *listptr = g_slist_prepend(*listptr, procmsg_msginfo_new_ref(msginfo));
239 MsgInfoList *msgcache_get_msg_list(MsgCache *cache)
241 MsgInfoList *msg_list = NULL;
242 START_TIMING("msgcache_get_msg_list");
243 g_return_val_if_fail(cache != NULL, NULL);
245 g_hash_table_foreach((GHashTable *)cache->msgnum_table, msgcache_get_msg_list_func, (gpointer)&msg_list);
246 cache->last_access = time(NULL);
248 msg_list = g_slist_reverse(msg_list);
253 time_t msgcache_get_last_access_time(MsgCache *cache)
255 g_return_val_if_fail(cache != NULL, 0);
257 return cache->last_access;
260 gint msgcache_get_memory_usage(MsgCache *cache)
262 g_return_val_if_fail(cache != NULL, 0);
264 return cache->memusage;
268 * Cache saving functions
271 #define READ_CACHE_DATA(data, fp, total_len) \
273 if ((tmp_len = msgcache_read_cache_data_str(fp, &data, conv)) < 0) { \
274 procmsg_msginfo_free(msginfo); \
278 total_len += tmp_len; \
281 #define READ_CACHE_DATA_INT(n, fp) \
286 if ((ni = fread(&idata, sizeof(idata), 1, fp)) != 1) { \
287 g_warning("read_int: Cache data corrupted, read %d of %d at " \
288 "offset %ld\n", ni, sizeof(idata), ftell(fp)); \
289 procmsg_msginfo_free(msginfo); \
293 n = swapping ? bswap_32(idata) : (idata);\
296 #define GET_CACHE_DATA_INT(n) \
298 n = (swapping ? (MMAP_TO_GUINT32_SWAPPED(walk_data)):(MMAP_TO_GUINT32(walk_data))); \
299 walk_data += 4; rem_len -= 4; \
302 #define GET_CACHE_DATA(data, total_len) \
304 GET_CACHE_DATA_INT(tmp_len); \
305 if ((tmp_len = msgcache_get_cache_data_str(walk_data, &data, tmp_len, conv)) < 0) { \
306 printf("error at rem_len:%d\n", rem_len);\
307 procmsg_msginfo_free(msginfo); \
311 total_len += tmp_len; \
312 walk_data += tmp_len; rem_len -= tmp_len; \
316 #define WRITE_CACHE_DATA_INT(n, fp) \
320 idata = (guint32)bswap_32(n); \
321 if (fwrite(&idata, sizeof(idata), 1, fp) != 1) \
326 #define PUT_CACHE_DATA_INT(n) \
328 walk_data[0]=(((guint32)n)&0x000000ff); \
329 walk_data[1]=(((guint32)n)&0x0000ff00)>>8; \
330 walk_data[2]=(((guint32)n)&0x00ff0000)>>16; \
331 walk_data[3]=(((guint32)n)&0xff000000)>>24; \
336 #define WRITE_CACHE_DATA(data, fp) \
342 len = strlen(data); \
343 WRITE_CACHE_DATA_INT(len, fp); \
344 if (w_err == 0 && len > 0) { \
345 if (fwrite(data, 1, len, fp) != len) \
351 #define PUT_CACHE_DATA(data) \
357 len = strlen(data); \
358 PUT_CACHE_DATA_INT(len); \
360 memcpy(walk_data, data, len); \
366 static FILE *msgcache_open_data_file(const gchar *file, guint version,
368 gchar *buf, size_t buf_size)
373 g_return_val_if_fail(file != NULL, NULL);
375 if (mode == DATA_WRITE) {
376 int w_err = 0, wrote = 0;
377 if ((fp = g_fopen(file, "w+")) == NULL) {
378 FILE_OP_ERROR(file, "fopen");
381 if (change_file_mode_rw(fp, file) < 0)
382 FILE_OP_ERROR(file, "chmod");
384 WRITE_CACHE_DATA_INT(version, fp);
386 g_warning("failed to write int\n");
394 if ((fp = g_fopen(file, "rb")) == NULL)
395 debug_print("Mark/Cache file '%s' not found\n", file);
397 if (buf && buf_size > 0)
398 setvbuf(fp, buf, _IOFBF, buf_size);
399 if (fread(&data_ver, sizeof(data_ver), 1, fp) != 1 ||
400 version != bswap_32(data_ver)) {
401 g_message("%s: Mark/Cache version is different (%u != %u).\n",
402 file, bswap_32(data_ver), version);
406 data_ver = bswap_32(data_ver);
409 if (mode == DATA_READ)
413 /* reopen with append mode */
415 if ((fp = g_fopen(file, "ab")) == NULL)
416 FILE_OP_ERROR(file, "fopen");
418 /* open with overwrite mode if mark file doesn't exist or
419 version is different */
420 fp = msgcache_open_data_file(file, version, DATA_WRITE, buf,
427 static gint msgcache_read_cache_data_str(FILE *fp, gchar **str,
428 StringConverter *conv)
430 gchar *tmpstr = NULL;
436 if ((ni = fread(&len, sizeof(len), 1, fp) != 1) ||
438 g_warning("read_data_str: Cache data (len) corrupted, read %d "
439 "of %d bytes at offset %ld\n", ni, sizeof(len),
444 if ((ni = fread(&len, sizeof(len), 1, fp) != 1) ||
445 bswap_32(len) > G_MAXINT) {
446 g_warning("read_data_str: Cache data (len) corrupted, read %d "
447 "of %d bytes at offset %ld\n", ni, sizeof(len),
457 tmpstr = g_try_malloc(len + 1);
460 g_warning("read_data_str: can't g_malloc %d bytes - cache data probably corrupted.\n", len);
464 if ((ni = fread(tmpstr, 1, len, fp)) != len) {
465 g_warning("read_data_str: Cache data corrupted, read %d of %d "
466 "bytes at offset %ld\n",
474 *str = conv->convert(conv, tmpstr);
482 static gint msgcache_get_cache_data_str(gchar *src, gchar **str, gint len,
483 StringConverter *conv)
485 gchar *tmpstr = NULL;
492 tmpstr = g_try_malloc(len + 1);
495 g_warning("read_data_str: can't g_malloc %d bytes - cache data probably corrupted.\n", len);
499 strncpy(tmpstr, src, len);
504 *str = conv->convert(conv, tmpstr);
512 gchar *strconv_strdup_convert(StringConverter *conv, gchar *srcstr)
514 return g_strdup(srcstr);
517 gchar *strconv_charset_convert(StringConverter *conv, gchar *srcstr)
519 CharsetConverter *charsetconv = (CharsetConverter *) conv;
521 return conv_codeset_strdup(srcstr, charsetconv->srccharset, charsetconv->dstcharset);
524 void strconv_charset_free(StringConverter *conv)
526 CharsetConverter *charsetconv = (CharsetConverter *) conv;
528 g_free(charsetconv->srccharset);
529 g_free(charsetconv->dstcharset);
532 MsgCache *msgcache_read_cache(FolderItem *item, const gchar *cache_file)
537 MsgTmpFlags tmp_flags = 0;
538 gchar file_buf[BUFFSIZE];
541 gboolean error = FALSE;
542 StringConverter *conv = NULL;
543 gchar *srccharset = NULL;
544 const gchar *dstcharset = NULL;
547 gint tmp_len = 0, map_len = -1;
548 char *cache_data = NULL;
551 g_return_val_if_fail(cache_file != NULL, NULL);
552 g_return_val_if_fail(item != NULL, NULL);
556 /* In case we can't open the mark file with MARK_VERSION, check if we can open it with the
557 * swapped MARK_VERSION. As msgcache_open_data_file swaps it too, if this succeeds,
558 * it means it's the old version (not little-endian) on a big-endian machine. The code has
559 * no effect on x86 as their file doesn't change. */
561 if ((fp = msgcache_open_data_file
562 (cache_file, CACHE_VERSION, DATA_READ, file_buf, sizeof(file_buf))) == NULL) {
563 if ((fp = msgcache_open_data_file
564 (cache_file, bswap_32(CACHE_VERSION), DATA_READ, file_buf, sizeof(file_buf))) == NULL)
570 debug_print("\tReading %sswapped message cache from %s...\n", swapping?"":"un", cache_file);
572 if (folder_has_parent_of_type(item, F_QUEUE)) {
573 tmp_flags |= MSG_QUEUED;
574 } else if (folder_has_parent_of_type(item, F_DRAFT)) {
575 tmp_flags |= MSG_DRAFT;
578 if (msgcache_read_cache_data_str(fp, &srccharset, NULL) < 0)
580 dstcharset = CS_UTF_8;
581 if (srccharset == NULL || dstcharset == NULL) {
583 } else if (strcmp(srccharset, dstcharset) == 0) {
584 debug_print("using Noop Converter\n");
588 CharsetConverter *charsetconv;
590 debug_print("using CharsetConverter\n");
592 charsetconv = g_new0(CharsetConverter, 1);
593 charsetconv->converter.convert = strconv_charset_convert;
594 charsetconv->converter.free = strconv_charset_free;
595 charsetconv->srccharset = g_strdup(srccharset);
596 charsetconv->dstcharset = g_strdup(dstcharset);
598 conv = (StringConverter *) charsetconv;
602 cache = msgcache_new();
604 if (msgcache_use_mmap == TRUE) {
605 if (fstat(fileno(fp), &st) >= 0)
606 map_len = st.st_size;
610 cache_data = mmap(NULL, map_len, PROT_READ, MAP_PRIVATE, fileno(fp), 0);
614 if (cache_data != NULL && cache_data != MAP_FAILED) {
615 int rem_len = map_len-ftell(fp);
616 char *walk_data = cache_data+ftell(fp);
619 GET_CACHE_DATA_INT(num);
621 msginfo = procmsg_msginfo_new();
622 msginfo->msgnum = num;
623 memusage += sizeof(MsgInfo);
625 GET_CACHE_DATA_INT(msginfo->size);
626 GET_CACHE_DATA_INT(msginfo->mtime);
627 GET_CACHE_DATA_INT(msginfo->date_t);
628 GET_CACHE_DATA_INT(msginfo->flags.tmp_flags);
630 GET_CACHE_DATA(msginfo->fromname, memusage);
632 GET_CACHE_DATA(msginfo->date, memusage);
633 GET_CACHE_DATA(msginfo->from, memusage);
634 GET_CACHE_DATA(msginfo->to, memusage);
635 GET_CACHE_DATA(msginfo->cc, memusage);
636 GET_CACHE_DATA(msginfo->newsgroups, memusage);
637 GET_CACHE_DATA(msginfo->subject, memusage);
638 GET_CACHE_DATA(msginfo->msgid, memusage);
639 GET_CACHE_DATA(msginfo->inreplyto, memusage);
640 GET_CACHE_DATA(msginfo->xref, memusage);
642 GET_CACHE_DATA_INT(msginfo->planned_download);
643 GET_CACHE_DATA_INT(msginfo->total_size);
644 GET_CACHE_DATA_INT(refnum);
646 for (; refnum != 0; refnum--) {
649 GET_CACHE_DATA(ref, memusage);
652 msginfo->references =
653 g_slist_prepend(msginfo->references, ref);
655 if (msginfo->references)
656 msginfo->references =
657 g_slist_reverse(msginfo->references);
659 msginfo->folder = item;
660 msginfo->flags.tmp_flags |= tmp_flags;
662 g_hash_table_insert(cache->msgnum_table, &msginfo->msgnum, msginfo);
664 g_hash_table_insert(cache->msgid_table, msginfo->msgid, msginfo);
667 munmap(cache_data, map_len);
669 while (fread(&num, sizeof(num), 1, fp) == 1) {
673 msginfo = procmsg_msginfo_new();
674 msginfo->msgnum = num;
675 memusage += sizeof(MsgInfo);
677 READ_CACHE_DATA_INT(msginfo->size, fp);
678 READ_CACHE_DATA_INT(msginfo->mtime, fp);
679 READ_CACHE_DATA_INT(msginfo->date_t, fp);
680 READ_CACHE_DATA_INT(msginfo->flags.tmp_flags, fp);
682 READ_CACHE_DATA(msginfo->fromname, fp, memusage);
684 READ_CACHE_DATA(msginfo->date, fp, memusage);
685 READ_CACHE_DATA(msginfo->from, fp, memusage);
686 READ_CACHE_DATA(msginfo->to, fp, memusage);
687 READ_CACHE_DATA(msginfo->cc, fp, memusage);
688 READ_CACHE_DATA(msginfo->newsgroups, fp, memusage);
689 READ_CACHE_DATA(msginfo->subject, fp, memusage);
690 READ_CACHE_DATA(msginfo->msgid, fp, memusage);
691 READ_CACHE_DATA(msginfo->inreplyto, fp, memusage);
692 READ_CACHE_DATA(msginfo->xref, fp, memusage);
694 READ_CACHE_DATA_INT(msginfo->planned_download, fp);
695 READ_CACHE_DATA_INT(msginfo->total_size, fp);
696 READ_CACHE_DATA_INT(refnum, fp);
698 for (; refnum != 0; refnum--) {
701 READ_CACHE_DATA(ref, fp, memusage);
704 msginfo->references =
705 g_slist_prepend(msginfo->references, ref);
707 if (msginfo->references)
708 msginfo->references =
709 g_slist_reverse(msginfo->references);
711 msginfo->folder = item;
712 msginfo->flags.tmp_flags |= tmp_flags;
714 g_hash_table_insert(cache->msgnum_table, &msginfo->msgnum, msginfo);
716 g_hash_table_insert(cache->msgid_table, msginfo->msgid, msginfo);
723 if (conv->free != NULL)
729 msgcache_destroy(cache);
733 cache->last_access = time(NULL);
734 cache->memusage = memusage;
736 debug_print("done. (%d items read)\n", g_hash_table_size(cache->msgnum_table));
737 debug_print("Cache size: %d messages, %d byte\n", g_hash_table_size(cache->msgnum_table), cache->memusage);
741 void msgcache_read_mark(MsgCache *cache, const gchar *mark_file)
745 MsgPermFlags perm_flags;
748 char *cache_data = NULL;
753 /* In case we can't open the mark file with MARK_VERSION, check if we can open it with the
754 * swapped MARK_VERSION. As msgcache_open_data_file swaps it too, if this succeeds,
755 * it means it's the old version (not little-endian) on a big-endian machine. The code has
756 * no effect on x86 as their file doesn't change. */
758 if ((fp = msgcache_open_data_file(mark_file, MARK_VERSION, DATA_READ, NULL, 0)) == NULL) {
759 /* see if it isn't swapped ? */
760 if ((fp = msgcache_open_data_file(mark_file, bswap_32(MARK_VERSION), DATA_READ, NULL, 0)) == NULL)
763 swapping = FALSE; /* yay */
765 debug_print("reading %sswapped mark file.\n", swapping?"":"un");
767 if (msgcache_use_mmap) {
768 if (fstat(fileno(fp), &st) >= 0)
769 map_len = st.st_size;
773 cache_data = mmap(NULL, map_len, PROT_READ, MAP_PRIVATE, fileno(fp), 0);
777 if (cache_data != NULL && cache_data != MAP_FAILED) {
778 int rem_len = map_len-ftell(fp);
779 char *walk_data = cache_data+ftell(fp);
782 GET_CACHE_DATA_INT(num);
783 GET_CACHE_DATA_INT(perm_flags);
784 msginfo = g_hash_table_lookup(cache->msgnum_table, &num);
786 msginfo->flags.perm_flags = perm_flags;
789 munmap(cache_data, map_len);
791 while (fread(&num, sizeof(num), 1, fp) == 1) {
794 if (fread(&perm_flags, sizeof(perm_flags), 1, fp) != 1) break;
796 perm_flags = bswap_32(perm_flags);
797 msginfo = g_hash_table_lookup(cache->msgnum_table, &num);
799 msginfo->flags.perm_flags = perm_flags;
806 static int msgcache_write_cache(MsgInfo *msginfo, FILE *fp)
808 MsgTmpFlags flags = msginfo->flags.tmp_flags & MSG_CACHED_FLAG_MASK;
810 int w_err = 0, wrote = 0;
812 WRITE_CACHE_DATA_INT(msginfo->msgnum, fp);
813 WRITE_CACHE_DATA_INT(msginfo->size, fp);
814 WRITE_CACHE_DATA_INT(msginfo->mtime, fp);
815 WRITE_CACHE_DATA_INT(msginfo->date_t, fp);
816 WRITE_CACHE_DATA_INT(flags, fp);
818 WRITE_CACHE_DATA(msginfo->fromname, fp);
820 WRITE_CACHE_DATA(msginfo->date, fp);
821 WRITE_CACHE_DATA(msginfo->from, fp);
822 WRITE_CACHE_DATA(msginfo->to, fp);
823 WRITE_CACHE_DATA(msginfo->cc, fp);
824 WRITE_CACHE_DATA(msginfo->newsgroups, fp);
825 WRITE_CACHE_DATA(msginfo->subject, fp);
826 WRITE_CACHE_DATA(msginfo->msgid, fp);
827 WRITE_CACHE_DATA(msginfo->inreplyto, fp);
828 WRITE_CACHE_DATA(msginfo->xref, fp);
829 WRITE_CACHE_DATA_INT(msginfo->planned_download, fp);
830 WRITE_CACHE_DATA_INT(msginfo->total_size, fp);
832 WRITE_CACHE_DATA_INT(g_slist_length(msginfo->references), fp);
834 for (cur = msginfo->references; cur != NULL; cur = cur->next) {
835 WRITE_CACHE_DATA((gchar *)cur->data, fp);
837 return w_err ? -1 : wrote;
840 static int msgcache_write_mmap_cache(MsgInfo *msginfo, char *walk_data)
842 MsgTmpFlags flags = msginfo->flags.tmp_flags & MSG_CACHED_FLAG_MASK;
846 PUT_CACHE_DATA_INT(msginfo->msgnum);
847 PUT_CACHE_DATA_INT(msginfo->size);
848 PUT_CACHE_DATA_INT(msginfo->mtime);
849 PUT_CACHE_DATA_INT(msginfo->date_t);
850 PUT_CACHE_DATA_INT(flags);
851 PUT_CACHE_DATA(msginfo->fromname);
853 PUT_CACHE_DATA(msginfo->date);
854 PUT_CACHE_DATA(msginfo->from);
855 PUT_CACHE_DATA(msginfo->to);
856 PUT_CACHE_DATA(msginfo->cc);
857 PUT_CACHE_DATA(msginfo->newsgroups);
858 PUT_CACHE_DATA(msginfo->subject);
859 PUT_CACHE_DATA(msginfo->msgid);
860 PUT_CACHE_DATA(msginfo->inreplyto);
861 PUT_CACHE_DATA(msginfo->xref);
862 PUT_CACHE_DATA_INT(msginfo->planned_download);
863 PUT_CACHE_DATA_INT(msginfo->total_size);
865 PUT_CACHE_DATA_INT(g_slist_length(msginfo->references));
867 for (cur = msginfo->references; cur != NULL; cur = cur->next) {
868 PUT_CACHE_DATA((gchar *)cur->data);
873 static int msgcache_write_flags(MsgInfo *msginfo, FILE *fp)
875 MsgPermFlags flags = msginfo->flags.perm_flags;
876 int w_err = 0, wrote = 0;
877 WRITE_CACHE_DATA_INT(msginfo->msgnum, fp);
878 WRITE_CACHE_DATA_INT(flags, fp);
879 return w_err ? -1 : wrote;
882 static int msgcache_write_mmap_flags(MsgInfo *msginfo, char *walk_data)
884 MsgPermFlags flags = msginfo->flags.perm_flags;
887 PUT_CACHE_DATA_INT(msginfo->msgnum);
888 PUT_CACHE_DATA_INT(flags);
903 static void msgcache_write_func(gpointer key, gpointer value, gpointer user_data)
906 struct write_fps *write_fps;
909 msginfo = (MsgInfo *)value;
910 write_fps = user_data;
912 tmp = msgcache_write_cache(msginfo, write_fps->cache_fp);
914 write_fps->error = 1;
916 write_fps->cache_size += tmp;
917 tmp= msgcache_write_flags(msginfo, write_fps->mark_fp);
919 write_fps->error = 1;
921 write_fps->mark_size += tmp;
924 static void msgcache_write_mmap_func(gpointer key, gpointer value, gpointer user_data)
927 struct write_fps *write_fps;
930 msginfo = (MsgInfo *)value;
931 write_fps = user_data;
933 tmp = msgcache_write_mmap_cache(msginfo, write_fps->cache_data);
934 write_fps->cache_size += tmp;
935 write_fps->cache_data += tmp;
936 tmp = msgcache_write_mmap_flags(msginfo, write_fps->mark_data);
937 write_fps->mark_size += tmp;
938 write_fps->mark_data += tmp;
941 gint msgcache_write(const gchar *cache_file, const gchar *mark_file, MsgCache *cache)
943 struct write_fps write_fps;
944 gchar *new_cache, *new_mark;
945 int w_err = 0, wrote = 0;
947 char *cache_data = NULL;
948 char *mark_data = NULL;
949 START_TIMING("*** writing caches");
950 g_return_val_if_fail(cache_file != NULL, -1);
951 g_return_val_if_fail(mark_file != NULL, -1);
952 g_return_val_if_fail(cache != NULL, -1);
954 new_cache = g_strconcat(cache_file, ".new", NULL);
955 new_mark = g_strconcat(mark_file, ".new", NULL);
958 write_fps.cache_size = 0;
959 write_fps.mark_size = 0;
960 write_fps.cache_fp = msgcache_open_data_file(new_cache, CACHE_VERSION,
961 DATA_WRITE, NULL, 0);
962 if (write_fps.cache_fp == NULL) {
968 WRITE_CACHE_DATA(CS_UTF_8, write_fps.cache_fp);
971 g_warning("failed to write charset\n");
972 fclose(write_fps.cache_fp);
979 write_fps.mark_fp = msgcache_open_data_file(new_mark, MARK_VERSION,
980 DATA_WRITE, NULL, 0);
981 if (write_fps.mark_fp == NULL) {
982 fclose(write_fps.cache_fp);
989 debug_print("\tWriting message cache to %s and %s...\n", new_cache, new_mark);
991 if (change_file_mode_rw(write_fps.cache_fp, new_cache) < 0)
992 FILE_OP_ERROR(new_cache, "chmod");
994 write_fps.cache_size = ftell(write_fps.cache_fp);
995 write_fps.mark_size = ftell(write_fps.mark_fp);
996 if (msgcache_use_mmap && cache->memusage > 0) {
997 map_len = cache->memusage;
998 if (ftruncate(fileno(write_fps.cache_fp), (off_t)map_len) == 0) {
999 cache_data = mmap(NULL, map_len, PROT_WRITE, MAP_SHARED,
1000 fileno(write_fps.cache_fp), 0);
1002 if (cache_data != NULL && cache_data != MAP_FAILED) {
1003 if (ftruncate(fileno(write_fps.mark_fp), (off_t)map_len) == 0) {
1004 mark_data = mmap(NULL, map_len, PROT_WRITE, MAP_SHARED,
1005 fileno(write_fps.mark_fp), 0);
1007 if (mark_data == NULL || mark_data == MAP_FAILED) {
1008 munmap(cache_data, map_len);
1014 if (cache_data != NULL && cache_data != MAP_FAILED) {
1015 write_fps.cache_data = cache_data + ftell(write_fps.cache_fp);
1016 write_fps.mark_data = mark_data + ftell(write_fps.mark_fp);
1017 g_hash_table_foreach(cache->msgnum_table, msgcache_write_mmap_func, (gpointer)&write_fps);
1018 munmap(cache_data, map_len);
1019 munmap(mark_data, map_len);
1021 g_hash_table_foreach(cache->msgnum_table, msgcache_write_func, (gpointer)&write_fps);
1023 ftruncate(fileno(write_fps.cache_fp), write_fps.cache_size);
1024 ftruncate(fileno(write_fps.mark_fp), write_fps.mark_size);
1025 fclose(write_fps.cache_fp);
1026 fclose(write_fps.mark_fp);
1029 if (write_fps.error != 0) {
1030 g_unlink(new_cache);
1036 move_file(new_cache, cache_file, TRUE);
1037 move_file(new_mark, mark_file, TRUE);
1038 cache->last_access = time(NULL);
1043 debug_print("done.\n");