qnetworkdiskcache.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/network/access/qnetworkdiskcache.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
1/****************************************************************************-
2**-
3** Copyright (C) 2016 The Qt Company Ltd.-
4** Contact: https://www.qt.io/licensing/-
5**-
6** This file is part of the QtNetwork module of the Qt Toolkit.-
7**-
8** $QT_BEGIN_LICENSE:LGPL$-
9** Commercial License Usage-
10** Licensees holding valid commercial Qt licenses may use this file in-
11** accordance with the commercial license agreement provided with the-
12** Software or, alternatively, in accordance with the terms contained in-
13** a written agreement between you and The Qt Company. For licensing terms-
14** and conditions see https://www.qt.io/terms-conditions. For further-
15** information use the contact form at https://www.qt.io/contact-us.-
16**-
17** GNU Lesser General Public License Usage-
18** Alternatively, this file may be used under the terms of the GNU Lesser-
19** General Public License version 3 as published by the Free Software-
20** Foundation and appearing in the file LICENSE.LGPL3 included in the-
21** packaging of this file. Please review the following information to-
22** ensure the GNU Lesser General Public License version 3 requirements-
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.-
24**-
25** GNU General Public License Usage-
26** Alternatively, this file may be used under the terms of the GNU-
27** General Public License version 2.0 or (at your option) the GNU General-
28** Public license version 3 or any later version approved by the KDE Free-
29** Qt Foundation. The licenses are as published by the Free Software-
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3-
31** included in the packaging of this file. Please review the following-
32** information to ensure the GNU General Public License requirements will-
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and-
34** https://www.gnu.org/licenses/gpl-3.0.html.-
35**-
36** $QT_END_LICENSE$-
37**-
38****************************************************************************/-
39-
40//#define QNETWORKDISKCACHE_DEBUG-
41-
42-
43#include "qnetworkdiskcache.h"-
44#include "qnetworkdiskcache_p.h"-
45#include "QtCore/qscopedpointer.h"-
46-
47#include <qfile.h>-
48#include <qdir.h>-
49#include <qdatastream.h>-
50#include <qdatetime.h>-
51#include <qdiriterator.h>-
52#include <qurl.h>-
53#include <qcryptographichash.h>-
54#include <qdebug.h>-
55-
56#define CACHE_POSTFIX QLatin1String(".d")-
57#define PREPARED_SLASH QLatin1String("prepared/")-
58#define CACHE_VERSION 8-
59#define DATA_DIR QLatin1String("data")-
60-
61#define MAX_COMPRESSION_SIZE (1024 * 1024 * 3)-
62-
63#ifndef QT_NO_NETWORKDISKCACHE-
64-
65QT_BEGIN_NAMESPACE-
66-
67/*!-
68 \class QNetworkDiskCache-
69 \since 4.5-
70 \inmodule QtNetwork-
71-
72 \brief The QNetworkDiskCache class provides a very basic disk cache.-
73-
74 QNetworkDiskCache stores each url in its own file inside of the-
75 cacheDirectory using QDataStream. Files with a text MimeType-
76 are compressed using qCompress. Data is written to disk only in insert()-
77 and updateMetaData().-
78-
79 Currently you cannot share the same cache files with more than-
80 one disk cache.-
81-
82 QNetworkDiskCache by default limits the amount of space that the cache will-
83 use on the system to 50MB.-
84-
85 Note you have to set the cache directory before it will work.-
86-
87 A network disk cache can be enabled by:-
88-
89 \snippet code/src_network_access_qnetworkdiskcache.cpp 0-
90-
91 When sending requests, to control the preference of when to use the cache-
92 and when to use the network, consider the following:-
93-
94 \snippet code/src_network_access_qnetworkdiskcache.cpp 1-
95-
96 To check whether the response came from the cache or from the network, the-
97 following can be applied:-
98-
99 \snippet code/src_network_access_qnetworkdiskcache.cpp 2-
100*/-
101-
102/*!-
103 Creates a new disk cache. The \a parent argument is passed to-
104 QAbstractNetworkCache's constructor.-
105 */-
106QNetworkDiskCache::QNetworkDiskCache(QObject *parent)-
107 : QAbstractNetworkCache(*new QNetworkDiskCachePrivate, parent)-
108{-
109}
executed 66 times by 3 tests: end of block
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
66
110-
111/*!-
112 Destroys the cache object. This does not clear the disk cache.-
113 */-
114QNetworkDiskCache::~QNetworkDiskCache()-
115{-
116 Q_D(QNetworkDiskCache);-
117 qDeleteAll(d->inserting);-
118}
executed 66 times by 3 tests: end of block
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
66
119-
120/*!-
121 Returns the location where cached files will be stored.-
122*/-
123QString QNetworkDiskCache::cacheDirectory() const-
124{-
125 Q_D(const QNetworkDiskCache);-
126 return d->cacheDirectory;
executed 226 times by 3 tests: return d->cacheDirectory;
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
226
127}-
128-
129/*!-
130 Sets the directory where cached files will be stored to \a cacheDir-
131-
132 QNetworkDiskCache will create this directory if it does not exists.-
133-
134 Prepared cache items will be stored in the new cache directory when-
135 they are inserted.-
136-
137 \sa QDesktopServices::CacheLocation-
138*/-
139void QNetworkDiskCache::setCacheDirectory(const QString &cacheDir)-
140{-
141#if defined(QNETWORKDISKCACHE_DEBUG)-
142 qDebug() << "QNetworkDiskCache::setCacheDirectory()" << cacheDir;-
143#endif-
144 Q_D(QNetworkDiskCache);-
145 if (cacheDir.isEmpty())
cacheDir.isEmpty()Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tst_QNetworkDiskCache
FALSEevaluated 65 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
2-65
146 return;
executed 2 times by 1 test: return;
Executed by:
  • tst_QNetworkDiskCache
2
147 d->cacheDirectory = cacheDir;-
148 QDir dir(d->cacheDirectory);-
149 d->cacheDirectory = dir.absolutePath();-
150 if (!d->cacheDirectory.endsWith(QLatin1Char('/')))
!d->cacheDirec...tin1Char('/'))Description
TRUEevaluated 65 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
FALSEnever evaluated
0-65
151 d->cacheDirectory += QLatin1Char('/');
executed 65 times by 3 tests: d->cacheDirectory += QLatin1Char('/');
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
65
152-
153 d->dataDirectory = d->cacheDirectory + DATA_DIR + QString::number(CACHE_VERSION) + QLatin1Char('/');-
154 d->prepareLayout();-
155}
executed 65 times by 3 tests: end of block
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
65
156-
157/*!-
158 \reimp-
159*/-
160qint64 QNetworkDiskCache::cacheSize() const-
161{-
162#if defined(QNETWORKDISKCACHE_DEBUG)-
163 qDebug("QNetworkDiskCache::cacheSize()");-
164#endif-
165 Q_D(const QNetworkDiskCache);-
166 if (d->cacheDirectory.isEmpty())
d->cacheDirectory.isEmpty()Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • tst_QNetworkDiskCache
FALSEevaluated 4 times by 1 test
Evaluated by:
  • tst_QNetworkDiskCache
1-4
167 return 0;
executed 1 time by 1 test: return 0;
Executed by:
  • tst_QNetworkDiskCache
1
168 if (d->currentCacheSize < 0) {
d->currentCacheSize < 0Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • tst_QNetworkDiskCache
FALSEevaluated 3 times by 1 test
Evaluated by:
  • tst_QNetworkDiskCache
1-3
169 QNetworkDiskCache *that = const_cast<QNetworkDiskCache*>(this);-
170 that->d_func()->currentCacheSize = that->expire();-
171 }
executed 1 time by 1 test: end of block
Executed by:
  • tst_QNetworkDiskCache
1
172 return d->currentCacheSize;
executed 4 times by 1 test: return d->currentCacheSize;
Executed by:
  • tst_QNetworkDiskCache
4
173}-
174-
175/*!-
176 \reimp-
177*/-
178QIODevice *QNetworkDiskCache::prepare(const QNetworkCacheMetaData &metaData)-
179{-
180#if defined(QNETWORKDISKCACHE_DEBUG)-
181 qDebug() << "QNetworkDiskCache::prepare()" << metaData.url();-
182#endif-
183 Q_D(QNetworkDiskCache);-
184 if (!metaData.isValid() || !metaData.url().isValid() || !metaData.saveToDisk())
!metaData.isValid()Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • tst_QNetworkDiskCache
FALSEevaluated 95 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
!metaData.url().isValid()Description
TRUEnever evaluated
FALSEevaluated 95 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
!metaData.saveToDisk()Description
TRUEevaluated 7 times by 1 test
Evaluated by:
  • tst_QAbstractNetworkCache
FALSEevaluated 88 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
0-95
185 return 0;
executed 8 times by 2 tests: return 0;
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
8
186-
187 if (d->cacheDirectory.isEmpty()) {
d->cacheDirectory.isEmpty()Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • tst_QNetworkDiskCache
FALSEevaluated 87 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
1-87
188 qWarning("QNetworkDiskCache::prepare() The cache directory is not set");-
189 return 0;
executed 1 time by 1 test: return 0;
Executed by:
  • tst_QNetworkDiskCache
1
190 }-
191-
192 const auto headers = metaData.rawHeaders();-
193 for (const auto &header : headers) {-
194 if (header.first.toLower() == "content-length") {
header.first.t...ontent-length"Description
TRUEevaluated 13 times by 2 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
FALSEevaluated 236 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
13-236
195 const qint64 size = header.second.toLongLong();-
196 if (size > (maximumCacheSize() * 3)/4)
size > (maximu...eSize() * 3)/4Description
TRUEnever evaluated
FALSEevaluated 13 times by 2 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
0-13
197 return 0;
never executed: return 0;
0
198 break;
executed 13 times by 2 tests: break;
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
13
199 }-
200 }
executed 236 times by 3 tests: end of block
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
236
201 QScopedPointer<QCacheItem> cacheItem(new QCacheItem);-
202 cacheItem->metaData = metaData;-
203-
204 QIODevice *device = 0;-
205 if (cacheItem->canCompress()) {
cacheItem->canCompress()Description
TRUEevaluated 12 times by 1 test
Evaluated by:
  • tst_QAbstractNetworkCache
FALSEevaluated 75 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
12-75
206 cacheItem->data.open(QBuffer::ReadWrite);-
207 device = &(cacheItem->data);-
208 } else {
executed 12 times by 1 test: end of block
Executed by:
  • tst_QAbstractNetworkCache
12
209 QString templateName = d->tmpCacheFileName();-
210 QT_TRY {-
211 cacheItem->file = new QTemporaryFile(templateName, &cacheItem->data);-
212 } QT_CATCH(...) {
executed 75 times by 3 tests: end of block
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
dead code: { cacheItem->file = 0; }
-
213 cacheItem->file = 0;
dead code: { cacheItem->file = 0; }
-
214 }
dead code: { cacheItem->file = 0; }
-
215 if (!cacheItem->file || !cacheItem->file->open()) {
!cacheItem->fileDescription
TRUEnever evaluated
FALSEevaluated 75 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
!cacheItem->file->open()Description
TRUEnever evaluated
FALSEevaluated 75 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
0-75
216 qWarning("QNetworkDiskCache::prepare() unable to open temporary file");-
217 cacheItem.reset();-
218 return 0;
never executed: return 0;
0
219 }-
220 cacheItem->writeHeader(cacheItem->file);-
221 device = cacheItem->file;-
222 }
executed 75 times by 3 tests: end of block
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
75
223 d->inserting[device] = cacheItem.take();-
224 return device;
executed 87 times by 3 tests: return device;
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
87
225}-
226-
227/*!-
228 \reimp-
229*/-
230void QNetworkDiskCache::insert(QIODevice *device)-
231{-
232#if defined(QNETWORKDISKCACHE_DEBUG)-
233 qDebug() << "QNetworkDiskCache::insert()" << device;-
234#endif-
235 Q_D(QNetworkDiskCache);-
236 const auto it = d->inserting.constFind(device);-
237 if (Q_UNLIKELY(it == d->inserting.cend())) {
__builtin_expe...end()), false)Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • tst_QNetworkDiskCache
FALSEevaluated 84 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
1-84
238 qWarning() << "QNetworkDiskCache::insert() called on a device we don't know about" << device;-
239 return;
executed 1 time by 1 test: return;
Executed by:
  • tst_QNetworkDiskCache
1
240 }-
241-
242 d->storeItem(it.value());-
243 delete it.value();-
244 d->inserting.erase(it);-
245}
executed 84 times by 3 tests: end of block
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
84
246-
247-
248/*!-
249 Create subdirectories and other housekeeping on the filesystem.-
250 Prevents too many files from being present in any single directory.-
251*/-
252void QNetworkDiskCachePrivate::prepareLayout()-
253{-
254 QDir helper;-
255 helper.mkpath(cacheDirectory + PREPARED_SLASH);-
256-
257 //Create directory and subdirectories 0-F-
258 helper.mkpath(dataDirectory);-
259 for (uint i = 0; i < 16 ; i++) {
i < 16Description
TRUEevaluated 1040 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
FALSEevaluated 65 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
65-1040
260 QString str = QString::number(i, 16);-
261 QString subdir = dataDirectory + str;-
262 helper.mkdir(subdir);-
263 }
executed 1040 times by 3 tests: end of block
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
1040
264}
executed 65 times by 3 tests: end of block
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
65
265-
266-
267void QNetworkDiskCachePrivate::storeItem(QCacheItem *cacheItem)-
268{-
269 Q_Q(QNetworkDiskCache);-
270 Q_ASSERT(cacheItem->metaData.saveToDisk());-
271-
272 QString fileName = cacheFileName(cacheItem->metaData.url());-
273 Q_ASSERT(!fileName.isEmpty());-
274-
275 if (QFile::exists(fileName)) {
QFile::exists(fileName)Description
TRUEevaluated 19 times by 2 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
FALSEevaluated 65 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
19-65
276 if (!QFile::remove(fileName)) {
!QFile::remove(fileName)Description
TRUEnever evaluated
FALSEevaluated 19 times by 2 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
0-19
277 qWarning() << "QNetworkDiskCache: couldn't remove the cache file " << fileName;-
278 return;
never executed: return;
0
279 }-
280 }
executed 19 times by 2 tests: end of block
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
19
281-
282 if (currentCacheSize > 0)
currentCacheSize > 0Description
TRUEevaluated 29 times by 2 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
FALSEevaluated 55 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
29-55
283 currentCacheSize += 1024 + cacheItem->size();
executed 29 times by 2 tests: currentCacheSize += 1024 + cacheItem->size();
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
29
284 currentCacheSize = q->expire();-
285 if (!cacheItem->file) {
!cacheItem->fileDescription
TRUEevaluated 12 times by 1 test
Evaluated by:
  • tst_QAbstractNetworkCache
FALSEevaluated 72 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
12-72
286 QString templateName = tmpCacheFileName();-
287 cacheItem->file = new QTemporaryFile(templateName, &cacheItem->data);-
288 if (cacheItem->file->open()) {
cacheItem->file->open()Description
TRUEevaluated 12 times by 1 test
Evaluated by:
  • tst_QAbstractNetworkCache
FALSEnever evaluated
0-12
289 cacheItem->writeHeader(cacheItem->file);-
290 cacheItem->writeCompressedData(cacheItem->file);-
291 }
executed 12 times by 1 test: end of block
Executed by:
  • tst_QAbstractNetworkCache
12
292 }
executed 12 times by 1 test: end of block
Executed by:
  • tst_QAbstractNetworkCache
12
293-
294 if (cacheItem->file
cacheItem->fileDescription
TRUEevaluated 84 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
FALSEnever evaluated
0-84
295 && cacheItem->file->isOpen()
cacheItem->file->isOpen()Description
TRUEevaluated 84 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
FALSEnever evaluated
0-84
296 && cacheItem->file->error() == QFile::NoError) {
cacheItem->fil...QFile::NoErrorDescription
TRUEevaluated 84 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
FALSEnever evaluated
0-84
297 cacheItem->file->setAutoRemove(false);-
298 // ### use atomic rename rather then remove & rename-
299 if (cacheItem->file->rename(fileName))
cacheItem->fil...name(fileName)Description
TRUEevaluated 84 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
FALSEnever evaluated
0-84
300 currentCacheSize += cacheItem->file->size();
executed 84 times by 3 tests: currentCacheSize += cacheItem->file->size();
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
84
301 else-
302 cacheItem->file->setAutoRemove(true);
never executed: cacheItem->file->setAutoRemove(true);
0
303 }-
304 if (cacheItem->metaData.url() == lastItem.metaData.url())
cacheItem->met...metaData.url()Description
TRUEevaluated 11 times by 2 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
FALSEevaluated 73 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
11-73
305 lastItem.reset();
executed 11 times by 2 tests: lastItem.reset();
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
11
306}
executed 84 times by 3 tests: end of block
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
84
307-
308/*!-
309 \reimp-
310*/-
311bool QNetworkDiskCache::remove(const QUrl &url)-
312{-
313#if defined(QNETWORKDISKCACHE_DEBUG)-
314 qDebug() << "QNetworkDiskCache::remove()" << url;-
315#endif-
316 Q_D(QNetworkDiskCache);-
317-
318 // remove is also used to cancel insertions, not a common operation-
319 for (auto it = d->inserting.cbegin(), end = d->inserting.cend(); it != end; ++it) {
it != endDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tst_QNetworkDiskCache
FALSEevaluated 14 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
2-14
320 QCacheItem *item = it.value();-
321 if (item && item->metaData.url() == url) {
itemDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tst_QNetworkDiskCache
FALSEnever evaluated
item->metaData.url() == urlDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • tst_QNetworkDiskCache
FALSEevaluated 1 time by 1 test
Evaluated by:
  • tst_QNetworkDiskCache
0-2
322 delete item;-
323 d->inserting.erase(it);-
324 return true;
executed 1 time by 1 test: return true;
Executed by:
  • tst_QNetworkDiskCache
1
325 }-
326 }
executed 1 time by 1 test: end of block
Executed by:
  • tst_QNetworkDiskCache
1
327-
328 if (d->lastItem.metaData.url() == url)
d->lastItem.me...a.url() == urlDescription
TRUEevaluated 3 times by 2 tests
Evaluated by:
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
FALSEevaluated 11 times by 2 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
3-11
329 d->lastItem.reset();
executed 3 times by 2 tests: d->lastItem.reset();
Executed by:
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
3
330 return d->removeFile(d->cacheFileName(url));
executed 14 times by 3 tests: return d->removeFile(d->cacheFileName(url));
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
14
331}-
332-
333/*!-
334 Put all of the misc file removing into one function to be extra safe-
335 */-
336bool QNetworkDiskCachePrivate::removeFile(const QString &file)-
337{-
338#if defined(QNETWORKDISKCACHE_DEBUG)-
339 qDebug() << "QNetworkDiskCache::removFile()" << file;-
340#endif-
341 if (file.isEmpty())
file.isEmpty()Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • tst_QNetworkDiskCache
FALSEevaluated 15 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
1-15
342 return false;
executed 1 time by 1 test: return false;
Executed by:
  • tst_QNetworkDiskCache
1
343 QFileInfo info(file);-
344 QString fileName = info.fileName();-
345 if (!fileName.endsWith(CACHE_POSTFIX))
!fileName.ends...1String(".d"))Description
TRUEnever evaluated
FALSEevaluated 15 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
0-15
346 return false;
never executed: return false;
0
347 qint64 size = info.size();-
348 if (QFile::remove(file)) {
QFile::remove(file)Description
TRUEevaluated 6 times by 2 tests
Evaluated by:
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
FALSEevaluated 9 times by 2 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
6-9
349 currentCacheSize -= size;-
350 return true;
executed 6 times by 2 tests: return true;
Executed by:
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
6
351 }-
352 return false;
executed 9 times by 2 tests: return false;
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
9
353}-
354-
355/*!-
356 \reimp-
357*/-
358QNetworkCacheMetaData QNetworkDiskCache::metaData(const QUrl &url)-
359{-
360#if defined(QNETWORKDISKCACHE_DEBUG)-
361 qDebug() << "QNetworkDiskCache::metaData()" << url;-
362#endif-
363 Q_D(QNetworkDiskCache);-
364 if (d->lastItem.metaData.url() == url)
d->lastItem.me...a.url() == urlDescription
TRUEevaluated 6 times by 2 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
FALSEevaluated 87 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
6-87
365 return d->lastItem.metaData;
executed 6 times by 2 tests: return d->lastItem.metaData;
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
6
366 return fileMetaData(d->cacheFileName(url));
executed 87 times by 3 tests: return fileMetaData(d->cacheFileName(url));
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
87
367}-
368-
369/*!-
370 Returns the QNetworkCacheMetaData for the cache file \a fileName.-
371-
372 If \a fileName is not a cache file QNetworkCacheMetaData will be invalid.-
373 */-
374QNetworkCacheMetaData QNetworkDiskCache::fileMetaData(const QString &fileName) const-
375{-
376#if defined(QNETWORKDISKCACHE_DEBUG)-
377 qDebug() << "QNetworkDiskCache::fileMetaData()" << fileName;-
378#endif-
379 Q_D(const QNetworkDiskCache);-
380 QFile file(fileName);-
381 if (!file.open(QFile::ReadOnly))
!file.open(QFile::ReadOnly)Description
TRUEevaluated 50 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
FALSEevaluated 48 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
48-50
382 return QNetworkCacheMetaData();
executed 50 times by 3 tests: return QNetworkCacheMetaData();
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
50
383 if (!d->lastItem.read(&file, false)) {
!d->lastItem.r...(&file, false)Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tst_QNetworkDiskCache
FALSEevaluated 46 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
2-46
384 file.close();-
385 QNetworkDiskCachePrivate *that = const_cast<QNetworkDiskCachePrivate*>(d);-
386 that->removeFile(fileName);-
387 }
executed 2 times by 1 test: end of block
Executed by:
  • tst_QNetworkDiskCache
2
388 return d->lastItem.metaData;
executed 48 times by 3 tests: return d->lastItem.metaData;
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
48
389}-
390-
391/*!-
392 \reimp-
393*/-
394QIODevice *QNetworkDiskCache::data(const QUrl &url)-
395{-
396#if defined(QNETWORKDISKCACHE_DEBUG)-
397 qDebug() << "QNetworkDiskCache::data()" << url;-
398#endif-
399 Q_D(QNetworkDiskCache);-
400 QScopedPointer<QBuffer> buffer;-
401 if (!url.isValid())
!url.isValid()Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • tst_QNetworkDiskCache
FALSEevaluated 41 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
1-41
402 return 0;
executed 1 time by 1 test: return 0;
Executed by:
  • tst_QNetworkDiskCache
1
403 if (d->lastItem.metaData.url() == url && d->lastItem.data.isOpen()) {
d->lastItem.me...a.url() == urlDescription
TRUEevaluated 27 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
FALSEevaluated 14 times by 2 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
d->lastItem.data.isOpen()Description
TRUEnever evaluated
FALSEevaluated 27 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
0-27
404 buffer.reset(new QBuffer);-
405 buffer->setData(d->lastItem.data.data());-
406 } else {
never executed: end of block
0
407 QScopedPointer<QFile> file(new QFile(d->cacheFileName(url)));-
408 if (!file->open(QFile::ReadOnly | QIODevice::Unbuffered))
!file->open(QF...e::Unbuffered)Description
TRUEnever evaluated
FALSEevaluated 41 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
0-41
409 return 0;
never executed: return 0;
0
410-
411 if (!d->lastItem.read(file.data(), true)) {
!d->lastItem.r....data(), true)Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • tst_QNetworkDiskCache
FALSEevaluated 40 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
1-40
412 file->close();-
413 remove(url);-
414 return 0;
executed 1 time by 1 test: return 0;
Executed by:
  • tst_QNetworkDiskCache
1
415 }-
416 if (d->lastItem.data.isOpen()) {
d->lastItem.data.isOpen()Description
TRUEevaluated 11 times by 2 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
FALSEevaluated 29 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
11-29
417 // compressed-
418 buffer.reset(new QBuffer);-
419 buffer->setData(d->lastItem.data.data());-
420 } else {
executed 11 times by 2 tests: end of block
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
11
421 buffer.reset(new QBuffer);-
422 // ### verify that QFile uses the fd size and not the file name-
423 qint64 size = file->size() - file->pos();-
424 const uchar *p = 0;-
425#if !defined(Q_OS_WINCE) && !defined(Q_OS_INTEGRITY)-
426 p = file->map(file->pos(), size);-
427#endif-
428 if (p) {
pDescription
TRUEevaluated 29 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
FALSEnever evaluated
0-29
429 buffer->setData((const char *)p, size);-
430 file.take()->setParent(buffer.data());-
431 } else {
executed 29 times by 3 tests: end of block
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
29
432 buffer->setData(file->readAll());-
433 }
never executed: end of block
0
434 }-
435 }-
436 buffer->open(QBuffer::ReadOnly);-
437 return buffer.take();
executed 40 times by 3 tests: return buffer.take();
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
40
438}-
439-
440/*!-
441 \reimp-
442*/-
443void QNetworkDiskCache::updateMetaData(const QNetworkCacheMetaData &metaData)-
444{-
445#if defined(QNETWORKDISKCACHE_DEBUG)-
446 qDebug() << "QNetworkDiskCache::updateMetaData()" << metaData.url();-
447#endif-
448 QUrl url = metaData.url();-
449 QIODevice *oldDevice = data(url);-
450 if (!oldDevice) {
!oldDeviceDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • tst_QNetworkDiskCache
FALSEevaluated 6 times by 2 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
1-6
451#if defined(QNETWORKDISKCACHE_DEBUG)-
452 qDebug("QNetworkDiskCache::updateMetaData(), no device!");-
453#endif-
454 return;
executed 1 time by 1 test: return;
Executed by:
  • tst_QNetworkDiskCache
1
455 }-
456-
457 QIODevice *newDevice = prepare(metaData);-
458 if (!newDevice) {
!newDeviceDescription
TRUEnever evaluated
FALSEevaluated 6 times by 2 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
0-6
459#if defined(QNETWORKDISKCACHE_DEBUG)-
460 qDebug() << "QNetworkDiskCache::updateMetaData(), no new device!" << url;-
461#endif-
462 return;
never executed: return;
0
463 }-
464 char data[1024];-
465 while (!oldDevice->atEnd()) {
!oldDevice->atEnd()Description
TRUEevaluated 6 times by 2 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
FALSEevaluated 6 times by 2 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
6
466 qint64 s = oldDevice->read(data, 1024);-
467 newDevice->write(data, s);-
468 }
executed 6 times by 2 tests: end of block
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
6
469 delete oldDevice;-
470 insert(newDevice);-
471}
executed 6 times by 2 tests: end of block
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
6
472-
473/*!-
474 Returns the current maximum size for the disk cache.-
475-
476 \sa setMaximumCacheSize()-
477 */-
478qint64 QNetworkDiskCache::maximumCacheSize() const-
479{-
480 Q_D(const QNetworkDiskCache);-
481 return d->maximumCacheSize;
executed 208 times by 3 tests: return d->maximumCacheSize;
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
208
482}-
483-
484/*!-
485 Sets the maximum size of the disk cache to be \a size.-
486-
487 If the new size is smaller then the current cache size then the cache will call expire().-
488-
489 \sa maximumCacheSize()-
490 */-
491void QNetworkDiskCache::setMaximumCacheSize(qint64 size)-
492{-
493 Q_D(QNetworkDiskCache);-
494 bool expireCache = (size < d->maximumCacheSize);-
495 d->maximumCacheSize = size;-
496 if (expireCache)
expireCacheDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • tst_QNetworkDiskCache
FALSEnever evaluated
0-1
497 d->currentCacheSize = expire();
executed 1 time by 1 test: d->currentCacheSize = expire();
Executed by:
  • tst_QNetworkDiskCache
1
498}
executed 1 time by 1 test: end of block
Executed by:
  • tst_QNetworkDiskCache
1
499-
500/*!-
501 Cleans the cache so that its size is under the maximum cache size.-
502 Returns the current size of the cache.-
503-
504 When the current size of the cache is greater than the maximumCacheSize()-
505 older cache files are removed until the total size is less then 90% of-
506 maximumCacheSize() starting with the oldest ones first using the file-
507 creation date to determine how old a cache file is.-
508-
509 Subclasses can reimplement this function to change the order that cache-
510 files are removed taking into account information in the application-
511 knows about that QNetworkDiskCache does not, for example the number of times-
512 a cache is accessed.-
513-
514 \note cacheSize() calls expire if the current cache size is unknown.-
515-
516 \sa maximumCacheSize(), fileMetaData()-
517 */-
518qint64 QNetworkDiskCache::expire()-
519{-
520 Q_D(QNetworkDiskCache);-
521 if (d->currentCacheSize >= 0 && d->currentCacheSize < maximumCacheSize())
d->currentCacheSize >= 0Description
TRUEevaluated 98 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
FALSEevaluated 65 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
d->currentCach...mumCacheSize()Description
TRUEevaluated 66 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
FALSEevaluated 32 times by 1 test
Evaluated by:
  • tst_QNetworkDiskCache
32-98
522 return d->currentCacheSize;
executed 66 times by 3 tests: return d->currentCacheSize;
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
66
523-
524 if (cacheDirectory().isEmpty()) {
cacheDirectory().isEmpty()Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • tst_QNetworkDiskCache
FALSEevaluated 96 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
1-96
525 qWarning("QNetworkDiskCache::expire() The cache directory is not set");-
526 return 0;
executed 1 time by 1 test: return 0;
Executed by:
  • tst_QNetworkDiskCache
1
527 }-
528-
529 // close file handle to prevent "in use" error when QFile::remove() is called-
530 d->lastItem.reset();-
531-
532 QDir::Filters filters = QDir::AllDirs | QDir:: Files | QDir::NoDotAndDotDot;-
533 QDirIterator it(cacheDirectory(), filters, QDirIterator::Subdirectories);-
534-
535 QMultiMap<QDateTime, QString> cacheItems;-
536 qint64 totalSize = 0;-
537 while (it.hasNext()) {
it.hasNext()Description
TRUEevaluated 1827 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
FALSEevaluated 96 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
96-1827
538 QString path = it.next();-
539 QFileInfo info = it.fileInfo();-
540 QString fileName = info.fileName();-
541 if (fileName.endsWith(CACHE_POSTFIX)) {
fileName.endsW...1String(".d"))Description
TRUEevaluated 98 times by 2 tests
Evaluated by:
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
FALSEevaluated 1729 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
98-1729
542 cacheItems.insert(info.created(), path);-
543 totalSize += info.size();-
544 }
executed 98 times by 2 tests: end of block
Executed by:
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
98
545 }
executed 1827 times by 3 tests: end of block
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
1827
546-
547 int removedFiles = 0;-
548 qint64 goal = (maximumCacheSize() * 9) / 10;-
549 QMultiMap<QDateTime, QString>::const_iterator i = cacheItems.constBegin();-
550 while (i != cacheItems.constEnd()) {
i != cacheItems.constEnd()Description
TRUEevaluated 53 times by 2 tests
Evaluated by:
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
FALSEevaluated 66 times by 2 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
53-66
551 if (totalSize < goal)
totalSize < goalDescription
TRUEevaluated 30 times by 2 tests
Evaluated by:
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
FALSEevaluated 23 times by 1 test
Evaluated by:
  • tst_QNetworkDiskCache
23-30
552 break;
executed 30 times by 2 tests: break;
Executed by:
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
30
553 QString name = i.value();-
554 QFile file(name);-
555-
556 if (name.contains(PREPARED_SLASH)) {
name.contains(...("prepared/"))Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • tst_QNetworkDiskCache
FALSEevaluated 22 times by 1 test
Evaluated by:
  • tst_QNetworkDiskCache
1-22
557 for (QCacheItem *item : qAsConst(d->inserting)) {-
558 if (item && item->file && item->file->fileName() == name) {
itemDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • tst_QNetworkDiskCache
FALSEnever evaluated
item->fileDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • tst_QNetworkDiskCache
FALSEnever evaluated
item->file->fileName() == nameDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • tst_QNetworkDiskCache
FALSEnever evaluated
0-1
559 delete item->file;-
560 item->file = 0;-
561 break;
executed 1 time by 1 test: break;
Executed by:
  • tst_QNetworkDiskCache
1
562 }-
563 }
never executed: end of block
0
564 }
executed 1 time by 1 test: end of block
Executed by:
  • tst_QNetworkDiskCache
1
565-
566 qint64 size = file.size();-
567 file.remove();-
568 totalSize -= size;-
569 ++removedFiles;-
570 ++i;-
571 }
executed 23 times by 1 test: end of block
Executed by:
  • tst_QNetworkDiskCache
23
572#if defined(QNETWORKDISKCACHE_DEBUG)-
573 if (removedFiles > 0) {-
574 qDebug() << "QNetworkDiskCache::expire()"-
575 << "Removed:" << removedFiles-
576 << "Kept:" << cacheItems.count() - removedFiles;-
577 }-
578#endif-
579 return totalSize;
executed 96 times by 3 tests: return totalSize;
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
96
580}-
581-
582/*!-
583 \reimp-
584*/-
585void QNetworkDiskCache::clear()-
586{-
587#if defined(QNETWORKDISKCACHE_DEBUG)-
588 qDebug("QNetworkDiskCache::clear()");-
589#endif-
590 Q_D(QNetworkDiskCache);-
591 qint64 size = d->maximumCacheSize;-
592 d->maximumCacheSize = 0;-
593 d->currentCacheSize = expire();-
594 d->maximumCacheSize = size;-
595}
executed 65 times by 2 tests: end of block
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
65
596-
597/*!-
598 Given a URL, generates a unique enough filename (and subdirectory)-
599 */-
600QString QNetworkDiskCachePrivate::uniqueFileName(const QUrl &url)-
601{-
602 QUrl cleanUrl = url;-
603 cleanUrl.setPassword(QString());-
604 cleanUrl.setFragment(QString());-
605-
606 QCryptographicHash hash(QCryptographicHash::Sha1);-
607 hash.addData(cleanUrl.toEncoded());-
608 // convert sha1 to base36 form and return first 8 bytes for use as string-
609 QByteArray id = QByteArray::number(*(qlonglong*)hash.result().data(), 36).left(8);-
610 // generates <one-char subdir>/<8-char filname.d>-
611 uint code = (uint)id.at(id.length()-1) % 16;-
612 QString pathFragment = QString::number(code, 16) + QLatin1Char('/')-
613 + QLatin1String(id) + CACHE_POSTFIX;-
614-
615 return pathFragment;
executed 310 times by 3 tests: return pathFragment;
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
310
616}-
617-
618QString QNetworkDiskCachePrivate::tmpCacheFileName() const-
619{-
620 //The subdirectory is presumed to be already read for use.-
621 return cacheDirectory + PREPARED_SLASH + QLatin1String("XXXXXX") + CACHE_POSTFIX;
executed 87 times by 3 tests: return cacheDirectory + QLatin1String("prepared/") + QLatin1String("XXXXXX") + QLatin1String(".d");
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
87
622}-
623-
624/*!-
625 Generates fully qualified path of cached resource from a URL.-
626 */-
627QString QNetworkDiskCachePrivate::cacheFileName(const QUrl &url) const-
628{-
629 if (!url.isValid())
!url.isValid()Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • tst_QNetworkDiskCache
FALSEevaluated 225 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
1-225
630 return QString();
executed 1 time by 1 test: return QString();
Executed by:
  • tst_QNetworkDiskCache
1
631-
632 QString fullpath = dataDirectory + uniqueFileName(url);-
633 return fullpath;
executed 225 times by 3 tests: return fullpath;
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
225
634}-
635-
636/*!-
637 We compress small text and JavaScript files.-
638 */-
639bool QCacheItem::canCompress() const-
640{-
641 bool sizeOk = false;-
642 bool typeOk = false;-
643 const auto headers = metaData.rawHeaders();-
644 for (const auto &header : headers) {-
645 if (header.first.toLower() == "content-length") {
header.first.t...ontent-length"Description
TRUEevaluated 26 times by 2 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
FALSEevaluated 496 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
26-496
646 qint64 size = header.second.toLongLong();-
647 if (size > MAX_COMPRESSION_SIZE)
size > (1024 * 1024 * 3)Description
TRUEnever evaluated
FALSEevaluated 26 times by 2 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
0-26
648 return false;
never executed: return false;
0
649 else-
650 sizeOk = true;
executed 26 times by 2 tests: sizeOk = true;
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
26
651 }-
652-
653 if (header.first.toLower() == "content-type") {
header.first.t..."content-type"Description
TRUEevaluated 140 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
FALSEevaluated 382 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
140-382
654 QByteArray type = header.second;-
655 if (type.startsWith("text/")
type.startsWith("text/")Description
TRUEevaluated 140 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
FALSEnever evaluated
0-140
656 || (type.startsWith("application/")
type.startsWit...application/")Description
TRUEnever evaluated
FALSEnever evaluated
0
657 && (type.endsWith("javascript") || type.endsWith("ecmascript"))))
type.endsWith("javascript")Description
TRUEnever evaluated
FALSEnever evaluated
type.endsWith("ecmascript")Description
TRUEnever evaluated
FALSEnever evaluated
0
658 typeOk = true;
executed 140 times by 3 tests: typeOk = true;
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
140
659 else-
660 return false;
never executed: return false;
0
661 }-
662 if (sizeOk && typeOk)
sizeOkDescription
TRUEevaluated 50 times by 2 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
FALSEevaluated 472 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
typeOkDescription
TRUEevaluated 24 times by 1 test
Evaluated by:
  • tst_QAbstractNetworkCache
FALSEevaluated 26 times by 2 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
24-472
663 return true;
executed 24 times by 1 test: return true;
Executed by:
  • tst_QAbstractNetworkCache
24
664 }
executed 498 times by 3 tests: end of block
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
498
665 return false;
executed 150 times by 3 tests: return false;
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
150
666}-
667-
668enum-
669{-
670 CacheMagic = 0xe8,-
671 CurrentCacheVersion = CACHE_VERSION-
672};-
673-
674void QCacheItem::writeHeader(QFile *device) const-
675{-
676 QDataStream out(device);-
677-
678 out << qint32(CacheMagic);-
679 out << qint32(CurrentCacheVersion);-
680 out << static_cast<qint32>(out.version());-
681 out << metaData;-
682 bool compressed = canCompress();-
683 out << compressed;-
684}
executed 87 times by 3 tests: end of block
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
87
685-
686void QCacheItem::writeCompressedData(QFile *device) const-
687{-
688 QDataStream out(device);-
689-
690 out << qCompress(data.data());-
691}
executed 12 times by 1 test: end of block
Executed by:
  • tst_QAbstractNetworkCache
12
692-
693/*!-
694 Returns \c false if the file is a cache file,-
695 but is an older version and should be removed otherwise true.-
696 */-
697bool QCacheItem::read(QFile *device, bool readData)-
698{-
699 reset();-
700-
701 QDataStream in(device);-
702-
703 qint32 marker;-
704 qint32 v;-
705 in >> marker;-
706 in >> v;-
707 if (marker != CacheMagic)
marker != CacheMagicDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • tst_QNetworkDiskCache
FALSEevaluated 88 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
1-88
708 return true;
executed 1 time by 1 test: return true;
Executed by:
  • tst_QNetworkDiskCache
1
709-
710 // If the cache magic is correct, but the version is not we should remove it-
711 if (v != CurrentCacheVersion)
v != CurrentCacheVersionDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tst_QNetworkDiskCache
FALSEevaluated 86 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
2-86
712 return false;
executed 2 times by 1 test: return false;
Executed by:
  • tst_QNetworkDiskCache
2
713-
714 qint32 streamVersion;-
715 in >> streamVersion;-
716 // Default stream version is also the highest we can handle-
717 if (streamVersion > in.version())
streamVersion > in.version()Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • tst_QNetworkDiskCache
FALSEevaluated 85 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
1-85
718 return false;
executed 1 time by 1 test: return false;
Executed by:
  • tst_QNetworkDiskCache
1
719 in.setVersion(streamVersion);-
720-
721 bool compressed;-
722 QByteArray dataBA;-
723 in >> metaData;-
724 in >> compressed;-
725 if (readData && compressed) {
readDataDescription
TRUEevaluated 40 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
FALSEevaluated 45 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
compressedDescription
TRUEevaluated 11 times by 2 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
FALSEevaluated 29 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
11-45
726 in >> dataBA;-
727 data.setData(qUncompress(dataBA));-
728 data.open(QBuffer::ReadOnly);-
729 }
executed 11 times by 2 tests: end of block
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
11
730-
731 // quick and dirty check if metadata's URL field and the file's name are in synch-
732 QString expectedFilename = QNetworkDiskCachePrivate::uniqueFileName(metaData.url());-
733 if (!device->fileName().endsWith(expectedFilename))
!device->fileN...ectedFilename)Description
TRUEnever evaluated
FALSEevaluated 85 times by 3 tests
Evaluated by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
0-85
734 return false;
never executed: return false;
0
735-
736 return metaData.isValid();
executed 85 times by 3 tests: return metaData.isValid();
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QNetworkDiskCache
  • tst_QNetworkReply
85
737}-
738-
739QT_END_NAMESPACE-
740-
741#endif // QT_NO_NETWORKDISKCACHE-
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9