Fix TpkArchiveInfo 97/226597/6
authorSangyoon Jang <jeremy.jang@samsung.com>
Thu, 27 Feb 2020 03:30:33 +0000 (12:30 +0900)
committerIlho Kim <ilho159.kim@samsung.com>
Tue, 10 Mar 2020 05:14:11 +0000 (14:14 +0900)
There is a new base class for TpkArchiveInfo.

Requires:
 - https://review.tizen.org/gerrit/c/platform/core/appfw/app-installers/+/226596

Change-Id: Icba15e406426f6e53f5be17e11e12269c54f8bc8
Signed-off-by: Sangyoon Jang <jeremy.jang@samsung.com>
src/lib/tpk_archive_info.cc
src/lib/tpk_archive_info.h
src/lib/tpk_pkgmgr.cc

index a181a5185c8369362d2c88712c4938bf47e826b3..eae8c9a072e0eafd7281152e9f56a550a914246e 100644 (file)
@@ -4,8 +4,6 @@
 
 #include "lib/tpk_archive_info.h"
 
-#include <package-manager-plugin.h>
-#include <pkgmgr-info.h>
 #include <vconf.h>
 
 #include <boost/filesystem/path.hpp>
@@ -24,7 +22,6 @@
 
 #include <common/utils/file_util.h>
 
-#include <cstdio>
 #include <cstdlib>
 #include <cstring>
 #include <fstream>
@@ -40,119 +37,75 @@ const char kVconfLanguageKey[] = VCONFKEY_LANGSET;
 const char kManifestFileName[] = "tizen-manifest.xml";
 const char kSharedResDir[] = "shared/res";
 
-bool ExtractPackageArchive(const char* archive_path, const char* file,
-    const bf::path& tmp_dir) {
-  if (!ci::ExtractToTmpDir(archive_path, tmp_dir, file)) {
+bool ExtractPackageArchive(const std::string& archive_path,
+    const std::string& file, const bf::path& tmp_dir) {
+  if (!ci::ExtractToTmpDir(archive_path.c_str(), tmp_dir, file.c_str())) {
     LOG(ERROR) << "Failed to extract";
     return false;
   }
   return true;
 }
 
-bool GetPackageInfo(const tpk::parse::TPKConfigParser& parser,
-    package_manager_pkg_detail_info_t* info) {
+}  // namespace
+
+bool TpkArchiveInfo::GetPackageInfo(const tpk::parse::TPKConfigParser& parser) {
   auto pkg_info =
       std::static_pointer_cast<const tpk::parse::PackageInfo>(
           parser.GetManifestData(tpk::parse::PackageInfo::key()));
   if (!pkg_info)
     return false;
 
-  snprintf(info->pkg_type, sizeof(info->pkg_type), "tpk");
-  snprintf(info->pkg_name, sizeof(info->pkg_name), "%s",
-      pkg_info->package().c_str());
-  snprintf(info->pkgid, sizeof(info->pkgid), "%s", pkg_info->package().c_str());
-  snprintf(info->version, sizeof(info->version), "%s",
-      pkg_info->version().c_str());
-  snprintf(info->api_version, sizeof(info->api_version), "%s",
-      pkg_info->api_version().c_str());
+  type_ = "tpk";
+  name_ = pkg_info->package();
+  pkgid_ = pkg_info->package();
+  version_ = pkg_info->version();
+  api_version_ = pkg_info->api_version();
 
   return true;
 }
 
-bool GetAuthorInfo(const tpk::parse::TPKConfigParser& parser,
-    package_manager_pkg_detail_info_t* info) {
+bool TpkArchiveInfo::GetAuthorInfo(const tpk::parse::TPKConfigParser& parser) {
   auto author_info =
       std::static_pointer_cast<const tpk::parse::AuthorInfo>(
           parser.GetManifestData(tpk::parse::AuthorInfo::Key()));
   if (!author_info)
     return false;
 
-  snprintf(info->author, sizeof(info->author), "%s",
-      author_info->name().c_str());
+  author_ = author_info->name();
 
   return true;
 }
 
-bool GetPrivilegesInfo(const tpk::parse::TPKConfigParser& parser,
-    package_manager_pkg_detail_info_t* info) {
+bool TpkArchiveInfo::GetPrivilegesInfo(
+    const tpk::parse::TPKConfigParser& parser) {
   auto privileges_info =
       std::static_pointer_cast<const tpk::parse::PrivilegesInfo>(
           parser.GetManifestData(tpk::parse::PrivilegesInfo::key()));
   if (!privileges_info)
     return false;
 
-  const auto& privileges = privileges_info->GetPrivileges();
-  for (auto& priv : privileges) {
-    info->privilege_list = g_list_append(info->privilege_list,
-        strdup(priv.first.c_str()));
-  }
+  for (const auto& priv : privileges_info->GetPrivileges())
+    privileges_.emplace_back(priv.first, priv.second);
 
   return true;
 }
 
-bool GetDependencyInfo(const tpk::parse::TPKConfigParser& parser,
-    package_manager_pkg_detail_info_t* info) {
+bool TpkArchiveInfo::GetDependencyInfo(
+    const tpk::parse::TPKConfigParser& parser) {
   auto dependencies_info =
       std::static_pointer_cast<const tpk::parse::DependenciesInfo>(
           parser.GetManifestData(tpk::application_keys::kDependenciesKey));
   if (!dependencies_info)
     return true;
 
-  for (const auto& dependency : dependencies_info->dependencies()) {
-    pkg_dependency_info_t* dep =
-        static_cast<pkg_dependency_info_t*>
-        (calloc(1, sizeof(pkg_dependency_info_t)));
-    if (!dep) {
-      LOG(ERROR) << "Out of memory";
-      return false;
-    }
-    snprintf(dep->pkgid, sizeof(dep->pkgid), "%s", dependency.pkgid().c_str());
-    snprintf(dep->type, sizeof(dep->type), "%s", dependency.type().c_str());
-    if (!dependency.type().empty())
-      snprintf(dep->required_version,
-          sizeof(dep->required_version), "%s",
-          dependency.required_version().c_str());
-    info->dependency_list = g_list_append(info->dependency_list, dep);
-  }
-  return true;
-}
+  for (const auto& dep : dependencies_info->dependencies())
+    dependencies_.emplace_back(dep.pkgid(), dep.type(), dep.required_version());
 
-template <typename T>
-bool GetAppLabel(const tpk::parse::TPKConfigParser& parser,
-    const std::string& key, const char* locale,
-    package_manager_pkg_detail_info_t* info) {
-  auto apps = std::static_pointer_cast<const T>(parser.GetManifestData(key));
-  if (!apps)
-    return false;
-  for (auto& label : apps->items[0].label) {
-    if (label.xml_lang().empty())
-      continue;
-    if (!strcmp(label.xml_lang().c_str(), locale)) {
-      snprintf(info->label, sizeof(info->label), "%s", label.text().c_str());
-      return true;
-    }
-  }
-  for (auto& label : apps->items[0].label) {
-    if (label.xml_lang().empty()) {
-      snprintf(info->label, sizeof(info->label), "%s", label.text().c_str());
-      return true;
-    }
-  }
-  return false;
+  return true;
 }
 
-bool GetLabelInfo(const tpk::parse::TPKConfigParser& parser, const char* locale,
-    package_manager_pkg_detail_info_t* info) {
+bool TpkArchiveInfo::GetLabelInfo(const tpk::parse::TPKConfigParser& parser,
+    const char* locale) {
   auto pkg_info =
       std::static_pointer_cast<const tpk::parse::PackageInfo>(
           parser.GetManifestData(tpk::parse::PackageInfo::key()));
@@ -170,37 +123,37 @@ bool GetLabelInfo(const tpk::parse::TPKConfigParser& parser, const char* locale,
     //   using LangTextPair = pair<lang, text>;
     // Maybe this can be refactored.
     if (!strcmp(label.first.c_str(), locale)) {
-      snprintf(info->label, sizeof(info->label), "%s", label.second.c_str());
+      label_ = label.second;
       return true;
     }
   }
   // search again if cannot find label with current locale
   for (auto& label : pkg_info->labels()) {
     if (label.first.empty()) {
-      snprintf(info->label, sizeof(info->label), "%s", label.second.c_str());
+      label_ = label.second;
       return true;
     }
   }
 
   // get label from app
   if (GetAppLabel<tpk::parse::UIApplicationInfoList>(parser,
-      tpk::application_keys::kUIApplicationKey, locale, info))
+      tpk::application_keys::kUIApplicationKey, locale))
     return true;
   if (GetAppLabel<tpk::parse::ServiceApplicationInfoList>(parser,
-      tpk::application_keys::kServiceApplicationKey, locale, info))
+      tpk::application_keys::kServiceApplicationKey, locale))
     return true;
   if (GetAppLabel<tpk::parse::WidgetApplicationInfoList>(parser,
-      tpk::application_keys::kWidgetApplicationKey, locale, info))
+      tpk::application_keys::kWidgetApplicationKey, locale))
     return true;
   if (GetAppLabel<tpk::parse::WatchApplicationInfoList>(parser,
-      tpk::application_keys::kWatchApplicationKey, locale, info))
+      tpk::application_keys::kWatchApplicationKey, locale))
     return true;
 
   return false;
 }
 
-bool GetDescriptionInfo(const tpk::parse::TPKConfigParser& parser,
-    const char* locale, package_manager_pkg_detail_info_t* info) {
+bool TpkArchiveInfo::GetDescriptionInfo(
+    const tpk::parse::TPKConfigParser& parser, const char* locale) {
   auto desc_info =
       std::static_pointer_cast<const tpk::parse::DescriptionInfoList>(
           parser.GetManifestData(tpk::parse::DescriptionInfoList::Key()));
@@ -213,8 +166,7 @@ bool GetDescriptionInfo(const tpk::parse::TPKConfigParser& parser,
       if (desc.xml_lang().empty())
         continue;
       if (!strcmp(desc.xml_lang().c_str(), locale)) {
-        snprintf(info->pkg_description, sizeof(info->pkg_description), "%s",
-            desc.description().c_str());
+        description_ = desc.description();
         return true;
       }
     }
@@ -222,8 +174,7 @@ bool GetDescriptionInfo(const tpk::parse::TPKConfigParser& parser,
   // search again if cannot find desc with current locale
   for (auto& desc : desc_info->descriptions) {
     if (desc.xml_lang().empty()) {
-      snprintf(info->pkg_description, sizeof(info->pkg_description), "%s",
-          desc.description().c_str());
+      description_ = desc.description();
       return true;
     }
   }
@@ -231,8 +182,25 @@ bool GetDescriptionInfo(const tpk::parse::TPKConfigParser& parser,
   return false;
 }
 
-bool ReadIcon(const bf::path& icon, const bf::path& tmp_dir,
-    package_manager_pkg_detail_info_t* info) {
+bool TpkArchiveInfo::GetIconInfo(const tpk::parse::TPKConfigParser& parser,
+    const char* locale) {
+  // get icon from ui application
+  if (GetAppIcon<tpk::parse::UIApplicationInfoList>(parser,
+      tpk::application_keys::kUIApplicationKey, locale))
+    return true;
+  if (GetAppIcon<tpk::parse::ServiceApplicationInfoList>(parser,
+      tpk::application_keys::kServiceApplicationKey, locale))
+    return true;
+  if (GetAppIcon<tpk::parse::WidgetApplicationInfoList>(parser,
+      tpk::application_keys::kWidgetApplicationKey, locale))
+    return true;
+  if (GetAppIcon<tpk::parse::WatchApplicationInfoList>(parser,
+      tpk::application_keys::kWatchApplicationKey, locale))
+    return true;
+  return false;
+}
+
+bool TpkArchiveInfo::ReadIcon(const bf::path& icon, const bf::path& tmp_dir) {
   bf::path icon_path = tmp_dir / icon;
 
   LOG(INFO) << "Icon file path: " << icon_path;
@@ -245,78 +213,32 @@ bool ReadIcon(const bf::path& icon, const bf::path& tmp_dir,
   std::ifstream ifs(icon_path.c_str(),
       std::ifstream::in | std::ifstream::binary);
   ifs.seekg(0, ifs.end);
-  int len = ifs.tellg();
+  std::streamoff len = ifs.tellg();
   ifs.seekg(0, ifs.beg);
 
   if (len <= 0)
     return false;
 
-  char* buf = static_cast<char*>(malloc(sizeof(char) * len));
-  if (!buf) {
-    LOG(ERROR) << "Out of memory";
+  icon_buf_.resize(len / sizeof(unsigned char));
+  ifs.read(reinterpret_cast<char*>(icon_buf_.data()), len);
+
+  if (len != icon_buf_.size()) {
+    LOG(ERROR) << "Reading icon failed, icon size is: " << len
+               << ", but read size is: " << icon_buf_.size();
     return false;
   }
 
-  LOG(INFO) << "Reading icon file, " << len << " bytes";
-  ifs.read(buf, len);
-
-  info->icon_buf = buf;
-  info->icon_size = len;
+  LOG(INFO) << "Reading icon file, " << icon_buf_.size() << " bytes";
 
   return true;
 }
 
-template <typename T>
-std::string GetAppIcon(const tpk::parse::TPKConfigParser& parser,
-    const std::string& key, const char* locale) {
-  auto apps = std::static_pointer_cast<const T>(parser.GetManifestData(key));
-  if (!apps)
-    return {};
-  for (auto& icon : apps->items[0].app_icons.icons()) {
-    if (icon.lang().empty())
-      continue;
-    if (!strcmp(icon.lang().c_str(), locale))
-      return std::string(icon.path());
-  }
-  for (auto& icon : apps->items[0].app_icons.icons()) {
-    if (icon.lang().empty())
-      return std::string(icon.path());
-  }
-  return {};
-}
-
-std::string GetIconInfo(const tpk::parse::TPKConfigParser& parser,
-    const char* locale) {
-  // get icon from ui application
-  std::string icon;
-  icon = GetAppIcon<tpk::parse::UIApplicationInfoList>(parser,
-      tpk::application_keys::kUIApplicationKey, locale);
-  if (!icon.empty())
-    return icon;
-  icon = GetAppIcon<tpk::parse::ServiceApplicationInfoList>(parser,
-      tpk::application_keys::kServiceApplicationKey, locale);
-  if (!icon.empty())
-    return icon;
-  icon = GetAppIcon<tpk::parse::WidgetApplicationInfoList>(parser,
-      tpk::application_keys::kWidgetApplicationKey, locale);
-  if (!icon.empty())
-    return icon;
-  icon = GetAppIcon<tpk::parse::WatchApplicationInfoList>(parser,
-      tpk::application_keys::kWatchApplicationKey, locale);
-  if (!icon.empty())
-    return icon;
-  return {};
-}
-
-}  // namespace
-
-bool TpkArchiveInfo::GetArchiveInfo(const char* file_path,
-    package_manager_pkg_detail_info_t* info) {
+bool TpkArchiveInfo::LoadArchiveInfo() {
   bf::path tmp_dir = ci::GenerateTmpDir("/tmp");
   if (!ci::CreateDir(tmp_dir))
     return false;
   LOG(DEBUG) << "Unpack at temporary dir: " << tmp_dir;
-  if (!ExtractPackageArchive(file_path, kManifestFileName, tmp_dir))
+  if (!ExtractPackageArchive(path_, kManifestFileName, tmp_dir))
     return false;
 
   tpk::parse::TPKConfigParser parser;
@@ -326,16 +248,14 @@ bool TpkArchiveInfo::GetArchiveInfo(const char* file_path,
     return false;
   }
 
-  if (!GetPackageInfo(parser, info)) {
-    LOG(ERROR) << "Failed to get package info";
-    ci::RemoveAll(tmp_dir);
+  if (!GetPackageInfo(parser))
     return false;
-  }
-  if (!GetAuthorInfo(parser, info))
+
+  if (!GetAuthorInfo(parser))
     LOG(WARNING) << "Failed to get author info";
-  if (!GetPrivilegesInfo(parser, info))
+  if (!GetPrivilegesInfo(parser))
     LOG(WARNING) << "Failed to get privilege info";
-  if (!GetDependencyInfo(parser, info))
+  if (!GetDependencyInfo(parser))
     LOG(WARNING) << "Failed to get dependency info";
 
   char* locale = vconf_get_str(kVconfLanguageKey);
@@ -346,21 +266,20 @@ bool TpkArchiveInfo::GetArchiveInfo(const char* file_path,
     return false;
   }
 
-  LOG(INFO) << "Current locale: " << locale;
-  if (!GetLabelInfo(parser, locale, info))
+  if (!GetLabelInfo(parser, locale))
     LOG(WARNING) << "Failed to get label info";
-  if (!GetDescriptionInfo(parser, locale, info))
+  if (!GetDescriptionInfo(parser, locale))
     LOG(WARNING) << "Failed to get description info";
-  std::string icon = GetIconInfo(parser, locale);
+  if (!GetIconInfo(parser, locale))
+    LOG(WARNING) << "Failed to get icon path";
   free(locale);
-  if (!icon.empty()) {
-    bf::path icon_path = bf::path(kSharedResDir) / icon;
-    if (!ExtractPackageArchive(
-        file_path, icon_path.string().c_str(), tmp_dir)) {
+  if (!icon_.empty()) {
+    bf::path icon_path = bf::path(kSharedResDir) / icon_;
+    if (!ExtractPackageArchive(path_, icon_path.string(), tmp_dir)) {
       ci::RemoveAll(tmp_dir);
       return false;
     }
-    if (!ReadIcon(icon_path, tmp_dir, info)) {
+    if (!ReadIcon(icon_path, tmp_dir)) {
       LOG(WARNING) << "Failed to get icon info";
       ci::RemoveAll(tmp_dir);
       return false;
index 160d33194bd70c6cc8f33006cd41b8a2852f1e81..693da3998c7dab9baec189742867041e37dc7f43 100644 (file)
@@ -5,17 +5,81 @@
 #ifndef LIB_TPK_ARCHIVE_INFO_H_
 #define LIB_TPK_ARCHIVE_INFO_H_
 
-#include <package-manager-plugin.h>
+#include <boost/filesystem/path.hpp>
 
+#include <common/archive_info.h>
+#include <tpk_manifest_handlers/tpk_config_parser.h>
 #include <manifest_parser/utils/logging.h>
 
-#include <common/utils/singleton.h>
+#include <string>
 
-class TpkArchiveInfo : public common_installer::Singleton<TpkArchiveInfo> {
-  CRTP_DECLARE_DEFAULT_CONSTRUCTOR_CLASS(TpkArchiveInfo)
+namespace bf = boost::filesystem;
+
+class TpkArchiveInfo : public common_installer::ArchiveInfo {
  public:
-  bool GetArchiveInfo(const char* path,
-    package_manager_pkg_detail_info_t* info);
+  explicit TpkArchiveInfo(std::string path)
+      : common_installer::ArchiveInfo(path) { }
+  bool LoadArchiveInfo() override;
+
+ private:
+  bool GetPackageInfo(const tpk::parse::TPKConfigParser& parser);
+  bool GetAuthorInfo(const tpk::parse::TPKConfigParser& parser);
+  bool GetPrivilegesInfo(const tpk::parse::TPKConfigParser& parser);
+  bool GetDependencyInfo(const tpk::parse::TPKConfigParser& parser);
+
+  template <typename T>
+  bool GetAppLabel(const tpk::parse::TPKConfigParser& parser,
+      const std::string& key, const char* locale) {
+    auto apps = std::static_pointer_cast<const T>(parser.GetManifestData(key));
+    if (!apps)
+      return false;
+    for (auto& label : apps->items[0].label) {
+      if (label.xml_lang().empty())
+        continue;
+      if (!strcmp(label.xml_lang().c_str(), locale)) {
+        label_ = label.text();
+        return true;
+      }
+    }
+    for (auto& label : apps->items[0].label) {
+      if (label.xml_lang().empty()) {
+        label_ = label.text();
+        return true;
+      }
+    }
+    return false;
+  }
+
+  template <typename T>
+  bool GetAppIcon(const tpk::parse::TPKConfigParser& parser,
+      const std::string& key, const char* locale) {
+    auto apps = std::static_pointer_cast<const T>(parser.GetManifestData(key));
+    if (!apps)
+      return false;
+    for (auto& icon : apps->items[0].app_icons.icons()) {
+      if (icon.lang().empty())
+        continue;
+      if (!strcmp(icon.lang().c_str(), locale)) {
+        icon_ = icon.path();
+        return true;
+      }
+    }
+    for (auto& icon : apps->items[0].app_icons.icons()) {
+      if (icon.lang().empty()) {
+        icon_ = icon.path();
+        return true;
+      }
+    }
+    return false;
+  }
+
+  bool GetLabelInfo(const tpk::parse::TPKConfigParser& parser,
+      const char* locale);
+  bool GetDescriptionInfo(const tpk::parse::TPKConfigParser& parser,
+      const char* locale);
+  bool GetIconInfo(const tpk::parse::TPKConfigParser& parser,
+      const char* locale);
+  bool ReadIcon(const bf::path& icon, const bf::path& tmp_dir);
 
   SCOPE_LOG_TAG(TpkArchiveInfo)
 };
index 021956448df113a48ea82cf20f1dc135743be7ce..d57de1437e09a7fb692e3cbc1d77c7c1670712ba 100644 (file)
@@ -10,7 +10,10 @@ namespace {
 
 int GetPackageArchiveInfo(const char* path,
     package_manager_pkg_detail_info_t* info) {
-  if (!TpkArchiveInfo::Instance().GetArchiveInfo(path, info))
+  TpkArchiveInfo archive_info(path);
+  if (!archive_info.LoadArchiveInfo())
+    return -1;
+  if (!archive_info.GetPkgDetailInfo(info))
     return -1;
   return 0;
 }