From 0141a2e621b8153c060cb76c072227d73cc12ccd Mon Sep 17 00:00:00 2001 From: David Rosca Date: Wed, 3 Jan 2018 11:06:21 +0100 Subject: [PATCH] SessionManager: Fix fallback to default session if session file is invalid Creating available sessions metadata is now also faster as it doesn't need to parse the entire session file, only check for validity. See #2331 --- src/lib/session/restoremanager.cpp | 31 +++++++++++++++++++++++++++++- src/lib/session/restoremanager.h | 1 + src/lib/session/sessionmanager.cpp | 8 +++----- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/lib/session/restoremanager.cpp b/src/lib/session/restoremanager.cpp index 3027f3d23..0d2715da4 100644 --- a/src/lib/session/restoremanager.cpp +++ b/src/lib/session/restoremanager.cpp @@ -100,6 +100,32 @@ static void loadVersion3(QDataStream &stream, RestoreData &data) } } +// static +bool RestoreManager::validateFile(const QString &file) +{ + if (!QFile::exists(file)) { + return false; + } + + QFile recoveryFile(file); + if (!recoveryFile.open(QIODevice::ReadOnly)) { + return false; + } + + QDataStream stream(&recoveryFile); + + int version; + stream >> version; + + if (version == Qz::sessionVersion || version == 0x0003 || version == (0x0003 | 0x050000)) { + int windowCount; + stream >> windowCount; + return windowCount > 0; + } + + return false; +} + // static void RestoreManager::createFromFile(const QString &file, RestoreData &data) { @@ -108,7 +134,10 @@ void RestoreManager::createFromFile(const QString &file, RestoreData &data) } QFile recoveryFile(file); - recoveryFile.open(QIODevice::ReadOnly); + if (!recoveryFile.open(QIODevice::ReadOnly)) { + return; + } + QDataStream stream(&recoveryFile); int version; diff --git a/src/lib/session/restoremanager.h b/src/lib/session/restoremanager.h index 10f6cd426..e68ef4320 100644 --- a/src/lib/session/restoremanager.h +++ b/src/lib/session/restoremanager.h @@ -38,6 +38,7 @@ public: QObject *recoveryObject(WebPage *page); + static bool validateFile(const QString &file); static void createFromFile(const QString &file, RestoreData &data); private: diff --git a/src/lib/session/sessionmanager.cpp b/src/lib/session/sessionmanager.cpp index 483b91687..1b00a08a1 100644 --- a/src/lib/session/sessionmanager.cpp +++ b/src/lib/session/sessionmanager.cpp @@ -279,10 +279,8 @@ void SessionManager::fillSessionsMetaDataListIfNeeded() for (int i = 0; i < sessionFiles.size(); ++i) { const QFileInfo &fileInfo = sessionFiles.at(i); - RestoreData data; - RestoreManager::createFromFile(fileInfo.absoluteFilePath(), data); - if (data.isEmpty()) + if (!RestoreManager::validateFile(fileInfo.absoluteFilePath())) continue; SessionMetaData metaData; @@ -321,8 +319,8 @@ void SessionManager::loadSettings() m_lastActiveSessionPath = sessionsDir.absoluteFilePath(m_lastActiveSessionPath); } - // fallback to default session - if (!QFile::exists(m_lastActiveSessionPath)) + // Fallback to default session + if (!RestoreManager::validateFile(m_lastActiveSessionPath)) m_lastActiveSessionPath = defaultSessionPath(); }