2015-02-03 03:55:30 +05:30
|
|
|
/* Copyright 2013-2015 MultiMC Contributors
|
2013-11-04 07:23:05 +05:30
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2015-02-09 06:21:14 +05:30
|
|
|
#include "Env.h"
|
2013-09-07 01:28:51 +05:30
|
|
|
#include "HttpMetaCache.h"
|
2015-06-04 00:40:28 +05:30
|
|
|
#include "FileSystem.h"
|
2013-09-08 05:45:20 +05:30
|
|
|
|
|
|
|
#include <QFileInfo>
|
2013-09-07 01:28:51 +05:30
|
|
|
#include <QFile>
|
2013-09-08 05:45:20 +05:30
|
|
|
#include <QTemporaryFile>
|
|
|
|
#include <QDateTime>
|
|
|
|
#include <QCryptographicHash>
|
|
|
|
|
2015-02-02 06:44:14 +05:30
|
|
|
#include <QDebug>
|
2013-09-08 05:45:20 +05:30
|
|
|
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonObject>
|
|
|
|
|
|
|
|
QString MetaEntry::getFullPath()
|
|
|
|
{
|
2015-01-31 21:29:03 +05:30
|
|
|
// FIXME: make local?
|
2016-03-26 21:26:57 +05:30
|
|
|
return FS::PathCombine(basePath, relativePath);
|
2013-09-08 05:45:20 +05:30
|
|
|
}
|
|
|
|
|
2013-11-04 07:23:05 +05:30
|
|
|
HttpMetaCache::HttpMetaCache(QString path) : QObject()
|
2013-09-07 01:28:51 +05:30
|
|
|
{
|
|
|
|
m_index_file = path;
|
2013-09-08 05:45:20 +05:30
|
|
|
saveBatchingTimer.setSingleShot(true);
|
|
|
|
saveBatchingTimer.setTimerType(Qt::VeryCoarseTimer);
|
2013-11-04 07:23:05 +05:30
|
|
|
connect(&saveBatchingTimer, SIGNAL(timeout()), SLOT(SaveNow()));
|
2013-09-07 01:28:51 +05:30
|
|
|
}
|
2013-09-08 05:45:20 +05:30
|
|
|
|
2013-09-07 01:28:51 +05:30
|
|
|
HttpMetaCache::~HttpMetaCache()
|
|
|
|
{
|
2013-09-08 05:45:20 +05:30
|
|
|
saveBatchingTimer.stop();
|
|
|
|
SaveNow();
|
2013-09-07 01:28:51 +05:30
|
|
|
}
|
|
|
|
|
2013-11-04 07:23:05 +05:30
|
|
|
MetaEntryPtr HttpMetaCache::getEntry(QString base, QString resource_path)
|
2013-09-07 01:28:51 +05:30
|
|
|
{
|
|
|
|
// no base. no base path. can't store
|
2013-11-04 07:23:05 +05:30
|
|
|
if (!m_entries.contains(base))
|
2013-09-08 05:45:20 +05:30
|
|
|
{
|
|
|
|
// TODO: log problem
|
|
|
|
return MetaEntryPtr();
|
|
|
|
}
|
2013-11-04 07:23:05 +05:30
|
|
|
EntryMap &map = m_entries[base];
|
|
|
|
if (map.entry_list.contains(resource_path))
|
2013-09-08 05:45:20 +05:30
|
|
|
{
|
|
|
|
return map.entry_list[resource_path];
|
|
|
|
}
|
|
|
|
return MetaEntryPtr();
|
|
|
|
}
|
|
|
|
|
2016-03-26 21:26:57 +05:30
|
|
|
MetaEntryPtr HttpMetaCache::resolveEntry(QString base, QString resource_path, QString expected_etag)
|
2013-09-08 05:45:20 +05:30
|
|
|
{
|
|
|
|
auto entry = getEntry(base, resource_path);
|
|
|
|
// it's not present? generate a default stale entry
|
2013-11-04 07:23:05 +05:30
|
|
|
if (!entry)
|
2013-09-08 05:45:20 +05:30
|
|
|
{
|
|
|
|
return staleEntry(base, resource_path);
|
|
|
|
}
|
2013-11-04 07:23:05 +05:30
|
|
|
|
|
|
|
auto &selected_base = m_entries[base];
|
2015-10-05 05:17:27 +05:30
|
|
|
QString real_path = FS::PathCombine(selected_base.base_path, resource_path);
|
2013-09-07 01:28:51 +05:30
|
|
|
QFileInfo finfo(real_path);
|
2013-11-04 07:23:05 +05:30
|
|
|
|
2013-09-08 05:45:20 +05:30
|
|
|
// is the file really there? if not -> stale
|
2013-11-04 07:23:05 +05:30
|
|
|
if (!finfo.isFile() || !finfo.isReadable())
|
2013-09-07 01:28:51 +05:30
|
|
|
{
|
2013-09-08 05:45:20 +05:30
|
|
|
// if the file doesn't exist, we disown the entry
|
|
|
|
selected_base.entry_list.remove(resource_path);
|
|
|
|
return staleEntry(base, resource_path);
|
|
|
|
}
|
2013-11-04 07:23:05 +05:30
|
|
|
|
|
|
|
if (!expected_etag.isEmpty() && expected_etag != entry->etag)
|
2013-09-08 05:45:20 +05:30
|
|
|
{
|
|
|
|
// if the etag doesn't match expected, we disown the entry
|
|
|
|
selected_base.entry_list.remove(resource_path);
|
|
|
|
return staleEntry(base, resource_path);
|
2013-09-07 01:28:51 +05:30
|
|
|
}
|
2013-11-04 07:23:05 +05:30
|
|
|
|
2013-09-08 05:45:20 +05:30
|
|
|
// if the file changed, check md5sum
|
|
|
|
qint64 file_last_changed = finfo.lastModified().toUTC().toMSecsSinceEpoch();
|
2013-11-04 07:23:05 +05:30
|
|
|
if (file_last_changed != entry->local_changed_timestamp)
|
2013-09-08 05:45:20 +05:30
|
|
|
{
|
|
|
|
QFile input(real_path);
|
|
|
|
input.open(QIODevice::ReadOnly);
|
2013-11-04 07:23:05 +05:30
|
|
|
QString md5sum = QCryptographicHash::hash(input.readAll(), QCryptographicHash::Md5)
|
|
|
|
.toHex()
|
|
|
|
.constData();
|
|
|
|
if (entry->md5sum != md5sum)
|
2013-09-08 05:45:20 +05:30
|
|
|
{
|
|
|
|
selected_base.entry_list.remove(resource_path);
|
|
|
|
return staleEntry(base, resource_path);
|
|
|
|
}
|
|
|
|
// md5sums matched... keep entry and save the new state to file
|
2013-10-18 04:30:46 +05:30
|
|
|
entry->local_changed_timestamp = file_last_changed;
|
2013-09-08 05:45:20 +05:30
|
|
|
SaveEventually();
|
|
|
|
}
|
|
|
|
|
|
|
|
// entry passed all the checks we cared about.
|
2016-03-26 21:26:57 +05:30
|
|
|
entry->basePath = getBasePath(base);
|
2013-09-08 05:45:20 +05:30
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
2013-11-04 07:23:05 +05:30
|
|
|
bool HttpMetaCache::updateEntry(MetaEntryPtr stale_entry)
|
2013-09-08 05:45:20 +05:30
|
|
|
{
|
2016-03-26 21:26:57 +05:30
|
|
|
if (!m_entries.contains(stale_entry->baseId))
|
2013-09-08 05:45:20 +05:30
|
|
|
{
|
2015-02-02 06:44:14 +05:30
|
|
|
qCritical() << "Cannot add entry with unknown base: "
|
2016-03-26 21:26:57 +05:30
|
|
|
<< stale_entry->baseId.toLocal8Bit();
|
2013-09-08 05:45:20 +05:30
|
|
|
return false;
|
|
|
|
}
|
2013-11-04 07:23:05 +05:30
|
|
|
if (stale_entry->stale)
|
2013-09-08 05:45:20 +05:30
|
|
|
{
|
2015-02-02 06:44:14 +05:30
|
|
|
qCritical() << "Cannot add stale entry: " << stale_entry->getFullPath().toLocal8Bit();
|
2013-09-08 05:45:20 +05:30
|
|
|
return false;
|
|
|
|
}
|
2016-03-26 21:26:57 +05:30
|
|
|
m_entries[stale_entry->baseId].entry_list[stale_entry->relativePath] = stale_entry;
|
2013-09-08 05:45:20 +05:30
|
|
|
SaveEventually();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-01 03:53:17 +05:30
|
|
|
bool HttpMetaCache::evictEntry(MetaEntryPtr entry)
|
|
|
|
{
|
|
|
|
if(entry)
|
|
|
|
{
|
|
|
|
entry->stale = true;
|
|
|
|
SaveEventually();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-08 05:45:20 +05:30
|
|
|
MetaEntryPtr HttpMetaCache::staleEntry(QString base, QString resource_path)
|
|
|
|
{
|
2016-03-26 21:26:57 +05:30
|
|
|
auto foo = new MetaEntry();
|
|
|
|
foo->baseId = base;
|
|
|
|
foo->basePath = getBasePath(base);
|
|
|
|
foo->relativePath = resource_path;
|
2013-09-08 05:45:20 +05:30
|
|
|
foo->stale = true;
|
|
|
|
return MetaEntryPtr(foo);
|
2013-09-07 01:28:51 +05:30
|
|
|
}
|
|
|
|
|
2013-11-04 07:23:05 +05:30
|
|
|
void HttpMetaCache::addBase(QString base, QString base_root)
|
2013-09-07 01:28:51 +05:30
|
|
|
{
|
|
|
|
// TODO: report error
|
2013-11-04 07:23:05 +05:30
|
|
|
if (m_entries.contains(base))
|
2013-09-07 01:28:51 +05:30
|
|
|
return;
|
|
|
|
// TODO: check if the base path is valid
|
|
|
|
EntryMap foo;
|
|
|
|
foo.base_path = base_root;
|
|
|
|
m_entries[base] = foo;
|
|
|
|
}
|
|
|
|
|
2013-11-04 07:23:05 +05:30
|
|
|
QString HttpMetaCache::getBasePath(QString base)
|
2013-09-08 05:45:20 +05:30
|
|
|
{
|
2013-11-04 07:23:05 +05:30
|
|
|
if (m_entries.contains(base))
|
2013-09-08 05:45:20 +05:30
|
|
|
{
|
|
|
|
return m_entries[base].base_path;
|
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2013-09-07 01:28:51 +05:30
|
|
|
void HttpMetaCache::Load()
|
|
|
|
{
|
2016-03-26 21:26:57 +05:30
|
|
|
if(m_index_file.isNull())
|
|
|
|
return;
|
|
|
|
|
2013-09-07 01:28:51 +05:30
|
|
|
QFile index(m_index_file);
|
2013-11-04 07:23:05 +05:30
|
|
|
if (!index.open(QIODevice::ReadOnly))
|
2013-09-07 01:28:51 +05:30
|
|
|
return;
|
2013-11-04 07:23:05 +05:30
|
|
|
|
2013-09-07 01:28:51 +05:30
|
|
|
QJsonDocument json = QJsonDocument::fromJson(index.readAll());
|
2013-11-04 07:23:05 +05:30
|
|
|
if (!json.isObject())
|
2013-09-07 01:28:51 +05:30
|
|
|
return;
|
|
|
|
auto root = json.object();
|
|
|
|
// check file version first
|
2013-11-04 07:23:05 +05:30
|
|
|
auto version_val = root.value("version");
|
|
|
|
if (!version_val.isString())
|
2013-09-07 01:28:51 +05:30
|
|
|
return;
|
2013-11-04 07:23:05 +05:30
|
|
|
if (version_val.toString() != "1")
|
2013-09-07 01:28:51 +05:30
|
|
|
return;
|
2013-11-04 07:23:05 +05:30
|
|
|
|
2013-09-07 01:28:51 +05:30
|
|
|
// read the entry array
|
2013-11-04 07:23:05 +05:30
|
|
|
auto entries_val = root.value("entries");
|
|
|
|
if (!entries_val.isArray())
|
2013-09-07 01:28:51 +05:30
|
|
|
return;
|
2013-09-08 05:45:20 +05:30
|
|
|
QJsonArray array = entries_val.toArray();
|
2013-11-04 07:23:05 +05:30
|
|
|
for (auto element : array)
|
2013-09-07 01:28:51 +05:30
|
|
|
{
|
2013-11-04 07:23:05 +05:30
|
|
|
if (!element.isObject())
|
2013-09-07 01:28:51 +05:30
|
|
|
return;
|
|
|
|
auto element_obj = element.toObject();
|
|
|
|
QString base = element_obj.value("base").toString();
|
2013-11-04 07:23:05 +05:30
|
|
|
if (!m_entries.contains(base))
|
2013-09-07 01:28:51 +05:30
|
|
|
continue;
|
2013-11-04 07:23:05 +05:30
|
|
|
auto &entrymap = m_entries[base];
|
2016-03-26 21:26:57 +05:30
|
|
|
auto foo = new MetaEntry();
|
|
|
|
foo->baseId = base;
|
|
|
|
QString path = foo->relativePath = element_obj.value("path").toString();
|
2013-09-07 01:28:51 +05:30
|
|
|
foo->md5sum = element_obj.value("md5sum").toString();
|
|
|
|
foo->etag = element_obj.value("etag").toString();
|
2013-10-18 04:30:46 +05:30
|
|
|
foo->local_changed_timestamp = element_obj.value("last_changed_timestamp").toDouble();
|
2013-11-04 07:23:05 +05:30
|
|
|
foo->remote_changed_timestamp =
|
|
|
|
element_obj.value("remote_changed_timestamp").toString();
|
2013-09-08 05:45:20 +05:30
|
|
|
// presumed innocent until closer examination
|
|
|
|
foo->stale = false;
|
2013-11-04 07:23:05 +05:30
|
|
|
entrymap.entry_list[path] = MetaEntryPtr(foo);
|
2013-09-07 01:28:51 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-08 19:29:50 +05:30
|
|
|
void HttpMetaCache::SaveEventually()
|
|
|
|
{
|
|
|
|
// reset the save timer
|
|
|
|
saveBatchingTimer.stop();
|
|
|
|
saveBatchingTimer.start(30000);
|
|
|
|
}
|
|
|
|
|
2013-09-08 05:45:20 +05:30
|
|
|
void HttpMetaCache::SaveNow()
|
2013-09-07 01:28:51 +05:30
|
|
|
{
|
2016-03-26 21:26:57 +05:30
|
|
|
if(m_index_file.isNull())
|
|
|
|
return;
|
2013-09-07 01:28:51 +05:30
|
|
|
QJsonObject toplevel;
|
2013-11-04 07:23:05 +05:30
|
|
|
toplevel.insert("version", QJsonValue(QString("1")));
|
2013-09-07 01:28:51 +05:30
|
|
|
QJsonArray entriesArr;
|
2013-11-04 07:23:05 +05:30
|
|
|
for (auto group : m_entries)
|
2013-09-07 01:28:51 +05:30
|
|
|
{
|
2013-11-04 07:23:05 +05:30
|
|
|
for (auto entry : group.entry_list)
|
2013-09-07 01:28:51 +05:30
|
|
|
{
|
2015-04-01 03:53:17 +05:30
|
|
|
// do not save stale entries. they are dead.
|
|
|
|
if(entry->stale)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2013-09-07 01:28:51 +05:30
|
|
|
QJsonObject entryObj;
|
2016-03-26 21:26:57 +05:30
|
|
|
entryObj.insert("base", QJsonValue(entry->baseId));
|
|
|
|
entryObj.insert("path", QJsonValue(entry->relativePath));
|
2013-09-07 01:28:51 +05:30
|
|
|
entryObj.insert("md5sum", QJsonValue(entry->md5sum));
|
|
|
|
entryObj.insert("etag", QJsonValue(entry->etag));
|
2013-11-04 07:23:05 +05:30
|
|
|
entryObj.insert("last_changed_timestamp",
|
|
|
|
QJsonValue(double(entry->local_changed_timestamp)));
|
|
|
|
if (!entry->remote_changed_timestamp.isEmpty())
|
|
|
|
entryObj.insert("remote_changed_timestamp",
|
|
|
|
QJsonValue(entry->remote_changed_timestamp));
|
2013-09-07 01:28:51 +05:30
|
|
|
entriesArr.append(entryObj);
|
|
|
|
}
|
|
|
|
}
|
2013-11-04 07:23:05 +05:30
|
|
|
toplevel.insert("entries", entriesArr);
|
2015-06-04 00:40:28 +05:30
|
|
|
|
2013-09-07 01:28:51 +05:30
|
|
|
QJsonDocument doc(toplevel);
|
2015-06-04 00:40:28 +05:30
|
|
|
try
|
|
|
|
{
|
|
|
|
FS::write(m_index_file, doc.toJson());
|
|
|
|
}
|
|
|
|
catch (Exception & e)
|
|
|
|
{
|
|
|
|
qWarning() << e.what();
|
|
|
|
}
|
2013-09-07 01:28:51 +05:30
|
|
|
}
|