summaryrefslogtreecommitdiff
path: root/src/dm_cache.c
blob: 6094c29754f1c9215fb588b7182df8780c55fe6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
  
 Copyright (c) 2004-2010 NFG Net Facilities Group BV support@nfg.nl

 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
 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "dbmail.h"
#include "dm_cache.h"

#define THIS_MODULE "Cache"
/*
 * cached raw message data
 */
#define T Cache_T

struct T {
	u64_t id;
	u64_t size;
	Mem_T memdump;
	Mem_T tmpdump;
	int file_dumped;
};

/* */
T Cache_new(void)
{
	int serr;
	T C;
	
	C = g_malloc0(sizeof(*C));

	C->id = 0;
	if (! (C->memdump = Mem_open())) {
		serr = errno;
		TRACE(TRACE_ERR,"Mem_open() failed [%s]", strerror(serr));
		g_free(C);
		errno = serr;
		return NULL;
	}
	
	if (! (C->tmpdump = Mem_open())) {
		serr = errno;
		TRACE(TRACE_ERR,"Mem_open() failed [%s]", strerror(serr));
		errno = serr;
		Mem_close(&C->memdump);
		g_free(C);
		return NULL;
	}

	return C;
}

u64_t Cache_set_dump(T C, char *buf, int dumptype)
{
	u64_t outcnt = 0;
	Mem_T M;

	switch (dumptype) {
		case IMAP_CACHE_MEMDUMP:
			M = Cache_get_memdump(C);
		break;
		case IMAP_CACHE_TMPDUMP:
			M = Cache_get_tmpdump(C);
		break;
		default:
			assert(0);
		break;
	}
	
	assert(M);
	assert(buf);

	outcnt = strlen(buf);

	Mem_rewind(M);
	Mem_write(M, buf, outcnt);
	Mem_rewind(M);

	return outcnt;
}

void Cache_clear(T C)
{
	C->id = 0;
	C->size = 0;
	Mem_close(&C->memdump);
	C->memdump = Mem_open();

	Mem_close(&C->tmpdump);
	C->tmpdump = Mem_open();
}


u64_t Cache_update(T C, DbmailMessage *message, int filter)
{
	u64_t tmpcnt = 0, outcnt = 0;
	char *crlf = NULL, *buf = NULL;

	TRACE(TRACE_DEBUG,"[%p] C->id[%llu] message->id[%llu]", C, C->id, message->id);

	if (C->id != message->id) {

		Cache_clear(C);

		buf = dbmail_message_to_string(message);
		crlf = get_crlf_encoded(buf);
		outcnt = Cache_set_dump(C,crlf,IMAP_CACHE_MEMDUMP);
		tmpcnt = Cache_set_dump(C,crlf,IMAP_CACHE_TMPDUMP);
		g_free(buf);
		g_free(crlf);

		assert(tmpcnt==outcnt);
		
		C->size = outcnt;
		C->id = message->id;
		
	}
	
	switch (filter) {
		/* for these two update the temp MEM buffer */	
		case DBMAIL_MESSAGE_FILTER_HEAD:
			buf = dbmail_message_hdrs_to_string(message);
			crlf = get_crlf_encoded(buf);
			outcnt = Cache_set_dump(C,crlf,IMAP_CACHE_TMPDUMP);
			g_free(buf);
			g_free(crlf);
		break;
		case DBMAIL_MESSAGE_FILTER_BODY:
			buf = dbmail_message_body_to_string(message);
			crlf = get_crlf_encoded(buf);
			outcnt = Cache_set_dump(C,crlf,IMAP_CACHE_TMPDUMP);
			g_free(buf);
			g_free(crlf);
		break;
		case DBMAIL_MESSAGE_FILTER_FULL:
			outcnt = C->size;
			Mem_rewind(C->memdump);
			Mem_rewind(C->tmpdump);
			/* done */
		break;

	}

	TRACE(TRACE_DEBUG,"C->size[%llu], outcnt[%llu]", C->size, outcnt);	

	return outcnt;
}


void Cache_set_memdump(T C, Mem_T M)
{
	assert(C);
	assert(M);
	C->memdump = M;
}

u64_t Cache_get_size(T C)
{
	assert(C);
	return C->size;
}
Mem_T Cache_get_memdump(T C)
{
	return C->memdump;
}

void Cache_set_tmpdump(T C, Mem_T M)
{
	assert(C);
	assert(M);
	C->tmpdump = M;
}

Mem_T Cache_get_tmpdump(T C)
{
	return C->tmpdump;
}


/*
 * closes the msg cache
 */
void Cache_free(T *C)
{
	T c = *C;
	c->id = -1;
	Mem_close(&c->memdump);
	Mem_close(&c->tmpdump);
	g_free(c);
	
}