Folder related functions cleanup 68/227768/2 accepted/tizen/unified/20200320.074815 submit/tizen/20200317.020149 submit/tizen/20200319.005828 submit/tizen/20200320.001353
authorMinje Ahn <minje.ahn@samsung.com>
Mon, 16 Mar 2020 08:19:40 +0000 (17:19 +0900)
committerMinje Ahn <minje.ahn@samsung.com>
Mon, 16 Mar 2020 22:55:25 +0000 (07:55 +0900)
1. Remove duplicate query
2. Use check function instead of get_folder_id()
3. Improve parent folder checking function

Change-Id: Ieb06a829f6890401c3ccf75c775aa2da844bd1d8
Signed-off-by: Minje Ahn <minje.ahn@samsung.com>
include/media-svc.h
plugin/media-content-plugin.c
src/common/media-svc-media-folder.c
src/common/media-svc.c
src/include/common/media-svc-media-folder.h

index 8be0e94..3b685e2 100755 (executable)
@@ -66,7 +66,7 @@ int media_svc_insert_storage(sqlite3 *handle, const char *storage_id, const char
 int media_svc_update_storage(sqlite3 *handle, const char *storage_id, const char *storage_path, uid_t uid);
 int media_svc_set_storage_validity(sqlite3 *handle, const char *storage_id, int validity, uid_t uid);
 int media_svc_get_storage_id(sqlite3 *handle, const char *path, char *storage_id, uid_t uid);
-int media_svc_set_folder_validity(sqlite3 *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid);
+int media_svc_set_folder_validity(const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid);
 
 int media_svc_check_folder_exist_by_path(sqlite3 *handle, const char *storage_id, const char *folder_path);
 
index afd1e93..1a6cbd9 100755 (executable)
@@ -193,7 +193,7 @@ int insert_folder(sqlite3 *handle, const char *storage_id, const char *file_path
 
 int set_folder_validity(sqlite3 *handle, const char *storage_id, const char* start_path, int validity, bool is_recursive, uid_t uid)
 {
-       return media_svc_set_folder_validity(handle, storage_id, start_path, validity, is_recursive, uid);
+       return media_svc_set_folder_validity(storage_id, start_path, validity, is_recursive, uid);
 }
 
 int get_folder_scan_status(sqlite3 *handle, const char *storage_id, const char *path, int *status)
index 2c50926..57fdd98 100755 (executable)
@@ -98,45 +98,36 @@ static int __media_svc_get_and_append_parent_folder(sqlite3 *handle, bool is_dir
        char *dir_path = NULL;
        const char *token = "/";
        char *folder_uuid = NULL;
-       char tmp_folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
-       bool folder_search_end = false;
        char *internal_path = NULL;
 
-       memset(tmp_folder_uuid, 0, sizeof(tmp_folder_uuid));
-       ret = ms_user_get_internal_root_path(uid, &internal_path);
-       media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Fail to get root path");
+       if (storage_type == MS_USER_STORAGE_INTERNAL) {
+               ret = ms_user_get_internal_root_path(uid, &internal_path);
+               media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Fail to get root path");
 
-       if (STRING_VALID(internal_path) && (strncmp(path, internal_path, strlen(internal_path)) == 0))
                next_pos = strlen(internal_path);
-       else if (STRING_VALID(MEDIA_ROOT_PATH_EXTERNAL) && strncmp(path, MEDIA_ROOT_PATH_EXTERNAL, strlen(MEDIA_ROOT_PATH_EXTERNAL)) == 0)
-               next_pos = strlen(MEDIA_ROOT_PATH_EXTERNAL) + 1;
-       else {
-               media_svc_error("Invalid Path");
-               media_svc_sec_error("Invalid Path [%s], internal_path [%s]", path, internal_path);
                SAFE_FREE(internal_path);
-               return MS_MEDIA_ERR_INTERNAL;
+       } else {
+               media_svc_retvm_if(!STRING_VALID(MEDIA_ROOT_PATH_EXTERNAL), MS_MEDIA_ERR_INTERNAL, "Failed to get root path");
+               next_pos = strlen(MEDIA_ROOT_PATH_EXTERNAL) + 1;
        }
 
-       SAFE_FREE(internal_path);
-
-       while (!folder_search_end) {
+       do {
                next = strstr(path + next_pos, token);
-               if (next != NULL) {
+               if (next) {
                        next_pos = (next - path);
-                       dir_path = strndup(path, next_pos);
+                       dir_path = g_strndup(path, next_pos);
                        next_pos++;
                } else {
-                       dir_path = strndup(path, strlen(path));
-                       folder_search_end = true;
-                       media_svc_sec_debug("End Path[%s]", dir_path);
+                       dir_path = g_strdup(path);
                }
 
-               ret = _media_svc_get_folder_id_by_path(handle, storage_id, dir_path, tmp_folder_uuid);
+               ret = _media_svc_check_folder_by_path(handle, storage_id, dir_path);
                if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
+                       SAFE_FREE(folder_uuid);
                        folder_uuid = _media_info_generate_uuid();
                        if (folder_uuid == NULL) {
                                media_svc_error("Invalid UUID");
-                               SAFE_FREE(dir_path);
+                               g_free(dir_path);
                                return MS_MEDIA_ERR_INTERNAL;
                        }
 
@@ -147,8 +138,8 @@ static int __media_svc_get_and_append_parent_folder(sqlite3 *handle, bool is_dir
                                media_svc_sec_debug("Append new folder path[%s] uuid[%s]", dir_path, folder_uuid);
                }
 
-               SAFE_FREE(dir_path);
-       }
+               g_free(dir_path);
+       } while (next);
 
        if (STRING_VALID(folder_uuid)) {
                SAFE_STRLCPY(folder_id, folder_uuid, MEDIA_SVC_UUID_SIZE + 1);
@@ -180,22 +171,18 @@ int _media_svc_get_and_append_folder_id_by_path(sqlite3 *handle, bool is_direct,
 int _media_svc_get_and_append_folder_id_by_folder_path(sqlite3 *handle, const char *storage_id, const char *path, ms_user_storage_type_e storage_type, uid_t uid)
 {
        int ret = MS_MEDIA_ERR_NONE;
-       char *sql = NULL;
        char folder_id[MEDIA_SVC_UUID_SIZE + 1] = {0, };
 
-       ret = _media_svc_get_folder_id_by_path(handle, storage_id, path, folder_id);
-       if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
+       ret = _media_svc_check_folder_by_path(handle, storage_id, path);
+       if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
                ret = __media_svc_get_and_append_parent_folder(handle, true, storage_id, path, storage_type, folder_id, uid);
-       } else {
-               sql = sqlite3_mprintf("UPDATE %q SET validity=1 WHERE storage_uuid = '%q' AND folder_path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
-               ret = _media_svc_sql_query_direct(sql, uid);
-               SQLITE3_SAFE_FREE(sql);
-       }
+       else
+               ret = _media_svc_set_folder_validity(true, storage_id, path, 1, false, uid);
 
        return ret;
 }
 
-int _media_svc_set_folder_validity(sqlite3 *handle, bool is_direct, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
+int _media_svc_set_folder_validity(bool is_direct, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
 {
        int ret = MS_MEDIA_ERR_NONE;
        char *sql = NULL;
index e018f44..5c8ef28 100755 (executable)
@@ -710,9 +710,9 @@ int media_svc_insert_folder(sqlite3 *handle, const char *storage_id, ms_user_sto
        return _media_svc_get_and_append_folder_id_by_folder_path(handle, storage_id, path, storage_type, uid);
 }
 
-int media_svc_set_folder_validity(sqlite3 *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
+int media_svc_set_folder_validity(const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
 {
-       return _media_svc_set_folder_validity(handle, true, storage_id, start_path, validity, is_recursive, uid);
+       return _media_svc_set_folder_validity(true, storage_id, start_path, validity, is_recursive, uid);
 }
 
 int media_svc_check_folder_exist_by_path(sqlite3 *handle, const char *storage_id, const char *folder_path)
index 28007a3..2663f6a 100755 (executable)
@@ -30,7 +30,7 @@ int _media_svc_get_folder_id_by_path(sqlite3 *handle, const char *storage_id, co
 int _media_svc_update_folder_modified_time_by_folder_uuid(const char *folder_uuid, const char *folder_path, uid_t uid);
 int _media_svc_get_and_append_folder_id_by_path(sqlite3 *handle, bool is_direct, const char *storage_id, const char *path, ms_user_storage_type_e storage_type, char *folder_id, uid_t uid);
 int _media_svc_get_and_append_folder_id_by_folder_path(sqlite3 *handle, const char *storage_id, const char *path, ms_user_storage_type_e storage_type, uid_t uid);
-int _media_svc_set_folder_validity(sqlite3 *handle, bool is_direct, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid);
+int _media_svc_set_folder_validity(bool is_direct, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid);
 int _media_svc_check_folder_by_path(sqlite3 *handle, const char *storage_id, const char *path);
 
 #endif /*_MEDIA_SVC_MEDIA_FOLDER_H_*/