Send pkg upgrade signal 00/306100/1
authorSangyoon Jang <jeremy.jang@samsung.com>
Tue, 24 Oct 2023 08:08:18 +0000 (17:08 +0900)
committerSangyoon Jang <jeremy.jang@samsung.com>
Fri, 16 Feb 2024 06:17:34 +0000 (15:17 +0900)
Change-Id: I117e850f6e32ea90e83d88fe4b9b889262d30446
Signed-off-by: Sangyoon Jang <jeremy.jang@samsung.com>
14 files changed:
src/pkg_upgrade/CMakeLists.txt
src/pkg_upgrade/include/common_type.hh
src/pkg_upgrade/include/pkg_upgrader.hh
src/pkg_upgrade/include/pkgmgr_signal.hh [new file with mode: 0644]
src/pkg_upgrade/include/upgrader.hh
src/pkg_upgrade/src/db_upgrader.cc
src/pkg_upgrade/src/pkg_upgrader.cc
src/pkg_upgrade/src/pkgmgr_signal.cc [new file with mode: 0644]
src/pkg_upgrade/src/ro2rw_upgrader.cc
src/pkg_upgrade/src/ro_upgrader.cc
src/pkg_upgrade/src/rw2ro_upgrader.cc
src/pkg_upgrade/src/rw_upgrader.cc
src/pkg_upgrade/src/simple_upgrader.cc
src/pkg_upgrade/src/upgrader.cc

index f5b621f887bb0526fe093772097be4f47a4f46c3..6353d1005df0c06a5a87c679c4ae8292930bcbcf 100755 (executable)
@@ -8,6 +8,7 @@ ADD_EXECUTABLE(${TARGET_PKG_UPGRADE} ${SRCS})
 # Dependency
 APPLY_PKG_CONFIG(${TARGET_PKG_UPGRADE} PUBLIC
   PKGMGR_INFO_DEPS
+  PKGMGR_INSTALLER_DEPS
   PKGMGR_PARSER_DEPS
   TIZEN_DATABASE_DEPS
   TZPLATFORM_DEPS
index 05a75980fc8655dab265770535ac525cfdfe5bea..40e33b4cdeba7d3ab24116580cf6968555eec76e 100644 (file)
@@ -50,6 +50,15 @@ enum class PkgVersionCmpResult {
   UNKNOWN
 };
 
+enum class PkgUpgradeResult {
+  SKIPPED,
+  INSTALLED,
+  UNINSTALLED,
+  UPDATED,
+  FAILED,
+  UNKNOWN,
+};
+
 class PkgContext {
  public:
   PkgContext(std::string id, std::string version, std::string type,
index fe96679b400852055479b407ea16d4128f26e7d2..94b487f8732b80d69ceb9b0cfb380ec273c8a4a3 100644 (file)
@@ -37,9 +37,13 @@ class PkgUpgrader {
   std::string GetVersion() const;
   const BackendInvoker& GetBackendInvoker() const;
   PkgVersionCmpResult CompareVersion(const PkgUpgrader& pkg) const;
+  PkgUpgradeResult GetResult() const;
 
   virtual bool Upgrade() = 0;
 
+ protected:
+  PkgUpgradeResult result_;
+
  private:
   PkgType type_;
   PkgLocation loc_;
diff --git a/src/pkg_upgrade/include/pkgmgr_signal.hh b/src/pkg_upgrade/include/pkgmgr_signal.hh
new file mode 100644 (file)
index 0000000..6a05c00
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2023 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 PKGMGR_SIGNAL_H_
+#define PKGMGR_SIGNAL_H_
+
+#include <pkgmgr_installer.h>
+
+#include <string>
+
+#include "common_type.hh"
+
+namespace common_fota {
+
+class PkgmgrSignal {
+ public:
+  PkgmgrSignal();
+  virtual ~PkgmgrSignal();
+
+  void SetTotalPkgs(int total_pkgs);
+  bool SendSignal(const std::string& pkgid, PkgUpgradeResult result);
+  bool SendInitialized();
+  bool SendFinished();
+  bool SendProgress();
+
+ private:
+  bool SendSignal(unsigned int progress);
+
+  pkgmgr_installer* pi_;
+  int count_;
+  int total_pkgs_;
+};
+
+}  // common_fota
+
+#endif  // PKGMGR_SIGNAL_H_
index 77479c58b2ace3e249860c3ccb2f78afb11139cf..7173d0daa52d1b509443ee1ce6c6160e4de28fee 100644 (file)
@@ -23,6 +23,7 @@
 #include "file_logbackend.hh"
 #include "pkg_finder.hh"
 #include "pkg_upgrader.hh"
+#include "pkgmgr_signal.hh"
 
 namespace common_fota {
 
@@ -56,6 +57,7 @@ class Upgrader {
   std::list<std::unique_ptr<PkgUpgrader>> failure_list_;
   std::string parser_db_;
   std::string cert_db_;
+  PkgmgrSignal signal_;
 };
 
 }  // common_fota
index a75631000e244b7c8827a0a7473ba8d28ef3f810..ef8c93147686588fca25965280fb7580df9e816f 100644 (file)
@@ -47,11 +47,13 @@ bool DbUpgrader::Upgrade() {
   if (GetLocation() == PkgLocation::RW) {
     if (!SetRwPkg(pkgid_)) {
       LOG(ERROR) << "Failed to update pkg " << pkgid_;
+      result_ = PkgUpgradeResult::FAILED;
       return false;
     }
     LOG(INFO) << "Preloaded files of " << pkgid_ << " has been removed. "
               << "This package will be non-preload RW package";
   }
+  result_ = PkgUpgradeResult::SKIPPED;
   return true;
 }
 
index 4442bee54ce39c98a26b715007f8fc67394e5eee..9bc2cbd1de2c419a457a1984536742bddaabf003 100644 (file)
@@ -30,9 +30,9 @@ PkgUpgrader::PkgUpgrader(const PkgContext& context, PkgOperation pkg_op)
 }
 
 PkgUpgrader::PkgUpgrader(std::string id, std::string version)
-    : type_(PkgType::UNKNOWN), loc_(PkgLocation::UNKNOWN),
-      op_(PkgOperation::COMPLEX), id_(std::move(id)),
-      version_(std::move(version)) {
+    : result_(PkgUpgradeResult::UNKNOWN), type_(PkgType::UNKNOWN),
+      loc_(PkgLocation::UNKNOWN), op_(PkgOperation::COMPLEX),
+      id_(std::move(id)), version_(std::move(version)) {
 }
 
 PkgType PkgUpgrader::GetType() const {
@@ -59,6 +59,10 @@ const BackendInvoker& PkgUpgrader::GetBackendInvoker() const {
   return backend_;
 }
 
+PkgUpgradeResult PkgUpgrader::GetResult() const {
+  return result_;
+}
+
 PkgVersionCmpResult PkgUpgrader::CompareVersion(
     const PkgUpgrader& pkg) const {
   pkgmgrinfo_version_compare_type compare = PMINFO_VERSION_OLD;
diff --git a/src/pkg_upgrade/src/pkgmgr_signal.cc b/src/pkg_upgrade/src/pkgmgr_signal.cc
new file mode 100644 (file)
index 0000000..aff9458
--- /dev/null
@@ -0,0 +1,83 @@
+/*
+ * Copyright (c) 2023 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.
+ */
+
+#include "pkgmgr_signal.hh"
+
+#include <pkgmgr_installer.h>
+
+#include <string>
+
+#include "common_type.hh"
+#include "logging.hh"
+
+namespace {
+
+const unsigned int kProgressRange = 100;
+
+}  // namespace
+
+namespace common_fota {
+
+PkgmgrSignal::PkgmgrSignal() : count_(1), total_pkgs_(0) {
+  pi_ = pkgmgr_installer_new();
+  if (pi_ == nullptr)
+    LOG(ERROR) << "Failed to create pkgmgr installer";
+}
+
+PkgmgrSignal::~PkgmgrSignal() {
+  if (pi_ != nullptr)
+    pkgmgr_installer_free(pi_);
+}
+
+void PkgmgrSignal::SetTotalPkgs(int total_pkgs) {
+  total_pkgs_ = total_pkgs;
+}
+
+bool PkgmgrSignal::SendSignal(unsigned int progress) {
+  if (pi_ == nullptr) {
+    LOG(ERROR) << "pkgmgr installer is not created";
+    return false;
+  }
+
+  int ret = pkgmgr_installer_send_pkg_upgrade_signal(pi_, progress);
+  if (ret != 0) {
+    LOG(ERROR) << "pkgmgr_installer_send_pkg_upgrade_signal failed";
+    return false;
+  }
+  return true;
+}
+
+bool PkgmgrSignal::SendInitialized() {
+  LOG(DEBUG) << "Send initialized signal";
+  return SendSignal(0);
+}
+
+bool PkgmgrSignal::SendFinished() {
+  LOG(DEBUG) << "Send finished signal";
+  return SendSignal(kProgressRange);
+}
+
+bool PkgmgrSignal::SendProgress() {
+  if (count_ > total_pkgs_) {
+    LOG(WARNING) << "count should not be greater than total packages";
+    return false;
+  }
+  unsigned int progress = count_++ * kProgressRange / total_pkgs_;
+  LOG(DEBUG) << "Send progress: " << progress;
+  return SendSignal(progress);
+}
+
+}  // namespace common_fota
index 7b0bcb0b937ab3960f63a638c6856981c6248205..4e46b7494bdf0787b8e7128cb87d655a3044d30c 100644 (file)
@@ -34,6 +34,7 @@ bool Ro2RwUpgrader::Upgrade() {
   if (old_pkg_.get() != nullptr) {
     if (!old_pkg_->Upgrade()) {
       LOG(ERROR) << "Upgrade operation of old RO package failed";
+      result_ = PkgUpgradeResult::FAILED;
       return false;
     }
   }
@@ -41,9 +42,11 @@ bool Ro2RwUpgrader::Upgrade() {
   if (new_pkg_.get() != nullptr) {
     if (!new_pkg_->Upgrade()) {
       LOG(ERROR) << "Upgrade operation of new RW package failed";
+      result_ = PkgUpgradeResult::FAILED;
       return false;
     }
   }
+  result_ = PkgUpgradeResult::UPDATED;
 
   return true;
 }
index 846724a3fe3ac56ce759515b82a8c2db7dd93513..282c7501fd039c39f79c59f0fd2a54b97d482d7f 100644 (file)
@@ -33,20 +33,24 @@ bool RoUpgrader::Upgrade() {
     if (result == PkgVersionCmpResult::UNKNOWN) {
       LOG(ERROR) << "Failed to compare version of package("
                  << new_pkg_->GetId() << ")";
+      result_ = PkgUpgradeResult::FAILED;
       return false;
     }
 
     if (result == PkgVersionCmpResult::SAME) {
       LOG(INFO) << new_pkg_->GetId() << " has same version("
                 << new_pkg_->GetVersion() << "). Nothing to do.";
+      result_ = PkgUpgradeResult::SKIPPED;
       return true;
     }
   }
 
   if (!new_pkg_->Upgrade()) {
     LOG(ERROR) << "Upgrade operation failed";
+    result_ = PkgUpgradeResult::FAILED;
     return false;
   }
+  result_ = PkgUpgradeResult::UPDATED;
 
   return true;
 }
index 3cd99110675df1286daac9a555e15a5785ce4964..add08ef56ceca64021a6513256d8659903014908 100644 (file)
@@ -34,24 +34,29 @@ bool Rw2RoUpgrader::Upgrade() {
   if (result == PkgVersionCmpResult::UNKNOWN) {
     LOG(ERROR) << "Failed to compare version of package(" << new_pkg_->GetId()
                << ")";
+    result_ = PkgUpgradeResult::FAILED;
     return false;
   }
 
   if (result == PkgVersionCmpResult::LOWER) {
     LOG(INFO) << old_pkg_->GetId() << " has higher version("
               << old_pkg_->GetVersion() << ") already. Nothing to do.";
+    result_ = PkgUpgradeResult::SKIPPED;
     return true;
   }
 
   if (!old_pkg_->Upgrade()) {
     LOG(ERROR) << "Upgrade operation of old RW package failed";
+    result_ = PkgUpgradeResult::FAILED;
     return false;
   }
 
   if (!new_pkg_->Upgrade()) {
     LOG(ERROR) << "Upgrade operation of new RO package failed";
+    result_ = PkgUpgradeResult::FAILED;
     return false;
   }
+  result_ = PkgUpgradeResult::UPDATED;
 
   return true;
 }
index e8b3192865383cadcc4f7194e782b6b732b9e906..479d5e49c1e236677335bf90f099d1ffb0dd508d 100644 (file)
@@ -39,26 +39,32 @@ bool RwUpgrader::Upgrade() {
     if (result == PkgVersionCmpResult::UNKNOWN) {
       LOG(ERROR) << "Failed to compare version of package("
                  << new_pkg_->GetId() << ")";
+      result_ = PkgUpgradeResult::FAILED;
       return false;
     }
 
     if (result == PkgVersionCmpResult::SAME) {
       LOG(INFO) << new_pkg_->GetId() << " has same version("
                 << new_pkg_->GetVersion() << "). Nothing to do.";
+      result_ = PkgUpgradeResult::SKIPPED;
       return true;
     }
   }
 
   if (new_pkg_->GetOperation() != PkgOperation::UNINSTALL &&
       new_pkg_->GetOperation() != PkgOperation::UNINSTALL_KEEP_RW_DATA) {
-    if (!UnzipPkgFromZip(new_pkg_->GetId()))
+    if (!UnzipPkgFromZip(new_pkg_->GetId())) {
+      result_ = PkgUpgradeResult::FAILED;
       return false;
+    }
   }
 
   if (!new_pkg_->Upgrade()) {
     LOG(ERROR) << "Upgrade operation failed";
+    result_ = PkgUpgradeResult::FAILED;
     return false;
   }
+  result_ = PkgUpgradeResult::UPDATED;
 
   return true;
 }
index 60285d3650b2d45ad534caaa660559eb954146c7..a8ff9ff7ad92924a668fba9fff137ea87212e873 100644 (file)
@@ -24,8 +24,14 @@ SimpleUpgrader::SimpleUpgrader(const PkgContext& context,
 }
 
 bool SimpleUpgrader::Upgrade() {
-  if (GetBackendInvoker().Run() == 0)
+  if (GetBackendInvoker().Run() == 0) {
+    if (GetOperation() == PkgOperation::UNINSTALL)
+      result_ = PkgUpgradeResult::UNINSTALLED;
+    else
+      result_ = PkgUpgradeResult::INSTALLED;
     return true;
+  }
+  result_ = PkgUpgradeResult::FAILED;
   return false;
 }
 
index fa4d3783b66f149f6fb538697e02c05d44fd7ad0..6c4a60eaf5b25c809fdcc9d905ee937c4cb9fd4a 100644 (file)
@@ -135,12 +135,19 @@ bool Upgrader::Process(PkgFinder* finder) {
     RemoveBackupDbs();
     return false;
   }
+  signal_.SetTotalPkgs(list.size());
+  LOG(DEBUG) << "Found " << list.size() << " packages to upgrade";
 
   auto priority_list = factory.MakePriorityList(finder, list);
+  // Send signal after initializing
+  if (!signal_.SendInitialized())
+    LOG(ERROR) << "Failed to send initialized signal";
 
   ProcessList(priority_list);
-  // send signal at this time?
   ProcessList(list);
+
+  if (!signal_.SendFinished())
+    LOG(ERROR) << "Failed to send finished signal";
   RemoveBackupDbs();
 
   logger_->WriteLog(::utils::LogLevel::LOG_INFO, "", "Upgrade Done");
@@ -150,7 +157,10 @@ bool Upgrader::Process(PkgFinder* finder) {
 
 void Upgrader::ProcessList(list<unique_ptr<PkgUpgrader>>& list) {
   for (auto& pkg : list) {
-    if (pkg->Upgrade()) {
+    bool result = pkg->Upgrade();
+    if (!signal_.SendProgress())
+      LOG(ERROR) << "Failed to send progress signal";
+    if (result) {
       LOG(DEBUG) << "upgrade success (" << pkg->GetId() << ")";
       success_list_.push_back(move(pkg));
     } else {