Change pkgmgr db label to "User::Home"
[platform/core/appfw/pkgmgr-info.git] / parser / pkgmgr_parser_db.c
1 /*
2  * pkgmgr-info
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
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>
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 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #include <sys/smack.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 #include <grp.h>
31 #include <pwd.h>
32
33 #include <db-util.h>
34 #include <glib.h>
35 /* For multi-user support */
36 #include <tzplatform_config.h>
37
38 #include "pkgmgr-info.h"
39 #include "pkgmgrinfo_basic.h"
40 #include "pkgmgrinfo_debug.h"
41 #include "pkgmgr_parser_internal.h"
42 #include "pkgmgr_parser_db.h"
43
44 #ifdef LOG_TAG
45 #undef LOG_TAG
46 #endif
47 #define LOG_TAG "PKGMGR_PARSER"
48
49 #define PKGMGR_PARSER_DB_FILE tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_parser.db")
50 #define PKGMGR_CERT_DB_FILE tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_cert.db")
51 #define MAX_QUERY_LEN           4096
52 #define BUFSIZE 4096
53 #define OWNER_ROOT 0
54
55 #define DB_LABEL "User::Home"
56 #define SET_SMACK_LABEL(x) \
57 do { \
58         if (smack_setlabel((x), DB_LABEL, SMACK_LABEL_ACCESS)) \
59                 _LOGE("failed chsmack -a %s %s", DB_LABEL, x); \
60         else \
61                 _LOGD("chsmack -a %s %s", DB_LABEL, x); \
62 } while (0)
63
64 sqlite3 *pkgmgr_parser_db;
65 sqlite3 *pkgmgr_cert_db;
66
67
68 #define QUERY_CREATE_TABLE_PACKAGE_INFO "create table if not exists package_info " \
69                                                 "(package text primary key not null, " \
70                                                 "package_type text DEFAULT 'rpm', " \
71                                                 "package_version text, " \
72                                                 "package_api_version text, " \
73                                                 "install_location text, " \
74                                                 "package_size text, " \
75                                                 "package_removable text DEFAULT 'true', " \
76                                                 "package_preload text DEFAULT 'false', " \
77                                                 "package_readonly text DEFAULT 'false', " \
78                                                 "package_update text DEFAULT 'false', " \
79                                                 "package_appsetting text DEFAULT 'false', " \
80                                                 "package_nodisplay text DEFAULT 'false', " \
81                                                 "package_system text DEFAULT 'false', " \
82                                                 "author_name text, " \
83                                                 "author_email text, " \
84                                                 "author_href text," \
85                                                 "installed_time text," \
86                                                 "installed_storage text," \
87                                                 "storeclient_id text," \
88                                                 "mainapp_id text," \
89                                                 "package_url text," \
90                                                 "root_path text," \
91                                                 "csc_path text," \
92                                                 "package_support_disable text DEFAULT 'false')"
93
94 #define QUERY_CREATE_TABLE_PACKAGE_LOCALIZED_INFO "create table if not exists package_localized_info " \
95                                                 "(package text not null, " \
96                                                 "package_locale text DEFAULT 'No Locale', " \
97                                                 "package_label text, " \
98                                                 "package_icon text, " \
99                                                 "package_description text, " \
100                                                 "package_license text, " \
101                                                 "package_author, " \
102                                                 "PRIMARY KEY(package, package_locale), " \
103                                                 "FOREIGN KEY(package) " \
104                                                 "REFERENCES package_info(package) " \
105                                                 "ON DELETE CASCADE)"
106
107 #define QUERY_CREATE_TABLE_PACKAGE_PRIVILEGE_INFO "create table if not exists package_privilege_info " \
108                                                 "(package text not null, " \
109                                                 "privilege text not null, " \
110                                                 "PRIMARY KEY(package, privilege) " \
111                                                 "FOREIGN KEY(package) " \
112                                                 "REFERENCES package_info(package) " \
113                                                 "ON DELETE CASCADE)"
114
115 #define QUERY_CREATE_TABLE_PACKAGE_APP_INFO "create table if not exists package_app_info " \
116                                                 "(app_id text primary key not null, " \
117                                                 "app_component text, " \
118                                                 "app_exec text, " \
119                                                 "app_nodisplay text DEFAULT 'false', " \
120                                                 "app_type text, " \
121                                                 "app_onboot text DEFAULT 'false', " \
122                                                 "app_multiple text DEFAULT 'false', " \
123                                                 "app_autorestart text DEFAULT 'false', " \
124                                                 "app_taskmanage text DEFAULT 'false', " \
125                                                 "app_enabled text DEFAULT 'true', " \
126                                                 "app_hwacceleration text DEFAULT 'use-system-setting', " \
127                                                 "app_screenreader text DEFAULT 'use-system-setting', " \
128                                                 "app_mainapp text, " \
129                                                 "app_recentimage text, " \
130                                                 "app_launchcondition text, " \
131                                                 "app_indicatordisplay text DEFAULT 'true', " \
132                                                 "app_portraitimg text, " \
133                                                 "app_landscapeimg text, " \
134                                                 "app_guestmodevisibility text DEFAULT 'true', " \
135                                                 "app_permissiontype text DEFAULT 'normal', " \
136                                                 "app_preload text DEFAULT 'false', " \
137                                                 "app_submode text DEFAULT 'false', " \
138                                                 "app_submode_mainid text, " \
139                                                 "app_launch_mode text NOT NULL DEFAULT 'caller', " \
140                                                 "app_ui_gadget text DEFAULT 'false', " \
141                                                 "app_support_disable text DEFAULT 'false', " \
142                                                 "component_type text, " \
143                                                 "package text not null, " \
144                                                 "FOREIGN KEY(package) " \
145                                                 "REFERENCES package_info(package) " \
146                                                 "ON DELETE CASCADE)"
147
148 #define QUERY_CREATE_TABLE_PACKAGE_APP_LOCALIZED_INFO "create table if not exists package_app_localized_info " \
149                                                 "(app_id text not null, " \
150                                                 "app_locale text DEFAULT 'No Locale', " \
151                                                 "app_label text, " \
152                                                 "app_icon text, " \
153                                                 "PRIMARY KEY(app_id,app_locale) " \
154                                                 "FOREIGN KEY(app_id) " \
155                                                 "REFERENCES package_app_info(app_id) " \
156                                                 "ON DELETE CASCADE)"
157
158 #define QUERY_CREATE_TABLE_PACKAGE_APP_ICON_SECTION_INFO "create table if not exists package_app_icon_section_info " \
159                                                 "(app_id text not null, " \
160                                                 "app_icon text, " \
161                                                 "app_icon_section text, " \
162                                                 "app_icon_resolution text, " \
163                                                 "PRIMARY KEY(app_id,app_icon_section,app_icon_resolution) " \
164                                                 "FOREIGN KEY(app_id) " \
165                                                 "REFERENCES package_app_info(app_id) " \
166                                                 "ON DELETE CASCADE)"
167
168 #define QUERY_CREATE_TABLE_PACKAGE_APP_IMAGE_INFO "create table if not exists package_app_image_info " \
169                                                 "(app_id text not null, " \
170                                                 "app_locale text DEFAULT 'No Locale', " \
171                                                 "app_image_section text, " \
172                                                 "app_image text, " \
173                                                 "PRIMARY KEY(app_id,app_image_section) " \
174                                                 "FOREIGN KEY(app_id) " \
175                                                 "REFERENCES package_app_info(app_id) " \
176                                                 "ON DELETE CASCADE)"
177
178 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_CONTROL "create table if not exists package_app_app_control " \
179                                                 "(app_id text not null, " \
180                                                 "app_control text not null, " \
181                                                 "PRIMARY KEY(app_id,app_control) " \
182                                                 "FOREIGN KEY(app_id) " \
183                                                 "REFERENCES package_app_info(app_id) " \
184                                                 "ON DELETE CASCADE)"
185
186 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_SVC "create table if not exists package_app_app_svc " \
187                                                 "(app_id text not null, " \
188                                                 "operation text not null, " \
189                                                 "uri_scheme text, " \
190                                                 "mime_type text, " \
191                                                 "subapp_name text, " \
192                                                 "PRIMARY KEY(app_id,operation,uri_scheme,mime_type,subapp_name) " \
193                                                 "FOREIGN KEY(app_id) " \
194                                                 "REFERENCES package_app_info(app_id) " \
195                                                 "ON DELETE CASCADE)"
196
197 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_CATEGORY "create table if not exists package_app_app_category " \
198                                                 "(app_id text not null, " \
199                                                 "category text not null, " \
200                                                 "PRIMARY KEY(app_id,category) " \
201                                                 "FOREIGN KEY(app_id) " \
202                                                 "REFERENCES package_app_info(app_id) " \
203                                                 "ON DELETE CASCADE)"
204
205 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_METADATA "create table if not exists package_app_app_metadata " \
206                                                 "(app_id text not null, " \
207                                                 "md_key text not null, " \
208                                                 "md_value text not null, " \
209                                                 "PRIMARY KEY(app_id, md_key, md_value) " \
210                                                 "FOREIGN KEY(app_id) " \
211                                                 "REFERENCES package_app_info(app_id) " \
212                                                 "ON DELETE CASCADE)"
213
214 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_PERMISSION "create table if not exists package_app_app_permission " \
215                                                 "(app_id text not null, " \
216                                                 "pm_type text not null, " \
217                                                 "pm_value text not null, " \
218                                                 "PRIMARY KEY(app_id, pm_type, pm_value) " \
219                                                 "FOREIGN KEY(app_id) " \
220                                                 "REFERENCES package_app_info(app_id) " \
221                                                 "ON DELETE CASCADE)"
222
223 #define QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_ALLOWED "create table if not exists package_app_share_allowed " \
224                                                 "(app_id text not null, " \
225                                                 "data_share_path text not null, " \
226                                                 "data_share_allowed text not null, " \
227                                                 "PRIMARY KEY(app_id,data_share_path,data_share_allowed) " \
228                                                 "FOREIGN KEY(app_id) " \
229                                                 "REFERENCES package_app_info(app_id) " \
230                                                 "ON DELETE CASCADE)"
231
232 #define QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_REQUEST "create table if not exists package_app_share_request " \
233                                                 "(app_id text not null, " \
234                                                 "data_share_request text not null, " \
235                                                 "PRIMARY KEY(app_id,data_share_request) " \
236                                                 "FOREIGN KEY(app_id) " \
237                                                 "REFERENCES package_app_info(app_id) " \
238                                                 "ON DELETE CASCADE)"
239
240 #define QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO "create table if not exists package_cert_index_info " \
241                                                 "(cert_info text not null, " \
242                                                 "cert_id integer, " \
243                                                 "cert_ref_count integer, " \
244                                                 "PRIMARY KEY(cert_id)) "
245
246 #define QUERY_CREATE_TABLE_PACKAGE_CERT_INFO "create table if not exists package_cert_info " \
247                                                 "(package text not null, " \
248                                                 "author_root_cert integer, " \
249                                                 "author_im_cert integer, " \
250                                                 "author_signer_cert integer, " \
251                                                 "dist_root_cert integer, " \
252                                                 "dist_im_cert integer, " \
253                                                 "dist_signer_cert integer, " \
254                                                 "dist2_root_cert integer, " \
255                                                 "dist2_im_cert integer, " \
256                                                 "dist2_signer_cert integer, " \
257                                                 "PRIMARY KEY(package)) "
258
259 #define QUERY_CREATE_TABLE_PACKAGE_APP_DATA_CONTROL "create table if not exists package_app_data_control " \
260                                                 "(app_id text not null, " \
261                                                 "providerid text not null, " \
262                                                 "access text not null, " \
263                                                 "type text not null, " \
264                                                 "PRIMARY KEY(app_id, providerid, access, type) " \
265                                                 "FOREIGN KEY(app_id) " \
266                                                 "REFERENCES package_app_info(app_id) " \
267                                                 "ON DELETE CASCADE)"
268
269 static int __insert_uiapplication_info(manifest_x *mfx);
270 static int __insert_serviceapplication_info(manifest_x *mfx);
271 static int __insert_uiapplication_appsvc_info(manifest_x *mfx);
272 static int __insert_serviceapplication_appsvc_info(manifest_x *mfx);
273 static int __insert_uiapplication_appcategory_info(manifest_x *mfx);
274 static int __insert_serviceapplication_appcategory_info(manifest_x *mfx);
275 static int __insert_uiapplication_appcontrol_info(manifest_x *mfx);
276 static int __insert_serviceapplication_appmetadata_info(manifest_x *mfx);
277 static int __insert_uiapplication_appmetadata_info(manifest_x *mfx);
278 static int __insert_serviceapplication_appcontrol_info(manifest_x *mfx);
279 static int __insert_uiapplication_share_allowed_info(manifest_x *mfx);
280 static int __insert_serviceapplication_share_allowed_info(manifest_x *mfx);
281 static int __insert_uiapplication_share_request_info(manifest_x *mfx);
282 static int __insert_serviceapplication_share_request_info(manifest_x *mfx);
283 static int __insert_uiapplication_datacontrol_info(manifest_x *mfx);
284 static int __insert_serviceapplication_datacontrol_info(manifest_x *mfx);
285 static void __insert_serviceapplication_locale_info(gpointer data, gpointer userdata);
286 static void __insert_uiapplication_locale_info(gpointer data, gpointer userdata);
287 static void __insert_pkglocale_info(gpointer data, gpointer userdata);
288 static int __insert_manifest_info_in_db(manifest_x *mfx, uid_t uid);
289 static int __delete_manifest_info_from_db(manifest_x *mfx, uid_t uid);
290 static int __delete_subpkg_info_from_db(char *appid);
291 static int __delete_appinfo_from_db(char *db_table, const char *appid);
292 static int __initialize_db(sqlite3 *db_handle, const char *db_query);
293 static int __exec_query(char *query);
294 static void __extract_data(gpointer data, label_x *lbl, license_x *lcn, icon_x *icn, description_x *dcn, author_x *ath,
295                 char **label, char **license, char **icon, char **description, char **author);
296 static gint __comparefunc(gconstpointer a, gconstpointer b, gpointer userdata);
297 static GList *__create_locale_list(GList *locale, label_x *lbl, license_x *lcn, icon_x *icn, description_x *dcn, author_x *ath);
298 static void __preserve_guestmode_visibility_value(manifest_x *mfx);
299 static int __guestmode_visibility_cb(void *data, int ncols, char **coltxt, char **colname);
300 static int __pkgmgr_parser_create_db(sqlite3 **db_handle, const char *db_path);
301 static int __parserdb_change_perm(const char *db_file, uid_t uid);
302
303 static int __delete_subpkg_list_cb(void *data, int ncols, char **coltxt, char **colname)
304 {
305         if (coltxt[0])
306                 __delete_subpkg_info_from_db(coltxt[0]);
307
308         return 0;
309 }
310
311 static const char *__get_str(const char *str)
312 {
313         if (str == NULL)
314         {
315                 return PKGMGR_PARSER_EMPTY_STR;
316         }
317
318         return str;
319 }
320
321 static int __pkgmgr_parser_create_db(sqlite3 **db_handle, const char *db_path)
322 {
323         int ret = -1;
324         sqlite3 *handle;
325
326         ret = db_util_open(db_path, &handle,  DB_UTIL_REGISTER_HOOK_METHOD);
327         if (ret != SQLITE_OK) {
328                 _LOGD("connect db [%s] failed!\n", db_path);
329                 return -1;
330         }
331         *db_handle = handle;
332
333         return 0;
334 }
335
336 static int __guestmode_visibility_cb(void *data, int ncols, char **coltxt, char **colname)
337 {
338         manifest_x *mfx = (manifest_x *)data;
339         int i = 0;
340         char *appid = NULL;
341         char *status = NULL;
342         uiapplication_x *uiapp = NULL;
343         for(i = 0; i < ncols; i++)
344         {
345                 uiapp = mfx->uiapplication;
346                 if (strcmp(colname[i], "app_id") == 0) {
347                         if (coltxt[i])
348                                 appid = strdup(coltxt[i]);
349                 } else if (strcmp(colname[i], "app_guestmodevisibility") == 0) {
350                         if (coltxt[i])
351                                 status = strdup(coltxt[i]);
352                 }
353         }
354         if (appid == NULL) {
355                 _LOGD("app id is NULL\n");
356                 return -1;
357         }
358         /*update guest mode visibility*/
359         for (; uiapp != NULL; uiapp = uiapp->next) {
360                 if (strcmp(uiapp->appid, appid) == 0) {
361                         free((void *)uiapp->guestmode_visibility);
362                         uiapp->guestmode_visibility = strdup(status);
363                         break;
364                 }
365         }
366         if (appid) {
367                 free(appid);
368                 appid = NULL;
369         }
370         if (status) {
371                 free(status);
372                 status = NULL;
373         }
374
375         return 0;
376 }
377
378 static void __preserve_guestmode_visibility_value(manifest_x *mfx)
379 {
380         char *error_message = NULL;
381         char query[MAX_QUERY_LEN] = {'\0'};
382         snprintf(query, MAX_QUERY_LEN - 1, "select app_id, app_guestmodevisibility from package_app_info where package='%s'", mfx->package);
383         if (SQLITE_OK !=
384             sqlite3_exec(pkgmgr_parser_db, query,
385                          __guestmode_visibility_cb, (void *)mfx, &error_message)) {
386                 _LOGD("Don't execute query = %s error message = %s\n",
387                        query, error_message);
388                 sqlite3_free(error_message);
389         }
390         return;
391 }
392
393 static int __initialize_db(sqlite3 *db_handle, const char *db_query)
394 {
395         char *error_message = NULL;
396         if (SQLITE_OK !=
397             sqlite3_exec(db_handle, db_query,
398                          NULL, NULL, &error_message)) {
399                 _LOGD("Don't execute query = %s error message = %s\n",
400                        db_query, error_message);
401                 sqlite3_free(error_message);
402                 return -1;
403         }
404         sqlite3_free(error_message);
405         return 0;
406 }
407
408 static int __exec_query(char *query)
409 {
410         char *error_message = NULL;
411         if (SQLITE_OK !=
412             sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, &error_message)) {
413                 _LOGD("Don't execute query = %s error message = %s\n", query,
414                        error_message);
415                 sqlite3_free(error_message);
416                 return -1;
417         }
418         sqlite3_free(error_message);
419         return 0;
420 }
421
422 static int __exec_query_no_msg(char *query)
423 {
424         char *error_message = NULL;
425         if (SQLITE_OK !=
426             sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, &error_message)) {
427                 sqlite3_free(error_message);
428                 return -1;
429         }
430         sqlite3_free(error_message);
431         return 0;
432 }
433
434 static GList *__create_locale_list(GList *locale, label_x *lbl, license_x *lcn, icon_x *icn, description_x *dcn, author_x *ath)
435 {
436
437         while(lbl != NULL)
438         {
439                 if (lbl->lang)
440                         locale = g_list_insert_sorted_with_data(locale, (gpointer)lbl->lang, __comparefunc, NULL);
441                 lbl = lbl->next;
442         }
443         while(lcn != NULL)
444         {
445                 if (lcn->lang)
446                         locale = g_list_insert_sorted_with_data(locale, (gpointer)lcn->lang, __comparefunc, NULL);
447                 lcn = lcn->next;
448         }
449         while(icn != NULL)
450         {
451                 if (icn->lang)
452                         locale = g_list_insert_sorted_with_data(locale, (gpointer)icn->lang, __comparefunc, NULL);
453                 icn = icn->next;
454         }
455         while(dcn != NULL)
456         {
457                 if (dcn->lang)
458                         locale = g_list_insert_sorted_with_data(locale, (gpointer)dcn->lang, __comparefunc, NULL);
459                 dcn = dcn->next;
460         }
461         while(ath != NULL)
462         {
463                 if (ath->lang)
464                         locale = g_list_insert_sorted_with_data(locale, (gpointer)ath->lang, __comparefunc, NULL);
465                 ath = ath->next;
466         }
467         return locale;
468
469 }
470
471 static GList *__create_icon_list(GList *locale, icon_x *icn)
472 {
473         while(icn != NULL)
474         {
475                 if (icn->section)
476                         locale = g_list_insert_sorted_with_data(locale, (gpointer)icn->section, __comparefunc, NULL);
477                 icn = icn->next;
478         }
479         return locale;
480 }
481
482 static GList *__create_image_list(GList *locale, image_x *image)
483 {
484         while(image != NULL)
485         {
486                 if (image->section)
487                         locale = g_list_insert_sorted_with_data(locale, (gpointer)image->section, __comparefunc, NULL);
488                 image = image->next;
489         }
490         return locale;
491 }
492
493 static void __trimfunc(GList* trim_list)
494 {
495         char *trim_data = NULL;
496         char *prev = NULL;
497
498         GList *list = NULL;
499         list = g_list_first(trim_list);
500
501         while (list) {
502                 trim_data = (char *)list->data;
503                 if (trim_data) {
504                         if (prev) {
505                                 if (strcmp(trim_data, prev) == 0) {
506                                         trim_list = g_list_remove(trim_list, trim_data);
507                                         list = g_list_first(trim_list);
508                                         prev = NULL;
509                                         continue;
510                                 } else
511                                         prev = trim_data;
512                         }
513                         else
514                                 prev = trim_data;
515                 }
516                 list = g_list_next(list);
517         }
518 }
519
520 static gint __comparefunc(gconstpointer a, gconstpointer b, gpointer userdata)
521 {
522         if (a == NULL || b == NULL)
523                 return 0;
524         if (strcmp((char*)a, (char*)b) == 0)
525                 return 0;
526         if (strcmp((char*)a, (char*)b) < 0)
527                 return -1;
528         if (strcmp((char*)a, (char*)b) > 0)
529                 return 1;
530         return 0;
531 }
532
533 static void __extract_data(gpointer data, label_x *lbl, license_x *lcn, icon_x *icn, description_x *dcn, author_x *ath,
534                 char **label, char **license, char **icon, char **description, char **author)
535 {
536         while(lbl != NULL)
537         {
538                 if (lbl->lang) {
539                         if (strcmp(lbl->lang, (char *)data) == 0) {
540                                 *label = (char*)lbl->text;
541                                 break;
542                         }
543                 }
544                 lbl = lbl->next;
545         }
546         while(lcn != NULL)
547         {
548                 if (lcn->lang) {
549                         if (strcmp(lcn->lang, (char *)data) == 0) {
550                                 *license = (char*)lcn->text;
551                                 break;
552                         }
553                 }
554                 lcn = lcn->next;
555         }
556         while(icn != NULL)
557         {
558                 if (icn->lang) {
559                         if (strcmp(icn->lang, (char *)data) == 0) {
560                                 *icon = (char*)icn->text;
561                                 break;
562                         }
563                 }
564                 icn = icn->next;
565         }
566         while(dcn != NULL)
567         {
568                 if (dcn->lang) {
569                         if (strcmp(dcn->lang, (char *)data) == 0) {
570                                 *description = (char*)dcn->text;
571                                 break;
572                         }
573                 }
574                 dcn = dcn->next;
575         }
576         while(ath != NULL)
577         {
578                 if (ath->lang) {
579                         if (strcmp(ath->lang, (char *)data) == 0) {
580                                 *author = (char*)ath->text;
581                                 break;
582                         }
583                 }
584                 ath = ath->next;
585         }
586
587 }
588
589 static void __extract_icon_data(gpointer data, icon_x *icn, char **icon, char **resolution)
590 {
591         while(icn != NULL)
592         {
593                 if (icn->section) {
594                         if (strcmp(icn->section, (char *)data) == 0) {
595                                 *icon = (char*)icn->text;
596                                 *resolution = (char*)icn->resolution;
597                                 break;
598                         }
599                 }
600                 icn = icn->next;
601         }
602 }
603
604 static void __extract_image_data(gpointer data, image_x*image, char **lang, char **img)
605 {
606         while(image != NULL)
607         {
608                 if (image->section) {
609                         if (strcmp(image->section, (char *)data) == 0) {
610                                 *lang = (char*)image->lang;
611                                 *img = (char*)image->text;
612                                 break;
613                         }
614                 }
615                 image = image->next;
616         }
617 }
618
619 static void __insert_pkglocale_info(gpointer data, gpointer userdata)
620 {
621         int ret = -1;
622         char *label = NULL;
623         char *icon = NULL;
624         char *description = NULL;
625         char *license = NULL;
626         char *author = NULL;
627         char query[MAX_QUERY_LEN] = {'\0'};
628
629         manifest_x *mfx = (manifest_x *)userdata;
630         label_x *lbl = mfx->label;
631         license_x *lcn = mfx->license;
632         icon_x *icn = mfx->icon;
633         description_x *dcn = mfx->description;
634         author_x *ath = mfx->author;
635
636         __extract_data(data, lbl, lcn, icn, dcn, ath, &label, &license, &icon, &description, &author);
637         if (!label && !description && !icon && !license && !author)
638                 return;
639
640         sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_localized_info(package, package_locale, " \
641                 "package_label, package_icon, package_description, package_license, package_author) values " \
642                 "('%q', '%q', '%q', '%q', '%s', '%s', '%s')",
643                 mfx->package,
644                 (char*)data,
645                 label,
646                 icon,
647                 __get_str(description),
648                 __get_str(license),
649                 __get_str(author));
650
651         ret = __exec_query(query);
652         if (ret == -1)
653                 _LOGD("Package Localized Info DB Insert failed\n");
654 }
655
656 static void __insert_uiapplication_locale_info(gpointer data, gpointer userdata)
657 {
658         int ret = -1;
659         char *label = NULL;
660         char *icon = NULL;
661         char query[MAX_QUERY_LEN] = {'\0'};
662
663         uiapplication_x *up = (uiapplication_x*)userdata;
664         label_x *lbl = up->label;
665         icon_x *icn = up->icon;
666
667         __extract_data(data, lbl, NULL, icn, NULL, NULL, &label, NULL, &icon, NULL, NULL);
668         if (!label && !icon)
669                 return;
670         sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_app_localized_info(app_id, app_locale, " \
671                 "app_label, app_icon) values " \
672                 "('%q', '%q', '%q', '%q')", up->appid, (char*)data,
673                 label, icon);
674         ret = __exec_query(query);
675         if (ret == -1)
676                 _LOGD("Package UiApp Localized Info DB Insert failed\n");
677
678         /*insert ui app locale info to pkg locale to get mainapp data */
679         if (strcasecmp(up->mainapp, "true")==0) {
680                 sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_localized_info(package, package_locale, " \
681                         "package_label, package_icon, package_description, package_license, package_author) values " \
682                         "('%q', '%q', '%q', '%q', '%q', '%q', '%q')",
683                         up->package,
684                         (char*)data,
685                         label,
686                         icon,
687                         PKGMGR_PARSER_EMPTY_STR,
688                         PKGMGR_PARSER_EMPTY_STR,
689                         PKGMGR_PARSER_EMPTY_STR);
690
691                 ret = __exec_query_no_msg(query);
692
693                 if (icon != NULL) {
694                         sqlite3_snprintf(MAX_QUERY_LEN, query, "update package_localized_info set package_icon='%s' "\
695                                 "where package='%s' and package_locale='%s'", icon, up->package, (char*)data);
696                         ret = __exec_query_no_msg(query);
697                 }
698         }
699 }
700
701 static void __insert_uiapplication_icon_section_info(gpointer data, gpointer userdata)
702 {
703         int ret = -1;
704         char *icon = NULL;
705         char *resolution = NULL;
706         char query[MAX_QUERY_LEN] = {'\0'};
707
708         uiapplication_x *up = (uiapplication_x*)userdata;
709         icon_x *icn = up->icon;
710
711         __extract_icon_data(data, icn, &icon, &resolution);
712         if (!icon && !resolution)
713                 return;
714         sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_app_icon_section_info(app_id, " \
715                 "app_icon, app_icon_section, app_icon_resolution) values " \
716                 "('%q', '%q', '%q', '%q')", up->appid,
717                 icon, (char*)data, resolution);
718
719         ret = __exec_query(query);
720         if (ret == -1)
721                 _LOGD("Package UiApp Localized Info DB Insert failed\n");
722
723 }
724
725 static void __insert_uiapplication_image_info(gpointer data, gpointer userdata)
726 {
727         int ret = -1;
728         char *lang = NULL;
729         char *img = NULL;
730         char query[MAX_QUERY_LEN] = {'\0'};
731
732         uiapplication_x *up = (uiapplication_x*)userdata;
733         image_x *image = up->image;
734
735         __extract_image_data(data, image, &lang, &img);
736         if (!lang && !img)
737                 return;
738         sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_app_image_info(app_id, app_locale, " \
739                 "app_image_section, app_image) values " \
740                 "('%q', '%q', '%q', '%q')", up->appid, lang, (char*)data, img);
741
742         ret = __exec_query(query);
743         if (ret == -1)
744                 _LOGD("Package UiApp image Info DB Insert failed\n");
745
746 }
747
748
749 static void __insert_serviceapplication_locale_info(gpointer data, gpointer userdata)
750 {
751         int ret = -1;
752         char *icon = NULL;
753         char *label = NULL;
754         char query[MAX_QUERY_LEN] = {'\0'};
755
756         serviceapplication_x *sp = (serviceapplication_x*)userdata;
757         label_x *lbl = sp->label;
758         icon_x *icn = sp->icon;
759
760         __extract_data(data, lbl, NULL, icn, NULL, NULL, &label, NULL, &icon, NULL, NULL);
761         if (!icon && !label)
762                 return;
763         sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_app_localized_info(app_id, app_locale, " \
764                 "app_label, app_icon) values " \
765                 "('%q', '%q', '%q', '%q')", sp->appid, (char*)data,
766                 label, icon);
767         ret = __exec_query(query);
768         if (ret == -1)
769                 _LOGD("Package ServiceApp Localized Info DB Insert failed\n");
770 }
771
772 static int __insert_ui_mainapp_info(manifest_x *mfx)
773 {
774         uiapplication_x *up = mfx->uiapplication;
775         int ret = -1;
776         char query[MAX_QUERY_LEN] = {'\0'};
777         while(up != NULL)
778         {
779                 snprintf(query, MAX_QUERY_LEN,
780                         "update package_app_info set app_mainapp='%s' where app_id='%s'", up->mainapp, up->appid);
781
782                 ret = __exec_query(query);
783                 if (ret == -1) {
784                         _LOGD("Package UiApp Info DB Insert Failed\n");
785                         return -1;
786                 }
787                 if (strcasecmp(up->mainapp, "True")==0)
788                         mfx->mainapp_id = strdup(up->appid);
789
790                 up = up->next;
791                 memset(query, '\0', MAX_QUERY_LEN);
792         }
793
794         if (mfx->mainapp_id == NULL){
795                 if (mfx->uiapplication && mfx->uiapplication->appid) {
796                         snprintf(query, MAX_QUERY_LEN, "update package_app_info set app_mainapp='true' where app_id='%s'", mfx->uiapplication->appid);
797                 } else {
798                         _LOGD("Not valid appid\n");
799                         return -1;
800                 }
801
802                 ret = __exec_query(query);
803                 if (ret == -1) {
804                         _LOGD("Package UiApp Info DB Insert Failed\n");
805                         return -1;
806                 }
807
808                 free((void *)mfx->uiapplication->mainapp);
809                 mfx->uiapplication->mainapp= strdup("true");
810                 mfx->mainapp_id = strdup(mfx->uiapplication->appid);
811         }
812
813         memset(query, '\0', MAX_QUERY_LEN);
814         snprintf(query, MAX_QUERY_LEN,
815                 "update package_info set mainapp_id='%s' where package='%s'", mfx->mainapp_id, mfx->package);
816         ret = __exec_query(query);
817         if (ret == -1) {
818                 _LOGD("Package Info DB update Failed\n");
819                 return -1;
820         }
821
822         return 0;
823 }
824 /* _PRODUCT_LAUNCHING_ENHANCED_
825 *  up->indicatordisplay, up->portraitimg, up->landscapeimg, up->guestmode_appstatus
826 */
827 static int __insert_uiapplication_info(manifest_x *mfx)
828 {
829         uiapplication_x *up = mfx->uiapplication;
830         int ret = -1;
831         char query[MAX_QUERY_LEN] = {'\0'};
832         while(up != NULL)
833         {
834                 snprintf(query, MAX_QUERY_LEN,
835                          "insert into package_app_info(app_id, app_component, app_exec, app_nodisplay, app_type, app_onboot, " \
836                         "app_multiple, app_autorestart, app_taskmanage, app_enabled, app_hwacceleration, app_screenreader, app_mainapp , app_recentimage, " \
837                         "app_launchcondition, app_indicatordisplay, app_portraitimg, app_landscapeimg, app_guestmodevisibility, app_permissiontype, "\
838                         "app_preload, app_submode, app_submode_mainid, app_launch_mode, app_ui_gadget, app_support_disable, component_type, package) " \
839                         "values('%s', '%s', '%s', '%s', '%s', '%s', '%s','%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",\
840                          up->appid,
841                          "uiapp",
842                          up->exec,
843                          up->nodisplay,
844                          up->type,
845                          PKGMGR_PARSER_EMPTY_STR,
846                          up->multiple,
847                          PKGMGR_PARSER_EMPTY_STR,
848                          up->taskmanage,
849                          up->enabled,
850                          up->hwacceleration,
851                          up->screenreader,
852                          up->mainapp,
853                          __get_str(up->recentimage),
854                          up->launchcondition,
855                          up->indicatordisplay,
856                          __get_str(up->portraitimg),
857                          __get_str(up->landscapeimg),
858                          up->guestmode_visibility,
859                          up->permission_type,
860                          mfx->preload,
861                          up->submode,
862                          __get_str(up->submode_mainid),
863                          up->launch_mode,
864                          up->ui_gadget,
865                          mfx->support_disable,
866                          up->component_type,
867                          mfx->package);
868
869                 ret = __exec_query(query);
870                 if (ret == -1) {
871                         _LOGD("Package UiApp Info DB Insert Failed\n");
872                         return -1;
873                 }
874                 up = up->next;
875                 memset(query, '\0', MAX_QUERY_LEN);
876         }
877         return 0;
878 }
879
880 static int __insert_uiapplication_appcategory_info(manifest_x *mfx)
881 {
882         uiapplication_x *up = mfx->uiapplication;
883         category_x *ct = NULL;
884         int ret = -1;
885         char query[MAX_QUERY_LEN] = {'\0'};
886         while(up != NULL)
887         {
888                 ct = up->category;
889                 while (ct != NULL)
890                 {
891                         snprintf(query, MAX_QUERY_LEN,
892                                 "insert into package_app_app_category(app_id, category) " \
893                                 "values('%s','%s')",\
894                                  up->appid, ct->name);
895                         ret = __exec_query(query);
896                         if (ret == -1) {
897                                 _LOGD("Package UiApp Category Info DB Insert Failed\n");
898                                 return -1;
899                         }
900                         ct = ct->next;
901                         memset(query, '\0', MAX_QUERY_LEN);
902                 }
903                 up = up->next;
904         }
905         return 0;
906 }
907
908 static int __insert_uiapplication_appmetadata_info(manifest_x *mfx)
909 {
910         uiapplication_x *up = mfx->uiapplication;
911         metadata_x *md = NULL;
912         int ret = -1;
913         char query[MAX_QUERY_LEN] = {'\0'};
914         while(up != NULL)
915         {
916                 md = up->metadata;
917                 while (md != NULL)
918                 {
919                         if (md->key) {
920                                 snprintf(query, MAX_QUERY_LEN,
921                                         "insert into package_app_app_metadata(app_id, md_key, md_value) " \
922                                         "values('%s','%s', '%s')",\
923                                          up->appid, md->key, md->value ? md->value : "");
924                                 ret = __exec_query(query);
925                                 if (ret == -1) {
926                                         _LOGD("Package UiApp Metadata Info DB Insert Failed\n");
927                                         return -1;
928                                 }
929                         }
930                         md = md->next;
931                         memset(query, '\0', MAX_QUERY_LEN);
932                 }
933                 up = up->next;
934         }
935         return 0;
936 }
937
938 static int __insert_uiapplication_apppermission_info(manifest_x *mfx)
939 {
940         uiapplication_x *up = mfx->uiapplication;
941         permission_x *pm = NULL;
942         int ret = -1;
943         char query[MAX_QUERY_LEN] = {'\0'};
944         while(up != NULL)
945         {
946                 pm = up->permission;
947                 while (pm != NULL)
948                 {
949                         snprintf(query, MAX_QUERY_LEN,
950                                 "insert into package_app_app_permission(app_id, pm_type, pm_value) " \
951                                 "values('%s','%s', '%s')",\
952                                  up->appid, pm->type, pm->value);
953                         ret = __exec_query(query);
954                         if (ret == -1) {
955                                 _LOGD("Package UiApp permission Info DB Insert Failed\n");
956                                 return -1;
957                         }
958                         pm = pm->next;
959                         memset(query, '\0', MAX_QUERY_LEN);
960                 }
961                 up = up->next;
962         }
963         return 0;
964 }
965
966 static int __insert_uiapplication_appcontrol_info(manifest_x *mfx)
967 {
968         uiapplication_x *up = mfx->uiapplication;
969         appcontrol_x *acontrol = NULL;
970         int ret = -1;
971         char query[MAX_QUERY_LEN] = {'\0'};
972         char buf[BUFSIZE] = {'\0'};
973         while (up != NULL) {
974                 acontrol = up->appcontrol;
975                 while (acontrol != NULL) {
976                         snprintf(buf, BUFSIZE, "%s|%s|%s",\
977                                         acontrol->operation ? (strlen(acontrol->operation) > 0 ? acontrol->operation : "NULL") : "NULL",
978                                         acontrol->uri ? (strlen(acontrol->uri) > 0 ? acontrol->uri : "NULL") : "NULL",
979                                         acontrol->mime ? (strlen(acontrol->mime) > 0 ? acontrol->mime : "NULL") : "NULL");
980                         snprintf(query, MAX_QUERY_LEN,
981                                         "insert into package_app_app_control(app_id, app_control) " \
982                                         "values('%s', '%s')",\
983                                         up->appid, buf);
984                         ret = __exec_query(query);
985                         if (ret == -1) {
986                                 _LOGD("Package UiApp AppSvc DB Insert Failed\n");
987                                 return -1;
988                         }
989                         memset(query, '\0', MAX_QUERY_LEN);
990                         acontrol = acontrol->next;
991                 }
992                 up = up->next;
993         }
994         return 0;
995 }
996
997 static int __insert_uiapplication_appsvc_info(manifest_x *mfx)
998 {
999         uiapplication_x *up = mfx->uiapplication;
1000         appsvc_x *asvc = NULL;
1001         operation_x *op = NULL;
1002         mime_x *mi = NULL;
1003         uri_x *ui = NULL;
1004         subapp_x *sub = NULL;
1005         int ret = -1;
1006         char query[MAX_QUERY_LEN] = {'\0'};
1007         const char *operation = NULL;
1008         const char *mime = NULL;
1009         const char *uri = NULL;
1010         const char *subapp = NULL;
1011         while(up != NULL)
1012         {
1013                 asvc = up->appsvc;
1014                 while(asvc != NULL)
1015                 {
1016                         op = asvc->operation;
1017                         while(op != NULL)
1018                         {
1019                                 if (op)
1020                                         operation = op->name;
1021                                 mi = asvc->mime;
1022
1023                                 do
1024                                 {
1025                                         if (mi)
1026                                                 mime = mi->name;
1027                                         sub = asvc->subapp;
1028                                         do
1029                                         {
1030                                                 if (sub)
1031                                                         subapp = sub->name;
1032                                                 ui = asvc->uri;
1033                                                 do
1034                                                 {
1035                                                         if (ui)
1036                                                                 uri = ui->name;
1037                                                         snprintf(query, MAX_QUERY_LEN,
1038                                                                  "insert into package_app_app_svc(app_id, operation, uri_scheme, mime_type, subapp_name) " \
1039                                                                 "values('%s', '%s', '%s', '%s', '%s')",\
1040                                                                  up->appid,
1041                                                                  operation,
1042                                                                  __get_str(uri),
1043                                                                  __get_str(mime),
1044                                                                  __get_str(subapp));
1045
1046                                                         ret = __exec_query(query);
1047                                                         if (ret == -1) {
1048                                                                 _LOGD("Package UiApp AppSvc DB Insert Failed\n");
1049                                                                 return -1;
1050                                                         }
1051                                                         memset(query, '\0', MAX_QUERY_LEN);
1052                                                         if (ui)
1053                                                                 ui = ui->next;
1054                                                         uri = NULL;
1055                                                 } while(ui != NULL);
1056                                                 if (sub)
1057                                                         sub = sub->next;
1058                                                 subapp = NULL;
1059                                         }while(sub != NULL);
1060                                         if (mi)
1061                                                 mi = mi->next;
1062                                         mime = NULL;
1063                                 }while(mi != NULL);
1064                                 if (op)
1065                                         op = op->next;
1066                                 operation = NULL;
1067                         }
1068                         asvc = asvc->next;
1069                 }
1070                 up = up->next;
1071         }
1072         return 0;
1073 }
1074
1075 static int __insert_uiapplication_datacontrol_info(manifest_x *mfx)
1076 {
1077         uiapplication_x *up = mfx->uiapplication;
1078         datacontrol_x *dc = NULL;
1079         int ret = -1;
1080         char query[MAX_QUERY_LEN] = {'\0'};
1081
1082         while(up != NULL)
1083         {
1084                 dc = up->datacontrol;
1085                 while(dc != NULL)
1086                 {
1087                         snprintf(query, MAX_QUERY_LEN,
1088                                         "insert into package_app_data_control(app_id, providerid, access, type) " \
1089                                         "values('%s', '%s', '%s', '%s')",\
1090                                         mfx->uiapplication->appid,
1091                                         dc->providerid,
1092                                         dc->access,
1093                                         dc->type);
1094
1095                         ret = __exec_query(query);
1096                         if (ret == -1) {
1097                                 _LOGD("Package UiApp Data Control DB Insert Failed\n");
1098                                 return -1;
1099                         }
1100                         memset(query, '\0', MAX_QUERY_LEN);
1101                         dc = dc->next;
1102                 }
1103                 up = up->next;
1104         }
1105         return 0;
1106 }
1107
1108 static int __insert_uiapplication_share_request_info(manifest_x *mfx)
1109 {
1110         uiapplication_x *up = mfx->uiapplication;
1111         datashare_x *ds = NULL;
1112         request_x *rq = NULL;
1113         int ret = -1;
1114         char query[MAX_QUERY_LEN] = {'\0'};
1115         while(up != NULL)
1116         {
1117                 ds = up->datashare;
1118                 while(ds != NULL)
1119                 {
1120                         rq = ds->request;
1121                         while(rq != NULL)
1122                         {
1123                                 snprintf(query, MAX_QUERY_LEN,
1124                                          "insert into package_app_share_request(app_id, data_share_request) " \
1125                                         "values('%s', '%s')",\
1126                                          up->appid, rq->text);
1127                                 ret = __exec_query(query);
1128                                 if (ret == -1) {
1129                                         _LOGD("Package UiApp Share Request DB Insert Failed\n");
1130                                         return -1;
1131                                 }
1132                                 memset(query, '\0', MAX_QUERY_LEN);
1133                                 rq = rq->next;
1134                         }
1135                         ds = ds->next;
1136                 }
1137                 up = up->next;
1138         }
1139         return 0;
1140 }
1141
1142 static int __insert_uiapplication_share_allowed_info(manifest_x *mfx)
1143 {
1144         uiapplication_x *up = mfx->uiapplication;
1145         datashare_x *ds = NULL;
1146         define_x *df = NULL;
1147         allowed_x *al = NULL;
1148         int ret = -1;
1149         char query[MAX_QUERY_LEN] = {'\0'};
1150         while(up != NULL)
1151         {
1152                 ds = up->datashare;
1153                 while(ds != NULL)
1154                 {
1155                         df = ds->define;
1156                         while(df != NULL)
1157                         {
1158                                 al = df->allowed;
1159                                 while(al != NULL)
1160                                 {
1161                                         snprintf(query, MAX_QUERY_LEN,
1162                                                  "insert into package_app_share_allowed(app_id, data_share_path, data_share_allowed) " \
1163                                                 "values('%s', '%s', '%s')",\
1164                                                  up->appid, df->path, al->text);
1165                                         ret = __exec_query(query);
1166                                         if (ret == -1) {
1167                                                 _LOGD("Package UiApp Share Allowed DB Insert Failed\n");
1168                                                 return -1;
1169                                         }
1170                                         memset(query, '\0', MAX_QUERY_LEN);
1171                                         al = al->next;
1172                                 }
1173                                 df = df->next;
1174                         }
1175                         ds = ds->next;
1176                 }
1177                 up = up->next;
1178         }
1179         return 0;
1180 }
1181
1182 static int __insert_serviceapplication_info(manifest_x *mfx)
1183 {
1184         serviceapplication_x *sp = mfx->serviceapplication;
1185         int ret = -1;
1186         char query[MAX_QUERY_LEN] = {'\0'};
1187         while(sp != NULL)
1188         {
1189                 snprintf(query, MAX_QUERY_LEN,
1190                          "insert into package_app_info(app_id, app_component, app_exec, app_type, app_onboot, " \
1191                         "app_multiple, app_autorestart, app_enabled, app_permissiontype, package) " \
1192                         "values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",\
1193                          sp->appid, "svcapp", sp->exec, sp->type, sp->onboot, "\0",
1194                          sp->autorestart, sp->enabled, sp->permission_type, mfx->package);
1195                 ret = __exec_query(query);
1196                 if (ret == -1) {
1197                         _LOGD("Package ServiceApp Info DB Insert Failed\n");
1198                         return -1;
1199                 }
1200                 sp = sp->next;
1201                 memset(query, '\0', MAX_QUERY_LEN);
1202         }
1203         return 0;
1204 }
1205
1206 static int __insert_serviceapplication_appcategory_info(manifest_x *mfx)
1207 {
1208         serviceapplication_x *sp = mfx->serviceapplication;
1209         category_x *ct = NULL;
1210         int ret = -1;
1211         char query[MAX_QUERY_LEN] = {'\0'};
1212         while(sp != NULL)
1213         {
1214                 ct = sp->category;
1215                 while (ct != NULL)
1216                 {
1217                         snprintf(query, MAX_QUERY_LEN,
1218                                 "insert into package_app_app_category(app_id, category) " \
1219                                 "values('%s','%s')",\
1220                                  sp->appid, ct->name);
1221                         ret = __exec_query(query);
1222                         if (ret == -1) {
1223                                 _LOGD("Package ServiceApp Category Info DB Insert Failed\n");
1224                                 return -1;
1225                         }
1226                         ct = ct->next;
1227                         memset(query, '\0', MAX_QUERY_LEN);
1228                 }
1229                 sp = sp->next;
1230         }
1231         return 0;
1232 }
1233
1234 static int __insert_serviceapplication_appmetadata_info(manifest_x *mfx)
1235 {
1236         serviceapplication_x *sp = mfx->serviceapplication;
1237         metadata_x *md = NULL;
1238         int ret = -1;
1239         char query[MAX_QUERY_LEN] = {'\0'};
1240         while(sp != NULL)
1241         {
1242                 md = sp->metadata;
1243                 while (md != NULL)
1244                 {
1245                         if (md->key) {
1246                                 snprintf(query, MAX_QUERY_LEN,
1247                                         "insert into package_app_app_metadata(app_id, md_key, md_value) " \
1248                                         "values('%s','%s', '%s')",\
1249                                          sp->appid, md->key, md->value ? md->value : "");
1250                                 ret = __exec_query(query);
1251                                 if (ret == -1) {
1252                                         _LOGD("Package ServiceApp Metadata Info DB Insert Failed\n");
1253                                         return -1;
1254                                 }
1255                         }
1256                         md = md->next;
1257                         memset(query, '\0', MAX_QUERY_LEN);
1258                 }
1259                 sp = sp->next;
1260         }
1261         return 0;
1262 }
1263
1264 static int __insert_serviceapplication_apppermission_info(manifest_x *mfx)
1265 {
1266         serviceapplication_x *sp = mfx->serviceapplication;
1267         permission_x *pm = NULL;
1268         int ret = -1;
1269         char query[MAX_QUERY_LEN] = {'\0'};
1270         while(sp != NULL)
1271         {
1272                 pm = sp->permission;
1273                 while (pm != NULL)
1274                 {
1275                         snprintf(query, MAX_QUERY_LEN,
1276                                 "insert into package_app_app_permission(app_id, pm_type, pm_value) " \
1277                                 "values('%s','%s', '%s')",\
1278                                  sp->appid, pm->type, pm->value);
1279                         ret = __exec_query(query);
1280                         if (ret == -1) {
1281                                 _LOGD("Package ServiceApp permission Info DB Insert Failed\n");
1282                                 return -1;
1283                         }
1284                         pm = pm->next;
1285                         memset(query, '\0', MAX_QUERY_LEN);
1286                 }
1287                 sp = sp->next;
1288         }
1289         return 0;
1290 }
1291
1292 static int __insert_serviceapplication_appcontrol_info(manifest_x *mfx)
1293 {
1294         serviceapplication_x *sp = mfx->serviceapplication;
1295         appcontrol_x *acontrol = NULL;
1296         int ret = -1;
1297         char query[MAX_QUERY_LEN] = {'\0'};
1298         char buf[BUFSIZE] = {'\0'};
1299         while (sp != NULL) {
1300                 acontrol = sp->appcontrol;
1301                 while (acontrol != NULL) {
1302                         snprintf(buf, BUFSIZE, "%s|%s|%s",
1303                                         acontrol->operation ? (strlen(acontrol->operation) > 0 ? acontrol->operation : "NULL") : "NULL",
1304                                         acontrol->uri ? (strlen(acontrol->uri) > 0 ? acontrol->uri : "NULL") : "NULL",
1305                                         acontrol->mime ? (strlen(acontrol->mime) > 0 ? acontrol->mime : "NULL") : "NULL");
1306                         snprintf(query, MAX_QUERY_LEN,
1307                                         "insert into package_app_app_control(app_id, app_control) " \
1308                                         "values('%s', '%s')",\
1309                                         sp->appid, buf);
1310                         ret = __exec_query(query);
1311                         if (ret == -1) {
1312                                 _LOGD("Package UiApp AppSvc DB Insert Failed\n");
1313                                 return -1;
1314                         }
1315                         memset(query, '\0', MAX_QUERY_LEN);
1316                         acontrol = acontrol->next;
1317                 }
1318                 sp = sp->next;
1319         }
1320         return 0;
1321 }
1322
1323 static int __insert_serviceapplication_appsvc_info(manifest_x *mfx)
1324 {
1325         serviceapplication_x *sp = mfx->serviceapplication;
1326         appsvc_x *asvc = NULL;
1327         int ret = -1;
1328         char query[MAX_QUERY_LEN] = {'\0'};
1329         operation_x *op = NULL;
1330         mime_x *mi = NULL;
1331         uri_x *ui = NULL;
1332         subapp_x *sub = NULL;
1333         const char *operation = NULL;
1334         const char *mime = NULL;
1335         const char *uri = NULL;
1336         const char *subapp = NULL;
1337         while(sp != NULL)
1338         {
1339                 asvc = sp->appsvc;
1340                 while(asvc != NULL)
1341                 {
1342                         op = asvc->operation;
1343                         while(op != NULL)
1344                         {
1345                         if (op)
1346                                 operation = op->name;
1347                         mi = asvc->mime;
1348                                 do
1349                                 {
1350                                 if (mi)
1351                                         mime = mi->name;
1352                                 sub = asvc->subapp;
1353                                         do
1354                                         {
1355                                         if (sub)
1356                                                 subapp = sub->name;
1357                                         ui = asvc->uri;
1358                                                         do
1359                                                         {
1360                                                                 if (ui)
1361                                                                         uri = ui->name;
1362                                                                 snprintf(query, MAX_QUERY_LEN,
1363                                                                          "insert into package_app_app_svc(app_id, operation, uri_scheme, mime_type, subapp_name) " \
1364                                                                         "values('%s', '%s', '%s', '%s', '%s')",\
1365                                                                          sp->appid,
1366                                                                          operation,
1367                                                                         __get_str(uri),
1368                                                                         __get_str(mime),
1369                                                                         __get_str(subapp));
1370                                                                 ret = __exec_query(query);
1371                                                                 if (ret == -1) {
1372                                                                         _LOGD("Package UiApp AppSvc DB Insert Failed\n");
1373                                                                         return -1;
1374                                                                 }
1375                                                                 memset(query, '\0', MAX_QUERY_LEN);
1376                                                                 if (ui)
1377                                                                         ui = ui->next;
1378                                                                 uri = NULL;
1379                                                         } while(ui != NULL);
1380                                                 if (sub)
1381                                                         sub     = sub->next;
1382                                                 subapp = NULL;
1383                                         }while(sub != NULL);
1384                                         if (mi)
1385                                                 mi = mi->next;
1386                                         mime = NULL;
1387                                 }while(mi != NULL);
1388                                 if (op)
1389                                         op = op->next;
1390                                 operation = NULL;
1391                         }
1392                         asvc = asvc->next;
1393                 }
1394                 sp = sp->next;
1395         }
1396         return 0;
1397 }
1398
1399 static int __insert_serviceapplication_datacontrol_info(manifest_x *mfx)
1400 {
1401         serviceapplication_x *sp = mfx->serviceapplication;
1402         datacontrol_x *dc = NULL;
1403         int ret = -1;
1404         char query[MAX_QUERY_LEN] = {'\0'};
1405
1406         while(sp != NULL)
1407         {
1408                 dc = sp->datacontrol;
1409                 while(dc != NULL)
1410                 {
1411                         snprintf(query, MAX_QUERY_LEN,
1412                                         "insert into package_app_data_control(app_id, providerid, access, type) " \
1413                                         "values('%s', '%s', '%s', '%s')",\
1414                                         mfx->serviceapplication->appid,
1415                                         dc->providerid,
1416                                         dc->access,
1417                                         dc->type);
1418
1419                         ret = __exec_query(query);
1420                         if (ret == -1) {
1421                                 _LOGD("Package ServiceApp Data Control DB Insert Failed\n");
1422                                 return -1;
1423                         }
1424                         memset(query, '\0', MAX_QUERY_LEN);
1425                         dc = dc->next;
1426                 }
1427                 sp = sp->next;
1428         }
1429         return 0;
1430 }
1431
1432 static int __insert_serviceapplication_share_request_info(manifest_x *mfx)
1433 {
1434         serviceapplication_x *sp = mfx->serviceapplication;
1435         datashare_x *ds = NULL;
1436         request_x *rq = NULL;
1437         int ret = -1;
1438         char query[MAX_QUERY_LEN] = {'\0'};
1439         while(sp != NULL)
1440         {
1441                 ds = sp->datashare;
1442                 while(ds != NULL)
1443                 {
1444                         rq = ds->request;
1445                         while(rq != NULL)
1446                         {
1447                                 snprintf(query, MAX_QUERY_LEN,
1448                                          "insert into package_app_share_request(app_id, data_share_request) " \
1449                                         "values('%s', '%s')",\
1450                                          sp->appid, rq->text);
1451                                 ret = __exec_query(query);
1452                                 if (ret == -1) {
1453                                         _LOGD("Package ServiceApp Share Request DB Insert Failed\n");
1454                                         return -1;
1455                                 }
1456                                 memset(query, '\0', MAX_QUERY_LEN);
1457                                 rq = rq->next;
1458                         }
1459                         ds = ds->next;
1460                 }
1461                 sp = sp->next;
1462         }
1463         return 0;
1464 }
1465
1466
1467
1468 static int __insert_serviceapplication_share_allowed_info(manifest_x *mfx)
1469 {
1470         serviceapplication_x *sp = mfx->serviceapplication;
1471         datashare_x *ds = NULL;
1472         define_x *df = NULL;
1473         allowed_x *al = NULL;
1474         int ret = -1;
1475         char query[MAX_QUERY_LEN] = {'\0'};
1476         while(sp != NULL)
1477         {
1478                 ds = sp->datashare;
1479                 while(ds != NULL)
1480                 {
1481                         df = ds->define;
1482                         while(df != NULL)
1483                         {
1484                                 al = df->allowed;
1485                                 while(al != NULL)
1486                                 {
1487                                         snprintf(query, MAX_QUERY_LEN,
1488                                                  "insert into package_app_share_allowed(app_id, data_share_path, data_share_allowed) " \
1489                                                 "values('%s', '%s', '%s')",\
1490                                                  sp->appid, df->path, al->text);
1491                                         ret = __exec_query(query);
1492                                         if (ret == -1) {
1493                                                 _LOGD("Package App Share Allowed DB Insert Failed\n");
1494                                                 return -1;
1495                                         }
1496                                         memset(query, '\0', MAX_QUERY_LEN);
1497                                         al = al->next;
1498                                 }
1499                                 df = df->next;
1500                         }
1501                         ds = ds->next;
1502                 }
1503                 sp = sp->next;
1504         }
1505         return 0;
1506 }
1507
1508 static int __insert_manifest_info_in_db(manifest_x *mfx, uid_t uid)
1509 {
1510         label_x *lbl = mfx->label;
1511         license_x *lcn = mfx->license;
1512         icon_x *icn = mfx->icon;
1513         description_x *dcn = mfx->description;
1514         author_x *ath = mfx->author;
1515         uiapplication_x *up = mfx->uiapplication;
1516         uiapplication_x *up_icn = mfx->uiapplication;
1517         uiapplication_x *up_image = mfx->uiapplication;
1518         serviceapplication_x *sp = mfx->serviceapplication;
1519         privileges_x *pvs = NULL;
1520         privilege_x *pv = NULL;
1521         char query[MAX_QUERY_LEN] = { '\0' };
1522         int ret = -1;
1523         const char *auth_name = NULL;
1524         const char *auth_email = NULL;
1525         const char *auth_href = NULL;
1526
1527         GList *pkglocale = NULL;
1528         GList *applocale = NULL;
1529         GList *appicon = NULL;
1530         GList *appimage = NULL;
1531
1532         if (ath) {
1533                 if (ath->text)
1534                         auth_name = ath->text;
1535                 if (ath->email)
1536                         auth_email = ath->email;
1537                 if (ath->href)
1538                         auth_href = ath->href;
1539         }
1540         /*Insert in the package_cert_info CERT_DB*/
1541         pkgmgrinfo_instcertinfo_h cert_handle = NULL;
1542         ret = pkgmgrinfo_set_cert_value(&cert_handle, PMINFO_SET_AUTHOR_ROOT_CERT, "author root certificate");
1543         if (ret != PMINFO_R_OK) {
1544                 pkgmgrinfo_destroy_certinfo_set_handle(cert_handle);
1545                 _LOGE("Cert Info DB create handle failed\n");
1546                 return -1;
1547         }
1548         ret = pkgmgrinfo_save_certinfo(mfx->package, &cert_handle, uid);
1549         if (ret != PMINFO_R_OK) {
1550                 pkgmgrinfo_destroy_certinfo_set_handle(cert_handle);
1551                 _LOGE("Cert Info DB Insert Failed\n");
1552                 return -1;
1553         }
1554
1555         /*Insert in the package_info DB*/
1556         snprintf(query, MAX_QUERY_LEN,
1557                  "insert into package_info(package, package_type, package_version, package_api_version, install_location, package_size, " \
1558                 "package_removable, package_preload, package_readonly, package_update, package_appsetting, package_nodisplay, package_system," \
1559                 "author_name, author_email, author_href, installed_time, installed_storage, storeclient_id, mainapp_id, package_url, root_path, csc_path, package_support_disable) " \
1560                 "values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",\
1561                  mfx->package,
1562                  mfx->type,
1563                  mfx->version,
1564                  __get_str(mfx->api_version),
1565                  __get_str(mfx->installlocation),
1566                  __get_str(mfx->package_size),
1567                  mfx->removable,
1568                  mfx->preload,
1569                  mfx->readonly,
1570                  mfx->update,
1571                  mfx->appsetting,
1572                  mfx->nodisplay_setting,
1573                  mfx->system,
1574                  __get_str(auth_name),
1575                  __get_str(auth_email),
1576                  __get_str(auth_href),
1577                  mfx->installed_time,
1578                  mfx->installed_storage,
1579                  __get_str(mfx->storeclient_id),
1580                  mfx->mainapp_id,
1581                  __get_str(mfx->package_url),
1582                  mfx->root_path,
1583                  __get_str(mfx->csc_path),
1584                  mfx->support_disable);
1585
1586         ret = __exec_query(query);
1587         if (ret == -1) {
1588                 _LOGD("Package Info DB Insert Failed\n");
1589                 return -1;
1590         }
1591
1592         /*Insert in the package_privilege_info DB*/
1593         pvs = mfx->privileges;
1594         while (pvs != NULL) {
1595                 pv = pvs->privilege;
1596                 while (pv != NULL) {
1597                         memset(query, '\0', MAX_QUERY_LEN);
1598                         snprintf(query, MAX_QUERY_LEN,
1599                                 "insert into package_privilege_info(package, privilege) " \
1600                                 "values('%s','%s')",\
1601                                  mfx->package, pv->text);
1602                         ret = __exec_query(query);
1603                         if (ret == -1) {
1604                                 _LOGD("Package Privilege Info DB Insert Failed\n");
1605                                 return -1;
1606                         }
1607                         pv = pv->next;
1608                 }
1609                 pvs = pvs->next;
1610         }
1611
1612         if (up != NULL) {
1613                 ret = __insert_ui_mainapp_info(mfx);
1614                 if (ret == -1)
1615                         return -1;
1616         }
1617
1618         /*Insert the package locale*/
1619         pkglocale = __create_locale_list(pkglocale, lbl, lcn, icn, dcn, ath);
1620         /*remove duplicated data in pkglocale*/
1621         __trimfunc(pkglocale);
1622
1623         /*Insert the app locale info */
1624         while(up != NULL)
1625         {
1626                 applocale = __create_locale_list(applocale, up->label, NULL, up->icon, NULL, NULL);
1627                 up = up->next;
1628         }
1629         while(sp != NULL)
1630         {
1631                 applocale = __create_locale_list(applocale, sp->label, NULL, sp->icon, NULL, NULL);
1632                 sp = sp->next;
1633         }
1634         /*remove duplicated data in applocale*/
1635         __trimfunc(applocale);
1636
1637         /*Insert the app icon info */
1638         while(up_icn != NULL)
1639         {
1640                 appicon = __create_icon_list(appicon, up_icn->icon);
1641                 up_icn = up_icn->next;
1642         }
1643         /*remove duplicated data in appicon*/
1644         __trimfunc(appicon);
1645
1646         /*Insert the image info */
1647         while(up_image != NULL)
1648         {
1649                 appimage = __create_image_list(appimage, up_image->image);
1650                 up_image = up_image->next;
1651         }
1652         /*remove duplicated data in appimage*/
1653         __trimfunc(appimage);
1654
1655         g_list_foreach(pkglocale, __insert_pkglocale_info, (gpointer)mfx);
1656
1657         /*native app locale info*/
1658         up = mfx->uiapplication;
1659         while(up != NULL)
1660         {
1661                 g_list_foreach(applocale, __insert_uiapplication_locale_info, (gpointer)up);
1662                 up = up->next;
1663         }
1664         /*agent app locale info*/
1665         sp = mfx->serviceapplication;
1666         while(sp != NULL)
1667         {
1668                 g_list_foreach(applocale, __insert_serviceapplication_locale_info, (gpointer)sp);
1669                 sp = sp->next;
1670         }
1671
1672         /*app icon locale info*/
1673         up_icn = mfx->uiapplication;
1674         while(up_icn != NULL)
1675         {
1676                 g_list_foreach(appicon, __insert_uiapplication_icon_section_info, (gpointer)up_icn);
1677                 up_icn = up_icn->next;
1678         }
1679
1680         /*app image info*/
1681         up_image = mfx->uiapplication;
1682         while(up_image != NULL)
1683         {
1684                 g_list_foreach(appimage, __insert_uiapplication_image_info, (gpointer)up_image);
1685                 up_image = up_image->next;
1686         }
1687
1688         g_list_free(pkglocale);
1689         pkglocale = NULL;
1690         g_list_free(applocale);
1691         applocale = NULL;
1692         g_list_free(appicon);
1693         appicon = NULL;
1694         g_list_free(appimage);
1695         appimage = NULL;
1696
1697         /*Insert in the package_app_info DB*/
1698         ret = __insert_uiapplication_info(mfx);
1699         if (ret == -1)
1700                 return -1;
1701         ret = __insert_serviceapplication_info(mfx);
1702         if (ret == -1)
1703                 return -1;
1704         /*Insert in the package_app_app_control DB*/
1705         ret = __insert_uiapplication_appcontrol_info(mfx);
1706         if (ret == -1)
1707                 return -1;
1708         ret = __insert_serviceapplication_appcontrol_info(mfx);
1709         if (ret == -1)
1710                 return -1;
1711
1712         /*Insert in the package_app_app_category DB*/
1713         ret = __insert_uiapplication_appcategory_info(mfx);
1714         if (ret == -1)
1715                 return -1;
1716         ret = __insert_serviceapplication_appcategory_info(mfx);
1717         if (ret == -1)
1718                 return -1;
1719
1720         /*Insert in the package_app_app_metadata DB*/
1721         ret = __insert_uiapplication_appmetadata_info(mfx);
1722         if (ret == -1)
1723                 return -1;
1724         ret = __insert_serviceapplication_appmetadata_info(mfx);
1725         if (ret == -1)
1726                 return -1;
1727
1728         /*Insert in the package_app_app_permission DB*/
1729         ret = __insert_uiapplication_apppermission_info(mfx);
1730         if (ret == -1)
1731                 return -1;
1732         ret = __insert_serviceapplication_apppermission_info(mfx);
1733         if (ret == -1)
1734                 return -1;
1735
1736         /*Insert in the package_app_app_svc DB*/
1737         ret = __insert_uiapplication_appsvc_info(mfx);
1738         if (ret == -1)
1739                 return -1;
1740         ret = __insert_serviceapplication_appsvc_info(mfx);
1741         if (ret == -1)
1742                 return -1;
1743
1744         /*Insert in the package_app_share_allowed DB*/
1745         ret = __insert_uiapplication_share_allowed_info(mfx);
1746         if (ret == -1)
1747                 return -1;
1748         ret = __insert_serviceapplication_share_allowed_info(mfx);
1749         if (ret == -1)
1750                 return -1;
1751
1752         /*Insert in the package_app_share_request DB*/
1753         ret = __insert_uiapplication_share_request_info(mfx);
1754         if (ret == -1)
1755                 return -1;
1756         ret = __insert_serviceapplication_share_request_info(mfx);
1757         if (ret == -1)
1758                 return -1;
1759
1760         /*Insert in the package_app_data_control DB*/
1761         ret = __insert_uiapplication_datacontrol_info(mfx);
1762         if (ret == -1)
1763                 return -1;
1764         ret = __insert_serviceapplication_datacontrol_info(mfx);
1765         if (ret == -1)
1766                 return -1;
1767
1768         return 0;
1769
1770 }
1771
1772 static int __delete_appinfo_from_db(char *db_table, const char *appid)
1773 {
1774         char query[MAX_QUERY_LEN] = { '\0' };
1775         int ret = -1;
1776         memset(query, '\0', MAX_QUERY_LEN);
1777         snprintf(query, MAX_QUERY_LEN,
1778                  "delete from %s where app_id='%s'", db_table, appid);
1779         ret = __exec_query(query);
1780         if (ret == -1) {
1781                 _LOGD("DB Deletion from table (%s) Failed\n", db_table);
1782                 return -1;
1783         }
1784         return 0;
1785 }
1786
1787 static int __delete_subpkg_info_from_db(char *appid)
1788 {
1789         int ret = -1;
1790
1791         ret = __delete_appinfo_from_db("package_app_info", appid);
1792         if (ret < 0)
1793                 return ret;
1794         ret = __delete_appinfo_from_db("package_app_localized_info", appid);
1795         if (ret < 0)
1796                 return ret;
1797         ret = __delete_appinfo_from_db("package_app_icon_section_info", appid);
1798         if (ret < 0)
1799                 return ret;
1800         ret = __delete_appinfo_from_db("package_app_image_info", appid);
1801         if (ret < 0)
1802                 return ret;
1803         ret = __delete_appinfo_from_db("package_app_app_svc", appid);
1804         if (ret < 0)
1805                 return ret;
1806         ret = __delete_appinfo_from_db("package_app_app_control", appid);
1807         if (ret < 0)
1808                 return ret;
1809         ret = __delete_appinfo_from_db("package_app_app_category", appid);
1810         if (ret < 0)
1811                 return ret;
1812         ret = __delete_appinfo_from_db("package_app_app_metadata", appid);
1813         if (ret < 0)
1814                 return ret;
1815         ret = __delete_appinfo_from_db("package_app_app_permission", appid);
1816         if (ret < 0)
1817                 return ret;
1818         ret = __delete_appinfo_from_db("package_app_share_allowed", appid);
1819         if (ret < 0)
1820                 return ret;
1821         ret = __delete_appinfo_from_db("package_app_share_request", appid);
1822         if (ret < 0)
1823                 return ret;
1824         ret = __delete_appinfo_from_db("package_app_data_control", appid);
1825         if (ret < 0)
1826                 return ret;
1827
1828         return 0;
1829 }
1830
1831 static int __delete_subpkg_from_db(manifest_x *mfx)
1832 {
1833         char query[MAX_QUERY_LEN] = { '\0' };
1834         char *error_message = NULL;
1835
1836         snprintf(query, MAX_QUERY_LEN, "select app_id from package_app_info where package='%s'", mfx->package);
1837         if (SQLITE_OK !=
1838             sqlite3_exec(pkgmgr_parser_db, query, __delete_subpkg_list_cb, NULL, &error_message)) {
1839                 _LOGE("Don't execute query = %s error message = %s\n", query,
1840                        error_message);
1841                 sqlite3_free(error_message);
1842                 return -1;
1843         }
1844         sqlite3_free(error_message);
1845
1846         return 0;
1847 }
1848
1849 static int __delete_manifest_info_from_db(manifest_x *mfx, uid_t uid)
1850 {
1851         char query[MAX_QUERY_LEN] = { '\0' };
1852         int ret = -1;
1853         uiapplication_x *up = mfx->uiapplication;
1854         serviceapplication_x *sp = mfx->serviceapplication;
1855         /*Delete from cert table*/
1856         if (uid != GLOBAL_USER)
1857                 ret = pkgmgrinfo_delete_usr_certinfo(mfx->package, uid);
1858         else
1859                 ret = pkgmgrinfo_delete_certinfo(mfx->package);
1860         if (ret) {
1861                 _LOGD("Cert Info  DB Delete Failed\n");
1862                 return -1;
1863         }
1864
1865         /*Delete from Package Info DB*/
1866         snprintf(query, MAX_QUERY_LEN,
1867                  "delete from package_info where package='%s'", mfx->package);
1868         ret = __exec_query(query);
1869         if (ret == -1) {
1870                 _LOGD("Package Info DB Delete Failed\n");
1871                 return -1;
1872         }
1873         memset(query, '\0', MAX_QUERY_LEN);
1874
1875         /*Delete from Package Localized Info*/
1876         snprintf(query, MAX_QUERY_LEN,
1877                  "delete from package_localized_info where package='%s'", mfx->package);
1878         ret = __exec_query(query);
1879         if (ret == -1) {
1880                 _LOGD("Package Localized Info DB Delete Failed\n");
1881                 return -1;
1882         }
1883
1884         /*Delete from Package Privilege Info*/
1885         snprintf(query, MAX_QUERY_LEN,
1886                  "delete from package_privilege_info where package='%s'", mfx->package);
1887         ret = __exec_query(query);
1888         if (ret == -1) {
1889                 _LOGD("Package Privilege Info DB Delete Failed\n");
1890                 return -1;
1891         }
1892
1893         while (up != NULL) {
1894                 ret = __delete_appinfo_from_db("package_app_info", up->appid);
1895                 if (ret < 0)
1896                         return ret;
1897                 ret = __delete_appinfo_from_db("package_app_localized_info", up->appid);
1898                 if (ret < 0)
1899                         return ret;
1900                 ret = __delete_appinfo_from_db("package_app_icon_section_info", up->appid);
1901                 if (ret < 0)
1902                         return ret;
1903                 ret = __delete_appinfo_from_db("package_app_image_info", up->appid);
1904                 if (ret < 0)
1905                         return ret;
1906                 ret = __delete_appinfo_from_db("package_app_app_svc", up->appid);
1907                 if (ret < 0)
1908                         return ret;
1909                 ret = __delete_appinfo_from_db("package_app_app_control", up->appid);
1910                 if (ret < 0)
1911                         return ret;
1912                 ret = __delete_appinfo_from_db("package_app_app_category", up->appid);
1913                 if (ret < 0)
1914                         return ret;
1915                 ret = __delete_appinfo_from_db("package_app_app_metadata", up->appid);
1916                 if (ret < 0)
1917                         return ret;
1918                 ret = __delete_appinfo_from_db("package_app_app_permission", up->appid);
1919                 if (ret < 0)
1920                         return ret;
1921                 ret = __delete_appinfo_from_db("package_app_share_allowed", up->appid);
1922                 if (ret < 0)
1923                         return ret;
1924                 ret = __delete_appinfo_from_db("package_app_share_request", up->appid);
1925                 if (ret < 0)
1926                         return ret;
1927                 ret = __delete_appinfo_from_db("package_app_data_control", up->appid);
1928                 if (ret < 0)
1929                         return ret;
1930                 up = up->next;
1931         }
1932
1933         while (sp != NULL) {
1934                 ret = __delete_appinfo_from_db("package_app_info", sp->appid);
1935                 if (ret < 0)
1936                         return ret;
1937                 ret = __delete_appinfo_from_db("package_app_localized_info", sp->appid);
1938                 if (ret < 0)
1939                         return ret;
1940                 ret = __delete_appinfo_from_db("package_app_icon_section_info", sp->appid);
1941                 if (ret < 0)
1942                         return ret;
1943                 ret = __delete_appinfo_from_db("package_app_image_info", sp->appid);
1944                 if (ret < 0)
1945                         return ret;
1946                 ret = __delete_appinfo_from_db("package_app_app_svc", sp->appid);
1947                 if (ret < 0)
1948                         return ret;
1949                 ret = __delete_appinfo_from_db("package_app_app_control", sp->appid);
1950                 if (ret < 0)
1951                         return ret;
1952                 ret = __delete_appinfo_from_db("package_app_app_category", sp->appid);
1953                 if (ret < 0)
1954                         return ret;
1955                 ret = __delete_appinfo_from_db("package_app_app_metadata", sp->appid);
1956                 if (ret < 0)
1957                         return ret;
1958                 ret = __delete_appinfo_from_db("package_app_app_permission", sp->appid);
1959                 if (ret < 0)
1960                         return ret;
1961                 ret = __delete_appinfo_from_db("package_app_share_allowed", sp->appid);
1962                 if (ret < 0)
1963                         return ret;
1964                 ret = __delete_appinfo_from_db("package_app_share_request", sp->appid);
1965                 if (ret < 0)
1966                         return ret;
1967                 ret = __delete_appinfo_from_db("package_app_data_control", sp->appid);
1968                 if (ret < 0)
1969                         return ret;
1970                 sp = sp->next;
1971         }
1972
1973         /* if main package has sub pkg, delete sub pkg data*/
1974         __delete_subpkg_from_db(mfx);
1975
1976         return 0;
1977 }
1978
1979 static int __update_preload_condition_in_db()
1980 {
1981         int ret = -1;
1982         char query[MAX_QUERY_LEN] = {'\0'};
1983
1984         snprintf(query, MAX_QUERY_LEN, "update package_info set package_preload='true'");
1985
1986         ret = __exec_query(query);
1987         if (ret == -1)
1988                 _LOGD("Package preload_condition update failed\n");
1989
1990         return ret;
1991 }
1992
1993 API int pkgmgr_parser_initialize_db(uid_t uid)
1994 {
1995         int ret = -1;
1996         /*Manifest DB*/
1997         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_INFO);
1998         if (ret == -1) {
1999                 _LOGD("package info DB initialization failed\n");
2000                 return ret;
2001         }
2002         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_LOCALIZED_INFO);
2003         if (ret == -1) {
2004                 _LOGD("package localized info DB initialization failed\n");
2005                 return ret;
2006         }
2007         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_PRIVILEGE_INFO);
2008         if (ret == -1) {
2009                 _LOGD("package app app privilege DB initialization failed\n");
2010                 return ret;
2011         }
2012         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_INFO);
2013         if (ret == -1) {
2014                 _LOGD("package app info DB initialization failed\n");
2015                 return ret;
2016         }
2017         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_LOCALIZED_INFO);
2018         if (ret == -1) {
2019                 _LOGD("package app localized info DB initialization failed\n");
2020                 return ret;
2021         }
2022         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_ICON_SECTION_INFO);
2023         if (ret == -1) {
2024                 _LOGD("package app icon localized info DB initialization failed\n");
2025                 return ret;
2026         }
2027         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_IMAGE_INFO);
2028         if (ret == -1) {
2029                 _LOGD("package app image info DB initialization failed\n");
2030                 return ret;
2031         }
2032         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_CONTROL);
2033         if (ret == -1) {
2034                 _LOGD("package app app control DB initialization failed\n");
2035                 return ret;
2036         }
2037         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_CATEGORY);
2038         if (ret == -1) {
2039                 _LOGD("package app app category DB initialization failed\n");
2040                 return ret;
2041         }
2042         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_METADATA);
2043         if (ret == -1) {
2044                 _LOGD("package app app category DB initialization failed\n");
2045                 return ret;
2046         }
2047         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_PERMISSION);
2048         if (ret == -1) {
2049                 _LOGD("package app app permission DB initialization failed\n");
2050                 return ret;
2051         }
2052         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_SVC);
2053         if (ret == -1) {
2054                 _LOGD("package app app svc DB initialization failed\n");
2055                 return ret;
2056         }
2057         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_ALLOWED);
2058         if (ret == -1) {
2059                 _LOGD("package app share allowed DB initialization failed\n");
2060                 return ret;
2061         }
2062         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_REQUEST);
2063         if (ret == -1) {
2064                 _LOGD("package app share request DB initialization failed\n");
2065                 return ret;
2066         }
2067         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_DATA_CONTROL);
2068         if (ret == -1) {
2069                 _LOGD("package app data control DB initialization failed\n");
2070                 return ret;
2071         }
2072         /*Cert DB*/
2073         ret = __initialize_db(pkgmgr_cert_db, QUERY_CREATE_TABLE_PACKAGE_CERT_INFO);
2074         if (ret == -1) {
2075                 _LOGD("package cert info DB initialization failed\n");
2076                 return ret;
2077         }
2078         ret = __initialize_db(pkgmgr_cert_db, QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO);
2079         if (ret == -1) {
2080                 _LOGD("package cert index info DB initialization failed\n");
2081                 return ret;
2082         }
2083         
2084         if( 0 != __parserdb_change_perm(getUserPkgCertDBPathUID(uid), uid)) {
2085                 _LOGD("Failed to change cert db permission\n");
2086         }
2087         if( 0 != __parserdb_change_perm(getUserPkgParserDBPathUID(uid), uid)) {
2088                 _LOGD("Failed to change parser db permission\n");
2089         }
2090
2091         return 0;
2092 }
2093
2094 static int __parserdb_change_perm(const char *db_file, uid_t uid)
2095 {
2096         char buf[BUFSIZE];
2097         char journal_file[BUFSIZE];
2098         char *files[3];
2099         int ret, i;
2100         struct passwd *userinfo = NULL;
2101         files[0] = (char *)db_file;
2102         files[1] = journal_file;
2103         files[2] = NULL;
2104
2105         if (db_file == NULL)
2106                 return -1;
2107
2108         if (getuid() != OWNER_ROOT) //At this time we should be root to apply this
2109                 return 0;
2110         snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal");
2111         if (uid == OWNER_ROOT)
2112                 uid = GLOBAL_USER;
2113         userinfo = getpwuid(uid);
2114         if (!userinfo) {
2115                 _LOGE("FAIL: user %d doesn't exist", uid);
2116                 return -1;
2117         }
2118         snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal");
2119
2120         for (i = 0; files[i]; i++) {
2121                 ret = chown(files[i], uid, userinfo->pw_gid);
2122                 if (ret == -1) {
2123                         if (strerror_r(errno, buf, sizeof(buf)))
2124                                 strcpy(buf, "");
2125                         _LOGD("FAIL : chown %s %d.%d : %s", files[i], uid,
2126                                         userinfo->pw_gid, buf);
2127                         return -1;
2128                 }
2129
2130                 ret = chmod(files[i], S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
2131                 if (ret == -1) {
2132                         if (strerror_r(errno, buf, sizeof(buf)))
2133                                 strcpy(buf, "");
2134                         _LOGD("FAIL : chmod %s 0664 : %s", files[i], buf);
2135                         return -1;
2136                 }
2137                 SET_SMACK_LABEL(files[i]);
2138         }
2139         return 0;
2140 }
2141
2142 API int pkgmgr_parser_create_and_initialize_db(uid_t uid)
2143 {
2144         int ret;
2145
2146         if (getuid() != OWNER_ROOT) {
2147                 _LOGE("Only root user is allowed");
2148                 return -1;
2149         }
2150
2151         if (access(getUserPkgParserDBPathUID(uid), F_OK) != -1) {
2152                 _LOGE("Manifest db for user %d is already exists", uid);
2153                 return -1;
2154         }
2155
2156         if (access(getUserPkgCertDBPathUID(uid), F_OK) != -1) {
2157                 _LOGE("Cert db for user %d is already exists", uid);
2158                 return -1;
2159         }
2160
2161         ret = pkgmgr_parser_check_and_create_db(uid);
2162         if (ret < 0)
2163                 return -1;
2164         ret = pkgmgr_parser_initialize_db(uid);
2165         if (ret < 0) {
2166                 pkgmgr_parser_close_db();
2167                 return -1;
2168         }
2169         pkgmgr_parser_close_db();
2170
2171         return 0;
2172 }
2173
2174 API int pkgmgr_parser_check_and_create_db(uid_t uid)
2175 {
2176         int ret = -1;
2177         /*Manifest DB*/
2178         ret = __pkgmgr_parser_create_db(&pkgmgr_parser_db, getUserPkgParserDBPathUID(uid));
2179         if (ret) {
2180                 _LOGD("Manifest DB creation Failed\n");
2181                 return -1;
2182         }
2183
2184         /*Cert DB*/
2185         ret = __pkgmgr_parser_create_db(&pkgmgr_cert_db, getUserPkgCertDBPathUID(uid));
2186         if (ret) {
2187                 _LOGD("Cert DB creation Failed\n");
2188                 return -1;
2189         }
2190         return 0;
2191 }
2192
2193 void pkgmgr_parser_close_db(void)
2194 {
2195         sqlite3_close(pkgmgr_parser_db);
2196         sqlite3_close(pkgmgr_cert_db);
2197 }
2198
2199
2200 API int pkgmgr_parser_insert_manifest_info_in_db(manifest_x *mfx)
2201 {
2202         _LOGD("pkgmgr_parser_insert_manifest_info_in_db\n");
2203         if (mfx == NULL) {
2204                 _LOGD("manifest pointer is NULL\n");
2205                 return -1;
2206         }
2207         int ret = 0;
2208         ret = pkgmgr_parser_check_and_create_db(GLOBAL_USER);
2209         if (ret == -1) {
2210                 _LOGD("Failed to open DB\n");
2211                 return ret;
2212         }
2213         ret = pkgmgr_parser_initialize_db(GLOBAL_USER);
2214         if (ret == -1)
2215                 goto err;
2216         /*Begin transaction*/
2217         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2218         if (ret != SQLITE_OK) {
2219                 _LOGD("Failed to begin transaction\n");
2220                 ret = -1;
2221                 goto err;
2222         }
2223         _LOGD("Transaction Begin\n");
2224         ret = __insert_manifest_info_in_db(mfx, GLOBAL_USER);
2225         if (ret == -1) {
2226                 _LOGD("Insert into DB failed. Rollback now\n");
2227                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2228                 goto err;
2229         }
2230         /*Commit transaction*/
2231         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
2232         if (ret != SQLITE_OK) {
2233                 _LOGD("Failed to commit transaction. Rollback now\n");
2234                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2235                 ret = -1;
2236                 goto err;
2237         }
2238         _LOGD("Transaction Commit and End\n");
2239 err:
2240         pkgmgr_parser_close_db();
2241         return ret;
2242 }
2243
2244 API int pkgmgr_parser_insert_manifest_info_in_usr_db(manifest_x *mfx, uid_t uid)
2245 {
2246         _LOGD("pkgmgr_parser_insert_manifest_info_in_usr_db\n");
2247         if (mfx == NULL) {
2248                 _LOGD("manifest pointer is NULL\n");
2249                 return -1;
2250         }
2251         int ret = 0;
2252         ret = pkgmgr_parser_check_and_create_db(uid);
2253         if (ret == -1) {
2254                 _LOGD("Failed to open DB\n");
2255                 return ret;
2256         }
2257         ret = pkgmgr_parser_initialize_db(uid);
2258         if (ret == -1)
2259                 goto err;
2260         /*Begin transaction*/
2261         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2262         if (ret != SQLITE_OK) {
2263                 _LOGD("Failed to begin transaction\n");
2264                 ret = -1;
2265                 goto err;
2266         }
2267         _LOGD("Transaction Begin\n");
2268         ret = __insert_manifest_info_in_db(mfx, uid);
2269         if (ret == -1) {
2270                 _LOGD("Insert into DB failed. Rollback now\n");
2271                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2272                 goto err;
2273         }
2274         /*Commit transaction*/
2275         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
2276         if (ret != SQLITE_OK) {
2277                 _LOGD("Failed to commit transaction. Rollback now\n");
2278                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2279                 ret = -1;
2280                 goto err;
2281         }
2282         _LOGD("Transaction Commit and End\n");
2283 err:
2284         pkgmgr_parser_close_db();
2285         return ret;
2286 }
2287
2288 API int pkgmgr_parser_update_manifest_info_in_usr_db(manifest_x *mfx, uid_t uid)
2289 {
2290         if (mfx == NULL) {
2291                 _LOGD("manifest pointer is NULL\n");
2292                 return -1;
2293         }
2294         int ret = 0;
2295         ret = pkgmgr_parser_check_and_create_db(uid);
2296         if (ret == -1) {
2297                 _LOGD("Failed to open DB\n");
2298                 return ret;
2299         }
2300         ret = pkgmgr_parser_initialize_db(uid);
2301         if (ret == -1)
2302                 goto err;
2303         /*Preserve guest mode visibility*/
2304         __preserve_guestmode_visibility_value( mfx);
2305         /*Begin transaction*/
2306         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2307         if (ret != SQLITE_OK) {
2308                 _LOGD("Failed to begin transaction\n");
2309                 ret = -1;
2310                 goto err;
2311         }
2312         _LOGD("Transaction Begin\n");
2313         ret = __delete_manifest_info_from_db(mfx, uid);
2314         if (ret == -1) {
2315                 _LOGD("Delete from DB failed. Rollback now\n");
2316                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2317                 goto err;
2318         }
2319         ret = __insert_manifest_info_in_db(mfx, uid);
2320         if (ret == -1) {
2321                 _LOGD("Insert into DB failed. Rollback now\n");
2322                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2323                 goto err;
2324         }
2325
2326         /*Commit transaction*/
2327         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
2328         if (ret != SQLITE_OK) {
2329                 _LOGD("Failed to commit transaction. Rollback now\n");
2330                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2331                 ret = -1;
2332                 goto err;
2333         }
2334         _LOGD("Transaction Commit and End\n");
2335 err:
2336         pkgmgr_parser_close_db();
2337         return ret;
2338 }
2339
2340 API int pkgmgr_parser_update_manifest_info_in_db(manifest_x *mfx)
2341 {
2342         return pkgmgr_parser_update_manifest_info_in_usr_db(mfx, GLOBAL_USER);
2343 }
2344
2345 API int pkgmgr_parser_delete_manifest_info_from_usr_db(manifest_x *mfx, uid_t uid)
2346 {
2347         if (mfx == NULL) {
2348                 _LOGD("manifest pointer is NULL\n");
2349                 return -1;
2350         }
2351         int ret = 0;
2352         ret = pkgmgr_parser_check_and_create_db(uid);
2353         if (ret == -1) {
2354                 _LOGD("Failed to open DB\n");
2355                 return ret;
2356         }
2357         /*Begin transaction*/
2358         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2359         if (ret != SQLITE_OK) {
2360                 _LOGD("Failed to begin transaction\n");
2361                 ret = -1;
2362                 goto err;
2363         }
2364         _LOGD("Transaction Begin\n");
2365         ret = __delete_manifest_info_from_db(mfx, uid);
2366         if (ret == -1) {
2367                 _LOGD("Delete from DB failed. Rollback now\n");
2368                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2369                 goto err;
2370         }
2371         /*Commit transaction*/
2372         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
2373         if (ret != SQLITE_OK) {
2374                 _LOGD("Failed to commit transaction, Rollback now\n");
2375                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2376                 ret = -1;
2377                 goto err;
2378         }
2379         _LOGD("Transaction Commit and End\n");
2380 err:
2381         pkgmgr_parser_close_db();
2382         return ret;
2383 }
2384
2385 API int pkgmgr_parser_delete_manifest_info_from_db(manifest_x *mfx)
2386 {
2387         return pkgmgr_parser_delete_manifest_info_from_usr_db(mfx, GLOBAL_USER);
2388 }
2389
2390 API int pkgmgr_parser_update_preload_info_in_db()
2391 {
2392         int ret = 0;
2393         ret = pkgmgr_parser_check_and_create_db(GLOBAL_USER);
2394         if (ret == -1) {
2395                 _LOGD("Failed to open DB\n");
2396                 return ret;
2397         }
2398         /*Begin transaction*/
2399         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2400         if (ret != SQLITE_OK) {
2401                 _LOGD("Failed to begin transaction\n");
2402                 ret = -1;
2403                 goto err;
2404         }
2405         _LOGD("Transaction Begin\n");
2406         ret = __update_preload_condition_in_db();
2407         if (ret == -1) {
2408                 _LOGD("__update_preload_condition_in_db failed. Rollback now\n");
2409                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2410                 goto err;
2411         }
2412         /*Commit transaction*/
2413         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
2414         if (ret != SQLITE_OK) {
2415                 _LOGD("Failed to commit transaction, Rollback now\n");
2416                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2417                 ret = -1;
2418                 goto err;
2419         }
2420         _LOGD("Transaction Commit and End\n");
2421 err:
2422         pkgmgr_parser_close_db();
2423         return ret;
2424 }
2425
2426 API int pkgmgr_parser_update_preload_info_in_usr_db(uid_t uid)
2427 {
2428         int ret = 0;
2429         ret = pkgmgr_parser_check_and_create_db(uid);
2430         if (ret == -1) {
2431                 _LOGD("Failed to open DB\n");
2432                 return ret;
2433         }
2434         /*Begin transaction*/
2435         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2436         if (ret != SQLITE_OK) {
2437                 _LOGD("Failed to begin transaction\n");
2438                 ret = -1;
2439                 goto err;
2440         }
2441         _LOGD("Transaction Begin\n");
2442         ret = __update_preload_condition_in_db();
2443         if (ret == -1) {
2444                 _LOGD("__update_preload_condition_in_db failed. Rollback now\n");
2445                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2446                 goto err;
2447         }
2448         /*Commit transaction*/
2449         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
2450         if (ret != SQLITE_OK) {
2451                 _LOGD("Failed to commit transaction, Rollback now\n");
2452                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2453                 ret = -1;
2454                 goto err;
2455         }
2456         _LOGD("Transaction Commit and End\n");
2457 err:
2458         pkgmgr_parser_close_db();
2459         return ret;
2460 }