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