From 1e77613a1b9e5ab9c51be49f6503e0eb6075b50e Mon Sep 17 00:00:00 2001 From: mlangkabel Date: Tue, 31 Jul 2018 18:06:46 +0200 Subject: [PATCH] ui: implemented handling of case where index db is used by different process while trying to swap files --- src/lib/project/Project.cpp | 50 +++++++++++++++++++++++++++++-------- src/lib/project/Project.h | 3 ++- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/lib/project/Project.cpp b/src/lib/project/Project.cpp index 2c1b3029..99c9e4d2 100644 --- a/src/lib/project/Project.cpp +++ b/src/lib/project/Project.cpp @@ -121,8 +121,12 @@ void Project::load(std::shared_ptr dialogView) { "Keep and Continue", "Discard and Restore" }) == 0) { LOG_INFO("Switching to temporary indexing data on user's decision"); - FileSystem::remove(dbPath); - FileSystem::rename(tempDbPath, dbPath); + if (!swapToTempStorageFile(dbPath, tempDbPath, dialogView)) + { + m_state = PROJECT_STATE_NOT_LOADED; + MessageStatus(L"Unable to load project", true, false).dispatch(); + return; + } } else { @@ -547,10 +551,9 @@ void Project::buildIndex(const RefreshInfo& info, std::shared_ptr di taskSequential->addTask(std::make_shared()->addChildTasks( std::make_shared()->addChildTasks( std::make_shared("keep_database"), - std::make_shared([this]() { - Task::dispatch(std::make_shared([this]() { - swapToTempStorage(); - m_state = PROJECT_STATE_LOADED; + std::make_shared([dialogView, this]() { + Task::dispatch(std::make_shared([dialogView, this]() { + swapToTempStorage(dialogView); })); }) ), @@ -566,7 +569,6 @@ void Project::buildIndex(const RefreshInfo& info, std::shared_ptr di taskSequential->addTask(std::make_shared([dialogView, this]() { m_isIndexing = false; - MessageIndexingFinished().dispatch(); })); @@ -577,7 +579,7 @@ void Project::buildIndex(const RefreshInfo& info, std::shared_ptr di MessageIndexingStarted().dispatch(); } -void Project::swapToTempStorage() +void Project::swapToTempStorage(std::shared_ptr dialogView) { LOG_INFO("Switching to temporary indexing data"); @@ -586,8 +588,13 @@ void Project::swapToTempStorage() const FilePath bookmarkDbFilePath = m_storage->getBookmarkDbFilePath(); m_storage.reset(); - FileSystem::remove(indexDbFilePath); - FileSystem::rename(tempIndexDbFilePath, indexDbFilePath); + + if (!swapToTempStorageFile(indexDbFilePath, tempIndexDbFilePath, dialogView)) + { + m_state = PROJECT_STATE_NOT_LOADED; + return; + } + m_storage = std::make_shared(indexDbFilePath, bookmarkDbFilePath); m_storage->setup(); @@ -597,6 +604,29 @@ void Project::swapToTempStorage() //dialogView->hideUnknownProgressDialog(); m_storageCache->setSubject(m_storage); + m_state = PROJECT_STATE_LOADED; +} + +bool Project::swapToTempStorageFile(const FilePath& indexDbFilePath, const FilePath& tempIndexDbFilePath, std::shared_ptr dialogView) +{ + try + { + FileSystem::remove(indexDbFilePath); + FileSystem::rename(tempIndexDbFilePath, indexDbFilePath); + } + catch (std::exception& e) + { + if (m_hasGUI) + { + dialogView->confirm( + "

The old index database file of this project seems to be used by a different process and cannot " + "be updated.

Please close all processes that are using this database and re-load this project to " + "apply or discard the changes pending from the current indexer run.

" + ); + } + return false; + } + return true; } void Project::discardTempStorage() diff --git a/src/lib/project/Project.h b/src/lib/project/Project.h index 46d043ce..687be6bc 100644 --- a/src/lib/project/Project.h +++ b/src/lib/project/Project.h @@ -57,7 +57,8 @@ private: Project(const Project&); - void swapToTempStorage(); + void swapToTempStorage(std::shared_ptr dialogView); + bool swapToTempStorageFile(const FilePath& indexDbFilePath, const FilePath& tempIndexDbFilePath, std::shared_ptr dialogView); void discardTempStorage(); bool hasCxxSourceGroup() const;