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