2002-06-30 01:48:43 +02:00
|
|
|
/*
|
|
|
|
* Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
|
2006-01-13 18:24:38 +01:00
|
|
|
* Copyright (C) 1999-2006 Hiroyuki Yamamoto & The Sylpheed Claws Team
|
2002-06-30 01:48:43 +02:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2005-09-21 20:22:51 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2002-06-30 01:48:43 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "defs.h"
|
|
|
|
|
|
|
|
#include <glib.h>
|
2005-02-10 13:06:07 +01:00
|
|
|
#include <glib/gi18n.h>
|
2006-08-24 18:43:55 +02:00
|
|
|
#include <sys/mman.h>
|
2002-06-30 01:48:43 +02:00
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include "msgcache.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "procmsg.h"
|
2004-08-06 13:34:34 +02:00
|
|
|
#include "codeconv.h"
|
2006-05-23 19:42:52 +02:00
|
|
|
#include "timing.h"
|
2002-06-30 01:48:43 +02:00
|
|
|
|
2005-07-21 12:03:48 +02:00
|
|
|
#if G_BYTE_ORDER == G_BIG_ENDIAN
|
2005-07-08 05:17:39 +02:00
|
|
|
#define bswap_32(x) \
|
|
|
|
((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
|
|
|
|
(((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
|
2006-08-24 18:43:55 +02:00
|
|
|
|
|
|
|
static gboolean msgcache_use_mmap = FALSE;
|
2005-07-08 05:17:39 +02:00
|
|
|
#else
|
|
|
|
#define bswap_32(x) (x)
|
2006-08-24 18:43:55 +02:00
|
|
|
static gboolean msgcache_use_mmap = TRUE;
|
2005-07-08 05:17:39 +02:00
|
|
|
#endif
|
|
|
|
|
2006-08-24 18:43:55 +02:00
|
|
|
#define MMAP_TO_GUINT32(x) \
|
|
|
|
(((x[0]&0xff)) | \
|
|
|
|
((x[1]&0xff) << 8) | \
|
|
|
|
((x[2]&0xff) << 16) | \
|
|
|
|
((x[3]&0xff) << 24))
|
|
|
|
|
2005-07-08 05:17:39 +02:00
|
|
|
static gboolean swapping = TRUE;
|
|
|
|
|
2004-02-21 12:01:01 +01:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
DATA_READ,
|
|
|
|
DATA_WRITE,
|
|
|
|
DATA_APPEND
|
|
|
|
} DataOpenMode;
|
|
|
|
|
2002-06-30 01:48:43 +02:00
|
|
|
struct _MsgCache {
|
|
|
|
GHashTable *msgnum_table;
|
2002-07-10 15:11:25 +02:00
|
|
|
GHashTable *msgid_table;
|
2002-06-30 01:48:43 +02:00
|
|
|
guint memusage;
|
|
|
|
time_t last_access;
|
|
|
|
};
|
|
|
|
|
2004-08-06 13:34:34 +02:00
|
|
|
typedef struct _StringConverter StringConverter;
|
|
|
|
struct _StringConverter {
|
|
|
|
gchar *(*convert) (StringConverter *converter, gchar *srcstr);
|
|
|
|
void (*free) (StringConverter *converter);
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct _StrdupConverter StrdupConverter;
|
|
|
|
struct _StrdupConverter {
|
|
|
|
StringConverter converter;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct _CharsetConverter CharsetConverter;
|
|
|
|
struct _CharsetConverter {
|
|
|
|
StringConverter converter;
|
|
|
|
|
|
|
|
gchar *srccharset;
|
|
|
|
gchar *dstcharset;
|
|
|
|
};
|
|
|
|
|
2003-05-13 13:54:19 +02:00
|
|
|
MsgCache *msgcache_new(void)
|
2002-06-30 01:48:43 +02:00
|
|
|
{
|
|
|
|
MsgCache *cache;
|
|
|
|
|
|
|
|
cache = g_new0(MsgCache, 1),
|
2002-07-10 15:11:25 +02:00
|
|
|
cache->msgnum_table = g_hash_table_new(g_int_hash, g_int_equal);
|
|
|
|
cache->msgid_table = g_hash_table_new(g_str_hash, g_str_equal);
|
2002-06-30 01:48:43 +02:00
|
|
|
cache->last_access = time(NULL);
|
|
|
|
|
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean msgcache_msginfo_free_func(gpointer num, gpointer msginfo, gpointer user_data)
|
|
|
|
{
|
|
|
|
procmsg_msginfo_free((MsgInfo *)msginfo);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void msgcache_destroy(MsgCache *cache)
|
|
|
|
{
|
|
|
|
g_return_if_fail(cache != NULL);
|
|
|
|
|
|
|
|
g_hash_table_foreach_remove(cache->msgnum_table, msgcache_msginfo_free_func, NULL);
|
2002-07-10 15:11:25 +02:00
|
|
|
g_hash_table_destroy(cache->msgid_table);
|
2002-06-30 01:48:43 +02:00
|
|
|
g_hash_table_destroy(cache->msgnum_table);
|
|
|
|
g_free(cache);
|
|
|
|
}
|
|
|
|
|
|
|
|
void msgcache_add_msg(MsgCache *cache, MsgInfo *msginfo)
|
|
|
|
{
|
|
|
|
MsgInfo *newmsginfo;
|
|
|
|
|
|
|
|
g_return_if_fail(cache != NULL);
|
|
|
|
g_return_if_fail(msginfo != NULL);
|
|
|
|
|
|
|
|
newmsginfo = procmsg_msginfo_new_ref(msginfo);
|
2002-07-10 15:11:25 +02:00
|
|
|
g_hash_table_insert(cache->msgnum_table, &newmsginfo->msgnum, newmsginfo);
|
|
|
|
if(newmsginfo->msgid != NULL)
|
|
|
|
g_hash_table_insert(cache->msgid_table, newmsginfo->msgid, newmsginfo);
|
2002-06-30 01:48:43 +02:00
|
|
|
cache->memusage += procmsg_msginfo_memusage(msginfo);
|
|
|
|
cache->last_access = time(NULL);
|
|
|
|
|
2004-05-10 12:22:28 +02:00
|
|
|
debug_print("Cache size: %d messages, %d bytes\n", g_hash_table_size(cache->msgnum_table), cache->memusage);
|
2002-06-30 01:48:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void msgcache_remove_msg(MsgCache *cache, guint msgnum)
|
|
|
|
{
|
|
|
|
MsgInfo *msginfo;
|
|
|
|
|
|
|
|
g_return_if_fail(cache != NULL);
|
|
|
|
g_return_if_fail(msgnum > 0);
|
|
|
|
|
2002-07-10 15:11:25 +02:00
|
|
|
msginfo = (MsgInfo *) g_hash_table_lookup(cache->msgnum_table, &msgnum);
|
2002-06-30 01:48:43 +02:00
|
|
|
if(!msginfo)
|
|
|
|
return;
|
|
|
|
|
|
|
|
cache->memusage -= procmsg_msginfo_memusage(msginfo);
|
2002-07-10 15:11:25 +02:00
|
|
|
if(msginfo->msgid)
|
|
|
|
g_hash_table_remove(cache->msgid_table, msginfo->msgid);
|
|
|
|
g_hash_table_remove(cache->msgnum_table, &msginfo->msgnum);
|
2002-06-30 01:48:43 +02:00
|
|
|
procmsg_msginfo_free(msginfo);
|
|
|
|
cache->last_access = time(NULL);
|
|
|
|
|
2002-08-15 09:38:17 +02:00
|
|
|
debug_print("Cache size: %d messages, %d byte\n", g_hash_table_size(cache->msgnum_table), cache->memusage);
|
2002-06-30 01:48:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void msgcache_update_msg(MsgCache *cache, MsgInfo *msginfo)
|
|
|
|
{
|
|
|
|
MsgInfo *oldmsginfo, *newmsginfo;
|
|
|
|
|
|
|
|
g_return_if_fail(cache != NULL);
|
|
|
|
g_return_if_fail(msginfo != NULL);
|
|
|
|
|
2002-07-10 15:11:25 +02:00
|
|
|
oldmsginfo = g_hash_table_lookup(cache->msgnum_table, &msginfo->msgnum);
|
2004-08-06 13:34:34 +02:00
|
|
|
if(oldmsginfo && oldmsginfo->msgid)
|
2002-07-10 15:11:25 +02:00
|
|
|
g_hash_table_remove(cache->msgid_table, oldmsginfo->msgid);
|
2004-07-27 21:57:57 +02:00
|
|
|
if (oldmsginfo) {
|
2002-07-10 15:11:25 +02:00
|
|
|
g_hash_table_remove(cache->msgnum_table, &oldmsginfo->msgnum);
|
2004-07-27 21:57:57 +02:00
|
|
|
cache->memusage -= procmsg_msginfo_memusage(oldmsginfo);
|
2004-08-06 13:34:34 +02:00
|
|
|
procmsg_msginfo_free(oldmsginfo);
|
2004-07-27 21:57:57 +02:00
|
|
|
}
|
2002-06-30 01:48:43 +02:00
|
|
|
|
|
|
|
newmsginfo = procmsg_msginfo_new_ref(msginfo);
|
2002-07-10 15:11:25 +02:00
|
|
|
g_hash_table_insert(cache->msgnum_table, &newmsginfo->msgnum, newmsginfo);
|
|
|
|
if(newmsginfo->msgid)
|
|
|
|
g_hash_table_insert(cache->msgid_table, newmsginfo->msgid, newmsginfo);
|
2002-06-30 01:48:43 +02:00
|
|
|
cache->memusage += procmsg_msginfo_memusage(newmsginfo);
|
|
|
|
cache->last_access = time(NULL);
|
|
|
|
|
2002-08-15 09:38:17 +02:00
|
|
|
debug_print("Cache size: %d messages, %d byte\n", g_hash_table_size(cache->msgnum_table), cache->memusage);
|
2002-06-30 01:48:43 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-02-21 12:01:01 +01:00
|
|
|
MsgInfo *msgcache_get_msg(MsgCache *cache, guint num)
|
|
|
|
{
|
|
|
|
MsgInfo *msginfo;
|
|
|
|
|
|
|
|
g_return_val_if_fail(cache != NULL, NULL);
|
|
|
|
|
|
|
|
msginfo = g_hash_table_lookup(cache->msgnum_table, &num);
|
|
|
|
if(!msginfo)
|
|
|
|
return NULL;
|
|
|
|
cache->last_access = time(NULL);
|
|
|
|
|
|
|
|
return procmsg_msginfo_new_ref(msginfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
msginfo = g_hash_table_lookup(cache->msgid_table, msgid);
|
|
|
|
if(!msginfo)
|
|
|
|
return NULL;
|
|
|
|
cache->last_access = time(NULL);
|
|
|
|
|
|
|
|
return procmsg_msginfo_new_ref(msginfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void msgcache_get_msg_list_func(gpointer key, gpointer value, gpointer user_data)
|
|
|
|
{
|
|
|
|
MsgInfoList **listptr = user_data;
|
|
|
|
MsgInfo *msginfo = value;
|
|
|
|
|
|
|
|
*listptr = g_slist_prepend(*listptr, procmsg_msginfo_new_ref(msginfo));
|
|
|
|
}
|
|
|
|
|
|
|
|
MsgInfoList *msgcache_get_msg_list(MsgCache *cache)
|
|
|
|
{
|
|
|
|
MsgInfoList *msg_list = NULL;
|
2006-05-23 19:42:52 +02:00
|
|
|
START_TIMING("msgcache_get_msg_list");
|
2004-02-21 12:01:01 +01:00
|
|
|
g_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);
|
|
|
|
|
|
|
|
msg_list = g_slist_reverse(msg_list);
|
2006-05-23 19:42:52 +02:00
|
|
|
END_TIMING();
|
2004-02-21 12:01:01 +01:00
|
|
|
return msg_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
time_t msgcache_get_last_access_time(MsgCache *cache)
|
|
|
|
{
|
|
|
|
g_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);
|
|
|
|
|
|
|
|
return cache->memusage;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Cache saving functions
|
|
|
|
*/
|
|
|
|
|
2005-05-31 21:00:21 +02:00
|
|
|
#define READ_CACHE_DATA(data, fp, total_len) \
|
2004-02-21 12:01:01 +01:00
|
|
|
{ \
|
2005-05-31 21:00:21 +02:00
|
|
|
if ((tmp_len = msgcache_read_cache_data_str(fp, &data, conv)) < 0) { \
|
2004-02-21 12:01:01 +01:00
|
|
|
procmsg_msginfo_free(msginfo); \
|
|
|
|
error = TRUE; \
|
2006-06-07 18:41:15 +02:00
|
|
|
goto bail_err; \
|
2004-02-21 12:01:01 +01:00
|
|
|
} \
|
2005-05-31 21:00:21 +02:00
|
|
|
total_len += tmp_len; \
|
2004-02-21 12:01:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#define READ_CACHE_DATA_INT(n, fp) \
|
|
|
|
{ \
|
2004-05-22 09:27:30 +02:00
|
|
|
guint32 idata; \
|
2005-01-14 08:52:51 +01:00
|
|
|
size_t ni; \
|
2004-05-22 09:27:30 +02:00
|
|
|
\
|
2005-07-08 05:17:39 +02:00
|
|
|
if ((ni = fread(&idata, sizeof(idata), 1, fp)) != 1) { \
|
2005-01-14 14:05:38 +01:00
|
|
|
g_warning("read_int: Cache data corrupted, read %d of %d at " \
|
2005-06-01 19:44:37 +02:00
|
|
|
"offset %ld\n", ni, sizeof(idata), ftell(fp)); \
|
2004-02-21 12:01:01 +01:00
|
|
|
procmsg_msginfo_free(msginfo); \
|
|
|
|
error = TRUE; \
|
2006-06-07 18:41:15 +02:00
|
|
|
goto bail_err; \
|
2004-05-22 09:27:30 +02:00
|
|
|
} else \
|
2005-07-08 05:17:39 +02:00
|
|
|
n = swapping ? bswap_32(idata) : (idata);\
|
2004-02-21 12:01:01 +01:00
|
|
|
}
|
|
|
|
|
2006-08-24 18:43:55 +02:00
|
|
|
#define GET_CACHE_DATA_INT(n) \
|
|
|
|
{ \
|
|
|
|
guint32 idata = MMAP_TO_GUINT32(walk_data); \
|
|
|
|
n = swapping ? bswap_32(idata) : (idata);\
|
|
|
|
walk_data += 4; rem_len -= 4; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define GET_CACHE_DATA(data, total_len) \
|
|
|
|
{ \
|
|
|
|
GET_CACHE_DATA_INT(tmp_len); \
|
|
|
|
if ((tmp_len = msgcache_get_cache_data_str(walk_data, &data, tmp_len, conv)) < 0) { \
|
|
|
|
printf("error at rem_len:%d\n", rem_len);\
|
|
|
|
procmsg_msginfo_free(msginfo); \
|
|
|
|
error = TRUE; \
|
|
|
|
goto bail_err; \
|
|
|
|
} \
|
|
|
|
total_len += tmp_len; \
|
|
|
|
walk_data += tmp_len; rem_len -= tmp_len; \
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-05-22 09:27:30 +02:00
|
|
|
#define WRITE_CACHE_DATA_INT(n, fp) \
|
|
|
|
{ \
|
|
|
|
guint32 idata; \
|
|
|
|
\
|
2005-07-08 05:17:39 +02:00
|
|
|
idata = (guint32)bswap_32(n); \
|
2006-08-24 18:43:55 +02:00
|
|
|
if (fwrite(&idata, sizeof(idata), 1, fp) != 1) \
|
|
|
|
w_err = 1; \
|
2004-05-22 09:27:30 +02:00
|
|
|
}
|
2004-02-21 12:01:01 +01:00
|
|
|
|
|
|
|
#define WRITE_CACHE_DATA(data, fp) \
|
|
|
|
{ \
|
2004-05-22 09:27:30 +02:00
|
|
|
size_t len; \
|
2004-02-21 12:01:01 +01:00
|
|
|
if (data == NULL) \
|
|
|
|
len = 0; \
|
|
|
|
else \
|
|
|
|
len = strlen(data); \
|
|
|
|
WRITE_CACHE_DATA_INT(len, fp); \
|
2006-08-24 18:43:55 +02:00
|
|
|
if (w_err == 0 && len > 0) { \
|
|
|
|
if (fwrite(data, 1, len, fp) != len) \
|
|
|
|
w_err = 1; \
|
2004-02-21 12:01:01 +01:00
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2005-03-30 22:29:56 +02:00
|
|
|
static FILE *msgcache_open_data_file(const gchar *file, guint version,
|
2004-02-21 12:01:01 +01:00
|
|
|
DataOpenMode mode,
|
|
|
|
gchar *buf, size_t buf_size)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
2005-03-30 12:13:48 +02:00
|
|
|
gint32 data_ver;
|
2004-02-21 12:01:01 +01:00
|
|
|
|
|
|
|
g_return_val_if_fail(file != NULL, NULL);
|
|
|
|
|
|
|
|
if (mode == DATA_WRITE) {
|
2006-08-24 18:43:55 +02:00
|
|
|
int w_err = 0;
|
2005-08-19 11:30:30 +02:00
|
|
|
if ((fp = g_fopen(file, "wb")) == NULL) {
|
2004-02-21 12:01:01 +01:00
|
|
|
FILE_OP_ERROR(file, "fopen");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (change_file_mode_rw(fp, file) < 0)
|
|
|
|
FILE_OP_ERROR(file, "chmod");
|
|
|
|
|
|
|
|
WRITE_CACHE_DATA_INT(version, fp);
|
2006-08-24 18:43:55 +02:00
|
|
|
if (w_err != 0) {
|
|
|
|
g_warning("failed to write int\n");
|
|
|
|
fclose(fp);
|
|
|
|
return NULL;
|
|
|
|
}
|
2004-02-21 12:01:01 +01:00
|
|
|
return fp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check version */
|
2005-08-19 11:30:30 +02:00
|
|
|
if ((fp = g_fopen(file, "rb")) == NULL)
|
2005-03-30 22:29:56 +02:00
|
|
|
debug_print("Mark/Cache file '%s' not found\n", file);
|
2004-02-21 12:01:01 +01:00
|
|
|
else {
|
|
|
|
if (buf && buf_size > 0)
|
|
|
|
setvbuf(fp, buf, _IOFBF, buf_size);
|
|
|
|
if (fread(&data_ver, sizeof(data_ver), 1, fp) != 1 ||
|
2005-07-08 05:17:39 +02:00
|
|
|
version != bswap_32(data_ver)) {
|
|
|
|
g_message("%s: Mark/Cache version is different (%u != %u).\n",
|
|
|
|
file, bswap_32(data_ver), version);
|
2004-02-21 12:01:01 +01:00
|
|
|
fclose(fp);
|
|
|
|
fp = NULL;
|
|
|
|
}
|
2005-07-08 05:17:39 +02:00
|
|
|
data_ver = bswap_32(data_ver);
|
2004-02-21 12:01:01 +01:00
|
|
|
}
|
2005-05-31 21:00:21 +02:00
|
|
|
|
2004-02-21 12:01:01 +01:00
|
|
|
if (mode == DATA_READ)
|
|
|
|
return fp;
|
|
|
|
|
|
|
|
if (fp) {
|
|
|
|
/* reopen with append mode */
|
|
|
|
fclose(fp);
|
2005-08-19 11:30:30 +02:00
|
|
|
if ((fp = g_fopen(file, "ab")) == NULL)
|
2004-02-21 12:01:01 +01:00
|
|
|
FILE_OP_ERROR(file, "fopen");
|
|
|
|
} else {
|
|
|
|
/* open with overwrite mode if mark file doesn't exist or
|
|
|
|
version is different */
|
|
|
|
fp = msgcache_open_data_file(file, version, DATA_WRITE, buf,
|
|
|
|
buf_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
return fp;
|
|
|
|
}
|
|
|
|
|
2005-01-14 14:05:38 +01:00
|
|
|
static gint msgcache_read_cache_data_str(FILE *fp, gchar **str,
|
|
|
|
StringConverter *conv)
|
2002-06-30 01:48:43 +02:00
|
|
|
{
|
2005-01-14 14:05:38 +01:00
|
|
|
gchar *tmpstr = NULL;
|
|
|
|
size_t ni;
|
|
|
|
guint32 len;
|
|
|
|
|
|
|
|
*str = NULL;
|
2005-07-08 05:17:39 +02:00
|
|
|
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),
|
|
|
|
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),
|
|
|
|
ftell(fp));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
len = bswap_32(len);
|
2005-01-14 14:05:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
return 0;
|
|
|
|
|
2006-08-21 18:22:31 +02:00
|
|
|
tmpstr = g_try_malloc(len + 1);
|
2005-01-14 14:05:38 +01:00
|
|
|
|
2006-06-11 15:36:35 +02:00
|
|
|
if(tmpstr == NULL) {
|
2006-08-21 18:22:31 +02:00
|
|
|
g_warning("read_data_str: can't g_malloc %d bytes - cache data probably corrupted.\n", len);
|
2006-06-11 15:36:35 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-01-14 14:05:38 +01:00
|
|
|
if ((ni = fread(tmpstr, 1, len, fp)) != len) {
|
|
|
|
g_warning("read_data_str: Cache data corrupted, read %d of %d "
|
2005-06-01 19:44:37 +02:00
|
|
|
"bytes at offset %ld\n",
|
2005-01-14 14:05:38 +01:00
|
|
|
ni, len, ftell(fp));
|
|
|
|
g_free(tmpstr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
tmpstr[len] = 0;
|
|
|
|
|
|
|
|
if (conv != NULL) {
|
|
|
|
*str = conv->convert(conv, tmpstr);
|
|
|
|
g_free(tmpstr);
|
|
|
|
} else
|
|
|
|
*str = tmpstr;
|
|
|
|
|
2005-05-31 21:00:21 +02:00
|
|
|
return len;
|
2002-06-30 01:48:43 +02:00
|
|
|
}
|
|
|
|
|
2006-08-24 18:43:55 +02:00
|
|
|
static gint msgcache_get_cache_data_str(gchar *src, gchar **str, gint len,
|
|
|
|
StringConverter *conv)
|
|
|
|
{
|
|
|
|
gchar *tmpstr = NULL;
|
|
|
|
|
|
|
|
*str = NULL;
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
tmpstr[len] = 0;
|
|
|
|
|
|
|
|
if (conv != NULL) {
|
|
|
|
*str = conv->convert(conv, tmpstr);
|
|
|
|
g_free(tmpstr);
|
|
|
|
} else
|
|
|
|
*str = tmpstr;
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2004-08-06 13:34:34 +02:00
|
|
|
gchar *strconv_strdup_convert(StringConverter *conv, gchar *srcstr)
|
|
|
|
{
|
|
|
|
return g_strdup(srcstr);
|
|
|
|
}
|
|
|
|
|
|
|
|
gchar *strconv_charset_convert(StringConverter *conv, gchar *srcstr)
|
|
|
|
{
|
|
|
|
CharsetConverter *charsetconv = (CharsetConverter *) conv;
|
|
|
|
|
|
|
|
return conv_codeset_strdup(srcstr, charsetconv->srccharset, charsetconv->dstcharset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void strconv_charset_free(StringConverter *conv)
|
|
|
|
{
|
|
|
|
CharsetConverter *charsetconv = (CharsetConverter *) conv;
|
|
|
|
|
|
|
|
g_free(charsetconv->srccharset);
|
|
|
|
g_free(charsetconv->dstcharset);
|
|
|
|
}
|
|
|
|
|
2002-06-30 01:48:43 +02:00
|
|
|
MsgCache *msgcache_read_cache(FolderItem *item, const gchar *cache_file)
|
|
|
|
{
|
|
|
|
MsgCache *cache;
|
|
|
|
FILE *fp;
|
|
|
|
MsgInfo *msginfo;
|
2002-09-10 12:38:13 +02:00
|
|
|
MsgTmpFlags tmp_flags = 0;
|
2002-06-30 01:48:43 +02:00
|
|
|
gchar file_buf[BUFFSIZE];
|
2005-03-30 12:13:48 +02:00
|
|
|
guint32 num;
|
2005-03-30 22:29:56 +02:00
|
|
|
guint refnum;
|
2002-06-30 01:48:43 +02:00
|
|
|
gboolean error = FALSE;
|
2004-08-06 13:34:34 +02:00
|
|
|
StringConverter *conv = NULL;
|
|
|
|
gchar *srccharset = NULL;
|
|
|
|
const gchar *dstcharset = NULL;
|
2005-05-31 21:00:21 +02:00
|
|
|
gchar *ref = NULL;
|
|
|
|
guint memusage = 0;
|
2006-08-24 18:43:55 +02:00
|
|
|
gint tmp_len = 0, map_len = -1;
|
|
|
|
char *cache_data = NULL;
|
|
|
|
struct stat st;
|
|
|
|
|
2002-06-30 01:48:43 +02:00
|
|
|
g_return_val_if_fail(cache_file != NULL, NULL);
|
|
|
|
g_return_val_if_fail(item != NULL, NULL);
|
|
|
|
|
2005-07-08 05:17:39 +02:00
|
|
|
swapping = TRUE;
|
|
|
|
|
|
|
|
/* In case we can't open the mark file with MARK_VERSION, check if we can open it with the
|
|
|
|
* swapped MARK_VERSION. As msgcache_open_data_file swaps it too, if this succeeds,
|
|
|
|
* it means it's the old version (not little-endian) on a big-endian machine. The code has
|
|
|
|
* no effect on x86 as their file doesn't change. */
|
|
|
|
|
2004-02-21 12:01:01 +01:00
|
|
|
if ((fp = msgcache_open_data_file
|
2005-07-08 05:17:39 +02:00
|
|
|
(cache_file, CACHE_VERSION, DATA_READ, file_buf, sizeof(file_buf))) == NULL) {
|
|
|
|
if ((fp = msgcache_open_data_file
|
|
|
|
(cache_file, bswap_32(CACHE_VERSION), DATA_READ, file_buf, sizeof(file_buf))) == NULL)
|
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
swapping = FALSE;
|
|
|
|
}
|
2002-06-30 01:48:43 +02:00
|
|
|
|
2005-07-08 05:17:39 +02:00
|
|
|
debug_print("\tReading %sswapped message cache from %s...\n", swapping?"":"un", cache_file);
|
2002-06-30 01:48:43 +02:00
|
|
|
|
2005-08-07 15:39:27 +02:00
|
|
|
if (folder_has_parent_of_type(item, F_QUEUE)) {
|
2002-09-10 12:38:13 +02:00
|
|
|
tmp_flags |= MSG_QUEUED;
|
2005-08-07 15:39:27 +02:00
|
|
|
} else if (folder_has_parent_of_type(item, F_DRAFT)) {
|
2002-09-10 12:38:13 +02:00
|
|
|
tmp_flags |= MSG_DRAFT;
|
|
|
|
}
|
|
|
|
|
2004-08-06 13:34:34 +02:00
|
|
|
if (msgcache_read_cache_data_str(fp, &srccharset, NULL) < 0)
|
|
|
|
return NULL;
|
|
|
|
dstcharset = CS_UTF_8;
|
|
|
|
if (srccharset == NULL || dstcharset == NULL) {
|
|
|
|
conv = NULL;
|
|
|
|
} else if (strcmp(srccharset, dstcharset) == 0) {
|
2005-10-10 18:56:26 +02:00
|
|
|
debug_print("using Noop Converter\n");
|
2004-08-06 13:34:34 +02:00
|
|
|
|
2005-10-10 18:56:26 +02:00
|
|
|
conv = NULL;
|
2004-08-06 13:34:34 +02:00
|
|
|
} else {
|
|
|
|
CharsetConverter *charsetconv;
|
|
|
|
|
|
|
|
debug_print("using CharsetConverter\n");
|
|
|
|
|
|
|
|
charsetconv = g_new0(CharsetConverter, 1);
|
|
|
|
charsetconv->converter.convert = strconv_charset_convert;
|
|
|
|
charsetconv->converter.free = strconv_charset_free;
|
|
|
|
charsetconv->srccharset = g_strdup(srccharset);
|
|
|
|
charsetconv->dstcharset = g_strdup(dstcharset);
|
|
|
|
|
|
|
|
conv = (StringConverter *) charsetconv;
|
|
|
|
}
|
|
|
|
g_free(srccharset);
|
|
|
|
|
|
|
|
cache = msgcache_new();
|
2002-06-30 01:48:43 +02:00
|
|
|
|
2006-08-24 18:43:55 +02:00
|
|
|
if (msgcache_use_mmap == TRUE) {
|
|
|
|
if (fstat(fileno(fp), &st) >= 0)
|
|
|
|
map_len = st.st_size;
|
|
|
|
else
|
|
|
|
map_len = -1;
|
|
|
|
if (map_len > 0)
|
|
|
|
cache_data = mmap(NULL, map_len, PROT_READ, MAP_PRIVATE, fileno(fp), 0);
|
|
|
|
} else {
|
|
|
|
cache_data = NULL;
|
|
|
|
}
|
|
|
|
if (cache_data != NULL && cache_data != MAP_FAILED) {
|
|
|
|
int rem_len = map_len-ftell(fp);
|
|
|
|
char *walk_data = cache_data+ftell(fp);
|
|
|
|
|
|
|
|
while(rem_len > 0) {
|
|
|
|
GET_CACHE_DATA_INT(num);
|
|
|
|
|
|
|
|
msginfo = procmsg_msginfo_new();
|
|
|
|
msginfo->msgnum = num;
|
|
|
|
memusage += sizeof(MsgInfo);
|
|
|
|
|
|
|
|
GET_CACHE_DATA_INT(msginfo->size);
|
|
|
|
GET_CACHE_DATA_INT(msginfo->mtime);
|
|
|
|
GET_CACHE_DATA_INT(msginfo->date_t);
|
|
|
|
GET_CACHE_DATA_INT(msginfo->flags.tmp_flags);
|
|
|
|
|
|
|
|
GET_CACHE_DATA(msginfo->fromname, memusage);
|
|
|
|
|
|
|
|
GET_CACHE_DATA(msginfo->date, memusage);
|
|
|
|
GET_CACHE_DATA(msginfo->from, memusage);
|
|
|
|
GET_CACHE_DATA(msginfo->to, memusage);
|
|
|
|
GET_CACHE_DATA(msginfo->cc, memusage);
|
|
|
|
GET_CACHE_DATA(msginfo->newsgroups, memusage);
|
|
|
|
GET_CACHE_DATA(msginfo->subject, memusage);
|
|
|
|
GET_CACHE_DATA(msginfo->msgid, memusage);
|
|
|
|
GET_CACHE_DATA(msginfo->inreplyto, memusage);
|
|
|
|
GET_CACHE_DATA(msginfo->xref, memusage);
|
|
|
|
|
|
|
|
GET_CACHE_DATA_INT(msginfo->planned_download);
|
|
|
|
GET_CACHE_DATA_INT(msginfo->total_size);
|
|
|
|
GET_CACHE_DATA_INT(refnum);
|
|
|
|
|
|
|
|
for (; refnum != 0; refnum--) {
|
|
|
|
ref = NULL;
|
|
|
|
|
|
|
|
GET_CACHE_DATA(ref, memusage);
|
|
|
|
|
|
|
|
if (ref && *ref)
|
|
|
|
msginfo->references =
|
|
|
|
g_slist_prepend(msginfo->references, ref);
|
|
|
|
}
|
|
|
|
if (msginfo->references)
|
|
|
|
msginfo->references =
|
|
|
|
g_slist_reverse(msginfo->references);
|
2005-03-30 22:29:56 +02:00
|
|
|
|
2006-08-24 18:43:55 +02:00
|
|
|
msginfo->folder = item;
|
|
|
|
msginfo->flags.tmp_flags |= tmp_flags;
|
2005-03-31 17:31:13 +02:00
|
|
|
|
2006-08-24 18:43:55 +02:00
|
|
|
g_hash_table_insert(cache->msgnum_table, &msginfo->msgnum, msginfo);
|
|
|
|
if(msginfo->msgid)
|
|
|
|
g_hash_table_insert(cache->msgid_table, msginfo->msgid, msginfo);
|
2005-03-30 22:29:56 +02:00
|
|
|
}
|
2006-08-24 18:43:55 +02:00
|
|
|
|
|
|
|
munmap(cache_data, map_len);
|
|
|
|
} else {
|
|
|
|
while (fread(&num, sizeof(num), 1, fp) == 1) {
|
|
|
|
if (swapping)
|
|
|
|
num = bswap_32(num);
|
|
|
|
|
|
|
|
msginfo = procmsg_msginfo_new();
|
|
|
|
msginfo->msgnum = num;
|
|
|
|
memusage += sizeof(MsgInfo);
|
|
|
|
|
|
|
|
READ_CACHE_DATA_INT(msginfo->size, fp);
|
|
|
|
READ_CACHE_DATA_INT(msginfo->mtime, fp);
|
|
|
|
READ_CACHE_DATA_INT(msginfo->date_t, fp);
|
|
|
|
READ_CACHE_DATA_INT(msginfo->flags.tmp_flags, fp);
|
|
|
|
|
|
|
|
READ_CACHE_DATA(msginfo->fromname, fp, memusage);
|
|
|
|
|
|
|
|
READ_CACHE_DATA(msginfo->date, fp, memusage);
|
|
|
|
READ_CACHE_DATA(msginfo->from, fp, memusage);
|
|
|
|
READ_CACHE_DATA(msginfo->to, fp, memusage);
|
|
|
|
READ_CACHE_DATA(msginfo->cc, fp, memusage);
|
|
|
|
READ_CACHE_DATA(msginfo->newsgroups, fp, memusage);
|
|
|
|
READ_CACHE_DATA(msginfo->subject, fp, memusage);
|
|
|
|
READ_CACHE_DATA(msginfo->msgid, fp, memusage);
|
|
|
|
READ_CACHE_DATA(msginfo->inreplyto, fp, memusage);
|
|
|
|
READ_CACHE_DATA(msginfo->xref, fp, memusage);
|
|
|
|
|
|
|
|
READ_CACHE_DATA_INT(msginfo->planned_download, fp);
|
|
|
|
READ_CACHE_DATA_INT(msginfo->total_size, fp);
|
|
|
|
READ_CACHE_DATA_INT(refnum, fp);
|
|
|
|
|
|
|
|
for (; refnum != 0; refnum--) {
|
|
|
|
ref = NULL;
|
|
|
|
|
|
|
|
READ_CACHE_DATA(ref, fp, memusage);
|
|
|
|
|
|
|
|
if (ref && *ref)
|
|
|
|
msginfo->references =
|
|
|
|
g_slist_prepend(msginfo->references, ref);
|
|
|
|
}
|
|
|
|
if (msginfo->references)
|
|
|
|
msginfo->references =
|
|
|
|
g_slist_reverse(msginfo->references);
|
2005-03-30 22:29:56 +02:00
|
|
|
|
2006-08-24 18:43:55 +02:00
|
|
|
msginfo->folder = item;
|
|
|
|
msginfo->flags.tmp_flags |= tmp_flags;
|
2002-06-30 01:48:43 +02:00
|
|
|
|
2006-08-24 18:43:55 +02:00
|
|
|
g_hash_table_insert(cache->msgnum_table, &msginfo->msgnum, msginfo);
|
|
|
|
if(msginfo->msgid)
|
|
|
|
g_hash_table_insert(cache->msgid_table, msginfo->msgid, msginfo);
|
|
|
|
}
|
2002-06-30 01:48:43 +02:00
|
|
|
}
|
2006-06-07 18:41:15 +02:00
|
|
|
bail_err:
|
2002-06-30 01:48:43 +02:00
|
|
|
fclose(fp);
|
|
|
|
|
2004-08-06 13:34:34 +02:00
|
|
|
if (conv != NULL) {
|
|
|
|
if (conv->free != NULL)
|
|
|
|
conv->free(conv);
|
|
|
|
g_free(conv);
|
|
|
|
}
|
|
|
|
|
2004-11-23 12:02:29 +01:00
|
|
|
if(error) {
|
|
|
|
msgcache_destroy(cache);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-06-30 01:48:43 +02:00
|
|
|
cache->last_access = time(NULL);
|
2005-05-31 21:00:21 +02:00
|
|
|
cache->memusage = memusage;
|
2002-06-30 01:48:43 +02:00
|
|
|
|
2002-08-15 09:38:17 +02:00
|
|
|
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);
|
2002-06-30 01:48:43 +02:00
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
void msgcache_read_mark(MsgCache *cache, const gchar *mark_file)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
MsgInfo *msginfo;
|
|
|
|
MsgPermFlags perm_flags;
|
2005-03-30 12:13:48 +02:00
|
|
|
guint32 num;
|
2006-08-24 18:43:55 +02:00
|
|
|
gint map_len = -1;
|
|
|
|
char *cache_data = NULL;
|
|
|
|
struct stat st;
|
2005-07-08 05:17:39 +02:00
|
|
|
|
|
|
|
swapping = TRUE;
|
|
|
|
|
|
|
|
/* In case we can't open the mark file with MARK_VERSION, check if we can open it with the
|
|
|
|
* swapped MARK_VERSION. As msgcache_open_data_file swaps it too, if this succeeds,
|
|
|
|
* it means it's the old version (not little-endian) on a big-endian machine. The code has
|
|
|
|
* no effect on x86 as their file doesn't change. */
|
|
|
|
|
|
|
|
if ((fp = msgcache_open_data_file(mark_file, MARK_VERSION, DATA_READ, NULL, 0)) == NULL) {
|
|
|
|
/* see if it isn't swapped ? */
|
|
|
|
if ((fp = msgcache_open_data_file(mark_file, bswap_32(MARK_VERSION), DATA_READ, NULL, 0)) == NULL)
|
|
|
|
return;
|
|
|
|
else
|
|
|
|
swapping = FALSE; /* yay */
|
|
|
|
}
|
|
|
|
debug_print("reading %sswapped mark file.\n", swapping?"":"un");
|
2006-08-24 18:43:55 +02:00
|
|
|
|
|
|
|
if (msgcache_use_mmap) {
|
|
|
|
if (fstat(fileno(fp), &st) >= 0)
|
|
|
|
map_len = st.st_size;
|
|
|
|
else
|
|
|
|
map_len = -1;
|
|
|
|
if (map_len > 0)
|
|
|
|
cache_data = mmap(NULL, map_len, PROT_READ, MAP_PRIVATE, fileno(fp), 0);
|
|
|
|
} else {
|
|
|
|
cache_data = NULL;
|
|
|
|
}
|
|
|
|
if (cache_data != NULL && cache_data != MAP_FAILED) {
|
|
|
|
int rem_len = map_len-ftell(fp);
|
|
|
|
char *walk_data = cache_data+ftell(fp);
|
|
|
|
|
|
|
|
while(rem_len > 0) {
|
|
|
|
GET_CACHE_DATA_INT(num);
|
|
|
|
GET_CACHE_DATA_INT(perm_flags);
|
|
|
|
msginfo = g_hash_table_lookup(cache->msgnum_table, &num);
|
|
|
|
if(msginfo) {
|
|
|
|
msginfo->flags.perm_flags = perm_flags;
|
|
|
|
}
|
2002-06-30 01:48:43 +02:00
|
|
|
}
|
2006-08-24 18:43:55 +02:00
|
|
|
munmap(cache_data, map_len);
|
|
|
|
} 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 (swapping)
|
|
|
|
perm_flags = bswap_32(perm_flags);
|
|
|
|
msginfo = g_hash_table_lookup(cache->msgnum_table, &num);
|
|
|
|
if(msginfo) {
|
|
|
|
msginfo->flags.perm_flags = perm_flags;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-06-30 01:48:43 +02:00
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
2006-08-24 18:43:55 +02:00
|
|
|
static int msgcache_write_cache(MsgInfo *msginfo, FILE *fp)
|
2002-06-30 01:48:43 +02:00
|
|
|
{
|
|
|
|
MsgTmpFlags flags = msginfo->flags.tmp_flags & MSG_CACHED_FLAG_MASK;
|
2005-03-30 22:29:56 +02:00
|
|
|
GSList *cur;
|
2006-08-24 18:43:55 +02:00
|
|
|
int w_err = 0;
|
2002-06-30 01:48:43 +02:00
|
|
|
|
|
|
|
WRITE_CACHE_DATA_INT(msginfo->msgnum, fp);
|
|
|
|
WRITE_CACHE_DATA_INT(msginfo->size, fp);
|
|
|
|
WRITE_CACHE_DATA_INT(msginfo->mtime, fp);
|
|
|
|
WRITE_CACHE_DATA_INT(msginfo->date_t, fp);
|
|
|
|
WRITE_CACHE_DATA_INT(flags, fp);
|
|
|
|
|
|
|
|
WRITE_CACHE_DATA(msginfo->fromname, fp);
|
|
|
|
|
|
|
|
WRITE_CACHE_DATA(msginfo->date, fp);
|
|
|
|
WRITE_CACHE_DATA(msginfo->from, fp);
|
|
|
|
WRITE_CACHE_DATA(msginfo->to, fp);
|
|
|
|
WRITE_CACHE_DATA(msginfo->cc, fp);
|
|
|
|
WRITE_CACHE_DATA(msginfo->newsgroups, fp);
|
|
|
|
WRITE_CACHE_DATA(msginfo->subject, fp);
|
|
|
|
WRITE_CACHE_DATA(msginfo->msgid, fp);
|
|
|
|
WRITE_CACHE_DATA(msginfo->inreplyto, fp);
|
|
|
|
WRITE_CACHE_DATA(msginfo->xref, fp);
|
2004-07-13 13:55:36 +02:00
|
|
|
WRITE_CACHE_DATA_INT(msginfo->planned_download, fp);
|
2004-12-08 12:31:25 +01:00
|
|
|
WRITE_CACHE_DATA_INT(msginfo->total_size, fp);
|
2005-03-30 22:29:56 +02:00
|
|
|
|
|
|
|
WRITE_CACHE_DATA_INT(g_slist_length(msginfo->references), fp);
|
2005-03-31 17:31:13 +02:00
|
|
|
|
2005-03-30 22:29:56 +02:00
|
|
|
for (cur = msginfo->references; cur != NULL; cur = cur->next) {
|
|
|
|
WRITE_CACHE_DATA((gchar *)cur->data, fp);
|
|
|
|
}
|
2006-08-24 18:43:55 +02:00
|
|
|
return w_err;
|
2002-06-30 01:48:43 +02:00
|
|
|
}
|
|
|
|
|
2006-08-24 18:43:55 +02:00
|
|
|
static int msgcache_write_flags(MsgInfo *msginfo, FILE *fp)
|
2002-06-30 01:48:43 +02:00
|
|
|
{
|
|
|
|
MsgPermFlags flags = msginfo->flags.perm_flags;
|
2006-08-24 18:43:55 +02:00
|
|
|
int w_err = 0;
|
2002-06-30 01:48:43 +02:00
|
|
|
WRITE_CACHE_DATA_INT(msginfo->msgnum, fp);
|
|
|
|
WRITE_CACHE_DATA_INT(flags, fp);
|
2006-08-24 18:43:55 +02:00
|
|
|
return w_err;
|
2002-06-30 01:48:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct write_fps
|
|
|
|
{
|
|
|
|
FILE *cache_fp;
|
|
|
|
FILE *mark_fp;
|
2006-08-24 18:43:55 +02:00
|
|
|
int error;
|
2002-06-30 01:48:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static void msgcache_write_func(gpointer key, gpointer value, gpointer user_data)
|
|
|
|
{
|
|
|
|
MsgInfo *msginfo;
|
|
|
|
struct write_fps *write_fps;
|
2003-04-17 19:02:07 +02:00
|
|
|
|
2002-06-30 01:48:43 +02:00
|
|
|
msginfo = (MsgInfo *)value;
|
|
|
|
write_fps = user_data;
|
|
|
|
|
2006-08-24 18:43:55 +02:00
|
|
|
write_fps->error |= msgcache_write_cache(msginfo, write_fps->cache_fp);
|
|
|
|
write_fps->error |= msgcache_write_flags(msginfo, write_fps->mark_fp);
|
2002-06-30 01:48:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
gint msgcache_write(const gchar *cache_file, const gchar *mark_file, MsgCache *cache)
|
|
|
|
{
|
|
|
|
struct write_fps write_fps;
|
2006-08-24 18:43:55 +02:00
|
|
|
gchar *new_cache, *new_mark;
|
|
|
|
int w_err = 0;
|
2002-06-30 01:48:43 +02:00
|
|
|
|
|
|
|
g_return_val_if_fail(cache_file != NULL, -1);
|
|
|
|
g_return_val_if_fail(mark_file != NULL, -1);
|
|
|
|
g_return_val_if_fail(cache != NULL, -1);
|
|
|
|
|
2006-08-24 18:43:55 +02:00
|
|
|
new_cache = g_strconcat(cache_file, ".new", NULL);
|
|
|
|
new_mark = g_strconcat(mark_file, ".new", NULL);
|
|
|
|
|
|
|
|
write_fps.error = 0;
|
|
|
|
write_fps.cache_fp = msgcache_open_data_file(new_cache, CACHE_VERSION,
|
2004-02-21 12:01:01 +01:00
|
|
|
DATA_WRITE, NULL, 0);
|
2006-08-24 18:43:55 +02:00
|
|
|
if (write_fps.cache_fp == NULL) {
|
|
|
|
g_free(new_cache);
|
|
|
|
g_free(new_mark);
|
2002-06-30 01:48:43 +02:00
|
|
|
return -1;
|
2006-08-24 18:43:55 +02:00
|
|
|
}
|
2002-06-30 01:48:43 +02:00
|
|
|
|
2004-08-06 13:34:34 +02:00
|
|
|
WRITE_CACHE_DATA(CS_UTF_8, write_fps.cache_fp);
|
|
|
|
|
2006-08-24 18:43:55 +02:00
|
|
|
if (w_err != 0) {
|
|
|
|
g_warning("failed to write charset\n");
|
|
|
|
fclose(write_fps.cache_fp);
|
|
|
|
g_unlink(new_cache);
|
|
|
|
g_free(new_cache);
|
|
|
|
g_free(new_mark);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
write_fps.mark_fp = msgcache_open_data_file(new_mark, MARK_VERSION,
|
2004-02-21 12:01:01 +01:00
|
|
|
DATA_WRITE, NULL, 0);
|
|
|
|
if (write_fps.mark_fp == NULL) {
|
2002-06-30 01:48:43 +02:00
|
|
|
fclose(write_fps.cache_fp);
|
2006-08-24 18:43:55 +02:00
|
|
|
g_unlink(new_cache);
|
|
|
|
g_free(new_cache);
|
|
|
|
g_free(new_mark);
|
2002-06-30 01:48:43 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-08-24 18:43:55 +02:00
|
|
|
debug_print("\tWriting message cache to %s and %s...\n", new_cache, new_mark);
|
2004-02-21 12:01:01 +01:00
|
|
|
|
2006-08-24 18:43:55 +02:00
|
|
|
if (change_file_mode_rw(write_fps.cache_fp, new_cache) < 0)
|
|
|
|
FILE_OP_ERROR(new_cache, "chmod");
|
2002-06-30 01:48:43 +02:00
|
|
|
|
|
|
|
g_hash_table_foreach(cache->msgnum_table, msgcache_write_func, (gpointer)&write_fps);
|
|
|
|
|
|
|
|
fclose(write_fps.cache_fp);
|
|
|
|
fclose(write_fps.mark_fp);
|
|
|
|
|
|
|
|
|
2006-08-24 18:43:55 +02:00
|
|
|
if (write_fps.error != 0) {
|
|
|
|
g_unlink(new_cache);
|
|
|
|
g_unlink(new_mark);
|
|
|
|
g_free(new_cache);
|
|
|
|
g_free(new_mark);
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
move_file(new_cache, cache_file, TRUE);
|
|
|
|
move_file(new_mark, mark_file, TRUE);
|
|
|
|
cache->last_access = time(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(new_cache);
|
|
|
|
g_free(new_mark);
|
2002-08-15 09:38:17 +02:00
|
|
|
debug_print("done.\n");
|
2002-07-03 00:28:11 +02:00
|
|
|
return 0;
|
2002-06-30 01:48:43 +02:00
|
|
|
}
|
|
|
|
|