From f1e05f4e231382abdbc5a3bc2d2db355a85f17a6 Mon Sep 17 00:00:00 2001 From: Hyunho Kang Date: Thu, 30 Jun 2016 11:00:08 +0900 Subject: [PATCH] Add multi-user feature Add APIs: - rua_stat_update_for_uid - rua_add_history_for_uid - rua_delete_history_with_pkgname_for_uid - rua_delete_history_with_apppath_for_uid - rua_clear_history_for_uid - rua_history_load_db_for_uid - rua_is_latest_app_for_uid - rua_db_delete_history - rua_db_add_history - rua_stat_get_stat_tags_for_uid - rua_stat_db_update Change-Id: I50b203b41a7864ae0609eaaf9125bb383c8a5b6c Signed-off-by: Hyunho Kang --- CMakeLists.txt | 2 + include/db-schema.h | 17 ++---- include/rua.h | 24 ++++++++ include/rua_internal.h | 6 +- include/rua_stat.h | 15 ++++- include/rua_stat_internal.h | 3 +- include/rua_util.h | 55 ++++++++++++++++++ src/rua.c | 138 +++++++++++++++++++++++++++++++------------- src/rua_internal.c | 71 ++++++++--------------- src/rua_stat.c | 46 ++++++++++++++- src/rua_stat_internal.c | 57 ++++++++---------- src/rua_util.c | 79 +++++++++++++++++++++++++ test/rua-test.c | 126 ++++++++++++++++++++++++++++++++++++---- 13 files changed, 490 insertions(+), 149 deletions(-) create mode 100644 include/rua_util.h create mode 100644 src/rua_util.c diff --git a/CMakeLists.txt b/CMakeLists.txt index c1a36dc..2299f4e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,7 @@ SET(INCLUDEDIR "\${prefix}/include/${PROJECT_NAME}") SET(VERSION 0.1.0) SET(SRCS + src/rua_util.c src/rua.c src/rua_internal.c src/rua_stat.c @@ -44,6 +45,7 @@ INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/rua.h DESTINATION include/${PR INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/rua_internal.h DESTINATION include/${PROJECT_NAME}) INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/rua_stat.h DESTINATION include/${PROJECT_NAME}) INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/rua_stat_internal.h DESTINATION include/${PROJECT_NAME}) +INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/rua_util.h DESTINATION include/${PROJECT_NAME}) ADD_SUBDIRECTORY(test) diff --git a/include/db-schema.h b/include/db-schema.h index 3054493..19efbcd 100644 --- a/include/db-schema.h +++ b/include/db-schema.h @@ -21,26 +21,23 @@ PRAGMA journal_mode = PERSIST; \ \ CREATE TABLE IF NOT EXISTS rua_history ( \ - id INTEGER PRIMARY KEY, \ pkg_name TEXT, \ app_path TEXT, \ arg TEXT, \ - launch_time INTEGER \ + launch_time INTEGER, \ + PRIMARY KEY(pkg_name) \ );" - #define CREATE_RUA_STAT_TABLE " \ CREATE TABLE if not exists rua_panel_stat ( \ -caller_panel TEXT NOT NULL, \ -rua_stat_tag TEXT NOT NULL, \ -score INTEGER DEFAULT 0, \ -PRIMARY KEY(rua_stat_tag, caller_panel) \ + caller_panel TEXT NOT NULL, \ + rua_stat_tag TEXT NOT NULL, \ + score INTEGER DEFAULT 0, \ + PRIMARY KEY(rua_stat_tag, caller_panel) \ );" - /* table index */ enum { - RUA_COL_ID = 0x00, RUA_COL_PKGNAME, RUA_COL_APPPATH, RUA_COL_ARG, @@ -53,6 +50,4 @@ enum { RUA_SATA_COL_SCORE }; - - #endif /* __RUA_SCHEMA_H__ */ diff --git a/include/rua.h b/include/rua.h index 4007df2..d9c7235 100755 --- a/include/rua.h +++ b/include/rua.h @@ -28,6 +28,16 @@ #include #include #include +#include +#include + +#include + +#ifdef LOG_TAG +#undef LOG_TAG +#endif + +#define LOG_TAG "RUA" #ifndef API #define API __attribute__ ((visibility("default"))) @@ -64,6 +74,15 @@ struct rua_rec { }; /** + * @brief Add history + * @param[in] pkg_name package name to delete history + * @return 0 on success, otherwise a nagative error value + * @retval 0 on successful + * @retval -1 on failed + */ +API int rua_add_history_for_uid(char *pkg_name, char *app_path, char *arg, uid_t uid); + +/** * @brief Delete history with pkg_name * @param[in] pkg_name package name to delete history * @return 0 on success, otherwise a nagative error value @@ -71,6 +90,7 @@ struct rua_rec { * @retval -1 on failed */ API int rua_delete_history_with_pkgname(char *pkg_name); +API int rua_delete_history_with_pkgname_for_uid(char *pkg_name, uid_t uid); /** * @brief Delete history with app_path @@ -80,6 +100,7 @@ API int rua_delete_history_with_pkgname(char *pkg_name); * @retval -1 on failed */ API int rua_delete_history_with_apppath(char *app_path); +API int rua_delete_history_with_apppath_for_uid(char *app_path, uid_t uid); /** * @brief Clear history @@ -88,6 +109,7 @@ API int rua_delete_history_with_apppath(char *app_path); * @retval -1 on failed */ API int rua_clear_history(void); +API int rua_clear_history_for_uid(uid_t uid); /** * @brief Load recently used application history db. @@ -99,6 +121,7 @@ API int rua_clear_history(void); * @retval -1 on failed */ API int rua_history_load_db(char ***table, int *nrows, int *ncols); +API int rua_history_load_db_for_uid(char ***table, int *nrows, int *ncols, uid_t uid); /** * @brief Unload recently used application history db. @@ -131,6 +154,7 @@ API int rua_history_get_rec(struct rua_rec *rec, char **table, * @retval -1 if not lastest applicaton or on failed */ API int rua_is_latest_app(const char *pkg_name); +API int rua_is_latest_app_for_uid(const char *pkg_name, uid_t uid); /** * @brief Initialize rua diff --git a/include/rua_internal.h b/include/rua_internal.h index e6a6c6c..6158faf 100644 --- a/include/rua_internal.h +++ b/include/rua_internal.h @@ -48,9 +48,7 @@ extern "C" { #define RUA_DB_NAME ".rua.db" #define RUA_HISTORY "rua_history" #define QUERY_MAXLEN 4096 -#define Q_LATEST \ - "select pkg_name from rua_history " \ - "order by launch_time desc limit 1 " +#define MAX_UID_STR_BUFSZ 20 /** * @brief Delete history from DB @@ -72,4 +70,4 @@ API int rua_db_add_history(struct rua_rec *rec); #ifdef __cplusplus } #endif -#endif /*__RUA_INTERNAL_H__*/ \ No newline at end of file +#endif /*__RUA_INTERNAL_H__*/ diff --git a/include/rua_stat.h b/include/rua_stat.h index 795d13b..03fb90b 100755 --- a/include/rua_stat.h +++ b/include/rua_stat.h @@ -25,7 +25,10 @@ #ifndef __RUA_STAT_H__ #define __RUA_STAT_H__ +#include +#include #include +#include #ifndef API #define API __attribute__ ((visibility("default"))) @@ -35,6 +38,14 @@ extern "C" { #endif +/** + * @brief Update rua status + * @param[in] caller, tag, uid + * @return 0 on success, otherwise a nagative error value + * @retval 0 on successful + * @retval -1 on failed + */ +API int rua_stat_update_for_uid(char *caller, char *tag, uid_t uid); /** * @brief Get rua status tag list @@ -45,7 +56,9 @@ extern "C" { */ API int rua_stat_get_stat_tags(char *caller, int (*rua_stat_tag_iter_fn)(const char *rua_stat_tag, void *data), void *data); - +API int rua_stat_get_stat_tags_for_uid(char *caller, + int (*rua_stat_tag_iter_fn)(const char *rua_stat_tag, void *data), + void *data, uid_t uid); #ifdef __cplusplus } #endif diff --git a/include/rua_stat_internal.h b/include/rua_stat_internal.h index daa79a6..838ce1a 100644 --- a/include/rua_stat_internal.h +++ b/include/rua_stat_internal.h @@ -47,8 +47,7 @@ extern "C" { #define WIN_SCORE 100 #define LOSE_SCORE_RATE 0.7f -int _rua_stat_init(sqlite3 **db, int flags); -int _rua_stat_fini(sqlite3 *db); +int _rua_stat_init(sqlite3 **db, char *db_name, int flags, uid_t uid); /** * @brief Add application launch status. diff --git a/include/rua_util.h b/include/rua_util.h new file mode 100644 index 0000000..260e47f --- /dev/null +++ b/include/rua_util.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2016 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. + */ + +/** + * @file rua_util.h + * @brief RUA UTIL API declaration header file. + * @author Hyunho Kang (hhstark.kang@samsung.com) + * @version 0.1 + * @history 0.1: RUA UTIL API Declarations, structure declaration + */ + +#ifndef __RUA_UTIL_H__ +#define __RUA_UTIL_H__ + +#include +#include +#include +#include + +#ifndef API +#define API __attribute__ ((visibility("default"))) +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef LOG_TAG +#undef LOG_TAG +#endif + +#define LOG_TAG "RUA" +#define BASE_UID 5000 + +char *_rua_util_get_db_path(uid_t uid, char *db_name); +int _rua_util_open_db(sqlite3 **db, int flags, uid_t uid, char *db_name); +int _rua_util_check_uid(uid_t target_uid); + +#ifdef __cplusplus +} +#endif +#endif /*__RUA_UTIL_H__*/ diff --git a/src/rua.c b/src/rua.c index ed8f847..42c28f7 100644 --- a/src/rua.c +++ b/src/rua.c @@ -22,21 +22,64 @@ /* For multi-user support */ #include +#include "rua_util.h" #include "rua_internal.h" #include "rua.h" #include "db-schema.h" -int rua_delete_history_with_pkgname(char *pkg_name) +int rua_add_history_for_uid(char *pkg_name, char *app_path, char *arg, uid_t uid) { int r; - bundle *b = bundle_create(); + char time_str[32] = {0,}; + bundle *b = NULL; + + if (pkg_name == NULL || app_path == NULL) { + LOGE("invalid param"); + return -1; + } + + r = _rua_util_check_uid(uid); + if (r == -1) + return r; + + b = bundle_create(); if (b == NULL) { LOGE("bundle_create fail out of memory."); return -1; } + snprintf(time_str, sizeof(time_str), "%d", (int)time(NULL)); + bundle_add_str(b, AUL_K_RUA_PKGNAME, pkg_name); + bundle_add_str(b, AUL_K_RUA_APPPATH, app_path); + bundle_add_str(b, AUL_K_RUA_ARG, arg); + bundle_add_str(b, AUL_K_RUA_TIME, time_str); + r = aul_add_rua_history_for_uid(b, uid); + LOGI("rua_add_history_for_uid result : %d ", r); + bundle_free(b); + return r; +} + +int rua_delete_history_with_pkgname(char *pkg_name) +{ + return rua_delete_history_with_pkgname_for_uid(pkg_name, getuid()); +} + +int rua_delete_history_with_pkgname_for_uid(char *pkg_name, uid_t uid) +{ + int r; + bundle *b; + + r = _rua_util_check_uid(uid); + if (r == -1) + return r; + + b = bundle_create(); + if (b == NULL) { + LOGE("bundle_create fail out of memory."); + return -1; + } bundle_add_str(b, AUL_K_RUA_PKGNAME, pkg_name); - r = aul_delete_rua_history(b); + r = aul_delete_rua_history_for_uid(b, uid); LOGI("rua_delete_history_with_pkgname result : %d ", r); bundle_free(b); return r; @@ -44,45 +87,61 @@ int rua_delete_history_with_pkgname(char *pkg_name) int rua_delete_history_with_apppath(char *app_path) { + return rua_delete_history_with_apppath_for_uid(app_path, getuid()); +} + +int rua_delete_history_with_apppath_for_uid(char *app_path, uid_t uid) +{ int r; - bundle *b = bundle_create(); + bundle *b; + + r = _rua_util_check_uid(uid); + if (r == -1) + return r; + + b = bundle_create(); if (b == NULL) { LOGE("bundle_create fail out of memory."); return -1; } - bundle_add_str(b, AUL_K_RUA_APPPATH, app_path); - r = aul_delete_rua_history(b); + r = aul_delete_rua_history_for_uid(b, uid); LOGI("rua_delete_history_with_apppath result : %d ", r); bundle_free(b); - return r; } int rua_clear_history(void) { + return rua_clear_history_for_uid(getuid()); +} + +int rua_clear_history_for_uid(uid_t uid) +{ int r; - r = aul_delete_rua_history(NULL); + + r = _rua_util_check_uid(uid); + if (r == -1) + return r; + + r = aul_delete_rua_history_for_uid(NULL, uid); LOGI("rua_clear_history result : %d ", r); return r; } int rua_history_load_db(char ***table, int *nrows, int *ncols) { + return rua_history_load_db_for_uid(table, nrows, ncols, getuid()); +} + +int rua_history_load_db_for_uid(char ***table, int *nrows, int *ncols, uid_t uid) +{ int r; char query[QUERY_MAXLEN]; char *db_err = NULL; char **db_result = NULL; sqlite3 *db = NULL; - char defname[FILENAME_MAX]; - const char *rua_db_path = tzplatform_getenv(TZ_USER_DB); - if (rua_db_path == NULL) { - LOGE("fail to get rua_db_path"); - return -1; - } - snprintf(defname, sizeof(defname), "%s/%s", rua_db_path, RUA_DB_NAME); - if (table == NULL) return -1; if (nrows == NULL) @@ -90,14 +149,16 @@ int rua_history_load_db(char ***table, int *nrows, int *ncols) if (ncols == NULL) return -1; - r = db_util_open_with_options(defname, &db, SQLITE_OPEN_READONLY, NULL); - if (r) { - db_util_close(db); + r = _rua_util_check_uid(uid); + if (r == -1) + return r; + + r = _rua_util_open_db(&db, SQLITE_OPEN_READONLY, uid, RUA_DB_NAME); + if (r != SQLITE_OK) return -1; - } snprintf(query, QUERY_MAXLEN, - "select * from %s order by launch_time desc;", RUA_HISTORY); + "select pkg_name, app_path, arg, launch_time from %s order by launch_time desc;", RUA_HISTORY); r = sqlite3_get_table(db, query, &db_result, nrows, ncols, &db_err); @@ -136,14 +197,11 @@ int rua_history_get_rec(struct rua_rec *rec, char **table, int nrows, int ncols, db_result = table + ((row + 1) * ncols); - tmp = db_result[RUA_COL_ID]; - if (tmp) - rec->id = atoi(tmp); - tmp = db_result[RUA_COL_PKGNAME]; if (tmp) rec->pkg_name = tmp; + LOGI("get rec pkg_name %s", rec->pkg_name); tmp = db_result[RUA_COL_APPPATH]; if (tmp) rec->app_path = tmp; @@ -161,29 +219,29 @@ int rua_history_get_rec(struct rua_rec *rec, char **table, int nrows, int ncols, int rua_is_latest_app(const char *pkg_name) { + return rua_is_latest_app_for_uid(pkg_name, getuid()); +} + +int rua_is_latest_app_for_uid(const char *pkg_name, uid_t uid) +{ int r = -1; sqlite3_stmt *stmt; const unsigned char *ct; - sqlite3 *db; - - char defname[FILENAME_MAX]; - const char *rua_db_path = tzplatform_getenv(TZ_USER_DB); - if (rua_db_path == NULL) { - LOGE("fail to get rua_db_path"); - return -1; - } - snprintf(defname, sizeof(defname), "%s/%s", rua_db_path, RUA_DB_NAME); + sqlite3 *db = NULL; + char *query = "select pkg_name from rua_history order by launch_time desc limit 1;"; if (!pkg_name) return -1; - r = db_util_open_with_options(defname, &db, SQLITE_OPEN_READONLY, NULL); - if (r) { - db_util_close(db); + r = _rua_util_check_uid(uid); + if (r == -1) + return r; + + r = _rua_util_open_db(&db, SQLITE_OPEN_READONLY, uid, RUA_DB_NAME); + if (r != SQLITE_OK) return -1; - } - r = sqlite3_prepare(db, Q_LATEST, sizeof(Q_LATEST), &stmt, NULL); + r = sqlite3_prepare(db, query, sizeof(query), &stmt, NULL); if (r != SQLITE_OK) { db_util_close(db); return -1; @@ -220,4 +278,4 @@ int rua_init(void) int rua_fini(void) { return 0; -} \ No newline at end of file +} diff --git a/src/rua_internal.c b/src/rua_internal.c index 4cc5f05..6438983 100644 --- a/src/rua_internal.c +++ b/src/rua_internal.c @@ -1,3 +1,18 @@ +/* + * Copyright (c) 2016 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. + */ #include #include @@ -5,6 +20,7 @@ #include "rua_internal.h" #include "db-schema.h" +#include "rua_util.h" static int __exec(sqlite3 *db, char *query) { @@ -40,19 +56,9 @@ static sqlite3 *__db_init() int r; sqlite3 *db = NULL; - char defname[FILENAME_MAX]; - const char *rua_db_path = tzplatform_getenv(TZ_USER_DB); - if (rua_db_path == NULL) { - LOGE("fail to get rua_db_path"); + r = _rua_util_open_db(&db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, getuid(), RUA_DB_NAME); + if (r != SQLITE_OK) return NULL; - } - snprintf(defname, sizeof(defname), "%s/%s", rua_db_path, RUA_DB_NAME); - - r = db_util_open_with_options(defname, &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL); - if (r) { - db_util_close(db); - return NULL; - } r = __create_table(db); if (r) { @@ -68,7 +74,6 @@ int rua_db_delete_history(bundle *b) int r; sqlite3 *db = NULL; char query[QUERY_MAXLEN]; - char *pkg_name = NULL; char *app_path = NULL; char *errmsg = NULL; @@ -105,15 +110,12 @@ int rua_db_delete_history(bundle *b) db_util_close(db); return result; - } int rua_db_add_history(struct rua_rec *rec) { int r; - int cnt = 0; char query[QUERY_MAXLEN]; - sqlite3_stmt *stmt; sqlite3 *db = NULL; db = __db_init(); @@ -129,37 +131,12 @@ int rua_db_add_history(struct rua_rec *rec) } snprintf(query, QUERY_MAXLEN, - "select count(*) from %s where pkg_name = '%s';", RUA_HISTORY, - rec->pkg_name); - - r = sqlite3_prepare(db, query, sizeof(query), &stmt, NULL); - if (r != SQLITE_OK) { - LOGE("Error sqlite3_prepare fail"); - db_util_close(db); - return -1; - } - - r = sqlite3_step(stmt); - if (r == SQLITE_ROW) - cnt = sqlite3_column_int(stmt, 0); - - sqlite3_finalize(stmt); - - if (cnt == 0) - /* insert */ - snprintf(query, QUERY_MAXLEN, - "insert into %s ( pkg_name, app_path, arg, launch_time ) " - " values ( \"%s\", \"%s\", \"%s\", %d ) ", - RUA_HISTORY, - rec->pkg_name ? rec->pkg_name : "", - rec->app_path ? rec->app_path : "", - rec->arg ? rec->arg : "", (int)time(NULL)); - else - /* update */ - snprintf(query, QUERY_MAXLEN, - "update %s set arg='%s', launch_time='%d' where pkg_name = '%s';", - RUA_HISTORY, - rec->arg ? rec->arg : "", (int)time(NULL), rec->pkg_name); + "insert or replace into %s ( pkg_name, app_path, arg, launch_time) " + " values ( \"%s\", \"%s\", \"%s\", %d) ", + RUA_HISTORY, + rec->pkg_name ? rec->pkg_name : "", + rec->app_path ? rec->app_path : "", + rec->arg ? rec->arg : "", (int)rec->launch_time); r = __exec(db, query); if (r == -1) { diff --git a/src/rua_stat.c b/src/rua_stat.c index 78f7da4..33fdc1e 100755 --- a/src/rua_stat.c +++ b/src/rua_stat.c @@ -23,23 +23,64 @@ #include #include #include +#include +#include #include +#include #include "rua_stat_internal.h" #include "rua_stat.h" +int rua_stat_update_for_uid(char *caller, char *tag, uid_t uid) +{ + int r; + bundle *b = NULL; + + r = _rua_util_check_uid(uid); + if (r == -1) + return r; + + if (caller == NULL || tag == NULL) { + LOGE("invalid param"); + return -1; + } + + b = bundle_create(); + if (b == NULL) { + LOGE("bundle_create fail out of memory."); + return -1; + } + bundle_add_str(b, AUL_SVC_K_RUA_STAT_CALLER, caller); + bundle_add_str(b, AUL_SVC_K_RUA_STAT_TAG, tag); + r = aul_update_rua_stat_for_uid(b, uid); + LOGI("rua_add_history_for_uid result : %d ", r); + bundle_free(b); + return r; +} + int rua_stat_get_stat_tags(char *caller, int (*rua_stat_tag_iter_fn)(const char *rua_stat_tag, void *data), void *data) { + return rua_stat_get_stat_tags_for_uid(caller, rua_stat_tag_iter_fn, data, getuid()); +} + +int rua_stat_get_stat_tags_for_uid(char *caller, + int (*rua_stat_tag_iter_fn)(const char *rua_stat_tag, void *data), + void *data, uid_t uid) +{ int r; sqlite3_stmt *stmt; char query[QUERY_MAXLEN]; const unsigned char *ct; sqlite3 *db = NULL; - r = _rua_stat_init(db, SQLITE_OPEN_READONLY); + r = _rua_util_check_uid(uid); + if (r == -1) + return r; + + r = _rua_stat_init(&db, RUA_STAT_DB_NAME, SQLITE_OPEN_READONLY, uid); if (r == -1) { LOGE("__rua_stat_init fail"); return -1; @@ -72,7 +113,8 @@ out: if (stmt) sqlite3_finalize(stmt); - _rua_stat_fini(db); + if (db) + db_util_close(db); return r; } diff --git a/src/rua_stat_internal.c b/src/rua_stat_internal.c index 5fd59e6..ee83905 100644 --- a/src/rua_stat_internal.c +++ b/src/rua_stat_internal.c @@ -1,3 +1,20 @@ +/* + * Copyright (c) 2016 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. + */ +#include +#include #include #include #include @@ -8,6 +25,7 @@ #include "db-schema.h" #include "rua_stat_internal.h" +#include "rua_util.h" int __rua_stat_insert(sqlite3 *db, char *caller, char *rua_stat_tag) { @@ -171,43 +189,20 @@ static int __create_table(sqlite3 *db) return 0; } -int _rua_stat_init(sqlite3 **db, int flags) +int _rua_stat_init(sqlite3 **db, char *db_name, int flags, uid_t uid) { - char defname[FILENAME_MAX]; - const char *rua_stat_db_path = tzplatform_getenv(TZ_USER_DB); int r; - - snprintf(defname, sizeof(defname), "%s/%s", rua_stat_db_path, RUA_STAT_DB_NAME); - r = db_util_open_with_options(defname, db, flags, NULL); - if (r) { - LOGE("db util open error(%d/%d/%d/%s)", r, - sqlite3_errcode(*db), - sqlite3_extended_errcode(*db), - sqlite3_errmsg(*db)); - return NULL; - - } - + r = _rua_util_open_db(db, flags, uid, db_name); r = __create_table(*db); if (r) { db_util_close(*db); - return NULL; + return -1; } if (*db == NULL) { - LOGW("__rua_stat_init error"); + LOGE("__rua_stat_init error"); return -1; } - - return 0; -} - -int _rua_stat_fini(sqlite3 *db) -{ - if (db) { - db_util_close(db); - db = NULL; - } return 0; } @@ -219,7 +214,7 @@ int rua_stat_db_update(char *caller, char *rua_stat_tag) LOGD("rua_stat_update start"); - r = _rua_stat_init(&db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE); + r = _rua_stat_init(&db, RUA_STAT_DB_NAME, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, getuid()); if (r == -1) { LOGE("__rua_stat_init fail"); return -1; @@ -240,7 +235,6 @@ int rua_stat_db_update(char *caller, char *rua_stat_tag) return -1; } - r = __rua_stat_lose_score_update(db, caller, rua_stat_tag); if (r != SQLITE_DONE) { LOGE("__rua_stat_lose_score_insert fail."); @@ -257,10 +251,9 @@ int rua_stat_db_update(char *caller, char *rua_stat_tag) return -1; } } - - _rua_stat_fini(db); + if (db) + db_util_close(db); LOGD("rua_stat_update done"); - return r; } diff --git a/src/rua_util.c b/src/rua_util.c new file mode 100644 index 0000000..e52ab3d --- /dev/null +++ b/src/rua_util.c @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2016 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. + */ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "rua_util.h" + +#define DBPATH_LEN_MAX 4096 + +char *_rua_util_get_db_path(uid_t uid, char *db_name) +{ + struct group *grpinfo = NULL; + char db_path[DBPATH_LEN_MAX] = {0, }; + struct passwd *userinfo = getpwuid(uid); + if (userinfo == NULL) { + LOGE("getpwuid(%d) returns NULL !", uid); + return NULL; + } + grpinfo = getgrnam("users"); + if (grpinfo == NULL) { + LOGE("getgrnam(users) returns NULL !"); + return NULL; + } + + if (grpinfo->gr_gid != userinfo->pw_gid) { + LOGE("UID [%d] does not belong to 'users' group!", uid); + return NULL; + } + snprintf(db_path, sizeof(db_path), "%s/.applications/dbspace/%s", userinfo->pw_dir, db_name); + LOGD("db path %s", db_path); + return db_path; +} + +int _rua_util_open_db(sqlite3 **db, int flags, uid_t uid, char *db_name) +{ + int r; + char *db_path = _rua_util_get_db_path(uid, db_name); + r = db_util_open_with_options(db_path, db, flags, NULL); + if (r) { + LOGE("db util open error(%d/%d/%d/%s)", r, + sqlite3_errcode(*db), + sqlite3_extended_errcode(*db), + sqlite3_errmsg(*db)); + return -1; + } + return r; +} + +int _rua_util_check_uid(uid_t target_uid) +{ + uid_t uid = getuid(); + if (uid > BASE_UID && uid != target_uid) { + LOGE("Invalid UID : %d, target UID : %d", uid, target_uid); + return -1; + } + return 0; +} diff --git a/test/rua-test.c b/test/rua-test.c index 68dcbe7..8d217b3 100644 --- a/test/rua-test.c +++ b/test/rua-test.c @@ -19,33 +19,139 @@ #include #include #include +#include +#include +#include /* For multi-user support */ #include #include "rua.h" #include "rua_internal.h" +#include "rua_stat.h" +#include "rua_stat_internal.h" -static int __add_history(char *pkgname) +static int __add_history() { int ret; - struct rua_rec rec; + char *app_path; + char *pkgname = "org.tizen.ruatester"; - memset(&rec, 0, sizeof(rec)); - rec.pkg_name = pkgname; - rec.app_path = (char *)tzplatform_mkpath(TZ_SYS_RW_APP, pkgname); + app_path = (char *)tzplatform_mkpath(TZ_SYS_RW_APP, pkgname); + ret = rua_add_history_for_uid(pkgname, app_path, NULL, 5001); + return ret; +} + +static int __delete_history_with_pkgname() +{ + int ret; + char *app_path; + char *pkgname = "org.tizen.ruatester"; + + ret = rua_delete_history_with_pkgname_for_uid(pkgname, 5001); + return ret; +} + +static int __load_rua_history() +{ + char **table = NULL; + int rows = 0; + int cols = 0; + struct rua_rec record; + + if (rua_history_load_db_for_uid(&table, &rows, &cols, 5001) || !table) { + printf("fail to load rua history \n"); + return -1; + } + + int row; + for (row = 0; row < rows; ++row) { + rua_history_get_rec(&record, table, rows, cols, row); + printf("pkgname : %s, time : %d \n", record.pkg_name, record.launch_time); + } + + rua_history_unload_db(&table); + return 0; +} + +static int __update_stat() +{ + int ret; + char *app_path; + char *pkgname = "org.tizen.ruatester"; + + ret = rua_stat_update_for_uid("ruacaller", "org.tizen.ruatester", 5001); + return ret; +} - ret = rua_db_add_history(&rec); +static int __rua_stat_tag_iter_cb(const char *rua_stat_tag, void *data) +{ + printf("rua_stat_tag : %s \n", rua_stat_tag); +} +static int __get_stat_tags() +{ + int ret; + ret = rua_stat_get_stat_tags_for_uid("ruacaller", __rua_stat_tag_iter_cb, NULL, 5001); return ret; } -int main(int argc, char* argv[]) +static gboolean run_test(int selected_number) +{ + gboolean go_to_loop = TRUE; + + switch (selected_number) { + case 0: + go_to_loop = FALSE; + break; + + case 1: + __add_history(); + break; + + case 2: + __delete_history_with_pkgname(); + break; + + case 3: + __load_rua_history(); + break; + + case 4: + __update_stat(); + break; + + case 5: + __get_stat_tags(); + break; + + default: + break; + } + + return go_to_loop; + +} + +int main() { int ret = 0; + int test_num; + gboolean run_next = TRUE; - if (argc != 2) - return 0; - ret = __add_history(argv[1]); + while(run_next) { + printf("==========================================\n"); + printf(" Basic test menu \n"); + printf("==========================================\n"); + printf(" 0. EXIT\n"); + printf(" 1. Add rua history to DEFAULT USER(5001)\n"); + printf(" 2. Delete history with pkgname\n"); + printf(" 3. Load RUA history\n"); + printf(" 4. Update RUA stat\n"); + printf(" 5. Get RUA stat tags\n"); + printf("------------------------------------------\n"); + scanf("%d", &test_num); + run_next = run_test(test_num); + } return ret; } -- 2.7.4