From 2dbb8c19ea19b9cef28e29899e50b788eded6f4d Mon Sep 17 00:00:00 2001 From: Arttu Tarkiainen Date: Wed, 19 Oct 2022 14:45:13 +0300 Subject: Add possibility to synchronize metadata cache manually The contents of the cache subdirectories and manifest.json may mismatch if the installer process is killed after the metadata fetch, because the manifest file is normally synchronized only when the cache object gets destroyed. While this is a recoverable error, it creates unnecessary overhead of discarding broken items and downloading them again. Add support for synchronizing the cache by the caller and do so each time the cache is updated by Metadatajob. Task-number: QTIFW-2817 Change-Id: Ia4cd3de44d57e8d732d11dc26968aa87323ada0f Reviewed-by: Katja Marttila --- src/libs/installer/genericdatacache.cpp | 22 ++++++++++++++++++++++ src/libs/installer/genericdatacache.h | 1 + src/libs/installer/metadatajob.cpp | 20 ++++++++++++++++---- 3 files changed, 39 insertions(+), 4 deletions(-) (limited to 'src/libs/installer') diff --git a/src/libs/installer/genericdatacache.cpp b/src/libs/installer/genericdatacache.cpp index 7f21a5365..1b33976c8 100644 --- a/src/libs/installer/genericdatacache.cpp +++ b/src/libs/installer/genericdatacache.cpp @@ -112,6 +112,11 @@ CacheableItem::~CacheableItem() methods declared in the \l{CacheableItem} interface. The GenericDataCache\ class can still be explicitly specialized to use the derived type as a template argument, to allow retrieving items as the derived type without casting. + + Each cache has a manifest file in its root directory, which lists the version + and wrapped type of the cache, and all its items. The file is updated automatically + when the cache object is destructed, or it can be updated periodically by + calling \l{sync()}. */ /*! @@ -272,6 +277,23 @@ bool GenericDataCache::clear() return success; } +/*! + \fn template QInstaller::GenericDataCache::sync() + + Synchronizes the contents of the cache to its manifest file. Returns \c true + if the manifest file was updates successfully, \c false otherwise. +*/ +template +bool GenericDataCache::sync() +{ + if (m_invalidated) { + setErrorString(QCoreApplication::translate("GenericDataCache", + "Cannot synchronize invalidated cache.")); + return false; + } + return toDisk(); +} + /*! \fn template QInstaller::GenericDataCache::isValid() const diff --git a/src/libs/installer/genericdatacache.h b/src/libs/installer/genericdatacache.h index 7b909cc7a..5042ed9a4 100644 --- a/src/libs/installer/genericdatacache.h +++ b/src/libs/installer/genericdatacache.h @@ -72,6 +72,7 @@ public: bool initialize(); bool clear(); + bool sync(); bool isValid() const; QString errorString() const; diff --git a/src/libs/installer/metadatajob.cpp b/src/libs/installer/metadatajob.cpp index a15209a31..659648e1a 100644 --- a/src/libs/installer/metadatajob.cpp +++ b/src/libs/installer/metadatajob.cpp @@ -346,12 +346,11 @@ bool MetadataJob::updateCache() // Register items from current run to cache QStringList registeredKeys; + bool success = true; for (auto *meta : qAsConst(m_fetchedMetadata)) { if (!m_metaFromCache.registerItem(meta, true)) { - emitFinishedWithError(QInstaller::CacheError, m_metaFromCache.errorString() - + u' ' - + tr("Clearing the cache directory and restarting the application may solve this.")); - return false; + success = false; + break; } meta->setPersistentRepositoryPath(meta->repository().url()); registeredKeys.append(m_fetchedMetadata.key(meta)); @@ -360,11 +359,24 @@ bool MetadataJob::updateCache() for (auto &key : qAsConst(registeredKeys)) m_fetchedMetadata.remove(key); + // Bail out if there was error while registering items + if (!success) { + emitFinishedWithError(QInstaller::CacheError, m_metaFromCache.errorString() + u' ' + + tr("Clearing the cache directory and restarting the application may solve this.")); + m_metaFromCache.sync(); + return false; + } + // ...and clean up obsolete cached items const QList obsolete = m_metaFromCache.obsoleteItems(); for (auto *meta : obsolete) m_metaFromCache.removeItem(meta->checksum()); + if (!m_metaFromCache.sync()) { + emitFinishedWithError(QInstaller::CacheError, m_metaFromCache.errorString() + u' ' + + tr("Clearing the cache directory and restarting the application may solve this.")); + return false; + } return true; } -- cgit v1.2.3