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