Fix StepUpdateApplication's undo 40/321140/1
authorIlho Kim <ilho159.kim@samsung.com>
Fri, 14 Mar 2025 09:58:01 +0000 (18:58 +0900)
committerilho kim <ilho159.kim@samsung.com>
Fri, 14 Mar 2025 10:10:09 +0000 (10:10 +0000)
Internal privilege is not recovered from the undo of
StepUpdateApplication

Change-Id: I801cd4032f32c5ecc44bd67c1e7218a6a33d6d9f
Signed-off-by: Ilho Kim <ilho159.kim@samsung.com>
(cherry picked from commit 551849049ab31f725fcfd141c1fd9eae429bc7e5)

src/common/step/pkgmgr/step_update_app.cc
src/common/step/pkgmgr/step_update_app.h

index a46932fc573c5407ab5b101ab10029e95d35de89..6da59cf68035d737a44ebe5e4e32c8297cc54310 100644 (file)
@@ -8,6 +8,7 @@
 #include <sys/types.h>
 #include <unistd.h>
 
+#include <pkgmgrinfo_type.h>
 #include <vcore/Certificate.h>
 
 #include <cassert>
 #include <string>
 
 #include "common/pkgmgr_registration.h"
+#include "common/privileges.h"
 #include "common/utils/pkgmgr_query.h"
 #include "common/utils/file_util.h"
 
 namespace common_installer {
 namespace pkgmgr {
 
+bool StepUpdateApplication::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_->old_manifest_data.get();
+  if (!manifest) {
+    LOG(ERROR) << "manifest data null";
+    return false;
+  }
+  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 StepUpdateApplication::process() {
+  if (!BackupPrivileges())
+    LOG(ERROR) << "can't backup privilege";
+
   if (!UpgradeAppInPkgmgr(context_->manifest_data.get(),
                           context_->pkgid.get(),
                           context_->certificate_info.get(),
index b607f53a9399320d76f2b93c3fafbfdb14c2e811..cc83e252589202b5caa2588778f6bb1d22304e07 100644 (file)
@@ -22,6 +22,9 @@ class StepUpdateApplication : public Step {
   Status undo() override;
   Status precheck() override { return Status::OK; }
 
+ private:
+  bool BackupPrivileges();
+
   STEP_NAME(UpdateApplication)
 };