Improve readability of media_bookmark
[platform/core/api/media-content.git] / src / media_util_private.c
index 46c4d07..ef337ad 100755 (executable)
@@ -83,29 +83,30 @@ int _media_util_check_file_exist(const char *path)
 
 void _media_util_trim_path(const char *input_path, char **output_path)
 {
-       char buf[4096] = {0,};
-       char tmp[4096] = {0,};
-       char *pos = NULL;
-
-       memset(buf, 0, sizeof(buf));
-       if (!SAFE_STRLCPY(buf, input_path, sizeof(buf)))
-               media_content_sec_debug("Truncation occurred[%zu]", strlen(input_path));
-
-       while ((pos = strstr(buf, "//")) != NULL) {
-               memset(tmp, 0, sizeof(tmp));
-               if (!SAFE_STRLCPY(tmp, buf, pos - buf + 1))
-                       media_content_sec_debug("Truncation occurred");
-               SAFE_STRLCAT(tmp, pos + 1, sizeof(tmp));
-
-               memset(buf, 0, sizeof(buf));
-               if (!SAFE_STRLCPY(buf, tmp, sizeof(buf)))
-                       media_content_sec_debug("Truncation occurred[%zu]", strlen(tmp));
-       }
-
-       if (g_str_has_suffix(buf, "/"))
-               *output_path = g_strndup(buf, strlen(buf) - 1);
-       else
-               *output_path = g_strdup(buf);
+       gchar **name_list = NULL;
+       gchar *tmp_path = NULL;
+
+       if (!STRING_VALID(input_path) || output_path == NULL)
+               return;
+
+       /* Workflow example
+               Input : /a/b//c/
+               After g_strsplit() : {'','a','b','','c',''}
+               After g_build_pathv() : a/b/c
+               After g_strdup_printf() : /a/b/c
+       */
+       name_list = g_strsplit(input_path, "/", -1);
+       if (!name_list)
+               return;
+
+       tmp_path = g_build_pathv(G_DIR_SEPARATOR_S, name_list);
+       g_strfreev(name_list);
+       if (!tmp_path)
+               return;
+
+       /* g_build_pathv does not add root '/' */
+       *output_path = g_strdup_printf("/%s", tmp_path);
+       g_free(tmp_path);
 }
 
 
@@ -201,24 +202,38 @@ int _media_util_check_ignore_file(const char *path, bool *ignore)
        return MEDIA_CONTENT_ERROR_NONE;
 }
 
+static bool __is_scan_ignore_exist(const char *path)
+{
+       const char *scan_ignore = ".scan_ignore";
+       char *ignore_path = NULL;
+       gboolean result = FALSE;
+
+       if (!STRING_VALID(path))
+               return false;
+
+       ignore_path = g_build_path(G_DIR_SEPARATOR_S, path, scan_ignore, NULL);
+       result = g_file_test(ignore_path, G_FILE_TEST_EXISTS);
+
+       if (result)
+               media_content_error("scan ignore file exist [%s]", ignore_path);
+
+       SAFE_FREE(ignore_path);
+
+       return (bool)result;
+}
+
 int _media_util_check_ignore_dir(const char *dir_path, bool *ignore)
 {
        int ret = MEDIA_CONTENT_ERROR_NONE;
        ms_user_storage_type_e storage_type = MS_USER_STORAGE_INTERNAL;
-       const char *scan_ignore = ".scan_ignore";
-       bool find = false;
-       GDir *dir = NULL;
-       GError *error = NULL;
-       const char *name;
-
-       media_content_sec_debug("dir_path : %s", dir_path);
 
        media_content_retvm_if(!STRING_VALID(dir_path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid dir_path");
+       media_content_sec_debug("dir_path : %s", dir_path);
 
-       *ignore = FALSE;
+       *ignore = false;
        /*1. Check Hidden Directory*/
        if (strstr(dir_path, "/.") != NULL) {
-               *ignore = TRUE;
+               *ignore = true;
                media_content_error("hidden path");
                return MEDIA_CONTENT_ERROR_NONE;
        }
@@ -234,77 +249,17 @@ int _media_util_check_ignore_dir(const char *dir_path, bool *ignore)
        char search_path[MAX_PATH_LEN] = {0, };
 
        memset(search_path, 0, sizeof(search_path));
-       if (!SAFE_STRLCPY(search_path, dir_path, sizeof(search_path))) {
-               media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
-               return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
-       }
+       SAFE_STRLCPY(search_path, dir_path, sizeof(search_path));
 
        while (STRING_VALID(search_path)) {
-               dir = g_dir_open(search_path, 0, &error);
-               if (dir != NULL && error == NULL) {
-                       while ((name = g_dir_read_name(dir))) {
-                               if (g_strcmp0(name, scan_ignore) == 0) {
-                                       media_content_sec_debug("Ignore path[%s]", search_path);
-                                       find = TRUE;
-                                       break;
-                               }
-                       }
-               } else {
-                       *ignore = TRUE;
-                       media_content_error("Open Directory fail");
-                       if (error->code == G_FILE_ERROR_ACCES) {
-                               g_error_free(error);
-                               return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
-                       } else {
-                               g_error_free(error);
-                               return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
-                       }
-               }
-
-               if (dir)
-                       g_dir_close(dir);
+               if ((*ignore = __is_scan_ignore_exist(search_path)))
+                       break;
 
-               if (find) {
-                       *ignore = TRUE;
+               leaf_path = strrchr(search_path, '/');
+               if (!leaf_path)
                        break;
-               } else {
-                       /*If root path, Stop Scanning*/
-                       if ((storage_type == MS_USER_STORAGE_INTERNAL) && (STRING_VALID(MEDIA_ROOT_PATH_INTERNAL) && g_strcmp0(search_path, MEDIA_ROOT_PATH_INTERNAL) == 0)) {
-                               break;
-                       } else if ((storage_type == MS_USER_STORAGE_EXTERNAL) && (STRING_VALID(MEDIA_ROOT_PATH_SDCARD)) && (g_strcmp0(search_path, MEDIA_ROOT_PATH_SDCARD) == 0)) {
-                               break;
-                       } else if ((storage_type == MS_USER_STORAGE_EXTERNAL_USB) && (STRING_VALID(MEDIA_ROOT_PATH_DISC)) && (g_strcmp0(search_path, MEDIA_ROOT_PATH_DISC) == 0)) {
-                               break;
-                       } else if (storage_type == MS_USER_STORAGE_EXTERNAL_USB) {
-                               char *parent_folder_path = NULL;
-                               bool is_root = FALSE;
-
-                               parent_folder_path = g_path_get_dirname(search_path);
-                               if (STRING_VALID(MEDIA_ROOT_PATH_USB) && STRING_VALID(parent_folder_path) && (g_strcmp0(parent_folder_path, MEDIA_ROOT_PATH_USB) == 0))
-                                       is_root = TRUE;
-
-                               SAFE_FREE(parent_folder_path);
-
-                               if (is_root == TRUE)
-                                       break;
-                       }
-#ifdef _USE_SENIOR_MODE
-                       if (_media_content_is_support_senior_mode()) {
-                               if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL) && (g_strcmp0(search_path, MEDIA_ROOT_PATH_SENIOR_MODE) == 0))
-                                       break;
-                       }
-#endif
 
-                       leaf_path = strrchr(search_path, '/');
-                       if (leaf_path != NULL) {
-                               int seek_len = leaf_path -search_path;
-                               search_path[seek_len] = '\0';
-                               /*media_content_sec_debug("go to other dir [%s]", search_path);*/
-                       } else {
-                               media_content_debug("Fail to find leaf path");
-                               break;
-                       }
-               }
+               search_path[leaf_path - search_path] = '\0';
        }
 
        return MEDIA_CONTENT_ERROR_NONE;
@@ -375,111 +330,37 @@ int _media_content_check_dir(const char *path)
 }
 
 
-int _media_content_replace_path_in_condition(const char *condition, char *replace_condition, bool replace)
+/* FIXME : If there are no issue reports related to this, it will be deleted in tizen 6.5 or after. */
+char * _media_content_replace_path_in_condition(const char *condition)
 {
-       int ret = MEDIA_CONTENT_ERROR_NONE;
+       return g_strdup(condition);
+#if 0
+       char **split_list = NULL;
+       char *result = NULL;
 
-#ifdef _USE_TVPD_MODE
-       snprintf(replace_condition, MAX_QUERY_SIZE, "%s", condition);
-#else
-       char old_condition[MAX_QUERY_SIZE] = {0, };
-       char new_condition[MAX_QUERY_SIZE] = {0, };
-       char *find = NULL;
-       unsigned int str_len = 0;
-
-       char *find_str = NULL;
-       char *to_replace_str = NULL;
-
-       if (replace == TRUE) {  //change User session path to System session path
-               find_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL_OLD);
-               if (!STRING_VALID(find_str)) {
-                       media_content_error("strdup failed");
-                       ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
-                       goto ERROR;
-               }
-
-               to_replace_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL);
-               if (!STRING_VALID(to_replace_str)) {
-                       media_content_error("Get TZ_USER_CONTENT failed");
-                       ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
-                       goto ERROR;
-               }
-       } else {
-               find_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL);
-               if (!STRING_VALID(find_str)) {
-                       media_content_error("Get TZ_USER_CONTENT failed");
-                       ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
-                       goto ERROR;
-               }
-
-               to_replace_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL_OLD);
-               if (!STRING_VALID(to_replace_str)) {
-                       media_content_error("strdup failed");
-                       ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
-                       goto ERROR;
-               }
-       }
-
-       memset(old_condition, 0, sizeof(old_condition));
-       memset(new_condition, 0, sizeof(new_condition));
+       if (!STRING_VALID(MEDIA_ROOT_PATH_INTERNAL_OLD) || !STRING_VALID(MEDIA_ROOT_PATH_INTERNAL))
+               return NULL;
 
        media_content_sec_debug("Old condition[%s]", condition);
 
-       if (!SAFE_STRLCPY(new_condition, condition, sizeof(new_condition))) {
-               media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
-               ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
-               goto ERROR;
-       }
-
-       if (g_strcmp0(find_str, to_replace_str))
-               find = strstr(new_condition, find_str);
-
-       while (find != NULL) {
-               str_len = find - new_condition;
-
-               memset(old_condition, 0, sizeof(old_condition));
-               if (!SAFE_STRLCPY(old_condition, new_condition, sizeof(old_condition))) {
-                       media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
-                       ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
-                       goto ERROR;
-               }
-               memset(new_condition, 0, sizeof(new_condition));
-
-               snprintf(new_condition, str_len + 1, "%s", old_condition);
-
-               SAFE_STRLCAT(new_condition, to_replace_str, sizeof(new_condition));
-               SAFE_STRLCAT(new_condition, old_condition + str_len + strlen(find_str), sizeof(new_condition));
-
-               find = strstr(new_condition, find_str);
-       }
-
-       if (!SAFE_STRLCPY(replace_condition, new_condition, MAX_QUERY_SIZE)) {
-               media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
-               ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
-               goto ERROR;
-       }
+       split_list = g_strsplit(condition, MEDIA_ROOT_PATH_INTERNAL_OLD, -1);
+       if (!split_list)
+               return NULL;
 
-       media_content_sec_debug("repl cond[%s]", replace_condition);
+       result = g_strjoinv(MEDIA_ROOT_PATH_INTERNAL, split_list);
+       g_strfreev(split_list);
 
-       if (!STRING_VALID(replace_condition)) {
-               media_content_error("replace failed");
-               ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
-               goto ERROR;
-       }
-
-ERROR:
-       SAFE_FREE(find_str);
-       SAFE_FREE(to_replace_str);
+       return result;
 #endif
-
-       return ret;
 }
 
+/* FIXME : If there are no issue reports related to this, it will be deleted in Tizen 6.5 or after. */
 int _media_content_replace_path(const char *path, char *replace_path)
 {
-#ifdef _USE_TVPD_MODE
+       media_content_retvm_if(!STRING_VALID(path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid path");
+
        snprintf(replace_path, MAX_PATH_LEN, "%s", path);
-#else
+#if 0
        if (strncmp(path, MEDIA_ROOT_PATH_INTERNAL_OLD, strlen(MEDIA_ROOT_PATH_INTERNAL_OLD)) == 0) {
                media_content_sec_debug("Old path[%s]", path);
                snprintf(replace_path, MAX_PATH_LEN, "%s%s", MEDIA_ROOT_PATH_INTERNAL, path + strlen(MEDIA_ROOT_PATH_INTERNAL_OLD));
@@ -488,11 +369,6 @@ int _media_content_replace_path(const char *path, char *replace_path)
        }
 #endif
 
-       if (!STRING_VALID(replace_path)) {
-               media_content_error("replace failed");
-               return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
-       }
-
        return MEDIA_CONTENT_ERROR_NONE;
 }