Remove duplicated codes 38/214838/2
authorHwankyu Jhun <h.jhun@samsung.com>
Fri, 27 Sep 2019 04:16:47 +0000 (13:16 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Fri, 27 Sep 2019 04:38:20 +0000 (13:38 +0900)
- Uses aul_db functions

Change-Id: I2bcddb51860a985dcd5ed50b16039460a290d785
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/aul_comp_info_internal.c

index ffa1b13..5f9f399 100644 (file)
  */
 
 #define _GNU_SOURCE
+#include <ctype.h>
+#include <linux/limits.h>
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <stdbool.h>
-#include <linux/limits.h>
-#include <ctype.h>
-#include <sqlite3.h>
-#include <glib.h>
 #include <bundle.h>
 #include <bundle_internal.h>
+#include <glib.h>
+#include <sqlite3.h>
 #include <vconf.h>
 
+#include "aul.h"
+#include "aul_api.h"
 #include "aul_comp_info_internal.h"
-#include "aul_util.h"
+#include "aul_db.h"
 #include "aul_sock.h"
-#include "aul_api.h"
-#include "aul.h"
+#include "aul_util.h"
 
-#define ROOT_UID 0
+#define COMPONENT_DB ".component.db"
 #define DEFAULT_LOCALE "No Locale"
-#define BUSY_WAITING_USEC 50000
-#define BUSY_WAITING_MAX 40
-
 #define AUL_COMPINFO_LOCALIZED_INFO_START 0
 #define AUL_COMPINFO_START 0
 
@@ -83,43 +81,6 @@ static void __destroy_compinfo_localized_info(gpointer data)
        free(info);
 }
 
-static char *__get_db_path(uid_t uid)
-{
-       char db_path[PATH_MAX];
-       const char *path;
-
-       path = tzplatform_getenv(TZ_SYS_DB);
-       if (!path) {
-               _E("Failed to get TZ_SYS_DB path");
-               return NULL;
-       }
-
-       if (uid == ROOT_UID || uid == GLOBAL_USER) {
-               snprintf(db_path, sizeof(db_path), "%s/.component.db", path);
-       } else {
-               snprintf(db_path, sizeof(db_path), "%s/user/%u/.component.db",
-                               path, uid);
-       }
-
-       return strdup(db_path);
-}
-
-static int __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 AUL_R_ENOMEM;
-               }
-       }
-
-       return AUL_R_OK;
-}
-
 static bool __get_boolean_value(const char *str)
 {
        if (str && !strcmp(str, "true"))
@@ -128,42 +89,28 @@ static bool __get_boolean_value(const char *str)
        return false;
 }
 
-static int __db_busy_handler(void *data, int count)
-{
-       if (count < BUSY_WAITING_MAX) {
-               usleep(BUSY_WAITING_USEC);
-               return 1;
-       }
-
-       LOGE("Database is busy");
-
-       return 0;
-}
-
-static sqlite3 *__open_db(const char *path)
+static sqlite3 *__open_component_db(uid_t uid)
 {
-       sqlite3 *db = NULL;
-       int ret;
+       sqlite3 *db;
+       char *path;
 
-       ret = sqlite3_open_v2(path, &db, SQLITE_OPEN_READONLY, NULL);
-       if (ret != SQLITE_OK) {
-               LOGE("sqlite3_open_v2() is failed. error(%d)", ret);
-               if (db)
-                       sqlite3_close_v2(db);
+       path = aul_db_get_path(COMPONENT_DB, uid);
+       if (!path) {
+               _E("Failed to get db path");
                return NULL;
        }
 
-       ret = sqlite3_busy_handler(db, __db_busy_handler, NULL);
-       if (ret != SQLITE_OK) {
-               LOGE("Failed to register busy handler. error(%s)",
-                               sqlite3_errmsg(db));
-               sqlite3_close_v2(db);
-               return NULL;
-       }
+       db = aul_db_open(path, true);
+       free(path);
 
        return db;
 }
 
+static void __close_component_db(sqlite3 *db)
+{
+       aul_db_close(db);
+}
+
 static void __destroy_compinfo(gpointer data)
 {
        struct aul_compinfo_s *info = (struct aul_compinfo_s *)data;
@@ -211,25 +158,16 @@ static int __create_compinfo(const char *comp_id, uid_t uid,
        sqlite3_stmt *stmt = NULL;
        sqlite3 *db;
        char *query;
-       char *db_path;
        int value_idx;
        int idx;
        struct aul_compinfo_s *info;
        int ret;
 
-       db_path = __get_db_path(uid);
-       if (!db_path) {
-               _E("Failed to get db path");
-               return AUL_R_ENOMEM;
-       }
-
-       db = __open_db(db_path);
+       db = __open_component_db(uid);
        if (!db) {
-               _E("Failed to open db(%s)", db_path);
-               free(db_path);
+               _E("Failed to open component db. uid(%u)", uid);
                return AUL_R_ERROR;
        }
-       free(db_path);
 
        query = sqlite3_mprintf(query_raw, comp_id);
        if (!query) {
@@ -273,7 +211,8 @@ static int __create_compinfo(const char *comp_id, uid_t uid,
 
        for (idx = AUL_COMPINFO_START; idx < AUL_COMPINFO_MAX - 1; idx++) {
                value_idx = (idx < AUL_COMPINFO_COMP_ID) ? idx : idx + 1;
-               ret = __save_column_str(stmt, idx, &info->value[value_idx]);
+               ret = aul_db_save_column_str(stmt, idx,
+                               &info->value[value_idx]);
                if (ret != AUL_R_OK) {
                        __destroy_compinfo(info);
                        goto end;
@@ -286,7 +225,7 @@ static int __create_compinfo(const char *comp_id, uid_t uid,
 end:
        sqlite3_finalize(stmt);
        sqlite3_free(query);
-       sqlite3_close_v2(db);
+       __close_component_db(db);
 
        return ret;
 }
@@ -464,24 +403,15 @@ static int __get_compinfo_list(uid_t uid, GList **list)
                "FROM component_info";
        sqlite3_stmt *stmt = NULL;
        sqlite3 *db;
-       char *db_path;
        int idx;
        struct aul_compinfo_s *info;
        int ret;
 
-       db_path = __get_db_path(uid);
-       if (!db_path) {
-               _E("Failed to get db path");
-               return AUL_R_ENOMEM;
-       }
-
-       db = __open_db(db_path);
+       db = __open_component_db(uid);
        if (!db) {
-               _E("Failed to open db(%s)", db_path);
-               free(db_path);
+               _E("Failed to open component db. uid(%u)", uid);
                return AUL_R_ERROR;
        }
-       free(db_path);
 
        ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
        if (ret != SQLITE_OK) {
@@ -507,7 +437,8 @@ static int __get_compinfo_list(uid_t uid, GList **list)
                }
 
                for (idx = AUL_COMPINFO_START; idx < AUL_COMPINFO_MAX; idx++) {
-                       ret = __save_column_str(stmt, idx, &info->value[idx]);
+                       ret = aul_db_save_column_str(stmt, idx,
+                                       &info->value[idx]);
                        if (ret != AUL_R_OK) {
                                __destroy_compinfo(info);
                                g_list_free_full(*list, __destroy_compinfo);
@@ -526,7 +457,7 @@ static int __get_compinfo_list(uid_t uid, GList **list)
 
 end:
        sqlite3_finalize(stmt);
-       sqlite3_close_v2(db);
+       __close_component_db(db);
 
        return ret;
 }
@@ -676,24 +607,15 @@ static int __get_localized_info_list(const char *comp_id, uid_t uid,
        sqlite3_stmt *stmt = NULL;
        sqlite3 *db;
        char *query;
-       char *db_path;
        int idx;
        struct aul_compinfo_localized_info_s *info;
        int ret;
 
-       db_path = __get_db_path(uid);
-       if (!db_path) {
-               _E("Failed to get db path");
-               return AUL_R_ENOMEM;
-       }
-
-       db = __open_db(db_path);
+       db = __open_component_db(uid);
        if (!db) {
-               _E("Failed to open db(%s)", db_path);
-               free(db_path);
+               _E("Failed to open component db. uid(%u)", uid);
                return AUL_R_ERROR;
        }
-       free(db_path);
 
        query = sqlite3_mprintf(query_raw, comp_id);
        if (!query) {
@@ -729,7 +651,8 @@ static int __get_localized_info_list(const char *comp_id, uid_t uid,
                for (idx = AUL_COMPINFO_LOCALIZED_INFO_START;
                                idx < AUL_COMPINFO_LOCALIZED_INFO_MAX;
                                idx++) {
-                       ret = __save_column_str(stmt, idx, &info->value[idx]);
+                       ret = aul_db_save_column_str(stmt, idx,
+                                       &info->value[idx]);
                        if (ret != AUL_R_OK) {
                                __destroy_compinfo_localized_info(info);
                                g_list_free_full(*list,
@@ -750,7 +673,7 @@ static int __get_localized_info_list(const char *comp_id, uid_t uid,
 end:
        sqlite3_finalize(stmt);
        sqlite3_free(query);
-       sqlite3_close_v2(db);
+       __close_component_db(db);
 
        return ret;
 }