From bb7e8985f6d189de0acac6a1c3033cb16378c1fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Mon, 4 Nov 2013 02:53:05 +0100 Subject: Reformat and (slightly) decruft all the things. --- logic/net/HttpMetaCache.cpp | 129 +++++++++++++++++++++++++------------------- 1 file changed, 74 insertions(+), 55 deletions(-) (limited to 'logic/net/HttpMetaCache.cpp') diff --git a/logic/net/HttpMetaCache.cpp b/logic/net/HttpMetaCache.cpp index 5ba5b98d..29007951 100644 --- a/logic/net/HttpMetaCache.cpp +++ b/logic/net/HttpMetaCache.cpp @@ -1,3 +1,18 @@ +/* Copyright 2013 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. + */ + #include "MultiMC.h" #include "HttpMetaCache.h" #include @@ -9,7 +24,7 @@ #include #include -#include +#include "logger/QsLog.h" #include #include @@ -20,14 +35,12 @@ QString MetaEntry::getFullPath() return PathCombine(MMC->metacache()->getBasePath(base), path); } - -HttpMetaCache::HttpMetaCache(QString path) - :QObject() +HttpMetaCache::HttpMetaCache(QString path) : QObject() { m_index_file = path; saveBatchingTimer.setSingleShot(true); saveBatchingTimer.setTimerType(Qt::VeryCoarseTimer); - connect(&saveBatchingTimer,SIGNAL(timeout()),SLOT(SaveNow())); + connect(&saveBatchingTimer, SIGNAL(timeout()), SLOT(SaveNow())); } HttpMetaCache::~HttpMetaCache() @@ -36,58 +49,61 @@ HttpMetaCache::~HttpMetaCache() SaveNow(); } -MetaEntryPtr HttpMetaCache::getEntry ( QString base, QString resource_path ) +MetaEntryPtr HttpMetaCache::getEntry(QString base, QString resource_path) { // no base. no base path. can't store - if(!m_entries.contains(base)) + if (!m_entries.contains(base)) { // TODO: log problem return MetaEntryPtr(); } - EntryMap & map = m_entries[base]; - if(map.entry_list.contains(resource_path)) + EntryMap &map = m_entries[base]; + if (map.entry_list.contains(resource_path)) { return map.entry_list[resource_path]; } return MetaEntryPtr(); } -MetaEntryPtr HttpMetaCache::resolveEntry ( QString base, QString resource_path, QString expected_etag ) +MetaEntryPtr HttpMetaCache::resolveEntry(QString base, QString resource_path, + QString expected_etag) { auto entry = getEntry(base, resource_path); // it's not present? generate a default stale entry - if(!entry) + if (!entry) { return staleEntry(base, resource_path); } - - auto & selected_base = m_entries[base]; + + auto &selected_base = m_entries[base]; QString real_path = PathCombine(selected_base.base_path, resource_path); QFileInfo finfo(real_path); - + // is the file really there? if not -> stale - if(!finfo.isFile() || !finfo.isReadable()) + if (!finfo.isFile() || !finfo.isReadable()) { // 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) + + if (!expected_etag.isEmpty() && expected_etag != entry->etag) { // if the etag doesn't match expected, we disown the entry selected_base.entry_list.remove(resource_path); return staleEntry(base, resource_path); } - + // if the file changed, check md5sum qint64 file_last_changed = finfo.lastModified().toUTC().toMSecsSinceEpoch(); - if(file_last_changed != entry->local_changed_timestamp) + if (file_last_changed != entry->local_changed_timestamp) { QFile input(real_path); input.open(QIODevice::ReadOnly); - QString md5sum = QCryptographicHash::hash(input.readAll(), QCryptographicHash::Md5).toHex().constData(); - if(entry->md5sum != md5sum) + QString md5sum = QCryptographicHash::hash(input.readAll(), QCryptographicHash::Md5) + .toHex() + .constData(); + if (entry->md5sum != md5sum) { selected_base.entry_list.remove(resource_path); return staleEntry(base, resource_path); @@ -101,14 +117,15 @@ MetaEntryPtr HttpMetaCache::resolveEntry ( QString base, QString resource_path, return entry; } -bool HttpMetaCache::updateEntry ( MetaEntryPtr stale_entry ) +bool HttpMetaCache::updateEntry(MetaEntryPtr stale_entry) { - if(!m_entries.contains(stale_entry->base)) + if (!m_entries.contains(stale_entry->base)) { - QLOG_ERROR() << "Cannot add entry with unknown base: " << stale_entry->base.toLocal8Bit(); + QLOG_ERROR() << "Cannot add entry with unknown base: " + << stale_entry->base.toLocal8Bit(); return false; } - if(stale_entry->stale) + if (stale_entry->stale) { QLOG_ERROR() << "Cannot add stale entry: " << stale_entry->getFullPath().toLocal8Bit(); return false; @@ -127,10 +144,10 @@ MetaEntryPtr HttpMetaCache::staleEntry(QString base, QString resource_path) return MetaEntryPtr(foo); } -void HttpMetaCache::addBase ( QString base, QString base_root ) +void HttpMetaCache::addBase(QString base, QString base_root) { // TODO: report error - if(m_entries.contains(base)) + if (m_entries.contains(base)) return; // TODO: check if the base path is valid EntryMap foo; @@ -138,57 +155,57 @@ void HttpMetaCache::addBase ( QString base, QString base_root ) m_entries[base] = foo; } -QString HttpMetaCache::getBasePath ( QString base ) +QString HttpMetaCache::getBasePath(QString base) { - if(m_entries.contains(base)) + if (m_entries.contains(base)) { return m_entries[base].base_path; } return QString(); } - void HttpMetaCache::Load() { QFile index(m_index_file); - if(!index.open(QIODevice::ReadOnly)) + if (!index.open(QIODevice::ReadOnly)) return; - + QJsonDocument json = QJsonDocument::fromJson(index.readAll()); - if(!json.isObject()) + if (!json.isObject()) return; auto root = json.object(); // check file version first - auto version_val =root.value("version"); - if(!version_val.isString()) + auto version_val = root.value("version"); + if (!version_val.isString()) return; - if(version_val.toString() != "1") + if (version_val.toString() != "1") return; - + // read the entry array - auto entries_val =root.value("entries"); - if(!entries_val.isArray()) + auto entries_val = root.value("entries"); + if (!entries_val.isArray()) return; QJsonArray array = entries_val.toArray(); - for(auto element: array) + for (auto element : array) { - if(!element.isObject()) + if (!element.isObject()) return; auto element_obj = element.toObject(); QString base = element_obj.value("base").toString(); - if(!m_entries.contains(base)) + if (!m_entries.contains(base)) continue; - auto & entrymap = m_entries[base]; + auto &entrymap = m_entries[base]; auto foo = new MetaEntry; foo->base = base; QString path = foo->path = element_obj.value("path").toString(); foo->md5sum = element_obj.value("md5sum").toString(); foo->etag = element_obj.value("etag").toString(); foo->local_changed_timestamp = element_obj.value("last_changed_timestamp").toDouble(); - foo->remote_changed_timestamp = element_obj.value("remote_changed_timestamp").toString(); + foo->remote_changed_timestamp = + element_obj.value("remote_changed_timestamp").toString(); // presumed innocent until closer examination foo->stale = false; - entrymap.entry_list[path] = MetaEntryPtr( foo ); + entrymap.entry_list[path] = MetaEntryPtr(foo); } } @@ -202,33 +219,35 @@ void HttpMetaCache::SaveEventually() void HttpMetaCache::SaveNow() { QSaveFile tfile(m_index_file); - if(!tfile.open(QIODevice::WriteOnly | QIODevice::Truncate)) + if (!tfile.open(QIODevice::WriteOnly | QIODevice::Truncate)) return; QJsonObject toplevel; - toplevel.insert("version",QJsonValue(QString("1"))); + toplevel.insert("version", QJsonValue(QString("1"))); QJsonArray entriesArr; - for(auto group : m_entries) + for (auto group : m_entries) { - for(auto entry : group.entry_list) + for (auto entry : group.entry_list) { QJsonObject entryObj; entryObj.insert("base", QJsonValue(entry->base)); entryObj.insert("path", QJsonValue(entry->path)); entryObj.insert("md5sum", QJsonValue(entry->md5sum)); entryObj.insert("etag", QJsonValue(entry->etag)); - 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)); + 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)); entriesArr.append(entryObj); } } - toplevel.insert("entries",entriesArr); + toplevel.insert("entries", entriesArr); QJsonDocument doc(toplevel); QByteArray jsonData = doc.toJson(); qint64 result = tfile.write(jsonData); - if(result == -1) + if (result == -1) return; - if(result != jsonData.size()) + if (result != jsonData.size()) return; tfile.commit(); } -- cgit v1.2.3