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