2006-06-11 [colin] 2.2.3cvs12
[claws.git] / src / msgcache.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto & The Sylpheed Claws Team
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #include "defs.h"
21
22 #include <glib.h>
23 #include <glib/gi18n.h>
24
25 #include <time.h>
26
27 #include "msgcache.h"
28 #include "utils.h"
29 #include "procmsg.h"
30 #include "codeconv.h"
31 #include "timing.h"
32
33 #if G_BYTE_ORDER == G_BIG_ENDIAN
34 #define bswap_32(x) \
35      ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) | \
36       (((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24))
37 #else
38 #define bswap_32(x) (x)
39 #endif
40
41 static gboolean swapping = TRUE;
42
43 typedef enum
44 {
45         DATA_READ,
46         DATA_WRITE,
47         DATA_APPEND
48 } DataOpenMode;
49
50 struct _MsgCache {
51         GHashTable      *msgnum_table;
52         GHashTable      *msgid_table;
53         guint            memusage;
54         time_t           last_access;
55 };
56
57 typedef struct _StringConverter StringConverter;
58 struct _StringConverter {
59         gchar *(*convert) (StringConverter *converter, gchar *srcstr);
60         void   (*free)    (StringConverter *converter);
61 };
62
63 typedef struct _StrdupConverter StrdupConverter;
64 struct _StrdupConverter {
65         StringConverter converter;
66 };
67
68 typedef struct _CharsetConverter CharsetConverter;
69 struct _CharsetConverter {
70         StringConverter converter;
71
72         gchar *srccharset;
73         gchar *dstcharset;
74 };
75
76 MsgCache *msgcache_new(void)
77 {
78         MsgCache *cache;
79         
80         cache = g_new0(MsgCache, 1),
81         cache->msgnum_table = g_hash_table_new(g_int_hash, g_int_equal);
82         cache->msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
83         cache->last_access = time(NULL);
84
85         return cache;
86 }
87
88 static gboolean msgcache_msginfo_free_func(gpointer num, gpointer msginfo, gpointer user_data)
89 {
90         procmsg_msginfo_free((MsgInfo *)msginfo);
91         return TRUE;
92 }                                                                                         
93
94 void msgcache_destroy(MsgCache *cache)
95 {
96         g_return_if_fail(cache != NULL);
97
98         g_hash_table_foreach_remove(cache->msgnum_table, msgcache_msginfo_free_func, NULL);
99         g_hash_table_destroy(cache->msgid_table);
100         g_hash_table_destroy(cache->msgnum_table);
101         g_free(cache);
102 }
103
104 void msgcache_add_msg(MsgCache *cache, MsgInfo *msginfo) 
105 {
106         MsgInfo *newmsginfo;
107
108         g_return_if_fail(cache != NULL);
109         g_return_if_fail(msginfo != NULL);
110
111         newmsginfo = procmsg_msginfo_new_ref(msginfo);
112         g_hash_table_insert(cache->msgnum_table, &newmsginfo->msgnum, newmsginfo);
113         if(newmsginfo->msgid != NULL)
114                 g_hash_table_insert(cache->msgid_table, newmsginfo->msgid, newmsginfo);
115         cache->memusage += procmsg_msginfo_memusage(msginfo);
116         cache->last_access = time(NULL);
117
118         debug_print("Cache size: %d messages, %d bytes\n", g_hash_table_size(cache->msgnum_table), cache->memusage);
119 }
120
121 void msgcache_remove_msg(MsgCache *cache, guint msgnum)
122 {
123         MsgInfo *msginfo;
124
125         g_return_if_fail(cache != NULL);
126         g_return_if_fail(msgnum > 0);
127
128         msginfo = (MsgInfo *) g_hash_table_lookup(cache->msgnum_table, &msgnum);
129         if(!msginfo)
130                 return;
131
132         cache->memusage -= procmsg_msginfo_memusage(msginfo);
133         if(msginfo->msgid)
134                 g_hash_table_remove(cache->msgid_table, msginfo->msgid);
135         g_hash_table_remove(cache->msgnum_table, &msginfo->msgnum);
136         procmsg_msginfo_free(msginfo);
137         cache->last_access = time(NULL);
138
139         debug_print("Cache size: %d messages, %d byte\n", g_hash_table_size(cache->msgnum_table), cache->memusage);
140 }
141
142 void msgcache_update_msg(MsgCache *cache, MsgInfo *msginfo)
143 {
144         MsgInfo *oldmsginfo, *newmsginfo;
145         
146         g_return_if_fail(cache != NULL);
147         g_return_if_fail(msginfo != NULL);
148
149         oldmsginfo = g_hash_table_lookup(cache->msgnum_table, &msginfo->msgnum);
150         if(oldmsginfo && oldmsginfo->msgid) 
151                 g_hash_table_remove(cache->msgid_table, oldmsginfo->msgid);
152         if (oldmsginfo) {
153                 g_hash_table_remove(cache->msgnum_table, &oldmsginfo->msgnum);
154                 cache->memusage -= procmsg_msginfo_memusage(oldmsginfo);
155                 procmsg_msginfo_free(oldmsginfo);
156         }
157
158         newmsginfo = procmsg_msginfo_new_ref(msginfo);
159         g_hash_table_insert(cache->msgnum_table, &newmsginfo->msgnum, newmsginfo);
160         if(newmsginfo->msgid)
161                 g_hash_table_insert(cache->msgid_table, newmsginfo->msgid, newmsginfo);
162         cache->memusage += procmsg_msginfo_memusage(newmsginfo);
163         cache->last_access = time(NULL);
164         
165         debug_print("Cache size: %d messages, %d byte\n", g_hash_table_size(cache->msgnum_table), cache->memusage);
166
167         return;
168 }
169
170 MsgInfo *msgcache_get_msg(MsgCache *cache, guint num)
171 {
172         MsgInfo *msginfo;
173
174         g_return_val_if_fail(cache != NULL, NULL);
175
176         msginfo = g_hash_table_lookup(cache->msgnum_table, &num);
177         if(!msginfo)
178                 return NULL;
179         cache->last_access = time(NULL);
180         
181         return procmsg_msginfo_new_ref(msginfo);
182 }
183
184 MsgInfo *msgcache_get_msg_by_id(MsgCache *cache, const gchar *msgid)
185 {
186         MsgInfo *msginfo;
187         
188         g_return_val_if_fail(cache != NULL, NULL);
189         g_return_val_if_fail(msgid != NULL, NULL);
190
191         msginfo = g_hash_table_lookup(cache->msgid_table, msgid);
192         if(!msginfo)
193                 return NULL;
194         cache->last_access = time(NULL);
195         
196         return procmsg_msginfo_new_ref(msginfo);        
197 }
198
199 static void msgcache_get_msg_list_func(gpointer key, gpointer value, gpointer user_data)
200 {
201         MsgInfoList **listptr = user_data;
202         MsgInfo *msginfo = value;
203
204         *listptr = g_slist_prepend(*listptr, procmsg_msginfo_new_ref(msginfo));
205 }
206
207 MsgInfoList *msgcache_get_msg_list(MsgCache *cache)
208 {
209         MsgInfoList *msg_list = NULL;
210         START_TIMING("msgcache_get_msg_list");
211         g_return_val_if_fail(cache != NULL, NULL);
212
213         g_hash_table_foreach((GHashTable *)cache->msgnum_table, msgcache_get_msg_list_func, (gpointer)&msg_list);       
214         cache->last_access = time(NULL);
215         
216         msg_list = g_slist_reverse(msg_list);
217         END_TIMING();
218         return msg_list;
219 }
220
221 time_t msgcache_get_last_access_time(MsgCache *cache)
222 {
223         g_return_val_if_fail(cache != NULL, 0);
224         
225         return cache->last_access;
226 }
227
228 gint msgcache_get_memory_usage(MsgCache *cache)
229 {
230         g_return_val_if_fail(cache != NULL, 0);
231
232         return cache->memusage;
233 }
234
235 /*
236  *  Cache saving functions
237  */
238
239 #define READ_CACHE_DATA(data, fp, total_len) \
240 { \
241         if ((tmp_len = msgcache_read_cache_data_str(fp, &data, conv)) < 0) { \
242                 procmsg_msginfo_free(msginfo); \
243                 error = TRUE; \
244                 goto bail_err; \
245         } \
246         total_len += tmp_len; \
247 }
248
249 #define READ_CACHE_DATA_INT(n, fp) \
250 { \
251         guint32 idata; \
252         size_t ni; \
253  \
254         if ((ni = fread(&idata, sizeof(idata), 1, fp)) != 1) { \
255                 g_warning("read_int: Cache data corrupted, read %d of %d at " \
256                           "offset %ld\n", ni, sizeof(idata), ftell(fp)); \
257                 procmsg_msginfo_free(msginfo); \
258                 error = TRUE; \
259                 goto bail_err; \
260         } else \
261                 n = swapping ? bswap_32(idata) : (idata);\
262 }
263
264 #define WRITE_CACHE_DATA_INT(n, fp)             \
265 {                                               \
266         guint32 idata;                          \
267                                                 \
268         idata = (guint32)bswap_32(n);                   \
269         fwrite(&idata, sizeof(idata), 1, fp);   \
270 }
271
272 #define WRITE_CACHE_DATA(data, fp) \
273 { \
274         size_t len; \
275         if (data == NULL) \
276                 len = 0; \
277         else \
278                 len = strlen(data); \
279         WRITE_CACHE_DATA_INT(len, fp); \
280         if (len > 0) { \
281                 fwrite(data, len, 1, fp); \
282         } \
283 }
284
285 static FILE *msgcache_open_data_file(const gchar *file, guint version,
286                                      DataOpenMode mode,
287                                      gchar *buf, size_t buf_size)
288 {
289         FILE *fp;
290         gint32 data_ver;
291
292         g_return_val_if_fail(file != NULL, NULL);
293
294         if (mode == DATA_WRITE) {
295                 if ((fp = g_fopen(file, "wb")) == NULL) {
296                         FILE_OP_ERROR(file, "fopen");
297                         return NULL;
298                 }
299                 if (change_file_mode_rw(fp, file) < 0)
300                         FILE_OP_ERROR(file, "chmod");
301
302                 WRITE_CACHE_DATA_INT(version, fp);
303                 return fp;
304         }
305
306         /* check version */
307         if ((fp = g_fopen(file, "rb")) == NULL)
308                 debug_print("Mark/Cache file '%s' not found\n", file);
309         else {
310                 if (buf && buf_size > 0)
311                         setvbuf(fp, buf, _IOFBF, buf_size);
312                 if (fread(&data_ver, sizeof(data_ver), 1, fp) != 1 ||
313                          version != bswap_32(data_ver)) {
314                         g_message("%s: Mark/Cache version is different (%u != %u).\n",
315                                   file, bswap_32(data_ver), version);
316                         fclose(fp);
317                         fp = NULL;
318                 }
319                 data_ver = bswap_32(data_ver);
320         }
321         
322         if (mode == DATA_READ)
323                 return fp;
324
325         if (fp) {
326                 /* reopen with append mode */
327                 fclose(fp);
328                 if ((fp = g_fopen(file, "ab")) == NULL)
329                         FILE_OP_ERROR(file, "fopen");
330         } else {
331                 /* open with overwrite mode if mark file doesn't exist or
332                    version is different */
333                 fp = msgcache_open_data_file(file, version, DATA_WRITE, buf,
334                                             buf_size);
335         }
336
337         return fp;
338 }
339
340 static gint msgcache_read_cache_data_str(FILE *fp, gchar **str, 
341                                          StringConverter *conv)
342 {
343         gchar *tmpstr = NULL;
344         size_t ni;
345         guint32 len;
346
347         *str = NULL;
348         if (!swapping) {
349                 if ((ni = fread(&len, sizeof(len), 1, fp) != 1) ||
350                     len > G_MAXINT) {
351                         g_warning("read_data_str: Cache data (len) corrupted, read %d "
352                                   "of %d bytes at offset %ld\n", ni, sizeof(len), 
353                                   ftell(fp));
354                         return -1;
355                 }
356         } else {
357                 if ((ni = fread(&len, sizeof(len), 1, fp) != 1) ||
358                     bswap_32(len) > G_MAXINT) {
359                         g_warning("read_data_str: Cache data (len) corrupted, read %d "
360                                   "of %d bytes at offset %ld\n", ni, sizeof(len), 
361                                   ftell(fp));
362                         return -1;
363                 }
364                 len = bswap_32(len);
365         }
366
367         if (len == 0)
368                 return 0;
369
370         if (len > (8<<20)) {
371                 /* allocating 8MB is too much. Something's going on */
372                 g_warning("read_data_str: Cache data (len) probably corrupted, asked for %d bytes.", len);
373                 return -1;
374         }
375         tmpstr = g_malloc(len + 1);
376
377         if(tmpstr == NULL) {
378                 g_warning("read_data_str: can't g_malloc %d bytes\n", len);
379                 return -1;
380         }
381
382         if ((ni = fread(tmpstr, 1, len, fp)) != len) {
383                 g_warning("read_data_str: Cache data corrupted, read %d of %d "
384                           "bytes at offset %ld\n", 
385                           ni, len, ftell(fp));
386                 g_free(tmpstr);
387                 return -1;
388         }
389         tmpstr[len] = 0;
390
391         if (conv != NULL) {
392                 *str = conv->convert(conv, tmpstr);
393                 g_free(tmpstr);
394         } else 
395                 *str = tmpstr;
396
397         return len;
398 }
399
400 gchar *strconv_strdup_convert(StringConverter *conv, gchar *srcstr)
401 {
402         return g_strdup(srcstr);
403 }
404
405 gchar *strconv_charset_convert(StringConverter *conv, gchar *srcstr)
406 {
407         CharsetConverter *charsetconv = (CharsetConverter *) conv;
408
409         return conv_codeset_strdup(srcstr, charsetconv->srccharset, charsetconv->dstcharset);
410 }
411
412 void strconv_charset_free(StringConverter *conv)
413 {
414         CharsetConverter *charsetconv = (CharsetConverter *) conv;
415
416         g_free(charsetconv->srccharset);
417         g_free(charsetconv->dstcharset);
418 }
419
420 MsgCache *msgcache_read_cache(FolderItem *item, const gchar *cache_file)
421 {
422         MsgCache *cache;
423         FILE *fp;
424         MsgInfo *msginfo;
425         MsgTmpFlags tmp_flags = 0;
426         gchar file_buf[BUFFSIZE];
427         guint32 num;
428         guint refnum;
429         gboolean error = FALSE;
430         StringConverter *conv = NULL;
431         gchar *srccharset = NULL;
432         const gchar *dstcharset = NULL;
433         gchar *ref = NULL;
434         guint memusage = 0;
435         gint tmp_len = 0;
436         
437         g_return_val_if_fail(cache_file != NULL, NULL);
438         g_return_val_if_fail(item != NULL, NULL);
439
440         swapping = TRUE;
441
442         /* In case we can't open the mark file with MARK_VERSION, check if we can open it with the
443          * swapped MARK_VERSION. As msgcache_open_data_file swaps it too, if this succeeds, 
444          * it means it's the old version (not little-endian) on a big-endian machine. The code has
445          * no effect on x86 as their file doesn't change. */
446
447         if ((fp = msgcache_open_data_file
448                 (cache_file, CACHE_VERSION, DATA_READ, file_buf, sizeof(file_buf))) == NULL) {
449                 if ((fp = msgcache_open_data_file
450                 (cache_file, bswap_32(CACHE_VERSION), DATA_READ, file_buf, sizeof(file_buf))) == NULL)
451                         return NULL;
452                 else
453                         swapping = FALSE;
454         }
455
456         debug_print("\tReading %sswapped message cache from %s...\n", swapping?"":"un", cache_file);
457
458         if (folder_has_parent_of_type(item, F_QUEUE)) {
459                 tmp_flags |= MSG_QUEUED;
460         } else if (folder_has_parent_of_type(item, F_DRAFT)) {
461                 tmp_flags |= MSG_DRAFT;
462         }
463
464         if (msgcache_read_cache_data_str(fp, &srccharset, NULL) < 0)
465                 return NULL;
466         dstcharset = CS_UTF_8;
467         if (srccharset == NULL || dstcharset == NULL) {
468                 conv = NULL;
469         } else if (strcmp(srccharset, dstcharset) == 0) {
470                 debug_print("using Noop Converter\n");
471
472                 conv = NULL;
473         } else {
474                 CharsetConverter *charsetconv;
475
476                 debug_print("using CharsetConverter\n");
477
478                 charsetconv = g_new0(CharsetConverter, 1);
479                 charsetconv->converter.convert = strconv_charset_convert;
480                 charsetconv->converter.free = strconv_charset_free;
481                 charsetconv->srccharset = g_strdup(srccharset);
482                 charsetconv->dstcharset = g_strdup(dstcharset);
483
484                 conv = (StringConverter *) charsetconv;
485         }
486         g_free(srccharset);
487
488         cache = msgcache_new();
489
490         while (fread(&num, sizeof(num), 1, fp) == 1) {
491                 if (swapping)
492                         num = bswap_32(num);
493
494                 msginfo = procmsg_msginfo_new();
495                 msginfo->msgnum = num;
496                 memusage += sizeof(MsgInfo);
497                 
498                 READ_CACHE_DATA_INT(msginfo->size, fp);
499                 READ_CACHE_DATA_INT(msginfo->mtime, fp);
500                 READ_CACHE_DATA_INT(msginfo->date_t, fp);
501                 READ_CACHE_DATA_INT(msginfo->flags.tmp_flags, fp);
502                                 
503                 READ_CACHE_DATA(msginfo->fromname, fp, memusage);
504
505                 READ_CACHE_DATA(msginfo->date, fp, memusage);
506                 READ_CACHE_DATA(msginfo->from, fp, memusage);
507                 READ_CACHE_DATA(msginfo->to, fp, memusage);
508                 READ_CACHE_DATA(msginfo->cc, fp, memusage);
509                 READ_CACHE_DATA(msginfo->newsgroups, fp, memusage);
510                 READ_CACHE_DATA(msginfo->subject, fp, memusage);
511                 READ_CACHE_DATA(msginfo->msgid, fp, memusage);
512                 READ_CACHE_DATA(msginfo->inreplyto, fp, memusage);
513                 READ_CACHE_DATA(msginfo->xref, fp, memusage);
514                 
515                 READ_CACHE_DATA_INT(msginfo->planned_download, fp);
516                 READ_CACHE_DATA_INT(msginfo->total_size, fp);
517                 READ_CACHE_DATA_INT(refnum, fp);
518                 
519                 for (; refnum != 0; refnum--) {
520                         ref = NULL;
521
522                         READ_CACHE_DATA(ref, fp, memusage);
523
524                         if (ref && strlen(ref))
525                                 msginfo->references =
526                                         g_slist_prepend(msginfo->references, ref);
527                 }
528                 if (msginfo->references)
529                         msginfo->references =
530                                 g_slist_reverse(msginfo->references);
531
532                 msginfo->folder = item;
533                 msginfo->flags.tmp_flags |= tmp_flags;
534
535                 g_hash_table_insert(cache->msgnum_table, &msginfo->msgnum, msginfo);
536                 if(msginfo->msgid)
537                         g_hash_table_insert(cache->msgid_table, msginfo->msgid, msginfo);
538         }
539 bail_err:
540         fclose(fp);
541
542         if (conv != NULL) {
543                 if (conv->free != NULL)
544                         conv->free(conv);
545                 g_free(conv);
546         }
547
548         if(error) {
549                 msgcache_destroy(cache);
550                 return NULL;
551         }
552
553         cache->last_access = time(NULL);
554         cache->memusage = memusage;
555
556         debug_print("done. (%d items read)\n", g_hash_table_size(cache->msgnum_table));
557         debug_print("Cache size: %d messages, %d byte\n", g_hash_table_size(cache->msgnum_table), cache->memusage);
558         return cache;
559 }
560
561 void msgcache_read_mark(MsgCache *cache, const gchar *mark_file)
562 {
563         FILE *fp;
564         MsgInfo *msginfo;
565         MsgPermFlags perm_flags;
566         guint32 num;
567         
568         swapping = TRUE;
569
570         /* In case we can't open the mark file with MARK_VERSION, check if we can open it with the
571          * swapped MARK_VERSION. As msgcache_open_data_file swaps it too, if this succeeds, 
572          * it means it's the old version (not little-endian) on a big-endian machine. The code has
573          * no effect on x86 as their file doesn't change. */
574
575         if ((fp = msgcache_open_data_file(mark_file, MARK_VERSION, DATA_READ, NULL, 0)) == NULL) {
576                 /* see if it isn't swapped ? */
577                 if ((fp = msgcache_open_data_file(mark_file, bswap_32(MARK_VERSION), DATA_READ, NULL, 0)) == NULL)
578                         return;
579                 else
580                         swapping = FALSE; /* yay */
581         }
582         debug_print("reading %sswapped mark file.\n", swapping?"":"un");
583         while (fread(&num, sizeof(num), 1, fp) == 1) {
584                 if (swapping)
585                         num = bswap_32(num);
586                 if (fread(&perm_flags, sizeof(perm_flags), 1, fp) != 1) break;
587                 if (swapping)
588                         perm_flags = bswap_32(perm_flags);
589                 msginfo = g_hash_table_lookup(cache->msgnum_table, &num);
590                 if(msginfo) {
591                         msginfo->flags.perm_flags = perm_flags;
592                 }
593         }       
594         fclose(fp);
595 }
596
597 void msgcache_write_cache(MsgInfo *msginfo, FILE *fp)
598 {
599         MsgTmpFlags flags = msginfo->flags.tmp_flags & MSG_CACHED_FLAG_MASK;
600         GSList *cur;
601
602         WRITE_CACHE_DATA_INT(msginfo->msgnum, fp);
603         WRITE_CACHE_DATA_INT(msginfo->size, fp);
604         WRITE_CACHE_DATA_INT(msginfo->mtime, fp);
605         WRITE_CACHE_DATA_INT(msginfo->date_t, fp);
606         WRITE_CACHE_DATA_INT(flags, fp);
607
608         WRITE_CACHE_DATA(msginfo->fromname, fp);
609
610         WRITE_CACHE_DATA(msginfo->date, fp);
611         WRITE_CACHE_DATA(msginfo->from, fp);
612         WRITE_CACHE_DATA(msginfo->to, fp);
613         WRITE_CACHE_DATA(msginfo->cc, fp);
614         WRITE_CACHE_DATA(msginfo->newsgroups, fp);
615         WRITE_CACHE_DATA(msginfo->subject, fp);
616         WRITE_CACHE_DATA(msginfo->msgid, fp);
617         WRITE_CACHE_DATA(msginfo->inreplyto, fp);
618         WRITE_CACHE_DATA(msginfo->xref, fp);
619         WRITE_CACHE_DATA_INT(msginfo->planned_download, fp);
620         WRITE_CACHE_DATA_INT(msginfo->total_size, fp);
621         
622         WRITE_CACHE_DATA_INT(g_slist_length(msginfo->references), fp);
623
624         for (cur = msginfo->references; cur != NULL; cur = cur->next) {
625                 WRITE_CACHE_DATA((gchar *)cur->data, fp);
626         }
627 }
628
629 static void msgcache_write_flags(MsgInfo *msginfo, FILE *fp)
630 {
631         MsgPermFlags flags = msginfo->flags.perm_flags;
632
633         WRITE_CACHE_DATA_INT(msginfo->msgnum, fp);
634         WRITE_CACHE_DATA_INT(flags, fp);
635 }
636
637 struct write_fps
638 {
639         FILE *cache_fp;
640         FILE *mark_fp;
641 };
642
643 static void msgcache_write_func(gpointer key, gpointer value, gpointer user_data)
644 {
645         MsgInfo *msginfo;
646         struct write_fps *write_fps;
647
648         msginfo = (MsgInfo *)value;
649         write_fps = user_data;
650
651         msgcache_write_cache(msginfo, write_fps->cache_fp);
652         msgcache_write_flags(msginfo, write_fps->mark_fp);
653 }
654
655 gint msgcache_write(const gchar *cache_file, const gchar *mark_file, MsgCache *cache)
656 {
657         struct write_fps write_fps;
658
659         g_return_val_if_fail(cache_file != NULL, -1);
660         g_return_val_if_fail(mark_file != NULL, -1);
661         g_return_val_if_fail(cache != NULL, -1);
662
663         write_fps.cache_fp = msgcache_open_data_file(cache_file, CACHE_VERSION,
664                 DATA_WRITE, NULL, 0);
665         if (write_fps.cache_fp == NULL)
666                 return -1;
667
668         WRITE_CACHE_DATA(CS_UTF_8, write_fps.cache_fp);
669
670         write_fps.mark_fp = msgcache_open_data_file(mark_file, MARK_VERSION,
671                 DATA_WRITE, NULL, 0);
672         if (write_fps.mark_fp == NULL) {
673                 fclose(write_fps.cache_fp);
674                 return -1;
675         }
676
677         debug_print("\tWriting message cache to %s and %s...\n", cache_file, mark_file);
678
679         if (change_file_mode_rw(write_fps.cache_fp, cache_file) < 0)
680                 FILE_OP_ERROR(cache_file, "chmod");
681
682         g_hash_table_foreach(cache->msgnum_table, msgcache_write_func, (gpointer)&write_fps);
683
684         fclose(write_fps.cache_fp);
685         fclose(write_fps.mark_fp);
686
687         cache->last_access = time(NULL);
688
689         debug_print("done.\n");
690         return 0;
691 }
692