implement TEP install, update
[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 <grp.h>
31 #include <pwd.h>
32
33 #include <db-util.h>
34 #include <glib.h>
35 /* For multi-user support */
36 #include <tzplatform_config.h>
37
38 #include "pkgmgr-info.h"
39 #include "pkgmgrinfo_basic.h"
40 #include "pkgmgrinfo_debug.h"
41 #include "pkgmgr_parser_internal.h"
42 #include "pkgmgr_parser_db.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 DB_LABEL "User::Home"
56 #define SET_SMACK_LABEL(x) \
57 do { \
58         if (smack_setlabel((x), DB_LABEL, SMACK_LABEL_ACCESS)) \
59                 _LOGE("failed chsmack -a %s %s", DB_LABEL, x); \
60         else \
61                 _LOGD("chsmack -a %s %s", DB_LABEL, x); \
62 } while (0)
63
64 sqlite3 *pkgmgr_parser_db;
65 sqlite3 *pkgmgr_cert_db;
66
67
68 #define QUERY_CREATE_TABLE_PACKAGE_INFO "create table if not exists package_info " \
69                                                 "(package text primary key not null, " \
70                                                 "package_type text DEFAULT 'rpm', " \
71                                                 "package_version text, " \
72                                                 "package_api_version text, " \
73                                                 "package_tep_name text, " \
74                                                 "install_location text, " \
75                                                 "package_size text, " \
76                                                 "package_removable text DEFAULT 'true', " \
77                                                 "package_preload text DEFAULT 'false', " \
78                                                 "package_readonly text DEFAULT 'false', " \
79                                                 "package_update text DEFAULT 'false', " \
80                                                 "package_appsetting text DEFAULT 'false', " \
81                                                 "package_nodisplay text DEFAULT 'false', " \
82                                                 "package_system text DEFAULT 'false', " \
83                                                 "author_name text, " \
84                                                 "author_email text, " \
85                                                 "author_href text," \
86                                                 "installed_time text," \
87                                                 "installed_storage text," \
88                                                 "storeclient_id text," \
89                                                 "mainapp_id text," \
90                                                 "package_url text," \
91                                                 "root_path text," \
92                                                 "csc_path text," \
93                                                 "package_support_disable text DEFAULT 'false')"
94
95 #define QUERY_CREATE_TABLE_PACKAGE_LOCALIZED_INFO "create table if not exists package_localized_info " \
96                                                 "(package text not null, " \
97                                                 "package_locale text DEFAULT 'No Locale', " \
98                                                 "package_label text, " \
99                                                 "package_icon text, " \
100                                                 "package_description text, " \
101                                                 "package_license text, " \
102                                                 "package_author, " \
103                                                 "PRIMARY KEY(package, package_locale), " \
104                                                 "FOREIGN KEY(package) " \
105                                                 "REFERENCES package_info(package) " \
106                                                 "ON DELETE CASCADE)"
107
108 #define QUERY_CREATE_TABLE_PACKAGE_PRIVILEGE_INFO "create table if not exists package_privilege_info " \
109                                                 "(package text not null, " \
110                                                 "privilege text not null, " \
111                                                 "PRIMARY KEY(package, privilege) " \
112                                                 "FOREIGN KEY(package) " \
113                                                 "REFERENCES package_info(package) " \
114                                                 "ON DELETE CASCADE)"
115
116 #define QUERY_CREATE_TABLE_PACKAGE_APP_INFO "create table if not exists package_app_info " \
117                                                 "(app_id text primary key not null, " \
118                                                 "app_component text, " \
119                                                 "app_exec text, " \
120                                                 "app_nodisplay text DEFAULT 'false', " \
121                                                 "app_type text, " \
122                                                 "app_onboot text DEFAULT 'false', " \
123                                                 "app_multiple text DEFAULT 'false', " \
124                                                 "app_autorestart text DEFAULT 'false', " \
125                                                 "app_taskmanage text DEFAULT 'false', " \
126                                                 "app_enabled text DEFAULT 'true', " \
127                                                 "app_hwacceleration text DEFAULT 'use-system-setting', " \
128                                                 "app_screenreader text DEFAULT 'use-system-setting', " \
129                                                 "app_mainapp text, " \
130                                                 "app_recentimage text, " \
131                                                 "app_launchcondition text, " \
132                                                 "app_indicatordisplay text DEFAULT 'true', " \
133                                                 "app_portraitimg text, " \
134                                                 "app_landscapeimg text, " \
135                                                 "app_guestmodevisibility text DEFAULT 'true', " \
136                                                 "app_permissiontype text DEFAULT 'normal', " \
137                                                 "app_preload text DEFAULT 'false', " \
138                                                 "app_submode text DEFAULT 'false', " \
139                                                 "app_submode_mainid text, " \
140                                                 "app_launch_mode text NOT NULL DEFAULT 'caller', " \
141                                                 "app_ui_gadget text DEFAULT 'false', " \
142                                                 "app_support_disable text DEFAULT 'false', " \
143                                                 "component_type text, " \
144                                                 "package text not null, " \
145                                                 "app_tep_name text, " \
146                                                 "FOREIGN KEY(package) " \
147                                                 "REFERENCES package_info(package) " \
148                                                 "ON DELETE CASCADE)"
149
150 #define QUERY_CREATE_TABLE_PACKAGE_APP_LOCALIZED_INFO "create table if not exists package_app_localized_info " \
151                                                 "(app_id text not null, " \
152                                                 "app_locale text DEFAULT 'No Locale', " \
153                                                 "app_label text, " \
154                                                 "app_icon text, " \
155                                                 "PRIMARY KEY(app_id,app_locale) " \
156                                                 "FOREIGN KEY(app_id) " \
157                                                 "REFERENCES package_app_info(app_id) " \
158                                                 "ON DELETE CASCADE)"
159
160 #define QUERY_CREATE_TABLE_PACKAGE_APP_ICON_SECTION_INFO "create table if not exists package_app_icon_section_info " \
161                                                 "(app_id text not null, " \
162                                                 "app_icon text, " \
163                                                 "app_icon_section text, " \
164                                                 "app_icon_resolution text, " \
165                                                 "PRIMARY KEY(app_id,app_icon_section,app_icon_resolution) " \
166                                                 "FOREIGN KEY(app_id) " \
167                                                 "REFERENCES package_app_info(app_id) " \
168                                                 "ON DELETE CASCADE)"
169
170 #define QUERY_CREATE_TABLE_PACKAGE_APP_IMAGE_INFO "create table if not exists package_app_image_info " \
171                                                 "(app_id text not null, " \
172                                                 "app_locale text DEFAULT 'No Locale', " \
173                                                 "app_image_section text, " \
174                                                 "app_image text, " \
175                                                 "PRIMARY KEY(app_id,app_image_section) " \
176                                                 "FOREIGN KEY(app_id) " \
177                                                 "REFERENCES package_app_info(app_id) " \
178                                                 "ON DELETE CASCADE)"
179
180 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_CONTROL "create table if not exists package_app_app_control " \
181                                                 "(app_id text not null, " \
182                                                 "app_control text not null, " \
183                                                 "PRIMARY KEY(app_id,app_control) " \
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_application_info(manifest_x *mfx);
261 static int __insert_application_appcategory_info(manifest_x *mfx);
262 static int __insert_application_appcontrol_info(manifest_x *mfx);
263 static int __insert_application_appmetadata_info(manifest_x *mfx);
264 static int __insert_application_share_allowed_info(manifest_x *mfx);
265 static int __insert_application_share_request_info(manifest_x *mfx);
266 static int __insert_application_datacontrol_info(manifest_x *mfx);
267 static void __insert_application_locale_info(gpointer data, gpointer userdata);
268 static void __insert_pkglocale_info(gpointer data, gpointer userdata);
269 static int __insert_manifest_info_in_db(manifest_x *mfx, uid_t uid);
270 static int __delete_manifest_info_from_db(manifest_x *mfx, uid_t uid);
271 static int __delete_subpkg_info_from_db(char *appid);
272 static int __delete_appinfo_from_db(char *db_table, const char *appid);
273 static int __initialize_db(sqlite3 *db_handle, const char *db_query);
274 static int __exec_query(char *query);
275 static void __extract_data(gpointer data, GList *lbl, GList *lcn, GList *icn, GList *dcn, GList *ath,
276                 char **label, char **license, char **icon, char **description, char **author);
277 static gint __comparefunc(gconstpointer a, gconstpointer b, gpointer userdata);
278 static GList *__create_locale_list(GList *locale, GList *lbl, GList *lcn, GList *icn, GList *dcn, GList *ath);
279 static void __preserve_guestmode_visibility_value(manifest_x *mfx);
280 static int __guestmode_visibility_cb(void *data, int ncols, char **coltxt, char **colname);
281 static int __pkgmgr_parser_create_db(sqlite3 **db_handle, const char *db_path);
282 static int __parserdb_change_perm(const char *db_file, uid_t uid);
283
284 static int __delete_subpkg_list_cb(void *data, int ncols, char **coltxt, char **colname)
285 {
286         if (coltxt[0])
287                 __delete_subpkg_info_from_db(coltxt[0]);
288
289         return 0;
290 }
291
292 static const char *__get_str(const char *str)
293 {
294         if (str == NULL)
295         {
296                 return PKGMGR_PARSER_EMPTY_STR;
297         }
298
299         return str;
300 }
301
302 static int __pkgmgr_parser_create_db(sqlite3 **db_handle, const char *db_path)
303 {
304         int ret = -1;
305         sqlite3 *handle;
306
307         ret = db_util_open(db_path, &handle,  DB_UTIL_REGISTER_HOOK_METHOD);
308         if (ret != SQLITE_OK) {
309                 _LOGD("connect db [%s] failed!\n", db_path);
310                 return -1;
311         }
312         *db_handle = handle;
313
314         return 0;
315 }
316
317 static int __guestmode_visibility_cb(void *data, int ncols, char **coltxt, char **colname)
318 {
319         manifest_x *mfx = (manifest_x *)data;
320         int i = 0;
321         char *appid = NULL;
322         char *status = NULL;
323         application_x *app;
324         GList *tmp;
325         if (mfx->application == NULL)
326                 return -1;
327         app = (application_x *)mfx->application->data;
328         for(i = 0; i < ncols; i++)
329         {
330                 if (strcmp(colname[i], "app_id") == 0) {
331                         if (coltxt[i])
332                                 appid = strdup(coltxt[i]);
333                 } else if (strcmp(colname[i], "app_guestmodevisibility") == 0) {
334                         if (coltxt[i])
335                                 status = strdup(coltxt[i]);
336                 }
337         }
338         if (appid == NULL) {
339                 _LOGD("app id is NULL\n");
340                 return -1;
341         }
342         /*update guest mode visibility*/
343         for (tmp = mfx->application; tmp; tmp = tmp->next) {
344                 app = (application_x *)tmp->data;
345                 if (app == NULL)
346                         continue;
347                 if (strcmp(app->appid, appid) == 0) {
348                         free((void *)app->guestmode_visibility);
349                         app->guestmode_visibility = strdup(status);
350                         break;
351                 }
352         }
353         if (appid) {
354                 free(appid);
355                 appid = NULL;
356         }
357         if (status) {
358                 free(status);
359                 status = NULL;
360         }
361
362         return 0;
363 }
364
365 static void __preserve_guestmode_visibility_value(manifest_x *mfx)
366 {
367         char *error_message = NULL;
368         char query[MAX_QUERY_LEN] = {'\0'};
369         snprintf(query, MAX_QUERY_LEN - 1, "select app_id, app_guestmodevisibility from package_app_info where package='%s'", mfx->package);
370         if (SQLITE_OK !=
371             sqlite3_exec(pkgmgr_parser_db, query,
372                          __guestmode_visibility_cb, (void *)mfx, &error_message)) {
373                 _LOGD("Don't execute query = %s error message = %s\n",
374                        query, error_message);
375                 sqlite3_free(error_message);
376         }
377         return;
378 }
379
380 static int __initialize_db(sqlite3 *db_handle, const char *db_query)
381 {
382         char *error_message = NULL;
383         if (SQLITE_OK !=
384             sqlite3_exec(db_handle, db_query,
385                          NULL, NULL, &error_message)) {
386                 _LOGD("Don't execute query = %s error message = %s\n",
387                        db_query, error_message);
388                 sqlite3_free(error_message);
389                 return -1;
390         }
391         sqlite3_free(error_message);
392         return 0;
393 }
394
395 static int __exec_query(char *query)
396 {
397         char *error_message = NULL;
398         if (SQLITE_OK !=
399             sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, &error_message)) {
400                 _LOGD("Don't execute query = %s error message = %s\n", query,
401                        error_message);
402                 sqlite3_free(error_message);
403                 return -1;
404         }
405         sqlite3_free(error_message);
406         return 0;
407 }
408
409 static int __exec_query_no_msg(char *query)
410 {
411         char *error_message = NULL;
412         if (SQLITE_OK !=
413             sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, &error_message)) {
414                 sqlite3_free(error_message);
415                 return -1;
416         }
417         sqlite3_free(error_message);
418         return 0;
419 }
420
421 static GList *__create_locale_list(GList *locale, GList *lbls, GList *lcns, GList *icns, GList *dcns, GList *aths)
422 {
423         GList *tmp;
424         label_x *lbl;
425         license_x *lcn;
426         icon_x *icn;
427         description_x *dcn;
428         author_x *ath;
429         for (tmp = lbls; tmp; tmp = tmp->next) {
430                 lbl = (label_x *)tmp->data;
431                 if (lbl == NULL)
432                         continue;
433                 if (lbl->lang)
434                         locale = g_list_insert_sorted_with_data(locale, (gpointer)lbl->lang, __comparefunc, NULL);
435         }
436         for (tmp = lcns; tmp; tmp = tmp->next) {
437                 lcn = (license_x *)tmp->data;
438                 if (lcn == NULL)
439                         continue;
440                 if (lcn->lang)
441                         locale = g_list_insert_sorted_with_data(locale, (gpointer)lcn->lang, __comparefunc, NULL);
442         }
443         for (tmp = icns; tmp; tmp = tmp->next) {
444                 icn = (icon_x *)tmp->data;
445                 if (icn == NULL)
446                         continue;
447                 if (icn->lang)
448                         locale = g_list_insert_sorted_with_data(locale, (gpointer)icn->lang, __comparefunc, NULL);
449         }
450         for (tmp = dcns; tmp; tmp = tmp->next) {
451                 dcn = (description_x *)tmp->data;
452                 if (dcn == NULL)
453                         continue;
454                 if (dcn->lang)
455                         locale = g_list_insert_sorted_with_data(locale, (gpointer)dcn->lang, __comparefunc, NULL);
456         }
457         for (tmp = aths; tmp; tmp = tmp->next) {
458                 ath = (author_x *)tmp->data;
459                 if (ath == NULL)
460                         continue;
461                 if (ath->lang)
462                         locale = g_list_insert_sorted_with_data(locale, (gpointer)ath->lang, __comparefunc, NULL);
463         }
464         return locale;
465
466 }
467
468 static GList *__create_icon_list(GList *locale, GList *icns)
469 {
470         GList *tmp;
471         icon_x *icn;
472
473         for (tmp = icns; tmp; tmp = tmp->next) {
474                 icn = (icon_x *)tmp->data;
475                 if (icn == NULL)
476                         continue;
477                 if (icn->section)
478                         locale = g_list_insert_sorted_with_data(locale, (gpointer)icn->section, __comparefunc, NULL);
479         }
480         return locale;
481 }
482
483 static GList *__create_image_list(GList *locale, GList *imgs)
484 {
485         GList *tmp;
486         image_x *img;
487
488         for (tmp = imgs; tmp; tmp = tmp->next) {
489                 img = (image_x *)tmp->data;
490                 if (img == NULL)
491                         continue;
492                 if (img->section)
493                         locale = g_list_insert_sorted_with_data(locale, (gpointer)img->section, __comparefunc, NULL);
494         }
495         return locale;
496 }
497
498 static void __trimfunc(GList* trim_list)
499 {
500         char *trim_data = NULL;
501         char *prev = NULL;
502
503         GList *list = NULL;
504         list = g_list_first(trim_list);
505
506         while (list) {
507                 trim_data = (char *)list->data;
508                 if (trim_data) {
509                         if (prev) {
510                                 if (strcmp(trim_data, prev) == 0) {
511                                         trim_list = g_list_remove(trim_list, trim_data);
512                                         list = g_list_first(trim_list);
513                                         prev = NULL;
514                                         continue;
515                                 } else
516                                         prev = trim_data;
517                         }
518                         else
519                                 prev = trim_data;
520                 }
521                 list = g_list_next(list);
522         }
523 }
524
525 static gint __comparefunc(gconstpointer a, gconstpointer b, gpointer userdata)
526 {
527         if (a == NULL || b == NULL)
528                 return 0;
529         if (strcmp((char*)a, (char*)b) == 0)
530                 return 0;
531         if (strcmp((char*)a, (char*)b) < 0)
532                 return -1;
533         if (strcmp((char*)a, (char*)b) > 0)
534                 return 1;
535         return 0;
536 }
537
538 static void __extract_data(gpointer data, GList *lbls, GList *lcns, GList *icns, GList *dcns, GList *aths,
539                 char **label, char **license, char **icon, char **description, char **author)
540 {
541         GList *tmp;
542         label_x *lbl;
543         license_x *lcn;
544         icon_x *icn;
545         description_x *dcn;
546         author_x *ath;
547         for (tmp = lbls; tmp; tmp = tmp->next) {
548                 lbl = (label_x *)tmp->data;
549                 if (lbl == NULL)
550                         continue;
551                 if (lbl->lang) {
552                         if (strcmp(lbl->lang, (char *)data) == 0) {
553                                 *label = (char*)lbl->text;
554                                 break;
555                         }
556                 }
557         }
558         for (tmp = lcns; tmp; tmp = tmp->next) {
559                 lcn = (license_x *)tmp->data;
560                 if (lcn == NULL)
561                         continue;
562                 if (lcn->lang) {
563                         if (strcmp(lcn->lang, (char *)data) == 0) {
564                                 *license = (char*)lcn->text;
565                                 break;
566                         }
567                 }
568         }
569         for (tmp = icns; tmp; tmp = tmp->next) {
570                 icn = (icon_x *)tmp->data;
571                 if (icn == NULL)
572                         continue;
573                 if (icn->lang) {
574                         if (strcmp(icn->lang, (char *)data) == 0) {
575                                 *icon = (char*)icn->text;
576                                 break;
577                         }
578                 }
579         }
580         for (tmp = dcns; tmp; tmp = tmp->next) {
581                 dcn = (description_x *)tmp->data;
582                 if (dcn == NULL)
583                         continue;
584                 if (dcn->lang) {
585                         if (strcmp(dcn->lang, (char *)data) == 0) {
586                                 *description = (char*)dcn->text;
587                                 break;
588                         }
589                 }
590         }
591         for (tmp = aths; tmp; tmp = tmp->next) {
592                 ath = (author_x *)tmp->data;
593                 if (ath == NULL)
594                         continue;
595                 if (ath->lang) {
596                         if (strcmp(ath->lang, (char *)data) == 0) {
597                                 *author = (char*)ath->text;
598                                 break;
599                         }
600                 }
601         }
602
603 }
604
605 static void __extract_icon_data(gpointer data, GList *icns, char **icon, char **resolution)
606 {
607         GList *tmp;
608         icon_x *icn;
609         for (tmp = icns; tmp; tmp = tmp->next) {
610                 icn = (icon_x *)tmp->data;
611                 if (icn == NULL)
612                         continue;
613                 if (icn->section) {
614                         if (strcmp(icn->section, (char *)data) == 0) {
615                                 *icon = (char*)icn->text;
616                                 *resolution = (char*)icn->resolution;
617                                 break;
618                         }
619                 }
620         }
621 }
622
623 static void __extract_image_data(gpointer data, GList *imgs, char **lang, char **image)
624 {
625         GList *tmp;
626         image_x *img;
627         for (tmp = imgs; tmp; tmp = tmp->next) {
628                 img = (image_x *)tmp->data;
629                 if (img == NULL)
630                         continue;
631                 if (img->section) {
632                         if (strcmp(img->section, (char *)data) == 0) {
633                                 *lang = (char*)img->lang;
634                                 *image = (char*)img->text;
635                                 break;
636                         }
637                 }
638         }
639 }
640
641 static void __insert_pkglocale_info(gpointer data, gpointer userdata)
642 {
643         int ret = -1;
644         char *label = NULL;
645         char *icon = NULL;
646         char *description = NULL;
647         char *license = NULL;
648         char *author = NULL;
649         char query[MAX_QUERY_LEN] = {'\0'};
650
651         manifest_x *mfx = (manifest_x *)userdata;
652         GList *lbl = mfx->label;
653         GList *lcn = mfx->license;
654         GList *icn = mfx->icon;
655         GList *dcn = mfx->description;
656         GList *ath = mfx->author;
657
658         __extract_data(data, lbl, lcn, icn, dcn, ath, &label, &license, &icon, &description, &author);
659         if (!label && !description && !icon && !license && !author)
660                 return;
661
662         sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_localized_info(package, package_locale, " \
663                 "package_label, package_icon, package_description, package_license, package_author) values " \
664                 "('%q', '%q', '%q', '%q', '%s', '%s', '%s')",
665                 mfx->package,
666                 (char*)data,
667                 label,
668                 icon,
669                 __get_str(description),
670                 __get_str(license),
671                 __get_str(author));
672
673         ret = __exec_query(query);
674         if (ret == -1)
675                 _LOGD("Package Localized Info DB Insert failed\n");
676 }
677
678 static void __insert_application_locale_info(gpointer data, gpointer userdata)
679 {
680         int ret = -1;
681         char *label = NULL;
682         char *icon = NULL;
683         char query[MAX_QUERY_LEN] = {'\0'};
684
685         application_x *app = (application_x*)userdata;
686         GList *lbl = app->label;
687         GList *icn = app->icon;
688
689         __extract_data(data, lbl, NULL, icn, NULL, NULL, &label, NULL, &icon, NULL, NULL);
690         if (!label && !icon)
691                 return;
692         sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_app_localized_info(app_id, app_locale, " \
693                 "app_label, app_icon) values " \
694                 "('%q', '%q', '%q', '%q')", app->appid, (char*)data,
695                 label, icon);
696         ret = __exec_query(query);
697         if (ret == -1)
698                 _LOGD("Package UiApp Localized Info DB Insert failed\n");
699
700         /*insert ui app locale info to pkg locale to get mainapp data */
701         if (strcasecmp(app->mainapp, "true")==0) {
702                 sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_localized_info(package, package_locale, " \
703                         "package_label, package_icon, package_description, package_license, package_author) values " \
704                         "('%q', '%q', '%q', '%q', '%q', '%q', '%q')",
705                         app->package,
706                         (char*)data,
707                         label,
708                         icon,
709                         PKGMGR_PARSER_EMPTY_STR,
710                         PKGMGR_PARSER_EMPTY_STR,
711                         PKGMGR_PARSER_EMPTY_STR);
712
713                 ret = __exec_query_no_msg(query);
714
715                 if (icon != NULL) {
716                         sqlite3_snprintf(MAX_QUERY_LEN, query, "update package_localized_info set package_icon='%s' "\
717                                 "where package='%s' and package_locale='%s'", icon, app->package, (char*)data);
718                         ret = __exec_query_no_msg(query);
719                 }
720         }
721 }
722
723 static void __insert_application_icon_section_info(gpointer data, gpointer userdata)
724 {
725         int ret = -1;
726         char *icon = NULL;
727         char *resolution = NULL;
728         char query[MAX_QUERY_LEN] = {'\0'};
729
730         application_x *app = (application_x*)userdata;
731         GList *icn = app->icon;
732
733         __extract_icon_data(data, icn, &icon, &resolution);
734         if (!icon && !resolution)
735                 return;
736         sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_app_icon_section_info(app_id, " \
737                 "app_icon, app_icon_section, app_icon_resolution) values " \
738                 "('%q', '%q', '%q', '%q')", app->appid,
739                 icon, (char*)data, resolution);
740
741         ret = __exec_query(query);
742         if (ret == -1)
743                 _LOGD("Package UiApp Localized Info DB Insert failed\n");
744
745 }
746
747 static void __insert_application_image_info(gpointer data, gpointer userdata)
748 {
749         int ret = -1;
750         char *lang = NULL;
751         char *img = NULL;
752         char query[MAX_QUERY_LEN] = {'\0'};
753
754         application_x *app = (application_x*)userdata;
755         GList *image = app->image;
756
757         __extract_image_data(data, image, &lang, &img);
758         if (!lang && !img)
759                 return;
760         sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_app_image_info(app_id, app_locale, " \
761                 "app_image_section, app_image) values " \
762                 "('%q', '%q', '%q', '%q')", app->appid, lang, (char*)data, img);
763
764         ret = __exec_query(query);
765         if (ret == -1)
766                 _LOGD("Package UiApp image Info DB Insert failed\n");
767
768 }
769
770
771 static int __insert_mainapp_info(manifest_x *mfx)
772 {
773         GList *tmp;
774         application_x *app;
775         int ret = -1;
776         char query[MAX_QUERY_LEN] = {'\0'};
777         for (tmp = mfx->application; tmp; tmp = tmp->next) {
778                 app = (application_x *)tmp->data;
779                 if (app == NULL)
780                         continue;
781                 snprintf(query, MAX_QUERY_LEN,
782                         "update package_app_info set app_mainapp='%s' where app_id='%s'", app->mainapp, app->appid);
783
784                 ret = __exec_query(query);
785                 if (ret == -1) {
786                         _LOGD("Package App Info DB Insert Failed\n");
787                         return -1;
788                 }
789                 if (strcasecmp(app->mainapp, "True")==0)
790                         mfx->mainapp_id = strdup(app->appid);
791         }
792
793         if (mfx->mainapp_id == NULL) {
794                 if (mfx->application == NULL)
795                         return -1;
796                 app = (application_x *)mfx->application->data;
797                 if (app == NULL)
798                         return -1;
799                 if (app->appid) {
800                         snprintf(query, MAX_QUERY_LEN, "update package_app_info set app_mainapp='true' where app_id='%s'", app->appid);
801                 } else {
802                         _LOGD("Not valid appid\n");
803                         return -1;
804                 }
805
806                 ret = __exec_query(query);
807                 if (ret == -1) {
808                         _LOGD("Package UiApp Info DB Insert Failed\n");
809                         return -1;
810                 }
811
812                 free((void *)app->mainapp);
813                 app->mainapp= strdup("true");
814                 mfx->mainapp_id = strdup(app->appid);
815         }
816
817         memset(query, '\0', MAX_QUERY_LEN);
818         snprintf(query, MAX_QUERY_LEN,
819                 "update package_info set mainapp_id='%s' where package='%s'", mfx->mainapp_id, mfx->package);
820         ret = __exec_query(query);
821         if (ret == -1) {
822                 _LOGD("Package Info DB update Failed\n");
823                 return -1;
824         }
825
826         return 0;
827 }
828 /* _PRODUCT_LAUNCHING_ENHANCED_
829 *  app->indicatordisplay, app->portraitimg, app->landscapeimg, app->guestmode_appstatus
830 */
831 static int __insert_application_info(manifest_x *mfx)
832 {
833         GList *tmp;
834         application_x *app;
835         int ret = -1;
836         char query[MAX_QUERY_LEN] = {'\0'};
837         for (tmp = mfx->application; tmp; tmp = tmp->next) {
838                 app = (application_x *)tmp->data;
839                 if (app == NULL)
840                         continue;
841
842                 snprintf(query, MAX_QUERY_LEN,
843                          "insert into package_app_info(app_id, app_component, app_exec, app_nodisplay, app_type, app_onboot, " \
844                         "app_multiple, app_autorestart, app_taskmanage, app_enabled, app_hwacceleration, app_screenreader, app_mainapp , app_recentimage, " \
845                         "app_launchcondition, app_indicatordisplay, app_portraitimg, app_landscapeimg, app_guestmodevisibility, app_permissiontype, "\
846                         "app_preload, app_submode, app_submode_mainid, app_launch_mode, app_ui_gadget, app_support_disable, component_type, package, app_tep_name) " \
847                         "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', '%s', '%s', '%s')",\
848                          app->appid,
849                          app->component_type,
850                          app->exec,
851                          app->nodisplay,
852                          app->type,
853                          app->onboot,
854                          app->multiple,
855                          app->autorestart,
856                          app->taskmanage,
857                          app->enabled,
858                          app->hwacceleration,
859                          app->screenreader,
860                          app->mainapp,
861                          __get_str(app->recentimage),
862                          app->launchcondition,
863                          app->indicatordisplay,
864                          __get_str(app->portraitimg),
865                          __get_str(app->landscapeimg),
866                          app->guestmode_visibility,
867                          app->permission_type,
868                          mfx->preload,
869                          app->submode,
870                          __get_str(app->submode_mainid),
871                          app->launch_mode,
872                          app->ui_gadget,
873                          mfx->support_disable,
874                          app->component_type,
875                          mfx->package,
876                          __get_str(mfx->tep_name));
877
878                 ret = __exec_query(query);
879                 if (ret == -1) {
880                         _LOGD("Package UiApp Info DB Insert Failed\n");
881                         return -1;
882                 }
883                 memset(query, '\0', MAX_QUERY_LEN);
884         }
885         return 0;
886 }
887
888 static int __insert_application_appcategory_info(manifest_x *mfx)
889 {
890         GList *app_tmp;
891         application_x *app;
892         GList *ct_tmp;
893         const char *ct;
894         int ret = -1;
895         char query[MAX_QUERY_LEN] = {'\0'};
896         for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
897                 app = (application_x *)app_tmp->data;
898                 if (app == NULL)
899                         continue;
900                 for (ct_tmp = app->category; ct_tmp; ct_tmp = ct_tmp->next) {
901                         ct = (const char *)ct_tmp->data;
902                         if (ct == NULL)
903                                 continue;
904                         snprintf(query, MAX_QUERY_LEN,
905                                 "insert into package_app_app_category(app_id, category) " \
906                                 "values('%s','%s')",\
907                                  app->appid, ct);
908                         ret = __exec_query(query);
909                         if (ret == -1) {
910                                 _LOGD("Package UiApp Category Info DB Insert Failed\n");
911                                 return -1;
912                         }
913                         memset(query, '\0', MAX_QUERY_LEN);
914                 }
915         }
916         return 0;
917 }
918
919 static int __insert_application_appmetadata_info(manifest_x *mfx)
920 {
921         GList *app_tmp;
922         application_x *app;
923         GList *md_tmp;
924         metadata_x *md;
925         int ret = -1;
926         char query[MAX_QUERY_LEN] = {'\0'};
927         for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
928                 app = (application_x *)app_tmp->data;
929                 if (app == NULL)
930                         continue;
931                 for (md_tmp = app->metadata; md_tmp; md_tmp = md_tmp->next) {
932                         md = (metadata_x *)md_tmp->data;
933                         if (md == NULL)
934                                 continue;
935                         if (md->key) {
936                                 snprintf(query, MAX_QUERY_LEN,
937                                         "insert into package_app_app_metadata(app_id, md_key, md_value) " \
938                                         "values('%s','%s', '%s')",\
939                                          app->appid, md->key, md->value ? md->value : "");
940                                 ret = __exec_query(query);
941                                 if (ret == -1) {
942                                         _LOGD("Package UiApp Metadata Info DB Insert Failed\n");
943                                         return -1;
944                                 }
945                         }
946                         memset(query, '\0', MAX_QUERY_LEN);
947                 }
948         }
949         return 0;
950 }
951
952 static int __insert_application_apppermission_info(manifest_x *mfx)
953 {
954         GList *app_tmp;
955         application_x *app;
956         GList *pm_tmp;
957         permission_x *pm;
958         int ret = -1;
959         char query[MAX_QUERY_LEN] = {'\0'};
960         for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
961                 app = (application_x *)app_tmp->data;
962                 if (app == NULL)
963                         continue;
964                 for (pm_tmp = app->permission; pm_tmp; pm_tmp = pm_tmp->next) {
965                         pm = (permission_x *)pm_tmp->data;
966                         if (pm == NULL)
967                                 continue;
968                         snprintf(query, MAX_QUERY_LEN,
969                                 "insert into package_app_app_permission(app_id, pm_type, pm_value) " \
970                                 "values('%s','%s', '%s')",\
971                                  app->appid, pm->type, pm->value);
972                         ret = __exec_query(query);
973                         if (ret == -1) {
974                                 _LOGD("Package UiApp permission Info DB Insert Failed\n");
975                                 return -1;
976                         }
977                         memset(query, '\0', MAX_QUERY_LEN);
978                 }
979         }
980         return 0;
981 }
982
983 static int __insert_application_appcontrol_info(manifest_x *mfx)
984 {
985         GList *app_tmp;
986         application_x *app;
987         GList *acontrol_tmp;
988         appcontrol_x *acontrol;
989         int ret = -1;
990         char query[MAX_QUERY_LEN] = {'\0'};
991         char buf[BUFSIZE] = {'\0'};
992         for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
993                 app = (application_x *)app_tmp->data;
994                 if (app == NULL)
995                         continue;
996                 for (acontrol_tmp = app->appcontrol; acontrol_tmp; acontrol_tmp = acontrol_tmp->next) {
997                         acontrol = (appcontrol_x *)acontrol_tmp->data;
998                         if (acontrol == NULL)
999                                 continue;
1000                         snprintf(buf, BUFSIZE, "%s|%s|%s",\
1001                                         acontrol->operation ? (strlen(acontrol->operation) > 0 ? acontrol->operation : "NULL") : "NULL",
1002                                         acontrol->uri ? (strlen(acontrol->uri) > 0 ? acontrol->uri : "NULL") : "NULL",
1003                                         acontrol->mime ? (strlen(acontrol->mime) > 0 ? acontrol->mime : "NULL") : "NULL");
1004                         snprintf(query, MAX_QUERY_LEN,
1005                                         "insert into package_app_app_control(app_id, app_control) " \
1006                                         "values('%s', '%s')",\
1007                                         app->appid, buf);
1008                         ret = __exec_query(query);
1009                         if (ret == -1) {
1010                                 _LOGD("Package UiApp AppSvc DB Insert Failed\n");
1011                                 return -1;
1012                         }
1013                         memset(query, '\0', MAX_QUERY_LEN);
1014                 }
1015         }
1016         return 0;
1017 }
1018
1019 static int __insert_application_datacontrol_info(manifest_x *mfx)
1020 {
1021         GList *app_tmp;
1022         application_x *app;
1023         GList *dc_tmp;
1024         datacontrol_x *dc;
1025         int ret = -1;
1026         char query[MAX_QUERY_LEN] = {'\0'};
1027
1028         for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
1029                 app = (application_x *)app_tmp->data;
1030                 if (app == NULL)
1031                         continue;
1032                 for (dc_tmp = app->datacontrol; dc_tmp; dc_tmp = dc_tmp->next) {
1033                         dc = (datacontrol_x *)dc_tmp->data;
1034                         if (dc == NULL)
1035                                 continue;
1036                         snprintf(query, MAX_QUERY_LEN,
1037                                         "insert into package_app_data_control(app_id, providerid, access, type) " \
1038                                         "values('%s', '%s', '%s', '%s')",\
1039                                         app->appid,
1040                                         dc->providerid,
1041                                         dc->access,
1042                                         dc->type);
1043
1044                         ret = __exec_query(query);
1045                         if (ret == -1) {
1046                                 _LOGD("Package UiApp Data Control DB Insert Failed\n");
1047                                 return -1;
1048                         }
1049                         memset(query, '\0', MAX_QUERY_LEN);
1050                 }
1051         }
1052         return 0;
1053 }
1054
1055 static int __insert_application_share_request_info(manifest_x *mfx)
1056 {
1057         GList *app_tmp;
1058         application_x *app;
1059         GList *ds_tmp;
1060         datashare_x *ds;
1061         GList *rq_tmp;
1062         const char *rq;
1063         int ret = -1;
1064         char query[MAX_QUERY_LEN] = {'\0'};
1065         for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
1066                 app = (application_x *)app_tmp->data;
1067                 if (app == NULL)
1068                         continue;
1069                 for (ds_tmp = app->datashare; ds_tmp; ds_tmp = ds_tmp->next) {
1070                         ds = (datashare_x *)ds_tmp->data;
1071                         if (ds == NULL)
1072                                 continue;
1073                         for (rq_tmp = ds->request; rq_tmp; rq_tmp = rq_tmp->next) {
1074                                 rq = (const char *)rq_tmp->data;
1075                                 if (rq == NULL)
1076                                         continue;
1077                                 snprintf(query, MAX_QUERY_LEN,
1078                                          "insert into package_app_share_request(app_id, data_share_request) " \
1079                                         "values('%s', '%s')",\
1080                                          app->appid, rq);
1081                                 ret = __exec_query(query);
1082                                 if (ret == -1) {
1083                                         _LOGD("Package UiApp Share Request DB Insert Failed\n");
1084                                         return -1;
1085                                 }
1086                                 memset(query, '\0', MAX_QUERY_LEN);
1087                         }
1088                 }
1089         }
1090         return 0;
1091 }
1092
1093 static int __insert_application_share_allowed_info(manifest_x *mfx)
1094 {
1095         GList *app_tmp;
1096         application_x *app;
1097         GList *ds_tmp;
1098         datashare_x *ds;
1099         GList *df_tmp;
1100         define_x *df;
1101         GList *al_tmp;
1102         const char *al;
1103         int ret = -1;
1104         char query[MAX_QUERY_LEN] = {'\0'};
1105         for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
1106                 app = (application_x *)app_tmp->data;
1107                 if (app == NULL)
1108                         continue;
1109                 for (ds_tmp = app->datashare; ds_tmp; ds_tmp = ds_tmp->next) {
1110                         ds = (datashare_x *)ds_tmp->data;
1111                         if (ds == NULL)
1112                                 continue;
1113                         for (df_tmp = ds->define; df_tmp; df_tmp = df_tmp->next) {
1114                                 df = (define_x *)df_tmp->data;
1115                                 if (df == NULL)
1116                                         continue;
1117                                 for (al_tmp = df->allowed; al_tmp; al_tmp = al_tmp->next) {
1118                                         al = (const char *)al_tmp->data;
1119                                         if (al == NULL)
1120                                                 continue;
1121                                         snprintf(query, MAX_QUERY_LEN,
1122                                                  "insert into package_app_share_allowed(app_id, data_share_path, data_share_allowed) " \
1123                                                 "values('%s', '%s', '%s')",\
1124                                                  app->appid, df->path, al);
1125                                         ret = __exec_query(query);
1126                                         if (ret == -1) {
1127                                                 _LOGD("Package UiApp Share Allowed DB Insert Failed\n");
1128                                                 return -1;
1129                                         }
1130                                         memset(query, '\0', MAX_QUERY_LEN);
1131                                 }
1132                         }
1133                 }
1134         }
1135         return 0;
1136 }
1137
1138 static int __insert_manifest_info_in_db(manifest_x *mfx, uid_t uid)
1139 {
1140         GList *tmp;
1141         application_x *app;
1142         const char *pv = NULL;
1143         char query[MAX_QUERY_LEN] = { '\0' };
1144         int ret = -1;
1145         author_x *author;
1146         const char *auth_name = NULL;
1147         const char *auth_email = NULL;
1148         const char *auth_href = NULL;
1149
1150         GList *pkglocale = NULL;
1151         GList *applocale = NULL;
1152         GList *appicon = NULL;
1153         GList *appimage = NULL;
1154
1155         if (mfx->author && mfx->author->data) {
1156                 author = (author_x *)mfx->author->data;
1157                 if (author->text)
1158                         auth_name = author->text;
1159                 if (author->email)
1160                         auth_email = author->email;
1161                 if (author->href)
1162                         auth_href = author->href;
1163         }
1164         /*Insert in the package_cert_info CERT_DB*/
1165         pkgmgrinfo_instcertinfo_h cert_handle = NULL;
1166         ret = pkgmgrinfo_set_cert_value(&cert_handle, PMINFO_SET_AUTHOR_ROOT_CERT, "author root certificate");
1167         if (ret != PMINFO_R_OK) {
1168                 pkgmgrinfo_destroy_certinfo_set_handle(cert_handle);
1169                 _LOGE("Cert Info DB create handle failed\n");
1170                 return -1;
1171         }
1172         ret = pkgmgrinfo_save_certinfo(mfx->package, &cert_handle, uid);
1173         if (ret != PMINFO_R_OK) {
1174                 pkgmgrinfo_destroy_certinfo_set_handle(cert_handle);
1175                 _LOGE("Cert Info DB Insert Failed\n");
1176                 return -1;
1177         }
1178
1179         /*Insert in the package_info DB*/
1180         snprintf(query, MAX_QUERY_LEN,
1181                  "insert into package_info(package, package_type, package_version, package_api_version, package_tep_name, install_location, package_size, " \
1182                 "package_removable, package_preload, package_readonly, package_update, package_appsetting, package_nodisplay, package_system," \
1183                 "author_name, author_email, author_href, installed_time, installed_storage, storeclient_id, mainapp_id, package_url, root_path, csc_path, package_support_disable) " \
1184                 "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')",\
1185                  mfx->package,
1186                  mfx->type,
1187                  mfx->version,
1188                  __get_str(mfx->api_version),
1189                  __get_str(mfx->tep_name),
1190                  __get_str(mfx->installlocation),
1191                  __get_str(mfx->package_size),
1192                  mfx->removable,
1193                  mfx->preload,
1194                  mfx->readonly,
1195                  mfx->update,
1196                  mfx->appsetting,
1197                  mfx->nodisplay_setting,
1198                  mfx->system,
1199                  __get_str(auth_name),
1200                  __get_str(auth_email),
1201                  __get_str(auth_href),
1202                  mfx->installed_time,
1203                  mfx->installed_storage,
1204                  __get_str(mfx->storeclient_id),
1205                  mfx->mainapp_id,
1206                  __get_str(mfx->package_url),
1207                  mfx->root_path,
1208                  __get_str(mfx->csc_path),
1209                  mfx->support_disable);
1210
1211         ret = __exec_query(query);
1212         if (ret == -1) {
1213                 _LOGD("Package Info DB Insert Failed\n");
1214                 return -1;
1215         }
1216
1217         /*Insert in the package_privilege_info DB*/
1218         for (tmp = mfx->privileges; tmp; tmp = tmp->next) {
1219                 pv = (const char *)tmp->data;
1220                 if (pv == NULL)
1221                         continue;
1222                 memset(query, '\0', MAX_QUERY_LEN);
1223                 snprintf(query, MAX_QUERY_LEN,
1224                         "insert into package_privilege_info(package, privilege) " \
1225                         "values('%s','%s')",\
1226                          mfx->package, pv);
1227                 ret = __exec_query(query);
1228                 if (ret == -1) {
1229                         _LOGD("Package Privilege Info DB Insert Failed\n");
1230                         return -1;
1231                 }
1232         }
1233
1234         if (mfx->application != NULL) {
1235                 ret = __insert_mainapp_info(mfx);
1236                 if (ret == -1)
1237                         return -1;
1238         }
1239
1240         /*Insert the package locale*/
1241         pkglocale = __create_locale_list(pkglocale, mfx->label, mfx->license, mfx->icon, mfx->description, mfx->author);
1242         /*remove duplicated data in pkglocale*/
1243         __trimfunc(pkglocale);
1244
1245         /*Insert the app locale, icon, image info */
1246         for (tmp = mfx->application; tmp; tmp = tmp->next) {
1247                 app = (application_x *)tmp->data;
1248                 if (app == NULL)
1249                         continue;
1250                 applocale = __create_locale_list(applocale, app->label, NULL, app->icon, NULL, NULL);
1251                 appicon = __create_icon_list(appicon, app->icon);
1252                 appimage = __create_image_list(appimage, app->image);
1253         }
1254         /*remove duplicated data in applocale*/
1255         __trimfunc(applocale);
1256         __trimfunc(appicon);
1257         __trimfunc(appimage);
1258
1259         g_list_foreach(pkglocale, __insert_pkglocale_info, (gpointer)mfx);
1260
1261         /*native app locale info*/
1262         for (tmp = mfx->application; tmp; tmp = tmp->next) {
1263                 app = (application_x *)tmp->data;
1264                 if (app == NULL)
1265                         continue;
1266                 g_list_foreach(applocale, __insert_application_locale_info, (gpointer)app);
1267                 g_list_foreach(appicon, __insert_application_icon_section_info, (gpointer)app);
1268                 g_list_foreach(appimage, __insert_application_image_info, (gpointer)app);
1269         }
1270
1271         g_list_free(pkglocale);
1272         pkglocale = NULL;
1273         g_list_free(applocale);
1274         applocale = NULL;
1275         g_list_free(appicon);
1276         appicon = NULL;
1277         g_list_free(appimage);
1278         appimage = NULL;
1279
1280         /*Insert in the package_app_info DB*/
1281         ret = __insert_application_info(mfx);
1282         if (ret == -1)
1283                 return -1;
1284         /*Insert in the package_app_app_control DB*/
1285         ret = __insert_application_appcontrol_info(mfx);
1286         if (ret == -1)
1287                 return -1;
1288
1289         /*Insert in the package_app_app_category DB*/
1290         ret = __insert_application_appcategory_info(mfx);
1291         if (ret == -1)
1292                 return -1;
1293
1294         /*Insert in the package_app_app_metadata DB*/
1295         ret = __insert_application_appmetadata_info(mfx);
1296         if (ret == -1)
1297                 return -1;
1298
1299         /*Insert in the package_app_app_permission DB*/
1300         ret = __insert_application_apppermission_info(mfx);
1301         if (ret == -1)
1302                 return -1;
1303
1304         /*Insert in the package_app_share_allowed DB*/
1305         ret = __insert_application_share_allowed_info(mfx);
1306         if (ret == -1)
1307                 return -1;
1308
1309         /*Insert in the package_app_share_request DB*/
1310         ret = __insert_application_share_request_info(mfx);
1311         if (ret == -1)
1312                 return -1;
1313
1314         /*Insert in the package_app_data_control DB*/
1315         ret = __insert_application_datacontrol_info(mfx);
1316         if (ret == -1)
1317                 return -1;
1318
1319         return 0;
1320
1321 }
1322
1323 static int __delete_appinfo_from_db(char *db_table, const char *appid)
1324 {
1325         char query[MAX_QUERY_LEN] = { '\0' };
1326         int ret = -1;
1327         memset(query, '\0', MAX_QUERY_LEN);
1328         snprintf(query, MAX_QUERY_LEN,
1329                  "delete from %s where app_id='%s'", db_table, appid);
1330         ret = __exec_query(query);
1331         if (ret == -1) {
1332                 _LOGD("DB Deletion from table (%s) Failed\n", db_table);
1333                 return -1;
1334         }
1335         return 0;
1336 }
1337
1338 static int __delete_subpkg_info_from_db(char *appid)
1339 {
1340         int ret = -1;
1341
1342         ret = __delete_appinfo_from_db("package_app_info", appid);
1343         if (ret < 0)
1344                 return ret;
1345         ret = __delete_appinfo_from_db("package_app_localized_info", appid);
1346         if (ret < 0)
1347                 return ret;
1348         ret = __delete_appinfo_from_db("package_app_icon_section_info", appid);
1349         if (ret < 0)
1350                 return ret;
1351         ret = __delete_appinfo_from_db("package_app_image_info", appid);
1352         if (ret < 0)
1353                 return ret;
1354         ret = __delete_appinfo_from_db("package_app_app_control", appid);
1355         if (ret < 0)
1356                 return ret;
1357         ret = __delete_appinfo_from_db("package_app_app_category", appid);
1358         if (ret < 0)
1359                 return ret;
1360         ret = __delete_appinfo_from_db("package_app_app_metadata", appid);
1361         if (ret < 0)
1362                 return ret;
1363         ret = __delete_appinfo_from_db("package_app_app_permission", appid);
1364         if (ret < 0)
1365                 return ret;
1366         ret = __delete_appinfo_from_db("package_app_share_allowed", appid);
1367         if (ret < 0)
1368                 return ret;
1369         ret = __delete_appinfo_from_db("package_app_share_request", appid);
1370         if (ret < 0)
1371                 return ret;
1372         ret = __delete_appinfo_from_db("package_app_data_control", appid);
1373         if (ret < 0)
1374                 return ret;
1375
1376         return 0;
1377 }
1378
1379 static int __delete_subpkg_from_db(manifest_x *mfx)
1380 {
1381         char query[MAX_QUERY_LEN] = { '\0' };
1382         char *error_message = NULL;
1383
1384         snprintf(query, MAX_QUERY_LEN, "select app_id from package_app_info where package='%s'", mfx->package);
1385         if (SQLITE_OK !=
1386             sqlite3_exec(pkgmgr_parser_db, query, __delete_subpkg_list_cb, NULL, &error_message)) {
1387                 _LOGE("Don't execute query = %s error message = %s\n", query,
1388                        error_message);
1389                 sqlite3_free(error_message);
1390                 return -1;
1391         }
1392         sqlite3_free(error_message);
1393
1394         return 0;
1395 }
1396
1397 static int __delete_manifest_info_from_db(manifest_x *mfx, uid_t uid)
1398 {
1399         char query[MAX_QUERY_LEN] = { '\0' };
1400         int ret = -1;
1401         GList *tmp;
1402         application_x *app;
1403         /*Delete from cert table*/
1404         if (uid != GLOBAL_USER)
1405                 ret = pkgmgrinfo_delete_usr_certinfo(mfx->package, uid);
1406         else
1407                 ret = pkgmgrinfo_delete_certinfo(mfx->package);
1408         if (ret) {
1409                 _LOGD("Cert Info  DB Delete Failed\n");
1410                 return -1;
1411         }
1412
1413         /*Delete from Package Info DB*/
1414         snprintf(query, MAX_QUERY_LEN,
1415                  "delete from package_info where package='%s'", mfx->package);
1416         ret = __exec_query(query);
1417         if (ret == -1) {
1418                 _LOGD("Package Info DB Delete Failed\n");
1419                 return -1;
1420         }
1421         memset(query, '\0', MAX_QUERY_LEN);
1422
1423         /*Delete from Package Localized Info*/
1424         snprintf(query, MAX_QUERY_LEN,
1425                  "delete from package_localized_info where package='%s'", mfx->package);
1426         ret = __exec_query(query);
1427         if (ret == -1) {
1428                 _LOGD("Package Localized Info DB Delete Failed\n");
1429                 return -1;
1430         }
1431
1432         /*Delete from Package Privilege Info*/
1433         snprintf(query, MAX_QUERY_LEN,
1434                  "delete from package_privilege_info where package='%s'", mfx->package);
1435         ret = __exec_query(query);
1436         if (ret == -1) {
1437                 _LOGD("Package Privilege Info DB Delete Failed\n");
1438                 return -1;
1439         }
1440
1441         for (tmp = mfx->application; tmp; tmp = tmp->next) {
1442                 app = (application_x *)tmp->data;
1443                 if (app == NULL)
1444                         continue;
1445                 ret = __delete_appinfo_from_db("package_app_info", app->appid);
1446                 if (ret < 0)
1447                         return ret;
1448                 ret = __delete_appinfo_from_db("package_app_localized_info", app->appid);
1449                 if (ret < 0)
1450                         return ret;
1451                 ret = __delete_appinfo_from_db("package_app_icon_section_info", app->appid);
1452                 if (ret < 0)
1453                         return ret;
1454                 ret = __delete_appinfo_from_db("package_app_image_info", app->appid);
1455                 if (ret < 0)
1456                         return ret;
1457                 ret = __delete_appinfo_from_db("package_app_app_control", app->appid);
1458                 if (ret < 0)
1459                         return ret;
1460                 ret = __delete_appinfo_from_db("package_app_app_category", app->appid);
1461                 if (ret < 0)
1462                         return ret;
1463                 ret = __delete_appinfo_from_db("package_app_app_metadata", app->appid);
1464                 if (ret < 0)
1465                         return ret;
1466                 ret = __delete_appinfo_from_db("package_app_app_permission", app->appid);
1467                 if (ret < 0)
1468                         return ret;
1469                 ret = __delete_appinfo_from_db("package_app_share_allowed", app->appid);
1470                 if (ret < 0)
1471                         return ret;
1472                 ret = __delete_appinfo_from_db("package_app_share_request", app->appid);
1473                 if (ret < 0)
1474                         return ret;
1475                 ret = __delete_appinfo_from_db("package_app_data_control", app->appid);
1476                 if (ret < 0)
1477                         return ret;
1478         }
1479
1480         /* if main package has sub pkg, delete sub pkg data*/
1481         __delete_subpkg_from_db(mfx);
1482
1483         return 0;
1484 }
1485
1486 static int __update_preload_condition_in_db()
1487 {
1488         int ret = -1;
1489         char query[MAX_QUERY_LEN] = {'\0'};
1490
1491         snprintf(query, MAX_QUERY_LEN, "update package_info set package_preload='true'");
1492
1493         ret = __exec_query(query);
1494         if (ret == -1)
1495                 _LOGD("Package preload_condition update failed\n");
1496
1497         return ret;
1498 }
1499
1500 API int pkgmgr_parser_initialize_db(uid_t uid)
1501 {
1502         int ret = -1;
1503         /*Manifest DB*/
1504         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_INFO);
1505         if (ret == -1) {
1506                 _LOGD("package info DB initialization failed\n");
1507                 return ret;
1508         }
1509         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_LOCALIZED_INFO);
1510         if (ret == -1) {
1511                 _LOGD("package localized info DB initialization failed\n");
1512                 return ret;
1513         }
1514         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_PRIVILEGE_INFO);
1515         if (ret == -1) {
1516                 _LOGD("package app app privilege DB initialization failed\n");
1517                 return ret;
1518         }
1519         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_INFO);
1520         if (ret == -1) {
1521                 _LOGD("package app info DB initialization failed\n");
1522                 return ret;
1523         }
1524         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_LOCALIZED_INFO);
1525         if (ret == -1) {
1526                 _LOGD("package app localized info DB initialization failed\n");
1527                 return ret;
1528         }
1529         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_ICON_SECTION_INFO);
1530         if (ret == -1) {
1531                 _LOGD("package app icon localized info DB initialization failed\n");
1532                 return ret;
1533         }
1534         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_IMAGE_INFO);
1535         if (ret == -1) {
1536                 _LOGD("package app image info DB initialization failed\n");
1537                 return ret;
1538         }
1539         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_CONTROL);
1540         if (ret == -1) {
1541                 _LOGD("package app app control DB initialization failed\n");
1542                 return ret;
1543         }
1544         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_CATEGORY);
1545         if (ret == -1) {
1546                 _LOGD("package app app category DB initialization failed\n");
1547                 return ret;
1548         }
1549         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_METADATA);
1550         if (ret == -1) {
1551                 _LOGD("package app app category DB initialization failed\n");
1552                 return ret;
1553         }
1554         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_PERMISSION);
1555         if (ret == -1) {
1556                 _LOGD("package app app permission DB initialization failed\n");
1557                 return ret;
1558         }
1559         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_ALLOWED);
1560         if (ret == -1) {
1561                 _LOGD("package app share allowed DB initialization failed\n");
1562                 return ret;
1563         }
1564         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_REQUEST);
1565         if (ret == -1) {
1566                 _LOGD("package app share request DB initialization failed\n");
1567                 return ret;
1568         }
1569         ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_DATA_CONTROL);
1570         if (ret == -1) {
1571                 _LOGD("package app data control DB initialization failed\n");
1572                 return ret;
1573         }
1574         /*Cert DB*/
1575         ret = __initialize_db(pkgmgr_cert_db, QUERY_CREATE_TABLE_PACKAGE_CERT_INFO);
1576         if (ret == -1) {
1577                 _LOGD("package cert info DB initialization failed\n");
1578                 return ret;
1579         }
1580         ret = __initialize_db(pkgmgr_cert_db, QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO);
1581         if (ret == -1) {
1582                 _LOGD("package cert index info DB initialization failed\n");
1583                 return ret;
1584         }
1585
1586         if( 0 != __parserdb_change_perm(getUserPkgCertDBPathUID(uid), uid)) {
1587                 _LOGD("Failed to change cert db permission\n");
1588         }
1589         if( 0 != __parserdb_change_perm(getUserPkgParserDBPathUID(uid), uid)) {
1590                 _LOGD("Failed to change parser db permission\n");
1591         }
1592
1593         return 0;
1594 }
1595
1596 static int __parserdb_change_perm(const char *db_file, uid_t uid)
1597 {
1598         char buf[BUFSIZE];
1599         char journal_file[BUFSIZE];
1600         char *files[3];
1601         int ret, i;
1602         struct passwd *userinfo = NULL;
1603         files[0] = (char *)db_file;
1604         files[1] = journal_file;
1605         files[2] = NULL;
1606
1607         if (db_file == NULL)
1608                 return -1;
1609
1610         if (getuid() != OWNER_ROOT) //At this time we should be root to apply this
1611                 return 0;
1612         snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal");
1613         if (uid == OWNER_ROOT)
1614                 uid = GLOBAL_USER;
1615         userinfo = getpwuid(uid);
1616         if (!userinfo) {
1617                 _LOGE("FAIL: user %d doesn't exist", uid);
1618                 return -1;
1619         }
1620         snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal");
1621
1622         for (i = 0; files[i]; i++) {
1623                 ret = chown(files[i], uid, userinfo->pw_gid);
1624                 if (ret == -1) {
1625                         if (strerror_r(errno, buf, sizeof(buf)))
1626                                 strcpy(buf, "");
1627                         _LOGD("FAIL : chown %s %d.%d : %s", files[i], uid,
1628                                         userinfo->pw_gid, buf);
1629                         return -1;
1630                 }
1631
1632                 ret = chmod(files[i], S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
1633                 if (ret == -1) {
1634                         if (strerror_r(errno, buf, sizeof(buf)))
1635                                 strcpy(buf, "");
1636                         _LOGD("FAIL : chmod %s 0664 : %s", files[i], buf);
1637                         return -1;
1638                 }
1639                 SET_SMACK_LABEL(files[i]);
1640         }
1641         return 0;
1642 }
1643
1644 API int pkgmgr_parser_create_and_initialize_db(uid_t uid)
1645 {
1646         int ret;
1647
1648         if (getuid() != OWNER_ROOT) {
1649                 _LOGE("Only root user is allowed");
1650                 return -1;
1651         }
1652
1653         if (access(getUserPkgParserDBPathUID(uid), F_OK) != -1) {
1654                 _LOGE("Manifest db for user %d is already exists", uid);
1655                 return -1;
1656         }
1657
1658         if (access(getUserPkgCertDBPathUID(uid), F_OK) != -1) {
1659                 _LOGE("Cert db for user %d is already exists", uid);
1660                 return -1;
1661         }
1662
1663         ret = pkgmgr_parser_check_and_create_db(uid);
1664         if (ret < 0)
1665                 return -1;
1666         ret = pkgmgr_parser_initialize_db(uid);
1667         if (ret < 0) {
1668                 pkgmgr_parser_close_db();
1669                 return -1;
1670         }
1671         pkgmgr_parser_close_db();
1672
1673         return 0;
1674 }
1675
1676 API int pkgmgr_parser_check_and_create_db(uid_t uid)
1677 {
1678         int ret = -1;
1679         /*Manifest DB*/
1680         ret = __pkgmgr_parser_create_db(&pkgmgr_parser_db, getUserPkgParserDBPathUID(uid));
1681         if (ret) {
1682                 _LOGD("Manifest DB creation Failed\n");
1683                 return -1;
1684         }
1685
1686         /*Cert DB*/
1687         ret = __pkgmgr_parser_create_db(&pkgmgr_cert_db, getUserPkgCertDBPathUID(uid));
1688         if (ret) {
1689                 _LOGD("Cert DB creation Failed\n");
1690                 return -1;
1691         }
1692         return 0;
1693 }
1694
1695 void pkgmgr_parser_close_db(void)
1696 {
1697         sqlite3_close(pkgmgr_parser_db);
1698         sqlite3_close(pkgmgr_cert_db);
1699 }
1700
1701
1702 API int pkgmgr_parser_insert_manifest_info_in_db(manifest_x *mfx)
1703 {
1704         _LOGD("pkgmgr_parser_insert_manifest_info_in_db\n");
1705         if (mfx == NULL) {
1706                 _LOGD("manifest pointer is NULL\n");
1707                 return -1;
1708         }
1709         int ret = 0;
1710         ret = pkgmgr_parser_check_and_create_db(GLOBAL_USER);
1711         if (ret == -1) {
1712                 _LOGD("Failed to open DB\n");
1713                 return ret;
1714         }
1715         ret = pkgmgr_parser_initialize_db(GLOBAL_USER);
1716         if (ret == -1)
1717                 goto err;
1718         /*Begin transaction*/
1719         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
1720         if (ret != SQLITE_OK) {
1721                 _LOGD("Failed to begin transaction\n");
1722                 ret = -1;
1723                 goto err;
1724         }
1725         _LOGD("Transaction Begin\n");
1726         ret = __insert_manifest_info_in_db(mfx, GLOBAL_USER);
1727         if (ret == -1) {
1728                 _LOGD("Insert into DB failed. Rollback now\n");
1729                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1730                 goto err;
1731         }
1732         /*Commit transaction*/
1733         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
1734         if (ret != SQLITE_OK) {
1735                 _LOGD("Failed to commit transaction. Rollback now\n");
1736                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1737                 ret = -1;
1738                 goto err;
1739         }
1740         _LOGD("Transaction Commit and End\n");
1741 err:
1742         pkgmgr_parser_close_db();
1743         return ret;
1744 }
1745
1746 API int pkgmgr_parser_insert_manifest_info_in_usr_db(manifest_x *mfx, uid_t uid)
1747 {
1748         _LOGD("pkgmgr_parser_insert_manifest_info_in_usr_db\n");
1749         if (mfx == NULL) {
1750                 _LOGD("manifest pointer is NULL\n");
1751                 return -1;
1752         }
1753         int ret = 0;
1754         ret = pkgmgr_parser_check_and_create_db(uid);
1755         if (ret == -1) {
1756                 _LOGD("Failed to open DB\n");
1757                 return ret;
1758         }
1759         ret = pkgmgr_parser_initialize_db(uid);
1760         if (ret == -1)
1761                 goto err;
1762         /*Begin transaction*/
1763         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
1764         if (ret != SQLITE_OK) {
1765                 _LOGD("Failed to begin transaction\n");
1766                 ret = -1;
1767                 goto err;
1768         }
1769         _LOGD("Transaction Begin\n");
1770         ret = __insert_manifest_info_in_db(mfx, uid);
1771         if (ret == -1) {
1772                 _LOGD("Insert into DB failed. Rollback now\n");
1773                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1774                 goto err;
1775         }
1776         /*Commit transaction*/
1777         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
1778         if (ret != SQLITE_OK) {
1779                 _LOGD("Failed to commit transaction. Rollback now\n");
1780                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1781                 ret = -1;
1782                 goto err;
1783         }
1784         _LOGD("Transaction Commit and End\n");
1785 err:
1786         pkgmgr_parser_close_db();
1787         return ret;
1788 }
1789
1790 API int pkgmgr_parser_update_tep_info_in_db(const char *pkgid, const char *tep_path)
1791 {
1792         return pkgmgr_parser_update_tep_info_in_usr_db(pkgid, tep_path, GLOBAL_USER);
1793 }
1794
1795 API int pkgmgr_parser_update_tep_info_in_usr_db(const char *pkgid, const char *tep_path, uid_t uid)
1796 {
1797         if (pkgid == NULL || tep_path == NULL) {
1798                 _LOGE("invalid parameter");
1799                 return -1;
1800         }
1801
1802         int ret = -1;
1803         char *query = NULL;
1804
1805         ret = pkgmgr_parser_check_and_create_db(uid);
1806         if (ret == -1) {
1807                 _LOGD("Failed to open DB\n");
1808                 return ret;
1809         }
1810         ret = pkgmgr_parser_initialize_db(uid);
1811         if (ret == -1)
1812                 goto err;
1813
1814         /*Begin transaction*/
1815         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
1816         if (ret != SQLITE_OK) {
1817                 _LOGD("Failed to begin transaction\n");
1818                 ret = -1;
1819                 goto err;
1820         }
1821         _LOGD("Transaction Begin\n");
1822
1823
1824         /* Updating TEP info in "package_info" table */
1825         query = sqlite3_mprintf("UPDATE package_info "\
1826                                                 "SET package_tep_name = %Q "\
1827                                                 "WHERE package = %Q", tep_path, pkgid);
1828
1829         ret = __exec_query(query);
1830         sqlite3_free(query);
1831         if (ret != SQLITE_OK) {
1832                 ret = PM_PARSER_R_ERROR;
1833                 _LOGE("sqlite exec failed to insert entries into package_info!!");
1834                 goto err;
1835         }
1836
1837         /* Updating TEP info in "package_app_info" table */
1838         query = sqlite3_mprintf("UPDATE package_app_info "\
1839                                                 "SET app_tep_name = %Q "\
1840                                                 "WHERE package = %Q", tep_path, pkgid);
1841
1842         ret = __exec_query(query);
1843         sqlite3_free(query);
1844         if (ret != SQLITE_OK) {
1845                 ret = PM_PARSER_R_ERROR;
1846                 _LOGE("sqlite exec failed to insert entries into package_app_info!!");
1847                 goto err;
1848         }
1849
1850         /*Commit transaction*/
1851         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
1852         if (ret != SQLITE_OK) {
1853                 _LOGE("Failed to commit transaction, Rollback now\n");
1854                 ret = sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1855                 if (ret != SQLITE_OK)
1856                         _LOGE("Failed to Rollback\n");
1857
1858                 ret = PM_PARSER_R_ERROR;
1859                 goto err;
1860         }
1861         _LOGD("Transaction Commit and End\n");
1862         ret =  PM_PARSER_R_OK;
1863
1864 err:
1865         pkgmgr_parser_close_db();
1866         return ret;
1867 }
1868
1869
1870 API int pkgmgr_parser_update_manifest_info_in_usr_db(manifest_x *mfx, uid_t uid)
1871 {
1872         if (mfx == NULL) {
1873                 _LOGD("manifest pointer is NULL\n");
1874                 return -1;
1875         }
1876         int ret = 0;
1877         ret = pkgmgr_parser_check_and_create_db(uid);
1878         if (ret == -1) {
1879                 _LOGD("Failed to open DB\n");
1880                 return ret;
1881         }
1882         ret = pkgmgr_parser_initialize_db(uid);
1883         if (ret == -1)
1884                 goto err;
1885         /*Preserve guest mode visibility*/
1886         __preserve_guestmode_visibility_value( mfx);
1887         /*Begin transaction*/
1888         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
1889         if (ret != SQLITE_OK) {
1890                 _LOGD("Failed to begin transaction\n");
1891                 ret = -1;
1892                 goto err;
1893         }
1894         _LOGD("Transaction Begin\n");
1895         ret = __delete_manifest_info_from_db(mfx, uid);
1896         if (ret == -1) {
1897                 _LOGD("Delete from DB failed. Rollback now\n");
1898                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1899                 goto err;
1900         }
1901         ret = __insert_manifest_info_in_db(mfx, uid);
1902         if (ret == -1) {
1903                 _LOGD("Insert into DB failed. Rollback now\n");
1904                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1905                 goto err;
1906         }
1907
1908         /*Commit transaction*/
1909         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
1910         if (ret != SQLITE_OK) {
1911                 _LOGD("Failed to commit transaction. Rollback now\n");
1912                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1913                 ret = -1;
1914                 goto err;
1915         }
1916         _LOGD("Transaction Commit and End\n");
1917 err:
1918         pkgmgr_parser_close_db();
1919         return ret;
1920 }
1921
1922 API int pkgmgr_parser_update_manifest_info_in_db(manifest_x *mfx)
1923 {
1924         return pkgmgr_parser_update_manifest_info_in_usr_db(mfx, GLOBAL_USER);
1925 }
1926
1927 API int pkgmgr_parser_delete_manifest_info_from_usr_db(manifest_x *mfx, uid_t uid)
1928 {
1929         if (mfx == NULL) {
1930                 _LOGD("manifest pointer is NULL\n");
1931                 return -1;
1932         }
1933         int ret = 0;
1934         ret = pkgmgr_parser_check_and_create_db(uid);
1935         if (ret == -1) {
1936                 _LOGD("Failed to open DB\n");
1937                 return ret;
1938         }
1939         /*Begin transaction*/
1940         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
1941         if (ret != SQLITE_OK) {
1942                 _LOGD("Failed to begin transaction\n");
1943                 ret = -1;
1944                 goto err;
1945         }
1946         _LOGD("Transaction Begin\n");
1947         ret = __delete_manifest_info_from_db(mfx, uid);
1948         if (ret == -1) {
1949                 _LOGD("Delete from DB failed. Rollback now\n");
1950                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1951                 goto err;
1952         }
1953         /*Commit transaction*/
1954         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
1955         if (ret != SQLITE_OK) {
1956                 _LOGD("Failed to commit transaction, Rollback now\n");
1957                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1958                 ret = -1;
1959                 goto err;
1960         }
1961         _LOGD("Transaction Commit and End\n");
1962 err:
1963         pkgmgr_parser_close_db();
1964         return ret;
1965 }
1966
1967 API int pkgmgr_parser_delete_manifest_info_from_db(manifest_x *mfx)
1968 {
1969         return pkgmgr_parser_delete_manifest_info_from_usr_db(mfx, GLOBAL_USER);
1970 }
1971
1972 API int pkgmgr_parser_update_preload_info_in_db()
1973 {
1974         int ret = 0;
1975         ret = pkgmgr_parser_check_and_create_db(GLOBAL_USER);
1976         if (ret == -1) {
1977                 _LOGD("Failed to open DB\n");
1978                 return ret;
1979         }
1980         /*Begin transaction*/
1981         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
1982         if (ret != SQLITE_OK) {
1983                 _LOGD("Failed to begin transaction\n");
1984                 ret = -1;
1985                 goto err;
1986         }
1987         _LOGD("Transaction Begin\n");
1988         ret = __update_preload_condition_in_db();
1989         if (ret == -1) {
1990                 _LOGD("__update_preload_condition_in_db failed. Rollback now\n");
1991                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1992                 goto err;
1993         }
1994         /*Commit transaction*/
1995         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
1996         if (ret != SQLITE_OK) {
1997                 _LOGD("Failed to commit transaction, Rollback now\n");
1998                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1999                 ret = -1;
2000                 goto err;
2001         }
2002         _LOGD("Transaction Commit and End\n");
2003 err:
2004         pkgmgr_parser_close_db();
2005         return ret;
2006 }
2007
2008 API int pkgmgr_parser_update_preload_info_in_usr_db(uid_t uid)
2009 {
2010         int ret = 0;
2011         ret = pkgmgr_parser_check_and_create_db(uid);
2012         if (ret == -1) {
2013                 _LOGD("Failed to open DB\n");
2014                 return ret;
2015         }
2016         /*Begin transaction*/
2017         ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2018         if (ret != SQLITE_OK) {
2019                 _LOGD("Failed to begin transaction\n");
2020                 ret = -1;
2021                 goto err;
2022         }
2023         _LOGD("Transaction Begin\n");
2024         ret = __update_preload_condition_in_db();
2025         if (ret == -1) {
2026                 _LOGD("__update_preload_condition_in_db failed. Rollback now\n");
2027                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2028                 goto err;
2029         }
2030         /*Commit transaction*/
2031         ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
2032         if (ret != SQLITE_OK) {
2033                 _LOGD("Failed to commit transaction, Rollback now\n");
2034                 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2035                 ret = -1;
2036                 goto err;
2037         }
2038         _LOGD("Transaction Commit and End\n");
2039 err:
2040         pkgmgr_parser_close_db();
2041         return ret;
2042 }