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