#include "lib/tpk_archive_info.h"
-#include <package-manager-plugin.h>
-#include <pkgmgr-info.h>
#include <vconf.h>
#include <boost/filesystem/path.hpp>
#include <common/utils/file_util.h>
-#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
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()));
// 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()));
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;
}
}
// 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;
}
}
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;
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;
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);
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;