Fix StepUnregisterApplication's undo 32/315332/3
authorIlho Kim <ilho159.kim@samsung.com>
Mon, 27 Nov 2023 10:28:38 +0000 (19:28 +0900)
committerIlho Kim <ilho159.kim@samsung.com>
Mon, 2 Dec 2024 03:54:29 +0000 (12:54 +0900)
Internal privilege is not recovered from the undo of
StepUnregisterApplication

Requires:
    - https://review.tizen.org/gerrit/c/platform/core/appfw/pkgmgr-info/+/315330

Change-Id: I5e94c93b0f8a76a15288894b1b1df1c8afe78d8e
Signed-off-by: Ilho Kim <ilho159.kim@samsung.com>
src/common/step/pkgmgr/step_unregister_app.cc
src/common/step/pkgmgr/step_unregister_app.h
src/common/utils/pkgmgr_query.cc
src/common/utils/pkgmgr_query.h

index 94e4e6399dde9743abbababc8a619d050e916b80..5fdf8f3bb9dfaaa046f767370414d22fc49138a0 100644 (file)
@@ -7,6 +7,7 @@
 #include <unistd.h>
 
 #include <pkgmgr_installer.h>
+#include <pkgmgrinfo_type.h>
 #include <vcore/Certificate.h>
 
 #include <cassert>
@@ -14,6 +15,7 @@
 #include <string>
 
 #include "common/pkgmgr_registration.h"
+#include "common/privileges.h"
 #include "common/utils/pkgmgr_query.h"
 #include "common/utils/file_util.h"
 
@@ -66,11 +68,48 @@ bool StepUnregisterApplication::BackupCertInfo() {
   return true;
 }
 
+bool StepUnregisterApplication::BackupPrivileges() {
+  PkgQueryInterface pkg_query(context_->pkgid.get(), context_->uid.get());
+  if (!pkg_query.IsValid()) {
+    LOG(ERROR) << "This package is not installed";
+    return false;
+  }
+
+  std::vector<std::tuple<int, std::string, std::string>> result;
+  if (!pkg_query.PrivilegesForPkgId(&result)) {
+    LOG(ERROR) << "Failed to get package's privileges";
+    return false;
+  }
+
+  manifest_x* manifest = context_->manifest_data.get();
+  g_list_free_full(manifest->privileges, common_installer::FreePrivilegeX);
+  manifest->privileges = nullptr;
+  for (const auto& it : result) {
+    if (std::get<0>(it) != static_cast<int>(PMINFO_PRIVILEGE_NORMAL))
+      continue;
+
+    privilege_x* privilege =
+        reinterpret_cast<privilege_x*>(calloc(1, sizeof(privilege_x)));
+    if (!privilege) {
+      LOG(ERROR) << "Out of memory";
+      return false;
+    }
+    privilege->value = strdup(std::get<1>(it).c_str());
+    privilege->type = strdup(std::get<2>(it).c_str());
+    manifest->privileges = g_list_append(manifest->privileges, privilege);
+  }
+
+  return true;
+}
+
 Step::Status StepUnregisterApplication::process() {
   // Prepare certificate info for rollback operations
   if (!BackupCertInfo())
     LOG(ERROR) << "Failed to backup cert info";
 
+  if (!BackupPrivileges())
+    LOG(ERROR) << "can't backup privilege";
+
   if (!UnregisterAppInPkgmgr(context_->manifest_data.get(),
                              context_->pkgid.get(),
                              context_->uid.get(),
index 48b58844eff9ca7997e43a3d0a11e5284f4c244d..8ef4d6ed05d59bb289640414912465e103055962 100644 (file)
@@ -24,6 +24,7 @@ class StepUnregisterApplication : public Step {
 
  private:
   bool BackupCertInfo();
+  bool BackupPrivileges();
 
   STEP_NAME(UnregisterApplication)
 };
index 69bffd68e5445623bc9162cd37bd70794bee32d3..f53b0774d09f8febc3659bd3aaf0057c7fe29c8a 100644 (file)
@@ -8,6 +8,7 @@
 #include <manifest_parser/utils/logging.h>
 #include <pkgmgr_installer.h>
 #include <pkgmgr_parser.h>
+#include <pkgmgrinfo_type.h>
 #include <tzplatform_config.h>
 
 #include <cstring>
@@ -279,14 +280,41 @@ bool PkgQueryInterface::AppidsForPkgId(std::vector<std::string>* result) {
   return error_ == PMINFO_R_OK;
 }
 
-bool PkgQueryInterface::PrivilegesForPkgId(std::vector<std::string>* result) {
+//bool PkgQueryInterface::PrivilegesForPkgId(std::vector<std::string>* result) {
+//  if (!IsValid())
+//    return false;
+//  error_ = pkgmgrinfo_pkginfo_foreach_privilege(handle_->handle,
+//      [](const char* privilege_name, void* user_data) -> int {
+//        auto* data = static_cast<std::vector<std::string>*>(user_data);
+//        data->emplace_back(privilege_name);
+//        return PMINFO_R_OK;
+//      },
+//      result);
+//  return error_ == PMINFO_R_OK;
+//}
+
+bool PkgQueryInterface::PrivilegesForPkgId(
+    std::vector<std::tuple<int, std::string, std::string>>* result) {
   if (!IsValid())
     return false;
-  error_ = pkgmgrinfo_pkginfo_foreach_privilege(handle_->handle,
-      [](const char* privilege_name, void* user_data) -> int {
-        auto* data = static_cast<std::vector<std::string>*>(user_data);
-        data->emplace_back(privilege_name);
-        return PMINFO_R_OK;
+  error_ = pkgmgrinfo_pkginfo_foreach_privilege_v2(handle_->handle,
+      [](pkgmgrinfo_pkginfo_privilege_h handle, void *user_data) -> int {
+        auto* data = static_cast<std::vector<std::tuple<int, std::string, std::string>>*>(user_data);
+        pkgmgrinfo_privilege_category ca;
+        char* type;
+        char* name;
+
+        if (pkgmgrinfo_get_privilege_category(handle, &ca) != PMINFO_R_OK)
+          return 0;
+
+        if (pkgmgrinfo_get_privilege_name(handle, &name) != PMINFO_R_OK)
+          return 0;
+
+        if (pkgmgrinfo_get_privilege_type(handle, &type) != PMINFO_R_OK)
+          return 0;
+
+        data->emplace_back(std::make_tuple(static_cast<int>(ca), std::string(name), std::string(type)));
+        return 0;
       },
       result);
   return error_ == PMINFO_R_OK;
index 2b9cf783999348d3a0e7c32a27bc23032a206dd7..d9aabf1e4d0014d300ab79d8d872dd220e450815 100644 (file)
@@ -154,7 +154,7 @@ class PkgQueryInterface {
    *
    * \return true if success
    */
-  bool PrivilegesForPkgId(std::vector<std::string>* result);
+  bool PrivilegesForPkgId(std::vector<std::tuple<int, std::string, std::string>>* result);
 
   /**
    * \brief Returns whether plugin has executed for given package