Remove unused variable
[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 <glib.h>
29
30 #include "pkgmgrinfo_basic.h"
31 #include "pkgmgrinfo_private.h"
32 #include "pkgmgrinfo_debug.h"
33 #include "pkgmgr-info.h"
34 #include "manager/pkginfo_manager.h"
35
36 static void __free_update_info(gpointer data)
37 {
38         updateinfo_x *update_info = (updateinfo_x *)data;
39         if (update_info == NULL)
40                 return;
41
42         if (update_info->pkgid)
43                 free((void *)update_info->pkgid);
44         if (update_info->version)
45                 free((void *)update_info->version);
46         free((void *)update_info);
47
48 }
49
50 API int pkgmgrinfo_updateinfo_create(
51                 pkgmgrinfo_updateinfo_h *updateinfo_handle)
52 {
53         updateinfo_x *update_info;
54
55         retvm_if(updateinfo_handle == NULL, PMINFO_R_EINVAL,
56                         "Update handle output parameter is NULL\n");
57
58         update_info = (updateinfo_x *)calloc(
59                         1, sizeof(updateinfo_x));
60         if (update_info == NULL) {
61                 _LOGE("Out of memory");
62                 return PMINFO_R_ERROR;
63         }
64
65         *updateinfo_handle = update_info;
66
67         return PMINFO_R_OK;
68 }
69
70 API int pkgmgrinfo_updateinfo_destroy(
71                 pkgmgrinfo_updateinfo_h updateinfo_handle)
72 {
73         retvm_if(updateinfo_handle == NULL, PMINFO_R_EINVAL,
74                         "Update info handle parameter is NULL\n");
75
76         __free_update_info(updateinfo_handle);
77         return PMINFO_R_OK;
78 }
79
80 API int pkgmgrinfo_updateinfo_set_pkgid(
81                 pkgmgrinfo_updateinfo_h updateinfo_handle, const char *pkgid)
82 {
83         updateinfo_x *updateinfo;
84
85         retvm_if(updateinfo_handle == NULL, PMINFO_R_EINVAL,
86                         "Update info handle parameter is NULL\n");
87         retvm_if(pkgid == NULL, PMINFO_R_EINVAL,
88                                 "pkgid parameter is NULL\n");
89
90         updateinfo = (updateinfo_x *)updateinfo_handle;
91         if (updateinfo->pkgid)
92                 free(updateinfo->pkgid);
93
94         updateinfo->pkgid = strdup(pkgid);
95         if (updateinfo->pkgid == NULL) {
96                 _LOGE("Out of memory");
97                 return PMINFO_R_ERROR;
98         }
99
100         return PMINFO_R_OK;
101 }
102
103 API int pkgmgrinfo_updateinfo_set_version(
104                 pkgmgrinfo_updateinfo_h updateinfo_handle, const char *version)
105 {
106         updateinfo_x *updateinfo;
107
108         retvm_if(updateinfo_handle == NULL, PMINFO_R_EINVAL,
109                         "Update info handle parameter is NULL\n");
110         retvm_if(version == NULL, PMINFO_R_EINVAL,
111                                 "pkgid parameter is NULL\n");
112
113         updateinfo = (updateinfo_x *)updateinfo_handle;
114         if (updateinfo->version)
115                 free(updateinfo->version);
116
117         updateinfo->version = strdup(version);
118         if (updateinfo->version == NULL) {
119                 _LOGE("Out of memory");
120                 return PMINFO_R_ERROR;
121         }
122
123         return PMINFO_R_OK;
124 }
125
126 API int pkgmgrinfo_updateinfo_set_type(
127                 pkgmgrinfo_updateinfo_h updateinfo_handle,
128                 pkgmgrinfo_updateinfo_update_type type)
129 {
130         updateinfo_x *updateinfo;
131
132         retvm_if(updateinfo_handle == NULL, PMINFO_R_EINVAL,
133                         "Update info handle parameter is NULL\n");
134
135         updateinfo = (updateinfo_x *)updateinfo_handle;
136         updateinfo->type = type;
137         return PMINFO_R_OK;
138 }
139
140 API int pkgmgrinfo_updateinfo_get_pkgid(
141                 pkgmgrinfo_updateinfo_h updateinfo_handle, char **pkgid)
142 {
143         updateinfo_x *update_info;
144
145         retvm_if(updateinfo_handle == NULL, PMINFO_R_EINVAL,
146                         "Update info node parameter is NULL\n");
147
148         update_info = (updateinfo_x *)updateinfo_handle;
149         if (update_info->pkgid == NULL)
150                 return PMINFO_R_ERROR;
151
152         *pkgid = update_info->pkgid;
153         return PMINFO_R_OK;
154 }
155
156 API int pkgmgrinfo_updateinfo_get_version(
157                 pkgmgrinfo_updateinfo_h updateinfo_handle, char **version)
158 {
159         updateinfo_x *update_info;
160
161         retvm_if(updateinfo_handle == NULL, PMINFO_R_EINVAL,
162                         "Update info node parameter is NULL\n");
163
164         update_info = (updateinfo_x *)updateinfo_handle;
165         if (update_info->version == NULL)
166                 return PMINFO_R_ERROR;
167
168         *version = update_info->version;
169         return PMINFO_R_OK;
170 }
171
172 API int pkgmgrinfo_updateinfo_get_update_type(
173                 pkgmgrinfo_updateinfo_h updateinfo_handle,
174                 pkgmgrinfo_updateinfo_update_type *type)
175 {
176         updateinfo_x *update_info;
177
178         retvm_if(updateinfo_handle == NULL, PMINFO_R_EINVAL,
179                         "Update info node parameter is NULL\n");
180
181         update_info = (updateinfo_x *)updateinfo_handle;
182         *type = update_info->type;
183
184         return PMINFO_R_OK;
185 }
186
187 API int pkgmgrinfo_updateinfo_get_usr_updateinfo(const char *pkgid,
188                 pkgmgrinfo_updateinfo_h *update_handle, uid_t uid)
189 {
190         int ret;
191         pkgmgrinfo_pkginfo_h pkginfo = NULL;
192         bool is_global_pkg;
193         GSList *info_list = NULL;
194
195         if (update_handle == NULL || pkgid == NULL)
196                 return PMINFO_R_EINVAL;
197
198         ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &pkginfo);
199         if (ret != PMINFO_R_OK)
200                 return ret;
201
202         ret = pkgmgrinfo_pkginfo_is_global(pkginfo, &is_global_pkg);
203         if (ret != PMINFO_R_OK) {
204                 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
205                 return ret;
206         }
207         pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
208
209         ret = _get_pkg_updateinfo(pkgid, &info_list,
210                         (is_global_pkg) ? GLOBAL_USER : uid);
211
212         if (ret != PMINFO_R_OK) {
213                 g_slist_free_full(info_list, __free_update_info);
214                 return PMINFO_R_ERROR;
215         }
216         *update_handle = (pkgmgrinfo_updateinfo_h)info_list->data;
217         g_slist_free(info_list);
218
219         return ret;
220 }
221
222 API int pkgmgrinfo_updateinfo_get_updateinfo(const char *pkgid,
223                 pkgmgrinfo_updateinfo_h *update_handle)
224 {
225         return pkgmgrinfo_updateinfo_get_usr_updateinfo(pkgid, update_handle, _getuid());
226 }
227
228 API int pkgmgrinfo_updateinfo_usr_foreach_updateinfo(uid_t uid,
229                 pkgmgrinfo_foreach_updateinfo_cb callback, void *user_data)
230 {
231         int ret;
232         GSList *info_list = NULL;
233         GSList *tmp_list;
234
235         if (callback == NULL)
236                 return PMINFO_R_EINVAL;
237
238         ret = _get_pkg_updateinfo(NULL, &info_list, uid);
239         if (ret != 0) {
240                 _LOGE("Failed to get pkg update info for user[%d]", (int)uid);
241                 return PMINFO_R_ERROR;
242         }
243
244         for (tmp_list = info_list; tmp_list; tmp_list = g_slist_next(tmp_list)) {
245                 ret = callback((pkgmgrinfo_updateinfo_h)tmp_list->data, user_data);
246                 if (ret < 0)
247                         break;
248         }
249
250         g_slist_free_full(info_list, __free_update_info);
251         return PMINFO_R_OK;
252 }
253
254 API int pkgmgrinfo_updateinfo_foreach_updateinfo(
255                 pkgmgrinfo_foreach_updateinfo_cb callback, void *user_data)
256 {
257         return pkgmgrinfo_updateinfo_usr_foreach_updateinfo(_getuid(),
258                         callback, user_data);
259 }