PrismLauncher/launcher/net/HttpMetaCache.cpp

287 lines
8.9 KiB
C++
Raw Normal View History

// SPDX-License-Identifier: GPL-3.0-only
/*
* PolyMC - Minecraft Launcher
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This file incorporates work covered by the following copyright and
* permission notice:
*
* Copyright 2013-2021 MultiMC Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2013-09-06 20:58:51 +01:00
#include "HttpMetaCache.h"
2015-06-03 20:10:28 +01:00
#include "FileSystem.h"
#include "Json.h"
#include <QCryptographicHash>
#include <QDateTime>
#include <QFile>
#include <QFileInfo>
#include <QDebug>
auto MetaEntry::getFullPath() -> QString
{
2018-07-15 13:51:05 +01:00
// FIXME: make local?
return FS::PathCombine(basePath, relativePath);
}
HttpMetaCache::HttpMetaCache(QString path) : QObject(), m_index_file(path)
2013-09-06 20:58:51 +01:00
{
2018-07-15 13:51:05 +01:00
saveBatchingTimer.setSingleShot(true);
saveBatchingTimer.setTimerType(Qt::VeryCoarseTimer);
2018-07-15 13:51:05 +01:00
connect(&saveBatchingTimer, SIGNAL(timeout()), SLOT(SaveNow()));
2013-09-06 20:58:51 +01:00
}
2013-09-06 20:58:51 +01:00
HttpMetaCache::~HttpMetaCache()
{
2018-07-15 13:51:05 +01:00
saveBatchingTimer.stop();
SaveNow();
2013-09-06 20:58:51 +01:00
}
auto HttpMetaCache::getEntry(QString base, QString resource_path) -> MetaEntryPtr
2013-09-06 20:58:51 +01:00
{
2018-07-15 13:51:05 +01:00
// no base. no base path. can't store
if (!m_entries.contains(base)) {
2018-07-15 13:51:05 +01:00
// TODO: log problem
return {};
2018-07-15 13:51:05 +01:00
}
EntryMap& map = m_entries[base];
if (map.entry_list.contains(resource_path)) {
2018-07-15 13:51:05 +01:00
return map.entry_list[resource_path];
}
return {};
}
auto HttpMetaCache::resolveEntry(QString base, QString resource_path, QString expected_etag) -> MetaEntryPtr
{
2018-07-15 13:51:05 +01:00
auto entry = getEntry(base, resource_path);
// it's not present? generate a default stale entry
if (!entry) {
2018-07-15 13:51:05 +01:00
return staleEntry(base, resource_path);
}
auto& selected_base = m_entries[base];
2018-07-15 13:51:05 +01:00
QString real_path = FS::PathCombine(selected_base.base_path, resource_path);
QFileInfo finfo(real_path);
2018-07-15 13:51:05 +01:00
// is the file really there? if not -> stale
if (!finfo.isFile() || !finfo.isReadable()) {
2018-07-15 13:51:05 +01:00
// if the file doesn't exist, we disown the entry
selected_base.entry_list.remove(resource_path);
return staleEntry(base, resource_path);
}
if (!expected_etag.isEmpty() && expected_etag != entry->etag) {
2018-07-15 13:51:05 +01:00
// if the etag doesn't match expected, we disown the entry
selected_base.entry_list.remove(resource_path);
return staleEntry(base, resource_path);
}
2018-07-15 13:51:05 +01:00
// if the file changed, check md5sum
qint64 file_last_changed = finfo.lastModified().toUTC().toMSecsSinceEpoch();
if (file_last_changed != entry->local_changed_timestamp) {
2018-07-15 13:51:05 +01:00
QFile input(real_path);
input.open(QIODevice::ReadOnly);
QString md5sum = QCryptographicHash::hash(input.readAll(), QCryptographicHash::Md5).toHex().constData();
if (entry->md5sum != md5sum) {
2018-07-15 13:51:05 +01:00
selected_base.entry_list.remove(resource_path);
return staleEntry(base, resource_path);
}
2018-07-15 13:51:05 +01:00
// md5sums matched... keep entry and save the new state to file
entry->local_changed_timestamp = file_last_changed;
SaveEventually();
}
// Get rid of old entries, to prevent cache problems
auto current_time = QDateTime::currentSecsSinceEpoch();
if (entry->isExpired(current_time - ( file_last_changed / 1000 ))) {
qWarning() << "Removing cache entry because of old age!";
selected_base.entry_list.remove(resource_path);
return staleEntry(base, resource_path);
}
2018-07-15 13:51:05 +01:00
// entry passed all the checks we cared about.
entry->basePath = getBasePath(base);
return entry;
}
auto HttpMetaCache::updateEntry(MetaEntryPtr stale_entry) -> bool
{
if (!m_entries.contains(stale_entry->baseId)) {
qCritical() << "Cannot add entry with unknown base: " << stale_entry->baseId.toLocal8Bit();
2018-07-15 13:51:05 +01:00
return false;
}
if (stale_entry->stale) {
2018-07-15 13:51:05 +01:00
qCritical() << "Cannot add stale entry: " << stale_entry->getFullPath().toLocal8Bit();
return false;
}
2018-07-15 13:51:05 +01:00
m_entries[stale_entry->baseId].entry_list[stale_entry->relativePath] = stale_entry;
SaveEventually();
2018-07-15 13:51:05 +01:00
return true;
}
auto HttpMetaCache::evictEntry(MetaEntryPtr entry) -> bool
{
if (!entry)
return false;
entry->stale = true;
SaveEventually();
return true;
}
auto HttpMetaCache::staleEntry(QString base, QString resource_path) -> MetaEntryPtr
{
2018-07-15 13:51:05 +01:00
auto foo = new MetaEntry();
foo->baseId = base;
foo->basePath = getBasePath(base);
foo->relativePath = resource_path;
foo->stale = true;
2018-07-15 13:51:05 +01:00
return MetaEntryPtr(foo);
2013-09-06 20:58:51 +01:00
}
void HttpMetaCache::addBase(QString base, QString base_root)
2013-09-06 20:58:51 +01:00
{
2018-07-15 13:51:05 +01:00
// TODO: report error
if (m_entries.contains(base))
return;
2018-07-15 13:51:05 +01:00
// TODO: check if the base path is valid
EntryMap foo;
foo.base_path = base_root;
m_entries[base] = foo;
2013-09-06 20:58:51 +01:00
}
auto HttpMetaCache::getBasePath(QString base) -> QString
{
if (m_entries.contains(base)) {
2018-07-15 13:51:05 +01:00
return m_entries[base].base_path;
}
return {};
}
2013-09-06 20:58:51 +01:00
void HttpMetaCache::Load()
{
if (m_index_file.isNull())
2018-07-15 13:51:05 +01:00
return;
2018-07-15 13:51:05 +01:00
QFile index(m_index_file);
if (!index.open(QIODevice::ReadOnly))
return;
2018-07-15 13:51:05 +01:00
QJsonDocument json = QJsonDocument::fromJson(index.readAll());
auto root = Json::requireObject(json, "HttpMetaCache root");
2018-07-15 13:51:05 +01:00
// check file version first
auto version_val = Json::ensureString(root, "version");
if (version_val != "1")
2018-07-15 13:51:05 +01:00
return;
2018-07-15 13:51:05 +01:00
// read the entry array
auto array = Json::ensureArray(root, "entries");
for (auto element : array) {
auto element_obj = Json::ensureObject(element);
auto base = Json::ensureString(element_obj, "base");
2018-07-15 13:51:05 +01:00
if (!m_entries.contains(base))
continue;
auto& entrymap = m_entries[base];
2018-07-15 13:51:05 +01:00
auto foo = new MetaEntry();
foo->baseId = base;
foo->relativePath = Json::ensureString(element_obj, "path");
foo->md5sum = Json::ensureString(element_obj, "md5sum");
foo->etag = Json::ensureString(element_obj, "etag");
foo->local_changed_timestamp = Json::ensureDouble(element_obj, "last_changed_timestamp");
foo->remote_changed_timestamp = Json::ensureString(element_obj, "remote_changed_timestamp");
foo->current_age = Json::ensureDouble(element_obj, "current_age");
foo->max_age = Json::ensureDouble(element_obj, "max_age");
2018-07-15 13:51:05 +01:00
// presumed innocent until closer examination
foo->stale = false;
entrymap.entry_list[foo->relativePath] = MetaEntryPtr(foo);
2018-07-15 13:51:05 +01:00
}
2013-09-06 20:58:51 +01:00
}
2013-09-08 14:59:50 +01:00
void HttpMetaCache::SaveEventually()
{
2018-07-15 13:51:05 +01:00
// reset the save timer
saveBatchingTimer.stop();
saveBatchingTimer.start(30000);
2013-09-08 14:59:50 +01:00
}
void HttpMetaCache::SaveNow()
2013-09-06 20:58:51 +01:00
{
if (m_index_file.isNull())
2018-07-15 13:51:05 +01:00
return;
qDebug() << "[HttpMetaCache]" << "Saving metacache with" << m_entries.size() << "entries";
2018-07-15 13:51:05 +01:00
QJsonObject toplevel;
Json::writeString(toplevel, "version", "1");
2018-07-15 13:51:05 +01:00
QJsonArray entriesArr;
for (auto group : m_entries) {
for (auto entry : group.entry_list) {
2018-07-15 13:51:05 +01:00
// do not save stale entries. they are dead.
if (entry->stale) {
2018-07-15 13:51:05 +01:00
continue;
}
2018-07-15 13:51:05 +01:00
QJsonObject entryObj;
Json::writeString(entryObj, "base", entry->baseId);
Json::writeString(entryObj, "path", entry->relativePath);
Json::writeString(entryObj, "md5sum", entry->md5sum);
Json::writeString(entryObj, "etag", entry->etag);
entryObj.insert("last_changed_timestamp", QJsonValue(double(entry->local_changed_timestamp)));
2018-07-15 13:51:05 +01:00
if (!entry->remote_changed_timestamp.isEmpty())
entryObj.insert("remote_changed_timestamp", QJsonValue(entry->remote_changed_timestamp));
entryObj.insert("current_age", QJsonValue(double(entry->current_age)));
entryObj.insert("max_age", QJsonValue(double(entry->max_age)));
2018-07-15 13:51:05 +01:00
entriesArr.append(entryObj);
}
}
toplevel.insert("entries", entriesArr);
2015-06-03 20:10:28 +01:00
try {
Json::write(toplevel, m_index_file);
} catch (const Exception& e) {
2018-07-15 13:51:05 +01:00
qWarning() << e.what();
}
2013-09-06 20:58:51 +01:00
}