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