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