From 624b16d5096cf24667e336381046d32f7a8fe351 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Wed, 19 Aug 2020 13:09:17 +0900 Subject: [PATCH] Refactor AUL metadata plugin parser This patch separates plugins from aul-parser. alias-appid-plugin and allowed-appid-plugin are added. When calling PKGMGR_MDPARSER_PLUGIN_CLEAN(), the plugins write the metadata to the database. Change-Id: Ib6bb7b3663fe851d696b70ccf525410819b73fbd Signed-off-by: Hwankyu Jhun --- packaging/aul.spec | 2 +- parser/CMakeLists.txt | 2 +- parser/metadata/CMakeLists.txt | 24 +- parser/metadata/alias-appid/CMakeLists.txt | 29 ++ parser/metadata/alias-appid/appsvc_db.cc | 81 ++++ parser/metadata/alias-appid/appsvc_db.hh | 43 ++ parser/metadata/alias-appid/data/alias-appid.info | 1 + parser/metadata/alias-appid/pkgmgr_interface.cc | 102 +++++ parser/metadata/alias-appid/plugin_manager.cc | 81 ++++ parser/metadata/alias-appid/plugin_manager.hh | 40 ++ parser/metadata/allowed-appid/CMakeLists.txt | 29 ++ parser/metadata/allowed-appid/appsvc_db.cc | 68 ++++ parser/metadata/allowed-appid/appsvc_db.hh | 42 ++ .../metadata/allowed-appid/data/allowed-appid.info | 1 + parser/metadata/allowed-appid/pkgmgr_interface.cc | 102 +++++ parser/metadata/allowed-appid/plugin_manager.cc | 65 +++ parser/metadata/allowed-appid/plugin_manager.hh | 40 ++ parser/metadata/common/app_event_args.hh | 68 ++++ parser/metadata/common/database.cc | 140 +++++++ parser/metadata/common/database.hh | 78 ++++ .../event_type.hh} | 24 +- parser/metadata/common/exception.hh | 58 +++ .../log_private.hh} | 9 +- parser/metadata/common/metadata.hh | 47 +++ parser/metadata/common/metadata_plugin.cc | 124 ++++++ parser/metadata/common/metadata_plugin.hh | 59 +++ parser/metadata/common/metadata_type.hh | 33 ++ parser/metadata/inc/metadata_plugin_parser_db.h | 40 -- .../metadata/inc/metadata_plugin_parser_handler.h | 34 -- parser/metadata/src/metadata_plugin_parser.c | 105 ----- parser/metadata/src/metadata_plugin_parser_db.c | 436 --------------------- .../metadata/src/metadata_plugin_parser_handler.c | 259 ------------ .../src/metadata_plugin_parser_pkgmgr_interface.c | 83 ---- 33 files changed, 1349 insertions(+), 1000 deletions(-) create mode 100644 parser/metadata/alias-appid/CMakeLists.txt create mode 100644 parser/metadata/alias-appid/appsvc_db.cc create mode 100644 parser/metadata/alias-appid/appsvc_db.hh create mode 100644 parser/metadata/alias-appid/data/alias-appid.info create mode 100644 parser/metadata/alias-appid/pkgmgr_interface.cc create mode 100644 parser/metadata/alias-appid/plugin_manager.cc create mode 100644 parser/metadata/alias-appid/plugin_manager.hh create mode 100644 parser/metadata/allowed-appid/CMakeLists.txt create mode 100644 parser/metadata/allowed-appid/appsvc_db.cc create mode 100644 parser/metadata/allowed-appid/appsvc_db.hh create mode 100644 parser/metadata/allowed-appid/data/allowed-appid.info create mode 100644 parser/metadata/allowed-appid/pkgmgr_interface.cc create mode 100644 parser/metadata/allowed-appid/plugin_manager.cc create mode 100644 parser/metadata/allowed-appid/plugin_manager.hh create mode 100644 parser/metadata/common/app_event_args.hh create mode 100644 parser/metadata/common/database.cc create mode 100644 parser/metadata/common/database.hh rename parser/metadata/{inc/metadata_plugin_parser.h => common/event_type.hh} (52%) create mode 100644 parser/metadata/common/exception.hh rename parser/metadata/{inc/metadata_plugin_parser_private.h => common/log_private.hh} (80%) create mode 100644 parser/metadata/common/metadata.hh create mode 100644 parser/metadata/common/metadata_plugin.cc create mode 100644 parser/metadata/common/metadata_plugin.hh create mode 100644 parser/metadata/common/metadata_type.hh delete mode 100644 parser/metadata/inc/metadata_plugin_parser_db.h delete mode 100644 parser/metadata/inc/metadata_plugin_parser_handler.h delete mode 100644 parser/metadata/src/metadata_plugin_parser.c delete mode 100644 parser/metadata/src/metadata_plugin_parser_db.c delete mode 100644 parser/metadata/src/metadata_plugin_parser_handler.c delete mode 100644 parser/metadata/src/metadata_plugin_parser_pkgmgr_interface.c diff --git a/packaging/aul.spec b/packaging/aul.spec index d9b9715..9c21e60 100755 --- a/packaging/aul.spec +++ b/packaging/aul.spec @@ -145,7 +145,7 @@ chsmack -a 'User::Home' %{TZ_SYS_DB}/.component.db-journal %{_datadir}/parser-plugins/* %{TZ_SYS_DB}/.appsvc.db* %{TZ_SYS_DB}/.component.db* -%{_sysconfdir}/package-manager/parserlib/metadata/libaul-parser.so +%{_sysconfdir}/package-manager/parserlib/metadata/*.so %{_sysconfdir}/package-manager/parserlib/libcomponent-based-application.so %attr(0755,root,root) %{_sysconfdir}/gumd/useradd.d/* %attr(0755,root,root) %{_datadir}/upgrade/scripts/501.component_upgrade.sh diff --git a/parser/CMakeLists.txt b/parser/CMakeLists.txt index 86a6684..ea6a081 100644 --- a/parser/CMakeLists.txt +++ b/parser/CMakeLists.txt @@ -1,2 +1,2 @@ -ADD_SUBDIRECTORY(metadata) ADD_SUBDIRECTORY(component) +ADD_SUBDIRECTORY(metadata) diff --git a/parser/metadata/CMakeLists.txt b/parser/metadata/CMakeLists.txt index 459dce3..590bf06 100644 --- a/parser/metadata/CMakeLists.txt +++ b/parser/metadata/CMakeLists.txt @@ -1,22 +1,2 @@ -SET(TARGET_AUL_PARSER "aul-parser") - -SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC") - -AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/src AUL_PARSER_SRCS) - -ADD_LIBRARY(${TARGET_AUL_PARSER} SHARED ${AUL_PARSER_SRCS}) - -TARGET_INCLUDE_DIRECTORIES(${TARGET_AUL_PARSER} - PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/inc) - -APPLY_PKG_CONFIG(${TARGET_AUL_PARSER} PUBLIC - DLOG_DEPS - GLIB_DEPS - LIBTZPLATFORM_CONFIG_DEPS - LIBXML_DEPS - PKGMGR_INSTALLER_DEPS - SQLITE3_DEPS -) - -INSTALL(TARGETS ${TARGET_AUL_PARSER} - DESTINATION ${SYSCONF_INSTALL_DIR}/package-manager/parserlib/metadata) +ADD_SUBDIRECTORY(alias-appid) +ADD_SUBDIRECTORY(allowed-appid) diff --git a/parser/metadata/alias-appid/CMakeLists.txt b/parser/metadata/alias-appid/CMakeLists.txt new file mode 100644 index 0000000..8a31b41 --- /dev/null +++ b/parser/metadata/alias-appid/CMakeLists.txt @@ -0,0 +1,29 @@ +SET(TARGET_ALIAS_APPID_PLUGIN "alias-appid-plugin") + +SET(SHARED_DIR "${CMAKE_INSTALL_PREFIX}/share") + +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC") + +AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} ALIAS_APPID_PLUGIN_SRCS) +AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/../common COMMON_SRCS) + +ADD_LIBRARY(${TARGET_ALIAS_APPID_PLUGIN} SHARED + ${ALIAS_APPID_PLUGIN_SRCS} + ${COMMON_SRCS}) + +TARGET_INCLUDE_DIRECTORIES(${TARGET_ALIAS_APPID_PLUGIN} PUBLIC + "${CMAKE_CURRENT_SOURCE_DIR}/../") + +APPLY_PKG_CONFIG(${TARGET_ALIAS_APPID_PLUGIN} PUBLIC + DLOG_DEPS + GLIB_DEPS + LIBTZPLATFORM_CONFIG_DEPS + LIBXML_DEPS + PKGMGR_INSTALLER_DEPS + SQLITE3_DEPS +) + +INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/data/alias-appid.info + DESTINATION ${SHARED_DIR}/parser-plugins) +INSTALL(TARGETS ${TARGET_ALIAS_APPID_PLUGIN} + DESTINATION ${SYSCONF_INSTALL_DIR}/package-manager/parserlib/metadata) diff --git a/parser/metadata/alias-appid/appsvc_db.cc b/parser/metadata/alias-appid/appsvc_db.cc new file mode 100644 index 0000000..d763e92 --- /dev/null +++ b/parser/metadata/alias-appid/appsvc_db.cc @@ -0,0 +1,81 @@ +/* + * 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. + */ + +#include + +#include + +#include "alias-appid/appsvc_db.hh" +#include "common/log_private.hh" + +namespace plugin { + +static const uid_t ROOT_UID = 0; +static const uid_t GLOBAL_USER = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER); + +AppSvcDB::AppSvcDB(uid_t uid) : Database(GetDBPath(uid)) { +} + +AppSvcDB::~AppSvcDB() = default; + +void AppSvcDB::Insert(const std::string& alias_appid, + const std::string& appid) { + static const char query[] = "INSERT OR REPLACE INTO " + "alias_info(alias_appid, appid) VALUES(?, ?);"; + sqlite3_stmt* stmt; + __PREPARE_V2(GetHandle(), query, strlen(query), stmt); + std::unique_ptr stmt_ptr( + stmt, sqlite3_finalize); + __BIND_TEXT(GetHandle(), stmt, 1, alias_appid.c_str()); + __BIND_TEXT(GetHandle(), stmt, 2, appid.c_str()); + __STEP(GetHandle(), stmt); +} + +void AppSvcDB::Delete(const std::string& alias_appid, + const std::string& appid) { + static const char query[] = "DELETE FROM alias_info WHERE " + "alias_appid = ? AND appid = ?;"; + sqlite3_stmt* stmt; + __PREPARE_V2(GetHandle(), query, strlen(query), stmt); + std::unique_ptr stmt_ptr( + stmt, sqlite3_finalize); + __BIND_TEXT(GetHandle(), stmt, 1, alias_appid.c_str()); + __BIND_TEXT(GetHandle(), stmt, 2, appid.c_str()); + __STEP(GetHandle(), stmt); +} + +void AppSvcDB::Delete(const std::string& appid) { + static const char query[] = "DELETE FROM alias_info WHERE appid = ?;"; + sqlite3_stmt* stmt; + __PREPARE_V2(GetHandle(), query, strlen(query), stmt); + std::unique_ptr stmt_ptr( + stmt, sqlite3_finalize); + __BIND_TEXT(GetHandle(), stmt, 1, appid.c_str()); + __STEP(GetHandle(), stmt); +} + +std::string AppSvcDB::GetDBPath(uid_t uid) { + std::string db_path; + if (uid == ROOT_UID || uid == GLOBAL_USER) { + db_path = std::string(tzplatform_getenv(TZ_SYS_DB)) + "/.appsvc.db"; + } else { + db_path = std::string(tzplatform_getenv(TZ_SYS_DB)) + "/user/" + + std::to_string(uid) + "/.appsvc.db"; + } + return db_path; +} + +} // namespace plugin diff --git a/parser/metadata/alias-appid/appsvc_db.hh b/parser/metadata/alias-appid/appsvc_db.hh new file mode 100644 index 0000000..ac0e094 --- /dev/null +++ b/parser/metadata/alias-appid/appsvc_db.hh @@ -0,0 +1,43 @@ +/* + * 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_APPSVC_DB_HH_ +#define ALIAS_APPID_APPSVC_DB_HH_ + +#include + +#include + +#include "common/database.hh" + +namespace plugin { + +class AppSvcDB : public Database { + public: + AppSvcDB(uid_t uid); + virtual ~AppSvcDB(); + + 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); + + private: + static std::string GetDBPath(uid_t uid); +}; + +} // namespace plugin + +#endif // ALIAS_APPID_APPSVC_DB_HH_ diff --git a/parser/metadata/alias-appid/data/alias-appid.info b/parser/metadata/alias-appid/data/alias-appid.info new file mode 100644 index 0000000..c57d194 --- /dev/null +++ b/parser/metadata/alias-appid/data/alias-appid.info @@ -0,0 +1 @@ +type="metadata";name="http://tizen.org/metadata/aliasappid";path="/etc/package-manager/parserlib/metadata/libalias-appid-plugin.so" diff --git a/parser/metadata/alias-appid/pkgmgr_interface.cc b/parser/metadata/alias-appid/pkgmgr_interface.cc new file mode 100644 index 0000000..a11eecf --- /dev/null +++ b/parser/metadata/alias-appid/pkgmgr_interface.cc @@ -0,0 +1,102 @@ +/* + * 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. + */ + +#include + +#include "common/log_private.hh" +#include "alias-appid/plugin_manager.hh" + +#undef API +#define API __attribute__ ((visibility("default"))) + +using namespace plugin; + +extern "C" API int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgid, + const char *appid, GList *list) +{ + _W("[__ALIAS_APPID__] pkgid(%s), appid(%s)", pkgid, appid); + PluginManager::GetInst().AddAppEventArgs(pkgid, appid, + EventType::Install, list); + return 0; +} + +extern "C" API int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgid, + const char *appid, GList *list) +{ + _W("[__ALIAS_APPID__] pkgid(%s), appid(%s)", pkgid, appid); + PluginManager::GetInst().AddAppEventArgs(pkgid, appid, + EventType::Uninstall, list); + return 0; +} + +extern "C" API int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgid, + const char *appid, GList *list) +{ + _W("[__ALIAS_APPID__] pkgid(%s), appid(%s)", pkgid, appid); + PluginManager::GetInst().AddAppEventArgs(pkgid, appid, + EventType::Upgrade, list); + return 0; +} + +extern "C" API int PKGMGR_MDPARSER_PLUGIN_RECOVERINSTALL(const char *pkgid, + const char *appid, GList *list) +{ + _W("[__ALIAS_APPID__] pkgid(%s), appid(%s)", pkgid, appid); + PluginManager::GetInst().AddAppEventArgs(pkgid, appid, + EventType::Uninstall, list); + return 0; +} + +extern "C" API int PKGMGR_MDPARSER_PLUGIN_RECOVERUNINSTALL(const char *pkgid, + const char *appid, GList *list) +{ + _W("[__ALIAS_APPID__] pkgid(%s), appid(%s)", pkgid, appid); + PluginManager::GetInst().AddAppEventArgs(pkgid, appid, + EventType::Uninstall, list); + return 0; +} + +extern "C" API int PKGMGR_MDPARSER_PLUGIN_RECOVERUPGRADE(const char *pkgid, + const char *appid, GList *list) +{ + _W("[__ALIAS_APPID__] pkgid(%s), appid(%s)", pkgid, appid); + PluginManager::GetInst().AddAppEventArgs(pkgid, appid, + EventType::Upgrade, list); + return 0; +} + +extern "C" API int PKGMGR_MDPARSER_PLUGIN_CLEAN(const char *pkgid, + const char *appid, GList *list) +{ + _W("[__ALIAS_APPID__] pkgid(%s), appid(%s)", pkgid, appid); + PluginManager::GetInst().Clean(); + return 0; +} + +extern "C" API int PKGMGR_MDPARSER_PLUGIN_UNDO(const char *pkgid, + const char *appid, GList *list) +{ + _W("[__ALIAS_APPID__] pkgid(%s), appid(%s)", pkgid, appid); + PluginManager::GetInst().Undo(); + return 0; +} + +extern "C" API int PKGMGR_MDPARSER_PLUGIN_REMOVED(const char *pkgid, + const char *appid, GList *list) +{ + _W("[__ALIAS_APPID__] pkgid(%s), appid(%s)", pkgid, appid); + return 0; +} diff --git a/parser/metadata/alias-appid/plugin_manager.cc b/parser/metadata/alias-appid/plugin_manager.cc new file mode 100644 index 0000000..65dc817 --- /dev/null +++ b/parser/metadata/alias-appid/plugin_manager.cc @@ -0,0 +1,81 @@ +/* + * 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. + */ + +#include "alias-appid/appsvc_db.hh" +#include "alias-appid/plugin_manager.hh" +#include "common/exception.hh" +#include "common/log_private.hh" + +namespace plugin { + +PluginManager::PluginManager() + : MetadataPlugin(std::unique_ptr( + new AppSvcDB(MetadataPlugin::GetUid()))) { +} + +PluginManager::~PluginManager() = default; + +PluginManager& PluginManager::GetInst() { + static PluginManager inst; + return inst; +} + +bool PluginManager::StepInstall(const std::unique_ptr& args) { + auto* db = dynamic_cast(GetDB()); + for (auto& metadata : args->GetMetadataList()) { + try { + db->Insert(metadata->GetValue(), args->GetAppId()); + } catch (Exception& e) { + return false; + } + } + return true; +} + +bool PluginManager::StepUninstall(const std::unique_ptr& args) { + if (args->GetMetadataList().empty()) { + try { + auto* db = dynamic_cast(GetDB()); + db->Delete(args->GetAppId()); + } catch (Exception& e) { + _W("Failed to delete alias info. appid(%s)", args->GetAppId().c_str()); + } + return true; + } + + for (auto& metadata : args->GetMetadataList()) { + try { + auto* db = dynamic_cast(GetDB()); + db->Delete(metadata->GetValue(), args->GetAppId()); + } catch (Exception& e) { + return false; + } + } + return true; +} + +bool PluginManager::StepUpgrade(const std::unique_ptr& args) { + try { + auto* db = dynamic_cast(GetDB()); + db->Delete(args->GetAppId()); + } catch (Exception& e) { + _W("Failed to delete alias info. appid(%s)", args->GetAppId().c_str()); + } + + return StepInstall(args); +} + +} // namespace plugin diff --git a/parser/metadata/alias-appid/plugin_manager.hh b/parser/metadata/alias-appid/plugin_manager.hh new file mode 100644 index 0000000..8238ed7 --- /dev/null +++ b/parser/metadata/alias-appid/plugin_manager.hh @@ -0,0 +1,40 @@ +/* + * 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_PLUGIN_MANAGER_HH_ +#define ALIAS_APPID_PLUGIN_MANAGER_HH_ + +#include "common/metadata_plugin.hh" + +namespace plugin { + +class PluginManager : public MetadataPlugin { + private: + PluginManager(); + ~PluginManager(); + + public: + static PluginManager& GetInst(); + + bool StepInstall(const std::unique_ptr& args) override; + bool StepUninstall(const std::unique_ptr& args) override; + bool StepUpgrade(const std::unique_ptr& args) override; + +}; + +} // namespace plugin + +#endif // ALIAS_APPID_PLUGIN_MANAGER_HH_ diff --git a/parser/metadata/allowed-appid/CMakeLists.txt b/parser/metadata/allowed-appid/CMakeLists.txt new file mode 100644 index 0000000..7a2ffe8 --- /dev/null +++ b/parser/metadata/allowed-appid/CMakeLists.txt @@ -0,0 +1,29 @@ +SET(TARGET_ALLOWED_APPID_PLUGIN "allowed-appid-plugin") + +SET(SHARED_DIR "${CMAKE_INSTALL_PREFIX}/share") + +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC") + +AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} ALLOWED_APPID_PLUGIN_SRCS) +AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/../common COMMON_SRCS) + +ADD_LIBRARY(${TARGET_ALLOWED_APPID_PLUGIN} SHARED + ${ALLOWED_APPID_PLUGIN_SRCS} + ${COMMON_SRCS}) + +TARGET_INCLUDE_DIRECTORIES(${TARGET_ALLOWED_APPID_PLUGIN} PUBLIC + "${CMAKE_CURRENT_SOURCE_DIR}/../") + +APPLY_PKG_CONFIG(${TARGET_ALLOWED_APPID_PLUGIN} PUBLIC + DLOG_DEPS + GLIB_DEPS + LIBTZPLATFORM_CONFIG_DEPS + LIBXML_DEPS + PKGMGR_INSTALLER_DEPS + SQLITE3_DEPS +) + +INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/data/allowed-appid.info + DESTINATION ${SHARED_DIR}/parser-plugins) +INSTALL(TARGETS ${TARGET_ALLOWED_APPID_PLUGIN} + DESTINATION ${SYSCONF_INSTALL_DIR}/package-manager/parserlib/metadata) diff --git a/parser/metadata/allowed-appid/appsvc_db.cc b/parser/metadata/allowed-appid/appsvc_db.cc new file mode 100644 index 0000000..f5a32a5 --- /dev/null +++ b/parser/metadata/allowed-appid/appsvc_db.cc @@ -0,0 +1,68 @@ +/* + * 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. + */ + +#include + +#include + +#include "allowed-appid/appsvc_db.hh" +#include "common/log_private.hh" + +namespace plugin { + +static const uid_t ROOT_UID = 0; +static const uid_t GLOBAL_USER = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER); + +AppSvcDB::AppSvcDB(uid_t uid) : Database(GetDBPath(uid)) { +} + +AppSvcDB::~AppSvcDB() = default; + +void AppSvcDB::Insert(const std::string& appid, + const std::string& allowed_appid) { + static const char query[] = "INSERT OR REPLACE INTO " + "allowed_info(appid, allowed_appid) VALUES(?, ?);"; + sqlite3_stmt* stmt; + __PREPARE_V2(GetHandle(), query, strlen(query), stmt); + std::unique_ptr stmt_ptr( + stmt, sqlite3_finalize); + __BIND_TEXT(GetHandle(), stmt, 1, appid.c_str()); + __BIND_TEXT(GetHandle(), stmt, 2, allowed_appid.c_str()); + __STEP(GetHandle(), stmt); +} + +void AppSvcDB::Delete(const std::string& appid) { + static const char query[] = "DELETE FROM allowed_info WHERE appid = ?;"; + sqlite3_stmt* stmt; + __PREPARE_V2(GetHandle(), query, strlen(query), stmt); + std::unique_ptr stmt_ptr( + stmt, sqlite3_finalize); + __BIND_TEXT(GetHandle(), stmt, 1, appid.c_str()); + __STEP(GetHandle(), stmt); +} + +std::string AppSvcDB::GetDBPath(uid_t uid) { + std::string db_path; + if (uid == ROOT_UID || uid == GLOBAL_USER) { + db_path = std::string(tzplatform_getenv(TZ_SYS_DB)) + "/.appsvc.db"; + } else { + db_path = std::string(tzplatform_getenv(TZ_SYS_DB)) + "/user/" + + std::to_string(uid) + "/.appsvc.db"; + } + return db_path; +} + +} // namespace plugin diff --git a/parser/metadata/allowed-appid/appsvc_db.hh b/parser/metadata/allowed-appid/appsvc_db.hh new file mode 100644 index 0000000..a3c3888 --- /dev/null +++ b/parser/metadata/allowed-appid/appsvc_db.hh @@ -0,0 +1,42 @@ +/* + * 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_APPSVC_DB_HH_ +#define ALIAS_APPID_APPSVC_DB_HH_ + +#include + +#include + +#include "common/database.hh" + +namespace plugin { + +class AppSvcDB : public Database { + public: + AppSvcDB(uid_t uid); + virtual ~AppSvcDB(); + + void Insert(const std::string& appid, const std::string& allowed_appid); + void Delete(const std::string& appid); + + private: + static std::string GetDBPath(uid_t uid); +}; + +} // namespace plugin + +#endif // ALIAS_APPID_APPSVC_DB_HH_ diff --git a/parser/metadata/allowed-appid/data/allowed-appid.info b/parser/metadata/allowed-appid/data/allowed-appid.info new file mode 100644 index 0000000..2d10b79 --- /dev/null +++ b/parser/metadata/allowed-appid/data/allowed-appid.info @@ -0,0 +1 @@ +type="metadata";name="http://tizen.org/metadata/allowedappid";path="/etc/package-manager/parserlib/metadata/liballowed-appid-plugin.so" diff --git a/parser/metadata/allowed-appid/pkgmgr_interface.cc b/parser/metadata/allowed-appid/pkgmgr_interface.cc new file mode 100644 index 0000000..4cb4219 --- /dev/null +++ b/parser/metadata/allowed-appid/pkgmgr_interface.cc @@ -0,0 +1,102 @@ +/* + * 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. + */ + +#include + +#include "allowed-appid/plugin_manager.hh" +#include "common/log_private.hh" + +#undef API +#define API __attribute__ ((visibility("default"))) + +using namespace plugin; + +extern "C" API int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgid, + const char *appid, GList *list) +{ + _W("[__ALLOWED_APPID__] pkgid(%s), appid(%s)", pkgid, appid); + PluginManager::GetInst().AddAppEventArgs(pkgid, appid, + EventType::Install, list); + return 0; +} + +extern "C" API int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgid, + const char *appid, GList *list) +{ + _W("[__ALLOWED_APPID__] pkgid(%s), appid(%s)", pkgid, appid); + PluginManager::GetInst().AddAppEventArgs(pkgid, appid, + EventType::Uninstall, list); + return 0; +} + +extern "C" API int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgid, + const char *appid, GList *list) +{ + _W("[__ALLOWED_APPID__] pkgid(%s), appid(%s)", pkgid, appid); + PluginManager::GetInst().AddAppEventArgs(pkgid, appid, + EventType::Upgrade, list); + return 0; +} + +extern "C" API int PKGMGR_MDPARSER_PLUGIN_RECOVERINSTALL(const char *pkgid, + const char *appid, GList *list) +{ + _W("[__ALLOWED_APPID__] pkgid(%s), appid(%s)", pkgid, appid); + PluginManager::GetInst().AddAppEventArgs(pkgid, appid, + EventType::Uninstall, list); + return 0; +} + +extern "C" API int PKGMGR_MDPARSER_PLUGIN_RECOVERUNINSTALL(const char *pkgid, + const char *appid, GList *list) +{ + _W("[__ALLOWED_APPID__] pkgid(%s), appid(%s)", pkgid, appid); + PluginManager::GetInst().AddAppEventArgs(pkgid, appid, + EventType::Uninstall, list); + return 0; +} + +extern "C" API int PKGMGR_MDPARSER_PLUGIN_RECOVERUPGRADE(const char *pkgid, + const char *appid, GList *list) +{ + _W("[__ALLOWED_APPID__] pkgid(%s), appid(%s)", pkgid, appid); + PluginManager::GetInst().AddAppEventArgs(pkgid, appid, + EventType::Upgrade, list); + return 0; +} + +extern "C" API int PKGMGR_MDPARSER_PLUGIN_CLEAN(const char *pkgid, + const char *appid, GList *list) +{ + _W("[__ALLOWED_APPID__] pkgid(%s), appid(%s)", pkgid, appid); + PluginManager::GetInst().Clean(); + return 0; +} + +extern "C" API int PKGMGR_MDPARSER_PLUGIN_UNDO(const char *pkgid, + const char *appid, GList *list) +{ + _W("[__ALLOWED_APPID__] pkgid(%s), appid(%s)", pkgid, appid); + PluginManager::GetInst().Undo(); + return 0; +} + +extern "C" API int PKGMGR_MDPARSER_PLUGIN_REMOVED(const char *pkgid, + const char *appid, GList *list) +{ + _W("[__ALLOWED_APPID__] pkgid(%s), appid(%s)", pkgid, appid); + return 0; +} diff --git a/parser/metadata/allowed-appid/plugin_manager.cc b/parser/metadata/allowed-appid/plugin_manager.cc new file mode 100644 index 0000000..6f50a7e --- /dev/null +++ b/parser/metadata/allowed-appid/plugin_manager.cc @@ -0,0 +1,65 @@ +/* + * 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. + */ + +#include "allowed-appid/appsvc_db.hh" +#include "allowed-appid/plugin_manager.hh" +#include "common/exception.hh" +#include "common/log_private.hh" + +namespace plugin { + +PluginManager::PluginManager() + : MetadataPlugin(std::unique_ptr( + new AppSvcDB(MetadataPlugin::GetUid()))) { +} + +PluginManager::~PluginManager() = default; + +PluginManager& PluginManager::GetInst() { + static PluginManager inst; + return inst; +} + +bool PluginManager::StepInstall(const std::unique_ptr& args) { + auto* db = dynamic_cast(GetDB()); + for (auto& metadata : args->GetMetadataList()) { + try { + db->Insert(args->GetAppId(), metadata->GetValue()); + } catch (Exception& e) { + return false; + } + } + return true; +} + +bool PluginManager::StepUninstall(const std::unique_ptr& args) { + try { + auto* db = dynamic_cast(GetDB()); + db->Delete(args->GetAppId()); + } catch (Exception& e) { + _W("Failed to delete alias info. appid(%s)", args->GetAppId().c_str()); + } + return true; +} + +bool PluginManager::StepUpgrade(const std::unique_ptr& args) { + if (!StepUninstall(args)) + return false; + + return StepInstall(args); +} + +} // namespace plugin diff --git a/parser/metadata/allowed-appid/plugin_manager.hh b/parser/metadata/allowed-appid/plugin_manager.hh new file mode 100644 index 0000000..8238ed7 --- /dev/null +++ b/parser/metadata/allowed-appid/plugin_manager.hh @@ -0,0 +1,40 @@ +/* + * 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_PLUGIN_MANAGER_HH_ +#define ALIAS_APPID_PLUGIN_MANAGER_HH_ + +#include "common/metadata_plugin.hh" + +namespace plugin { + +class PluginManager : public MetadataPlugin { + private: + PluginManager(); + ~PluginManager(); + + public: + static PluginManager& GetInst(); + + bool StepInstall(const std::unique_ptr& args) override; + bool StepUninstall(const std::unique_ptr& args) override; + bool StepUpgrade(const std::unique_ptr& args) override; + +}; + +} // namespace plugin + +#endif // ALIAS_APPID_PLUGIN_MANAGER_HH_ diff --git a/parser/metadata/common/app_event_args.hh b/parser/metadata/common/app_event_args.hh new file mode 100644 index 0000000..947dd5a --- /dev/null +++ b/parser/metadata/common/app_event_args.hh @@ -0,0 +1,68 @@ +/* + * 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 COMMON_APP_EVENT_ARGS_HH_ +#define COMMON_APP_EVENT_ARGS_HH_ + +#include +#include +#include + +#include "common/event_type.hh" +#include "common/metadata.hh" + +namespace plugin { + +class AppEventArgs { + public: + AppEventArgs(std::string appid, std::string pkgid, EventType event_type) + : appid_(std::move(appid)), + pkgid_(std::move(pkgid)), + event_type_(event_type) { + } + + virtual ~AppEventArgs() = default; + + const std::string& GetAppId() { + return appid_; + } + + const std::string& GetPkgId() { + return pkgid_; + } + + EventType GetEventType() const { + return event_type_; + } + + const std::list>& GetMetadataList() { + return list_; + } + + void AddMetadata(std::unique_ptr metadata) { + list_.push_back(std::move(metadata)); + } + + private: + std::string appid_; + std::string pkgid_; + EventType event_type_; + std::list> list_; +}; + +} // namespace plugin + +#endif // COMMON_APP_EVENT_ARGS_HH_ diff --git a/parser/metadata/common/database.cc b/parser/metadata/common/database.cc new file mode 100644 index 0000000..7789626 --- /dev/null +++ b/parser/metadata/common/database.cc @@ -0,0 +1,140 @@ +/* + * 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. + */ + +#include + +#include + +#include "common/database.hh" +#include "common/exception.hh" +#include "common/log_private.hh" + +namespace plugin { + +static const int BUSY_WAITING_USEC = 50000; +static const int BUSY_WAITING_MAX = 40; + +Database::Database(std::string path) : path_(std::move(path)) { +} + +Database::~Database() { + Close(); +} + +void Database::BeginTransaction() { + int ret = sqlite3_exec(db_, "BEGIN TRANSACTION", nullptr, nullptr, nullptr); + if (ret != SQLITE_OK) { + _E("sqlite3_exec() is failed. error(%s:%d)", sqlite3_errmsg(db_), ret); + THROW(-ret); + } +} + +void Database::EndTransaction() { + int ret = sqlite3_exec(db_, "END TRANSACTION", nullptr, nullptr, nullptr); + if (ret != SQLITE_OK) { + _E("sqlite3_exec() is failed. error(%s:%d)", sqlite3_errmsg(db_), ret); + THROW(-ret); + } +} + +void Database::Rollback() { + int ret = sqlite3_exec(db_, "ROLLBACK", nullptr, nullptr, nullptr); + if (ret != SQLITE_OK) { + _E("sqlite3_exec() is failed. error(%s:%d", sqlite3_errmsg(db_), ret); + THROW(-ret); + } +} + +void Database::Open() { + sqlite3* db; + int ret = sqlite3_open_v2(path_.c_str(), &db, SQLITE_OPEN_READWRITE, nullptr); + if (ret != SQLITE_OK) { + _E("sqlite3_open_v2() is failed. error(%d)", ret); + THROW(-ret); + } + + ret = sqlite3_busy_handler(db, BusyHandler, + static_cast(const_cast(path_.c_str()))); + if (ret != SQLITE_OK) { + _E("sqlite3_busy_handler() is failed. error(%s:%d)", + sqlite3_errmsg(db), ret); + sqlite3_close_v2(db); + THROW(-ret); + } + + ret = sqlite3_exec(db, "PRAGMA foreign_keys=ON", nullptr, nullptr, nullptr); + if (ret != SQLITE_OK) { + _E("sqlite3_exec() is failed. error(%s:%d)", sqlite3_errmsg(db), ret); + sqlite3_close_v2(db); + THROW(-ret); + } + + db_ = db; +} + +void Database::Close() { + if (db_) { + sqlite3_close(db_); + db_ = nullptr; + } +} + +sqlite3* Database::GetHandle() { + return db_; +} + +bool Database::IntegrityCheck() { + static const char query[] = "PRAGMA integrity_check"; + sqlite3_stmt* stmt; + int ret = sqlite3_prepare_v2(db_, query, strlen(query), &stmt, nullptr); + if (ret != SQLITE_OK) { + _E("sqlite3_prepare_v2() is failed. error(%s:%d)", + sqlite3_errmsg(db_), ret); + return false; + } + + std::unique_ptr stmt_ptr( + stmt, sqlite3_finalize); + + ret = sqlite3_step(stmt); + if (ret != SQLITE_ROW) { + _E("sqlite3_step() is failed. error(%s:%d)", sqlite3_errmsg(db_), ret); + return false; + } + + auto* res = reinterpret_cast(sqlite3_column_text(stmt, 0)); + if (res == nullptr) { + _E("sqlite3_column_text() is failed. error(%s)", sqlite3_errmsg(db_)); + return false; + } + + if (strcmp(res, "ok") != 0) + return false; + + return true; +} + +int Database::BusyHandler(void* data, int count) { + if (count < BUSY_WAITING_MAX) { + usleep(BUSY_WAITING_USEC); + return 1; + } + auto* path = static_cast(data); + _E("Database(%s) is busy", path); + return 0; +} + +} // namespace plugin diff --git a/parser/metadata/common/database.hh b/parser/metadata/common/database.hh new file mode 100644 index 0000000..ef73956 --- /dev/null +++ b/parser/metadata/common/database.hh @@ -0,0 +1,78 @@ +/* + * 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 COMMON_DATABASE_HH_ +#define COMMON_DATABASE_HH_ + +#include + +#include + +#include "common/exception.hh" + +#define __PREPARE_V2(db, query, length, stmt) do { \ + int ret = sqlite3_prepare_v2(db, query, length, &stmt, nullptr); \ + if (ret != SQLITE_OK) { \ + _E("sqlite3_prepare_v2() is failed. error(%s:%d)", \ + sqlite3_errmsg(db), ret); \ + THROW(-ret); \ + } \ +} while (0) + + +#define __BIND_TEXT(db, stmt, i, text) do { \ + int ret = sqlite3_bind_text(stmt, i, text, -1, SQLITE_TRANSIENT); \ + if (ret != SQLITE_OK) { \ + _E("sqlite3_bind_text() is failed. index(%d), error(%s:%d)", \ + i, sqlite3_errmsg(db), ret); \ + THROW(-ret); \ + } \ +} while (0) + +#define __STEP(db, stmt) do { \ + int ret = sqlite3_step(stmt); \ + if (ret != SQLITE_DONE) { \ + _E("sqlite3_step() is failed. error(%s:%d)", sqlite3_errmsg(db), ret); \ + THROW(-ret); \ + } \ +} while (0) + +namespace plugin { + +class Database { + public: + Database(std::string path); + virtual ~Database(); + + void Open(); + void Close(); + bool IntegrityCheck(); + void BeginTransaction(); + void EndTransaction(); + void Rollback(); + sqlite3* GetHandle(); + + private: + static int BusyHandler(void* data, int count); + + private: + std::string path_; + sqlite3* db_ = nullptr; +}; + +} // namespace plugin + +#endif // COMMON_DATABASE_HH_ diff --git a/parser/metadata/inc/metadata_plugin_parser.h b/parser/metadata/common/event_type.hh similarity index 52% rename from parser/metadata/inc/metadata_plugin_parser.h rename to parser/metadata/common/event_type.hh index 8ec25b4..13ea562 100644 --- a/parser/metadata/inc/metadata_plugin_parser.h +++ b/parser/metadata/common/event_type.hh @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016 - 2020 Samsung Electronics Co., Ltd All Rights Reserved + * 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. @@ -14,19 +14,17 @@ * limitations under the License. */ -#ifndef __METADATA_PLUGIN_PARSER_H__ -#define __METADATA_PLUGIN_PARSER_H__ +#ifndef COMMON_EVENT_TYPE_HH_ +#define COMMON_EVENT_TYPE_HH_ -#include -#include +namespace plugin { -int metadata_plugin_parser_install(const char *pkgid, const char *appid, - GList *list); +enum class EventType { + Install, + Uninstall, + Upgrade, +}; -int metadata_plugin_parser_uninstall(const char *pkgid, const char *appid, - GList *list); +} // namespace plugin -int metadata_plugin_parser_upgrade(const char *pkgid, const char *appid, - GList *list); - -#endif /* __METADATA_PLUGIN_PARSER_H__ */ +#endif // COMMON_EVENT_TYPE_HH_ diff --git a/parser/metadata/common/exception.hh b/parser/metadata/common/exception.hh new file mode 100644 index 0000000..b2fd682 --- /dev/null +++ b/parser/metadata/common/exception.hh @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * + * 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 COMMON_EXCEPTION_HH_ +#define COMMON_EXCEPTION_HH_ + +#include + +#include +#include + +#include "common/log_private.hh" + +#define THROW(error_code) throw Exception(error_code, __FILE__, __LINE__) + +namespace plugin { + +class Exception : public std::exception { + public: + explicit Exception(int error_code, std::string file = __FILE__, + int line = __LINE__ ) { + error_code_ = error_code; + message_ = std::string(basename(const_cast(file.c_str()))) + ":" + + std::to_string(line) + " code:" + std::to_string(error_code_); + _E("%s", message_.c_str()); + } + + virtual ~Exception() {} + + virtual const char* what(void) const noexcept { + return message_.c_str(); + } + + int GetErrorCode() { + return error_code_; + } + + private: + int error_code_; + std::string message_; +}; + +} // namespace plugin + +#endif // COMMON_EXCEPTION_HH_ diff --git a/parser/metadata/inc/metadata_plugin_parser_private.h b/parser/metadata/common/log_private.hh similarity index 80% rename from parser/metadata/inc/metadata_plugin_parser_private.h rename to parser/metadata/common/log_private.hh index 4f3aa3d..53d1730 100644 --- a/parser/metadata/inc/metadata_plugin_parser_private.h +++ b/parser/metadata/common/log_private.hh @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef __METADATA_PLUGIN_PARSER_PRIVATE_H__ -#define __METADATA_PLUGIN_PARSER_PRIVATE_H__ +#ifndef COMMON_LOG_PRIVATE_HH_ +#define COMMON_LOG_PRIVATE_HH_ #include @@ -34,7 +34,4 @@ #undef _D #define _D LOGD -#undef API -#define API __attribute__ ((visibility("default"))) - -#endif /* __METADATA_PLUGIN_PARSER_PRIVATE_H__ */ +#endif // COMMON_LOG_PRIVATE_HH_ diff --git a/parser/metadata/common/metadata.hh b/parser/metadata/common/metadata.hh new file mode 100644 index 0000000..993376d --- /dev/null +++ b/parser/metadata/common/metadata.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 COMMON_METADATA_HH_ +#define COMMON_METADATA_HH_ + +#include + +namespace plugin { + +class Metadata { + public: + Metadata(std::string key, std::string value) + : key_(std::move(key)), value_(std::move(value)) { + } + + virtual ~Metadata() = default; + + const std::string& GetKey() { + return key_; + } + + const std::string& GetValue() { + return value_; + } + + private: + std::string key_; + std::string value_; +}; + +} // namespace plugin + +#endif // COMMON_METADATA_HH_ diff --git a/parser/metadata/common/metadata_plugin.cc b/parser/metadata/common/metadata_plugin.cc new file mode 100644 index 0000000..ab64fa2 --- /dev/null +++ b/parser/metadata/common/metadata_plugin.cc @@ -0,0 +1,124 @@ +/* + * 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. + */ + +#include +#include + +#include "common/database.hh" +#include "common/exception.hh" +#include "common/log_private.hh" +#include "common/metadata_plugin.hh" +#include "common/metadata_type.hh" + +namespace plugin { + +void MetadataPlugin::AddAppEventArgs(const std::string& pkgid, + const std::string& appid, EventType event_type, GList* list) { + auto args = new (std::nothrow) AppEventArgs(appid, pkgid, event_type); + if (args == nullptr) { + _E("Out of memory"); + return; + } + + GList* iter = list; + while (iter) { + metadata_t* md = static_cast(iter->data); + args->AddMetadata(std::unique_ptr( + new (std::nothrow) Metadata(md->key, md->value))); + iter = g_list_next(iter); + } + + list_.emplace_back(args); +} + +void MetadataPlugin::Clean() { + if (!Prepare()) + return; + + for (auto& args : list_) { + bool ret = true; + if (args->GetEventType() == EventType::Install) { + ret = StepInstall(args); + } else if (args->GetEventType() == EventType::Uninstall) { + ret = StepUninstall(args); + } else if (args->GetEventType() == EventType::Upgrade) { + ret = StepUpgrade(args); + } + + if (!ret) { + Rollback(); + return; + } + } + Post(); +} + +Database* MetadataPlugin::GetDB() { + return db_.get(); +} + +uid_t MetadataPlugin::GetUid() { + uid_t target_uid; + pkgmgr_installer_info_get_target_uid(&target_uid); + return target_uid; +} + +bool MetadataPlugin::Prepare() { + try { + db_->Open(); + if (!db_->IntegrityCheck()) + return false; + db_->BeginTransaction(); + } catch (Exception& e) { + return false; + } + return true; +} + +bool MetadataPlugin::StepInstall(const std::unique_ptr& args) { + return true; +} + +bool MetadataPlugin::StepUninstall(const std::unique_ptr& args) { + return true; +} + +bool MetadataPlugin::StepUpgrade(const std::unique_ptr& args) { + return true; +} + +void MetadataPlugin::Post() { + try { + db_->EndTransaction(); + db_->Close(); + } catch (Exception& e) { + _E("Exception(%d) occurs", e.GetErrorCode()); + } +} + +void MetadataPlugin::Rollback() { + try { + db_->Rollback(); + } catch (Exception& e) { + _E("Exception(%d) occurs", e.GetErrorCode()); + } +} + +void MetadataPlugin::Undo() { + _E("Undo"); +} + +} // namespace plugin diff --git a/parser/metadata/common/metadata_plugin.hh b/parser/metadata/common/metadata_plugin.hh new file mode 100644 index 0000000..bf41b45 --- /dev/null +++ b/parser/metadata/common/metadata_plugin.hh @@ -0,0 +1,59 @@ +/* + * 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 COMMON_METADATA_PLUGIN_HH_ +#define COMMON_METADATA_PLUGIN_HH_ + +#include +#include + +#include +#include +#include + +#include "common/app_event_args.hh" +#include "common/database.hh" + +namespace plugin { + +class MetadataPlugin { + public: + MetadataPlugin(std::unique_ptr db) : db_(std::move(db)) { } + virtual ~MetadataPlugin() = default; + + void AddAppEventArgs(const std::string& pkgid, const std::string& appid, + EventType event_type, GList* list); + void Clean(); + void Undo(); + Database* GetDB(); + + virtual bool Prepare(); + 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 void Post(); + virtual void Rollback(); + + static uid_t GetUid(); + + public: + std::unique_ptr db_; + std::list> list_; +}; + +} // namespace plugin + +#endif // COMMON_METADATA_PLUGIN_HH_ diff --git a/parser/metadata/common/metadata_type.hh b/parser/metadata/common/metadata_type.hh new file mode 100644 index 0000000..629daed --- /dev/null +++ b/parser/metadata/common/metadata_type.hh @@ -0,0 +1,33 @@ +/* + * 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 COMMON_METADATA_TYPE_HH_ +#define COMMON_METADATA_TYPE_HH_ + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct metadata_s { + const char* key; + const char* value; +} metadata_t; + +#ifdef __cplusplus +} +#endif + +#endif // COMMON_METADATA_TYPE_HH_ diff --git a/parser/metadata/inc/metadata_plugin_parser_db.h b/parser/metadata/inc/metadata_plugin_parser_db.h deleted file mode 100644 index d572980..0000000 --- a/parser/metadata/inc/metadata_plugin_parser_db.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2016 - 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 __METADATA_PLUGIN_PARSER_DB_H__ -#define __METADATA_PLUGIN_PARSER_DB_H__ - -int metadata_parser_db_insert_alias_info(const char *alias_appid, - const char *appid); - -int metadata_parser_db_delete_alias_info(const char *appid); - -int metadata_parser_db_insert_allowed_info(const char *appid, - const char *allowed_appid); - -int metadata_parser_db_delete_allowed_info(const char *appid); - -int metadata_parser_db_init(void); - -void metadata_parser_db_fini(void); - -int metadata_parser_db_begin_transaction(void); - -int metadata_parser_db_end_transaction(void); - -int metadata_parser_db_rollback(void); - -#endif /* __METADATA_PLUGIN_PARSER_DB_H__ */ diff --git a/parser/metadata/inc/metadata_plugin_parser_handler.h b/parser/metadata/inc/metadata_plugin_parser_handler.h deleted file mode 100644 index 3c8a355..0000000 --- a/parser/metadata/inc/metadata_plugin_parser_handler.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2016 - 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 __METADATA_PLUGIN_PARSER_HANDLER_H__ -#define __METADATA_PLUGIN_PARSER_HANDLER_H__ - -int metadata_parser_handler_install(const char *appid, - const char *key, const char *value); - -int metadata_parser_handler_uninstall(const char *appid, - const char *key, const char *value); - -int metadata_parser_handler_uninstall_all(const char *appid); - -int metadata_parser_handler_init(void); - -void metadata_parser_handler_fini(void); - -int metadata_parser_handler_rollback(void); - -#endif /* __METADATA_PLUGIN_PARSER_HANDLER_H__ */ diff --git a/parser/metadata/src/metadata_plugin_parser.c b/parser/metadata/src/metadata_plugin_parser.c deleted file mode 100644 index a0ec680..0000000 --- a/parser/metadata/src/metadata_plugin_parser.c +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Copyright (c) 2016 - 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. - */ - -#include -#include -#include -#include -#include -#include - -#include "metadata_plugin_parser.h" -#include "metadata_plugin_parser_handler.h" -#include "metadata_plugin_parser_private.h" - -typedef struct metadata { - const char *key; - const char *value; -} metadata; - -int metadata_plugin_parser_install(const char *pkgid, const char *appid, - GList *list) -{ - metadata *md; - GList *iter; - int ret; - - ret = metadata_parser_handler_init(); - if (ret < 0) - return ret; - - iter = list; - while (iter) { - md = (metadata *)iter->data; - ret = metadata_parser_handler_install(appid, md->key, - md->value); - if (ret < 0) { - _E("Failed to install %s:%s", md->key, md->value); - metadata_parser_handler_rollback(); - break; - } - - iter = g_list_next(iter); - } - - metadata_parser_handler_fini(); - return ret; -} - -int metadata_plugin_parser_uninstall(const char *pkgid, const char *appid, - GList *list) -{ - metadata *md; - GList *iter; - int ret; - - ret = metadata_parser_handler_init(); - if (ret < 0) - return ret; - - iter = list; - while (iter) { - md = (metadata *)iter->data; - ret = metadata_parser_handler_uninstall(appid, md->key, - md->value); - if (ret < 0) { - _E("Failed to uninstall %s:%s", md->key, md->value); - metadata_parser_handler_rollback(); - break; - } - - iter = g_list_next(iter); - } - - if (!list) { - ret = metadata_parser_handler_uninstall_all(appid); - if (ret < 0) { - _E("Failed to uninstall all metadata info. appid(%s)", - appid); - metadata_parser_handler_rollback(); - } - } - - metadata_parser_handler_fini(); - return ret; -} - -int metadata_plugin_parser_upgrade(const char *pkgid, const char *appid, - GList *list) -{ - metadata_plugin_parser_uninstall(pkgid, appid, list); - return metadata_plugin_parser_install(pkgid, appid, list); -} diff --git a/parser/metadata/src/metadata_plugin_parser_db.c b/parser/metadata/src/metadata_plugin_parser_db.c deleted file mode 100644 index d82538b..0000000 --- a/parser/metadata/src/metadata_plugin_parser_db.c +++ /dev/null @@ -1,436 +0,0 @@ -/* - * Copyright (c) 2016 - 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. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "metadata_plugin_parser_db.h" -#include "metadata_plugin_parser_private.h" - -#define ROOT_UID 0 -#define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER) -#define BUSY_WAITING_USEC 50000 -#define BUSY_WAITING_MAX 40 - -static sqlite3 *__db; - -static uid_t __get_target_uid(void) -{ - static uid_t target_uid = (uid_t)-1; - int ret; - - if (target_uid != (uid_t)-1) - return target_uid; - - ret = pkgmgr_installer_info_get_target_uid(&target_uid); - if (ret < 0) { - _E("Failed to get target uid. error(%d)", ret); - return target_uid; - } - - return target_uid; -} - -static const char *__get_db_path(uid_t uid) -{ - static char db_path[PATH_MAX]; - - if (uid == ROOT_UID || uid == GLOBAL_USER) { - snprintf(db_path, sizeof(db_path), "%s/.appsvc.db", - tzplatform_getenv(TZ_SYS_DB)); - } else { - snprintf(db_path, sizeof(db_path), "%s/user/%d/.appsvc.db", - tzplatform_getenv(TZ_SYS_DB), uid); - } - - return db_path; -} - -static int __db_busy_handler(void *data, int count) -{ - const char *path = (const char *)data; - - if (count < BUSY_WAITING_MAX) { - usleep(BUSY_WAITING_USEC); - return 1; - } - - _E("Database(%s) is busy", path); - - return 0; -} - -static sqlite3 *__open_db(const char *path) -{ - sqlite3 *db; - int ret; - - ret = sqlite3_open_v2(path, &db, SQLITE_OPEN_READWRITE, NULL); - if (ret != SQLITE_OK) { - _E("sqlite3_open_v2() is failed. error(%d)", ret); - return NULL; - } - - ret = sqlite3_busy_handler(db, __db_busy_handler, (void *)path); - if (ret != SQLITE_OK) { - _E("sqlite3_busy_handler() is failed. error(%s:%d)", - sqlite3_errmsg(db), ret); - sqlite3_close_v2(db); - return NULL; - } - - ret = sqlite3_exec(db, "PRAGMA foreign_keys=ON", NULL, NULL, NULL); - if (ret != SQLITE_OK) { - _E("sqlite3_exec() is failed. error(%s:%d)", - sqlite3_errmsg(db), ret); - sqlite3_close_v2(db); - return NULL; - } - - return db; -} - -static void __close_db(sqlite3 *db) -{ - if (!db) - return; - - sqlite3_close_v2(db); -} - -static bool __integrity_check(sqlite3 *db) -{ - static const char query[] = "PRAGMA integrity_check"; - sqlite3_stmt *stmt = NULL; - const char *res; - int ret; - - ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); - if (ret != SQLITE_OK) { - _E("sqlite3_prepare_v2() is failed. error(%s:%d)", - sqlite3_errmsg(db), ret); - return false; - } - - ret = sqlite3_step(stmt); - if (ret != SQLITE_ROW) { - _E("sqlite3_step() is failed. error(%s:%d)", - sqlite3_errmsg(db), ret); - sqlite3_finalize(stmt); - return false; - } - - res = (const char *)sqlite3_column_text(stmt, 0); - if (!res) { - _E("Failed to check integrity db. error(%s:%d)", - sqlite3_errmsg(db), ret); - sqlite3_finalize(stmt); - return false; - } - - if (strcmp(res, "ok") != 0) { - sqlite3_finalize(stmt); - return false; - } - - sqlite3_finalize(stmt); - return true; -} - -int metadata_parser_db_init(void) -{ - uid_t uid = __get_target_uid(); - const char *path; - int ret; - - if (__db) - return 0; - - path = __get_db_path(uid); - if (!path) { - _E("Failed to get db path. uid(%u)", uid); - return -1; - } - - ret = access(path, F_OK); - if (ret != 0) - _E("Database(%s) does not exist", path); - - __db = __open_db(path); - if (!__db) { - _E("Failed to open db(%s)", path); - return -1; - } - - if (!__integrity_check(__db)) { - _E("Database(%s) is corrupted", path); - __close_db(__db); - __db = NULL; - return -1; - } - - return 0; -} - -void metadata_parser_db_fini(void) -{ - if (!__db) - return; - - __close_db(__db); - __db = NULL; -} - -int metadata_parser_db_begin_transaction(void) -{ - int ret; - - if (!__db) { - _E("Database is not prepared"); - return -1; - } - - ret = sqlite3_exec(__db, "BEGIN TRANSACTION", NULL, NULL, NULL); - if (ret != SQLITE_OK) { - _E("sqlite3_exec() is failed. error(%s:%d)", - sqlite3_errmsg(__db), ret); - return -1; - } - - return 0; -} - -int metadata_parser_db_end_transaction(void) -{ - int ret; - - if (!__db) { - _E("Database is not preapred"); - return -1; - } - - ret = sqlite3_exec(__db, "END TRANSACTION", NULL, NULL, NULL); - if (ret != SQLITE_OK) { - _E("sqlite3_exec() is failed. error(%s:%d)", - sqlite3_errmsg(__db), ret); - return -1; - } - - return 0; -} - -int metadata_parser_db_rollback(void) -{ - int ret; - - if (!__db) { - _E("Database is not prepared"); - return -1; - } - - ret = sqlite3_exec(__db, "ROLLBACK", NULL, NULL, NULL); - if (ret != SQLITE_OK) { - _E("sqlite3_exec() is failed. error(%s:%d)", - sqlite3_errmsg(__db), ret); - return -1; - } - - return 0; -} - -int metadata_parser_db_insert_alias_info(const char *alias_appid, - const char *appid) -{ - const char *query = - "INSERT OR REPLACE INTO alias_info(alias_appid, appid) " - "values(?,?);"; - sqlite3_stmt *stmt; - int result = 0; - int ret; - - if (alias_appid == NULL || appid == NULL) { - _E("Invalid parameters"); - return -1; - } - - ret = sqlite3_prepare_v2(__db, query, strlen(query), &stmt, NULL); - if (ret != SQLITE_OK) { - _E("sqlite3_prepare_v2() is failed. error(%s:%d)", - sqlite3_errmsg(__db), ret); - return -1; - } - - ret = sqlite3_bind_text(stmt, 1, alias_appid, -1, SQLITE_TRANSIENT); - if (ret != SQLITE_OK) { - _E("sqlite3_bind_text() is failed. error(%s:%d)", - sqlite3_errmsg(__db), ret); - result = -1; - goto end; - } - - ret = sqlite3_bind_text(stmt, 2, appid, -1, SQLITE_TRANSIENT); - if (ret != SQLITE_OK) { - _E("sqlite3_bind_text() is failed. error(%s:%d)", - sqlite3_errmsg(__db), ret); - result = -1; - goto end; - } - - ret = sqlite3_step(stmt); - if (ret != SQLITE_DONE) { - _E("sqlite3_step() is failed. error(%s:%d)", - sqlite3_errmsg(__db), ret); - result = -1; - } - -end: - sqlite3_finalize(stmt); - return result; -} - -int metadata_parser_db_delete_alias_info(const char *appid) -{ - const char *query = "DELETE FROM alias_info WHERE appid = ?;"; - sqlite3_stmt *stmt; - int result = 0; - int ret; - - if (appid == NULL) { - _E("Invalid parameter"); - return -1; - } - - ret = sqlite3_prepare_v2(__db, query, strlen(query), &stmt, NULL); - if (ret != SQLITE_OK) { - _E("sqlite3_prepare_v2() is failed. error(%s:%d)", - sqlite3_errmsg(__db), ret); - return -1; - } - - ret = sqlite3_bind_text(stmt, 1, appid, -1, SQLITE_TRANSIENT); - if (ret != SQLITE_OK) { - _E("sqlite3_bind_text() is failed. error(%s:%d)", - sqlite3_errmsg(__db), ret); - result = -1; - goto end; - } - - ret = sqlite3_step(stmt); - if (ret != SQLITE_DONE) { - _W("sqlite3_step() is failed. error(%s:%d)", - sqlite3_errmsg(__db), ret); - } - -end: - sqlite3_finalize(stmt); - return result; -} - -int metadata_parser_db_insert_allowed_info(const char *appid, - const char *allowed_appid) -{ - const char *query = - "INSERT OR REPLACE INTO allowed_info(appid, allowed_appid) " - "values(?,?);"; - sqlite3_stmt *stmt; - int result = 0; - int ret; - - if (allowed_appid == NULL || appid == NULL) { - _E("Invalid parameters"); - return -1; - } - - ret = sqlite3_prepare_v2(__db, query, strlen(query), &stmt, NULL); - if (ret != SQLITE_OK) { - _E("sqlite3_prepare_v2() is failed. error(%s:%d)", - sqlite3_errmsg(__db), ret); - return -1; - } - - ret = sqlite3_bind_text(stmt, 1, appid, -1, SQLITE_TRANSIENT); - if (ret != SQLITE_OK) { - _E("sqlite3_bind_text() is failed. error(%s:%d)", - sqlite3_errmsg(__db), ret); - result = -1; - goto end; - } - - ret = sqlite3_bind_text(stmt, 2, allowed_appid, -1, SQLITE_TRANSIENT); - if (ret != SQLITE_OK) { - _E("sqlite3_bind_text() is failed. error(%s:%d)", - sqlite3_errmsg(__db), ret); - result = -1; - goto end; - } - - ret = sqlite3_step(stmt); - if (ret != SQLITE_DONE) { - _E("sqlite3_step() is failed. error(%s:%d)", - sqlite3_errmsg(__db), ret); - result = -1; - } - -end: - sqlite3_finalize(stmt); - return result; -} - -int metadata_parser_db_delete_allowed_info(const char *appid) -{ - const char *query = "DELETE FROM allowed_info WHERE appid = ?;"; - sqlite3_stmt *stmt; - int result = 0; - int ret; - - if (appid == NULL) { - _E("Invalid parameter"); - return -1; - } - - ret = sqlite3_prepare_v2(__db, query, strlen(query), &stmt, NULL); - if (ret != SQLITE_OK) { - _E("sqlite3_prepare_v2() is failed. error(%s:%d)", - sqlite3_errmsg(__db), ret); - return -1; - } - - ret = sqlite3_bind_text(stmt, 1, appid, -1, SQLITE_TRANSIENT); - if (ret != SQLITE_OK) { - _E("sqlite3_bind_text() is failed. error(%s:%d)", - sqlite3_errmsg(__db), ret); - result = -1; - goto end; - } - - ret = sqlite3_step(stmt); - if (ret != SQLITE_DONE) { - _W("sqlite3_step() is failed. error(%s:%d)", - sqlite3_errmsg(__db), ret); - } - -end: - sqlite3_finalize(stmt); - return result; -} diff --git a/parser/metadata/src/metadata_plugin_parser_handler.c b/parser/metadata/src/metadata_plugin_parser_handler.c deleted file mode 100644 index c1268a6..0000000 --- a/parser/metadata/src/metadata_plugin_parser_handler.c +++ /dev/null @@ -1,259 +0,0 @@ -/* - * Copyright (c) 2016 - 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. - */ - -#include -#include -#include -#include -#include -#include - -#include "metadata_plugin_parser_db.h" -#include "metadata_plugin_parser_handler.h" -#include "metadata_plugin_parser_private.h" - -#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) - -typedef int (*metadata_parser_dispatch_install)(const char *appid, - const char *key, const char *value); - -typedef int (*metadata_parser_dispatch_uninstall)(const char *appid); - -typedef struct metadater_parser_dispatcher_s { - metadata_parser_dispatch_install install; - metadata_parser_dispatch_uninstall uninstall; -} metadata_parser_dispatcher; - -enum metadata_key { - KEY_NONE, - KEY_ALIAS_APPID, - KEY_ALLOWED_APPID, - KEY_MAX, -}; - -struct key_map { - enum metadata_key key; - char *name; -}; - -struct key_map map[] = { - { - KEY_ALIAS_APPID, - "http://tizen.org/metadata/aliasappid" - }, - { - KEY_ALLOWED_APPID, - "http://tizen.org/metadata/allowedappid" - } -}; - -static GHashTable *key_table; - -static int __init_metadata_key_table(void) -{ - int i; - - if (key_table) - return 0; - - key_table = g_hash_table_new(g_int_hash, g_str_equal); - if (key_table == NULL) { - _E("out of memory"); - return -1; - } - - for (i = 0; i < ARRAY_SIZE(map); i++) { - g_hash_table_insert(key_table, map[i].name, - (gpointer)map[i].key); - } - - return 0; -} - -static void __fini_metadata_key_table(void) -{ - if (!key_table) - return; - - g_hash_table_destroy(key_table); - key_table = NULL; -} - -int metadata_parser_handler_init(void) -{ - int ret; - - ret = metadata_parser_db_init(); - if (ret < 0) - return ret; - - ret = __init_metadata_key_table(); - if (ret < 0) - return ret; - - return metadata_parser_db_begin_transaction(); -} - -void metadata_parser_handler_fini(void) -{ - metadata_parser_db_end_transaction(); - __fini_metadata_key_table(); - metadata_parser_db_fini(); -} - -int metadata_parser_handler_rollback(void) -{ - return metadata_parser_db_rollback(); -} - -static enum metadata_key __get_metadata_key(const char *key) -{ - if (key == NULL) - return KEY_NONE; - - return (enum metadata_key)g_hash_table_lookup(key_table, key); -} - -static int __install_alias_info(const char *appid, - const char *key, const char *value) -{ - int ret; - - if (appid == NULL || key == NULL || value == NULL) { - _E("Invalid parameter"); - return -1; - } - - ret = metadata_parser_db_insert_alias_info(value, appid); - if (ret < 0) - return -1; - - return 0; -} - -static int __uninstall_alias_info(const char *appid) -{ - int ret; - - if (appid == NULL) { - _E("Invalid parameter"); - return -1; - } - - ret = metadata_parser_db_delete_alias_info(appid); - if (ret < 0) - return -1; - - return 0; -} - -static int __install_allowed_info(const char *appid, - const char *key, const char *value) -{ - int ret; - - if (appid == NULL || key == NULL || value == NULL) { - _E("Invalid parameter"); - return -1; - } - - ret = metadata_parser_db_insert_allowed_info(appid, value); - if (ret < 0) - return -1; - - return 0; -} - -static int __uninstall_allowed_info(const char *appid) -{ - int ret; - - if (appid == NULL) { - _E("Invalid parameter"); - return -1; - } - - ret = metadata_parser_db_delete_allowed_info(appid); - if (ret < 0) - return -1; - - return 0; -} - -static metadata_parser_dispatcher dispatch_table[KEY_MAX] = { - [KEY_ALIAS_APPID] = { - .install = __install_alias_info, - .uninstall = __uninstall_alias_info, - }, - [KEY_ALLOWED_APPID] = { - .install = __install_allowed_info, - .uninstall = __uninstall_allowed_info, - } -}; - -int metadata_parser_handler_install(const char *appid, - const char *key, const char *value) -{ - enum metadata_key mkey; - int ret; - - mkey = __get_metadata_key(key); - if (mkey > KEY_NONE && mkey < KEY_MAX && - dispatch_table[mkey].install) { - ret = dispatch_table[mkey].install(appid, key, value); - if (ret < 0) { - _E("Failed to install %s", key); - return -1; - } - } - - return 0; -} - -int metadata_parser_handler_uninstall(const char *appid, - const char *key, const char *value) -{ - enum metadata_key mkey; - int ret; - - mkey = __get_metadata_key(key); - if (mkey > KEY_NONE && mkey < KEY_MAX && - dispatch_table[mkey].uninstall) { - ret = dispatch_table[mkey].uninstall(appid); - if (ret < 0) { - _E("Failed to uninstall %s", key); - return -1; - } - } - - return 0; -} - -int metadata_parser_handler_uninstall_all(const char *appid) -{ - enum metadata_key iter; - int ret; - - for (iter = KEY_NONE + 1; iter < KEY_MAX; ++iter) { - if (dispatch_table[iter].uninstall) { - ret = dispatch_table[iter].uninstall(appid); - if (ret < 0) - _W("Failed to uninstall %d", iter); - } - } - - return 0; -} diff --git a/parser/metadata/src/metadata_plugin_parser_pkgmgr_interface.c b/parser/metadata/src/metadata_plugin_parser_pkgmgr_interface.c deleted file mode 100644 index 8ecf434..0000000 --- a/parser/metadata/src/metadata_plugin_parser_pkgmgr_interface.c +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright (c) 2016 - 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. - */ - -#include - -#include "metadata_plugin_parser.h" -#include "metadata_plugin_parser_private.h" - -API int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, - GList *list) -{ - _W("pkgid(%s), appid(%s)", pkgid, appid); - return metadata_plugin_parser_install(pkgid, appid, list); -} - -API int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, - GList *list) -{ - _W("pkgid(%s), appid(%s)", pkgid, appid); - return metadata_plugin_parser_uninstall(pkgid, appid, list); -} - -API int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgid, const char *appid, - GList *list) -{ - _W("pkgid(%s), appid(%s)", pkgid, appid); - return metadata_plugin_parser_upgrade(pkgid, appid, list); -} - -API int PKGMGR_MDPARSER_PLUGIN_RECOVERINSTALL(const char *pkgid, - const char *appid, GList *list) -{ - _W("pkgid(%s), appid(%s)", pkgid, appid); - return metadata_plugin_parser_uninstall(pkgid, appid, list); -} - -API int PKGMGR_MDPARSER_PLUGIN_RECOVERUNINSTALL(const char *pkgid, - const char *appid, GList *list) -{ - _W("pkgid(%s), appid(%s)", pkgid, appid); - return metadata_plugin_parser_uninstall(pkgid, appid, list); -} - -API int PKGMGR_MDPARSER_PLUGIN_RECOVERUPGRADE(const char *pkgid, - const char *appid, GList *list) -{ - _W("pkgid(%s), appid(%s)", pkgid, appid); - return metadata_plugin_parser_upgrade(pkgid, appid, list); -} - -API int PKGMGR_MDPARSER_PLUGIN_CLEAN(const char *pkgid, - const char *appid, GList *list) -{ - _W("pkgid(%s), appid(%s)", pkgid, appid); - return 0; -} - -API int PKGMGR_MDPARSER_PLUGIN_UNDO(const char *pkgid, - const char *appid, GList *list) -{ - _W("pkgid(%s), appid(%s)", pkgid, appid); - return metadata_plugin_parser_uninstall(pkgid, appid, list); -} - -API int PKGMGR_MDPARSER_PLUGIN_REMOVED(const char *pkgid, - const char *appid, GList *list) -{ - _W("pkgid(%s), appid(%s)", pkgid, appid); - return metadata_plugin_parser_uninstall(pkgid, appid, list); -} -- 2.7.4