Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/network/access/qnetworkdiskcache.cpp |
Switch to Source code | Preprocessed file |
Line | Source | Count | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | - | |||||||||||||||||||
2 | - | |||||||||||||||||||
3 | QNetworkDiskCache::QNetworkDiskCache(QObject *parent) | - | ||||||||||||||||||
4 | : QAbstractNetworkCache(*new QNetworkDiskCachePrivate, parent) | - | ||||||||||||||||||
5 | { | - | ||||||||||||||||||
6 | } | - | ||||||||||||||||||
7 | - | |||||||||||||||||||
8 | - | |||||||||||||||||||
9 | - | |||||||||||||||||||
10 | - | |||||||||||||||||||
11 | QNetworkDiskCache::~QNetworkDiskCache() | - | ||||||||||||||||||
12 | { | - | ||||||||||||||||||
13 | QNetworkDiskCachePrivate * const d = d_func(); | - | ||||||||||||||||||
14 | QHashIterator<QIODevice*, QCacheItem*> itqDeleteAll(d->inserting); | - | ||||||||||||||||||
while (it.hasNext()) { | ||||||||||||||||||||
it.next(); | ||||||||||||||||||||
delete it.value(); | ||||||||||||||||||||
}} executed 66 times by 3 tests: end of block Executed by:
| ||||||||||||||||||||
16 | - | |||||||||||||||||||
17 | - | |||||||||||||||||||
18 | - | |||||||||||||||||||
19 | - | |||||||||||||||||||
20 | QString QNetworkDiskCache::cacheDirectory() const | - | ||||||||||||||||||
21 | { | - | ||||||||||||||||||
22 | const QNetworkDiskCachePrivate * const d = d_func(); | - | ||||||||||||||||||
23 | return d->cacheDirectory; | - | ||||||||||||||||||
24 | } | - | ||||||||||||||||||
25 | void QNetworkDiskCache::setCacheDirectory(const QString &cacheDir) | - | ||||||||||||||||||
26 | { | - | ||||||||||||||||||
27 | - | |||||||||||||||||||
28 | - | |||||||||||||||||||
29 | - | |||||||||||||||||||
30 | QNetworkDiskCachePrivate * const d = d_func(); | - | ||||||||||||||||||
31 | if (cacheDir.isEmpty()) | - | ||||||||||||||||||
32 | return; | - | ||||||||||||||||||
33 | d->cacheDirectory = cacheDir; | - | ||||||||||||||||||
34 | QDir dir(d->cacheDirectory); | - | ||||||||||||||||||
35 | d->cacheDirectory = dir.absolutePath(); | - | ||||||||||||||||||
36 | if (!d->cacheDirectory.endsWith(QLatin1Char('/'))) | - | ||||||||||||||||||
37 | d->cacheDirectory += QLatin1Char('/'); | - | ||||||||||||||||||
38 | - | |||||||||||||||||||
39 | d->dataDirectory = d->cacheDirectory + QLatin1String("data") + QString::number(8) + QLatin1Char('/'); | - | ||||||||||||||||||
40 | d->prepareLayout(); | - | ||||||||||||||||||
41 | } | - | ||||||||||||||||||
42 | - | |||||||||||||||||||
43 | - | |||||||||||||||||||
44 | - | |||||||||||||||||||
45 | - | |||||||||||||||||||
46 | qint64 QNetworkDiskCache::cacheSize() const | - | ||||||||||||||||||
47 | { | - | ||||||||||||||||||
48 | - | |||||||||||||||||||
49 | - | |||||||||||||||||||
50 | - | |||||||||||||||||||
51 | const QNetworkDiskCachePrivate * const d = d_func(); | - | ||||||||||||||||||
52 | if (d->cacheDirectory.isEmpty()
| 1-4 | ||||||||||||||||||
53 | return executed 1 time by 1 test: 0;return 0; Executed by:
executed 1 time by 1 test: return 0; Executed by:
| 1 | ||||||||||||||||||
54 | if (d->currentCacheSize < 0
| 1-3 | ||||||||||||||||||
55 | QNetworkDiskCache *that = const_cast<QNetworkDiskCache*>(this); | - | ||||||||||||||||||
56 | that->d_func()->currentCacheSize = that->expire(); | - | ||||||||||||||||||
57 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||||||||
58 | return executed 4 times by 1 test: d->currentCacheSize;return d->currentCacheSize; Executed by:
executed 4 times by 1 test: return d->currentCacheSize; Executed by:
| 4 | ||||||||||||||||||
59 | } | - | ||||||||||||||||||
60 | - | |||||||||||||||||||
61 | - | |||||||||||||||||||
62 | - | |||||||||||||||||||
63 | - | |||||||||||||||||||
64 | QIODevice *QNetworkDiskCache::prepare(const QNetworkCacheMetaData &metaData) | - | ||||||||||||||||||
65 | { | - | ||||||||||||||||||
66 | - | |||||||||||||||||||
67 | - | |||||||||||||||||||
68 | - | |||||||||||||||||||
69 | QNetworkDiskCachePrivate * const d = d_func(); | - | ||||||||||||||||||
70 | if (!metaData.isValid()
| 0-95 | ||||||||||||||||||
71 | return executed 8 times by 2 tests: 0;return 0; Executed by:
executed 8 times by 2 tests: return 0; Executed by:
| 8 | ||||||||||||||||||
72 | - | |||||||||||||||||||
73 | if (d->cacheDirectory.isEmpty()
| 1-87 | ||||||||||||||||||
74 | QMessageLogger(__FILE__, 186188, __PRETTY_FUNCTION__).warning() << ("QNetworkDiskCache::prepare() The cache directory is not set";); | - | ||||||||||||||||||
75 | return executed 1 time by 1 test: 0;return 0; Executed by:
executed 1 time by 1 test: return 0; Executed by:
| 1 | ||||||||||||||||||
76 | } | - | ||||||||||||||||||
77 | - | |||||||||||||||||||
78 | for (QForeachContainer<typename QtPrivate::remove_reference<decltype(metaData.rawHeaders())>::type> _container_((const auto headers = metaData.rawHeaders())); _container_.control && _container_.i != _container_.e; ++_container_.i, _container_.control ^= 1)(); | - | ||||||||||||||||||
79 | for (const QNetworkCacheMetaData::RawHeaderauto &header = *_container_.i; _container_.control; _container_.control = 0: headers) { | - | ||||||||||||||||||
80 | if (header.first.toLower() == "content-length"
| 13-236 | ||||||||||||||||||
81 | const qint64 size = header.second.toLongLong(); | - | ||||||||||||||||||
82 | if (size > (maximumCacheSize() * 3)/4
| 0-13 | ||||||||||||||||||
83 | return never executed: 0;return 0; never executed: return 0; | 0 | ||||||||||||||||||
84 | break; executed 13 times by 2 tests: break; Executed by:
| 13 | ||||||||||||||||||
85 | } | - | ||||||||||||||||||
86 | } executed 236 times by 3 tests: end of block Executed by:
| 236 | ||||||||||||||||||
87 | QScopedPointer<QCacheItem> cacheItem(new QCacheItem); | - | ||||||||||||||||||
88 | cacheItem->metaData = metaData; | - | ||||||||||||||||||
89 | - | |||||||||||||||||||
90 | QIODevice *device = 0; | - | ||||||||||||||||||
91 | if (cacheItem->canCompress()
| 12-75 | ||||||||||||||||||
92 | cacheItem->data.open(QBuffer::ReadWrite); | - | ||||||||||||||||||
93 | device = &(cacheItem->data); | - | ||||||||||||||||||
94 | } executed 12 times by 1 test: else {end of block Executed by:
| 12 | ||||||||||||||||||
95 | QString templateName = d->tmpCacheFileName(); | - | ||||||||||||||||||
96 | if (true) { | - | ||||||||||||||||||
97 | cacheItem->file = new QTemporaryFile(templateName, &cacheItem->data); | - | ||||||||||||||||||
98 | } executed 75 times by 3 tests: else {end of block Executed by:
dead code: { cacheItem->file = 0; } | - | ||||||||||||||||||
99 | cacheItem->file = 0; dead code: { cacheItem->file = 0; } | - | ||||||||||||||||||
100 | } dead code: { cacheItem->file = 0; } | - | ||||||||||||||||||
101 | if (!cacheItem->file
| 0-75 | ||||||||||||||||||
102 | QMessageLogger(__FILE__, 213216, __PRETTY_FUNCTION__).warning() << ("QNetworkDiskCache::prepare() unable to open temporary file";); | - | ||||||||||||||||||
103 | cacheItem.reset(); | - | ||||||||||||||||||
104 | return never executed: 0;return 0; never executed: return 0; | 0 | ||||||||||||||||||
105 | } | - | ||||||||||||||||||
106 | cacheItem->writeHeader(cacheItem->file); | - | ||||||||||||||||||
107 | device = cacheItem->file; | - | ||||||||||||||||||
108 | } executed 75 times by 3 tests: end of block Executed by:
| 75 | ||||||||||||||||||
109 | d->inserting[device] = cacheItem.take(); | - | ||||||||||||||||||
110 | return executed 87 times by 3 tests: device;return device; Executed by:
executed 87 times by 3 tests: return device; Executed by:
| 87 | ||||||||||||||||||
111 | } | - | ||||||||||||||||||
112 | - | |||||||||||||||||||
113 | - | |||||||||||||||||||
114 | - | |||||||||||||||||||
115 | - | |||||||||||||||||||
116 | void QNetworkDiskCache::insert(QIODevice *device) | - | ||||||||||||||||||
117 | { | - | ||||||||||||||||||
118 | - | |||||||||||||||||||
119 | - | |||||||||||||||||||
120 | - | |||||||||||||||||||
121 | QNetworkDiskCachePrivate * const d = d_func(); | - | ||||||||||||||||||
122 | QHash<QIODevice*, QCacheItem*>::iteratorconst auto it = d->inserting.findconstFind(device); | - | ||||||||||||||||||
123 | if (__builtin_expect(!!(
| 1-84 | ||||||||||||||||||
124 | QMessageLogger(__FILE__, 235238, __PRETTY_FUNCTION__).warning() << "QNetworkDiskCache::insert() called on a device we don't know about" << device; | - | ||||||||||||||||||
125 | return; executed 1 time by 1 test: return; Executed by:
| 1 | ||||||||||||||||||
126 | } | - | ||||||||||||||||||
127 | - | |||||||||||||||||||
128 | d->storeItem(it.value()); | - | ||||||||||||||||||
129 | delete it.value(); | - | ||||||||||||||||||
130 | d->inserting.erase(it); | - | ||||||||||||||||||
131 | } executed 84 times by 3 tests: end of block Executed by:
| 84 | ||||||||||||||||||
132 | - | |||||||||||||||||||
133 | - | |||||||||||||||||||
134 | - | |||||||||||||||||||
135 | - | |||||||||||||||||||
136 | - | |||||||||||||||||||
137 | - | |||||||||||||||||||
138 | void QNetworkDiskCachePrivate::prepareLayout() | - | ||||||||||||||||||
139 | { | - | ||||||||||||||||||
140 | QDir helper; | - | ||||||||||||||||||
141 | helper.mkpath(cacheDirectory + QLatin1String("prepared/")); | - | ||||||||||||||||||
142 | - | |||||||||||||||||||
143 | - | |||||||||||||||||||
144 | helper.mkpath(dataDirectory); | - | ||||||||||||||||||
145 | for (uint i = 0; i < 16 ; i++) { | - | ||||||||||||||||||
146 | QString str = QString::number(i, 16); | - | ||||||||||||||||||
147 | QString subdir = dataDirectory + str; | - | ||||||||||||||||||
148 | helper.mkdir(subdir); | - | ||||||||||||||||||
149 | } | - | ||||||||||||||||||
150 | } | - | ||||||||||||||||||
151 | - | |||||||||||||||||||
152 | - | |||||||||||||||||||
153 | void QNetworkDiskCachePrivate::storeItem(QCacheItem *cacheItem) | - | ||||||||||||||||||
154 | { | - | ||||||||||||||||||
155 | QNetworkDiskCache * const q = q_func(); | - | ||||||||||||||||||
156 | ((!(cacheItem->metaData.saveToDisk())) ? qt_assert("cacheItem->metaData.saveToDisk()",__FILE__,267270) : qt_noop()); | - | ||||||||||||||||||
157 | - | |||||||||||||||||||
158 | QString fileName = cacheFileName(cacheItem->metaData.url()); | - | ||||||||||||||||||
159 | ((!(!fileName.isEmpty())) ? qt_assert("!fileName.isEmpty()",__FILE__,270273) : qt_noop()); | - | ||||||||||||||||||
160 | - | |||||||||||||||||||
161 | if (QFile::exists(fileName)) { | - | ||||||||||||||||||
162 | if (!QFile::remove(fileName)) { | - | ||||||||||||||||||
163 | QMessageLogger(__FILE__, 274277, __PRETTY_FUNCTION__).warning() << "QNetworkDiskCache: couldn't remove the cache file " << fileName; | - | ||||||||||||||||||
164 | return; | - | ||||||||||||||||||
165 | } | - | ||||||||||||||||||
166 | } | - | ||||||||||||||||||
167 | - | |||||||||||||||||||
168 | if (currentCacheSize > 0) | - | ||||||||||||||||||
169 | currentCacheSize += 1024 + cacheItem->size(); | - | ||||||||||||||||||
170 | currentCacheSize = q->expire(); | - | ||||||||||||||||||
171 | if (!cacheItem->file) { | - | ||||||||||||||||||
172 | QString templateName = tmpCacheFileName(); | - | ||||||||||||||||||
173 | cacheItem->file = new QTemporaryFile(templateName, &cacheItem->data); | - | ||||||||||||||||||
174 | if (cacheItem->file->open()) { | - | ||||||||||||||||||
175 | cacheItem->writeHeader(cacheItem->file); | - | ||||||||||||||||||
176 | cacheItem->writeCompressedData(cacheItem->file); | - | ||||||||||||||||||
177 | } | - | ||||||||||||||||||
178 | } | - | ||||||||||||||||||
179 | - | |||||||||||||||||||
180 | if (cacheItem->file | - | ||||||||||||||||||
181 | && cacheItem->file->isOpen() | - | ||||||||||||||||||
182 | && cacheItem->file->error() == QFile::NoError) { | - | ||||||||||||||||||
183 | cacheItem->file->setAutoRemove(false); | - | ||||||||||||||||||
184 | - | |||||||||||||||||||
185 | if (cacheItem->file->rename(fileName)) | - | ||||||||||||||||||
186 | currentCacheSize += cacheItem->file->size(); | - | ||||||||||||||||||
187 | else | - | ||||||||||||||||||
188 | cacheItem->file->setAutoRemove(true); | - | ||||||||||||||||||
189 | } | - | ||||||||||||||||||
190 | if (cacheItem->metaData.url() == lastItem.metaData.url()) | - | ||||||||||||||||||
191 | lastItem.reset(); | - | ||||||||||||||||||
192 | } | - | ||||||||||||||||||
193 | - | |||||||||||||||||||
194 | - | |||||||||||||||||||
195 | - | |||||||||||||||||||
196 | - | |||||||||||||||||||
197 | bool QNetworkDiskCache::remove(const QUrl &url) | - | ||||||||||||||||||
198 | { | - | ||||||||||||||||||
199 | - | |||||||||||||||||||
200 | - | |||||||||||||||||||
201 | - | |||||||||||||||||||
202 | QNetworkDiskCachePrivate * const d = d_func(); | - | ||||||||||||||||||
203 | - | |||||||||||||||||||
204 | - | |||||||||||||||||||
205 | QHashIterator<QIODevice*, QCacheItem*> itfor (auto it = d->inserting); | 2-14 | ||||||||||||||||||
while (it.hasNext()) { | ||||||||||||||||||||
itcbegin(), end = d->inserting.nextcend(); it != end
| ||||||||||||||||||||
206 | QCacheItem *item = it.value(); | - | ||||||||||||||||||
207 | if (item
| 0-2 | ||||||||||||||||||
208 | delete item; | - | ||||||||||||||||||
209 | d->inserting.removeerase(it.key());); | - | ||||||||||||||||||
210 | return executed 1 time by 1 test: true;return true; Executed by:
executed 1 time by 1 test: return true; Executed by:
| 1 | ||||||||||||||||||
211 | } | - | ||||||||||||||||||
212 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||||||||
213 | - | |||||||||||||||||||
214 | if (d->lastItem.metaData.url() == url
| 3-11 | ||||||||||||||||||
215 | d->lastItem.reset(); executed 3 times by 2 tests: d->lastItem.reset(); Executed by:
| 3 | ||||||||||||||||||
216 | return executed 14 times by 3 tests: d->removeFile(d->cacheFileName(url));return d->removeFile(d->cacheFileName(url)); Executed by:
executed 14 times by 3 tests: return d->removeFile(d->cacheFileName(url)); Executed by:
| 14 | ||||||||||||||||||
217 | } | - | ||||||||||||||||||
218 | - | |||||||||||||||||||
219 | - | |||||||||||||||||||
220 | - | |||||||||||||||||||
221 | - | |||||||||||||||||||
222 | bool QNetworkDiskCachePrivate::removeFile(const QString &file) | - | ||||||||||||||||||
223 | { | - | ||||||||||||||||||
224 | - | |||||||||||||||||||
225 | - | |||||||||||||||||||
226 | - | |||||||||||||||||||
227 | if (file.isEmpty()) | - | ||||||||||||||||||
228 | return false; | - | ||||||||||||||||||
229 | QFileInfo info(file); | - | ||||||||||||||||||
230 | QString fileName = info.fileName(); | - | ||||||||||||||||||
231 | if (!fileName.endsWith(QLatin1String(".d"))) | - | ||||||||||||||||||
232 | return false; | - | ||||||||||||||||||
233 | qint64 size = info.size(); | - | ||||||||||||||||||
234 | if (QFile::remove(file)) { | - | ||||||||||||||||||
235 | currentCacheSize -= size; | - | ||||||||||||||||||
236 | return true; | - | ||||||||||||||||||
237 | } | - | ||||||||||||||||||
238 | return false; | - | ||||||||||||||||||
239 | } | - | ||||||||||||||||||
240 | - | |||||||||||||||||||
241 | - | |||||||||||||||||||
242 | - | |||||||||||||||||||
243 | - | |||||||||||||||||||
244 | QNetworkCacheMetaData QNetworkDiskCache::metaData(const QUrl &url) | - | ||||||||||||||||||
245 | { | - | ||||||||||||||||||
246 | - | |||||||||||||||||||
247 | - | |||||||||||||||||||
248 | - | |||||||||||||||||||
249 | QNetworkDiskCachePrivate * const d = d_func(); | - | ||||||||||||||||||
250 | if (d->lastItem.metaData.url() == url) | - | ||||||||||||||||||
251 | return d->lastItem.metaData; | - | ||||||||||||||||||
252 | return fileMetaData(d->cacheFileName(url)); | - | ||||||||||||||||||
253 | } | - | ||||||||||||||||||
254 | - | |||||||||||||||||||
255 | - | |||||||||||||||||||
256 | - | |||||||||||||||||||
257 | - | |||||||||||||||||||
258 | - | |||||||||||||||||||
259 | - | |||||||||||||||||||
260 | QNetworkCacheMetaData QNetworkDiskCache::fileMetaData(const QString &fileName) const | - | ||||||||||||||||||
261 | { | - | ||||||||||||||||||
262 | - | |||||||||||||||||||
263 | - | |||||||||||||||||||
264 | - | |||||||||||||||||||
265 | const QNetworkDiskCachePrivate * const d = d_func(); | - | ||||||||||||||||||
266 | QFile file(fileName); | - | ||||||||||||||||||
267 | if (!file.open(QFile::ReadOnly)) | - | ||||||||||||||||||
268 | return QNetworkCacheMetaData(); | - | ||||||||||||||||||
269 | if (!d->lastItem.read(&file, false)) { | - | ||||||||||||||||||
270 | file.close(); | - | ||||||||||||||||||
271 | QNetworkDiskCachePrivate *that = const_cast<QNetworkDiskCachePrivate*>(d); | - | ||||||||||||||||||
272 | that->removeFile(fileName); | - | ||||||||||||||||||
273 | } | - | ||||||||||||||||||
274 | return d->lastItem.metaData; | - | ||||||||||||||||||
275 | } | - | ||||||||||||||||||
276 | - | |||||||||||||||||||
277 | - | |||||||||||||||||||
278 | - | |||||||||||||||||||
279 | - | |||||||||||||||||||
280 | QIODevice *QNetworkDiskCache::data(const QUrl &url) | - | ||||||||||||||||||
281 | { | - | ||||||||||||||||||
282 | - | |||||||||||||||||||
283 | - | |||||||||||||||||||
284 | - | |||||||||||||||||||
285 | QNetworkDiskCachePrivate * const d = d_func(); | - | ||||||||||||||||||
286 | QScopedPointer<QBuffer> buffer; | - | ||||||||||||||||||
287 | if (!url.isValid()) | - | ||||||||||||||||||
288 | return 0; | - | ||||||||||||||||||
289 | if (d->lastItem.metaData.url() == url && d->lastItem.data.isOpen()) { | - | ||||||||||||||||||
290 | buffer.reset(new QBuffer); | - | ||||||||||||||||||
291 | buffer->setData(d->lastItem.data.data()); | - | ||||||||||||||||||
292 | } else { | - | ||||||||||||||||||
293 | QScopedPointer<QFile> file(new QFile(d->cacheFileName(url))); | - | ||||||||||||||||||
294 | if (!file->open(QFile::ReadOnly | QIODevice::Unbuffered)) | - | ||||||||||||||||||
295 | return 0; | - | ||||||||||||||||||
296 | - | |||||||||||||||||||
297 | if (!d->lastItem.read(file.data(), true)) { | - | ||||||||||||||||||
298 | file->close(); | - | ||||||||||||||||||
299 | remove(url); | - | ||||||||||||||||||
300 | return 0; | - | ||||||||||||||||||
301 | } | - | ||||||||||||||||||
302 | if (d->lastItem.data.isOpen()) { | - | ||||||||||||||||||
303 | - | |||||||||||||||||||
304 | buffer.reset(new QBuffer); | - | ||||||||||||||||||
305 | buffer->setData(d->lastItem.data.data()); | - | ||||||||||||||||||
306 | } else { | - | ||||||||||||||||||
307 | buffer.reset(new QBuffer); | - | ||||||||||||||||||
308 | - | |||||||||||||||||||
309 | qint64 size = file->size() - file->pos(); | - | ||||||||||||||||||
310 | const uchar *p = 0; | - | ||||||||||||||||||
311 | - | |||||||||||||||||||
312 | p = file->map(file->pos(), size); | - | ||||||||||||||||||
313 | - | |||||||||||||||||||
314 | if (p) { | - | ||||||||||||||||||
315 | buffer->setData((const char *)p, size); | - | ||||||||||||||||||
316 | file.take()->setParent(buffer.data()); | - | ||||||||||||||||||
317 | } else { | - | ||||||||||||||||||
318 | buffer->setData(file->readAll()); | - | ||||||||||||||||||
319 | } | - | ||||||||||||||||||
320 | } | - | ||||||||||||||||||
321 | } | - | ||||||||||||||||||
322 | buffer->open(QBuffer::ReadOnly); | - | ||||||||||||||||||
323 | return buffer.take(); | - | ||||||||||||||||||
324 | } | - | ||||||||||||||||||
325 | - | |||||||||||||||||||
326 | - | |||||||||||||||||||
327 | - | |||||||||||||||||||
328 | - | |||||||||||||||||||
329 | void QNetworkDiskCache::updateMetaData(const QNetworkCacheMetaData &metaData) | - | ||||||||||||||||||
330 | { | - | ||||||||||||||||||
331 | - | |||||||||||||||||||
332 | - | |||||||||||||||||||
333 | - | |||||||||||||||||||
334 | QUrl url = metaData.url(); | - | ||||||||||||||||||
335 | QIODevice *oldDevice = data(url); | - | ||||||||||||||||||
336 | if (!oldDevice
| 1-6 | ||||||||||||||||||
337 | - | |||||||||||||||||||
338 | - | |||||||||||||||||||
339 | - | |||||||||||||||||||
340 | return; executed 1 time by 1 test: return; Executed by:
| 1 | ||||||||||||||||||
341 | } | - | ||||||||||||||||||
342 | - | |||||||||||||||||||
343 | QIODevice *newDevice = prepare(metaData); | - | ||||||||||||||||||
344 | if (!newDevice
| 0-6 | ||||||||||||||||||
345 | - | |||||||||||||||||||
346 | - | |||||||||||||||||||
347 | - | |||||||||||||||||||
348 | return; never executed: return; | 0 | ||||||||||||||||||
349 | } | - | ||||||||||||||||||
350 | char data[1024]; | - | ||||||||||||||||||
351 | while (!oldDevice->atEnd()
| 6 | ||||||||||||||||||
352 | qint64 s = oldDevice->read(data, 1024); | - | ||||||||||||||||||
353 | newDevice->write(data, s); | - | ||||||||||||||||||
354 | } executed 6 times by 2 tests: end of block Executed by:
| 6 | ||||||||||||||||||
355 | delete oldDevice; | - | ||||||||||||||||||
356 | insert(newDevice); | - | ||||||||||||||||||
357 | } executed 6 times by 2 tests: end of block Executed by:
| 6 | ||||||||||||||||||
358 | - | |||||||||||||||||||
359 | - | |||||||||||||||||||
360 | - | |||||||||||||||||||
361 | - | |||||||||||||||||||
362 | - | |||||||||||||||||||
363 | - | |||||||||||||||||||
364 | qint64 QNetworkDiskCache::maximumCacheSize() const | - | ||||||||||||||||||
365 | { | - | ||||||||||||||||||
366 | const QNetworkDiskCachePrivate * const d = d_func(); | - | ||||||||||||||||||
367 | return d->maximumCacheSize; | - | ||||||||||||||||||
368 | } | - | ||||||||||||||||||
369 | void QNetworkDiskCache::setMaximumCacheSize(qint64 size) | - | ||||||||||||||||||
370 | { | - | ||||||||||||||||||
371 | QNetworkDiskCachePrivate * const d = d_func(); | - | ||||||||||||||||||
372 | bool expireCache = (size < d->maximumCacheSize); | - | ||||||||||||||||||
373 | d->maximumCacheSize = size; | - | ||||||||||||||||||
374 | if (expireCache) | - | ||||||||||||||||||
375 | d->currentCacheSize = expire(); | - | ||||||||||||||||||
376 | } | - | ||||||||||||||||||
377 | qint64 QNetworkDiskCache::expire() | - | ||||||||||||||||||
378 | { | - | ||||||||||||||||||
379 | QNetworkDiskCachePrivate * const d = d_func(); | - | ||||||||||||||||||
380 | if (d->currentCacheSize >= 0
| 32-98 | ||||||||||||||||||
381 | return executed 66 times by 3 tests: d->currentCacheSize;return d->currentCacheSize; Executed by:
executed 66 times by 3 tests: return d->currentCacheSize; Executed by:
| 66 | ||||||||||||||||||
382 | - | |||||||||||||||||||
383 | if (cacheDirectory().isEmpty()
| 1-96 | ||||||||||||||||||
384 | QMessageLogger(__FILE__, 524525, __PRETTY_FUNCTION__).warning() << ("QNetworkDiskCache::expire() The cache directory is not set";); | - | ||||||||||||||||||
385 | return executed 1 time by 1 test: 0;return 0; Executed by:
executed 1 time by 1 test: return 0; Executed by:
| 1 | ||||||||||||||||||
386 | } | - | ||||||||||||||||||
387 | - | |||||||||||||||||||
388 | - | |||||||||||||||||||
389 | d->lastItem.reset(); | - | ||||||||||||||||||
390 | - | |||||||||||||||||||
391 | QDir::Filters filters = QDir::AllDirs | QDir:: Files | QDir::NoDotAndDotDot; | - | ||||||||||||||||||
392 | QDirIterator it(cacheDirectory(), filters, QDirIterator::Subdirectories); | - | ||||||||||||||||||
393 | - | |||||||||||||||||||
394 | QMultiMap<QDateTime, QString> cacheItems; | - | ||||||||||||||||||
395 | qint64 totalSize = 0; | - | ||||||||||||||||||
396 | while (it.hasNext()
| 96-1827 | ||||||||||||||||||
397 | QString path = it.next(); | - | ||||||||||||||||||
398 | QFileInfo info = it.fileInfo(); | - | ||||||||||||||||||
399 | QString fileName = info.fileName(); | - | ||||||||||||||||||
400 | if (fileName.endsWith(QLatin1String(".d"))
| 98-1729 | ||||||||||||||||||
401 | cacheItems.insert(info.created(), path); | - | ||||||||||||||||||
402 | totalSize += info.size(); | - | ||||||||||||||||||
403 | } executed 98 times by 2 tests: end of block Executed by:
| 98 | ||||||||||||||||||
404 | } executed 1827 times by 3 tests: end of block Executed by:
| 1827 | ||||||||||||||||||
405 | - | |||||||||||||||||||
406 | int removedFiles = 0; | - | ||||||||||||||||||
407 | qint64 goal = (maximumCacheSize() * 9) / 10; | - | ||||||||||||||||||
408 | QMultiMap<QDateTime, QString>::const_iterator i = cacheItems.constBegin(); | - | ||||||||||||||||||
409 | while (i != cacheItems.constEnd()
| 53-66 | ||||||||||||||||||
410 | if (totalSize < goal
| 23-30 | ||||||||||||||||||
411 | break; executed 30 times by 2 tests: break; Executed by:
| 30 | ||||||||||||||||||
412 | QString name = i.value(); | - | ||||||||||||||||||
413 | QFile file(name); | - | ||||||||||||||||||
414 | - | |||||||||||||||||||
415 | if (name.contains(QLatin1String("prepared/"))
| 1-22 | ||||||||||||||||||
416 | QHashIterator<QIODevice*,for (QCacheItem *> iterator*item : qAsConst(d->inserting); | - | ||||||||||||||||||
while (iterator.hasNext()))) { | ||||||||||||||||||||
417 | iterator.next(); | 0-1 | ||||||||||||||||||
QCacheItem *item = iterator.value();if (item
| ||||||||||||||||||||
418 | delete item->file; | - | ||||||||||||||||||
419 | item->file = 0; | - | ||||||||||||||||||
420 | break; executed 1 time by 1 test: break; Executed by:
| 1 | ||||||||||||||||||
421 | } | - | ||||||||||||||||||
422 | } never executed: end of block | 0 | ||||||||||||||||||
423 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||||||||
424 | - | |||||||||||||||||||
425 | qint64 size = file.size(); | - | ||||||||||||||||||
426 | file.remove(); | - | ||||||||||||||||||
427 | totalSize -= size; | - | ||||||||||||||||||
428 | ++removedFiles; | - | ||||||||||||||||||
429 | ++i; | - | ||||||||||||||||||
430 | } executed 23 times by 1 test: end of block Executed by:
| 23 | ||||||||||||||||||
431 | - | |||||||||||||||||||
432 | - | |||||||||||||||||||
433 | - | |||||||||||||||||||
434 | - | |||||||||||||||||||
435 | - | |||||||||||||||||||
436 | - | |||||||||||||||||||
437 | - | |||||||||||||||||||
438 | return executed 96 times by 3 tests: totalSize;return totalSize; Executed by:
executed 96 times by 3 tests: return totalSize; Executed by:
| 96 | ||||||||||||||||||
439 | } | - | ||||||||||||||||||
440 | - | |||||||||||||||||||
441 | - | |||||||||||||||||||
442 | - | |||||||||||||||||||
443 | - | |||||||||||||||||||
444 | void QNetworkDiskCache::clear() | - | ||||||||||||||||||
445 | { | - | ||||||||||||||||||
446 | - | |||||||||||||||||||
447 | - | |||||||||||||||||||
448 | - | |||||||||||||||||||
449 | QNetworkDiskCachePrivate * const d = d_func(); | - | ||||||||||||||||||
450 | qint64 size = d->maximumCacheSize; | - | ||||||||||||||||||
451 | d->maximumCacheSize = 0; | - | ||||||||||||||||||
452 | d->currentCacheSize = expire(); | - | ||||||||||||||||||
453 | d->maximumCacheSize = size; | - | ||||||||||||||||||
454 | } executed 65 times by 2 tests: end of block Executed by:
| 65 | ||||||||||||||||||
455 | - | |||||||||||||||||||
456 | - | |||||||||||||||||||
457 | - | |||||||||||||||||||
458 | - | |||||||||||||||||||
459 | QString QNetworkDiskCachePrivate::uniqueFileName(const QUrl &url) | - | ||||||||||||||||||
460 | { | - | ||||||||||||||||||
461 | QUrl cleanUrl = url; | - | ||||||||||||||||||
462 | cleanUrl.setPassword(QString()); | - | ||||||||||||||||||
463 | cleanUrl.setFragment(QString()); | - | ||||||||||||||||||
464 | - | |||||||||||||||||||
465 | QCryptographicHash hash(QCryptographicHash::Sha1); | - | ||||||||||||||||||
466 | hash.addData(cleanUrl.toEncoded()); | - | ||||||||||||||||||
467 | - | |||||||||||||||||||
468 | QByteArray id = QByteArray::number(*(qlonglong*)hash.result().data(), 36).left(8); | - | ||||||||||||||||||
469 | - | |||||||||||||||||||
470 | uint code = (uint)id.at(id.length()-1) % 16; | - | ||||||||||||||||||
471 | QString pathFragment = QString::number(code, 16) + QLatin1Char('/') | - | ||||||||||||||||||
472 | + QLatin1String(id) + QLatin1String(".d"); | - | ||||||||||||||||||
473 | - | |||||||||||||||||||
474 | return pathFragment; | - | ||||||||||||||||||
475 | } | - | ||||||||||||||||||
476 | - | |||||||||||||||||||
477 | QString QNetworkDiskCachePrivate::tmpCacheFileName() const | - | ||||||||||||||||||
478 | { | - | ||||||||||||||||||
479 | - | |||||||||||||||||||
480 | return cacheDirectory + QLatin1String("prepared/") + QLatin1String("XXXXXX") + QLatin1String(".d"); | - | ||||||||||||||||||
481 | } | - | ||||||||||||||||||
482 | - | |||||||||||||||||||
483 | - | |||||||||||||||||||
484 | - | |||||||||||||||||||
485 | - | |||||||||||||||||||
486 | QString QNetworkDiskCachePrivate::cacheFileName(const QUrl &url) const | - | ||||||||||||||||||
487 | { | - | ||||||||||||||||||
488 | if (!url.isValid()) | - | ||||||||||||||||||
489 | return QString(); | - | ||||||||||||||||||
490 | - | |||||||||||||||||||
491 | QString fullpath = dataDirectory + uniqueFileName(url); | - | ||||||||||||||||||
492 | return fullpath; | - | ||||||||||||||||||
493 | } | - | ||||||||||||||||||
494 | - | |||||||||||||||||||
495 | - | |||||||||||||||||||
496 | - | |||||||||||||||||||
497 | - | |||||||||||||||||||
498 | bool QCacheItem::canCompress() const | - | ||||||||||||||||||
499 | { | - | ||||||||||||||||||
500 | bool sizeOk = false; | - | ||||||||||||||||||
501 | bool typeOk = false; | - | ||||||||||||||||||
502 | for (QForeachContainer<typename QtPrivate::remove_reference<decltype(metaData.rawHeaders())>::type> _container_((const auto headers = metaData.rawHeaders())); _container_.control && _container_.i != _container_.e; ++_container_.i, _container_.control ^= 1)(); | - | ||||||||||||||||||
503 | for (const QNetworkCacheMetaData::RawHeaderauto &header = *_container_.i; _container_.control; _container_.control = 0: headers) { | - | ||||||||||||||||||
504 | if (header.first.toLower() == "content-length"
| 26-496 | ||||||||||||||||||
505 | qint64 size = header.second.toLongLong(); | - | ||||||||||||||||||
506 | if (size > (1024 * 1024 * 3)
| 0-26 | ||||||||||||||||||
507 | return never executed: false;return false; never executed: return false; | 0 | ||||||||||||||||||
508 | else | - | ||||||||||||||||||
509 | sizeOk = true; executed 26 times by 2 tests: sizeOk = true; Executed by:
| 26 | ||||||||||||||||||
510 | } | - | ||||||||||||||||||
511 | - | |||||||||||||||||||
512 | if (header.first.toLower() == "content-type"
| 140-382 | ||||||||||||||||||
513 | QByteArray type = header.second; | - | ||||||||||||||||||
514 | if (type.startsWith("text/")
| 0-140 | ||||||||||||||||||
515 | || (type.startsWith("application/")
| 0 | ||||||||||||||||||
516 | && (type.endsWith("javascript")
| 0 | ||||||||||||||||||
517 | typeOk = true; executed 140 times by 3 tests: typeOk = true; Executed by:
| 140 | ||||||||||||||||||
518 | else | - | ||||||||||||||||||
519 | return never executed: false;return false; never executed: return false; | 0 | ||||||||||||||||||
520 | } | - | ||||||||||||||||||
521 | if (sizeOk
| 24-472 | ||||||||||||||||||
522 | return executed 24 times by 1 test: true;return true; Executed by:
executed 24 times by 1 test: return true; Executed by:
| 24 | ||||||||||||||||||
523 | } executed 498 times by 3 tests: end of block Executed by:
| 498 | ||||||||||||||||||
524 | return executed 150 times by 3 tests: false;return false; Executed by:
executed 150 times by 3 tests: return false; Executed by:
| 150 | ||||||||||||||||||
525 | } | - | ||||||||||||||||||
526 | - | |||||||||||||||||||
527 | enum | - | ||||||||||||||||||
528 | { | - | ||||||||||||||||||
529 | CacheMagic = 0xe8, | - | ||||||||||||||||||
530 | CurrentCacheVersion = 8 | - | ||||||||||||||||||
531 | }; | - | ||||||||||||||||||
532 | - | |||||||||||||||||||
533 | void QCacheItem::writeHeader(QFile *device) const | - | ||||||||||||||||||
534 | { | - | ||||||||||||||||||
535 | QDataStream out(device); | - | ||||||||||||||||||
536 | - | |||||||||||||||||||
537 | out << qint32(CacheMagic); | - | ||||||||||||||||||
538 | out << qint32(CurrentCacheVersion); | - | ||||||||||||||||||
539 | out << static_cast<qint32>(out.version()); | - | ||||||||||||||||||
540 | out << metaData; | - | ||||||||||||||||||
541 | bool compressed = canCompress(); | - | ||||||||||||||||||
542 | out << compressed; | - | ||||||||||||||||||
543 | } | - | ||||||||||||||||||
544 | - | |||||||||||||||||||
545 | void QCacheItem::writeCompressedData(QFile *device) const | - | ||||||||||||||||||
546 | { | - | ||||||||||||||||||
547 | QDataStream out(device); | - | ||||||||||||||||||
548 | - | |||||||||||||||||||
549 | out << qCompress(data.data()); | - | ||||||||||||||||||
550 | } | - | ||||||||||||||||||
551 | - | |||||||||||||||||||
552 | - | |||||||||||||||||||
553 | - | |||||||||||||||||||
554 | - | |||||||||||||||||||
555 | - | |||||||||||||||||||
556 | bool QCacheItem::read(QFile *device, bool readData) | - | ||||||||||||||||||
557 | { | - | ||||||||||||||||||
558 | reset(); | - | ||||||||||||||||||
559 | - | |||||||||||||||||||
560 | QDataStream in(device); | - | ||||||||||||||||||
561 | - | |||||||||||||||||||
562 | qint32 marker; | - | ||||||||||||||||||
563 | qint32 v; | - | ||||||||||||||||||
564 | in >> marker; | - | ||||||||||||||||||
565 | in >> v; | - | ||||||||||||||||||
566 | if (marker != CacheMagic) | - | ||||||||||||||||||
567 | return true; | - | ||||||||||||||||||
568 | - | |||||||||||||||||||
569 | - | |||||||||||||||||||
570 | if (v != CurrentCacheVersion) | - | ||||||||||||||||||
571 | return false; | - | ||||||||||||||||||
572 | - | |||||||||||||||||||
573 | qint32 streamVersion; | - | ||||||||||||||||||
574 | in >> streamVersion; | - | ||||||||||||||||||
575 | - | |||||||||||||||||||
576 | if (streamVersion > in.version()) | - | ||||||||||||||||||
577 | return false; | - | ||||||||||||||||||
578 | in.setVersion(streamVersion); | - | ||||||||||||||||||
579 | - | |||||||||||||||||||
580 | bool compressed; | - | ||||||||||||||||||
581 | QByteArray dataBA; | - | ||||||||||||||||||
582 | in >> metaData; | - | ||||||||||||||||||
583 | in >> compressed; | - | ||||||||||||||||||
584 | if (readData && compressed) { | - | ||||||||||||||||||
585 | in >> dataBA; | - | ||||||||||||||||||
586 | data.setData(qUncompress(dataBA)); | - | ||||||||||||||||||
587 | data.open(QBuffer::ReadOnly); | - | ||||||||||||||||||
588 | } | - | ||||||||||||||||||
589 | - | |||||||||||||||||||
590 | - | |||||||||||||||||||
591 | QString expectedFilename = QNetworkDiskCachePrivate::uniqueFileName(metaData.url()); | - | ||||||||||||||||||
592 | if (!device->fileName().endsWith(expectedFilename)) | - | ||||||||||||||||||
593 | return false; | - | ||||||||||||||||||
594 | - | |||||||||||||||||||
595 | return metaData.isValid(); | - | ||||||||||||||||||
596 | } | - | ||||||||||||||||||
597 | - | |||||||||||||||||||
598 | - | |||||||||||||||||||
Switch to Source code | Preprocessed file |