Fix memory leak
[platform/core/appfw/app-installers.git] / src / common / utils / manifest_util.cc
1 // Copyright (c) 2016 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/utils/manifest_util.h"
6
7 #include <manifest_parser/utils/logging.h>
8 #include <pkgmgr/pkgmgr_parser.h>
9 #include <pkgmgr-info.h>
10 #include <tzplatform_config.h>
11
12 #include <boost/filesystem/path.hpp>
13 #include <string>
14
15 #include "common/pkgmgr_query.h"
16
17 namespace bf = boost::filesystem;
18
19 namespace {
20
21 int PkgmgrAppInfoCallback(const pkgmgrinfo_appinfo_h handle,
22                           void *user_data) {
23   manifest_x* mfx = static_cast<manifest_x*>(user_data);
24
25   char* app_id = nullptr;
26   if (pkgmgrinfo_appinfo_get_appid(handle, &app_id))
27     return PMINFO_R_ERROR;
28
29   char* icon_text = nullptr;
30   if (pkgmgrinfo_appinfo_get_icon(handle, &icon_text))
31     LOG(INFO) << "No Icon for " << app_id;
32
33   if (!mfx->root_path) {
34     char *root_path = nullptr;
35     if (pkgmgrinfo_appinfo_get_root_path(handle, &root_path))
36       return PMINFO_R_ERROR;
37     mfx->root_path = strdup(root_path);
38   }
39
40   application_x* application =
41       static_cast<application_x*>(calloc(1, sizeof(application_x)));
42   if (!application)
43     return PMINFO_R_ERROR;
44
45   if (icon_text) {
46     icon_x* icon = static_cast<icon_x*>(calloc(1, sizeof(icon_x)));
47     if (!icon) {
48       free(application);
49       return PMINFO_R_ERROR;
50     }
51     icon->text = strdup(icon_text);
52     application->icon = g_list_append(application->icon, icon);
53   }
54   application->appid = strdup(app_id);
55   mfx->application = g_list_append(mfx->application, application);
56
57   return PMINFO_R_OK;
58 }
59
60 }  // namespace
61
62 namespace common_installer {
63
64 bf::path GetManifestLocation(const std::string& pkgid,
65                              uid_t uid,
66                              bool is_readonly) {
67   if (QueryIsGlobalPackage(pkgid.c_str(), uid)) {
68     uid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
69     is_readonly = QueryIsReadonlyPackage(pkgid.c_str(), uid);
70   }
71   bf::path xml_path = bf::path(getUserManifestPath(uid, is_readonly))
72                       / bf::path(pkgid.c_str());
73   xml_path += ".xml";
74
75   return xml_path;
76 }
77
78 int PkgmgrGenerateManifestInfoFromDB(manifest_x* mfx,
79                                      const std::string &pkgid,
80                                      uid_t uid) {
81   pkgmgrinfo_appinfo_filter_h filter;
82   mfx->package = strdup(pkgid.c_str());
83
84   if (pkgmgrinfo_appinfo_filter_create(&filter))
85     return PMINFO_R_ERROR;
86
87   if (pkgmgrinfo_appinfo_filter_add_string(filter,
88                           PMINFO_APPINFO_PROP_APP_PACKAGE, pkgid.c_str())) {
89     pkgmgrinfo_appinfo_filter_destroy(filter);
90     return PMINFO_R_ERROR;
91   }
92
93   if (pkgmgrinfo_appinfo_usr_filter_foreach_appinfo(filter,
94                                                     PkgmgrAppInfoCallback,
95                                                     mfx, uid)) {
96     pkgmgrinfo_appinfo_filter_destroy(filter);
97     return PMINFO_R_ERROR;
98   }
99
100   pkgmgrinfo_appinfo_filter_destroy(filter);
101
102   return PMINFO_R_OK;
103 }
104
105 }  // namespace common_installer