33c5cd34b0249a555631a33066fb45fe5f73f81b
[platform/core/appfw/pkgmgr-info.git] / src / pkgmgrinfo_archiveinfo.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <stdlib.h>
19 #include <string.h>
20
21 #include "pkgmgrinfo_type.h"
22 #include "pkgmgrinfo_private.h"
23 #include "pkgmgrinfo_debug.h"
24
25 API int pkgmgrinfo_archiveinfo_get_archiveinfo(const char *path,
26                 pkgmgrinfo_archiveinfo_h *handle)
27 {
28         char *pkg_type;
29         pkg_plugin_set *plugin_set;
30         package_manager_pkg_detail_info_t *info;
31
32         if (path == NULL || handle == NULL) {
33                 _LOGE("invalid parameter");
34                 return PMINFO_R_EINVAL;
35         }
36
37         pkg_type = __get_type_from_path(path);
38         if (pkg_type == NULL) {
39                 _LOGE("cannot get pkg type");
40                 return PMINFO_R_ERROR;
41         }
42
43         plugin_set = __load_library(pkg_type);
44         if (plugin_set == NULL) {
45                 _LOGE("failed to load library for %s", pkg_type);
46                 free(pkg_type);
47                 return PMINFO_R_ERROR;
48         }
49
50         info = calloc(1, sizeof(package_manager_pkg_detail_info_t));
51         if (info == NULL) {
52                 _LOGE("out of memory");
53                 __unload_library(pkg_type);
54                 free(pkg_type);
55                 return PMINFO_R_ERROR;
56         }
57
58         if (plugin_set->get_pkg_detail_info_from_package(path, info)) {
59                 _LOGE("failed to get archive info of %s", path);
60                 free(info);
61                 __unload_library(pkg_type);
62                 free(pkg_type);
63                 return PMINFO_R_ERROR;
64         }
65
66         *handle = info;
67
68         return PMINFO_R_OK;
69 }
70
71 API int pkgmgrinfo_archiveinfo_destroy_archiveinfo(
72                 pkgmgrinfo_archiveinfo_h handle)
73 {
74         package_manager_pkg_detail_info_t *info =
75                 (package_manager_pkg_detail_info_t *)handle;
76
77         if (info == NULL) {
78                 _LOGE("invalid parameter");
79                 return PMINFO_R_EINVAL;
80         }
81
82         free(info->icon_buf);
83         free(info);
84
85         return PMINFO_R_OK;
86 }
87
88 API int pkgmgrinfo_archiveinfo_get_pkgid(pkgmgrinfo_archiveinfo_h handle,
89                 const char **pkgid)
90 {
91         package_manager_pkg_detail_info_t *info =
92                 (package_manager_pkg_detail_info_t *)handle;
93
94         if (info == NULL || strlen(info->pkgid) == 0 || pkgid == NULL) {
95                 _LOGE("invalid parameter");
96                 return PMINFO_R_EINVAL;
97         }
98
99         *pkgid = info->pkgid;
100
101         return PMINFO_R_OK;
102 }
103
104 API int pkgmgrinfo_archiveinfo_get_type(pkgmgrinfo_archiveinfo_h handle,
105                 const char **type)
106 {
107         package_manager_pkg_detail_info_t *info =
108                 (package_manager_pkg_detail_info_t *)handle;
109
110         if (info == NULL || strlen(info->pkg_type) == 0 || type == NULL ) {
111                 _LOGE("invalid parameter");
112                 return PMINFO_R_EINVAL;
113         }
114
115         *type = info->pkg_type;
116
117         return PMINFO_R_OK;
118 }
119
120 API int pkgmgrinfo_archiveinfo_get_version(pkgmgrinfo_archiveinfo_h handle,
121                 const char **version)
122 {
123         package_manager_pkg_detail_info_t *info =
124                 (package_manager_pkg_detail_info_t *)handle;
125
126         if (info == NULL || strlen(info->version) == 0 || version == NULL) {
127                 _LOGE("invalid parameter");
128                 return PMINFO_R_EINVAL;
129         }
130
131         *version = info->version;
132
133         return PMINFO_R_OK;
134 }
135
136 API int pkgmgrinfo_archiveinfo_get_api_version(pkgmgrinfo_archiveinfo_h handle,
137                 const char **api_version)
138 {
139         package_manager_pkg_detail_info_t *info =
140                 (package_manager_pkg_detail_info_t *)handle;
141
142         if (info == NULL || strlen(info->api_version) == 0 ||
143                         api_version == NULL) {
144                 _LOGE("invalid parameter");
145                 return PMINFO_R_EINVAL;
146         }
147
148         *api_version = info->api_version;
149
150         return PMINFO_R_OK;
151 }
152
153 API int pkgmgrinfo_archiveinfo_get_description(pkgmgrinfo_archiveinfo_h handle,
154                 const char **description)
155 {
156         package_manager_pkg_detail_info_t *info =
157                 (package_manager_pkg_detail_info_t *)handle;
158
159         if (info == NULL || description == NULL) {
160                 _LOGE("invalid parameter");
161                 return PMINFO_R_EINVAL;
162         }
163
164         if (strlen(info->pkg_description) == 0)
165                 return PMINFO_R_ENOENT;
166
167         *description = info->pkg_description;
168
169         return PMINFO_R_OK;
170 }
171
172 API int pkgmgrinfo_archiveinfo_get_label(pkgmgrinfo_archiveinfo_h handle,
173                 const char **label)
174 {
175         package_manager_pkg_detail_info_t *info =
176                 (package_manager_pkg_detail_info_t *)handle;
177
178         if (info == NULL || label == NULL) {
179                 _LOGE("invalid parameter");
180                 return PMINFO_R_EINVAL;
181         }
182
183         if (strlen(info->label) == 0)
184                 return PMINFO_R_ENOENT;
185
186         *label = info->label;
187
188         return PMINFO_R_OK;
189 }
190
191 API int pkgmgrinfo_archiveinfo_get_author(pkgmgrinfo_archiveinfo_h handle,
192                 const char **author)
193 {
194         package_manager_pkg_detail_info_t *info =
195                 (package_manager_pkg_detail_info_t *)handle;
196
197         if (info == NULL || author == NULL) {
198                 _LOGE("invalid parameter");
199                 return PMINFO_R_EINVAL;
200         }
201
202         if (strlen(info->author) == 0)
203                 return PMINFO_R_ENOENT;
204
205         *author = info->author;
206
207         return PMINFO_R_OK;
208 }
209
210 API int pkgmgrinfo_archiveinfo_get_icon(pkgmgrinfo_archiveinfo_h handle,
211                 const unsigned char **icon, size_t *size)
212 {
213         package_manager_pkg_detail_info_t *info =
214                 (package_manager_pkg_detail_info_t *)handle;
215
216         if (info == NULL || icon == NULL || size == NULL) {
217                 _LOGE("invalid parameter");
218                 return PMINFO_R_EINVAL;
219         }
220
221         if (info->icon_buf == NULL)
222                 return PMINFO_R_ENOENT;
223
224         *icon = (unsigned char *)info->icon_buf;
225         *size = info->icon_size;
226
227         return PMINFO_R_OK;
228 }