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>
34 #if G_BYTE_ORDER == G_BIG_ENDIAN
36 ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
37 (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
39 #define MMAP_TO_GUINT32(x) \
41 ((x[2]&0xff) << 8) | \
42 ((x[1]&0xff) << 16) | \
45 #define MMAP_TO_GUINT32_SWAPPED(x) \
47 ((x[1]&0xff) << 8) | \
48 ((x[2]&0xff) << 16) | \
51 static gboolean msgcache_use_mmap = FALSE;
54 #define bswap_32(x) (x)
56 #define MMAP_TO_GUINT32(x) \
58 ((x[1]&0xff) << 8) | \
59 ((x[2]&0xff) << 16) | \
62 #define MMAP_TO_GUINT32_SWAPPED(x) \
64 ((x[1]&0xff) << 8) | \
65 ((x[2]&0xff) << 16) | \
68 static gboolean msgcache_use_mmap = FALSE;
71 static gboolean swapping = TRUE;
81 GHashTable *msgnum_table;
82 GHashTable *msgid_table;
87 typedef struct _StringConverter StringConverter;
88 struct _StringConverter {
89 gchar *(*convert) (StringConverter *converter, gchar *srcstr);
90 void (*free) (StringConverter *converter);
93 typedef struct _StrdupConverter StrdupConverter;
94 struct _StrdupConverter {
95 StringConverter converter;
98 typedef struct _CharsetConverter CharsetConverter;
99 struct _CharsetConverter {
100 StringConverter converter;
106 MsgCache *msgcache_new(void)
110 cache = g_new0(MsgCache, 1),
111 cache->msgnum_table = g_hash_table_new(g_int_hash, g_int_equal);
112 cache->msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
113 cache->last_access = time(NULL);
118 static gboolean msgcache_msginfo_free_func(gpointer num, gpointer msginfo, gpointer user_data)
120 procmsg_msginfo_free((MsgInfo *)msginfo);
124 void msgcache_destroy(MsgCache *cache)
126 g_return_if_fail(cache != NULL);
128 g_hash_table_foreach_remove(cache->msgnum_table, msgcache_msginfo_free_func, NULL);
129 g_hash_table_destroy(cache->msgid_table);
130 g_hash_table_destroy(cache->msgnum_table);
134 void msgcache_add_msg(MsgCache *cache, MsgInfo *msginfo)
138 g_return_if_fail(cache != NULL);
139 g_return_if_fail(msginfo != NULL);
141 newmsginfo = procmsg_msginfo_new_ref(msginfo);
142 g_hash_table_insert(cache->msgnum_table, &newmsginfo->msgnum, newmsginfo);
143 if(newmsginfo->msgid != NULL)
144 g_hash_table_insert(cache->msgid_table, newmsginfo->msgid, newmsginfo);
145 cache->memusage += procmsg_msginfo_memusage(msginfo);
146 cache->last_access = time(NULL);
148 debug_print("Cache size: %d messages, %d bytes\n", g_hash_table_size(cache->msgnum_table), cache->memusage);
151 void msgcache_remove_msg(MsgCache *cache, guint msgnum)
155 g_return_if_fail(cache != NULL);
156 g_return_if_fail(msgnum > 0);
158 msginfo = (MsgInfo *) g_hash_table_lookup(cache->msgnum_table, &msgnum);
162 cache->memusage -= procmsg_msginfo_memusage(msginfo);
164 g_hash_table_remove(cache->msgid_table, msginfo->msgid);
165 g_hash_table_remove(cache->msgnum_table, &msginfo->msgnum);
166 procmsg_msginfo_free(msginfo);
167 cache->last_access = time(NULL);
169 debug_print("Cache size: %d messages, %d byte\n", g_hash_table_size(cache->msgnum_table), cache->memusage);
172 void msgcache_update_msg(MsgCache *cache, MsgInfo *msginfo)
174 MsgInfo *oldmsginfo, *newmsginfo;
176 g_return_if_fail(cache != NULL);
177 g_return_if_fail(msginfo != NULL);
179 oldmsginfo = g_hash_table_lookup(cache->msgnum_table, &msginfo->msgnum);
180 if(oldmsginfo && oldmsginfo->msgid)
181 g_hash_table_remove(cache->msgid_table, oldmsginfo->msgid);
183 g_hash_table_remove(cache->msgnum_table, &oldmsginfo->msgnum);
184 cache->memusage -= procmsg_msginfo_memusage(oldmsginfo);
185 procmsg_msginfo_free(oldmsginfo);
188 newmsginfo = procmsg_msginfo_new_ref(msginfo);
189 g_hash_table_insert(cache->msgnum_table, &newmsginfo->msgnum, newmsginfo);
190 if(newmsginfo->msgid)
191 g_hash_table_insert(cache->msgid_table, newmsginfo->msgid, newmsginfo);
192 cache->memusage += procmsg_msginfo_memusage(newmsginfo);
193 cache->last_access = time(NULL);
195 debug_print("Cache size: %d messages, %d byte\n", g_hash_table_size(cache->msgnum_table), cache->memusage);
200 MsgInfo *msgcache_get_msg(MsgCache *cache, guint num)
204 g_return_val_if_fail(cache != NULL, NULL);
206 msginfo = g_hash_table_lookup(cache->msgnum_table, &num);
209 cache->last_access = time(NULL);
211 return procmsg_msginfo_new_ref(msginfo);
214 MsgInfo *msgcache_get_msg_by_id(MsgCache *cache, const gchar *msgid)
218 g_return_val_if_fail(cache != NULL, NULL);
219 g_return_val_if_fail(msgid != NULL, NULL);
221 msginfo = g_hash_table_lookup(cache->msgid_table, msgid);
224 cache->last_access = time(NULL);
226 return procmsg_msginfo_new_ref(msginfo);
229 static void msgcache_get_msg_list_func(gpointer key, gpointer value, gpointer user_data)
231 MsgInfoList **listptr = user_data;
232 MsgInfo *msginfo = value;
234 *listptr = g_slist_prepend(*listptr, procmsg_msginfo_new_ref(msginfo));
237 MsgInfoList *msgcache_get_msg_list(MsgCache *cache)
239 MsgInfoList *msg_list = NULL;
240 START_TIMING("msgcache_get_msg_list");
241 g_return_val_if_fail(cache != NULL, NULL);
243 g_hash_table_foreach((GHashTable *)cache->msgnum_table, msgcache_get_msg_list_func, (gpointer)&msg_list);
244 cache->last_access = time(NULL);
246 msg_list = g_slist_reverse(msg_list);
251 time_t msgcache_get_last_access_time(MsgCache *cache)
253 g_return_val_if_fail(cache != NULL, 0);
255 return cache->last_access;
258 gint msgcache_get_memory_usage(MsgCache *cache)
260 g_return_val_if_fail(cache != NULL, 0);
262 return cache->memusage;
266 * Cache saving functions
269 #define READ_CACHE_DATA(data, fp, total_len) \
271 if ((tmp_len = msgcache_read_cache_data_str(fp, &data, conv)) < 0) { \
272 procmsg_msginfo_free(msginfo); \
276 total_len += tmp_len; \
279 #define READ_CACHE_DATA_INT(n, fp) \
284 if ((ni = fread(&idata, sizeof(idata), 1, fp)) != 1) { \
285 g_warning("read_int: Cache data corrupted, read %d of %d at " \
286 "offset %ld\n", ni, sizeof(idata), ftell(fp)); \
287 procmsg_msginfo_free(msginfo); \
291 n = swapping ? bswap_32(idata) : (idata);\
294 #define GET_CACHE_DATA_INT(n) \
296 n = (swapping ? (MMAP_TO_GUINT32_SWAPPED(walk_data)):(MMAP_TO_GUINT32(walk_data))); \
297 walk_data += 4; rem_len -= 4; \
300 #define GET_CACHE_DATA(data, total_len) \
302 GET_CACHE_DATA_INT(tmp_len); \
303 if ((tmp_len = msgcache_get_cache_data_str(walk_data, &data, tmp_len, conv)) < 0) { \
304 printf("error at rem_len:%d\n", rem_len);\
305 procmsg_msginfo_free(msginfo); \
309 total_len += tmp_len; \
310 walk_data += tmp_len; rem_len -= tmp_len; \
314 #define WRITE_CACHE_DATA_INT(n, fp) \
318 idata = (guint32)bswap_32(n); \
319 if (fwrite(&idata, sizeof(idata), 1, fp) != 1) \
324 #define PUT_CACHE_DATA_INT(n) \
326 walk_data[0]=(((guint32)n)&0x000000ff); \
327 walk_data[1]=(((guint32)n)&0x0000ff00)>>8; \
328 walk_data[2]=(((guint32)n)&0x00ff0000)>>16; \
329 walk_data[3]=(((guint32)n)&0xff000000)>>24; \
334 #define WRITE_CACHE_DATA(data, fp) \
340 len = strlen(data); \
341 WRITE_CACHE_DATA_INT(len, fp); \
342 if (w_err == 0 && len > 0) { \
343 if (fwrite(data, 1, len, fp) != len) \
349 #define PUT_CACHE_DATA(data) \
355 len = strlen(data); \
356 PUT_CACHE_DATA_INT(len); \
358 memcpy(walk_data, data, len); \
364 static FILE *msgcache_open_data_file(const gchar *file, guint version,
366 gchar *buf, size_t buf_size)
371 g_return_val_if_fail(file != NULL, NULL);
373 if (mode == DATA_WRITE) {
374 int w_err = 0, wrote = 0;
375 if ((fp = g_fopen(file, "w+")) == NULL) {
376 FILE_OP_ERROR(file, "fopen");
379 if (change_file_mode_rw(fp, file) < 0)
380 FILE_OP_ERROR(file, "chmod");
382 WRITE_CACHE_DATA_INT(version, fp);
384 g_warning("failed to write int\n");
392 if ((fp = g_fopen(file, "rb")) == NULL)
393 debug_print("Mark/Cache file '%s' not found\n", file);
395 if (buf && buf_size > 0)
396 setvbuf(fp, buf, _IOFBF, buf_size);
397 if (fread(&data_ver, sizeof(data_ver), 1, fp) != 1 ||
398 version != bswap_32(data_ver)) {
399 g_message("%s: Mark/Cache version is different (%u != %u).\n",
400 file, bswap_32(data_ver), version);
404 data_ver = bswap_32(data_ver);
407 if (mode == DATA_READ)
411 /* reopen with append mode */
413 if ((fp = g_fopen(file, "ab")) == NULL)
414 FILE_OP_ERROR(file, "fopen");
416 /* open with overwrite mode if mark file doesn't exist or
417 version is different */
418 fp = msgcache_open_data_file(file, version, DATA_WRITE, buf,
425 static gint msgcache_read_cache_data_str(FILE *fp, gchar **str,
426 StringConverter *conv)
428 gchar *tmpstr = NULL;
434 if ((ni = fread(&len, sizeof(len), 1, fp) != 1) ||
436 g_warning("read_data_str: Cache data (len) corrupted, read %d "
437 "of %d bytes at offset %ld\n", ni, sizeof(len),
442 if ((ni = fread(&len, sizeof(len), 1, fp) != 1) ||
443 bswap_32(len) > G_MAXINT) {
444 g_warning("read_data_str: Cache data (len) corrupted, read %d "
445 "of %d bytes at offset %ld\n", ni, sizeof(len),
455 tmpstr = g_try_malloc(len + 1);
458 g_warning("read_data_str: can't g_malloc %d bytes - cache data probably corrupted.\n", len);
462 if ((ni = fread(tmpstr, 1, len, fp)) != len) {
463 g_warning("read_data_str: Cache data corrupted, read %d of %d "
464 "bytes at offset %ld\n",
472 *str = conv->convert(conv, tmpstr);
480 static gint msgcache_get_cache_data_str(gchar *src, gchar **str, gint len,
481 StringConverter *conv)
483 gchar *tmpstr = NULL;
490 tmpstr = g_try_malloc(len + 1);
493 g_warning("read_data_str: can't g_malloc %d bytes - cache data probably corrupted.\n", len);
497 strncpy(tmpstr, src, len);
502 *str = conv->convert(conv, tmpstr);
510 gchar *strconv_strdup_convert(StringConverter *conv, gchar *srcstr)
512 return g_strdup(srcstr);
515 gchar *strconv_charset_convert(StringConverter *conv, gchar *srcstr)
517 CharsetConverter *charsetconv = (CharsetConverter *) conv;
519 return conv_codeset_strdup(srcstr, charsetconv->srccharset, charsetconv->dstcharset);
522 void strconv_charset_free(StringConverter *conv)
524 CharsetConverter *charsetconv = (CharsetConverter *) conv;
526 g_free(charsetconv->srccharset);
527 g_free(charsetconv->dstcharset);
530 MsgCache *msgcache_read_cache(FolderItem *item, const gchar *cache_file)
535 MsgTmpFlags tmp_flags = 0;
536 gchar file_buf[BUFFSIZE];
539 gboolean error = FALSE;
540 StringConverter *conv = NULL;
541 gchar *srccharset = NULL;
542 const gchar *dstcharset = NULL;
545 gint tmp_len = 0, map_len = -1;
546 char *cache_data = NULL;
549 g_return_val_if_fail(cache_file != NULL, NULL);
550 g_return_val_if_fail(item != NULL, NULL);
554 /* In case we can't open the mark file with MARK_VERSION, check if we can open it with the
555 * swapped MARK_VERSION. As msgcache_open_data_file swaps it too, if this succeeds,
556 * it means it's the old version (not little-endian) on a big-endian machine. The code has
557 * no effect on x86 as their file doesn't change. */
559 if ((fp = msgcache_open_data_file
560 (cache_file, CACHE_VERSION, DATA_READ, file_buf, sizeof(file_buf))) == NULL) {
561 if ((fp = msgcache_open_data_file
562 (cache_file, bswap_32(CACHE_VERSION), DATA_READ, file_buf, sizeof(file_buf))) == NULL)
568 debug_print("\tReading %sswapped message cache from %s...\n", swapping?"":"un", cache_file);
570 if (folder_has_parent_of_type(item, F_QUEUE)) {
571 tmp_flags |= MSG_QUEUED;
572 } else if (folder_has_parent_of_type(item, F_DRAFT)) {
573 tmp_flags |= MSG_DRAFT;
576 if (msgcache_read_cache_data_str(fp, &srccharset, NULL) < 0)
578 dstcharset = CS_UTF_8;
579 if (srccharset == NULL || dstcharset == NULL) {
581 } else if (strcmp(srccharset, dstcharset) == 0) {
582 debug_print("using Noop Converter\n");
586 CharsetConverter *charsetconv;
588 debug_print("using CharsetConverter\n");
590 charsetconv = g_new0(CharsetConverter, 1);
591 charsetconv->converter.convert = strconv_charset_convert;
592 charsetconv->converter.free = strconv_charset_free;
593 charsetconv->srccharset = g_strdup(srccharset);
594 charsetconv->dstcharset = g_strdup(dstcharset);
596 conv = (StringConverter *) charsetconv;
600 cache = msgcache_new();
602 if (msgcache_use_mmap == TRUE) {
603 if (fstat(fileno(fp), &st) >= 0)
604 map_len = st.st_size;
608 cache_data = mmap(NULL, map_len, PROT_READ, MAP_PRIVATE, fileno(fp), 0);
612 if (cache_data != NULL && cache_data != MAP_FAILED) {
613 int rem_len = map_len-ftell(fp);
614 char *walk_data = cache_data+ftell(fp);
617 GET_CACHE_DATA_INT(num);
619 msginfo = procmsg_msginfo_new();
620 msginfo->msgnum = num;
621 memusage += sizeof(MsgInfo);
623 GET_CACHE_DATA_INT(msginfo->size);
624 GET_CACHE_DATA_INT(msginfo->mtime);
625 GET_CACHE_DATA_INT(msginfo->date_t);
626 GET_CACHE_DATA_INT(msginfo->flags.tmp_flags);
628 GET_CACHE_DATA(msginfo->fromname, memusage);
630 GET_CACHE_DATA(msginfo->date, memusage);
631 GET_CACHE_DATA(msginfo->from, memusage);
632 GET_CACHE_DATA(msginfo->to, memusage);
633 GET_CACHE_DATA(msginfo->cc, memusage);
634 GET_CACHE_DATA(msginfo->newsgroups, memusage);
635 GET_CACHE_DATA(msginfo->subject, memusage);
636 GET_CACHE_DATA(msginfo->msgid, memusage);
637 GET_CACHE_DATA(msginfo->inreplyto, memusage);
638 GET_CACHE_DATA(msginfo->xref, memusage);
640 GET_CACHE_DATA_INT(msginfo->planned_download);
641 GET_CACHE_DATA_INT(msginfo->total_size);
642 GET_CACHE_DATA_INT(refnum);
644 for (; refnum != 0; refnum--) {
647 GET_CACHE_DATA(ref, memusage);
650 msginfo->references =
651 g_slist_prepend(msginfo->references, ref);
653 if (msginfo->references)
654 msginfo->references =
655 g_slist_reverse(msginfo->references);
657 msginfo->folder = item;
658 msginfo->flags.tmp_flags |= tmp_flags;
660 g_hash_table_insert(cache->msgnum_table, &msginfo->msgnum, msginfo);
662 g_hash_table_insert(cache->msgid_table, msginfo->msgid, msginfo);
665 munmap(cache_data, map_len);
667 while (fread(&num, sizeof(num), 1, fp) == 1) {
671 msginfo = procmsg_msginfo_new();
672 msginfo->msgnum = num;
673 memusage += sizeof(MsgInfo);
675 READ_CACHE_DATA_INT(msginfo->size, fp);
676 READ_CACHE_DATA_INT(msginfo->mtime, fp);
677 READ_CACHE_DATA_INT(msginfo->date_t, fp);
678 READ_CACHE_DATA_INT(msginfo->flags.tmp_flags, fp);
680 READ_CACHE_DATA(msginfo->fromname, fp, memusage);
682 READ_CACHE_DATA(msginfo->date, fp, memusage);
683 READ_CACHE_DATA(msginfo->from, fp, memusage);
684 READ_CACHE_DATA(msginfo->to, fp, memusage);
685 READ_CACHE_DATA(msginfo->cc, fp, memusage);
686 READ_CACHE_DATA(msginfo->newsgroups, fp, memusage);
687 READ_CACHE_DATA(msginfo->subject, fp, memusage);
688 READ_CACHE_DATA(msginfo->msgid, fp, memusage);
689 READ_CACHE_DATA(msginfo->inreplyto, fp, memusage);
690 READ_CACHE_DATA(msginfo->xref, fp, memusage);
692 READ_CACHE_DATA_INT(msginfo->planned_download, fp);
693 READ_CACHE_DATA_INT(msginfo->total_size, fp);
694 READ_CACHE_DATA_INT(refnum, fp);
696 for (; refnum != 0; refnum--) {
699 READ_CACHE_DATA(ref, fp, memusage);
702 msginfo->references =
703 g_slist_prepend(msginfo->references, ref);
705 if (msginfo->references)
706 msginfo->references =
707 g_slist_reverse(msginfo->references);
709 msginfo->folder = item;
710 msginfo->flags.tmp_flags |= tmp_flags;
712 g_hash_table_insert(cache->msgnum_table, &msginfo->msgnum, msginfo);
714 g_hash_table_insert(cache->msgid_table, msginfo->msgid, msginfo);
721 if (conv->free != NULL)
727 msgcache_destroy(cache);
731 cache->last_access = time(NULL);
732 cache->memusage = memusage;
734 debug_print("done. (%d items read)\n", g_hash_table_size(cache->msgnum_table));
735 debug_print("Cache size: %d messages, %d byte\n", g_hash_table_size(cache->msgnum_table), cache->memusage);
739 void msgcache_read_mark(MsgCache *cache, const gchar *mark_file)
743 MsgPermFlags perm_flags;
746 char *cache_data = NULL;
751 /* In case we can't open the mark file with MARK_VERSION, check if we can open it with the
752 * swapped MARK_VERSION. As msgcache_open_data_file swaps it too, if this succeeds,
753 * it means it's the old version (not little-endian) on a big-endian machine. The code has
754 * no effect on x86 as their file doesn't change. */
756 if ((fp = msgcache_open_data_file(mark_file, MARK_VERSION, DATA_READ, NULL, 0)) == NULL) {
757 /* see if it isn't swapped ? */
758 if ((fp = msgcache_open_data_file(mark_file, bswap_32(MARK_VERSION), DATA_READ, NULL, 0)) == NULL)
761 swapping = FALSE; /* yay */
763 debug_print("reading %sswapped mark file.\n", swapping?"":"un");
765 if (msgcache_use_mmap) {
766 if (fstat(fileno(fp), &st) >= 0)
767 map_len = st.st_size;
771 cache_data = mmap(NULL, map_len, PROT_READ, MAP_PRIVATE, fileno(fp), 0);
775 if (cache_data != NULL && cache_data != MAP_FAILED) {
776 int rem_len = map_len-ftell(fp);
777 char *walk_data = cache_data+ftell(fp);
780 GET_CACHE_DATA_INT(num);
781 GET_CACHE_DATA_INT(perm_flags);
782 msginfo = g_hash_table_lookup(cache->msgnum_table, &num);
784 msginfo->flags.perm_flags = perm_flags;
787 munmap(cache_data, map_len);
789 while (fread(&num, sizeof(num), 1, fp) == 1) {
792 if (fread(&perm_flags, sizeof(perm_flags), 1, fp) != 1) break;
794 perm_flags = bswap_32(perm_flags);
795 msginfo = g_hash_table_lookup(cache->msgnum_table, &num);
797 msginfo->flags.perm_flags = perm_flags;
804 static int msgcache_write_cache(MsgInfo *msginfo, FILE *fp)
806 MsgTmpFlags flags = msginfo->flags.tmp_flags & MSG_CACHED_FLAG_MASK;
808 int w_err = 0, wrote = 0;
810 WRITE_CACHE_DATA_INT(msginfo->msgnum, fp);
811 WRITE_CACHE_DATA_INT(msginfo->size, fp);
812 WRITE_CACHE_DATA_INT(msginfo->mtime, fp);
813 WRITE_CACHE_DATA_INT(msginfo->date_t, fp);
814 WRITE_CACHE_DATA_INT(flags, fp);
816 WRITE_CACHE_DATA(msginfo->fromname, fp);
818 WRITE_CACHE_DATA(msginfo->date, fp);
819 WRITE_CACHE_DATA(msginfo->from, fp);
820 WRITE_CACHE_DATA(msginfo->to, fp);
821 WRITE_CACHE_DATA(msginfo->cc, fp);
822 WRITE_CACHE_DATA(msginfo->newsgroups, fp);
823 WRITE_CACHE_DATA(msginfo->subject, fp);
824 WRITE_CACHE_DATA(msginfo->msgid, fp);
825 WRITE_CACHE_DATA(msginfo->inreplyto, fp);
826 WRITE_CACHE_DATA(msginfo->xref, fp);
827 WRITE_CACHE_DATA_INT(msginfo->planned_download, fp);
828 WRITE_CACHE_DATA_INT(msginfo->total_size, fp);
830 WRITE_CACHE_DATA_INT(g_slist_length(msginfo->references), fp);
832 for (cur = msginfo->references; cur != NULL; cur = cur->next) {
833 WRITE_CACHE_DATA((gchar *)cur->data, fp);
835 return w_err ? -1 : wrote;
838 static int msgcache_write_mmap_cache(MsgInfo *msginfo, char *walk_data)
840 MsgTmpFlags flags = msginfo->flags.tmp_flags & MSG_CACHED_FLAG_MASK;
844 PUT_CACHE_DATA_INT(msginfo->msgnum);
845 PUT_CACHE_DATA_INT(msginfo->size);
846 PUT_CACHE_DATA_INT(msginfo->mtime);
847 PUT_CACHE_DATA_INT(msginfo->date_t);
848 PUT_CACHE_DATA_INT(flags);
849 PUT_CACHE_DATA(msginfo->fromname);
851 PUT_CACHE_DATA(msginfo->date);
852 PUT_CACHE_DATA(msginfo->from);
853 PUT_CACHE_DATA(msginfo->to);
854 PUT_CACHE_DATA(msginfo->cc);
855 PUT_CACHE_DATA(msginfo->newsgroups);
856 PUT_CACHE_DATA(msginfo->subject);
857 PUT_CACHE_DATA(msginfo->msgid);
858 PUT_CACHE_DATA(msginfo->inreplyto);
859 PUT_CACHE_DATA(msginfo->xref);
860 PUT_CACHE_DATA_INT(msginfo->planned_download);
861 PUT_CACHE_DATA_INT(msginfo->total_size);
863 PUT_CACHE_DATA_INT(g_slist_length(msginfo->references));
865 for (cur = msginfo->references; cur != NULL; cur = cur->next) {
866 PUT_CACHE_DATA((gchar *)cur->data);
871 static int msgcache_write_flags(MsgInfo *msginfo, FILE *fp)
873 MsgPermFlags flags = msginfo->flags.perm_flags;
874 int w_err = 0, wrote = 0;
875 WRITE_CACHE_DATA_INT(msginfo->msgnum, fp);
876 WRITE_CACHE_DATA_INT(flags, fp);
877 return w_err ? -1 : wrote;
880 static int msgcache_write_mmap_flags(MsgInfo *msginfo, char *walk_data)
882 MsgPermFlags flags = msginfo->flags.perm_flags;
885 PUT_CACHE_DATA_INT(msginfo->msgnum);
886 PUT_CACHE_DATA_INT(flags);
901 static void msgcache_write_func(gpointer key, gpointer value, gpointer user_data)
904 struct write_fps *write_fps;
907 msginfo = (MsgInfo *)value;
908 write_fps = user_data;
910 tmp = msgcache_write_cache(msginfo, write_fps->cache_fp);
912 write_fps->error = 1;
914 write_fps->cache_size += tmp;
915 tmp= msgcache_write_flags(msginfo, write_fps->mark_fp);
917 write_fps->error = 1;
919 write_fps->mark_size += tmp;
922 static void msgcache_write_mmap_func(gpointer key, gpointer value, gpointer user_data)
925 struct write_fps *write_fps;
928 msginfo = (MsgInfo *)value;
929 write_fps = user_data;
931 tmp = msgcache_write_mmap_cache(msginfo, write_fps->cache_data);
932 write_fps->cache_size += tmp;
933 write_fps->cache_data += tmp;
934 tmp = msgcache_write_mmap_flags(msginfo, write_fps->mark_data);
935 write_fps->mark_size += tmp;
936 write_fps->mark_data += tmp;
939 gint msgcache_write(const gchar *cache_file, const gchar *mark_file, MsgCache *cache)
941 struct write_fps write_fps;
942 gchar *new_cache, *new_mark;
943 int w_err = 0, wrote = 0;
945 char *cache_data = NULL;
946 char *mark_data = NULL;
947 START_TIMING("*** writing caches");
948 g_return_val_if_fail(cache_file != NULL, -1);
949 g_return_val_if_fail(mark_file != NULL, -1);
950 g_return_val_if_fail(cache != NULL, -1);
952 new_cache = g_strconcat(cache_file, ".new", NULL);
953 new_mark = g_strconcat(mark_file, ".new", NULL);
956 write_fps.cache_size = 0;
957 write_fps.mark_size = 0;
958 write_fps.cache_fp = msgcache_open_data_file(new_cache, CACHE_VERSION,
959 DATA_WRITE, NULL, 0);
960 if (write_fps.cache_fp == NULL) {
966 WRITE_CACHE_DATA(CS_UTF_8, write_fps.cache_fp);
969 g_warning("failed to write charset\n");
970 fclose(write_fps.cache_fp);
977 write_fps.mark_fp = msgcache_open_data_file(new_mark, MARK_VERSION,
978 DATA_WRITE, NULL, 0);
979 if (write_fps.mark_fp == NULL) {
980 fclose(write_fps.cache_fp);
987 debug_print("\tWriting message cache to %s and %s...\n", new_cache, new_mark);
989 if (change_file_mode_rw(write_fps.cache_fp, new_cache) < 0)
990 FILE_OP_ERROR(new_cache, "chmod");
992 write_fps.cache_size = ftell(write_fps.cache_fp);
993 write_fps.mark_size = ftell(write_fps.mark_fp);
994 if (msgcache_use_mmap && cache->memusage > 0) {
995 map_len = cache->memusage;
996 if (ftruncate(fileno(write_fps.cache_fp), (off_t)map_len) == 0) {
997 cache_data = mmap(NULL, map_len, PROT_WRITE, MAP_SHARED,
998 fileno(write_fps.cache_fp), 0);
1000 if (cache_data != NULL && cache_data != MAP_FAILED) {
1001 if (ftruncate(fileno(write_fps.mark_fp), (off_t)map_len) == 0) {
1002 mark_data = mmap(NULL, map_len, PROT_WRITE, MAP_SHARED,
1003 fileno(write_fps.mark_fp), 0);
1005 if (mark_data == NULL || mark_data == MAP_FAILED) {
1006 munmap(cache_data, map_len);
1012 if (cache_data != NULL && cache_data != MAP_FAILED) {
1013 write_fps.cache_data = cache_data + ftell(write_fps.cache_fp);
1014 write_fps.mark_data = mark_data + ftell(write_fps.mark_fp);
1015 g_hash_table_foreach(cache->msgnum_table, msgcache_write_mmap_func, (gpointer)&write_fps);
1016 munmap(cache_data, map_len);
1017 munmap(mark_data, map_len);
1019 g_hash_table_foreach(cache->msgnum_table, msgcache_write_func, (gpointer)&write_fps);
1021 ftruncate(fileno(write_fps.cache_fp), write_fps.cache_size);
1022 ftruncate(fileno(write_fps.mark_fp), write_fps.mark_size);
1023 fclose(write_fps.cache_fp);
1024 fclose(write_fps.mark_fp);
1027 if (write_fps.error != 0) {
1028 g_unlink(new_cache);
1034 move_file(new_cache, cache_file, TRUE);
1035 move_file(new_mark, mark_file, TRUE);
1036 cache->last_access = time(NULL);
1041 debug_print("done.\n");