media_util_private.c cleanup 43/309643/3
authorMinje Ahn <minje.ahn@samsung.com>
Mon, 15 Apr 2024 02:55:24 +0000 (11:55 +0900)
committerMinje Ahn <minje.ahn@samsung.com>
Sun, 21 Apr 2024 22:56:38 +0000 (07:56 +0900)
Change-Id: I6227eca030b1b89ea7652ae2c6d144f29cba5051
Signed-off-by: Minje Ahn <minje.ahn@samsung.com>
packaging/capi-content-media-content.spec
src/media_util_private.c

index f7334e4..f389384 100644 (file)
@@ -1,6 +1,6 @@
 Name:       capi-content-media-content
 Summary:    A Media content library in Tizen Native API
-Version:    0.5.1
+Version:    0.5.2
 Release:    0
 Group:      Multimedia/API
 License:    Apache-2.0
index 552a679..e9a54cd 100755 (executable)
@@ -38,7 +38,6 @@ bool _media_util_check_support_media_type(const char *path)
                MEDIA_CONTENT_OTHER_SUPPORT = is_supported;
        }
 
-       /* If not, check media type */
        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");
@@ -50,21 +49,14 @@ bool _media_util_check_support_media_type(const char *path)
 
 int _media_util_check_file_exist(const char *path)
 {
-       int exist;
-
-       /* check the file exits actually */
-       exist = open(path, O_RDONLY);
-       if (exist < 0) {
-               if (errno == EACCES || errno == EPERM) {
-                       content_stderror("open file fail");
-                       return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
-               } else {
-                       content_stderror("open file fail");
-                       return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
-               }
+       int fd = open(path, O_RDONLY);
+       if (fd < 0) {
+               content_retvm_if(errno == EACCES || errno == EPERM, MEDIA_CONTENT_ERROR_PERMISSION_DENIED, "permission denied");
+               content_stderror("open file failed");
+               return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
        }
 
-       close(exist);
+       close(fd);
 
        return MEDIA_CONTENT_ERROR_NONE;
 }
@@ -78,93 +70,59 @@ int _media_util_get_file_time(const char *path)
        return statbuf.st_mtime;
 }
 
-bool _media_util_is_ignorable_file(const char *path)
+static bool __media_util_is_valid_path(const char *path)
 {
        g_autofree gchar *real = NULL;
        g_autofree gchar *org_path = NULL;
 #ifndef _USE_TVPD_MODE
        char replace[MAX_PATH_LEN] = {0, };
 #endif
+       content_retv_if(!STRING_VALID(path), false);
 
-       content_retip_if_fail(STRING_VALID(path));
-
-       /* Check is exist (It may be the path to the deleted file) */
-       content_retvm_if(!g_file_test(path, G_FILE_TEST_EXISTS), false, "removed path");
-
-       /* Check symbolic link file */
-       content_retvm_if(g_file_test(path, G_FILE_TEST_IS_SYMLINK), true, "symbolic link(file)");
-
-       /* Check hidden path */
-       content_retvm_if(strstr(path, "/."), true, "hidden path");
-
-       /* Check symbolic directory */
        real = realpath(path, NULL);
-       /* Get trimmed path */
        org_path = g_canonicalize_filename(path, NULL);
 
-#ifdef _USE_TVPD_MODE
-       if (g_strcmp0(real, org_path) != 0) {
-               content_error("symbolic link(directory)");
-               return true;
-       }
-#else
+#ifndef _USE_TVPD_MODE
        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(replace, MAX_PATH_LEN, "%s%s", tzplatform_getenv(TZ_USER_MEDIASHARED), real + strlen(tzplatform_getenv(TZ_SYS_MEDIASHARED)));
-               if (g_strcmp0(replace, org_path) != 0) {
-                       content_error("symbolic link(directory)");
-                       return true;
-               }
-       } else {
-               if (g_strcmp0(real, org_path) != 0) {
-                       content_error("symbolic link(directory)");
-                       return true;
-               }
+               g_free(real);
+               real = g_strdup(replace);
        }
 #endif
+       content_retvm_if(g_strcmp0(real, org_path) != 0, false, "symbolic link(directory)");
 
-       return false;
+       return true;
 }
 
-static bool __is_scan_ignore_exist(const char *path)
+bool _media_util_is_ignorable_file(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);
+       content_retv_if(!STRING_VALID(path), true);
+       content_retvm_if(!g_file_test(path, G_FILE_TEST_EXISTS), false, "[no error] removed path");
+       content_retvm_if(g_file_test(path, G_FILE_TEST_IS_SYMLINK), true, "[no error] symbolic link(file)");
+       content_retvm_if(strstr(path, "/."), true, "[no error] hidden path");
 
-       if (result)
-               content_error("scan ignore file exist [%s]", ignore_path);
-
-       g_free(ignore_path);
+       return (!__media_util_is_valid_path(path));
+}
 
-       return (bool)result;
+static bool __is_scan_ignore_exist(const char *path)
+{
+       g_autofree gchar *ignore_path = g_build_path(G_DIR_SEPARATOR_S, path, ".scan_ignore", NULL);
+       return (bool)g_file_test(ignore_path, G_FILE_TEST_EXISTS);
 }
 
 bool _media_util_is_ignorable_dir(const char *dir_path)
 {
-       if (!STRING_VALID(dir_path))
-               return true;
+       char *leaf_path = NULL;
+       char search_path[MAX_PATH_LEN] = {0, };
 
-       /*1. Check Hidden Directory*/
+       content_retv_if(!STRING_VALID(dir_path), true);
        content_retvm_if(strstr(dir_path, "/."), true, "hidden path");
-
-       /*2. Check Scan Ignore Directory*/
        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, };
-
        g_strlcpy(search_path, dir_path, sizeof(search_path));
 
        while (strlen(search_path) > 0) {
-               if (__is_scan_ignore_exist(search_path))
-                       return true;
+               content_retvm_if(__is_scan_ignore_exist(search_path), true, "scan ignore file exist");
 
                leaf_path = strrchr(search_path, '/');
                if (!leaf_path)
@@ -178,50 +136,16 @@ bool _media_util_is_ignorable_dir(const char *dir_path)
 
 int _media_content_check_dir(const char *path)
 {
-       DIR *dp = NULL;
-       g_autofree gchar *real = NULL;
-       g_autofree gchar *origin = NULL;
-#ifndef _USE_TVPD_MODE
-       char result_path[MAX_PATH_LEN] = {0, };
-#endif
-       dp = opendir(path);
-       if (dp == NULL) {
-               if (errno == EACCES || errno == EPERM) {
-                       content_stderror("open dir fail");
-                       return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
-               } else {
-                       content_stderror("open dir fail");
-                       return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
-               }
+       DIR *d = opendir(path);
+       if (!d) {
+               content_retvm_if(errno == EACCES, MEDIA_CONTENT_ERROR_PERMISSION_DENIED, "permission denied");
+               content_stderror("opendir failed");
+               return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
        }
 
-       closedir(dp);
-
-       /* Check symbolic link directory */
-       real = realpath(path, NULL);
-       /* Get trimmed path */
-       origin = g_canonicalize_filename(path, NULL);
+       closedir(d);
 
-#ifdef _USE_TVPD_MODE
-       if (g_strcmp0(real, origin) != 0) {
-               content_error("symbolic link(directory)");
-               return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
-       }
-#else
-       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)");
-                       return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
-               }
-       } else {
-               if (g_strcmp0(real, origin) != 0) {
-                       content_error("symbolic link(directory)");
-                       return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
-               }
-       }
-#endif
+       content_retv_if(!__media_util_is_valid_path(path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
 
        return MEDIA_CONTENT_ERROR_NONE;
 }