4 * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>,
7 * Jaeho Lee <jaeho81.lee@samsung.com>, Shobhit Srivastava <shobhit.s@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.
26 #include <sys/types.h>
34 #include "pkgmgr-info.h"
35 #include "pkgmgr-info-debug.h"
36 #include "pkgmgr_parser_db.h"
37 #include "pkgmgr_parser_internal.h"
38 #include "pkgmgr_parser_resource.h"
39 #include "pkgmgr_parser_resource_db.h"
45 #define LOG_TAG "PKGMGR_PARSER"
48 #define PKGMGR_PARSER_DB_FILE tzplatform_mkpath(TZ_USER_DB, ".pkgmgr_parser.db")
53 NODE_ATTR_SCREEN_DPI_RANGE,
54 NODE_ATTR_SCREEN_WIDTH_RANGE,
55 NODE_ATTR_SCREEN_LARGE,
57 NODE_ATTR_PLATFORM_VER,
62 static int _get_attr_val(bundle *b, char **attr_name, const char **attr_val, int attr_index)
66 _LOGE("bundle is null");
67 return PMINFO_R_EINVAL;
70 buf = malloc(BUF_SIZE);
72 _LOGE("malloc failed");
73 return PMINFO_R_ERROR;
75 memset(buf, '\0', BUF_SIZE);
77 case NODE_ATTR_SCREEN_DPI:
78 snprintf(buf, BUF_SIZE, "%s", RSC_NODE_ATTR_SCREEN_DPI);
79 *attr_val = bundle_get_val(b, RSC_NODE_ATTR_SCREEN_DPI);
81 case NODE_ATTR_SCREEN_DPI_RANGE:
82 snprintf(buf, BUF_SIZE, "%s", RSC_NODE_ATTR_SCREEN_DPI_RANGE);
83 *attr_val = bundle_get_val(b, RSC_NODE_ATTR_SCREEN_DPI_RANGE);
85 case NODE_ATTR_SCREEN_WIDTH_RANGE:
86 snprintf(buf, BUF_SIZE, "%s", RSC_NODE_ATTR_SCREEN_WIDTH_RANGE);
87 *attr_val = bundle_get_val(b, RSC_NODE_ATTR_SCREEN_WIDTH_RANGE);
89 case NODE_ATTR_SCREEN_LARGE:
90 snprintf(buf, BUF_SIZE, "%s", RSC_NODE_ATTR_SCREEN_LARGE);
91 *attr_val = bundle_get_val(b, RSC_NODE_ATTR_SCREEN_LARGE);
93 case NODE_ATTR_SCREEN_BPP:
94 snprintf(buf, BUF_SIZE, "%s", RSC_NODE_ATTR_SCREEN_BPP);
95 *attr_val = bundle_get_val(b, RSC_NODE_ATTR_SCREEN_BPP);
97 case NODE_ATTR_PLATFORM_VER:
98 snprintf(buf, BUF_SIZE, "%s", RSC_NODE_ATTR_PLATFORM_VER);
99 *attr_val = bundle_get_val(b, RSC_NODE_ATTR_PLATFORM_VER);
101 case NODE_ATTR_LANGUAGE:
102 snprintf(buf, BUF_SIZE, "%s", RSC_NODE_ATTR_LANGUAGE);
103 *attr_val = bundle_get_val(b, RSC_NODE_ATTR_LANGUAGE);
106 _LOGE("unidentified index[%d]", attr_index);
109 return PMINFO_R_ERROR;
112 if (buf != NULL && strlen(buf) != 0)
118 static int _insert_node_data_into_db(sqlite3 *pkginfo, const char *package, char *group_type, GList *node_list)
121 resource_node_t *rsc_node = NULL;
122 GList *tmp_node_list = NULL;
124 const char *attr_val = NULL;
125 char *attr_name = NULL;
129 if (pkginfo == NULL || package == NULL || strlen(package) == 0 || group_type == NULL || strlen(group_type) == 0 || node_list == NULL)
130 return PMINFO_R_EINVAL;
132 tmp_node_list = g_list_first(node_list);
134 if (tmp_node_list == NULL) {
135 _LOGE("list is null");
136 return PMINFO_R_ERROR;
139 while (tmp_node_list != NULL) {
140 rsc_node = (resource_node_t *)tmp_node_list->data;
141 if (rsc_node == NULL) {
142 _LOGE("node is null");
143 return PMINFO_R_ERROR;
146 /*get bundle for each nodes*/
149 _LOGE("bundle is null");
150 return PMINFO_R_ERROR;
153 for (i = NODE_ATTR_MIN + 1; i < NODE_ATTR_MAX; i++) {
154 ret = _get_attr_val(b, &attr_name, &attr_val, i);
156 _LOGE("get attribute from bundle failed");
160 if (attr_name == NULL || attr_val == NULL)
162 query = sqlite3_mprintf("insert into package_resource_data(id, node_folder, attr_name, attr_value) VALUES(" \
163 "(select rowid from package_resource_info where pkg_id=%Q and group_type=%Q), %Q, %Q, %Q)", package, group_type, rsc_node->folder, attr_name, attr_val);
165 /*Begin transaction*/
166 ret = sqlite3_exec(pkginfo, query, NULL, NULL, NULL);
167 if (ret != SQLITE_OK)
168 _LOGE("Failed to insert into package_resource_data, attr_name[%s], attr_val[%s]", attr_name, attr_val);
175 tmp_node_list = g_list_next(tmp_node_list);
181 int pkgmgr_parser_resource_db_remove(const char *package)
183 sqlite3 *pkginfo = NULL;
187 if (package == NULL) {
188 _LOGE("parameter is NULL");
189 return PMINFO_R_EINVAL;
193 ret = db_util_open(PKGMGR_PARSER_DB_FILE, &pkginfo, 0);
194 retvm_if(ret != SQLITE_OK, PMINFO_R_ERROR, "connect db [%s] failed!", PKGMGR_PARSER_DB_FILE);
196 /*Begin transaction*/
197 ret = sqlite3_exec(pkginfo, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
198 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Failed to begin transaction\n");
199 _LOGD("Transaction Begin\n");
201 /*delete data from package_resource_data*/
202 query = sqlite3_mprintf("delete from package_resource_data where id in (select rowid from package_resource_info where pkg_id=%Q)", package);
203 ret = sqlite3_exec(pkginfo, query, NULL, NULL, NULL);
204 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Failed to delete from package_resource_info");
208 /*delete data from package_resource_info*/
209 query = sqlite3_mprintf("delete from package_resource_info where pkg_id=%Q", package);
210 ret = sqlite3_exec(pkginfo, query, NULL, NULL, NULL);
211 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Failed to delete from package_resource_info");
213 /*Commit transaction*/
214 ret = sqlite3_exec(pkginfo, "COMMIT", NULL, NULL, NULL);
215 if (ret != SQLITE_OK) {
216 _LOGE("Failed to commit transaction. Rollback now\n");
217 ret = sqlite3_exec(pkginfo, "ROLLBACK", NULL, NULL, NULL);
218 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
220 _LOGD("Transaction Commit and End\n");
224 sqlite3_close(pkginfo);
230 int pkgmgr_parser_resource_db_save(const char *package, resource_data_t *data)
232 sqlite3 *pkginfo = NULL;
235 GList *group_list = NULL;
236 GList *node_list = NULL;
237 resource_group_t *rsc_group = NULL;
239 if (package == NULL || strlen(package) == 0 || data == NULL) {
240 _LOGE("invalid parameter");
244 ret = pkgmgr_parser_check_and_create_db(getuid());
246 ret = pkgmgr_parser_initialize_db();
248 _LOGE("db initialization failed");
253 group_list = g_list_first(data->group_list);
255 ret = db_util_open(PKGMGR_PARSER_DB_FILE, &pkginfo, 0);
256 retvm_if(ret != SQLITE_OK, PMINFO_R_ERROR, "connect db [%s] failed!", PKGMGR_PARSER_DB_FILE);
258 /*Begin transaction*/
259 ret = sqlite3_exec(pkginfo, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
260 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Failed to begin transaction\n");
261 _LOGD("Transaction Begin\n");
263 while (group_list != NULL) {
267 rsc_group = (resource_group_t *)group_list->data;
268 node_list = g_list_first(rsc_group->node_list);
270 if (rsc_group == NULL || node_list == NULL) {
271 _LOGE("value is null");
276 query = sqlite3_mprintf("insert into package_resource_info(pkg_id, group_folder, group_type) VALUES(%Q, %Q, %Q)", \
277 package, rsc_group->folder, rsc_group->type);
279 /*Begin transaction*/
280 ret = sqlite3_exec(pkginfo, query, NULL, NULL, NULL);
281 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Failed to insert into package_resource_info");
285 ret = _insert_node_data_into_db(pkginfo, package, rsc_group->type, node_list);
287 group_list = g_list_next(group_list);
290 /*Commit transaction*/
291 ret = sqlite3_exec(pkginfo, "COMMIT", NULL, NULL, NULL);
292 if (ret != SQLITE_OK) {
293 _LOGE("Failed to commit transaction. Rollback now\n");
294 ret = sqlite3_exec(pkginfo, "ROLLBACK", NULL, NULL, NULL);
295 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "Don't execute query = %s\n", query);
297 _LOGD("Transaction Commit and End\n");
303 pkgmgr_parser_close_db();
304 sqlite3_close(pkginfo);
311 static gint _find_group_type(void *group, void *data)
313 resource_group_t *rsc_group = (resource_group_t *)group;
315 return strcmp(rsc_group->type, (char *)data);
318 static gint _find_node_folder(void *node, void *data)
320 resource_node_t *rsc_node = (resource_node_t *)node;
321 char *str = (char *)data;
323 return strcmp(rsc_node->folder, str);
326 static int _init_node(char *node_folder, resource_node_t **rsc_node)
328 resource_node_t *tmp_node = NULL;
330 if (node_folder == NULL) {
331 _LOGE("invalid parameter");
332 return PMINFO_R_EINVAL;
335 tmp_node = malloc(sizeof(resource_node_t));
336 if (tmp_node == NULL) {
337 _LOGE("malloc failed");
338 return PMINFO_R_ERROR;
341 tmp_node->folder = strdup(node_folder);
342 tmp_node->attr = bundle_create();
343 *rsc_node = tmp_node;
348 static int _init_group(char *group_type, resource_group_t **rsc_group)
350 resource_group_t *tmp_group = NULL;
352 if (group_type == NULL) {
353 _LOGE("invalid parameter");
354 return PMINFO_R_EINVAL;
357 tmp_group = malloc(sizeof(resource_group_t));
358 if (tmp_group == NULL) {
359 _LOGE("malloc failed");
360 return PMINFO_R_ERROR;
363 tmp_group->type = strdup(group_type);
364 tmp_group->node_list = NULL;
365 *rsc_group = tmp_group;
370 int pkgmgr_parser_resource_db_load(const char *package, resource_data_t **data)
372 sqlite3 *pkginfo = NULL;
373 sqlite3_stmt *stmt = NULL;
375 char *colname = NULL;
376 char *group_type = NULL;
377 char *group_folder = NULL;
378 char *node_folder = NULL;
379 char *attr_name = NULL;
380 char *attr_value = NULL;
384 resource_data_t *rsc_data = NULL;
385 resource_group_t *rsc_group = NULL;
386 resource_node_t *rsc_node = NULL;
387 GList *group_list = NULL;
388 GList *node_list = NULL;
389 GList *tmp_group_list = NULL;
390 GList *tmp_node_list = NULL;
392 ret = db_util_open(PKGMGR_PARSER_DB_FILE, &pkginfo, 0);
393 retvm_if(ret != SQLITE_OK, PMINFO_R_ERROR, "connect db [%s] failed!", PKGMGR_PARSER_DB_FILE);
394 query = sqlite3_mprintf("select " \
395 "package_resource_info.group_type, package_resource_info.group_folder, package_resource_data.node_folder, package_resource_data.attr_name, package_resource_data.attr_value " \
396 "from package_resource_info, package_resource_data where " \
397 "package_resource_info.rowid=package_resource_data.id and " \
398 "package_resource_info.pkg_id=%Q order by package_resource_data.rowid asc", \
400 ret = sqlite3_prepare_v2(pkginfo, query, strlen(query), &stmt, NULL);
401 tryvm_if(ret != SQLITE_OK, ret = PMINFO_R_ERROR, "sqlite3_prepare_v2 query = %s\n", query);
402 cols = sqlite3_column_count(stmt);
405 ret = sqlite3_step(stmt);
406 if (ret != SQLITE_ROW)
408 tmp_group_list = NULL;
409 tmp_node_list = NULL;
413 for (i = 0; i < cols; i++) {
414 colname = (char *)sqlite3_column_name(stmt, i);
415 if (strcmp(colname, "group_type") == 0) {
417 group_type = (char *)sqlite3_column_text(stmt, i);
418 tmp_group_list = g_list_find_custom(group_list, group_type, (GCompareFunc)_find_group_type);
419 if (tmp_group_list == NULL) {
420 ret = _init_group(group_type, &rsc_group);
421 if (ret != PMINFO_R_OK) {
422 _LOGE("group initialization failed[%d]", ret);
425 group_list = g_list_append(group_list, rsc_group);
428 rsc_group = (resource_group_t *)tmp_group_list->data;
429 node_list = rsc_group->node_list;
431 } else if (strcmp(colname, "group_folder") == 0) {
433 group_folder = (char *)sqlite3_column_text(stmt, i);
434 if (rsc_group->folder != NULL && strcmp(rsc_group->folder, group_folder) == 0)
436 else if (rsc_group != NULL && group_folder != NULL)
437 rsc_group->folder = strdup(group_folder);
439 _LOGE("rsc_group and group_folder should not be null");
440 ret = PMINFO_R_ERROR;
443 } else if (strcmp(colname, "node_folder") == 0) {
445 node_folder = (char *)sqlite3_column_text(stmt, i);
446 tmp_node_list = g_list_find_custom(node_list, node_folder, (GCompareFunc)_find_node_folder);
447 if (tmp_node_list == NULL) {
448 ret = _init_node(node_folder, &rsc_node);
449 /*initialize new node*/
450 if (ret != PMINFO_R_OK) {
451 _LOGE("node initialization failed[%d]", ret);
454 node_list = g_list_append(node_list, rsc_node);
455 if (rsc_group->node_list == NULL)
456 rsc_group->node_list = node_list;
458 rsc_node = (resource_node_t *)tmp_node_list->data;
459 } else if (strcmp(colname, "attr_name") == 0) {
461 attr_name = (char *)sqlite3_column_text(stmt, i);
462 } else if (strcmp(colname, "attr_value") == 0) {
464 attr_value = (char *)sqlite3_column_text(stmt, i);
465 if (rsc_node != NULL && attr_name != NULL && attr_value != NULL) {
466 if (rsc_node->attr != NULL)
467 bundle_add(rsc_node->attr, attr_name, attr_value);
469 _LOGE("bundle is not initialized");
470 ret = PMINFO_R_ERROR;
474 _LOGE("error happened");
475 ret = PMINFO_R_ERROR;
480 _LOGE("unexpected column name detected:[%s]", colname);
481 ret = PMINFO_R_ERROR;
486 if (ret == SQLITE_DONE)
489 rsc_data = malloc(sizeof(resource_data_t));
490 if (rsc_data == NULL) {
491 _LOGD("malloc failed");
492 ret = PMINFO_R_ERROR;
495 rsc_data->group_list = group_list;
496 rsc_data->package = strdup(package);
500 sqlite3_close(pkginfo);