-
Notifications
You must be signed in to change notification settings - Fork 0
/
CachedFile.cc
275 lines (227 loc) · 6.52 KB
/
CachedFile.cc
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
//========================================================================
//
// CachedFile.cc
//
// This file is licensed under the GPLv2 or later
//
// Copyright 2009 Stefan Thomas <thomas@eload24.com>
// Copyright 2010, 2011 Hib Eris <hib@hiberis.nl>
// Copyright 2010, 2018-2020, 2022 Albert Astals Cid <aacid@kde.org>
// Copyright (C) 2013 Julien Nabet <serval2412@yahoo.fr>
//
//========================================================================
#include <config.h>
#include "CachedFile.h"
//------------------------------------------------------------------------
// CachedFile
//------------------------------------------------------------------------
CachedFile::CachedFile(CachedFileLoader *cacheLoader)
{
loader = cacheLoader;
streamPos = 0;
chunks = new std::vector<Chunk>();
length = 0;
length = loader->init(this);
refCnt = 1;
if (length != ((size_t)-1)) {
chunks->resize(length / CachedFileChunkSize + 1);
} else {
error(errInternal, -1, "Failed to initialize file cache.");
chunks->resize(0);
}
}
CachedFile::~CachedFile()
{
delete loader;
delete chunks;
}
void CachedFile::incRefCnt()
{
refCnt++;
}
void CachedFile::decRefCnt()
{
if (--refCnt == 0) {
delete this;
}
}
long int CachedFile::tell()
{
return streamPos;
}
int CachedFile::seek(long int offset, int origin)
{
if (origin == SEEK_SET) {
streamPos = offset;
} else if (origin == SEEK_CUR) {
streamPos += offset;
} else {
streamPos = length + offset;
}
if (streamPos > length) {
streamPos = 0;
return 1;
}
return 0;
}
int CachedFile::cache(const std::vector<ByteRange> &origRanges)
{
std::vector<int> loadChunks;
int numChunks = length / CachedFileChunkSize + 1;
std::vector<bool> chunkNeeded(numChunks);
int startChunk, endChunk;
std::vector<ByteRange> chunk_ranges, all;
ByteRange range;
const std::vector<ByteRange> *ranges = &origRanges;
if (ranges->empty()) {
range.offset = 0;
range.length = length;
all.push_back(range);
ranges = &all;
}
for (int i = 0; i < numChunks; ++i) {
chunkNeeded[i] = false;
}
for (const ByteRange &r : *ranges) {
if (r.length == 0) {
continue;
}
if (r.offset >= length) {
continue;
}
const size_t start = r.offset;
size_t end = start + r.length - 1;
if (end >= length) {
end = length - 1;
}
startChunk = start / CachedFileChunkSize;
endChunk = end / CachedFileChunkSize;
for (int chunk = startChunk; chunk <= endChunk; chunk++) {
if ((*chunks)[chunk].state == chunkStateNew) {
chunkNeeded[chunk] = true;
}
}
}
int chunk = 0;
while (chunk < numChunks) {
while (!chunkNeeded[chunk] && (++chunk != numChunks)) {
;
}
if (chunk == numChunks) {
break;
}
startChunk = chunk;
loadChunks.push_back(chunk);
while ((++chunk != numChunks) && chunkNeeded[chunk]) {
loadChunks.push_back(chunk);
}
endChunk = chunk - 1;
range.offset = startChunk * CachedFileChunkSize;
range.length = (endChunk - startChunk + 1) * CachedFileChunkSize;
chunk_ranges.push_back(range);
}
if (chunk_ranges.size() > 0) {
CachedFileWriter writer = CachedFileWriter(this, &loadChunks);
return loader->load(chunk_ranges, &writer);
}
return 0;
}
size_t CachedFile::read(void *ptr, size_t unitsize, size_t count)
{
size_t bytes = unitsize * count;
if (length < (streamPos + bytes)) {
bytes = length - streamPos;
}
if (bytes == 0) {
return 0;
}
// Load data
if (cache(streamPos, bytes) != 0) {
return 0;
}
// Copy data to buffer
size_t toCopy = bytes;
while (toCopy) {
int chunk = streamPos / CachedFileChunkSize;
int offset = streamPos % CachedFileChunkSize;
size_t len = CachedFileChunkSize - offset;
if (len > toCopy) {
len = toCopy;
}
memcpy(ptr, (*chunks)[chunk].data + offset, len);
streamPos += len;
toCopy -= len;
ptr = (char *)ptr + len;
}
return bytes;
}
int CachedFile::cache(size_t rangeOffset, size_t rangeLength)
{
std::vector<ByteRange> r;
ByteRange range;
range.offset = rangeOffset;
range.length = rangeLength;
r.push_back(range);
return cache(r);
}
//------------------------------------------------------------------------
// CachedFileWriter
//------------------------------------------------------------------------
CachedFileWriter::CachedFileWriter(CachedFile *cachedFileA, std::vector<int> *chunksA)
{
cachedFile = cachedFileA;
chunks = chunksA;
if (chunks) {
offset = 0;
it = (*chunks).begin();
}
}
CachedFileWriter::~CachedFileWriter() { }
size_t CachedFileWriter::write(const char *ptr, size_t size)
{
const char *cp = ptr;
size_t len = size;
size_t nfree, ncopy;
size_t written = 0;
size_t chunk;
if (!len) {
return 0;
}
while (len) {
if (chunks) {
if (offset == CachedFileChunkSize) {
++it;
if (it == (*chunks).end()) {
return written;
}
offset = 0;
}
chunk = *it;
} else {
offset = cachedFile->length % CachedFileChunkSize;
chunk = cachedFile->length / CachedFileChunkSize;
}
if (chunk >= cachedFile->chunks->size()) {
cachedFile->chunks->resize(chunk + 1);
}
nfree = CachedFileChunkSize - offset;
ncopy = (len >= nfree) ? nfree : len;
memcpy(&((*cachedFile->chunks)[chunk].data[offset]), cp, ncopy);
len -= ncopy;
cp += ncopy;
offset += ncopy;
written += ncopy;
if (!chunks) {
cachedFile->length += ncopy;
}
if (offset == CachedFileChunkSize) {
(*cachedFile->chunks)[chunk].state = CachedFile::chunkStateLoaded;
}
}
if ((chunk == (cachedFile->length / CachedFileChunkSize)) && (offset == (cachedFile->length % CachedFileChunkSize))) {
(*cachedFile->chunks)[chunk].state = CachedFile::chunkStateLoaded;
}
return written;
}
CachedFileLoader::~CachedFileLoader() = default;
//------------------------------------------------------------------------