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