1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qv4compileddata_p.h"
#include <QtQml/qqmlfile.h>
#include <QtCore/qcryptographichash.h>
#include <QtCore/qdir.h>
#include <QtCore/qscopeguard.h>
#include <QtCore/qstandardpaths.h>
QT_BEGIN_NAMESPACE
namespace QV4 {
namespace CompiledData {
QString CompilationUnit::localCacheFilePath(const QUrl &url)
{
static const QByteArray envCachePath = qgetenv("QML_DISK_CACHE_PATH");
const QString localSourcePath = QQmlFile::urlToLocalFileOrQrc(url);
const QString cacheFileSuffix
= QFileInfo(localSourcePath + QLatin1Char('c')).completeSuffix();
QCryptographicHash fileNameHash(QCryptographicHash::Sha1);
fileNameHash.addData(localSourcePath.toUtf8());
QString directory = envCachePath.isEmpty()
? QStandardPaths::writableLocation(QStandardPaths::CacheLocation)
+ QLatin1String("/qmlcache/")
: QString::fromLocal8Bit(envCachePath) + QLatin1String("/");
QDir::root().mkpath(directory);
return directory + QString::fromUtf8(fileNameHash.result().toHex())
+ QLatin1Char('.') + cacheFileSuffix;
}
bool CompilationUnit::loadFromDisk(
const QUrl &url, const QDateTime &sourceTimeStamp, QString *errorString)
{
if (!QQmlFile::isLocalFile(url)) {
*errorString = QStringLiteral("File has to be a local file.");
return false;
}
const QString sourcePath = QQmlFile::urlToLocalFileOrQrc(url);
auto cacheFile = std::make_unique<CompilationUnitMapper>();
const QStringList cachePaths = { sourcePath + QLatin1Char('c'), localCacheFilePath(url) };
for (const QString &cachePath : cachePaths) {
Unit *mappedUnit = cacheFile->get(cachePath, sourceTimeStamp, errorString);
if (!mappedUnit)
continue;
const Unit *oldData = unitData();
const Unit * const oldDataPtr
= (oldData && !(oldData->flags & Unit::StaticData))
? oldData
: nullptr;
auto dataPtrRevert = qScopeGuard([this, oldData](){
setUnitData(oldData);
});
setUnitData(mappedUnit);
if (mappedUnit->sourceFileIndex != 0) {
if (mappedUnit->sourceFileIndex >=
mappedUnit->stringTableSize + dynamicStrings.size()) {
*errorString = QStringLiteral("QML source file index is invalid.");
continue;
}
if (sourcePath !=
QQmlFile::urlToLocalFileOrQrc(stringAt(mappedUnit->sourceFileIndex))) {
*errorString = QStringLiteral("QML source file has moved to a different location.");
continue;
}
}
dataPtrRevert.dismiss();
free(const_cast<Unit*>(oldDataPtr));
backingFile = std::move(cacheFile);
return true;
}
return false;
}
bool CompilationUnit::saveToDisk(const QUrl &unitUrl, QString *errorString)
{
if (unitData()->sourceTimeStamp == 0) {
*errorString = QStringLiteral("Missing time stamp for source file");
return false;
}
if (!QQmlFile::isLocalFile(unitUrl)) {
*errorString = QStringLiteral("File has to be a local file.");
return false;
}
return SaveableUnitPointer(unitData()).saveToDisk<char>(
[&unitUrl, errorString](const char *data, quint32 size) {
const QString cachePath = localCacheFilePath(unitUrl);
if (SaveableUnitPointer::writeDataToFile(
cachePath, data, size, errorString)) {
CompilationUnitMapper::invalidate(cachePath);
return true;
}
return false;
});
}
} // namespace CompiledData
} // namespace QV4
QT_END_NAMESPACE
|