Fix Metadata Plugin Parser 75/245975/5
authorHwankyu Jhun <h.jhun@samsung.com>
Wed, 21 Oct 2020 05:26:10 +0000 (14:26 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Thu, 22 Oct 2020 09:28:40 +0000 (18:28 +0900)
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 <h.jhun@samsung.com>
16 files changed:
parser/metadata/alias-appid/alias_info.hh [new file with mode: 0644]
parser/metadata/alias-appid/appsvc_db.cc
parser/metadata/alias-appid/appsvc_db.hh
parser/metadata/alias-appid/pkgmgr_interface.cc
parser/metadata/alias-appid/plugin_manager.cc
parser/metadata/alias-appid/plugin_manager.hh
parser/metadata/allowed-appid/allowed_info.hh [new file with mode: 0644]
parser/metadata/allowed-appid/appsvc_db.cc
parser/metadata/allowed-appid/appsvc_db.hh
parser/metadata/allowed-appid/pkgmgr_interface.cc
parser/metadata/allowed-appid/plugin_manager.cc
parser/metadata/allowed-appid/plugin_manager.hh
parser/metadata/common/database.cc
parser/metadata/common/database.hh
parser/metadata/common/metadata_plugin.cc
parser/metadata/common/metadata_plugin.hh

diff --git a/parser/metadata/alias-appid/alias_info.hh b/parser/metadata/alias-appid/alias_info.hh
new file mode 100644 (file)
index 0000000..539b06e
--- /dev/null
@@ -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 <string>
+
+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_
index d763e92..333b694 100644 (file)
@@ -31,6 +31,26 @@ AppSvcDB::AppSvcDB(uid_t uid) : Database(GetDBPath(uid)) {
 
 AppSvcDB::~AppSvcDB() = default;
 
+std::vector<std::shared_ptr<AliasInfo>> 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<sqlite3_stmt, decltype(sqlite3_finalize)*>(
+      stmt, sqlite3_finalize);
+  __BIND_TEXT(GetHandle(), stmt, 1, appid.c_str());
+
+  std::vector<std::shared_ptr<AliasInfo>> 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 "
index ac0e094..04190de 100644 (file)
 
 #include <unistd.h>
 
+#include <memory>
 #include <string>
+#include <vector>
 
+#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<std::shared_ptr<AliasInfo>> 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);
index f0ec880..edb4faa 100644 (file)
@@ -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;
 }
 
index c057d5a..040ce90 100644 (file)
@@ -33,6 +33,25 @@ PluginManager& PluginManager::GetInst() {
   return inst;
 }
 
+bool PluginManager::StepBackup(const std::unique_ptr<AppEventArgs>& args) {
+  auto* db = dynamic_cast<AppSvcDB*>(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<AppEventArgs>& args) {
   auto* db = dynamic_cast<AppSvcDB*>(GetDB());
   if (db == nullptr) {
@@ -92,4 +111,24 @@ bool PluginManager::StepUpgrade(const std::unique_ptr<AppEventArgs>& args) {
   return StepInstall(args);
 }
 
+bool PluginManager::StepRestore() {
+  _E("Restore");
+  auto* db = dynamic_cast<AppSvcDB*>(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
index 8238ed7..2d9a3c0 100644 (file)
@@ -17,6 +17,9 @@
 #ifndef ALIAS_APPID_PLUGIN_MANAGER_HH_
 #define ALIAS_APPID_PLUGIN_MANAGER_HH_
 
+#include <vector>
+
+#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<AppEventArgs>& args) override;
   bool StepInstall(const std::unique_ptr<AppEventArgs>& args) override;
   bool StepUninstall(const std::unique_ptr<AppEventArgs>& args) override;
   bool StepUpgrade(const std::unique_ptr<AppEventArgs>& args) override;
+  bool StepRestore() override;
 
+ private:
+  std::vector<std::shared_ptr<AliasInfo>> 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 (file)
index 0000000..52512d8
--- /dev/null
@@ -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 <memory>
+#include <string>
+
+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_
index f5a32a5..500dd18 100644 (file)
@@ -31,6 +31,28 @@ AppSvcDB::AppSvcDB(uid_t uid) : Database(GetDBPath(uid)) {
 
 AppSvcDB::~AppSvcDB() = default;
 
+std::vector<std::shared_ptr<AllowedInfo>> 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<sqlite3_stmt, decltype(sqlite3_finalize)*>(
+      stmt, sqlite3_finalize);
+  __BIND_TEXT(GetHandle(), stmt, 1, appid.c_str());
+
+  std::vector<std::shared_ptr<AllowedInfo>> 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 "
index a3c3888..a66f6be 100644 (file)
  * 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 <unistd.h>
 
+#include <memory>
 #include <string>
+#include <vector>
 
+#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<std::shared_ptr<AllowedInfo>> 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_
index 018cfa4..3c16f90 100644 (file)
@@ -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;
 }
 
index 896d615..62fc062 100644 (file)
@@ -33,6 +33,25 @@ PluginManager& PluginManager::GetInst() {
   return inst;
 }
 
+bool PluginManager::StepBackup(const std::unique_ptr<AppEventArgs>& args) {
+  auto* db = dynamic_cast<AppSvcDB*>(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<std::string> 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<AppEventArgs>& args) {
   return StepInstall(args);
 }
 
+bool PluginManager::StepRestore() {
+  _E("Restore");
+  auto* db = dynamic_cast<AppSvcDB*>(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
index bdc41c2..5c5af7a 100644 (file)
  * 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 <vector>
 
 #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<AppEventArgs>& args) override;
   bool StepInstall(const std::unique_ptr<AppEventArgs>& args) override;
   bool StepUninstall(const std::unique_ptr<AppEventArgs>& args) override;
   bool StepUpgrade(const std::unique_ptr<AppEventArgs>& args) override;
+  bool StepRestore() override;
 
  private:
   std::vector<std::string> Split(const std::string& str,
       const std::string& delim);
+
+ private:
+  std::vector<std::shared_ptr<AllowedInfo>> infos_;
 };
 
 }  // namespace plugin
 
-#endif  // ALIAS_APPID_PLUGIN_MANAGER_HH_
+#endif  // ALLOWED_APPID_PLUGIN_MANAGER_HH_
index 7789626..5699971 100644 (file)
@@ -96,6 +96,14 @@ sqlite3* Database::GetHandle() {
   return db_;
 }
 
+std::string Database::ColumnText(sqlite3_stmt* stmt, int index) {
+  auto* text = reinterpret_cast<const char*>(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;
index ef73956..37302a0 100644 (file)
@@ -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);
index ab64fa2..bfd0bfb 100644 (file)
@@ -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<AppEventArgs>& args) {
+  return true;
+}
+
 bool MetadataPlugin::StepInstall(const std::unique_ptr<AppEventArgs>& args) {
   return true;
 }
@@ -100,6 +117,10 @@ bool MetadataPlugin::StepUpgrade(const std::unique_ptr<AppEventArgs>& 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
index bf41b45..2416f69 100644 (file)
@@ -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<AppEventArgs>& args);
   virtual bool StepInstall(const std::unique_ptr<AppEventArgs>& args);
   virtual bool StepUninstall(const std::unique_ptr<AppEventArgs>& args);
   virtual bool StepUpgrade(const std::unique_ptr<AppEventArgs>& args);
+  virtual bool StepRestore();
   virtual void Post();
   virtual void Rollback();