From: Hwankyu Jhun Date: Fri, 9 Aug 2019 05:31:07 +0000 (+0900) Subject: Refactor Service DB X-Git-Tag: accepted/tizen/unified/20190904.224456~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=3dd8be7be2b256ddb4083ed5b254979a78995f54;p=platform%2Fcore%2Fappfw%2Faul-1.git Refactor Service DB - Defines and uses macros - Removes unnecessary exceptions - Adds database busy handler - Change insert query of appsvc Change-Id: I9d374724b9ded987b3b9074ed6dfb898bbc34230 Signed-off-by: Hwankyu Jhun --- diff --git a/src/aul_db.c b/src/aul_db.c new file mode 100644 index 0000000..2b45fc4 --- /dev/null +++ b/src/aul_db.c @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2015 - 2019 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define _GNU_SOURCE +#include +#include +#include + +#include "aul_db.h" + +#define BUSY_WAITING_USEC 50000 +#define BUSY_WAITING_MAX 100 + +char *aul_db_get_path(const char *file, uid_t uid) +{ + char buf[PATH_MAX]; + + if (uid >= REGULAR_UID_MIN) { + snprintf(buf, sizeof(buf), "%s/user/%u/%s", + tzplatform_getenv(TZ_SYS_DB), uid, file); + } else { + snprintf(buf, sizeof(buf), "%s/%s", + tzplatform_getenv(TZ_SYS_DB), file); + } + + return strdup(buf); +} + +static int __db_busy_handler(void *data, int count) +{ + const char *path = (const char *)data; + + if (count < BUSY_WAITING_MAX) { + usleep(BUSY_WAITING_USEC); + return 1; + } + + _E("Database(%s) is busy", path); + return 0; +} + +sqlite3 *aul_db_open(const char *path, bool readonly) +{ + sqlite3 *db; + int flags; + int ret; + + flags = readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE; + ret = sqlite3_open_v2(path, &db, flags, NULL); + if (ret != SQLITE_OK) { + _E("sqlite3_open_v2() is failed. error(%d)", ret); + sqlite3_close_v2(db); + return NULL; + } + + ret = sqlite3_busy_handler(db, __db_busy_handler, (void *)path); + if (ret != SQLITE_OK) { + _E("sqlite3_busy_handler() is failed. error(%s)", + sqlite3_errmsg(db)); + sqlite3_close_v2(db); + return NULL; + } + + return db; +} + +void aul_db_close(sqlite3 *db) +{ + sqlite3_close_v2(db); +} + +int aul_db_save_column_str(sqlite3_stmt *stmt, int idx, char **str) +{ + const char *val; + + val = (const char *)sqlite3_column_text(stmt, idx); + if (val) { + *str = strdup(val); + if (*str == NULL) { + _E("Out of memory"); + return -ENOMEM; + } + } + + return 0; +} diff --git a/src/aul_db.h b/src/aul_db.h new file mode 100644 index 0000000..191804c --- /dev/null +++ b/src/aul_db.h @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define _GNU_SOURCE +#include +#include +#include + +#include "aul_util.h" + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#define __PREPARE_V2(db, query, length, stmt) do { \ + if (sqlite3_prepare_v2(db, query, length, &stmt, NULL) != SQLITE_OK) { \ + _E("sqlite3_prepare_v2() is failed. error(%s)", \ + sqlite3_errmsg(db)); \ + return -1; \ + } \ +} while (0) + +#define __BIND_TEXT(db, stmt, i, text) do { \ + if (sqlite3_bind_text(stmt, i, text, -1, SQLITE_STATIC) != SQLITE_OK) {\ + _E("sqlite3_bind_text() is failed. index(%d), error(%s)", \ + i, sqlite3_errmsg(db)); \ + sqlite3_finalize(stmt); \ + return -1; \ + } \ +} while (0) + +#define __BIND_INT(db, stmt, i, val) do { \ + if (sqlite3_bind_int(stmt, i, val) != SQLITE_OK) { \ + _E("sqlite3_bind_int() is failed. index(%d), error(%s)", \ + i, sqlite3_errmsg(db)); \ + sqlite3_finalize(stmt); \ + return -1; \ + } \ +} while (0) + +#define __STEP(db, stmt) do { \ + if (sqlite3_step(stmt) != SQLITE_DONE) { \ + _E("sqlite3_step() is failed. error(%s)", \ + sqlite3_errmsg(db)); \ + sqlite3_finalize(stmt); \ + return -1; \ + } \ +} while (0) + +char *aul_db_get_path(const char *file, uid_t uid); + +sqlite3 *aul_db_open(const char *path, bool readonly); + +void aul_db_close(sqlite3* db); + +int aul_db_save_column_str(sqlite3_stmt *stmt, int idx, char **str); + +#ifdef __cplusplus +} +#endif diff --git a/src/service_db.c b/src/service_db.c old mode 100755 new mode 100644 index 18a1aa2..45682dc --- a/src/service_db.c +++ b/src/service_db.c @@ -1,14 +1,14 @@ /* - * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2015 - 2019 Samsung Electronics Co., Ltd All Rights Reserved * - * Licensed under the Apache License, Version 2.0 (the License); + * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an AS IS BASIS, + * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. @@ -22,46 +22,19 @@ #include #include #include +#include #include #include #include #include "aul_svc_db.h" +#include "aul_db.h" #include "aul_util.h" -#define APP_INFO_DB_PATH tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_parser.db") - +#define APP_SVC_DB ".appsvc.db" +#define PKGMGR_PARSER_DB ".pkgmgr_parser.db" +#define APP_SVC_COLLATION "appsvc_collation" #define QUERY_MAX_LEN 8192 -#define URI_MAX_LEN 4096 -#define BUF_MAX_LEN 1024 -#define ROOT_UID 0 - -#define SVC_COLLATION "appsvc_collation" - -#define QUERY_CREATE_TABLE_APPSVC "create table if not exists appsvc " \ - "(operation text, " \ - "mime_type text, " \ - "uri text, " \ - "pkg_name text, " \ - "PRIMARY KEY(pkg_name)) " - -#define __BIND_TEXT(db, stmt, i, text) \ -do { \ - if (sqlite3_bind_text(stmt, i, text, -1, SQLITE_STATIC) != SQLITE_OK) { \ - _E("bind error(index %d): %s", i, sqlite3_errmsg(db)); \ - sqlite3_finalize(stmt); \ - return -1; \ - } \ -} while (0) - -#define __BIND_INT(db, stmt, i, int) \ -do { \ - if (sqlite3_bind_int(stmt, i, int) != SQLITE_OK) { \ - _E("bind error(index %d): %s", i, sqlite3_errmsg(db)); \ - sqlite3_finalize(stmt); \ - return -1; \ - } \ -} while (0) struct alias_info_s { char *alias_appid; @@ -74,61 +47,25 @@ struct allowed_info_s { char *allowed_appid; }; -static char *__get_svc_db(uid_t uid) +static sqlite3 *__open_app_svc_db(uid_t uid, bool readonly) { - char db_path[PATH_MAX]; - - if (uid >= REGULAR_UID_MIN) { - snprintf(db_path, sizeof(db_path), "%s/user/%d/.appsvc.db", - tzplatform_getenv(TZ_SYS_DB), uid); - } else { - snprintf(db_path, sizeof(db_path), "%s/.appsvc.db", - tzplatform_getenv(TZ_SYS_DB)); - } + sqlite3 *db; + char *path; - return strdup(db_path); -} - -/** - * db initialize - */ -static int __init(uid_t uid, bool readonly, sqlite3 **svc_db) -{ - int rc; - char *db_path; - - if (*svc_db) { - _D("Already initialized"); - return 0; - } - - db_path = __get_svc_db(uid); - if (db_path == NULL) { - _E("Failed to get service db path - %d", uid); - return -1; + path = aul_db_get_path(APP_SVC_DB, uid); + if (!path) { + _E("Failed to get db path"); + return NULL; } - rc = sqlite3_open_v2(db_path, svc_db, - readonly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE, - NULL); - if (rc) { - _E("Can't open database(%s): %d, %s, extended: %d", - db_path, rc, sqlite3_errmsg(*svc_db), - sqlite3_extended_errcode(*svc_db)); - free(db_path); - if (*svc_db) { - sqlite3_close(*svc_db); - *svc_db = NULL; - } - return -1; - } - free(db_path); + db = aul_db_open(path, readonly); + free(path); - return 0; + return db; } static int __collate_appsvc(void *ucol, int str1_len, const void *str1, - int str2_len, const void *str2) + int str2_len, const void *str2) { char *saveptr1 = NULL; char *saveptr2 = NULL; @@ -167,7 +104,6 @@ static int __collate_appsvc(void *ucol, int str1_len, const void *str1, } token = strtok_r(dup_str1, ";", &saveptr1); - if (token == NULL) { free(dup_str1); free(dup_str2); @@ -185,9 +121,11 @@ static int __collate_appsvc(void *ucol, int str1_len, const void *str1, } if ((strcmp(op, in_op) == 0) && (strcmp(mime, in_mime) == 0)) { - SECURE_LOGD("%s %s %s %s %s %s", op, in_op, mime, in_mime, uri, in_uri); + SECURE_LOGD("%s %s %s %s %s %s", + op, in_op, mime, in_mime, uri, in_uri); if (g_pattern_match_simple(uri, in_uri)) { - SECURE_LOGD("in_uri : %s | uri : %s", in_uri, uri); + SECURE_LOGD("in_uri : %s | uri : %s", + in_uri, uri); free(dup_str1); free(dup_str2); return 0; @@ -201,124 +139,52 @@ static int __collate_appsvc(void *ucol, int str1_len, const void *str1, return -1; } -static int __init_app_info_db(uid_t uid, sqlite3 **app_info_db, sqlite3 **global_app_info_db) +static sqlite3 *__open_pkgmgr_parser_db(uid_t uid) { - int rc; - char *db_path; - - if (*app_info_db && *global_app_info_db) { - _D("Already initialized"); - return 0; - } - - db_path = getUserPkgParserDBPathUID(uid); - if (db_path == NULL) { - _E("Failed to get pkg parser db path - %d", uid); - return -1; - } - - rc = sqlite3_open_v2(db_path, app_info_db, SQLITE_OPEN_READONLY, NULL); - free(db_path); - if (rc) { - _E("Can't open database: %d, %s, extended: %d", - rc, sqlite3_errmsg(*app_info_db), - sqlite3_extended_errcode(*app_info_db)); - goto err; - } + sqlite3 *db; + char *path; + int ret; - rc = sqlite3_exec(*app_info_db, "PRAGMA journal_mode = PERSIST", - NULL, NULL, NULL); - if (SQLITE_OK != rc) { - _D("Fail to change journal mode"); - goto err; + path = aul_db_get_path(PKGMGR_PARSER_DB, uid); + if (!path) { + _E("Failed to get db path"); + return NULL; } - sqlite3_create_collation(*app_info_db, SVC_COLLATION, SQLITE_UTF8, - NULL, __collate_appsvc); - - rc = sqlite3_open_v2(APP_INFO_DB_PATH, global_app_info_db, - SQLITE_OPEN_READONLY, NULL); - if (rc) { - _E("Can't open database: %d, %s, extended: %d", - rc, sqlite3_errmsg(*global_app_info_db), - sqlite3_extended_errcode(*global_app_info_db)); - goto err; - } + db = aul_db_open(path, true); + free(path); - rc = sqlite3_exec(*global_app_info_db, "PRAGMA journal_mode = PERSIST", + ret = sqlite3_exec(db, "PRAGMA journal_mode = PERSIST", NULL, NULL, NULL); - if (SQLITE_OK != rc) { - _D("Fail to change journal mode"); - goto err; + if (ret != SQLITE_OK) { + _E("sqlite3_exec() is failed. error(%s)", sqlite3_errmsg(db)); + aul_db_close(db); + return NULL; } - sqlite3_create_collation(*global_app_info_db, SVC_COLLATION, SQLITE_UTF8, + sqlite3_create_collation(db, APP_SVC_COLLATION, SQLITE_UTF8, NULL, __collate_appsvc); - return 0; -err: - if (*app_info_db) { - sqlite3_close(*app_info_db); - *app_info_db = NULL; - } - - if (*global_app_info_db) { - sqlite3_close(*global_app_info_db); - *global_app_info_db = NULL; - } - - return -1; -} - -static void __fini_app_info_db(sqlite3 **app_info_db, sqlite3 **global_app_info_db) -{ - if (*app_info_db) { - sqlite3_close(*app_info_db); - *app_info_db = NULL; - } - - if (*global_app_info_db) { - sqlite3_close(*global_app_info_db); - *global_app_info_db = NULL; - } -} - -static int __fini(sqlite3 **svc_db) -{ - if (*svc_db) { - sqlite3_close(*svc_db); - *svc_db = NULL; - } - return 0; + return db; } static int __insert_info(sqlite3 *db, const char *op, const char *mime_type, const char *uri, const char *appid) { - const char query[] = - "INSERT INTO appsvc(operation, mime_type, uri, pkg_name) " + const char query[] = "INSERT OR REPLACE INTO " + "appsvc(operation, mime_type, uri, pkg_name) " "VALUES(?, ?, ?, ?)"; sqlite3_stmt *stmt; int idx = 1; - int r; - r = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); - if (r != SQLITE_OK) { - _E("Prepare failed: %s", sqlite3_errmsg(db)); - return -1; - } + __PREPARE_V2(db, query, strlen(query), stmt); __BIND_TEXT(db, stmt, idx++, op); __BIND_TEXT(db, stmt, idx++, mime_type ? mime_type : "NULL"); __BIND_TEXT(db, stmt, idx++, uri ? uri : "NULL"); __BIND_TEXT(db, stmt, idx++, appid); - r = sqlite3_step(stmt); - if (r != SQLITE_DONE) { - _E("Step failed: %s", sqlite3_errmsg(db)); - sqlite3_finalize(stmt); - return -1; - } + __STEP(db, stmt); sqlite3_finalize(stmt); return 0; @@ -327,19 +193,22 @@ static int __insert_info(sqlite3 *db, const char *op, const char *mime_type, int _svc_db_add_app(const char *op, const char *mime_type, const char *uri, const char *pkg_name, uid_t uid) { - int r; - sqlite3 *svc_db = NULL; + sqlite3 *db; + int ret; - if (__init(uid, false, &svc_db) < 0) + if (!op) { + _E("Invalid parameter"); return -1; + } - if (op == NULL) + db = __open_app_svc_db(uid, false); + if (!db) return -1; - r = __insert_info(svc_db, op, mime_type, uri, pkg_name); - __fini(&svc_db); + ret = __insert_info(db, op, mime_type, uri, pkg_name); + aul_db_close(db); - return r; + return ret; } static int __delete_info(sqlite3 *db, const char *appid) @@ -347,22 +216,12 @@ static int __delete_info(sqlite3 *db, const char *appid) const char query[] = "DELETE FROM appsvc WHERE pkg_name = ?;"; sqlite3_stmt *stmt; int idx = 1; - int r; - r = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); - if (r != SQLITE_OK) { - _E("Prepare failed: %s", sqlite3_errmsg(db)); - return -1; - } + __PREPARE_V2(db, query, strlen(query), stmt); __BIND_TEXT(db, stmt, idx++, appid); - r = sqlite3_step(stmt); - if (r != SQLITE_DONE) { - _E("Step failed: %s", sqlite3_errmsg(db)); - sqlite3_finalize(stmt); - return -1; - } + __STEP(db, stmt); sqlite3_finalize(stmt); return 0; @@ -370,39 +229,41 @@ static int __delete_info(sqlite3 *db, const char *appid) int _svc_db_delete_with_pkgname(const char *pkg_name, uid_t uid) { - int r; - sqlite3 *svc_db = NULL; + sqlite3 *db; + int ret; - if (pkg_name == NULL) { - _E("Invalid argument: data to delete is NULL"); + if (!pkg_name) { + _E("Invalid parameter"); return -1; } - if (__init(uid, false, &svc_db) < 0) + db = __open_app_svc_db(uid, false); + if (!db) return -1; - r = __delete_info(svc_db, pkg_name); - __fini(&svc_db); + ret = __delete_info(db, pkg_name); + aul_db_close(db); - return r; + return ret; } int _svc_db_delete_all(uid_t uid) { const char query[] = "DELETE FROM appsvc;"; - int r; - sqlite3 *svc_db = NULL; + sqlite3 *db; + int ret; - if (__init(uid, false, &svc_db) < 0) + db = __open_app_svc_db(uid, false); + if (!db) return -1; - r = sqlite3_exec(svc_db, query, NULL, NULL, NULL); - if (r != SQLITE_OK) { - _E("Exec failed: %s", sqlite3_errmsg(svc_db)); - __fini(&svc_db); + ret = sqlite3_exec(db, query, NULL, NULL, NULL); + if (ret != SQLITE_OK) { + _E("sqlite3_exec() is failed. error(%s)", sqlite3_errmsg(db)); + aul_db_close(db); return -1; } - __fini(&svc_db); + aul_db_close(db); return 0; } @@ -415,11 +276,7 @@ static int __get_count(sqlite3 *db, const char *appid) int count; int r; - r = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); - if (r != SQLITE_OK) { - _E("Prepare failed: %s", sqlite3_errmsg(db)); - return -1; - } + __PREPARE_V2(db, query, strlen(query), stmt); __BIND_TEXT(db, stmt, idx++, appid); @@ -436,21 +293,22 @@ static int __get_count(sqlite3 *db, const char *appid) int _svc_db_is_defapp(const char *pkg_name, uid_t uid) { - int r; - sqlite3 *svc_db = NULL; + sqlite3 *db; + int count; - if (pkg_name == NULL) { - _E("Invalid argument: data to delete is NULL"); + if (!pkg_name) { + _E("Invalid parameter"); return 0; } - if (__init(uid, true, &svc_db) < 0) + db = __open_app_svc_db(uid, true); + if (!db) return 0; - r = __get_count(svc_db, pkg_name); - __fini(&svc_db); + count = __get_count(db, pkg_name); + aul_db_close(db); - if (r < 1) + if (count < 1) return 0; return 1; @@ -463,38 +321,26 @@ static int __get_appid(sqlite3 *db, const char *op, const char *mime_type, "SELECT pkg_name FROM appsvc WHERE operation = ? " "AND mime_type = ? AND uri = ?;"; sqlite3_stmt *stmt; - char *temp; int idx = 1; - int r; + int ret; - r = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); - if (r != SQLITE_OK) { - _E("Prepare failed: %s", sqlite3_errmsg(db)); - return -1; - } + __PREPARE_V2(db, query, strlen(query), stmt); __BIND_TEXT(db, stmt, idx++, op); __BIND_TEXT(db, stmt, idx++, mime_type ? mime_type : "NULL"); __BIND_TEXT(db, stmt, idx++, uri ? uri : "NULL"); - r = sqlite3_step(stmt); - if (r != SQLITE_ROW) { - _E("Step failed: %s", sqlite3_errmsg(db)); + ret = sqlite3_step(stmt); + if (ret != SQLITE_ROW) { + _E("sqlite3_step() is failed. error(%s)", sqlite3_errmsg(db)); sqlite3_finalize(stmt); return -1; } - temp = (char *)sqlite3_column_text(stmt, 0); - if (temp) { - *appid = strdup(temp); - if (*appid == NULL) { - _E("Out of memory"); - sqlite3_finalize(stmt); - return -1; - } - } - + ret = aul_db_save_column_str(stmt, 0, appid); sqlite3_finalize(stmt); + if (ret != 0) + return ret; return 0; } @@ -502,34 +348,30 @@ static int __get_appid(sqlite3 *db, const char *op, const char *mime_type, char *_svc_db_get_app(const char *op, const char *mime_type, const char *uri, uid_t uid) { - char *appid = NULL; - int r; - sqlite3 *svc_db = NULL; + sqlite3 *db; + char *appid; + int ret; - if (op == NULL) + if (!op) { + _E("Invalid parameter"); return NULL; + } - if (__init(uid, true, &svc_db) < 0) + db = __open_app_svc_db(uid, true); + if (!db) return NULL; - r = __get_appid(svc_db, op, mime_type, uri, &appid); - if (r != 0) { - __fini(&svc_db); + ret = __get_appid(db, op, mime_type, uri, &appid); + aul_db_close(db); + if (ret != 0) return NULL; - } - __fini(&svc_db); - SECURE_LOGD("appid: %s", appid); + SECURE_LOGD("appid: %s", appid); return appid; } -static int __compare_appid(gconstpointer a, gconstpointer b) -{ - return strcmp(a, b); -} - -static int __adjust_list_with_submode(sqlite3 *db, sqlite3 *app_info_db, - int mainapp_mode, const char *win_id, GSList **list) +static int __adjust_list_with_submode(sqlite3 *db, int mainapp_mode, + const char *win_id, GSList **list) { const char query[] = "SELECT ac.app_id, ai.app_submode_mainid " @@ -541,13 +383,8 @@ static int __adjust_list_with_submode(sqlite3 *db, sqlite3 *app_info_db, char *excluded_appid; GSList *found_subapp; GSList *found_mainapp; - int r; - r = sqlite3_prepare_v2(app_info_db, query, strlen(query), &stmt, NULL); - if (r != SQLITE_OK) { - _E("Prepare failed: %s", sqlite3_errmsg(app_info_db)); - return -1; - } + __PREPARE_V2(db, query, strlen(query), stmt); while (sqlite3_step(stmt) == SQLITE_ROW) { sub_appid = (char *)sqlite3_column_text(stmt, 0); @@ -555,7 +392,7 @@ static int __adjust_list_with_submode(sqlite3 *db, sqlite3 *app_info_db, continue; found_subapp = g_slist_find_custom(*list, sub_appid, - __compare_appid); + (GCompareFunc)strcmp); if (found_subapp == NULL) continue; @@ -564,7 +401,7 @@ static int __adjust_list_with_submode(sqlite3 *db, sqlite3 *app_info_db, continue; found_mainapp = g_slist_find_custom(*list, submode_mainid, - __compare_appid); + (GCompareFunc)strcmp); if (found_mainapp == NULL) continue; @@ -588,27 +425,35 @@ static int __adjust_list_with_submode(sqlite3 *db, sqlite3 *app_info_db, int _svc_db_adjust_list_with_submode(int mainapp_mode, char *win_id, GSList **pkg_list, uid_t uid) { - int r; - sqlite3 *app_info_db = NULL; - sqlite3 *global_app_info_db = NULL; + sqlite3 *global_user_db; + sqlite3 *user_db; + int ret; - if (__init_app_info_db(uid, &app_info_db, &global_app_info_db) < 0) + global_user_db = __open_pkgmgr_parser_db(GLOBAL_USER); + if (!global_user_db) return 0; - r = __adjust_list_with_submode(app_info_db, app_info_db, mainapp_mode, win_id, - pkg_list); - if (r < 0) { - __fini_app_info_db(&app_info_db, &global_app_info_db); - return -1; - } + user_db = __open_pkgmgr_parser_db(uid); + if (!user_db) + return 0; - r = __adjust_list_with_submode(global_app_info_db, app_info_db, mainapp_mode, win_id, + ret = __adjust_list_with_submode(user_db, mainapp_mode, win_id, pkg_list); - if (r < 0) { - __fini_app_info_db(&app_info_db, &global_app_info_db); - return -1; + aul_db_close(user_db); + if (ret < 0) + return ret; + + if (uid != GLOBAL_USER) { + global_user_db = __open_pkgmgr_parser_db(GLOBAL_USER); + if (!global_user_db) + return 0; + + ret = __adjust_list_with_submode(global_user_db, mainapp_mode, + win_id, pkg_list); + aul_db_close(global_user_db); + if (ret < 0) + return ret; } - __fini_app_info_db(&app_info_db, &global_app_info_db); return 0; } @@ -617,32 +462,24 @@ static int __get_list_with_query(sqlite3 *db, const char *query, GSList **list) { sqlite3_stmt *stmt; GSList *found; - char *str; char *appid; - int r; + int ret; - r = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); - if (r != SQLITE_OK) { - _E("Prepare failed: %s", sqlite3_errmsg(db)); - return -1; - } + __PREPARE_V2(db, query, strlen(query), stmt); while (sqlite3_step(stmt) == SQLITE_ROW) { - str = (char *)sqlite3_column_text(stmt, 0); - if (str == NULL) - continue; - - found = g_slist_find_custom(*list, str, __compare_appid); - if (found == NULL) { - appid = strdup(str); - if (appid == NULL) { - _E("Out of memory"); - break; - } + ret = aul_db_save_column_str(stmt, 0, &appid); + if (ret != 0) + break; - *list = g_slist_append(*list, appid); - _D("%s is added", appid); + found = g_slist_find_custom(*list, appid, (GCompareFunc)strcmp); + if (found) { + free(appid); + continue; } + + *list = g_slist_append(*list, appid); + _D("%s is added", appid); } sqlite3_finalize(stmt); @@ -652,16 +489,17 @@ static int __get_list_with_query(sqlite3 *db, const char *query, GSList **list) int _svc_db_get_list_with_all_defapps(GSList **pkg_list, uid_t uid) { const char query[] = "SELECT pkg_name FROM appsvc;"; - int r; - sqlite3 *svc_db = NULL; + sqlite3 *db; + int ret; - if (__init(uid, true, &svc_db) < 0) + db = __open_app_svc_db(uid, true); + if (!db) return -1; - r = __get_list_with_query(svc_db, query, pkg_list); - __fini(&svc_db); + ret = __get_list_with_query(db, query, pkg_list); + aul_db_close(db); - return r; + return ret; } char *_svc_db_query_builder_add(char *old_query, char *op, char *uri, @@ -681,11 +519,13 @@ char *_svc_db_query_builder_add(char *old_query, char *op, char *uri, } } else { if (old_query) { - query = sqlite3_mprintf("%s OR ac.app_control like '%%%q|%q|%q%%' ", + query = sqlite3_mprintf("%s OR ac.app_control like " + "'%%%q|%q|%q%%' ", old_query, op, uri, mime); free(old_query); } else { - query = sqlite3_mprintf("ac.app_control like '%%%q|%q|%q%%' ", + query = sqlite3_mprintf("ac.app_control like " + "'%%%q|%q|%q%%' ", op, uri, mime); } } @@ -737,32 +577,54 @@ char *_svc_db_query_builder_build(char *old_query) int _svc_db_exec_query(const char *query, GSList **pkg_list, uid_t uid) { - int r; - sqlite3 *app_info_db = NULL; - sqlite3 *global_app_info_db = NULL; + sqlite3 *global_user_db; + sqlite3 *user_db; + int ret; - if (query == NULL) { - _E("query is NULL"); + if (!query) { + _E("Invalid parameter"); return -1; } - if (__init_app_info_db(uid, &app_info_db, &global_app_info_db) < 0) + user_db = __open_pkgmgr_parser_db(uid); + if (!user_db) return 0; - SECURE_LOGD("query : %s", query); + ret = __get_list_with_query(user_db, query, pkg_list); + aul_db_close(user_db); + if (ret < 0) + return ret; - r = __get_list_with_query(app_info_db, query, pkg_list); - if (r < 0) { - __fini_app_info_db(&app_info_db, &global_app_info_db); - return -1; - } + if (uid != GLOBAL_USER) { + global_user_db = __open_pkgmgr_parser_db(GLOBAL_USER); + if (!global_user_db) + return 0; - r = __get_list_with_query(global_app_info_db, query, pkg_list); - if (r < 0) { - __fini_app_info_db(&app_info_db, &global_app_info_db); - return -1; + ret = __get_list_with_query(global_user_db, query, pkg_list); + aul_db_close(global_user_db); + if (ret < 0) + return ret; } - __fini_app_info_db(&app_info_db, &global_app_info_db); + + return 0; +} + +static int __add_alias_appid(sqlite3 *db, const char *alias_appid, + const char *appid) +{ + const char query[] = + "INSERT OR REPLACE INTO alias_info(alias_appid, appid) " + "values(?,?);"; + sqlite3_stmt *stmt; + int idx = 1; + + __PREPARE_V2(db, query, strlen(query), stmt); + + __BIND_TEXT(db, stmt, idx++, alias_appid); + __BIND_TEXT(db, stmt, idx++, appid); + + __STEP(db, stmt); + sqlite3_finalize(stmt); return 0; } @@ -770,189 +632,116 @@ int _svc_db_exec_query(const char *query, GSList **pkg_list, uid_t uid) int _svc_db_add_alias_appid(const char *alias_appid, const char *appid, uid_t uid) { + sqlite3 *db; int ret; - sqlite3_stmt *stmt = NULL; - sqlite3 *svc_db = NULL; - const char *query = - "INSERT OR REPLACE INTO alias_info(alias_appid, appid) " \ - "values(?,?);"; - int result = 0; - if (alias_appid == NULL || appid == NULL) { + if (!alias_appid || !appid) { _E("Invalid parameters"); return -1; } - if (__init(uid, false, &svc_db) < 0) + db = __open_app_svc_db(uid, false); + if (!db) return -1; - ret = sqlite3_prepare_v2(svc_db, query, strlen(query), &stmt, NULL); - if (ret != SQLITE_OK) { - _E("sqlite3_prepare_v2() error: %d(%s)", - ret, sqlite3_errmsg(svc_db)); - __fini(&svc_db); - return ret; - } + ret = __add_alias_appid(db, alias_appid, appid); + aul_db_close(db); - ret = sqlite3_bind_text(stmt, 1, alias_appid, -1, SQLITE_TRANSIENT); - if (ret != SQLITE_OK) { - _E("sqlite3_bind_text() error: %d(%s)", - ret, sqlite3_errmsg(svc_db)); - result = -1; - goto end; - } + return ret; +} - ret = sqlite3_bind_text(stmt, 2, appid, -1, SQLITE_TRANSIENT); - if (ret != SQLITE_OK) { - _E("sqlite3_bind_text() error: %d(%s)", - ret, sqlite3_errmsg(svc_db)); - result = -1; - goto end; - } +static int __delete_alias_appid(sqlite3 *db, const char *alias_appid) +{ + const char query[] = "DELETE FROM alias_info WHERE alias_appid = ?;"; + sqlite3_stmt *stmt; - ret = sqlite3_step(stmt); - if (ret != SQLITE_DONE) { - _E("sqlite3_step() error: %d(%s)", - ret, sqlite3_errmsg(svc_db)); - result = -1; - goto end; - } + __PREPARE_V2(db, query, strlen(query), stmt); + + __BIND_TEXT(db, stmt, 1, alias_appid); -end: + __STEP(db, stmt); sqlite3_finalize(stmt); - __fini(&svc_db); - return result; + return 0; } int _svc_db_delete_alias_appid(const char *alias_appid, uid_t uid) { + sqlite3 *db; int ret; - sqlite3_stmt *stmt = NULL; - const char *query = "DELETE FROM alias_info WHERE alias_appid = ?;"; - int result = 0; - sqlite3 *svc_db = NULL; - if (alias_appid == NULL) { + if (!alias_appid) { _E("Invalid parameter"); return -1; } - if (__init(uid, false, &svc_db) < 0) + db = __open_app_svc_db(uid, false); + if (!db) return -1; - ret = sqlite3_prepare_v2(svc_db, query, strlen(query), &stmt, NULL); - if (ret != SQLITE_OK) { - _E("sqlite3_prepare_v2() error: %d(%s)", - ret, sqlite3_errmsg(svc_db)); - __fini(&svc_db); - return -1; - } - - ret = sqlite3_bind_text(stmt, 1, alias_appid, -1, SQLITE_TRANSIENT); - if (ret != SQLITE_OK) { - _E("sqlite3_bind_text() error: %d(%s)", - ret, sqlite3_errmsg(svc_db)); - result = -1; - goto end; - } - - ret = sqlite3_step(stmt); - if (ret != SQLITE_DONE) { - _E("sqlite3_step() error: %d(%s)", - ret, sqlite3_errmsg(svc_db)); - result = -1; - } + ret = __delete_alias_appid(db, alias_appid); + aul_db_close(db); -end: - sqlite3_finalize(stmt); - __fini(&svc_db); - - return result; + return ret; } -int __get_appid_from_alias_info(const char *alias_appid, char **appid, - uid_t uid, uid_t db_uid) +static int __get_appid_from_alias_info(sqlite3 *db, const char *alias_appid, + uid_t uid, uid_t db_uid, char **appid) { - int ret; - int result = 0; - sqlite3_stmt *stmt = NULL; + const char global_user_query[] = + "SELECT appid FROM alias_info WHERE " + "alias_info.alias_appid = ? AND alias_info.appid NOT IN " + "(SELECT appid FROM alias_info_for_uid WHERE uid = ?);"; + const char user_query[] = + "SELECT appid FROM alias_info WHERE " + "alias_appid = ? AND enable = 'true';"; + sqlite3_stmt *stmt; const char *query; - const char *real_appid; - sqlite3 *svc_db = NULL; + int idx = 1; + int ret; - if (appid == NULL || alias_appid == NULL) { - _E("Invalid parameter"); - return -1; - } + query = (db_uid == GLOBAL_USER) ? global_user_query : user_query; + __PREPARE_V2(db, query, strlen(query), stmt); - if (__init(db_uid, true, &svc_db) < 0) - return -1; + __BIND_TEXT(db, stmt, idx++, alias_appid); + if (db_uid == GLOBAL_USER) + __BIND_INT(db, stmt, idx++, uid); - if (db_uid == GLOBAL_USER) { - query = "SELECT appid FROM alias_info WHERE " \ - "alias_info.alias_appid = ? AND " \ - "alias_info.appid NOT IN " \ - "(SELECT appid FROM alias_info_for_uid WHERE " \ - "uid = ?);"; - } else { - query = "SELECT appid FROM alias_info WHERE " \ - "alias_appid = ? AND enable = 'true';"; - } + ret = sqlite3_step(stmt); + if (ret != SQLITE_ROW) { + if (ret != SQLITE_DONE) { + _W("sqlite3_step() is failed. error(%s)", + sqlite3_errmsg(db)); + } - ret = sqlite3_prepare_v2(svc_db, query, strlen(query), &stmt, NULL); - if (ret != SQLITE_OK) { - _E("sqlite3_prepare_v2() error: %d(%s)", - ret, sqlite3_errmsg(svc_db)); - __fini(&svc_db); + sqlite3_finalize(stmt); return -1; } - ret = sqlite3_bind_text(stmt, 1, alias_appid, -1, SQLITE_TRANSIENT); - if (ret != SQLITE_OK) { - _E("sqlite3_bind_text() error: %d(%s)", - ret, sqlite3_errmsg(svc_db)); - result = -1; - goto end; - } + ret = aul_db_save_column_str(stmt, 0, appid); + sqlite3_finalize(stmt); + if (ret != 0) + return ret; - if (db_uid == GLOBAL_USER) { - ret = sqlite3_bind_int(stmt, 2, uid); - if (ret != SQLITE_OK) { - _E("sqlite3_bind_int() error - %s(%d)", - sqlite3_errmsg(svc_db), ret); - result = -1; - goto end; - } - } + SECURE_LOGD("alias_appid(%s), appid(%s)", alias_appid, *appid); - ret = sqlite3_step(stmt); - if (ret != SQLITE_ROW) { - if (ret != SQLITE_DONE) { - _W("sqlite3 step() error: %d(%s)", - ret, sqlite3_errmsg(svc_db)); - } - result = -1; - goto end; - } + return 0; +} - real_appid = (const char *)sqlite3_column_text(stmt, 0); - if (real_appid) { - *appid = strdup(real_appid); - if (*appid == NULL) { - _E("out of memory"); - result = -1; - goto end; - } - } +static int __alias_info_get_appid(const char *alias_appid, uid_t uid, + uid_t db_uid, char **appid) +{ + sqlite3 *db; + int ret; - SECURE_LOGD("alias_appid: %s, appid: %s", alias_appid, real_appid); + db = __open_app_svc_db(db_uid, false); + if (!db) + return -1; -end: - sqlite3_finalize(stmt); - __fini(&svc_db); + ret = __get_appid_from_alias_info(db, alias_appid, uid, db_uid, appid); + aul_db_close(db); - return result; + return ret; } int _svc_db_get_appid_from_alias_info(const char *alias_appid, char **appid, @@ -960,125 +749,98 @@ int _svc_db_get_appid_from_alias_info(const char *alias_appid, char **appid, { int ret; - ret = __get_appid_from_alias_info(alias_appid, appid, uid, uid); + ret = __alias_info_get_appid(alias_appid, uid, uid, appid); if (ret < 0) { - ret = __get_appid_from_alias_info(alias_appid, appid, uid, - GLOBAL_USER); + ret = __alias_info_get_appid(alias_appid, uid, GLOBAL_USER, + appid); } return ret; } -static int __get_alias_info_list(uid_t uid, uid_t db_uid, GHashTable *list) +static void __destroy_alias_info(gpointer data) { - int ret; - sqlite3_stmt *stmt = NULL; - const char *query; - const char *alias_id; - const char *id; - struct alias_info_s *info; - sqlite3 *svc_db = NULL; + struct alias_info_s *info = (struct alias_info_s *)data; - if (__init(db_uid, true, &svc_db) < 0) - return -1; + free(info->appid); + free(info->alias_appid); + free(info); +} - if (db_uid == GLOBAL_USER) { - query = "SELECT alias_appid, appid FROM alias_info WHERE " \ - "alias_info.appid NOT IN " \ - "(SELECT appid FROM alias_info_for_uid WHERE " \ - "uid = ?);"; - } else { - query = "SELECT alias_appid, appid FROM alias_info WHERE " \ - "enable = 'true';"; - } +static int __get_alias_info_list(sqlite3 *db, uid_t uid, uid_t db_uid, + GHashTable *list) +{ + const char global_user_query[] = + "SELECT alias_appid, appid FROM alias_info WHERE " + "alias_info.appid NOT IN " + "(SELECT appid FROM alias_info_for_uid WHERE uid = ?);"; + const char user_query[] = + "SELECT alias_appid, appid FROM alias_info WHERE " + "enable = 'true';"; + struct alias_info_s *info; + sqlite3_stmt *stmt; + const char *query; + int idx = 1; + int ret; - ret = sqlite3_prepare_v2(svc_db, query, strlen(query), &stmt, NULL); - if (ret != SQLITE_OK) { - _E("sqlite3_prepare_v2() error - %s(%d)", - sqlite3_errmsg(svc_db), ret); - __fini(&svc_db); - return -1; - } + query = (db_uid == GLOBAL_USER) ? global_user_query : user_query; + __PREPARE_V2(db, query, strlen(query), stmt); - if (db_uid == GLOBAL_USER) { - ret = sqlite3_bind_int(stmt, 1, uid); - if (ret != SQLITE_OK) { - _E("sqlite3_bind_int() error - %s(%d)", - sqlite3_errmsg(svc_db), ret); - sqlite3_finalize(stmt); - __fini(&svc_db); - return -1; - } - } + if (db_uid == GLOBAL_USER) + __BIND_INT(db, stmt, idx++, uid); while (sqlite3_step(stmt) == SQLITE_ROW) { - alias_id = (const char *)sqlite3_column_text(stmt, 0); - if (alias_id == NULL) { - _E("Failed to get alias_appid"); - break; - } - - id = (const char *)sqlite3_column_text(stmt, 1); - if (id == NULL) { - _E("Failed to get appid"); - break; - } - - info = malloc(sizeof(struct alias_info_s)); - if (info == NULL) { - _E("out of memory"); + info = calloc(1, sizeof(struct alias_info_s)); + if (!info) { + _E("Out of memory"); break; } - info->alias_appid = strdup(alias_id); - if (info->alias_appid == NULL) { - _E("out of memory"); - free(info); + idx = 0; + ret = aul_db_save_column_str(stmt, idx++, &info->alias_appid); + if (ret != 0) { + __destroy_alias_info(info); break; } - info->appid = strdup(id); - if (info->appid == NULL) { - _E("out of memory"); - free(info->alias_appid); - free(info); + ret = aul_db_save_column_str(stmt, idx++, &info->appid); + if (ret != 0) { + __destroy_alias_info(info); break; } g_hash_table_insert(list, info->alias_appid, info); - } sqlite3_finalize(stmt); - __fini(&svc_db); return 0; } -static void __destroy_alias_info(gpointer data) +static int __alias_info_get_list(uid_t uid, uid_t db_uid, GHashTable *list) { - struct alias_info_s *info = (struct alias_info_s *)data; + sqlite3 *db; + int ret; - if (info == NULL) - return; + db = __open_app_svc_db(db_uid, true); + if (!db) + return -1; - if (info->appid) - free(info->appid); - if (info->alias_appid) - free(info->alias_appid); - free(info); + ret = __get_alias_info_list(db, uid, db_uid, list); + aul_db_close(db); + + return ret; } int _svc_db_foreach_alias_info(void (*callback)(const char *alias_appid, const char *appid, void *data), uid_t uid, void *user_data) { - int ret; - GHashTable *list; - GHashTableIter iter; - gpointer value = NULL; struct alias_info_s *info; - bool invoked = false; + GHashTableIter iter; + GHashTable *list; + gpointer value; + int ret; list = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, __destroy_alias_info); @@ -1087,9 +849,9 @@ int _svc_db_foreach_alias_info(void (*callback)(const char *alias_appid, return -1; } - ret = __get_alias_info_list(uid, uid, list); + ret = __alias_info_get_list(uid, uid, list); if (ret == 0 && uid != GLOBAL_USER) - ret = __get_alias_info_list(uid, GLOBAL_USER, list); + ret = __alias_info_get_list(uid, GLOBAL_USER, list); if (ret != 0) { _E("Failed to get alias info list"); @@ -1100,122 +862,73 @@ int _svc_db_foreach_alias_info(void (*callback)(const char *alias_appid, g_hash_table_iter_init(&iter, list); while (g_hash_table_iter_next(&iter, NULL, &value)) { info = (struct alias_info_s *)value; - if (info) - callback(info->alias_appid, info->appid, user_data); - invoked = true; - value = NULL; + callback(info->alias_appid, info->appid, user_data); } - g_hash_table_destroy(list); - if (!invoked) - _W("alias info is empty"); + g_hash_table_destroy(list); return 0; } -static int __enable_disable_alias_info(const char *appid, uid_t uid, - uid_t db_uid, bool enable) +static int __enable_or_disable_alias_info(sqlite3 *db, const char *appid, + uid_t uid, uid_t db_uid, bool enable) { - int ret; - int result = 0; - sqlite3_stmt *stmt = NULL; + const char global_user_query[] = + "INSERT OR REPLACE INTO alias_info_for_uid(appid, uid, " + "is_enabled) values((SELECT appid FROM alias_info " + "WHERE appid = ?), ?, ?);"; + const char user_query[] = + "UPDATE alias_info set enable = ? WHERE appid = ?;"; + sqlite3_stmt *stmt; const char *query; - const char *value; - sqlite3 *svc_db = NULL; + int idx = 1; - if (appid == NULL) { + if (!appid) { _E("Invalid parameter"); return -1; } - if (__init(db_uid, false, &svc_db) < 0) - return -1; - - if (enable) - value = "true"; - else - value = "false"; + query = (db_uid == GLOBAL_USER) ? global_user_query : user_query; + __PREPARE_V2(db, query, strlen(query), stmt); if (db_uid == GLOBAL_USER) { - query = "INSERT OR REPLACE INTO alias_info_for_uid(appid, uid, is_enabled) " \ - "values((SELECT appid FROM alias_info WHERE appid = ?), ?, ?);"; + __BIND_TEXT(db, stmt, idx++, appid); + __BIND_INT(db, stmt, idx++, uid); + __BIND_TEXT(db, stmt, idx++, enable ? "true" : "false"); } else { - query = "UPDATE alias_info set enable = ? WHERE appid = ?;"; - } - - ret = sqlite3_prepare_v2(svc_db, query, strlen(query), &stmt, NULL); - if (ret != SQLITE_OK) { - _E("sqlite3_prepare_v2() error - %s(%d)", - sqlite3_errmsg(svc_db), ret); - __fini(&svc_db); - return -1; + __BIND_TEXT(db, stmt, idx++, enable ? "true" : "false"); + __BIND_TEXT(db, stmt, idx++, appid); } - if (db_uid == GLOBAL_USER) { - ret = sqlite3_bind_text(stmt, 1, appid, -1, SQLITE_TRANSIENT); - if (ret != SQLITE_OK) { - _E("sqlite3_bind_text() error - %s(%d)", - sqlite3_errmsg(svc_db), ret); - result = -1; - goto end; - } - - ret = sqlite3_bind_int(stmt, 2, uid); - if (ret != SQLITE_OK) { - _E("sqlite3_bind_int() error - %s(%d)", - sqlite3_errmsg(svc_db), ret); - result = -1; - goto end; - } + __STEP(db, stmt); + sqlite3_finalize(stmt); - ret = sqlite3_bind_text(stmt, 3, value, -1, SQLITE_TRANSIENT); - if (ret != SQLITE_OK) { - _E("sqlite3_bind_text() error - %s(%d)", - sqlite3_errmsg(svc_db), ret); - result = -1; - goto end; - } - } else { - ret = sqlite3_bind_text(stmt, 1, value, -1, SQLITE_TRANSIENT); - if (ret != SQLITE_OK) { - _E("sqlite3_bind_text() error - %s(%d)", - sqlite3_errmsg(svc_db), ret); - result = -1; - goto end; - } + return 0; +} - ret = sqlite3_bind_text(stmt, 2, appid, -1, SQLITE_TRANSIENT); - if (ret != SQLITE_OK) { - _E("sqlite3_bind_text() error - %s(%d)", - sqlite3_errmsg(svc_db), ret); - result = -1; - goto end; - } - } +static int __alias_info_enable(const char *appid, uid_t uid, uid_t db_uid, + bool enable) +{ + sqlite3 *db; + int ret; - ret = sqlite3_step(stmt); - if (ret != SQLITE_DONE) { - _W("sqlite3_step() error - %s(%d)", - sqlite3_errmsg(svc_db), ret); - result = -1; - } + db = __open_app_svc_db(db_uid, true); + if (!db) + return -1; -end: - sqlite3_finalize(stmt); - __fini(&svc_db); + ret = __enable_or_disable_alias_info(db, appid, uid, db_uid, enable); + aul_db_close(db); - return result; + return ret; } int _svc_db_enable_alias_info(const char *appid, uid_t uid) { int ret; - ret = __enable_disable_alias_info(appid, uid, uid, true); - if (ret < 0) { - ret = __enable_disable_alias_info(appid, uid, - GLOBAL_USER, true); - } + ret = __alias_info_enable(appid, uid, uid, true); + if (ret < 0 && uid != GLOBAL_USER) + ret = __alias_info_enable(appid, uid, GLOBAL_USER, true); return ret; } @@ -1224,122 +937,89 @@ int _svc_db_disable_alias_info(const char *appid, uid_t uid) { int ret; - ret = __enable_disable_alias_info(appid, uid, uid, false); - if (ret < 0) { - ret = __enable_disable_alias_info(appid, uid, - GLOBAL_USER, false); - } + ret = __alias_info_enable(appid, uid, uid, false); + if (ret < 0 && uid != GLOBAL_USER) + ret = __alias_info_enable(appid, uid, GLOBAL_USER, false); return ret; } -static int __get_alias_info_list_by_appid(const char *appid, uid_t uid, - uid_t db_uid, GHashTable *list) +static int __get_alias_info_list_by_appid(sqlite3 *db, const char *appid, + uid_t uid, uid_t db_uid, GHashTable *list) { - int ret; - sqlite3_stmt *stmt = NULL; - const char *query; - const char *alias_id; - const char *id; + const char global_user_query[] = + "SELECT alias_appid, appid FROM alias_info WHERE " + "alias_info.appid = ? AND alias_info.appid NOT IN " + "(SELECT appid FROM alias_info_for_uid WHERE uid = ?);"; + const char user_query[] = + "SELECT alias_appid, appid FROM alias_info WHERE " + "appid = ? AND enable = 'true';"; struct alias_info_s *info; - sqlite3 *svc_db = NULL; - - if (__init(db_uid, true, &svc_db) < 0) - return -1; - - if (db_uid == GLOBAL_USER) { - query = "SELECT alias_appid, appid FROM alias_info WHERE " \ - "alias_info.appid = ? AND " \ - "alias_info.appid NOT IN " \ - "(SELECT appid FROM alias_info_for_uid WHERE "\ - "uid = ?);"; - } else { - query = "SELECT alias_appid, appid FROM alias_info WHERE " \ - "appid = ? AND enable = 'true';"; - } - - ret = sqlite3_prepare_v2(svc_db, query, strlen(query), &stmt, NULL); - if (ret != SQLITE_OK) { - _E("sqlite3_prepare_v2() error - %s(%d)", - sqlite3_errmsg(svc_db), ret); - __fini(&svc_db); - return -1; - } + sqlite3_stmt *stmt; + const char *query; + int idx = 1; + int ret; - ret = sqlite3_bind_text(stmt, 1, appid, -1, SQLITE_TRANSIENT); - if (ret != SQLITE_OK) { - _E("sqlite3_bind_text() error - %s(%d)", - sqlite3_errmsg(svc_db), ret); - sqlite3_finalize(stmt); - __fini(&svc_db); - return -1; - } + query = (db_uid == GLOBAL_USER) ? global_user_query : user_query; + __PREPARE_V2(db, query, strlen(query), stmt); - if (db_uid == GLOBAL_USER) { - ret = sqlite3_bind_int(stmt, 2, uid); - if (ret != SQLITE_OK) { - _E("sqlite3_bind_int() error - %s(%d)", - sqlite3_errmsg(svc_db), ret); - sqlite3_finalize(stmt); - __fini(&svc_db); - return -1; - } - } + __BIND_TEXT(db, stmt, idx++, appid); + if (db_uid == GLOBAL_USER) + __BIND_INT(db, stmt, idx++, uid); while (sqlite3_step(stmt) == SQLITE_ROW) { - alias_id = (const char *)sqlite3_column_text(stmt, 0); - if (alias_id == NULL) { - _E("Failed to get alias_appid"); - break; - } - - id = (const char *)sqlite3_column_text(stmt, 1); - if (id == NULL) { - _E("Failed to get appid"); - break; - } - - info = malloc(sizeof(struct alias_info_s)); - if (info == NULL) { - _E("out of memory"); + info = calloc(1, sizeof(struct alias_info_s)); + if (!info) { + _E("Out of memory"); break; } - info->alias_appid = strdup(alias_id); - if (info->alias_appid == NULL) { - _E("out of memory"); - free(info); + idx = 0; + ret = aul_db_save_column_str(stmt, idx++, &info->alias_appid); + if (ret != 0) { + __destroy_alias_info(info); break; } - info->appid = strdup(id); - if (info->appid == NULL) { - _E("out of memory"); - free(info->alias_appid); - free(info); + ret = aul_db_save_column_str(stmt, idx++, &info->appid); + if (ret != 0) { + __destroy_alias_info(info); break; } g_hash_table_insert(list, info->alias_appid, info); - } sqlite3_finalize(stmt); - __fini(&svc_db); return 0; } -int _svc_db_foreach_alias_info_by_appid(int (*callback)( - const char *alias_appid, const char *appid, void *data), - const char *appid, uid_t uid, void *user_data) +static int __alias_info_get_list_by_appid(const char *appid, uid_t uid, + uid_t db_uid, GHashTable *list) { + sqlite3 *db; int ret; - GHashTable *list; - GHashTableIter iter; - gpointer value = NULL; + + db = __open_app_svc_db(db_uid, true); + if (!db) + return -1; + + ret = __get_alias_info_list_by_appid(db, appid, uid, db_uid, list); + aul_db_close(db); + + return ret; +} + +int _svc_db_foreach_alias_info_by_appid( + int (*callback)(const char *, const char *, void *), + const char *appid, uid_t uid, void *user_data) +{ struct alias_info_s *info; - bool invoked = false; + GHashTableIter iter; + GHashTable *list; + gpointer value; + int ret; list = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, __destroy_alias_info); @@ -1348,10 +1028,10 @@ int _svc_db_foreach_alias_info_by_appid(int (*callback)( return -1; } - ret = __get_alias_info_list_by_appid(appid, uid, uid, list); + ret = __alias_info_get_list_by_appid(appid, uid, uid, list); if (ret == 0 && uid != GLOBAL_USER) { - ret = __get_alias_info_list_by_appid(appid, uid, - GLOBAL_USER, list); + ret = __alias_info_get_list_by_appid(appid, uid, GLOBAL_USER, + list); } if (ret != 0) { @@ -1363,106 +1043,76 @@ int _svc_db_foreach_alias_info_by_appid(int (*callback)( g_hash_table_iter_init(&iter, list); while (g_hash_table_iter_next(&iter, NULL, &value)) { info = (struct alias_info_s *)value; - if (info) - callback(info->alias_appid, info->appid, user_data); - invoked = true; - value = NULL; + callback(info->alias_appid, info->appid, user_data); } g_hash_table_destroy(list); - if (!invoked) - _W("alias info is empty"); - return 0; } -static int __get_allowed_info_list(const char *appid, uid_t uid, - uid_t db_uid, GHashTable *tbl) +static void __destroy_allowed_info(gpointer data) { - int ret; - sqlite3_stmt *stmt = NULL; - const char *query; - const char *id; - const char *allowed_id; - struct allowed_info_s *info; - GList *list; - GList *iter; - sqlite3 *svc_db = NULL; + struct allowed_info_s *info = (struct allowed_info_s *)data; - if (__init(db_uid, true, &svc_db) < 0) - return -1; + free(info->allowed_appid); + free(info->appid); + free(info); +} - if (appid) { - query = "SELECT appid, allowed_appid FROM allowed_info WHERE " \ - "appid = ?;"; - } else { - query = "SELECT appid, allowed_appid FROM allowed_info;"; - } +static int __get_allowed_info_list(sqlite3 *db, const char *appid, + uid_t uid, GHashTable *tbl) +{ + const char appid_query[] = + "SELECT appid, allowed_appid FROM allowed_info WHERE " + "appid = ?;"; + const char all_query[] = + "SELECT appid, allowed_appid FROM allowed_info;"; + struct allowed_info_s *info; + sqlite3_stmt *stmt; + const char *query; + GList *list; + char *id; + int idx = 1; + int ret; - ret = sqlite3_prepare_v2(svc_db, query, strlen(query), &stmt, NULL); - if (ret != SQLITE_OK) { - _E("sqlite3_prepare_v2() error - %s(%d)", - sqlite3_errmsg(svc_db), ret); - __fini(&svc_db); - return -1; - } + query = appid ? appid_query : all_query; + __PREPARE_V2(db, query, strlen(query), stmt); - if (appid) { - ret = sqlite3_bind_text(stmt, 1, appid, -1, SQLITE_TRANSIENT); - if (ret != SQLITE_OK) { - _E("sqlite3_bind_text() error - %s(%d)", - sqlite3_errmsg(svc_db), ret); - sqlite3_finalize(stmt); - __fini(&svc_db); - return -1; - } - } + if (appid) + __BIND_TEXT(db, stmt, idx++, appid); while (sqlite3_step(stmt) == SQLITE_ROW) { - id = (const char *)sqlite3_column_text(stmt, 0); - if (id == NULL) { - _E("Failed to get appid"); + idx = 0; + ret = aul_db_save_column_str(stmt, idx++, &id); + if (ret != 0) break; - } - - allowed_id = (const char *)sqlite3_column_text(stmt, 1); - if (allowed_id == NULL) { - _E("Failed to get allowed appid"); - break; - } list = g_hash_table_lookup(tbl, id); if (list) { - iter = g_list_first(list); - if (iter) { - info = (struct allowed_info_s *)iter->data; - if (info && info->uid != db_uid) - continue; + info = (struct allowed_info_s *)list->data; + if (info->uid != uid) { + free(id); + continue; } } - info = malloc(sizeof(struct allowed_info_s)); - if (info == NULL) { - _E("out of memory"); + info = calloc(1, sizeof(struct allowed_info_s)); + if (!info) { + _E("Out of memory"); + free(id); break; } - info->uid = db_uid; - info->appid = strdup(id); - if (info->appid == NULL) { - _E("out of memory"); - free(info); - break; - } + info->appid = id; - info->allowed_appid = strdup(allowed_id); - if (info->allowed_appid == NULL) { - _E("out of memory"); - free(info->appid); - free(info); + ret = aul_db_save_column_str(stmt, idx++, &info->allowed_appid); + if (ret != 0) { + __destroy_allowed_info(info); break; } + info->uid = uid; + if (list) { list = g_list_append(list, info); } else { @@ -1472,47 +1122,43 @@ static int __get_allowed_info_list(const char *appid, uid_t uid, } sqlite3_finalize(stmt); - __fini(&svc_db); return 0; } -static void __destroy_allowed_info(gpointer data) +static int __allowed_info_get_list(const char *appid, uid_t uid, + GHashTable *tbl) { - struct allowed_info_s *info = (struct allowed_info_s *)data; + sqlite3 *db; + int ret; + + db = __open_app_svc_db(uid, true); + if (!db) + return -1; - if (info == NULL) - return; + ret = __get_allowed_info_list(db, appid, uid, tbl); + aul_db_close(db); - if (info->allowed_appid) - free(info->allowed_appid); - if (info->appid) - free(info->appid); - free(info); + return ret; } static void __destroy_allowed_info_list(gpointer data) { GList *list = (GList *)data; - if (list == NULL) - return; - g_list_free_full(list, __destroy_allowed_info); } -int _svc_db_foreach_allowed_info_by_appid(int (*callback)(const char *appid, - const char *allowed_appid, void *data), +int _svc_db_foreach_allowed_info_by_appid( + int (*callback)(const char *, const char *, void *), const char *appid, uid_t uid, void *user_data) { - int ret; - GHashTable *tbl; - GHashTableIter hash_iter; - gpointer value = NULL; struct allowed_info_s *info; - bool invoked = false; - GList *list; + GHashTableIter hash_iter; + GHashTable *tbl; + gpointer value; GList *iter; + int ret; tbl = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, __destroy_allowed_info_list); @@ -1521,9 +1167,9 @@ int _svc_db_foreach_allowed_info_by_appid(int (*callback)(const char *appid, return -1; } - ret = __get_allowed_info_list(appid, uid, uid, tbl); + ret = __allowed_info_get_list(appid, uid, tbl); if (ret == 0 && uid != GLOBAL_USER) - ret = __get_allowed_info_list(appid, uid, GLOBAL_USER, tbl); + ret = __allowed_info_get_list(appid, GLOBAL_USER, tbl); if (ret != 0) { _E("Failed to get allowed info table"); @@ -1533,29 +1179,21 @@ int _svc_db_foreach_allowed_info_by_appid(int (*callback)(const char *appid, g_hash_table_iter_init(&hash_iter, tbl); while (g_hash_table_iter_next(&hash_iter, NULL, &value)) { - list = (GList *)value; - if (list) { - iter = g_list_first(list); - while (iter) { - info = (struct allowed_info_s *)iter->data; - if (info) - callback(info->appid, info->allowed_appid, user_data); - iter = g_list_next(iter); - } + iter = (GList *)value; + while (iter) { + info = (struct allowed_info_s *)iter->data; + callback(info->appid, info->allowed_appid, user_data); + iter = g_list_next(iter); } - invoked = true; - value = NULL; } - g_hash_table_destroy(tbl); - if (!invoked) - _W("allowed info is empty"); + g_hash_table_destroy(tbl); return 0; } -int _svc_db_foreach_allowed_info(int (*callback)(const char *appid, - const char *allowed_appid, void *data), +int _svc_db_foreach_allowed_info( + int (*callback)(const char *, const char *, void *), uid_t uid, void *user_data) { return _svc_db_foreach_allowed_info_by_appid(callback, NULL,