Add functions for classes inherits this class
[platform/core/appfw/app-installers.git] / src / common / archive_info.cc
1 // Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by an apache 2.0 license that can be
3 // found in the LICENSE file.
4
5 #include "common/archive_info.h"
6
7 #include <glib.h>
8 #include <manifest_parser/utils/logging.h>
9 #include <package-manager-plugin.h>
10
11 #include <common/utils/file_util.h>
12
13 #include <algorithm>
14 #include <cstdio>
15 #include <cstring>
16 #include <utility>
17 #include <vector>
18
19 namespace common_installer {
20
21 bool ArchiveInfo::GetPkgDetailInfo(package_manager_pkg_detail_info_t* info) {
22   if (!info)
23     return false;
24   if (pkgid_.empty())
25     return false;
26
27   snprintf(info->pkg_type, sizeof(info->pkg_type), "%s", type_.c_str());
28   snprintf(info->pkg_name, sizeof(info->pkg_name), "%s", pkgid_.c_str());
29   snprintf(info->pkgid, sizeof(info->pkgid), "%s", pkgid_.c_str());
30   snprintf(info->version, sizeof(info->version), "%s", version_.c_str());
31   snprintf(info->api_version, sizeof(info->api_version), "%s",
32       api_version_.c_str());
33   snprintf(info->author, sizeof(info->author), "%s", author_.c_str());
34   snprintf(info->label, sizeof(info->label), "%s", label_.c_str());
35   snprintf(info->pkg_description, sizeof(info->pkg_description), "%s",
36       description_.c_str());
37
38   for (const auto& priv : privileges_) {
39     char* priv_str = strdup(priv.first.c_str());
40     if (!priv_str) {
41       LOG(ERROR) << "Out of memory";
42       g_list_free_full(info->dependency_list, free);
43       return false;
44     }
45     info->privilege_list = g_list_append(info->privilege_list, priv_str);
46   }
47
48   for (const auto& dependency : dependencies_) {
49     pkg_dependency_info_t* dep =
50         static_cast<pkg_dependency_info_t*>
51         (calloc(1, sizeof(pkg_dependency_info_t)));
52     if (!dep) {
53       LOG(ERROR) << "Out of memory";
54       g_list_free_full(info->dependency_list, free);
55       return false;
56     }
57     snprintf(dep->pkgid, sizeof(dep->pkgid), "%s",
58         std::get<0>(dependency).c_str());
59     snprintf(dep->type, sizeof(dep->type), "%s",
60         std::get<1>(dependency).c_str());
61     snprintf(dep->required_version, sizeof(dep->required_version), "%s",
62         std::get<2>(dependency).c_str());
63     info->dependency_list = g_list_append(info->dependency_list, dep);
64   }
65
66   info->icon_buf =
67       reinterpret_cast<char*>(malloc(sizeof(char) * icon_buf_.size()));
68   if (!info->icon_buf) {
69     LOG(ERROR) << "Out of memory";
70     g_list_free_full(info->dependency_list, free);
71     return false;
72   }
73
74   std::copy(icon_buf_.begin(), icon_buf_.end(), info->icon_buf);
75   info->icon_size = icon_buf_.size();
76
77   return true;
78 }
79
80 void ArchiveInfo::RemoveTmpDir(const std::string& tmp_dir) {
81   common_installer::RemoveAll(tmp_dir);
82 }
83
84 }  // namespace common_installer