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