Implement priority list feature in pkg_upgrader 68/298668/7
authorSangyoon Jang <jeremy.jang@samsung.com>
Tue, 12 Sep 2023 07:18:50 +0000 (16:18 +0900)
committerSangyoon Jang <jeremy.jang@samsung.com>
Wed, 15 Nov 2023 05:16:14 +0000 (14:16 +0900)
Change-Id: I4e3e922cf0c8d4a4e7472039976391ddecf99c04
Signed-off-by: Sangyoon Jang <jeremy.jang@samsung.com>
src/pkg_upgrade/include/pkg_finder.hh
src/pkg_upgrade/include/pkg_upgrader_factory.hh
src/pkg_upgrade/include/upgrader.hh
src/pkg_upgrade/src/pkg_finder.cc
src/pkg_upgrade/src/pkg_upgrader_factory.cc
src/pkg_upgrade/src/upgrader.cc

index 7da7f15..011b57d 100644 (file)
@@ -35,14 +35,17 @@ class PkgFinder {
   virtual int Find();
   const std::list<PkgContext>& GetOldPkgs() { return old_pkgs_; }
   const std::list<PkgContext>& GetNewPkgs() { return new_pkgs_; }
+  const std::list<std::string>& GetPriorityPkgs() { return priority_pkgs_; }
   void SetManifestDir(std::string dir);
   void SetPreloadRwListPath(std::string path);
+  void SetPriorityListPath(std::string path);
 
  private:
   static int PkgidListCb(const pkgmgrinfo_pkginfo_h handle, void* user_data);
   int FindPreloadPkgidFromDb(bool read_only = true);
   int FindPreloadPkgidFromXml(const std::string& xml_directory);
   int FindPreloadPkgidFromFile();
+  int FindPriorityPkgs();
   std::string GetValidManifest(std::string manifest);
   std::string FindInfoFromXml(const std::string& manifest,
       const std::string& find_info);
@@ -55,9 +58,11 @@ class PkgFinder {
  private:
   std::list<PkgContext> old_pkgs_;
   std::list<PkgContext> new_pkgs_;
+  std::list<std::string> priority_pkgs_;
   bool read_only_ = true;
   std::string manifest_dir_;
   std::string preload_rw_list_path_;
+  std::string priority_list_path_;
 };
 
 }  // common_fota
index 042e995..8cd606a 100644 (file)
@@ -30,6 +30,8 @@ namespace common_fota {
 class PkgUpgraderFactory {
  public:
   std::list<std::unique_ptr<PkgUpgrader>> MakeList(PkgFinder* finder);
+  std::list<std::unique_ptr<PkgUpgrader>> MakePriorityList(PkgFinder* finder,
+      std::list<std::unique_ptr<PkgUpgrader>>& list);
   bool IsFailed();
 
  private:
index 4d282bd..77479c5 100644 (file)
@@ -48,6 +48,7 @@ class Upgrader {
   int RemoveBackupFlag(const std::string& path);
   int IntegrityCheckBackupDbs();
   int IntegrityCheck(const std::string& path);
+  void ProcessList(std::list<std::unique_ptr<PkgUpgrader>>& list);
 
  private:
   std::shared_ptr<utils::FileLogBackend> logger_;
index fbf95de..899210b 100644 (file)
@@ -54,6 +54,7 @@ constexpr char kTokenRemoveStr[] = "removable=";
 constexpr char kDefaultVersionStr[] = "0.0.1";
 constexpr char kDefaultPkgTypeStr[] = "tpk";
 constexpr char kRpkTypeStr[] = "rpk";
+constexpr char kPriorityListFile[] = "/etc/package-manager/priority_list";
 
 }  // namespace
 
@@ -63,6 +64,7 @@ PkgFinder::PkgFinder() {
   xmlInitParser();
   manifest_dir_ = USR_MANIFEST_DIRECTORY;
   preload_rw_list_path_ = PRELOAD_RW_LIST_FILE;
+  priority_list_path_ = kPriorityListFile;
 
   if (access(kOptZipFile, F_OK) != 0) {
     LOG(ERROR) << kOptZipFile << " does not exist";
@@ -108,6 +110,11 @@ int PkgFinder::Find() {
     return -1;
   }
 
+  if (FindPriorityPkgs() != 0) {
+    LOG(ERROR) << "FindPriorityPkgs failed";
+    return -1;
+  }
+
   return 0;
 }
 
@@ -119,6 +126,10 @@ void PkgFinder::SetPreloadRwListPath(std::string path) {
   preload_rw_list_path_ = std::move(path);
 }
 
+void PkgFinder::SetPriorityListPath(std::string path) {
+  priority_list_path_ = std::move(path);
+}
+
 int PkgFinder::PkgidListCb(const pkgmgrinfo_pkginfo_h handle, void* user_data) {
   char* pkgid = nullptr;
   if (pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgid) != PMINFO_R_OK)
@@ -323,6 +334,25 @@ int PkgFinder::FindPreloadPkgidFromFile() {
   return 0;
 }
 
+int PkgFinder::FindPriorityPkgs() {
+  char buf[kBufSize] = {0};
+  FILE *fp;
+
+  fp = fopen(priority_list_path_.c_str(), "r");
+  if (fp == nullptr) {
+    LOG(INFO) << "no priority list file";
+    return 0;
+  }
+
+  while (fgets(buf, kBufSize, fp) != NULL) {
+    StrTrim(buf);
+    priority_pkgs_.emplace_back(buf);
+  }
+  fclose(fp);
+
+  return 0;
+}
+
 void PkgFinder::AddRwPkgInfoFromFile(const char* info_str) {
   if (info_str == nullptr)
     return;
index c02c694..4f0ff7f 100644 (file)
@@ -16,6 +16,7 @@
 
 #include "pkg_upgrader_factory.hh"
 
+#include <algorithm>
 #include <memory>
 
 #include "db_upgrader.hh"
@@ -70,6 +71,20 @@ list<unique_ptr<PkgUpgrader>> PkgUpgraderFactory::MakeList(PkgFinder* finder) {
   return Merge(finder->GetOldPkgs(), finder->GetNewPkgs());
 }
 
+list<unique_ptr<PkgUpgrader>> PkgUpgraderFactory::MakePriorityList(
+    PkgFinder* finder, list<unique_ptr<PkgUpgrader>>& list) {
+  const auto& pkgs = finder->GetPriorityPkgs();
+  auto it = list.begin();
+  std::list<std::unique_ptr<PkgUpgrader>> priority_list;
+  while (it != list.end()) {
+    auto tmp = std::next(it);
+    if (std::find(pkgs.begin(), pkgs.end(), (*it)->GetId()) != pkgs.end())
+      priority_list.splice(priority_list.begin(), list, it);
+    it = tmp;
+  }
+  return priority_list;
+}
+
 bool PkgUpgraderFactory::IsFailed() {
   return failed_;
 }
index b261150..fa4d378 100644 (file)
@@ -136,6 +136,19 @@ bool Upgrader::Process(PkgFinder* finder) {
     return false;
   }
 
+  auto priority_list = factory.MakePriorityList(finder, list);
+
+  ProcessList(priority_list);
+  // send signal at this time?
+  ProcessList(list);
+  RemoveBackupDbs();
+
+  logger_->WriteLog(::utils::LogLevel::LOG_INFO, "", "Upgrade Done");
+  logger_->WriteLogToFile();
+  return true;
+}
+
+void Upgrader::ProcessList(list<unique_ptr<PkgUpgrader>>& list) {
   for (auto& pkg : list) {
     if (pkg->Upgrade()) {
       LOG(DEBUG) << "upgrade success (" << pkg->GetId() << ")";
@@ -145,12 +158,6 @@ bool Upgrader::Process(PkgFinder* finder) {
       failure_list_.push_back(move(pkg));
     }
   }
-
-  RemoveBackupDbs();
-
-  logger_->WriteLog(::utils::LogLevel::LOG_INFO, "", "Upgrade Done");
-  logger_->WriteLogToFile();
-  return true;
 }
 
 const list<unique_ptr<PkgUpgrader>>& Upgrader::GetSuccessList() const {