Reinforce line coverage
[platform/core/api/media-content.git] / src / media_util_private.c
index a0b6233..3ccf413 100755 (executable)
 #include <dirent.h>
 #include <fcntl.h>
 #include <media_info_private.h>
-#include <storage.h>
 #include <system_info.h>
 #include <sys/stat.h>
 
-#ifdef _USE_SENIOR_MODE
-#include <media_util_private.h>
-#endif
-
 static int MEDIA_CONTENT_OTHER_SUPPORT = -1;
 
 bool _media_util_check_support_media_type(const char *path)
@@ -38,10 +33,7 @@ bool _media_util_check_support_media_type(const char *path)
 
        if (MEDIA_CONTENT_OTHER_SUPPORT == -1) {
                ret = system_info_get_platform_bool("http://tizen.org/feature/content.scanning.others", &is_supported);
-               if (ret != SYSTEM_INFO_ERROR_NONE) {
-                       content_debug("SYSTEM_INFO_ERROR: content.scanning.others [%d]", ret);
-                       return false;
-               }
+               content_retvm_if(ret != SYSTEM_INFO_ERROR_NONE, false, "SYSTEM_INFO_ERROR: content.scanning.others [%d]", ret);
 
                MEDIA_CONTENT_OTHER_SUPPORT = is_supported;
        }
@@ -50,9 +42,7 @@ bool _media_util_check_support_media_type(const char *path)
        if (!MEDIA_CONTENT_OTHER_SUPPORT) {
                ret = media_svc_get_media_type(path, &media_type);
                content_retvm_if(ret != MS_MEDIA_ERR_NONE, false, "Failed to get media type");
-
-               if (media_type == MEDIA_CONTENT_TYPE_OTHERS)
-                       return false;
+               content_retv_if(media_type == MEDIA_CONTENT_TYPE_OTHERS, false);
        }
 
        return true;
@@ -67,11 +57,9 @@ int _media_util_check_file_exist(const char *path)
        if (exist < 0) {
                if (errno == EACCES || errno == EPERM) {
                        content_stderror("open file fail");
-                       content_sec_debug("path [%s]", path);
                        return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
                } else {
                        content_stderror("open file fail");
-                       content_sec_debug("path [%s]", path);
                        return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
                }
        }
@@ -81,117 +69,64 @@ 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)
-{
-       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);
-}
-
-
 int _media_util_get_file_time(const char *path)
 {
-       struct stat statbuf;
-       int ret = 0;
-
-       memset(&statbuf, 0, sizeof(struct stat));
-       ret = stat(path, &statbuf);
-       if (ret == -1) {
-               content_stderror("stat failed");
-               return ret;
-       }
+       struct stat statbuf = { 0, };
+
+       content_retvm_if(stat(path, &statbuf) == -1, 0, "stat failed");
 
        return statbuf.st_mtime;
 }
 
 bool _media_util_is_ignorable_file(const char *path)
 {
-       content_retvm_if(!STRING_VALID(path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid path");
-
        char *tmp_path = NULL;
-       char *org_path = NULL;
+       g_autofree gchar *org_path = NULL;
 
 #ifndef _USE_TVPD_MODE
        char replace[MAX_PATH_LEN] = {0, };
 #endif
 
+       content_retip_if_fail(STRING_VALID(path));
+
        /* Check is exist (It may be the path to the deleted file) */
-       if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
-               content_sec_debug("removed path[%s]", path);
-               return false;
-       }
+       content_retvm_if(!g_file_test(path, G_FILE_TEST_EXISTS), false, "removed path");
 
        /* Check symbolic link file */
-       if (g_file_test(path, G_FILE_TEST_IS_SYMLINK)) {
-               content_error("symbolic link(file)");
-               content_sec_debug("path : %s", path);
-               return true;
-       }
+       content_retvm_if(g_file_test(path, G_FILE_TEST_IS_SYMLINK), true, "symbolic link(file)");
 
        /* Check hidden path */
-       if (strstr(path, "/.") != NULL) {
-               content_error("hidden path");
-               content_sec_debug("path : %s", path);
-               return true;
-       }
+       content_retvm_if(strstr(path, "/."), true, "hidden path");
 
        /* Check symbolic directory */
        tmp_path = realpath(path, NULL);
        /* Get trimmed path */
-       _media_util_trim_path(path, &org_path);
+       org_path = g_canonicalize_filename(path, NULL);
 
 #ifdef _USE_TVPD_MODE
        if (g_strcmp0(tmp_path, org_path) != 0) {
                content_error("symbolic link(directory)");
-               content_sec_debug("path[%s] real[%s]", org_path, tmp_path);
                SAFE_FREE(tmp_path);
-               g_free(org_path);
                return true;
        }
 #else
-       if (g_str_has_prefix(tmp_path, MEDIA_SHARE_PATH)) {
-               /* If shared dirctory, it should be change path to TZ_USER_SHARE from realpath */
-               snprintf(replace, MAX_PATH_LEN, "%s%s", tzplatform_getenv(TZ_USER_MEDIASHARED), tmp_path + strlen(MEDIA_SHARE_PATH));
+       if (g_str_has_prefix(tmp_path, tzplatform_getenv(TZ_SYS_MEDIASHARED))) {
+               /* If shared directory, it should be change path to TZ_USER_SHARE from realpath */
+               snprintf(replace, MAX_PATH_LEN, "%s%s", tzplatform_getenv(TZ_USER_MEDIASHARED), tmp_path + strlen(tzplatform_getenv(TZ_SYS_MEDIASHARED)));
                if (g_strcmp0(replace, org_path) != 0) {
                        content_error("symbolic link(directory)");
-                       content_sec_debug("path[%s] real[%s]", org_path, tmp_path);
                        SAFE_FREE(tmp_path);
-                       g_free(org_path);
                        return true;
                }
        } else {
                if (g_strcmp0(tmp_path, org_path) != 0) {
                        content_error("symbolic link(directory)");
-                       content_sec_debug("path[%s] real[%s]", org_path, tmp_path);
                        SAFE_FREE(tmp_path);
-                       g_free(org_path);
                        return true;
                }
        }
 #endif
        SAFE_FREE(tmp_path);
-       g_free(org_path);
 
        return false;
 }
@@ -218,24 +153,14 @@ static bool __is_scan_ignore_exist(const char *path)
 
 bool _media_util_is_ignorable_dir(const char *dir_path)
 {
-       int ret = MEDIA_CONTENT_ERROR_NONE;
-       ms_user_storage_type_e storage_type = MS_USER_STORAGE_INTERNAL;
-
-       content_retvm_if(!STRING_VALID(dir_path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid dir_path");
-       content_sec_debug("dir_path : %s", dir_path);
+       if (!STRING_VALID(dir_path))
+               return true;
 
        /*1. Check Hidden Directory*/
-       if (strstr(dir_path, "/.") != NULL) {
-               content_error("hidden path");
-               return true;
-       }
+       content_retvm_if(strstr(dir_path, "/."), true, "hidden path");
 
        /*2. Check Scan Ignore Directory*/
-       ret = ms_user_get_storage_type(_content_get_uid(), dir_path, &storage_type);
-       if (ret != MS_MEDIA_ERR_NONE) {
-               content_error("ms_user_get_storage_type failed : %d", ret);
-               return false;
-       }
+       content_retvm_if(!ms_user_is_valid_path(_content_get_uid(), dir_path), true, "ms_user_is_valid_path failed");
 
        char *leaf_path = NULL;
        char search_path[MAX_PATH_LEN] = {0, };
@@ -260,7 +185,7 @@ int _media_content_check_dir(const char *path)
 {
        DIR *dp = NULL;
        char *real = NULL;
-       char *origin = NULL;
+       g_autofree gchar *origin = NULL;
 #ifndef _USE_TVPD_MODE
        char result_path[MAX_PATH_LEN] = {0, };
 #endif
@@ -268,11 +193,9 @@ int _media_content_check_dir(const char *path)
        if (dp == NULL) {
                if (errno == EACCES || errno == EPERM) {
                        content_stderror("open dir fail");
-                       content_sec_error("path [%s]", path);
                        return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
                } else {
                        content_stderror("open dir fail");
-                       content_sec_error("path [%s]", path);
                        return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
                }
        }
@@ -282,98 +205,33 @@ int _media_content_check_dir(const char *path)
        /* Check symbolic link directory */
        real = realpath(path, NULL);
        /* Get trimmed path */
-       _media_util_trim_path(path, &origin);
+       origin = g_canonicalize_filename(path, NULL);
 
 #ifdef _USE_TVPD_MODE
        if (g_strcmp0(real, origin) != 0) {
                content_error("symbolic link(directory)");
-               content_sec_debug("path[%s] real[%s]", origin, real);
                SAFE_FREE(real);
-               g_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_str_has_prefix(real, tzplatform_getenv(TZ_SYS_MEDIASHARED))) {
+               /* If shared directory, 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(tzplatform_getenv(TZ_SYS_MEDIASHARED)));
                if (g_strcmp0(result_path, origin) != 0) {
                        content_error("symbolic link(directory)");
-                       content_sec_debug("path[%s] real[%s]", origin, real);
                        SAFE_FREE(real);
-                       g_free(origin);
                        return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
                }
        } else {
                if (g_strcmp0(real, origin) != 0) {
                        content_error("symbolic link(directory)");
-                       content_sec_debug("path[%s] real[%s]", origin, real);
                        SAFE_FREE(real);
-                       g_free(origin);
                        return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
                }
        }
 #endif
 
        SAFE_FREE(real);
-       g_free(origin);
 
        return MEDIA_CONTENT_ERROR_NONE;
 }
-
-
-/* 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)
-{
-       return g_strdup(condition);
-#if 0
-       char **split_list = NULL;
-       char *result = NULL;
-
-       if (!STRING_VALID(MEDIA_ROOT_PATH_INTERNAL_OLD) || !STRING_VALID(MEDIA_ROOT_PATH_INTERNAL))
-               return NULL;
-
-       content_sec_debug("Old condition[%s]", condition);
-
-       split_list = g_strsplit(condition, MEDIA_ROOT_PATH_INTERNAL_OLD, -1);
-       if (!split_list)
-               return NULL;
-
-       result = g_strjoinv(MEDIA_ROOT_PATH_INTERNAL, split_list);
-       g_strfreev(split_list);
-
-       return result;
-#endif
-}
-
-/* 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)
-{
-       content_retvm_if(!STRING_VALID(path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid path");
-
-       snprintf(replace_path, MAX_PATH_LEN, "%s", path);
-#if 0
-       if (strncmp(path, MEDIA_ROOT_PATH_INTERNAL_OLD, strlen(MEDIA_ROOT_PATH_INTERNAL_OLD)) == 0) {
-               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));
-       } else {
-               snprintf(replace_path, MAX_PATH_LEN, "%s", path);
-       }
-#endif
-
-       return MEDIA_CONTENT_ERROR_NONE;
-}
-
-#ifdef _USE_SENIOR_MODE
-bool _media_content_is_support_senior_mode()
-{
-       bool bSupportSeniorMode = false;
-
-       if (system_info_get_value_bool(SYSTEM_INFO_KEY_GET_SENIOR_MODE_SUPPORTED, &bSupportSeniorMode) != SYSTEM_INFO_ERROR_NONE) {
-               content_debug("Get senior mode support failed");
-               return false;
-       }
-       /* content_debug("Senior mode Support : [%d]", bSupportSeniorMode); */
-       return bSupportSeniorMode;
-}
-#endif
-