4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
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>
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 #include <sys/types.h>
27 #include <sys/smack.h>
35 /* For multi-user support */
36 #include <tzplatform_config.h>
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"
47 #define LOG_TAG "PKGMGR_PARSER"
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
55 #define DB_LABEL "User::Home"
56 #define SET_SMACK_LABEL(x) \
58 if (smack_setlabel((x), DB_LABEL, SMACK_LABEL_ACCESS)) \
59 _LOGE("failed chsmack -a %s %s", DB_LABEL, x); \
61 _LOGD("chsmack -a %s %s", DB_LABEL, x); \
64 sqlite3 *pkgmgr_parser_db;
65 sqlite3 *pkgmgr_cert_db;
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, " \
86 "installed_time text," \
87 "installed_storage text," \
88 "storeclient_id text," \
93 "package_support_disable text DEFAULT 'false', " \
94 "package_disable text DEFAULT 'false')"
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, " \
104 "PRIMARY KEY(package, package_locale), " \
105 "FOREIGN KEY(package) " \
106 "REFERENCES package_info(package) " \
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) " \
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, " \
121 "app_nodisplay text DEFAULT 'false', " \
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 "component_type text, " \
148 "package text not null, " \
149 "app_tep_name text, " \
150 "app_background_category INTEGER DEFAULT 0, " \
151 "FOREIGN KEY(package) " \
152 "REFERENCES package_info(package) " \
155 #define QUERY_CREATE_TABLE_PACKAGE_APP_LOCALIZED_INFO "create table if not exists package_app_localized_info " \
156 "(app_id text not null, " \
157 "app_locale text DEFAULT 'No Locale', " \
160 "PRIMARY KEY(app_id,app_locale) " \
161 "FOREIGN KEY(app_id) " \
162 "REFERENCES package_app_info(app_id) " \
165 #define QUERY_CREATE_TABLE_PACKAGE_APP_ICON_SECTION_INFO "create table if not exists package_app_icon_section_info " \
166 "(app_id text not null, " \
168 "app_icon_section text, " \
169 "app_icon_resolution text, " \
170 "PRIMARY KEY(app_id,app_icon_section,app_icon_resolution) " \
171 "FOREIGN KEY(app_id) " \
172 "REFERENCES package_app_info(app_id) " \
175 #define QUERY_CREATE_TABLE_PACKAGE_APP_IMAGE_INFO "create table if not exists package_app_image_info " \
176 "(app_id text not null, " \
177 "app_locale text DEFAULT 'No Locale', " \
178 "app_image_section text, " \
180 "PRIMARY KEY(app_id,app_image_section) " \
181 "FOREIGN KEY(app_id) " \
182 "REFERENCES package_app_info(app_id) " \
185 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_CONTROL "create table if not exists package_app_app_control " \
186 "(app_id text not null, " \
187 "app_control text not null, " \
188 "PRIMARY KEY(app_id,app_control) " \
189 "FOREIGN KEY(app_id) " \
190 "REFERENCES package_app_info(app_id) " \
193 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_CATEGORY "create table if not exists package_app_app_category " \
194 "(app_id text not null, " \
195 "category text not null, " \
196 "PRIMARY KEY(app_id,category) " \
197 "FOREIGN KEY(app_id) " \
198 "REFERENCES package_app_info(app_id) " \
201 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_METADATA "create table if not exists package_app_app_metadata " \
202 "(app_id text not null, " \
203 "md_key text not null, " \
204 "md_value text not null, " \
205 "PRIMARY KEY(app_id, md_key, md_value) " \
206 "FOREIGN KEY(app_id) " \
207 "REFERENCES package_app_info(app_id) " \
210 #define QUERY_CREATE_TABLE_PACKAGE_APP_APP_PERMISSION "create table if not exists package_app_app_permission " \
211 "(app_id text not null, " \
212 "pm_type text not null, " \
213 "pm_value text not null, " \
214 "PRIMARY KEY(app_id, pm_type, pm_value) " \
215 "FOREIGN KEY(app_id) " \
216 "REFERENCES package_app_info(app_id) " \
219 #define QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_ALLOWED "create table if not exists package_app_share_allowed " \
220 "(app_id text not null, " \
221 "data_share_path text not null, " \
222 "data_share_allowed text not null, " \
223 "PRIMARY KEY(app_id,data_share_path,data_share_allowed) " \
224 "FOREIGN KEY(app_id) " \
225 "REFERENCES package_app_info(app_id) " \
228 #define QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_REQUEST "create table if not exists package_app_share_request " \
229 "(app_id text not null, " \
230 "data_share_request text not null, " \
231 "PRIMARY KEY(app_id,data_share_request) " \
232 "FOREIGN KEY(app_id) " \
233 "REFERENCES package_app_info(app_id) " \
236 /* FIXME: duplicated at pkgmgrinfo_db.c */
237 #define QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO \
238 "CREATE TABLE IF NOT EXISTS package_cert_index_info( " \
239 " cert_info TEXT UNIQUE, " \
240 " cert_id INTEGER PRIMARY KEY, " \
241 " cert_ref_count INTEGER NOT NULL)"
243 #define QUERY_CREATE_TABLE_PACKAGE_CERT_INFO \
244 "CREATE TABLE IF NOT EXISTS package_cert_info( " \
245 " package TEXT PRIMARY KEY, " \
246 " author_root_cert INTEGER, " \
247 " author_im_cert INTEGER, " \
248 " author_signer_cert INTEGER, " \
249 " dist_root_cert INTEGER, " \
250 " dist_im_cert INTEGER, " \
251 " dist_signer_cert INTEGER, " \
252 " dist2_root_cert INTEGER, " \
253 " dist2_im_cert INTEGER, " \
254 " dist2_signer_cert INTEGER)"
256 #define QUERY_CREATE_TRIGGER_DELETE_CERT_INFO \
257 "CREATE TRIGGER IF NOT EXISTS delete_cert_info " \
258 "AFTER DELETE ON package_cert_info " \
260 " UPDATE package_cert_index_info SET" \
261 " cert_ref_count = cert_ref_count - 1" \
262 " WHERE cert_id = OLD.author_root_cert" \
263 " OR cert_id = OLD.author_im_cert" \
264 " OR cert_id = OLD.author_signer_cert" \
265 " OR cert_id = OLD.dist_root_cert" \
266 " OR cert_id = OLD.dist_im_cert" \
267 " OR cert_id = OLD.dist_signer_cert" \
268 " OR cert_id = OLD.dist2_root_cert" \
269 " OR cert_id = OLD.dist2_im_cert" \
270 " OR cert_id = OLD.dist2_signer_cert;" \
273 #define QUERY_CREATE_TRIGGER_UPDATE_CERT_INDEX_INFO \
274 "CREATE TRIGGER IF NOT EXISTS update_cert_index_info " \
275 "AFTER UPDATE ON package_cert_index_info " \
276 "WHEN ((SELECT cert_ref_count FROM package_cert_index_info " \
277 " WHERE cert_id = OLD.cert_id) = 0) "\
279 " DELETE FROM package_cert_index_info WHERE cert_id = OLD.cert_id;" \
282 #define QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO_FORMAT \
283 "CREATE TRIGGER IF NOT EXISTS update_%s_info " \
284 "AFTER UPDATE ON package_cert_info " \
285 "WHEN (OLD.%s IS NOT NULL) " \
287 " UPDATE package_cert_index_info SET" \
288 " cert_ref_count = cert_ref_count - 1" \
289 " WHERE cert_id = OLD.%s;" \
292 #define QUERY_CREATE_TABLE_PACKAGE_APP_DATA_CONTROL "create table if not exists package_app_data_control " \
293 "(app_id text not null, " \
294 "providerid text not null, " \
295 "access text not null, " \
296 "type text not null, " \
297 "PRIMARY KEY(app_id, providerid, access, type) " \
298 "FOREIGN KEY(app_id) " \
299 "REFERENCES package_app_info(app_id) " \
302 #define QUERY_CREATE_TABLE_PACKAGE_APP_DISABLE_FOR_USER "CREATE TABLE IF NOT EXISTS package_app_disable_for_user " \
303 "(app_id text not null, " \
304 "uid text not null, " \
305 "PRIMARY KEY(app_id, uid))"
307 static int __insert_application_info(manifest_x *mfx);
308 static int __insert_application_appcategory_info(manifest_x *mfx);
309 static int __insert_application_appcontrol_info(manifest_x *mfx);
310 static int __insert_application_appmetadata_info(manifest_x *mfx);
311 static int __insert_application_share_allowed_info(manifest_x *mfx);
312 static int __insert_application_share_request_info(manifest_x *mfx);
313 static int __insert_application_datacontrol_info(manifest_x *mfx);
314 static void __insert_application_locale_info(gpointer data, gpointer userdata);
315 static void __insert_pkglocale_info(gpointer data, gpointer userdata);
316 static int __insert_manifest_info_in_db(manifest_x *mfx, uid_t uid);
317 static int __delete_manifest_info_from_db(manifest_x *mfx, uid_t uid);
318 static int __delete_subpkg_info_from_db(char *appid);
319 static int __delete_appinfo_from_db(char *db_table, const char *appid);
320 static int __initialize_db(sqlite3 *db_handle, const char *db_query);
321 static int __exec_query(char *query);
322 static void __extract_data(gpointer data, GList *lbl, GList *lcn, GList *icn, GList *dcn, GList *ath,
323 char **label, char **license, char **icon, char **description, char **author);
324 static gint __comparefunc(gconstpointer a, gconstpointer b, gpointer userdata);
325 static GList *__create_locale_list(GList *locale, GList *lbl, GList *lcn, GList *icn, GList *dcn, GList *ath);
326 static void __preserve_guestmode_visibility_value(manifest_x *mfx);
327 static int __guestmode_visibility_cb(void *data, int ncols, char **coltxt, char **colname);
328 static int __pkgmgr_parser_create_db(sqlite3 **db_handle, const char *db_path);
329 static int __parserdb_change_perm(const char *db_file, uid_t uid);
331 static int __delete_subpkg_list_cb(void *data, int ncols, char **coltxt, char **colname)
334 __delete_subpkg_info_from_db(coltxt[0]);
339 static const char *__get_str(const char *str)
343 return PKGMGR_PARSER_EMPTY_STR;
349 static int __pkgmgr_parser_create_db(sqlite3 **db_handle, const char *db_path)
354 ret = db_util_open(db_path, &handle, DB_UTIL_REGISTER_HOOK_METHOD);
355 if (ret != SQLITE_OK) {
356 _LOGD("connect db [%s] failed!\n", db_path);
364 static int __guestmode_visibility_cb(void *data, int ncols, char **coltxt, char **colname)
366 manifest_x *mfx = (manifest_x *)data;
372 if (mfx->application == NULL)
374 app = (application_x *)mfx->application->data;
375 for(i = 0; i < ncols; i++)
377 if (strcmp(colname[i], "app_id") == 0) {
379 appid = strdup(coltxt[i]);
380 } else if (strcmp(colname[i], "app_guestmodevisibility") == 0) {
382 status = strdup(coltxt[i]);
386 _LOGD("app id is NULL\n");
389 /*update guest mode visibility*/
390 for (tmp = mfx->application; tmp; tmp = tmp->next) {
391 app = (application_x *)tmp->data;
394 if (strcmp(app->appid, appid) == 0) {
395 free((void *)app->guestmode_visibility);
396 app->guestmode_visibility = strdup(status);
412 static void __preserve_guestmode_visibility_value(manifest_x *mfx)
414 char *error_message = NULL;
415 char query[MAX_QUERY_LEN] = {'\0'};
416 snprintf(query, MAX_QUERY_LEN - 1, "select app_id, app_guestmodevisibility from package_app_info where package='%s'", mfx->package);
418 sqlite3_exec(pkgmgr_parser_db, query,
419 __guestmode_visibility_cb, (void *)mfx, &error_message)) {
420 _LOGD("Don't execute query = %s error message = %s\n",
421 query, error_message);
422 sqlite3_free(error_message);
427 static int __initialize_db(sqlite3 *db_handle, const char *db_query)
429 char *error_message = NULL;
431 sqlite3_exec(db_handle, db_query,
432 NULL, NULL, &error_message)) {
433 _LOGD("Don't execute query = %s error message = %s\n",
434 db_query, error_message);
435 sqlite3_free(error_message);
438 sqlite3_free(error_message);
442 static int __exec_query(char *query)
444 char *error_message = NULL;
446 sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, &error_message)) {
447 _LOGE("Don't execute query = %s error message = %s\n", query,
449 sqlite3_free(error_message);
452 sqlite3_free(error_message);
456 static int __exec_query_no_msg(char *query)
458 char *error_message = NULL;
460 sqlite3_exec(pkgmgr_parser_db, query, NULL, NULL, &error_message)) {
461 sqlite3_free(error_message);
464 sqlite3_free(error_message);
468 static GList *__create_locale_list(GList *locale, GList *lbls, GList *lcns, GList *icns, GList *dcns, GList *aths)
476 for (tmp = lbls; tmp; tmp = tmp->next) {
477 lbl = (label_x *)tmp->data;
481 locale = g_list_insert_sorted_with_data(locale, (gpointer)lbl->lang, __comparefunc, NULL);
483 for (tmp = lcns; tmp; tmp = tmp->next) {
484 lcn = (license_x *)tmp->data;
488 locale = g_list_insert_sorted_with_data(locale, (gpointer)lcn->lang, __comparefunc, NULL);
490 for (tmp = icns; tmp; tmp = tmp->next) {
491 icn = (icon_x *)tmp->data;
495 locale = g_list_insert_sorted_with_data(locale, (gpointer)icn->lang, __comparefunc, NULL);
497 for (tmp = dcns; tmp; tmp = tmp->next) {
498 dcn = (description_x *)tmp->data;
502 locale = g_list_insert_sorted_with_data(locale, (gpointer)dcn->lang, __comparefunc, NULL);
504 for (tmp = aths; tmp; tmp = tmp->next) {
505 ath = (author_x *)tmp->data;
509 locale = g_list_insert_sorted_with_data(locale, (gpointer)ath->lang, __comparefunc, NULL);
515 static GList *__create_icon_list(GList *locale, GList *icns)
520 for (tmp = icns; tmp; tmp = tmp->next) {
521 icn = (icon_x *)tmp->data;
525 locale = g_list_insert_sorted_with_data(locale, (gpointer)icn->section, __comparefunc, NULL);
530 static GList *__create_image_list(GList *locale, GList *imgs)
535 for (tmp = imgs; tmp; tmp = tmp->next) {
536 img = (image_x *)tmp->data;
540 locale = g_list_insert_sorted_with_data(locale, (gpointer)img->section, __comparefunc, NULL);
545 static void __trimfunc(GList* trim_list)
547 char *trim_data = NULL;
551 list = g_list_first(trim_list);
554 trim_data = (char *)list->data;
557 if (strcmp(trim_data, prev) == 0) {
558 trim_list = g_list_remove(trim_list, trim_data);
559 list = g_list_first(trim_list);
568 list = g_list_next(list);
572 static gint __comparefunc(gconstpointer a, gconstpointer b, gpointer userdata)
574 if (a == NULL || b == NULL)
576 if (strcmp((char*)a, (char*)b) == 0)
578 if (strcmp((char*)a, (char*)b) < 0)
580 if (strcmp((char*)a, (char*)b) > 0)
585 static void __extract_data(gpointer data, GList *lbls, GList *lcns, GList *icns, GList *dcns, GList *aths,
586 char **label, char **license, char **icon, char **description, char **author)
594 for (tmp = lbls; tmp; tmp = tmp->next) {
595 lbl = (label_x *)tmp->data;
599 if (strcmp(lbl->lang, (char *)data) == 0) {
600 *label = (char*)lbl->text;
605 for (tmp = lcns; tmp; tmp = tmp->next) {
606 lcn = (license_x *)tmp->data;
610 if (strcmp(lcn->lang, (char *)data) == 0) {
611 *license = (char*)lcn->text;
616 for (tmp = icns; tmp; tmp = tmp->next) {
617 icn = (icon_x *)tmp->data;
621 if (strcmp(icn->lang, (char *)data) == 0) {
622 *icon = (char*)icn->text;
627 for (tmp = dcns; tmp; tmp = tmp->next) {
628 dcn = (description_x *)tmp->data;
632 if (strcmp(dcn->lang, (char *)data) == 0) {
633 *description = (char*)dcn->text;
638 for (tmp = aths; tmp; tmp = tmp->next) {
639 ath = (author_x *)tmp->data;
643 if (strcmp(ath->lang, (char *)data) == 0) {
644 *author = (char*)ath->text;
652 static void __extract_icon_data(gpointer data, GList *icns, char **icon, char **resolution)
656 for (tmp = icns; tmp; tmp = tmp->next) {
657 icn = (icon_x *)tmp->data;
661 if (strcmp(icn->section, (char *)data) == 0) {
662 *icon = (char*)icn->text;
663 *resolution = (char*)icn->resolution;
670 static void __extract_image_data(gpointer data, GList *imgs, char **lang, char **image)
674 for (tmp = imgs; tmp; tmp = tmp->next) {
675 img = (image_x *)tmp->data;
679 if (strcmp(img->section, (char *)data) == 0) {
680 *lang = (char*)img->lang;
681 *image = (char*)img->text;
688 static void __insert_pkglocale_info(gpointer data, gpointer userdata)
693 char *description = NULL;
694 char *license = NULL;
696 char query[MAX_QUERY_LEN] = {'\0'};
698 manifest_x *mfx = (manifest_x *)userdata;
699 GList *lbl = mfx->label;
700 GList *lcn = mfx->license;
701 GList *icn = mfx->icon;
702 GList *dcn = mfx->description;
703 GList *ath = mfx->author;
705 __extract_data(data, lbl, lcn, icn, dcn, ath, &label, &license, &icon, &description, &author);
706 if (!label && !description && !icon && !license && !author)
709 sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_localized_info(package, package_locale, " \
710 "package_label, package_icon, package_description, package_license, package_author) values " \
711 "('%q', '%q', '%q', '%q', '%s', '%s', '%s')",
716 __get_str(description),
720 ret = __exec_query(query);
722 _LOGD("Package Localized Info DB Insert failed\n");
725 static void __insert_application_locale_info(gpointer data, gpointer userdata)
730 char query[MAX_QUERY_LEN] = {'\0'};
732 application_x *app = (application_x*)userdata;
733 GList *lbl = app->label;
734 GList *icn = app->icon;
736 __extract_data(data, lbl, NULL, icn, NULL, NULL, &label, NULL, &icon, NULL, NULL);
739 sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_app_localized_info(app_id, app_locale, " \
740 "app_label, app_icon) values " \
741 "('%q', '%q', '%q', '%q')", app->appid, (char*)data,
743 ret = __exec_query(query);
745 _LOGD("Package UiApp Localized Info DB Insert failed\n");
747 /*insert ui app locale info to pkg locale to get mainapp data */
748 if (strcasecmp(app->mainapp, "true")==0) {
749 sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_localized_info(package, package_locale, " \
750 "package_label, package_icon, package_description, package_license, package_author) values " \
751 "('%q', '%q', '%q', '%q', '%q', '%q', '%q')",
756 PKGMGR_PARSER_EMPTY_STR,
757 PKGMGR_PARSER_EMPTY_STR,
758 PKGMGR_PARSER_EMPTY_STR);
760 ret = __exec_query_no_msg(query);
763 sqlite3_snprintf(MAX_QUERY_LEN, query, "update package_localized_info set package_icon='%s' "\
764 "where package='%s' and package_locale='%s'", icon, app->package, (char*)data);
765 ret = __exec_query_no_msg(query);
770 static void __insert_application_icon_section_info(gpointer data, gpointer userdata)
774 char *resolution = NULL;
775 char query[MAX_QUERY_LEN] = {'\0'};
777 application_x *app = (application_x*)userdata;
778 GList *icn = app->icon;
780 __extract_icon_data(data, icn, &icon, &resolution);
781 if (!icon && !resolution)
783 sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_app_icon_section_info(app_id, " \
784 "app_icon, app_icon_section, app_icon_resolution) values " \
785 "('%q', '%q', '%q', '%q')", app->appid,
786 icon, (char*)data, resolution);
788 ret = __exec_query(query);
790 _LOGD("Package UiApp Localized Info DB Insert failed\n");
794 static void __insert_application_image_info(gpointer data, gpointer userdata)
799 char query[MAX_QUERY_LEN] = {'\0'};
801 application_x *app = (application_x*)userdata;
802 GList *image = app->image;
804 __extract_image_data(data, image, &lang, &img);
807 sqlite3_snprintf(MAX_QUERY_LEN, query, "insert into package_app_image_info(app_id, app_locale, " \
808 "app_image_section, app_image) values " \
809 "('%q', '%q', '%q', '%q')", app->appid, lang, (char*)data, img);
811 ret = __exec_query(query);
813 _LOGD("Package UiApp image Info DB Insert failed\n");
818 static int __insert_mainapp_info(manifest_x *mfx)
823 char query[MAX_QUERY_LEN] = {'\0'};
824 for (tmp = mfx->application; tmp; tmp = tmp->next) {
825 app = (application_x *)tmp->data;
828 snprintf(query, MAX_QUERY_LEN,
829 "update package_app_info set app_mainapp='%s' where app_id='%s'", app->mainapp, app->appid);
831 ret = __exec_query(query);
833 _LOGD("Package App Info DB Insert Failed\n");
836 if (strcasecmp(app->mainapp, "True")==0)
837 mfx->mainapp_id = strdup(app->appid);
840 if (mfx->mainapp_id == NULL) {
841 if (mfx->application == NULL)
843 app = (application_x *)mfx->application->data;
847 snprintf(query, MAX_QUERY_LEN, "update package_app_info set app_mainapp='true' where app_id='%s'", app->appid);
849 _LOGD("Not valid appid\n");
853 ret = __exec_query(query);
855 _LOGD("Package UiApp Info DB Insert Failed\n");
859 free((void *)app->mainapp);
860 app->mainapp= strdup("true");
861 mfx->mainapp_id = strdup(app->appid);
864 memset(query, '\0', MAX_QUERY_LEN);
865 snprintf(query, MAX_QUERY_LEN,
866 "update package_info set mainapp_id='%s' where package='%s'", mfx->mainapp_id, mfx->package);
867 ret = __exec_query(query);
869 _LOGD("Package Info DB update Failed\n");
876 static int __convert_background_category(GList *category_list)
879 GList *tmp_list = category_list;
880 char *category_data = NULL;
882 if (category_list == NULL)
885 while (tmp_list != NULL) {
886 category_data = (char *)tmp_list->data;
887 if (strcmp(category_data, APP_BG_CATEGORY_MEDIA_STR) == 0) {
888 ret = ret | APP_BG_CATEGORY_MEDIA_VAL;
889 } else if (strcmp(category_data, APP_BG_CATEGORY_DOWNLOAD_STR) == 0) {
890 ret = ret | APP_BG_CATEGORY_DOWNLOAD_VAL;
891 } else if (strcmp(category_data, APP_BG_CATEGORY_BGNETWORK_STR) == 0) {
892 ret = ret | APP_BG_CATEGORY_BGNETWORK_VAL;
893 } else if (strcmp(category_data, APP_BG_CATEGORY_LOCATION_STR) == 0) {
894 ret = ret | APP_BG_CATEGORY_LOCATION_VAL;
895 } else if (strcmp(category_data, APP_BG_CATEGORY_SENSOR_STR) == 0) {
896 ret = ret | APP_BG_CATEGORY_SENSOR_VAL;
897 } else if (strcmp(category_data, APP_BG_CATEGORY_IOTCOMM_STR) == 0) {
898 ret = ret | APP_BG_CATEGORY_IOTCOMM_VAL;
899 } else if (strcmp(category_data, APP_BG_CATEGORY_SYSTEM) == 0) {
900 ret = ret | APP_BG_CATEGORY_SYSTEM_VAL;
902 _LOGE("Unidentified category [%s]", category_data);
904 tmp_list = g_list_next(tmp_list);
910 /* _PRODUCT_LAUNCHING_ENHANCED_
911 * app->indicatordisplay, app->portraitimg, app->landscapeimg, app->guestmode_appstatus
913 static int __insert_application_info(manifest_x *mfx)
918 int background_value = 0;
919 char query[MAX_QUERY_LEN] = {'\0'};
921 for (tmp = mfx->application; tmp; tmp = tmp->next) {
922 app = (application_x *)tmp->data;
926 background_value = __convert_background_category(app->background_category);
927 if (background_value < 0) {
928 _LOGE("Failed to retrieve background value[%d]", background_value);
929 background_value = 0;
932 snprintf(query, MAX_QUERY_LEN,
933 "insert into package_app_info(" \
934 "app_id, app_component, app_exec, app_nodisplay, app_type, " \
935 "app_onboot, app_multiple, app_autorestart, app_taskmanage, app_enabled, " \
936 "app_hwacceleration, app_screenreader, app_mainapp, app_recentimage, app_launchcondition, " \
937 "app_indicatordisplay, app_portraitimg, app_landscapeimg, app_guestmodevisibility, app_permissiontype, " \
938 "app_preload, app_submode, app_submode_mainid, app_installed_storage, app_process_pool, " \
939 "app_launch_mode, app_ui_gadget, app_support_disable, component_type, package, " \
940 "app_tep_name, app_background_category) " \
942 "'%s', '%s', '%s', '%s', '%s', " \
943 "'%s', '%s', '%s', '%s', '%s', " \
944 "'%s', '%s', '%s', '%s', '%s', " \
945 "'%s', '%s', '%s', '%s', '%s', " \
946 "'%s', '%s', '%s', '%s', '%s', " \
947 "'%s', '%s', '%s', '%s', '%s', " \
949 app->appid, app->component_type, app->exec, app->nodisplay, app->type,
950 app->onboot, app->multiple, app->autorestart, app->taskmanage, app->enabled,
951 app->hwacceleration, app->screenreader, app->mainapp, __get_str(app->recentimage), app->launchcondition,
952 app->indicatordisplay, __get_str(app->portraitimg), __get_str(app->landscapeimg),
953 app->guestmode_visibility, app->permission_type,
954 mfx->preload, app->submode, __get_str(app->submode_mainid), mfx->installed_storage, app->process_pool,
955 app->launch_mode, app->ui_gadget, mfx->support_disable, app->component_type, mfx->package,
956 __get_str(mfx->tep_name), background_value);
958 ret = __exec_query(query);
960 _LOGD("Package UiApp Info DB Insert Failed\n");
963 memset(query, '\0', MAX_QUERY_LEN);
968 static int __insert_application_appcategory_info(manifest_x *mfx)
975 char query[MAX_QUERY_LEN] = {'\0'};
976 for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
977 app = (application_x *)app_tmp->data;
980 for (ct_tmp = app->category; ct_tmp; ct_tmp = ct_tmp->next) {
981 ct = (const char *)ct_tmp->data;
984 snprintf(query, MAX_QUERY_LEN,
985 "insert into package_app_app_category(app_id, category) " \
986 "values('%s','%s')",\
988 ret = __exec_query(query);
990 _LOGD("Package UiApp Category Info DB Insert Failed\n");
993 memset(query, '\0', MAX_QUERY_LEN);
999 static int __insert_application_appmetadata_info(manifest_x *mfx)
1006 char query[MAX_QUERY_LEN] = {'\0'};
1007 for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
1008 app = (application_x *)app_tmp->data;
1011 for (md_tmp = app->metadata; md_tmp; md_tmp = md_tmp->next) {
1012 md = (metadata_x *)md_tmp->data;
1016 snprintf(query, MAX_QUERY_LEN,
1017 "insert into package_app_app_metadata(app_id, md_key, md_value) " \
1018 "values('%s','%s', '%s')",\
1019 app->appid, md->key, md->value ? md->value : "");
1020 ret = __exec_query(query);
1022 _LOGD("Package UiApp Metadata Info DB Insert Failed\n");
1026 memset(query, '\0', MAX_QUERY_LEN);
1032 static int __insert_application_apppermission_info(manifest_x *mfx)
1039 char query[MAX_QUERY_LEN] = {'\0'};
1040 for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
1041 app = (application_x *)app_tmp->data;
1044 for (pm_tmp = app->permission; pm_tmp; pm_tmp = pm_tmp->next) {
1045 pm = (permission_x *)pm_tmp->data;
1048 snprintf(query, MAX_QUERY_LEN,
1049 "insert into package_app_app_permission(app_id, pm_type, pm_value) " \
1050 "values('%s','%s', '%s')",\
1051 app->appid, pm->type, pm->value);
1052 ret = __exec_query(query);
1054 _LOGD("Package UiApp permission Info DB Insert Failed\n");
1057 memset(query, '\0', MAX_QUERY_LEN);
1063 static int __insert_application_appcontrol_info(manifest_x *mfx)
1067 GList *acontrol_tmp;
1068 appcontrol_x *acontrol;
1070 char query[MAX_QUERY_LEN] = {'\0'};
1071 char buf[BUFSIZE] = {'\0'};
1072 for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
1073 app = (application_x *)app_tmp->data;
1076 for (acontrol_tmp = app->appcontrol; acontrol_tmp; acontrol_tmp = acontrol_tmp->next) {
1077 acontrol = (appcontrol_x *)acontrol_tmp->data;
1078 if (acontrol == NULL)
1080 snprintf(buf, BUFSIZE, "%s|%s|%s",\
1081 acontrol->operation ? (strlen(acontrol->operation) > 0 ? acontrol->operation : "NULL") : "NULL",
1082 acontrol->uri ? (strlen(acontrol->uri) > 0 ? acontrol->uri : "NULL") : "NULL",
1083 acontrol->mime ? (strlen(acontrol->mime) > 0 ? acontrol->mime : "NULL") : "NULL");
1084 snprintf(query, MAX_QUERY_LEN,
1085 "insert into package_app_app_control(app_id, app_control) " \
1086 "values('%s', '%s')",\
1088 ret = __exec_query(query);
1090 _LOGD("Package UiApp AppSvc DB Insert Failed\n");
1093 memset(query, '\0', MAX_QUERY_LEN);
1099 static int __insert_application_datacontrol_info(manifest_x *mfx)
1106 char query[MAX_QUERY_LEN] = {'\0'};
1108 for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
1109 app = (application_x *)app_tmp->data;
1112 for (dc_tmp = app->datacontrol; dc_tmp; dc_tmp = dc_tmp->next) {
1113 dc = (datacontrol_x *)dc_tmp->data;
1116 snprintf(query, MAX_QUERY_LEN,
1117 "insert into package_app_data_control(app_id, providerid, access, type) " \
1118 "values('%s', '%s', '%s', '%s')",\
1124 ret = __exec_query(query);
1126 _LOGD("Package UiApp Data Control DB Insert Failed\n");
1129 memset(query, '\0', MAX_QUERY_LEN);
1135 static int __insert_application_share_request_info(manifest_x *mfx)
1144 char query[MAX_QUERY_LEN] = {'\0'};
1145 for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
1146 app = (application_x *)app_tmp->data;
1149 for (ds_tmp = app->datashare; ds_tmp; ds_tmp = ds_tmp->next) {
1150 ds = (datashare_x *)ds_tmp->data;
1153 for (rq_tmp = ds->request; rq_tmp; rq_tmp = rq_tmp->next) {
1154 rq = (const char *)rq_tmp->data;
1157 snprintf(query, MAX_QUERY_LEN,
1158 "insert into package_app_share_request(app_id, data_share_request) " \
1159 "values('%s', '%s')",\
1161 ret = __exec_query(query);
1163 _LOGD("Package UiApp Share Request DB Insert Failed\n");
1166 memset(query, '\0', MAX_QUERY_LEN);
1173 static int __insert_application_share_allowed_info(manifest_x *mfx)
1184 char query[MAX_QUERY_LEN] = {'\0'};
1185 for (app_tmp = mfx->application; app_tmp; app_tmp = app_tmp->next) {
1186 app = (application_x *)app_tmp->data;
1189 for (ds_tmp = app->datashare; ds_tmp; ds_tmp = ds_tmp->next) {
1190 ds = (datashare_x *)ds_tmp->data;
1193 for (df_tmp = ds->define; df_tmp; df_tmp = df_tmp->next) {
1194 df = (define_x *)df_tmp->data;
1197 for (al_tmp = df->allowed; al_tmp; al_tmp = al_tmp->next) {
1198 al = (const char *)al_tmp->data;
1201 snprintf(query, MAX_QUERY_LEN,
1202 "insert into package_app_share_allowed(app_id, data_share_path, data_share_allowed) " \
1203 "values('%s', '%s', '%s')",\
1204 app->appid, df->path, al);
1205 ret = __exec_query(query);
1207 _LOGD("Package UiApp Share Allowed DB Insert Failed\n");
1210 memset(query, '\0', MAX_QUERY_LEN);
1218 static int __insert_manifest_info_in_db(manifest_x *mfx, uid_t uid)
1222 const char *pv = NULL;
1223 char query[MAX_QUERY_LEN] = { '\0' };
1226 const char *auth_name = NULL;
1227 const char *auth_email = NULL;
1228 const char *auth_href = NULL;
1230 GList *pkglocale = NULL;
1231 GList *applocale = NULL;
1232 GList *appicon = NULL;
1233 GList *appimage = NULL;
1235 if (mfx->author && mfx->author->data) {
1236 author = (author_x *)mfx->author->data;
1238 auth_name = author->text;
1240 auth_email = author->email;
1242 auth_href = author->href;
1244 /*Insert in the package_cert_info CERT_DB*/
1245 pkgmgrinfo_instcertinfo_h cert_handle = NULL;
1246 ret = pkgmgrinfo_set_cert_value(&cert_handle, PMINFO_SET_AUTHOR_ROOT_CERT, "author root certificate");
1247 if (ret != PMINFO_R_OK) {
1248 pkgmgrinfo_destroy_certinfo_set_handle(cert_handle);
1249 _LOGE("Cert Info DB create handle failed\n");
1252 ret = pkgmgrinfo_save_certinfo(mfx->package, &cert_handle, uid);
1253 if (ret != PMINFO_R_OK) {
1254 pkgmgrinfo_destroy_certinfo_set_handle(cert_handle);
1255 _LOGE("Cert Info DB Insert Failed\n");
1259 /*Insert in the package_info DB*/
1260 snprintf(query, MAX_QUERY_LEN,
1261 "insert into package_info(" \
1262 "package, package_type, package_version, package_api_version, package_tep_name, " \
1263 "install_location, package_size, package_removable, package_preload, package_readonly, " \
1264 "package_update, package_appsetting, package_nodisplay, package_system, author_name, " \
1265 "author_email, author_href, installed_time, installed_storage, storeclient_id, " \
1266 "mainapp_id, package_url, root_path, csc_path, package_support_disable) " \
1268 "'%s', '%s', '%s', '%s', '%s', " \
1269 "'%s', '%s', '%s', '%s', '%s', " \
1270 "'%s', '%s', '%s', '%s', '%s', " \
1271 "'%s', '%s', '%s', '%s', '%s', " \
1272 "'%s', '%s', '%s', '%s', '%s')", \
1273 mfx->package, mfx->type, mfx->version, __get_str(mfx->api_version), __get_str(mfx->tep_name),
1274 __get_str(mfx->installlocation), __get_str(mfx->package_size), mfx->removable, mfx->preload, mfx->readonly,
1275 mfx->update, mfx->appsetting, mfx->nodisplay_setting, mfx->system, __get_str(auth_name),
1276 __get_str(auth_email), __get_str(auth_href), mfx->installed_time, mfx->installed_storage,
1277 __get_str(mfx->storeclient_id),
1278 mfx->mainapp_id, __get_str(mfx->package_url), mfx->root_path, __get_str(mfx->csc_path), mfx->support_disable);
1280 ret = __exec_query(query);
1282 _LOGD("Package Info DB Insert Failed\n");
1286 /*Insert in the package_privilege_info DB*/
1287 for (tmp = mfx->privileges; tmp; tmp = tmp->next) {
1288 pv = (const char *)tmp->data;
1291 memset(query, '\0', MAX_QUERY_LEN);
1292 snprintf(query, MAX_QUERY_LEN,
1293 "insert into package_privilege_info(package, privilege) " \
1294 "values('%s','%s')",\
1296 ret = __exec_query(query);
1298 _LOGD("Package Privilege Info DB Insert Failed\n");
1303 if (mfx->application != NULL) {
1304 ret = __insert_mainapp_info(mfx);
1309 /*Insert the package locale*/
1310 pkglocale = __create_locale_list(pkglocale, mfx->label, mfx->license, mfx->icon, mfx->description, mfx->author);
1311 /*remove duplicated data in pkglocale*/
1312 __trimfunc(pkglocale);
1314 /*Insert the app locale, icon, image info */
1315 for (tmp = mfx->application; tmp; tmp = tmp->next) {
1316 app = (application_x *)tmp->data;
1319 applocale = __create_locale_list(applocale, app->label, NULL, app->icon, NULL, NULL);
1320 appicon = __create_icon_list(appicon, app->icon);
1321 appimage = __create_image_list(appimage, app->image);
1323 /*remove duplicated data in applocale*/
1324 __trimfunc(applocale);
1325 __trimfunc(appicon);
1326 __trimfunc(appimage);
1328 g_list_foreach(pkglocale, __insert_pkglocale_info, (gpointer)mfx);
1330 /*native app locale info*/
1331 for (tmp = mfx->application; tmp; tmp = tmp->next) {
1332 app = (application_x *)tmp->data;
1335 g_list_foreach(applocale, __insert_application_locale_info, (gpointer)app);
1336 g_list_foreach(appicon, __insert_application_icon_section_info, (gpointer)app);
1337 g_list_foreach(appimage, __insert_application_image_info, (gpointer)app);
1340 g_list_free(pkglocale);
1342 g_list_free(applocale);
1344 g_list_free(appicon);
1346 g_list_free(appimage);
1349 /*Insert in the package_app_info DB*/
1350 ret = __insert_application_info(mfx);
1353 /*Insert in the package_app_app_control DB*/
1354 ret = __insert_application_appcontrol_info(mfx);
1358 /*Insert in the package_app_app_category DB*/
1359 ret = __insert_application_appcategory_info(mfx);
1363 /*Insert in the package_app_app_metadata DB*/
1364 ret = __insert_application_appmetadata_info(mfx);
1368 /*Insert in the package_app_app_permission DB*/
1369 ret = __insert_application_apppermission_info(mfx);
1373 /*Insert in the package_app_share_allowed DB*/
1374 ret = __insert_application_share_allowed_info(mfx);
1378 /*Insert in the package_app_share_request DB*/
1379 ret = __insert_application_share_request_info(mfx);
1383 /*Insert in the package_app_data_control DB*/
1384 ret = __insert_application_datacontrol_info(mfx);
1392 static int __delete_appinfo_from_db(char *db_table, const char *appid)
1394 char query[MAX_QUERY_LEN] = { '\0' };
1396 memset(query, '\0', MAX_QUERY_LEN);
1397 snprintf(query, MAX_QUERY_LEN,
1398 "delete from %s where app_id='%s'", db_table, appid);
1399 ret = __exec_query(query);
1401 _LOGD("DB Deletion from table (%s) Failed\n", db_table);
1407 static int __delete_subpkg_info_from_db(char *appid)
1411 ret = __delete_appinfo_from_db("package_app_info", appid);
1414 ret = __delete_appinfo_from_db("package_app_localized_info", appid);
1417 ret = __delete_appinfo_from_db("package_app_icon_section_info", appid);
1420 ret = __delete_appinfo_from_db("package_app_image_info", appid);
1423 ret = __delete_appinfo_from_db("package_app_app_control", appid);
1426 ret = __delete_appinfo_from_db("package_app_app_category", appid);
1429 ret = __delete_appinfo_from_db("package_app_app_metadata", appid);
1432 ret = __delete_appinfo_from_db("package_app_app_permission", appid);
1435 ret = __delete_appinfo_from_db("package_app_share_allowed", appid);
1438 ret = __delete_appinfo_from_db("package_app_share_request", appid);
1441 ret = __delete_appinfo_from_db("package_app_data_control", appid);
1448 static int __delete_subpkg_from_db(manifest_x *mfx)
1450 char query[MAX_QUERY_LEN] = { '\0' };
1451 char *error_message = NULL;
1453 snprintf(query, MAX_QUERY_LEN, "select app_id from package_app_info where package='%s'", mfx->package);
1455 sqlite3_exec(pkgmgr_parser_db, query, __delete_subpkg_list_cb, NULL, &error_message)) {
1456 _LOGE("Don't execute query = %s error message = %s\n", query,
1458 sqlite3_free(error_message);
1461 sqlite3_free(error_message);
1466 static int __delete_manifest_info_from_db(manifest_x *mfx, uid_t uid)
1468 char query[MAX_QUERY_LEN] = { '\0' };
1472 /*Delete from cert table*/
1473 ret = pkgmgrinfo_delete_certinfo(mfx->package);
1475 _LOGD("Cert Info DB Delete Failed\n");
1479 /*Delete from Package Info DB*/
1480 snprintf(query, MAX_QUERY_LEN,
1481 "delete from package_info where package='%s'", mfx->package);
1482 ret = __exec_query(query);
1484 _LOGD("Package Info DB Delete Failed\n");
1487 memset(query, '\0', MAX_QUERY_LEN);
1489 /*Delete from Package Localized Info*/
1490 snprintf(query, MAX_QUERY_LEN,
1491 "delete from package_localized_info where package='%s'", mfx->package);
1492 ret = __exec_query(query);
1494 _LOGD("Package Localized Info DB Delete Failed\n");
1498 /*Delete from Package Privilege Info*/
1499 snprintf(query, MAX_QUERY_LEN,
1500 "delete from package_privilege_info where package='%s'", mfx->package);
1501 ret = __exec_query(query);
1503 _LOGD("Package Privilege Info DB Delete Failed\n");
1507 for (tmp = mfx->application; tmp; tmp = tmp->next) {
1508 app = (application_x *)tmp->data;
1511 ret = __delete_appinfo_from_db("package_app_info", app->appid);
1514 ret = __delete_appinfo_from_db("package_app_localized_info", app->appid);
1517 ret = __delete_appinfo_from_db("package_app_icon_section_info", app->appid);
1520 ret = __delete_appinfo_from_db("package_app_image_info", app->appid);
1523 ret = __delete_appinfo_from_db("package_app_app_control", app->appid);
1526 ret = __delete_appinfo_from_db("package_app_app_category", app->appid);
1529 ret = __delete_appinfo_from_db("package_app_app_metadata", app->appid);
1532 ret = __delete_appinfo_from_db("package_app_app_permission", app->appid);
1535 ret = __delete_appinfo_from_db("package_app_share_allowed", app->appid);
1538 ret = __delete_appinfo_from_db("package_app_share_request", app->appid);
1541 ret = __delete_appinfo_from_db("package_app_data_control", app->appid);
1546 /* if main package has sub pkg, delete sub pkg data*/
1547 __delete_subpkg_from_db(mfx);
1552 static int __disable_global_app_for_user(const char *appid, uid_t uid)
1555 char query[MAX_QUERY_LEN] = {'\0'};
1557 sqlite3_snprintf(MAX_QUERY_LEN, query, "INSERT INTO " \
1558 "package_app_disable_for_user(app_id, uid) VALUES(%Q, %Q)",
1560 ret = __exec_query(query);
1562 _LOGD("Insert global app disable failed\n");
1567 static int __enable_global_app_for_user(const char *appid, uid_t uid)
1570 char query[MAX_QUERY_LEN] = {'\0'};
1572 sqlite3_snprintf(MAX_QUERY_LEN, query, "DELETE FROM " \
1573 "package_app_disable_for_user WHERE app_id=%Q AND uid=%Q",
1575 ret = __exec_query(query);
1577 _LOGD("Delete global app disable failed\n");
1582 static int __update_preload_condition_in_db()
1585 char query[MAX_QUERY_LEN] = {'\0'};
1587 snprintf(query, MAX_QUERY_LEN, "update package_info set package_preload='true'");
1589 ret = __exec_query(query);
1591 _LOGD("Package preload_condition update failed\n");
1596 API int pkgmgr_parser_initialize_db(uid_t uid)
1600 char query[MAX_QUERY_LEN];
1601 static const char *columns[] = {
1602 "author_root_cert", "author_im_cert", "author_signer_cert",
1603 "dist_root_cert", "dist_im_cert", "dist_signer_cert",
1604 "dist2_root_cert", "dist2_im_cert", "dist2_signer_cert",
1608 ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_INFO);
1610 _LOGD("package info DB initialization failed\n");
1613 ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_LOCALIZED_INFO);
1615 _LOGD("package localized info DB initialization failed\n");
1618 ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_PRIVILEGE_INFO);
1620 _LOGD("package app app privilege DB initialization failed\n");
1623 ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_INFO);
1625 _LOGD("package app info DB initialization failed\n");
1628 ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_LOCALIZED_INFO);
1630 _LOGD("package app localized info DB initialization failed\n");
1633 ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_ICON_SECTION_INFO);
1635 _LOGD("package app icon localized info DB initialization failed\n");
1638 ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_IMAGE_INFO);
1640 _LOGD("package app image info DB initialization failed\n");
1643 ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_CONTROL);
1645 _LOGD("package app app control DB initialization failed\n");
1648 ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_CATEGORY);
1650 _LOGD("package app app category DB initialization failed\n");
1653 ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_METADATA);
1655 _LOGD("package app app category DB initialization failed\n");
1658 ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_APP_PERMISSION);
1660 _LOGD("package app app permission DB initialization failed\n");
1663 ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_ALLOWED);
1665 _LOGD("package app share allowed DB initialization failed\n");
1668 ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_SHARE_REQUEST);
1670 _LOGD("package app share request DB initialization failed\n");
1673 ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_DATA_CONTROL);
1675 _LOGD("package app data control DB initialization failed\n");
1679 ret = __initialize_db(pkgmgr_parser_db, QUERY_CREATE_TABLE_PACKAGE_APP_DISABLE_FOR_USER);
1681 _LOGD("package app disable for user DB initialization failed\n");
1686 /* TODO: refactor this code */
1687 ret = __initialize_db(pkgmgr_cert_db, QUERY_CREATE_TABLE_PACKAGE_CERT_INFO);
1689 _LOGD("package cert info DB initialization failed\n");
1692 ret = __initialize_db(pkgmgr_cert_db, QUERY_CREATE_TABLE_PACKAGE_CERT_INDEX_INFO);
1694 _LOGD("package cert index info DB initialization failed\n");
1697 ret = __initialize_db(pkgmgr_cert_db, QUERY_CREATE_TRIGGER_DELETE_CERT_INFO);
1699 _LOGD("package cert info DB initialization failed\n");
1702 ret = __initialize_db(pkgmgr_cert_db, QUERY_CREATE_TRIGGER_UPDATE_CERT_INDEX_INFO);
1704 _LOGD("package cert index info DB initialization failed\n");
1707 for (i = 0; columns[i] != NULL; i++) {
1708 snprintf(query, sizeof(query),
1709 QUERY_CREATE_TRIGGER_UPDATE_CERT_INFO_FORMAT,
1710 columns[i], columns[i], columns[i], columns[i]);
1711 ret = __initialize_db(pkgmgr_cert_db, query);
1713 _LOGD("package cert index info DB initialization failed\n");
1718 if( 0 != __parserdb_change_perm(getUserPkgCertDBPathUID(GLOBAL_USER), GLOBAL_USER)) {
1719 _LOGD("Failed to change cert db permission\n");
1721 if( 0 != __parserdb_change_perm(getUserPkgParserDBPathUID(uid), uid)) {
1722 _LOGD("Failed to change parser db permission\n");
1728 static int __parserdb_change_perm(const char *db_file, uid_t uid)
1731 char journal_file[BUFSIZE];
1734 struct passwd *userinfo = NULL;
1735 files[0] = (char *)db_file;
1736 files[1] = journal_file;
1740 if (db_file == NULL)
1743 if (getuid() != OWNER_ROOT) //At this time we should be root to apply this
1745 snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal");
1746 if (uid == OWNER_ROOT)
1748 userinfo = getpwuid(uid);
1750 _LOGE("FAIL: user %d doesn't exist", uid);
1753 snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal");
1755 for (i = 0; files[i]; i++) {
1756 ret = chown(files[i], uid, userinfo->pw_gid);
1758 if (strerror_r(errno, buf, sizeof(buf)))
1760 _LOGD("FAIL : chown %s %d.%d : %s", files[i], uid,
1761 userinfo->pw_gid, buf);
1765 mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH;
1766 if (!strcmp(db_file, getUserPkgCertDBPathUID(GLOBAL_USER)))
1768 ret = chmod(files[i], mode);
1770 if (strerror_r(errno, buf, sizeof(buf)))
1772 _LOGD("FAIL : chmod %s 0664 : %s", files[i], buf);
1775 SET_SMACK_LABEL(files[i]);
1780 API int pkgmgr_parser_create_and_initialize_db(uid_t uid)
1784 if (getuid() != OWNER_ROOT) {
1785 _LOGE("Only root user is allowed");
1789 if (access(getUserPkgParserDBPathUID(uid), F_OK) != -1) {
1790 _LOGE("Manifest db for user %d is already exists", uid);
1794 if (access(getUserPkgCertDBPathUID(uid), F_OK) != -1) {
1795 _LOGE("Cert db for user %d is already exists", uid);
1799 ret = pkgmgr_parser_check_and_create_db(uid);
1802 ret = pkgmgr_parser_initialize_db(uid);
1804 pkgmgr_parser_close_db();
1807 pkgmgr_parser_close_db();
1812 API int pkgmgr_parser_check_and_create_db(uid_t uid)
1816 ret = __pkgmgr_parser_create_db(&pkgmgr_parser_db, getUserPkgParserDBPathUID(uid));
1818 _LOGD("Manifest DB creation Failed\n");
1823 ret = __pkgmgr_parser_create_db(&pkgmgr_cert_db, getUserPkgCertDBPathUID(GLOBAL_USER));
1825 _LOGD("Cert DB creation Failed\n");
1831 void pkgmgr_parser_close_db(void)
1833 sqlite3_close(pkgmgr_parser_db);
1834 sqlite3_close(pkgmgr_cert_db);
1838 API int pkgmgr_parser_insert_manifest_info_in_db(manifest_x *mfx)
1840 _LOGD("pkgmgr_parser_insert_manifest_info_in_db\n");
1842 _LOGD("manifest pointer is NULL\n");
1846 ret = pkgmgr_parser_check_and_create_db(GLOBAL_USER);
1848 _LOGD("Failed to open DB\n");
1851 ret = pkgmgr_parser_initialize_db(GLOBAL_USER);
1854 /*Begin transaction*/
1855 ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
1856 if (ret != SQLITE_OK) {
1857 _LOGD("Failed to begin transaction\n");
1861 _LOGD("Transaction Begin\n");
1862 ret = __insert_manifest_info_in_db(mfx, GLOBAL_USER);
1864 _LOGD("Insert into DB failed. Rollback now\n");
1865 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1868 /*Commit transaction*/
1869 ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
1870 if (ret != SQLITE_OK) {
1871 _LOGD("Failed to commit transaction. Rollback now\n");
1872 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1876 _LOGD("Transaction Commit and End\n");
1878 pkgmgr_parser_close_db();
1882 API int pkgmgr_parser_insert_manifest_info_in_usr_db(manifest_x *mfx, uid_t uid)
1884 _LOGD("pkgmgr_parser_insert_manifest_info_in_usr_db\n");
1886 _LOGD("manifest pointer is NULL\n");
1890 ret = pkgmgr_parser_check_and_create_db(uid);
1892 _LOGD("Failed to open DB\n");
1895 ret = pkgmgr_parser_initialize_db(uid);
1898 /*Begin transaction*/
1899 ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
1900 if (ret != SQLITE_OK) {
1901 _LOGD("Failed to begin transaction\n");
1905 _LOGD("Transaction Begin\n");
1906 ret = __insert_manifest_info_in_db(mfx, uid);
1908 _LOGD("Insert into DB failed. Rollback now\n");
1909 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1912 /*Commit transaction*/
1913 ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
1914 if (ret != SQLITE_OK) {
1915 _LOGD("Failed to commit transaction. Rollback now\n");
1916 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1920 _LOGD("Transaction Commit and End\n");
1922 pkgmgr_parser_close_db();
1926 API int pkgmgr_parser_update_tep_info_in_db(const char *pkgid, const char *tep_path)
1928 return pkgmgr_parser_update_tep_info_in_usr_db(pkgid, tep_path, GLOBAL_USER);
1931 API int pkgmgr_parser_update_tep_info_in_usr_db(const char *pkgid, const char *tep_path, uid_t uid)
1933 if (pkgid == NULL || tep_path == NULL) {
1934 _LOGE("invalid parameter");
1941 ret = pkgmgr_parser_check_and_create_db(uid);
1943 _LOGD("Failed to open DB\n");
1946 ret = pkgmgr_parser_initialize_db(uid);
1950 /*Begin transaction*/
1951 ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
1952 if (ret != SQLITE_OK) {
1953 _LOGD("Failed to begin transaction\n");
1957 _LOGD("Transaction Begin\n");
1960 /* Updating TEP info in "package_info" table */
1961 query = sqlite3_mprintf("UPDATE package_info "\
1962 "SET package_tep_name = %Q "\
1963 "WHERE package = %Q", tep_path, pkgid);
1965 ret = __exec_query(query);
1966 sqlite3_free(query);
1967 if (ret != SQLITE_OK) {
1968 ret = PM_PARSER_R_ERROR;
1969 _LOGE("sqlite exec failed to insert entries into package_info!!");
1973 /* Updating TEP info in "package_app_info" table */
1974 query = sqlite3_mprintf("UPDATE package_app_info "\
1975 "SET app_tep_name = %Q "\
1976 "WHERE package = %Q", tep_path, pkgid);
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_app_info!!");
1986 /*Commit transaction*/
1987 ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
1988 if (ret != SQLITE_OK) {
1989 _LOGE("Failed to commit transaction, Rollback now\n");
1990 ret = sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
1991 if (ret != SQLITE_OK)
1992 _LOGE("Failed to Rollback\n");
1994 ret = PM_PARSER_R_ERROR;
1997 _LOGD("Transaction Commit and End\n");
1998 ret = PM_PARSER_R_OK;
2001 pkgmgr_parser_close_db();
2006 API int pkgmgr_parser_update_manifest_info_in_usr_db(manifest_x *mfx, uid_t uid)
2009 _LOGD("manifest pointer is NULL\n");
2013 ret = pkgmgr_parser_check_and_create_db(uid);
2015 _LOGD("Failed to open DB\n");
2018 ret = pkgmgr_parser_initialize_db(uid);
2021 /*Preserve guest mode visibility*/
2022 __preserve_guestmode_visibility_value( mfx);
2023 /*Begin transaction*/
2024 ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2025 if (ret != SQLITE_OK) {
2026 _LOGD("Failed to begin transaction\n");
2030 _LOGD("Transaction Begin\n");
2031 ret = __delete_manifest_info_from_db(mfx, uid);
2033 _LOGD("Delete from DB failed. Rollback now\n");
2034 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2037 ret = __insert_manifest_info_in_db(mfx, uid);
2039 _LOGD("Insert into DB failed. Rollback now\n");
2040 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2044 /*Commit transaction*/
2045 ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
2046 if (ret != SQLITE_OK) {
2047 _LOGD("Failed to commit transaction. Rollback now\n");
2048 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2052 _LOGD("Transaction Commit and End\n");
2054 pkgmgr_parser_close_db();
2058 API int pkgmgr_parser_update_manifest_info_in_db(manifest_x *mfx)
2060 return pkgmgr_parser_update_manifest_info_in_usr_db(mfx, GLOBAL_USER);
2063 API int pkgmgr_parser_delete_manifest_info_from_usr_db(manifest_x *mfx, uid_t uid)
2066 _LOGD("manifest pointer is NULL\n");
2070 ret = pkgmgr_parser_check_and_create_db(uid);
2072 _LOGD("Failed to open DB\n");
2075 /*Begin transaction*/
2076 ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2077 if (ret != SQLITE_OK) {
2078 _LOGD("Failed to begin transaction\n");
2082 _LOGD("Transaction Begin\n");
2083 ret = __delete_manifest_info_from_db(mfx, uid);
2085 _LOGD("Delete from DB failed. Rollback now\n");
2086 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2089 /*Commit transaction*/
2090 ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
2091 if (ret != SQLITE_OK) {
2092 _LOGD("Failed to commit transaction, Rollback now\n");
2093 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2097 _LOGD("Transaction Commit and End\n");
2099 pkgmgr_parser_close_db();
2103 API int pkgmgr_parser_delete_manifest_info_from_db(manifest_x *mfx)
2105 return pkgmgr_parser_delete_manifest_info_from_usr_db(mfx, GLOBAL_USER);
2108 API int pkgmgr_parser_update_preload_info_in_db()
2111 ret = pkgmgr_parser_check_and_create_db(GLOBAL_USER);
2113 _LOGD("Failed to open DB\n");
2116 /*Begin transaction*/
2117 ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2118 if (ret != SQLITE_OK) {
2119 _LOGD("Failed to begin transaction\n");
2123 _LOGD("Transaction Begin\n");
2124 ret = __update_preload_condition_in_db();
2126 _LOGD("__update_preload_condition_in_db failed. Rollback now\n");
2127 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2130 /*Commit transaction*/
2131 ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
2132 if (ret != SQLITE_OK) {
2133 _LOGD("Failed to commit transaction, Rollback now\n");
2134 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2138 _LOGD("Transaction Commit and End\n");
2140 pkgmgr_parser_close_db();
2144 API int pkgmgr_parser_update_preload_info_in_usr_db(uid_t uid)
2147 ret = pkgmgr_parser_check_and_create_db(uid);
2149 _LOGD("Failed to open DB\n");
2152 /*Begin transaction*/
2153 ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2154 if (ret != SQLITE_OK) {
2155 _LOGD("Failed to begin transaction\n");
2159 _LOGD("Transaction Begin\n");
2160 ret = __update_preload_condition_in_db();
2162 _LOGD("__update_preload_condition_in_db failed. Rollback now\n");
2163 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2166 /*Commit transaction*/
2167 ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
2168 if (ret != SQLITE_OK) {
2169 _LOGD("Failed to commit transaction, Rollback now\n");
2170 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2174 _LOGD("Transaction Commit and End\n");
2176 pkgmgr_parser_close_db();
2180 API int pkgmgr_parser_update_global_app_disable_info_in_db(const char *appid, uid_t uid, int is_disable)
2184 ret = pkgmgr_parser_check_and_create_db(GLOBAL_USER);
2186 _LOGD("Failed to open DB\n");
2190 /*Begin transaction*/
2191 ret = sqlite3_exec(pkgmgr_parser_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
2192 if (ret != SQLITE_OK) {
2193 _LOGD("Failed to begin transaction\n");
2197 _LOGD("Transaction Begin\n");
2199 ret = __disable_global_app_for_user(appid, uid);
2201 ret = __enable_global_app_for_user(appid, uid);
2203 _LOGD("__update_preload_condition_in_db failed. Rollback now\n");
2204 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2207 /*Commit transaction*/
2208 ret = sqlite3_exec(pkgmgr_parser_db, "COMMIT", NULL, NULL, NULL);
2209 if (ret != SQLITE_OK) {
2210 _LOGD("Failed to commit transaction, Rollback now\n");
2211 sqlite3_exec(pkgmgr_parser_db, "ROLLBACK", NULL, NULL, NULL);
2215 _LOGD("Transaction Commit and End\n");
2217 pkgmgr_parser_close_db();