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