Fix format error in logs
[platform/core/api/media-content.git] / src / media_util_private.c
index d963500..1415b4f 100755 (executable)
@@ -20,6 +20,7 @@
 #include <media_info_private.h>
 #include <storage.h>
 #include <system_info.h>
+
 #ifdef _USE_SENIOR_MODE
 #include <media_util_private.h>
 #endif
@@ -76,6 +77,33 @@ int _media_util_check_file_exist(const char *path)
        return MEDIA_CONTENT_ERROR_NONE;
 }
 
+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);
+}
+
 int _media_util_check_ignore_file(const char *path, bool *ignore)
 {
        media_content_retvm_if(!STRING_VALID(path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid path");
@@ -112,11 +140,8 @@ int _media_util_check_ignore_file(const char *path, bool *ignore)
 
        /* Check symbolic directory */
        tmp_path = realpath(path, NULL);
-       /* remove if suffix is '/' */
-       if (g_str_has_suffix(path, "/"))
-               org_path = g_strndup(path, strlen(path) - 1);
-       else
-               org_path = g_strdup(path);
+       /* Get trimmed path */
+       _media_util_trim_path(path, &org_path);
 
 #ifdef _USE_TVPD_MODE
        if (g_strcmp0(tmp_path, org_path) != 0) {
@@ -159,7 +184,7 @@ int _media_util_check_ignore_file(const char *path, bool *ignore)
 int _media_util_check_ignore_dir(const char *dir_path, bool *ignore)
 {
        int ret = MEDIA_CONTENT_ERROR_NONE;
-       media_svc_storage_type_e storage_type = 0;
+       ms_user_storage_type_e storage_type = MS_USER_STORAGE_INTERNAL;
        const char *scan_ignore = ".scan_ignore";
        bool find = false;
        GDir *dir = NULL;
@@ -179,9 +204,9 @@ int _media_util_check_ignore_dir(const char *dir_path, bool *ignore)
        }
 
        /*2. Check Scan Ignore Directory*/
-       ret = media_svc_get_storage_type(dir_path, &storage_type, _content_get_uid());
+       ret = ms_user_get_storage_type(_content_get_uid(), dir_path, &storage_type);
        if (ret != MS_MEDIA_ERR_NONE) {
-               media_content_error("media_svc_get_storage_type failed : %d", ret);
+               media_content_error("ms_user_get_storage_type failed : %d", ret);
                return _content_error_capi(MEDIA_CONTENT_TYPE, ret);
        }
 
@@ -207,10 +232,13 @@ int _media_util_check_ignore_dir(const char *dir_path, bool *ignore)
                } else {
                        *ignore = TRUE;
                        media_content_error("Open Directory fail");
-                       if (error->code == G_FILE_ERROR_ACCES)
+                       if (error->code == G_FILE_ERROR_ACCES) {
+                               g_error_free(error);
                                return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
-                       else
+                       } else {
+                               g_error_free(error);
                                return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
+                       }
                }
 
                if (dir)
@@ -221,13 +249,13 @@ int _media_util_check_ignore_dir(const char *dir_path, bool *ignore)
                        break;
                } else {
                        /*If root path, Stop Scanning*/
-                       if ((storage_type == MEDIA_SVC_STORAGE_INTERNAL) && (STRING_VALID(MEDIA_ROOT_PATH_INTERNAL) && g_strcmp0(search_path, MEDIA_ROOT_PATH_INTERNAL) == 0)) {
+                       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 == MEDIA_SVC_STORAGE_EXTERNAL) && (STRING_VALID(MEDIA_ROOT_PATH_SDCARD)) && (g_strcmp0(search_path, MEDIA_ROOT_PATH_SDCARD) == 0)) {
+                       } 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 == MEDIA_SVC_STORAGE_EXTERNAL_USB) && (STRING_VALID(MEDIA_ROOT_PATH_DISC)) && (g_strcmp0(search_path, MEDIA_ROOT_PATH_DISC) == 0)) {
+                       } 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 == MEDIA_SVC_STORAGE_EXTERNAL_USB) {
+                       } else if (storage_type == MS_USER_STORAGE_EXTERNAL_USB) {
                                char *parent_folder_path = NULL;
                                bool is_root = FALSE;
 
@@ -262,6 +290,69 @@ int _media_util_check_ignore_dir(const char *dir_path, bool *ignore)
        return MEDIA_CONTENT_ERROR_NONE;
 }
 
+int _media_content_check_dir(const char *path)
+{
+       DIR *dp = NULL;
+       char *real = NULL;
+       char *origin = NULL;
+#ifndef _USE_TVPD_MODE
+       char result_path[MAX_PATH_LEN] = {0, };
+#endif
+       dp = opendir(path);
+       if (dp == NULL) {
+               media_content_sec_error("path [%s]", path);
+               media_content_stderror("open dir fail");
+
+               if (errno == EACCES || errno == EPERM)
+                       return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
+               else
+                       return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
+       }
+
+       closedir(dp);
+
+       /* Check symbolic link directory */
+       real = realpath(path, NULL);
+       /* Get trimmed path */
+       _media_util_trim_path(path, &origin);
+
+#ifdef _USE_TVPD_MODE
+       if (g_strcmp0(real, origin) != 0) {
+               media_content_error("symbolic link(directory)");
+               media_content_sec_debug("path[%s] real[%s]", origin, real);
+               SAFE_FREE(real);
+               SAFE_FREE(origin);
+               return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
+       }
+#else
+       if (g_str_has_prefix(real, MEDIA_SHARE_PATH)) {
+               /* If shared dirctory, it should be change path to TZ_USER_SHARE from realpath */
+               snprintf(result_path, MAX_PATH_LEN, "%s%s", tzplatform_getenv(TZ_USER_MEDIASHARED), real + strlen(MEDIA_SHARE_PATH));
+               if (g_strcmp0(result_path, origin) != 0) {
+                       media_content_error("symbolic link(directory)");
+                       media_content_sec_debug("path[%s] real[%s]", origin, real);
+                       SAFE_FREE(real);
+                       SAFE_FREE(origin);
+                       return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
+               }
+       } else {
+               if (g_strcmp0(real, origin) != 0) {
+                       media_content_error("symbolic link(directory)");
+                       media_content_sec_debug("path[%s] real[%s]", origin, real);
+                       SAFE_FREE(real);
+                       SAFE_FREE(origin);
+                       return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
+               }
+       }
+#endif
+
+       SAFE_FREE(real);
+       SAFE_FREE(origin);
+
+       return MEDIA_CONTENT_ERROR_NONE;
+}
+
+
 int _media_content_replace_path_in_condition(const char *condition, char *replace_condition, bool replace)
 {
        int ret = MEDIA_CONTENT_ERROR_NONE;
@@ -278,9 +369,9 @@ int _media_content_replace_path_in_condition(const char *condition, char *replac
        char *to_replace_str = NULL;
 
        if (replace == TRUE) {  //change User session path to System session path
-               ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &find_str);
-               if (ret != STORAGE_ERROR_NONE) {
-                       media_content_error("storage_get_directory failed");
+               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;
                }
@@ -299,9 +390,9 @@ int _media_content_replace_path_in_condition(const char *condition, char *replac
                        goto ERROR;
                }
 
-               ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &to_replace_str);
-               if (ret != STORAGE_ERROR_NONE) {
-                       media_content_error("storage_get_directory failed");
+               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;
                }
@@ -367,57 +458,12 @@ int _media_content_replace_path(const char *path, char *replace_path)
 #ifdef _USE_TVPD_MODE
        snprintf(replace_path, MAX_PATH_LEN, "%s", path);
 #else
-
-       int ret = MEDIA_CONTENT_ERROR_NONE;
-       char *old_internal_path =  NULL;
-
-       ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &old_internal_path);
-       if (ret != STORAGE_ERROR_NONE) {
-               media_content_error("storage_get_directory failed");
-               return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
-       }
-
-       if (strncmp(path, old_internal_path, strlen(old_internal_path)) == 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(old_internal_path));
-       } else {
-               snprintf(replace_path, MAX_PATH_LEN, "%s", path);
-       }
-
-       SAFE_FREE(old_internal_path);
-#endif
-
-       if (!STRING_VALID(replace_path)) {
-               media_content_error("replace failed");
-               return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
-       }
-
-       return MEDIA_CONTENT_ERROR_NONE;
-}
-
-int _media_content_rollback_path(const char *path, char *replace_path)
-{
-#ifdef _USE_TVPD_MODE
-               snprintf(replace_path, MAX_PATH_LEN, "%s", path);
-#else
-
-       int ret = MEDIA_CONTENT_ERROR_NONE;
-       char *old_internal_path =  NULL;
-
-       ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &old_internal_path);
-       if (ret != STORAGE_ERROR_NONE) {
-               media_content_error("storage_get_directory failed");
-               return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
-       }
-
-       if (strncmp(path, MEDIA_ROOT_PATH_INTERNAL, strlen(MEDIA_ROOT_PATH_INTERNAL)) == 0) {
-               media_content_sec_debug("new path[%s]", path);
-               snprintf(replace_path, MAX_PATH_LEN, "%s%s", old_internal_path, path + strlen(MEDIA_ROOT_PATH_INTERNAL));
+               snprintf(replace_path, MAX_PATH_LEN, "%s%s", MEDIA_ROOT_PATH_INTERNAL, path + strlen(MEDIA_ROOT_PATH_INTERNAL_OLD));
        } else {
                snprintf(replace_path, MAX_PATH_LEN, "%s", path);
        }
-
-       SAFE_FREE(old_internal_path);
 #endif
 
        if (!STRING_VALID(replace_path)) {