DB open with READONLY in rua_history_load_db, rua_is_latest_app api. 39/47939/5
authorhyunho kang <hhstark.kang@samsung.com>
Thu, 10 Sep 2015 07:41:17 +0000 (16:41 +0900)
committerhyunho kang <hhstark.kang@samsung.com>
Thu, 10 Sep 2015 10:05:09 +0000 (19:05 +0900)
Change-Id: Ie9a49c4940cf2aca24f3d8248fcba04cfdd00643
Signed-off-by: hyunho kang <hhstark.kang@samsung.com>
src/rua.c

index 1e70911..2e2e64c 100644 (file)
--- a/src/rua.c
+++ b/src/rua.c
@@ -167,6 +167,11 @@ int rua_history_load_db(char ***table, int *nrows, int *ncols)
        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);
+       snprintf(defname, sizeof(defname), "%s/%s", rua_db_path, RUA_DB_NAME);
 
        if (table == NULL)
                return -1;
@@ -175,16 +180,25 @@ 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);
+               return -1;
+       }
+
        snprintf(query, QUERY_MAXLEN,
                 "select * from %s order by launch_time desc;", RUA_HISTORY);
 
-       r = sqlite3_get_table(_db, query, &db_result, nrows, ncols, &db_err);
+       r = sqlite3_get_table(db, query, &db_result, nrows, ncols, &db_err);
 
        if (r == SQLITE_OK)
                *table = db_result;
        else
                sqlite3_free_table(db_result);
 
+       if (db)
+               db_util_close(db);
+
        return r;
 }
 
@@ -243,14 +257,25 @@ int rua_history_get_rec(struct rua_rec *rec, char **table, int nrows, int ncols,
 
 int rua_is_latest_app(const char *pkg_name)
 {
-       int r;
+       int r = -1;
        sqlite3_stmt *stmt;
        const unsigned char *ct;
+       sqlite3 *db;
 
-       if (!pkg_name || !_db)
+       char defname[FILENAME_MAX];
+       const char *rua_db_path = tzplatform_getenv(TZ_USER_DB);
+       snprintf(defname, sizeof(defname), "%s/%s", rua_db_path, RUA_DB_NAME);
+
+       if (!pkg_name)
                return -1;
 
-       r = sqlite3_prepare(_db, Q_LATEST, sizeof(Q_LATEST), &stmt, NULL);
+       r = db_util_open_with_options(defname, &db, SQLITE_OPEN_READONLY, NULL);
+       if (r) {
+               db_util_close(db);
+               return -1;
+       }
+
+       r = sqlite3_prepare(db, Q_LATEST, sizeof(Q_LATEST), &stmt, NULL);
        if (r != SQLITE_OK) {
                return -1;
        }
@@ -259,18 +284,23 @@ int rua_is_latest_app(const char *pkg_name)
        if (r == SQLITE_ROW) {
                ct = sqlite3_column_text(stmt, 0);
                if (ct == NULL || ct[0] == '\0') {
-                       sqlite3_finalize(stmt);
-                       return -1;
+                       r = -1;
+                       goto out;
                }
 
                if (strncmp(pkg_name, ct, strlen(pkg_name)) == 0) {
-                       sqlite3_finalize(stmt);
-                       return 0;
+                       r = 0;
+                       goto out;
                }
        }
 
-       sqlite3_finalize(stmt);
-       return -1;
+out:
+       if (stmt)
+               sqlite3_finalize(stmt);
+       if (db)
+               db_util_close(db);
+
+       return r;
 }
 
 int rua_init(void)