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