Fix codes for setting certificate
[platform/core/appfw/pkgmgr-info.git] / src / pkgmgrinfo_updateinfo.c
1 /*
2  * pkgmgr-info
3  *
4  * Copyright (c) 2000 - 2017 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Junghyun Yeon(jungh.yeon@samsung.com>,
7  * Jongmyeong Ko(jongmyeong.ko@samsung.com>, Sangyoon Jang(s89.jang@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22
23 #define _GNU_SOURCE
24 #include <stdlib.h>
25 #include <stdbool.h>
26 #include <ctype.h>
27
28 #include <sqlite3.h>
29 #include <glib.h>
30
31 #include "pkgmgrinfo_basic.h"
32 #include "pkgmgrinfo_private.h"
33 #include "pkgmgrinfo_debug.h"
34 #include "pkgmgr-info.h"
35 #include "manager/pkginfo_manager.h"
36
37 static void __free_update_info(gpointer data)
38 {
39         updateinfo_x *update_info = (updateinfo_x *)data;
40         if (update_info == NULL)
41                 return;
42
43         if (update_info->pkgid)
44                 free((void *)update_info->pkgid);
45         if (update_info->version)
46                 free((void *)update_info->version);
47         free((void *)update_info);
48
49 }
50
51 API int pkgmgrinfo_updateinfo_create(
52                 pkgmgrinfo_updateinfo_h *updateinfo_handle)
53 {
54         updateinfo_x *update_info;
55
56         retvm_if(updateinfo_handle == NULL, PMINFO_R_EINVAL,
57                         "Update handle output parameter is NULL\n");
58
59         update_info = (updateinfo_x *)calloc(
60                         1, sizeof(updateinfo_x));
61         if (update_info == NULL) {
62                 _LOGE("Out of memory");
63                 return PMINFO_R_ERROR;
64         }
65
66         *updateinfo_handle = update_info;
67
68         return PMINFO_R_OK;
69 }
70
71 API int pkgmgrinfo_updateinfo_destroy(
72                 pkgmgrinfo_updateinfo_h updateinfo_handle)
73 {
74         retvm_if(updateinfo_handle == NULL, PMINFO_R_EINVAL,
75                         "Update info handle parameter is NULL\n");
76
77         __free_update_info(updateinfo_handle);
78         return PMINFO_R_OK;
79 }
80
81 API int pkgmgrinfo_updateinfo_set_pkgid(
82                 pkgmgrinfo_updateinfo_h updateinfo_handle, const char *pkgid)
83 {
84         updateinfo_x *updateinfo;
85
86         retvm_if(updateinfo_handle == NULL, PMINFO_R_EINVAL,
87                         "Update info handle parameter is NULL\n");
88         retvm_if(pkgid == NULL, PMINFO_R_EINVAL,
89                                 "pkgid parameter is NULL\n");
90
91         updateinfo = (updateinfo_x *)updateinfo_handle;
92         if (updateinfo->pkgid)
93                 free(updateinfo->pkgid);
94
95         updateinfo->pkgid = strdup(pkgid);
96         if (updateinfo->pkgid == NULL) {
97                 _LOGE("Out of memory");
98                 return PMINFO_R_ERROR;
99         }
100
101         return PMINFO_R_OK;
102 }
103
104 API int pkgmgrinfo_updateinfo_set_version(
105                 pkgmgrinfo_updateinfo_h updateinfo_handle, const char *version)
106 {
107         updateinfo_x *updateinfo;
108
109         retvm_if(updateinfo_handle == NULL, PMINFO_R_EINVAL,
110                         "Update info handle parameter is NULL\n");
111         retvm_if(version == NULL, PMINFO_R_EINVAL,
112                                 "pkgid parameter is NULL\n");
113
114         updateinfo = (updateinfo_x *)updateinfo_handle;
115         if (updateinfo->version)
116                 free(updateinfo->version);
117
118         updateinfo->version = strdup(version);
119         if (updateinfo->version == NULL) {
120                 _LOGE("Out of memory");
121                 return PMINFO_R_ERROR;
122         }
123
124         return PMINFO_R_OK;
125 }
126
127 API int pkgmgrinfo_updateinfo_set_type(
128                 pkgmgrinfo_updateinfo_h updateinfo_handle,
129                 pkgmgrinfo_updateinfo_update_type type)
130 {
131         updateinfo_x *updateinfo;
132
133         retvm_if(updateinfo_handle == NULL, PMINFO_R_EINVAL,
134                         "Update info handle parameter is NULL\n");
135
136         updateinfo = (updateinfo_x *)updateinfo_handle;
137         updateinfo->type = type;
138         return PMINFO_R_OK;
139 }
140
141 API int pkgmgrinfo_updateinfo_get_pkgid(
142                 pkgmgrinfo_updateinfo_h updateinfo_handle, char **pkgid)
143 {
144         updateinfo_x *update_info;
145
146         retvm_if(updateinfo_handle == NULL, PMINFO_R_EINVAL,
147                         "Update info node parameter is NULL\n");
148
149         update_info = (updateinfo_x *)updateinfo_handle;
150         if (update_info->pkgid == NULL)
151                 return PMINFO_R_ERROR;
152
153         *pkgid = update_info->pkgid;
154         return PMINFO_R_OK;
155 }
156
157 API int pkgmgrinfo_updateinfo_get_version(
158                 pkgmgrinfo_updateinfo_h updateinfo_handle, char **version)
159 {
160         updateinfo_x *update_info;
161
162         retvm_if(updateinfo_handle == NULL, PMINFO_R_EINVAL,
163                         "Update info node parameter is NULL\n");
164
165         update_info = (updateinfo_x *)updateinfo_handle;
166         if (update_info->version == NULL)
167                 return PMINFO_R_ERROR;
168
169         *version = update_info->version;
170         return PMINFO_R_OK;
171 }
172
173 API int pkgmgrinfo_updateinfo_get_update_type(
174                 pkgmgrinfo_updateinfo_h updateinfo_handle,
175                 pkgmgrinfo_updateinfo_update_type *type)
176 {
177         updateinfo_x *update_info;
178
179         retvm_if(updateinfo_handle == NULL, PMINFO_R_EINVAL,
180                         "Update info node parameter is NULL\n");
181
182         update_info = (updateinfo_x *)updateinfo_handle;
183         *type = update_info->type;
184
185         return PMINFO_R_OK;
186 }
187
188 API int pkgmgrinfo_updateinfo_get_usr_updateinfo(const char *pkgid,
189                 pkgmgrinfo_updateinfo_h *update_handle, uid_t uid)
190 {
191         int ret;
192         pkgmgrinfo_pkginfo_h pkginfo = NULL;
193         bool is_global_pkg;
194         GSList *info_list = NULL;
195
196         if (update_handle == NULL || pkgid == NULL)
197                 return PMINFO_R_EINVAL;
198
199         ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &pkginfo);
200         if (ret != PMINFO_R_OK)
201                 return ret;
202
203         ret = pkgmgrinfo_pkginfo_is_global(pkginfo, &is_global_pkg);
204         if (ret != PMINFO_R_OK) {
205                 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
206                 return ret;
207         }
208         pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
209
210         ret = _get_pkg_updateinfo(pkgid, &info_list,
211                         (is_global_pkg) ? GLOBAL_USER : uid);
212
213         if (ret != PMINFO_R_OK) {
214                 g_slist_free_full(info_list, __free_update_info);
215                 return PMINFO_R_ERROR;
216         }
217         *update_handle = (pkgmgrinfo_updateinfo_h)info_list->data;
218         g_slist_free(info_list);
219
220         return ret;
221 }
222
223 API int pkgmgrinfo_updateinfo_get_updateinfo(const char *pkgid,
224                 pkgmgrinfo_updateinfo_h *update_handle)
225 {
226         return pkgmgrinfo_updateinfo_get_usr_updateinfo(pkgid, update_handle, _getuid());
227 }
228
229 API int pkgmgrinfo_updateinfo_usr_foreach_updateinfo(uid_t uid,
230                 pkgmgrinfo_foreach_updateinfo_cb callback, void *user_data)
231 {
232         int ret;
233         GSList *info_list = NULL;
234         GSList *tmp_list;
235
236         if (callback == NULL)
237                 return PMINFO_R_EINVAL;
238
239         ret = _get_pkg_updateinfo(NULL, &info_list, uid);
240         if (ret != 0) {
241                 _LOGE("Failed to get pkg update info for user[%d]", (int)uid);
242                 g_slist_free_full(info_list, __free_update_info);
243                 return PMINFO_R_ERROR;
244         }
245
246         ret = _get_pkg_updateinfo(NULL, &info_list, GLOBAL_USER);
247         if (ret != 0) {
248                 _LOGE("Failed to get pkg update info for user[%d]",
249                                 (int)GLOBAL_USER);
250                 g_slist_free_full(info_list, __free_update_info);
251                 return PMINFO_R_ERROR;
252         }
253
254         for (tmp_list = info_list; tmp_list; tmp_list = g_slist_next(tmp_list)) {
255                 ret = callback((pkgmgrinfo_updateinfo_h)tmp_list->data, user_data);
256                 if (ret < 0)
257                         break;
258         }
259
260         g_slist_free_full(info_list, __free_update_info);
261         return PMINFO_R_OK;
262 }
263
264 API int pkgmgrinfo_updateinfo_foreach_updateinfo(
265                 pkgmgrinfo_foreach_updateinfo_cb callback, void *user_data)
266 {
267         return pkgmgrinfo_updateinfo_usr_foreach_updateinfo(_getuid(),
268                         callback, user_data);
269 }