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