Code cleanup 46/222846/3
authorMinje Ahn <minje.ahn@samsung.com>
Mon, 20 Jan 2020 23:23:52 +0000 (08:23 +0900)
committerMinje ahn <minje.ahn@samsung.com>
Tue, 21 Jan 2020 02:13:31 +0000 (02:13 +0000)
1. Use g_strdup instead.
2. Change trigger naming. ({trigger_name}_{event_table} -> {trigger_name})
   - Fixes due to deleting external storage.
3. Remove unnecessary fields

Change-Id: I12ce305854d59b3a161dadee11fc24fc5c4fe0e2
Signed-off-by: Minje Ahn <minje.ahn@samsung.com>
src/common/media-svc-db-utils.c
src/include/common/media-svc-env.h

index 814cb81..85553bb 100755 (executable)
@@ -38,7 +38,7 @@ static int __media_svc_query_direct(sqlite3 *handle, const char *query, uid_t ui
 static GHashTable *table;
 static GSList *column_list[MEDIA_SVC_DB_LIST_MAX];
 
-int __media_svc_add_table_info(const char *name, const char *trigger_name, const char *event_table, const char *action_table, const char *view_name)
+static int __media_svc_add_table_info(const char *name, const char *trigger_name, const char *event_table, const char *action_table, const char *view_name)
 {
        table_info_s *tbl = NULL;
 
@@ -49,31 +49,17 @@ int __media_svc_add_table_info(const char *name, const char *trigger_name, const
                media_svc_retvm_if(!STRING_VALID(action_table), MS_MEDIA_ERR_INVALID_PARAMETER, "action_table is NULL");
        }
 
-       tbl = malloc(sizeof(table_info_s));
-       if (tbl == NULL) {
-               media_svc_error("MS_MEDIA_ERR_OUT_OF_MEMORY");
-               return MS_MEDIA_ERR_OUT_OF_MEMORY;
-       }
-
-       memset(tbl, 0x00, sizeof(table_info_s));
+       tbl = calloc(1, sizeof(table_info_s));
+       media_svc_retvm_if(!tbl, MS_MEDIA_ERR_OUT_OF_MEMORY, "Failed to allocation");
 
        if (STRING_VALID(trigger_name)) {
-               tbl->trigger_name = malloc(MEDIA_SVC_PATHNAME_SIZE);
-               if (tbl->trigger_name == NULL) {
-                       media_svc_error("MS_MEDIA_ERR_OUT_OF_MEMORY");
-                       SAFE_FREE(tbl);
-                       return MS_MEDIA_ERR_OUT_OF_MEMORY;
-               }
-
-               memset(tbl->trigger_name, 0x00, MEDIA_SVC_PATHNAME_SIZE);
-               snprintf(tbl->trigger_name, MEDIA_SVC_PATHNAME_SIZE, "%s_%s", trigger_name, event_table);
-
-               tbl->event_table = strndup(event_table, strlen(event_table));
-               tbl->action_table = strndup(action_table, strlen(action_table));
+               tbl->trigger_name = g_strdup(trigger_name);
+               tbl->event_table = g_strdup(event_table);
+               tbl->action_table = g_strdup(action_table);
        }
 
        if (STRING_VALID(view_name))
-               tbl->view_name = strndup(view_name, strlen(view_name));
+               tbl->view_name = g_strdup(view_name);
 
        g_hash_table_insert(table, (gpointer)name, (gpointer)tbl);
 
@@ -83,31 +69,19 @@ int __media_svc_add_table_info(const char *name, const char *trigger_name, const
 int __media_svc_add_column_info(GSList **slist, const char *name, const char *type, const char *option, int version, const char *index_name, bool is_unique, bool is_trigger, bool is_view)
 {
        column_info_s *col = NULL;
-       col = malloc(sizeof(column_info_s));
-       if (col == NULL) {
-               media_svc_error("MS_MEDIA_ERR_OUT_OF_MEMORY");
-               return MS_MEDIA_ERR_OUT_OF_MEMORY;
-       }
-       memset(col, 0, sizeof(column_info_s));
+       col = calloc(1, sizeof(column_info_s));
+       media_svc_retvm_if(!col, MS_MEDIA_ERR_OUT_OF_MEMORY, "Failed to allocation");
 
-       col->name = strndup(name, strlen(name));
-       col->type = strndup(type, strlen(type));
-       if (option != NULL) {
-               col->has_option = true;
-               col->option = strndup(option, strlen(option));
-       } else {
-               col->has_option = false;
-       }
+       col->name = g_strdup(name);
+       col->type = g_strdup(type);
+       col->option = g_strdup(option);
        col->version = version;
-       if (index_name != NULL) {
-               col->is_index = true;
-               col->index_name = strndup(index_name, strlen(index_name));
-       } else {
-               col->is_index = false;
-       }
+       col->index_name = g_strdup(index_name);
+
        col->is_unique = is_unique;
        col->is_trigger = is_trigger;
        col->is_view = is_view;
+
        *slist = g_slist_append(*slist, col);
 
        return MS_MEDIA_ERR_NONE;
@@ -286,7 +260,7 @@ int _media_svc_make_table_query(const char *table_name, media_svc_table_slist_e
                        continue;
 
                /*create table */
-               if (col_ptr->has_option) {
+               if (col_ptr->option) {
                        if (table_query->len != 0)
                                g_string_append_printf(table_query, ", %s %s %s", col_ptr->name, col_ptr->type, col_ptr->option);
                        else
@@ -307,7 +281,7 @@ int _media_svc_make_table_query(const char *table_name, media_svc_table_slist_e
                }
 
                /*create index */
-               if (col_ptr->is_index)
+               if (col_ptr->index_name)
                        g_string_append_printf(index_query, MEDIA_SVC_DB_QUERY_INDEX, col_ptr->index_name, table_name, col_ptr->name);
 
                /*create trigger */
@@ -394,7 +368,7 @@ static int __media_svc_upgrade_table_query(sqlite3 *db_handle, const char *table
                if (col_ptr->version > cur_version) {
                        /*alter table */
                        memset(temp, 0, sizeof(temp));
-                       if (col_ptr->has_option)
+                       if (col_ptr->option)
                                snprintf(temp, sizeof(temp), "%s %s %s", col_ptr->name, col_ptr->type, col_ptr->option);
                        else
                                snprintf(temp, sizeof(temp), "%s %s", col_ptr->name, col_ptr->type);
@@ -403,7 +377,7 @@ static int __media_svc_upgrade_table_query(sqlite3 *db_handle, const char *table
                        SQLITE3_SAFE_FREE(sql);
                        media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
                        /*create index */
-                       if (col_ptr->is_index) {
+                       if (col_ptr->index_name) {
                                sql = sqlite3_mprintf(MEDIA_SVC_DB_QUERY_INDEX, col_ptr->index_name, table_name, col_ptr->name);
                                ret = _media_svc_sql_query(sql, uid);
                                SQLITE3_SAFE_FREE(sql);
index 3fc0206..6695157 100755 (executable)
@@ -182,10 +182,8 @@ typedef struct {
 typedef struct {
        char *name;
        char *type;
-       bool has_option;
        char *option;
        int version;
-       bool is_index;
        char *index_name;
        bool is_unique;
        bool is_trigger;