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