4 * Copyright (c) 2000 - 2017 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Junghyun Yeon(jungh.yeon@samsung.com>,
7 * Jongmyeong Ko(jongmyeong.ko@samsung.com>, Sangyoon Jang(s89.jang@samsung.com>
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
31 #include "pkgmgrinfo_basic.h"
32 #include "pkgmgrinfo_private.h"
33 #include "pkgmgrinfo_debug.h"
34 #include "pkgmgr-info.h"
36 static void __free_update_info(gpointer data)
38 updateinfo_x *update_info = (updateinfo_x *)data;
39 if (update_info == NULL)
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);
50 static int __convert_update_type(const char *type, pkgmgrinfo_updateinfo_update_type *convert_type)
52 if (strncasecmp(type, PMINFO_UPDATEINFO_TYPE_NONE,
53 strlen(PMINFO_UPDATEINFO_TYPE_NONE)) == 0)
54 *convert_type = PMINFO_UPDATEINFO_NONE;
55 else if (strncasecmp(type, PMINFO_UPDATEINFO_TYPE_FORCE,
56 strlen(PMINFO_UPDATEINFO_TYPE_FORCE)) == 0)
57 *convert_type = PMINFO_UPDATEINFO_FORCE;
58 else if (strncasecmp(type, PMINFO_UPDATEINFO_TYPE_OPTIONAL,
59 strlen(PMINFO_UPDATEINFO_TYPE_OPTIONAL)) == 0)
60 *convert_type = PMINFO_UPDATEINFO_OPTIONAL;
66 API int pkgmgrinfo_updateinfo_create(
67 pkgmgrinfo_updateinfo_h *updateinfo_handle)
69 updateinfo_x *update_info;
71 retvm_if(updateinfo_handle == NULL, PMINFO_R_EINVAL,
72 "Update handle output parameter is NULL\n");
74 update_info = (updateinfo_x *)calloc(
75 1, sizeof(updateinfo_x));
76 if (update_info == NULL) {
77 _LOGE("Out of memory");
78 return PMINFO_R_ERROR;
81 *updateinfo_handle = update_info;
86 API int pkgmgrinfo_updateinfo_destroy(
87 pkgmgrinfo_updateinfo_h updateinfo_handle)
89 retvm_if(updateinfo_handle == NULL, PMINFO_R_EINVAL,
90 "Update info handle parameter is NULL\n");
92 __free_update_info(updateinfo_handle);
96 API int pkgmgrinfo_updateinfo_set_pkgid(
97 pkgmgrinfo_updateinfo_h updateinfo_handle, const char *pkgid)
99 updateinfo_x *updateinfo;
101 retvm_if(updateinfo_handle == NULL, PMINFO_R_EINVAL,
102 "Update info handle parameter is NULL\n");
103 retvm_if(pkgid == NULL, PMINFO_R_EINVAL,
104 "pkgid parameter is NULL\n");
106 updateinfo = (updateinfo_x *)updateinfo_handle;
107 if (updateinfo->pkgid)
108 free(updateinfo->pkgid);
110 updateinfo->pkgid = strdup(pkgid);
111 if (updateinfo->pkgid == NULL) {
112 _LOGE("Out of memory");
113 return PMINFO_R_ERROR;
119 API int pkgmgrinfo_updateinfo_set_version(
120 pkgmgrinfo_updateinfo_h updateinfo_handle, const char *version)
122 updateinfo_x *updateinfo;
124 retvm_if(updateinfo_handle == NULL, PMINFO_R_EINVAL,
125 "Update info handle parameter is NULL\n");
126 retvm_if(version == NULL, PMINFO_R_EINVAL,
127 "pkgid parameter is NULL\n");
129 updateinfo = (updateinfo_x *)updateinfo_handle;
130 if (updateinfo->version)
131 free(updateinfo->version);
133 updateinfo->version = strdup(version);
134 if (updateinfo->version == NULL) {
135 _LOGE("Out of memory");
136 return PMINFO_R_ERROR;
142 API int pkgmgrinfo_updateinfo_set_type(
143 pkgmgrinfo_updateinfo_h updateinfo_handle,
144 pkgmgrinfo_updateinfo_update_type type)
146 updateinfo_x *updateinfo;
148 retvm_if(updateinfo_handle == NULL, PMINFO_R_EINVAL,
149 "Update info handle parameter is NULL\n");
151 updateinfo = (updateinfo_x *)updateinfo_handle;
152 updateinfo->type = type;
156 API int pkgmgrinfo_updateinfo_get_pkgid(
157 pkgmgrinfo_updateinfo_h updateinfo_handle, char **pkgid)
159 updateinfo_x *update_info;
161 retvm_if(updateinfo_handle == NULL, PMINFO_R_EINVAL,
162 "Update info node parameter is NULL\n");
164 update_info = (updateinfo_x *)updateinfo_handle;
165 if (update_info->pkgid == NULL)
166 return PMINFO_R_ERROR;
168 *pkgid = update_info->pkgid;
172 API int pkgmgrinfo_updateinfo_get_version(
173 pkgmgrinfo_updateinfo_h updateinfo_handle, char **version)
175 updateinfo_x *update_info;
177 retvm_if(updateinfo_handle == NULL, PMINFO_R_EINVAL,
178 "Update info node parameter is NULL\n");
180 update_info = (updateinfo_x *)updateinfo_handle;
181 if (update_info->version == NULL)
182 return PMINFO_R_ERROR;
184 *version = update_info->version;
188 API int pkgmgrinfo_updateinfo_get_update_type(
189 pkgmgrinfo_updateinfo_h updateinfo_handle,
190 pkgmgrinfo_updateinfo_update_type *type)
192 updateinfo_x *update_info;
194 retvm_if(updateinfo_handle == NULL, PMINFO_R_EINVAL,
195 "Update info node parameter is NULL\n");
197 update_info = (updateinfo_x *)updateinfo_handle;
198 *type = update_info->type;
203 static int _get_pkg_updateinfo_from_db(const char *pkgid,
204 GSList **update_info_list, uid_t uid)
208 char query[MAX_QUERY_LEN] = { '\0' };
213 updateinfo_x *update_info = NULL;
214 pkgmgrinfo_updateinfo_update_type convert_type;
216 dbpath = getUserPkgParserDBPathUID(uid);
218 return PMINFO_R_ERROR;
220 ret = __open_db(dbpath, &db, SQLITE_OPEN_READONLY);
221 if (ret != SQLITE_OK) {
222 _LOGD("failed to open db: %d", ret);
228 sqlite3_snprintf(MAX_QUERY_LEN, query,
229 "SELECT package, update_version, update_type " \
230 "FROM package_update_info");
232 sqlite3_snprintf(MAX_QUERY_LEN, query,
233 "SELECT package, update_version, update_type " \
234 "FROM package_update_info WHERE package=%Q",
237 ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
238 if (ret != SQLITE_OK) {
239 _LOGE("Don't execute query = %s error message = %s\n", query,
245 while (sqlite3_step(stmt) == SQLITE_ROW) {
246 update_info = calloc(1, sizeof(updateinfo_x));
247 if (update_info == NULL) {
248 _LOGE("Out of memory");
250 sqlite3_finalize(stmt);
251 sqlite3_close_v2(db);
255 _save_column_str(stmt, idx++, &update_info->pkgid);
256 _save_column_str(stmt, idx++, &update_info->version);
257 _save_column_str(stmt, idx, &type);
258 ret = __convert_update_type(type, &convert_type);
260 __free_update_info(update_info);
262 sqlite3_finalize(stmt);
263 sqlite3_close_v2(db);
266 update_info->type = convert_type;
267 *update_info_list = g_slist_append(*update_info_list, update_info);
271 sqlite3_finalize(stmt);
272 sqlite3_close_v2(db);
277 API int pkgmgrinfo_updateinfo_get_usr_updateinfo(const char *pkgid,
278 pkgmgrinfo_updateinfo_h *update_handle, uid_t uid)
281 pkgmgrinfo_pkginfo_h pkginfo = NULL;
283 updateinfo_x *update_info;
284 GSList *info_list = NULL;
286 if (update_handle == NULL || pkgid == NULL)
287 return PMINFO_R_EINVAL;
289 update_info = calloc(1, sizeof(updateinfo_x));
290 if (update_info == NULL) {
291 _LOGE("Out of memory");
292 return PMINFO_R_ERROR;
295 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &pkginfo);
296 if (ret != PMINFO_R_OK) {
300 ret = pkgmgrinfo_pkginfo_is_global(pkginfo, &is_global_pkg);
301 if (ret != PMINFO_R_OK) {
303 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
306 pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo);
308 ret = _get_pkg_updateinfo_from_db(pkgid, &info_list,
309 (is_global_pkg) ? GLOBAL_USER : uid);
311 if (ret != PMINFO_R_OK) {
312 g_slist_free_full(info_list, __free_update_info);
313 return PMINFO_R_ERROR;
315 *update_handle = (pkgmgrinfo_updateinfo_h)info_list->data;
316 g_slist_free(info_list);
321 API int pkgmgrinfo_updateinfo_get_updateinfo(const char *pkgid,
322 pkgmgrinfo_updateinfo_h *update_handle)
324 return pkgmgrinfo_updateinfo_get_usr_updateinfo(pkgid, update_handle, _getuid());
327 API int pkgmgrinfo_updateinfo_usr_foreach_updateinfo(uid_t uid,
328 pkgmgrinfo_foreach_updateinfo_cb callback, void *user_data)
334 if (callback == NULL)
335 return PMINFO_R_EINVAL;
337 info_list = calloc(1, sizeof(updateinfo_x));
338 if (info_list == NULL) {
339 _LOGE("Out of memory");
340 return PMINFO_R_ERROR;
343 ret = _get_pkg_updateinfo_from_db(NULL, &info_list, uid);
345 _LOGE("Failed to get pkg update info for user[%d]", (int)uid);
346 pkgmgrinfo_updateinfo_destroy(info_list);
347 return PMINFO_R_ERROR;
350 ret = _get_pkg_updateinfo_from_db(NULL, &info_list, GLOBAL_USER);
352 _LOGE("Failed to get pkg update info for user[%d]",
354 pkgmgrinfo_updateinfo_destroy(info_list);
355 return PMINFO_R_ERROR;
358 for (tmp_list = info_list; tmp_list; tmp_list = g_slist_next(tmp_list)) {
359 ret = callback((pkgmgrinfo_updateinfo_h)tmp_list->data, user_data);
364 g_slist_free_full(info_list, __free_update_info);
368 API int pkgmgrinfo_updateinfo_foreach_updateinfo(
369 pkgmgrinfo_foreach_updateinfo_cb callback, void *user_data)
371 return pkgmgrinfo_updateinfo_usr_foreach_updateinfo(_getuid(),
372 callback, user_data);