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