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