-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.cpp
387 lines (345 loc) · 9.66 KB
/
cache.cpp
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include "ff.h"
#include "vdskapi.h"
#include "disk.h"
#include "functions.h"
#include "extern.h"
#include <vector>
#pragma hdrstop
using namespace std;
unsigned long highcacheoffset(char *fn)
{
unsigned long bestoffset = 0;
ccs->Enter();
for (vector<CACHE>::iterator it = cache.begin(); it != cache.end(); it++)
{
if (strncmpi(fn, (*it).fn, 13) == 0 && bestoffset < (*it).offset + (*it).size)
{
bestoffset = (*it).offset + (*it).size;
}
}
ccs->Leave();
return bestoffset;
}
unsigned long nextcacheoffset(char *fn, unsigned long offset)
{
ccs->Enter();
unsigned long nextoffset = 0;
bool found = false;
for (vector<CACHE>::iterator it = cache.begin(); it != cache.end(); it++)
{
if (strncmpi(fn, (*it).fn, 13) == 0 && offset >= (*it).offset && offset <= (*it).offset +
(*it).size)
{
nextoffset = (*it).offset + (*it).size;
found = true;
}
}
ccs->Leave();
if (!found)
{
deb(E_FTPCONTROL, "next cache offset not found for %s @ %lu", fn, offset);
nextoffset = offset;
}
return nextoffset;
}
unsigned long seqcacheoffset(char *fn)
{
unsigned long seqoffset = 0;
ccs->Enter();
for (vector<CACHE>::iterator it = cache.begin(); it != cache.end(); it++)
{
if (strncmpi(fn, (*it).fn, 13) == 0)
{
// seqoffset = (*it).seqoffset + (*it).seqlen;
}
}
ccs->Leave();
return seqoffset;
}
bool in_cache(char *fn, unsigned long offset, ULONG len)
{
bool incache = false;
ccs->Enter();
for (vector<CACHE>::iterator it = cache.begin(); it != cache.end(); it++)
{
// deb(E_CACHEMSG, "'%s' ofs %lu fs %lu", (*it).fn, (*it).offset, (*it).fetchedsize);
// if (strcmpi(fn, (*it).fn) == 0)
// deb(E_CACHEMSG, "fn match %s", fn);
// else
// deb(E_CACHEMSG, "'%s' != '%s'", fn, (*it).fn);
if (strncmpi(fn, (*it).fn, 13) == 0 && offset >= (*it).offset && offset + len <= (*it)
.offset + (*it).size)
{
(*it).lastaccessed = GetTickCount();
// deb(E_CACHEMSG, "%s VS %s | offset %lu len %lu, otpfs %lu itfetsoiz %lu", fn, (*it).fn, offset, len, (*it).offset,
// (*it).fetchedsize);
// deb(E_CACHEMSG, "found in cache @ %p [%s] fetched: %lu, offset: %lu - %lu itofset: %lu (len %lu)",
// (*it).buf, fn, (*it).fetchedsize, offset, offset + len, (*it).offset, len);
incache = true;
break;
}
}
ccs->Leave();
// deb(E_CACHEMSG, "in cache '%s' %d", fn, incache);
return incache;
}
long cache_read(char *fn, unsigned long offset, LPVOID buf, ULONG len, bool paging)
{
ccs->Enter();
long hits = 0;
for (vector<CACHE>::iterator it = cache.begin(); it != cache.end(); it++)
{
if (strncmpi(fn, (*it).fn, 13) == 0 && offset >= (*it).offset && offset + len <= (*it)
.offset + (*it).size)
{
char *readoffset = (char*)(*it).buf;
if ((*it).offset > 0)
readoffset = (char*)((unsigned long)readoffset + (unsigned long)
((unsigned long)offset - ((unsigned long)(*it).offset)));
else
readoffset = (char*)((unsigned long)readoffset + (unsigned long)
(((unsigned long)offset)));
(*it).lastaccessed = GetTickCount();
try
{
memcpy(buf, (void*)(readoffset), len);
}
catch(...)
{
deb("memcpy(%p, %lu, %lu) FAILED", buf, readoffset, len);
if (IsBadReadPtr((void*)(readoffset), len))
deb(
"[readoffset] memory @ %p bad read ptr for len %lu, size=%lu, ofset=%lu,itofs=%lu",
readoffset, len, (*it).size, (((unsigned long)(unsigned long)offset)), (*it).offset);
if (IsBadWritePtr(buf, len))
deb("disks [buf] @ %p is bad write ptr for %lu bytes (cache.size is %lu)", buf, len,
(*it).size);
exp;
}
(*it).cacheleft = (offset + len) - ((*it).offset + (*it).size);
hits = (long)(*it).hits++;
}
}
ccs->Leave();
return hits;
}
void cache_clear(void)
{
ccs->Enter();
for (vector<CACHE>::iterator it = cache.begin(); it != cache.end(); it++)
{
deb("cache clear %lu @ %p", HeapSize(GetProcessHeap(), 0, (*it).buf), (*it).buf);
if (!HeapFree(GetProcessHeap(), 0, (*it).buf))
deb("heapfree: %s", fmterr());
}
cache.clear();
ccs->Leave();
}
int cache_seq_reads(void)
{
int seqreads = 0;
for (vector<FATFILE>::iterator it = files.begin(); it != files.end(); it++)
{
if ((*it).sequential_reads)
seqreads++;
}
return seqreads;
}
int cache_save(char *ftpdir, char *fn, unsigned long offset, unsigned long size, char *buffer)
{
CACHE c;
unsigned long ofs = 0, len = 0;
if (!size)
ds;
// deb("save buf %p size %lu", buffer, size);
ccs->Enter();
// deb("enter");
memset(&c, 0, sizeof(c));
strncpy(c.fn, fn, sizeof(c.fn));
c.size = size; // HeapSize(GetProcessHeap(), 0, buffer);
c.fetchedsize = size;
c.offset = offset;
c.bufsize = size;
c.buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY | HEAP_GENERATE_EXCEPTIONS, (SIZE_T)size);
memcpy(c.buf, buffer, size);
c.bufsize=size;
// try
// {
// c.bufsize = (unsigned long)HeapSize(GetProcessHeap(), 0, buffer);
// char *bbuf = NULL;
// if ((SIZE_T)c.bufsize == -1)
// {
// c.bufsize = size;
// c.buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY|HEAP_GENERATE_EXCEPTIONS, size);
// memcpy(c.buf, buffer, size);
//
// }
// else
// {
// c.buf = buffer;
//
// }
// }
// catch(...)
// {
// deb("exception cache_save heapsize");
// ccs->Leave();
// return 0;
// }
c.len = size;
c.lastaccessed = GetTickCount();
strncpy(c.ftppath, ftpdir, sizeof(c.ftppath));
bool coagulated = false;
// if (cache.size())
// deb(E_FTPCONTROL, "linking %lu @ %lu", size, offset);
// deb(E_CACHEMSG, "ssssss", cache.size(), fn, len, ofs);
// if (size <= 65535)
// {
// deb("'%s' linking %lu @ %p", fn, size, c.buf);
for (vector<CACHE>::iterator it = cache.begin(); it != cache.end(); it++)
{
if (strncmpi(fn, (*it).fn, 13) == 0 && offset == (*it).offset + (*it).size && (*it).size < precacheminbytes)
{
// if((*it).size + size >= 65535)
// continue;
unsigned long newsiz;
newsiz = (*it).size + size;
try
{
// deb("cache match %lu @ %lu, newsiz=%lu, bufsize %lu", (*it).size, (*it).offset, newsiz, (*it).bufsize);
if (newsiz > (*it).bufsize)
{
// deb("rea buf %p %u => %lu heapsize=%d", (*it).buf,
// HeapSize(GetProcessHeap(), 0, (*it).buf), newsiz,
// HeapSize(GetProcessHeap(), 0, (*it).buf));
// deb(E_FTPCONTROL, "%lu + %lu = %lu", (*it).size, size, newsiz);
(*it).buf = HeapReAlloc(GetProcessHeap(), 0, (*it).buf, newsiz);
// deb("reallocated buf %p %u", (*it).buf, HeapSize(GetProcessHeap(), 0, (*it).buf));
//(*it).bufsize=newsiz;
}
char *dest;
dest = (char*)((unsigned long)(*it).buf + (unsigned long)(*it).size);
// deb("memcpy(%p, %p, %lu)", dest, buffer, size);
memcpy(dest, buffer, size);
// deb("done");
}
catch(...)
{
// deb("memcpy cache_save ex");
ccs->Leave();
break;
}
(*it).size = newsiz;
(*it).len = newsiz;
ofs = (*it).offset;
len = newsiz;
(*it).bufsize=newsiz;
(*it).lastaccessed = GetTickCount();
coagulated = true;
}
else
{
// deb(E_FTPCONTROL, "offset %lu != %lu + %lu (%lu)", offset, (*it).offset, (*it).size,
// (*it).offset + (*it).size);
}
}
// deb("done link");
// }
if (!coagulated)
{
// deb("pusing cac");
cache.push_back(c);
// deb("pusing done");
deb(E_CACHEMSG, "new #%2d '%14s' size: %5lu ofset: %7lu", cache.size(), fn, size, offset);
cache_list();
}
else
{ // deb("call_events E_CACHEMSG");
// deb(E_CACHEMSG, "add #%2d '%s' size: %lu blksize: %lu ofset: %lu", cache.size(), fn, size, len, ofs);
HeapFree(GetProcessHeap(), 0, c.buf);
}
// deb("call_events E_PRECACHED");
call_events(E_PRECACHED, (LPVOID)ftpdir, (LPVOID)fn, (LPVOID)offset, (LPVOID)buffer,
(LPVOID)size);
ccs->Leave();
// deb("ccs->Leave();");
// deb(E_CACHEMSG, "aaaa", cache.size(), fn, len, ofs);
}
void cache_list(void)
{
if (!ccs)
return;
ccs->Enter();
deb(E_CACHEMSG, "--- File cache: %d blocks ---", cache.size());
for (vector<CACHE>::iterator it = cache.begin(); it != cache.end(); it++)
{
deb(E_CACHEMSG, "%13s %9lu - %9lu size=%8lu buf=%8lu accessed: %d ms", (*it).fn, (*it).offset,
(*it).offset + (*it).size, (*it).size,(*it).bufsize,GetTickCount()-(*it).lastaccessed);
}
ccs->Leave();
}
//
// bool cache_write(long offset, LPVOID buf, ULONG len, int*hits, bool unsaved)
// {
// bool found = false;
// bool notcached = true;
// int upds = 0;
// LARGE_INTEGER li;
// // cev->SetEvent();
// ccs->Enter();
// for (vector<CACHE>::iterator it = cache.begin(); it != cache.end(); it++)
// {
// if ((*it).offset == offset)
// {
// found = true;
// if (len > (*it).len)
// {
// li.QuadPart = (*it).offset;
// // paintdisk(5, li, (*it).len, false);
// char str[128];
// sprintf(str, " * ÊÅØ: áûñòðûé ñåéâ %d @ %d: \r\n ðàçìåð %5d, íóæåí %5d", (*it).len, (*it).offset,
// (*it).len, len);
// deb(str);
// nwrite((unsigned long)(*it).buf, (*it).offset, (*it).len, 0, false, 0x00000010, str);
// HeapFree(GetProcessHeap(), 0, (*it).buf);
// totcachsize -= (*it).len;
// totcachsize += len;
// (*it).buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY|HEAP_GENERATE_EXCEPTIONS, len);
// (*it).len = len;
// // paintdisk(4, li, 1, 0);
// // (*it).unsaved = false;
// }
// // deb("cache overwrite");
// memcpy((*it).buf, buf, len);
// //
// (*it).changed = GetTickCount();
// (*it).unsaved = unsaved;
// (*it).opid = numpkt;
// (*it).updates++;
// if (hits)
// * hits = (*it).hits;
// upds = (*it).updates++;
// break;
// }
// }
// if (!found)
// {
// CACHE ch;
// ch.offset = offset;
// ch.len = len;
// ch.buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY|HEAP_GENERATE_EXCEPTIONS, len);
// memcpy(ch.buf, buf, len);
// totcachsize += len;
// ch.changed = GetTickCount();
// ch.unsaved = unsaved;
// ch.opid = numpkt;
// ch.updates = 0;
// ch.hits = 0;
// upds = 0;
// cache.push_back(ch);
// // deb(" + çàêåøèðîâàíî %lu áàéò", len, offset);
// // upds=false;
// }
// ccs->Leave();
// return upds;
// }