From 31f6eacd41199ed64172d96e01b9fb41661c8e3b Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Wed, 21 Oct 2020 14:26:10 +0900 Subject: [PATCH] Fix Metadata Plugin Parser After this patch is applied, a commit is performed in step methods. The StepBackup() is added to save previous data. If the installation is failed, previous data will be applied to the database. Change-Id: I0b1290315b00dff6a93c90162daa0e15c2b7b3e6 Signed-off-by: Hwankyu Jhun --- parser/metadata/alias-appid/alias_info.hh | 47 ++++++++++++++++++++++ parser/metadata/alias-appid/appsvc_db.cc | 20 ++++++++++ parser/metadata/alias-appid/appsvc_db.hh | 4 ++ parser/metadata/alias-appid/pkgmgr_interface.cc | 6 +++ parser/metadata/alias-appid/plugin_manager.cc | 39 ++++++++++++++++++ parser/metadata/alias-appid/plugin_manager.hh | 7 ++++ parser/metadata/allowed-appid/allowed_info.hh | 48 +++++++++++++++++++++++ parser/metadata/allowed-appid/appsvc_db.cc | 22 +++++++++++ parser/metadata/allowed-appid/appsvc_db.hh | 10 +++-- parser/metadata/allowed-appid/pkgmgr_interface.cc | 6 +++ parser/metadata/allowed-appid/plugin_manager.cc | 39 ++++++++++++++++++ parser/metadata/allowed-appid/plugin_manager.hh | 12 ++++-- parser/metadata/common/database.cc | 8 ++++ parser/metadata/common/database.hh | 1 + parser/metadata/common/metadata_plugin.cc | 35 ++++++++++++++++- parser/metadata/common/metadata_plugin.hh | 6 ++- 16 files changed, 302 insertions(+), 8 deletions(-) create mode 100644 parser/metadata/alias-appid/alias_info.hh create mode 100644 parser/metadata/allowed-appid/allowed_info.hh diff --git a/parser/metadata/alias-appid/alias_info.hh b/parser/metadata/alias-appid/alias_info.hh new file mode 100644 index 0000000..539b06e --- /dev/null +++ b/parser/metadata/alias-appid/alias_info.hh @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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. + */ + +#ifndef ALIAS_APPID_ALIAS_INFO_HH_ +#define ALIAS_APPID_ALIAS_INFO_HH_ + +#include + +namespace plugin { + +class AliasInfo { + public: + AliasInfo(std::string alias_appid, std::string appid) + : alias_appid_(std::move(alias_appid)), appid_(std::move(appid)) { + } + + virtual ~AliasInfo() = default; + + const std::string& GetAliasAppId() const { + return alias_appid_; + } + + const std::string& GetAppId() const { + return appid_; + } + + private: + std::string alias_appid_; + std::string appid_; +}; + +} // namespace plugin + +#endif // ALIAS_APPID_ALIAS_INFO_HH_ diff --git a/parser/metadata/alias-appid/appsvc_db.cc b/parser/metadata/alias-appid/appsvc_db.cc index d763e92..333b694 100644 --- a/parser/metadata/alias-appid/appsvc_db.cc +++ b/parser/metadata/alias-appid/appsvc_db.cc @@ -31,6 +31,26 @@ AppSvcDB::AppSvcDB(uid_t uid) : Database(GetDBPath(uid)) { AppSvcDB::~AppSvcDB() = default; +std::vector> AppSvcDB::Select( + const std::string& appid) { + static const char query[] = "SELECT alias_appid FROM alias_info " + "WHERE appid = ?;"; + sqlite3_stmt* stmt; + __PREPARE_V2(GetHandle(), query, strlen(query), stmt); + auto stmt_ptr = std::unique_ptr( + stmt, sqlite3_finalize); + __BIND_TEXT(GetHandle(), stmt, 1, appid.c_str()); + + std::vector> infos; + while (sqlite3_step(stmt) == SQLITE_ROW) { + std::string alias_appid = ColumnText(stmt, 0); + if (!alias_appid.empty()) + infos.emplace_back(new (std::nothrow) AliasInfo(alias_appid, appid)); + } + + return infos; +} + void AppSvcDB::Insert(const std::string& alias_appid, const std::string& appid) { static const char query[] = "INSERT OR REPLACE INTO " diff --git a/parser/metadata/alias-appid/appsvc_db.hh b/parser/metadata/alias-appid/appsvc_db.hh index ac0e094..04190de 100644 --- a/parser/metadata/alias-appid/appsvc_db.hh +++ b/parser/metadata/alias-appid/appsvc_db.hh @@ -19,8 +19,11 @@ #include +#include #include +#include +#include "alias-appid/alias_info.hh" #include "common/database.hh" namespace plugin { @@ -30,6 +33,7 @@ class AppSvcDB : public Database { AppSvcDB(uid_t uid); virtual ~AppSvcDB(); + std::vector> Select(const std::string& appid); void Insert(const std::string& alias_appid, const std::string& appid); void Delete(const std::string& alias_appid, const std::string& appid); void Delete(const std::string& appid); diff --git a/parser/metadata/alias-appid/pkgmgr_interface.cc b/parser/metadata/alias-appid/pkgmgr_interface.cc index f0ec880..edb4faa 100644 --- a/parser/metadata/alias-appid/pkgmgr_interface.cc +++ b/parser/metadata/alias-appid/pkgmgr_interface.cc @@ -30,6 +30,7 @@ extern "C" API int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char* pkgid, _W("[__ALIAS_APPID__] pkgid(%s), appid(%s)", pkgid, appid); PluginManager::GetInst().AddAppEventArgs(pkgid, appid, EventType::Install, list); + PluginManager::GetInst().Do(); return 0; } @@ -39,6 +40,7 @@ extern "C" API int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char* pkgid, _W("[__ALIAS_APPID__] pkgid(%s), appid(%s)", pkgid, appid); PluginManager::GetInst().AddAppEventArgs(pkgid, appid, EventType::Uninstall, list); + PluginManager::GetInst().Do(); return 0; } @@ -48,6 +50,7 @@ extern "C" API int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char* pkgid, _W("[__ALIAS_APPID__] pkgid(%s), appid(%s)", pkgid, appid); PluginManager::GetInst().AddAppEventArgs(pkgid, appid, EventType::Upgrade, list); + PluginManager::GetInst().Do(); return 0; } @@ -57,6 +60,7 @@ extern "C" API int PKGMGR_MDPARSER_PLUGIN_RECOVERINSTALL(const char* pkgid, _W("[__ALIAS_APPID__] pkgid(%s), appid(%s)", pkgid, appid); PluginManager::GetInst().AddAppEventArgs(pkgid, appid, EventType::Uninstall, list); + PluginManager::GetInst().Do(); return 0; } @@ -66,6 +70,7 @@ extern "C" API int PKGMGR_MDPARSER_PLUGIN_RECOVERUNINSTALL(const char* pkgid, _W("[__ALIAS_APPID__] pkgid(%s), appid(%s)", pkgid, appid); PluginManager::GetInst().AddAppEventArgs(pkgid, appid, EventType::Uninstall, list); + PluginManager::GetInst().Do(); return 0; } @@ -75,6 +80,7 @@ extern "C" API int PKGMGR_MDPARSER_PLUGIN_RECOVERUPGRADE(const char* pkgid, _W("[__ALIAS_APPID__] pkgid(%s), appid(%s)", pkgid, appid); PluginManager::GetInst().AddAppEventArgs(pkgid, appid, EventType::Upgrade, list); + PluginManager::GetInst().Do(); return 0; } diff --git a/parser/metadata/alias-appid/plugin_manager.cc b/parser/metadata/alias-appid/plugin_manager.cc index c057d5a..040ce90 100644 --- a/parser/metadata/alias-appid/plugin_manager.cc +++ b/parser/metadata/alias-appid/plugin_manager.cc @@ -33,6 +33,25 @@ PluginManager& PluginManager::GetInst() { return inst; } +bool PluginManager::StepBackup(const std::unique_ptr& args) { + auto* db = dynamic_cast(GetDB()); + if (db == nullptr) { + _E("MetadataPlugin is not prepared"); + return false; + } + + try { + auto info_arr = db->Select(args->GetAppId()); + if (info_arr.size() != 0) + infos_.insert(infos_.end(), info_arr.begin(), info_arr.end()); + } catch (Exception& e) { + _E("Exception(%d) occurs", e.GetErrorCode()); + return false; + } + + return true; +} + bool PluginManager::StepInstall(const std::unique_ptr& args) { auto* db = dynamic_cast(GetDB()); if (db == nullptr) { @@ -92,4 +111,24 @@ bool PluginManager::StepUpgrade(const std::unique_ptr& args) { return StepInstall(args); } +bool PluginManager::StepRestore() { + _E("Restore"); + auto* db = dynamic_cast(GetDB()); + if (db == nullptr) { + _E("MetadataPlugin is not prepared"); + return false; + } + + for (auto& info : infos_) { + try { + db->Insert(info->GetAliasAppId(), info->GetAppId()); + } catch (Exception& e) { + _E("Exception(%d) occurs", e.GetErrorCode()); + return false; + } + } + + return true; +} + } // namespace plugin diff --git a/parser/metadata/alias-appid/plugin_manager.hh b/parser/metadata/alias-appid/plugin_manager.hh index 8238ed7..2d9a3c0 100644 --- a/parser/metadata/alias-appid/plugin_manager.hh +++ b/parser/metadata/alias-appid/plugin_manager.hh @@ -17,6 +17,9 @@ #ifndef ALIAS_APPID_PLUGIN_MANAGER_HH_ #define ALIAS_APPID_PLUGIN_MANAGER_HH_ +#include + +#include "alias-appid/alias_info.hh" #include "common/metadata_plugin.hh" namespace plugin { @@ -29,10 +32,14 @@ class PluginManager : public MetadataPlugin { public: static PluginManager& GetInst(); + bool StepBackup(const std::unique_ptr& args) override; bool StepInstall(const std::unique_ptr& args) override; bool StepUninstall(const std::unique_ptr& args) override; bool StepUpgrade(const std::unique_ptr& args) override; + bool StepRestore() override; + private: + std::vector> infos_; }; } // namespace plugin diff --git a/parser/metadata/allowed-appid/allowed_info.hh b/parser/metadata/allowed-appid/allowed_info.hh new file mode 100644 index 0000000..52512d8 --- /dev/null +++ b/parser/metadata/allowed-appid/allowed_info.hh @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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. + */ + +#ifndef ALLOWED_APPID_ALLOWED_INFO_HH_ +#define ALLOWED_APPID_ALLOWED_INFO_HH_ + +#include +#include + +namespace plugin { + +class AllowedInfo { + public: + AllowedInfo(std::string appid, std::string allowed_appid) + : appid_(std::move(appid)), allowed_appid_(std::move(allowed_appid)) { + } + + virtual ~AllowedInfo() = default; + + const std::string& GetAppId() const { + return appid_; + } + + const std::string& GetAllowedAppId() const { + return allowed_appid_; + } + + private: + std::string appid_; + std::string allowed_appid_; +}; + +} // namespace plugin + +#endif // ALLOWED_APPID_ALLOWED_INFO_HH_ diff --git a/parser/metadata/allowed-appid/appsvc_db.cc b/parser/metadata/allowed-appid/appsvc_db.cc index f5a32a5..500dd18 100644 --- a/parser/metadata/allowed-appid/appsvc_db.cc +++ b/parser/metadata/allowed-appid/appsvc_db.cc @@ -31,6 +31,28 @@ AppSvcDB::AppSvcDB(uid_t uid) : Database(GetDBPath(uid)) { AppSvcDB::~AppSvcDB() = default; +std::vector> AppSvcDB::Select( + const std::string& appid) { + static const char query[] = "SELECT allowed_appid FROM " + "allowed_info WHERE appid = ?;"; + sqlite3_stmt* stmt; + __PREPARE_V2(GetHandle(), query, strlen(query), stmt); + auto stmt_ptr = std::unique_ptr( + stmt, sqlite3_finalize); + __BIND_TEXT(GetHandle(), stmt, 1, appid.c_str()); + + std::vector> allowed_infos; + while (sqlite3_step(stmt) == SQLITE_ROW) { + std::string allowed_appid = ColumnText(stmt, 0); + if (!allowed_appid.empty()) { + allowed_infos.emplace_back( + new (std::nothrow) AllowedInfo(appid, allowed_appid)); + } + } + + return allowed_infos; +} + void AppSvcDB::Insert(const std::string& appid, const std::string& allowed_appid) { static const char query[] = "INSERT OR REPLACE INTO " diff --git a/parser/metadata/allowed-appid/appsvc_db.hh b/parser/metadata/allowed-appid/appsvc_db.hh index a3c3888..a66f6be 100644 --- a/parser/metadata/allowed-appid/appsvc_db.hh +++ b/parser/metadata/allowed-appid/appsvc_db.hh @@ -14,13 +14,16 @@ * limitations under the License. */ -#ifndef ALIAS_APPID_APPSVC_DB_HH_ -#define ALIAS_APPID_APPSVC_DB_HH_ +#ifndef ALLOWED_APPID_APPSVC_DB_HH_ +#define ALLOWED_APPID_APPSVC_DB_HH_ #include +#include #include +#include +#include "allowed-appid/allowed_info.hh" #include "common/database.hh" namespace plugin { @@ -30,6 +33,7 @@ class AppSvcDB : public Database { AppSvcDB(uid_t uid); virtual ~AppSvcDB(); + std::vector> Select(const std::string& appid); void Insert(const std::string& appid, const std::string& allowed_appid); void Delete(const std::string& appid); @@ -39,4 +43,4 @@ class AppSvcDB : public Database { } // namespace plugin -#endif // ALIAS_APPID_APPSVC_DB_HH_ +#endif // ALLOWED_APPID_APPSVC_DB_HH_ diff --git a/parser/metadata/allowed-appid/pkgmgr_interface.cc b/parser/metadata/allowed-appid/pkgmgr_interface.cc index 018cfa4..3c16f90 100644 --- a/parser/metadata/allowed-appid/pkgmgr_interface.cc +++ b/parser/metadata/allowed-appid/pkgmgr_interface.cc @@ -30,6 +30,7 @@ extern "C" API int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char* pkgid, _W("[__ALLOWED_APPID__] pkgid(%s), appid(%s)", pkgid, appid); PluginManager::GetInst().AddAppEventArgs(pkgid, appid, EventType::Install, list); + PluginManager::GetInst().Do(); return 0; } @@ -39,6 +40,7 @@ extern "C" API int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char* pkgid, _W("[__ALLOWED_APPID__] pkgid(%s), appid(%s)", pkgid, appid); PluginManager::GetInst().AddAppEventArgs(pkgid, appid, EventType::Uninstall, list); + PluginManager::GetInst().Do(); return 0; } @@ -48,6 +50,7 @@ extern "C" API int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char* pkgid, _W("[__ALLOWED_APPID__] pkgid(%s), appid(%s)", pkgid, appid); PluginManager::GetInst().AddAppEventArgs(pkgid, appid, EventType::Upgrade, list); + PluginManager::GetInst().Do(); return 0; } @@ -57,6 +60,7 @@ extern "C" API int PKGMGR_MDPARSER_PLUGIN_RECOVERINSTALL(const char* pkgid, _W("[__ALLOWED_APPID__] pkgid(%s), appid(%s)", pkgid, appid); PluginManager::GetInst().AddAppEventArgs(pkgid, appid, EventType::Uninstall, list); + PluginManager::GetInst().Do(); return 0; } @@ -66,6 +70,7 @@ extern "C" API int PKGMGR_MDPARSER_PLUGIN_RECOVERUNINSTALL(const char* pkgid, _W("[__ALLOWED_APPID__] pkgid(%s), appid(%s)", pkgid, appid); PluginManager::GetInst().AddAppEventArgs(pkgid, appid, EventType::Uninstall, list); + PluginManager::GetInst().Do(); return 0; } @@ -75,6 +80,7 @@ extern "C" API int PKGMGR_MDPARSER_PLUGIN_RECOVERUPGRADE(const char* pkgid, _W("[__ALLOWED_APPID__] pkgid(%s), appid(%s)", pkgid, appid); PluginManager::GetInst().AddAppEventArgs(pkgid, appid, EventType::Upgrade, list); + PluginManager::GetInst().Do(); return 0; } diff --git a/parser/metadata/allowed-appid/plugin_manager.cc b/parser/metadata/allowed-appid/plugin_manager.cc index 896d615..62fc062 100644 --- a/parser/metadata/allowed-appid/plugin_manager.cc +++ b/parser/metadata/allowed-appid/plugin_manager.cc @@ -33,6 +33,25 @@ PluginManager& PluginManager::GetInst() { return inst; } +bool PluginManager::StepBackup(const std::unique_ptr& args) { + auto* db = dynamic_cast(GetDB()); + if (db == nullptr) { + _E("MetadataPlugin is not prepared"); + return false; + } + + try { + auto info_arr = db->Select(args->GetAppId()); + if (info_arr.size() != 0) + infos_.insert(infos_.end(), info_arr.begin(), info_arr.end()); + } catch (Exception& e) { + _E("Execption(%d) occurs", e.GetErrorCode()); + return false; + } + + return true; +} + std::vector PluginManager::Split(const std::string& str, const std::string& delim) { std::string string(str); @@ -88,4 +107,24 @@ bool PluginManager::StepUpgrade(const std::unique_ptr& args) { return StepInstall(args); } +bool PluginManager::StepRestore() { + _E("Restore"); + auto* db = dynamic_cast(GetDB()); + if (db == nullptr) { + _E("MetadataPlugin is not prepared"); + return false; + } + + for (auto& info : infos_) { + try { + db->Insert(info->GetAppId(), info->GetAllowedAppId()); + } catch (Exception& e) { + _E("Exception(%d) occurs", e.GetErrorCode()); + return false; + } + } + + return true; +} + } // namespace plugin diff --git a/parser/metadata/allowed-appid/plugin_manager.hh b/parser/metadata/allowed-appid/plugin_manager.hh index bdc41c2..5c5af7a 100644 --- a/parser/metadata/allowed-appid/plugin_manager.hh +++ b/parser/metadata/allowed-appid/plugin_manager.hh @@ -14,12 +14,13 @@ * limitations under the License. */ -#ifndef ALIAS_APPID_PLUGIN_MANAGER_HH_ -#define ALIAS_APPID_PLUGIN_MANAGER_HH_ +#ifndef ALLOWED_APPID_PLUGIN_MANAGER_HH_ +#define ALLOWED_APPID_PLUGIN_MANAGER_HH_ #include #include "common/metadata_plugin.hh" +#include "allowed-appid/allowed_info.hh" namespace plugin { @@ -31,15 +32,20 @@ class PluginManager : public MetadataPlugin { public: static PluginManager& GetInst(); + bool StepBackup(const std::unique_ptr& args) override; bool StepInstall(const std::unique_ptr& args) override; bool StepUninstall(const std::unique_ptr& args) override; bool StepUpgrade(const std::unique_ptr& args) override; + bool StepRestore() override; private: std::vector Split(const std::string& str, const std::string& delim); + + private: + std::vector> infos_; }; } // namespace plugin -#endif // ALIAS_APPID_PLUGIN_MANAGER_HH_ +#endif // ALLOWED_APPID_PLUGIN_MANAGER_HH_ diff --git a/parser/metadata/common/database.cc b/parser/metadata/common/database.cc index 7789626..5699971 100644 --- a/parser/metadata/common/database.cc +++ b/parser/metadata/common/database.cc @@ -96,6 +96,14 @@ sqlite3* Database::GetHandle() { return db_; } +std::string Database::ColumnText(sqlite3_stmt* stmt, int index) { + auto* text = reinterpret_cast(sqlite3_column_text(stmt, index)); + if (text) + return std::string(text); + + return {}; +} + bool Database::IntegrityCheck() { static const char query[] = "PRAGMA integrity_check"; sqlite3_stmt* stmt; diff --git a/parser/metadata/common/database.hh b/parser/metadata/common/database.hh index ef73956..37302a0 100644 --- a/parser/metadata/common/database.hh +++ b/parser/metadata/common/database.hh @@ -64,6 +64,7 @@ class Database { void EndTransaction(); void Rollback(); sqlite3* GetHandle(); + std::string ColumnText(sqlite3_stmt* stmt, int index); private: static int BusyHandler(void* data, int count); diff --git a/parser/metadata/common/metadata_plugin.cc b/parser/metadata/common/metadata_plugin.cc index ab64fa2..bfd0bfb 100644 --- a/parser/metadata/common/metadata_plugin.cc +++ b/parser/metadata/common/metadata_plugin.cc @@ -44,11 +44,20 @@ void MetadataPlugin::AddAppEventArgs(const std::string& pkgid, list_.emplace_back(args); } -void MetadataPlugin::Clean() { +void MetadataPlugin::Do() { + _W("Do"); if (!Prepare()) return; for (auto& args : list_) { + bool ret = StepBackup(args); + if (!ret) { + Post(); + return; + } + } + + for (auto& args : list_) { bool ret = true; if (args->GetEventType() == EventType::Install) { ret = StepInstall(args); @@ -66,6 +75,10 @@ void MetadataPlugin::Clean() { Post(); } +void MetadataPlugin::Clean() { + _W("Clean"); +} + Database* MetadataPlugin::GetDB() { return db_.get(); } @@ -88,6 +101,10 @@ bool MetadataPlugin::Prepare() { return true; } +bool MetadataPlugin::StepBackup(const std::unique_ptr& args) { + return true; +} + bool MetadataPlugin::StepInstall(const std::unique_ptr& args) { return true; } @@ -100,6 +117,10 @@ bool MetadataPlugin::StepUpgrade(const std::unique_ptr& args) { return true; } +bool MetadataPlugin::StepRestore() { + return true; +} + void MetadataPlugin::Post() { try { db_->EndTransaction(); @@ -119,6 +140,18 @@ void MetadataPlugin::Rollback() { void MetadataPlugin::Undo() { _E("Undo"); + if (!Prepare()) + return; + + for (auto& args : list_) { + if (!StepUninstall(args)) + _E("StepUninstall() is failed"); + } + + if (!StepRestore()) + _E("StepRestore() is failed"); + + Post(); } } // namespace plugin diff --git a/parser/metadata/common/metadata_plugin.hh b/parser/metadata/common/metadata_plugin.hh index bf41b45..2416f69 100644 --- a/parser/metadata/common/metadata_plugin.hh +++ b/parser/metadata/common/metadata_plugin.hh @@ -36,14 +36,18 @@ class MetadataPlugin { void AddAppEventArgs(const std::string& pkgid, const std::string& appid, EventType event_type, GList* list); + Database* GetDB(); + + void Do(); void Clean(); void Undo(); - Database* GetDB(); virtual bool Prepare(); + virtual bool StepBackup(const std::unique_ptr& args); virtual bool StepInstall(const std::unique_ptr& args); virtual bool StepUninstall(const std::unique_ptr& args); virtual bool StepUpgrade(const std::unique_ptr& args); + virtual bool StepRestore(); virtual void Post(); virtual void Rollback(); -- 2.7.4