X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=src%2Fmedia_util_private.c;h=2e1bfdaed0b8e8487671b401eae1707f921902eb;hb=refs%2Fchanges%2F45%2F229145%2F3;hp=db15289ec9079c9dfde77ac5e6aacb94e4f89e9c;hpb=86eb0f96b650c93411db7cceb498ebc071748167;p=platform%2Fcore%2Fapi%2Fmedia-content.git diff --git a/src/media_util_private.c b/src/media_util_private.c index db15289..2e1bfda 100755 --- a/src/media_util_private.c +++ b/src/media_util_private.c @@ -14,19 +14,50 @@ * limitations under the License. */ + #include -#include -#include -#include #include -#include #include -#include #include -#ifdef _USE_TV_PROFILE #include +#include + +#ifdef _USE_SENIOR_MODE +#include #endif +static int MEDIA_CONTENT_OTHER_SUPPORT = -1; + +bool _media_util_check_support_media_type(const char *path) +{ + int ret = SYSTEM_INFO_ERROR_NONE; + int media_type = -1; + bool is_supported = false; + + content_retvm_if(!STRING_VALID(path), false, "path is empty"); + + 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; + } + + 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"); + + if (media_type == MEDIA_CONTENT_TYPE_OTHERS) + return false; + } + + return true; +} + int _media_util_check_file_exist(const char *path) { int exist; @@ -34,12 +65,15 @@ int _media_util_check_file_exist(const char *path) /* check the file exits actually */ exist = open(path, O_RDONLY); if (exist < 0) { - media_content_sec_debug("path [%s]", path); - media_content_stderror("open file fail"); - if (errno == EACCES || errno == EPERM) + if (errno == EACCES || errno == EPERM) { + content_stderror("open file fail"); + content_sec_debug("path [%s]", path); return MEDIA_CONTENT_ERROR_PERMISSION_DENIED; - else + } else { + content_stderror("open file fail"); + content_sec_debug("path [%s]", path); return MEDIA_CONTENT_ERROR_INVALID_PARAMETER; + } } close(exist); @@ -47,293 +81,284 @@ int _media_util_check_file_exist(const char *path) return MEDIA_CONTENT_ERROR_NONE; } -int _media_util_check_ignore_file(const char *path, bool *ignore) +void _media_util_trim_path(const char *input_path, char **output_path) { - media_content_retvm_if(!STRING_VALID(path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid 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); +} - *ignore = FALSE; - if (strstr(path, "/.") != NULL) { - *ignore = TRUE; - media_content_error("hidden path"); - media_content_sec_debug("path : %s", 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; } - return MEDIA_CONTENT_ERROR_NONE; + return statbuf.st_mtime; } -int _media_util_check_ignore_dir(const char *dir_path, bool *ignore) +bool _media_util_is_ignorable_file(const char *path) { - int ret = MEDIA_CONTENT_ERROR_NONE; - media_svc_storage_type_e storage_type = 0; - const char *scan_ignore = ".scan_ignore"; - bool find = false; + content_retvm_if(!STRING_VALID(path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid path"); - media_content_sec_debug("dir_path : %s", dir_path); + char *tmp_path = NULL; + char *org_path = NULL; - media_content_retvm_if(!STRING_VALID(dir_path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid dir_path"); +#ifndef _USE_TVPD_MODE + char replace[MAX_PATH_LEN] = {0, }; +#endif - *ignore = FALSE; - /*1. Check Hidden Directory*/ - if (strstr(dir_path, "/.") != NULL) { - *ignore = TRUE; - media_content_error("hidden path"); - return MEDIA_CONTENT_ERROR_NONE; + /* 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; } - /*2. Check Scan Ignore Directory*/ - ret = media_svc_get_storage_type(dir_path, &storage_type, tzplatform_getuid(TZ_USER_NAME)); - if (ret != MS_MEDIA_ERR_NONE) { - media_content_error("media_svc_get_storage_type failed : %d", ret); - return _content_error_capi(MEDIA_CONTENT_TYPE, ret); + /* 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; } - DIR *dp = NULL; - struct dirent entry; - struct dirent *result = NULL; - - char *leaf_path = NULL; - char search_path[4096] = {0, }; + /* Check hidden path */ + if (strstr(path, "/.") != NULL) { + content_error("hidden path"); + content_sec_debug("path : %s", path); + return true; + } - strncpy(search_path, dir_path, strlen(dir_path)); - while (STRING_VALID(search_path)) { - dp = opendir(search_path); - if (dp == NULL) { - *ignore = TRUE; - media_content_error("Open Directory fail"); - media_content_sec_debug("Open fail path[%s]", search_path); - if (errno == EACCES || errno == EPERM) - return MEDIA_CONTENT_ERROR_PERMISSION_DENIED; - else - return MEDIA_CONTENT_ERROR_INVALID_PARAMETER; + /* Check symbolic directory */ + tmp_path = realpath(path, NULL); + /* Get trimmed path */ + _media_util_trim_path(path, &org_path); + +#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); + SAFE_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_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); + SAFE_FREE(org_path); + return true; } - - media_content_retvm_if(dp == NULL, MEDIA_CONTENT_ERROR_INVALID_OPERATION, "Open Directory fail"); - - while (!readdir_r(dp, &entry, &result)) { - if (result == NULL) - break; - - if (STRING_VALID(entry.d_name) && (strcmp(entry.d_name, scan_ignore) == 0)) { - media_content_error("Find Ignore path"); - media_content_sec_debug("Ignore path[%s]", search_path); - find = TRUE; - break; - } else { - /*media_content_sec_debug("entry.d_name[%s]", entry.d_name);*/ - continue; - } + } 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); + SAFE_FREE(org_path); + return true; } + } +#endif + SAFE_FREE(tmp_path); + SAFE_FREE(org_path); - if (dp) closedir(dp); - dp = NULL; + return false; +} - if (find) { - *ignore = TRUE; - break; - } else { - /*If root path, Stop Scanning*/ - if ((storage_type == MEDIA_SVC_STORAGE_INTERNAL) && (STRING_VALID(MEDIA_ROOT_PATH_INTERNAL) && strcmp(search_path, MEDIA_ROOT_PATH_INTERNAL) == 0)) { - break; - } else if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL) && (STRING_VALID(MEDIA_ROOT_PATH_SDCARD)) && (strcmp(search_path, MEDIA_ROOT_PATH_SDCARD) == 0)) { - break; - } else if (storage_type == MEDIA_SVC_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) && (strcmp(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) && (strcmp(search_path, MEDIA_ROOT_PATH_SENIOR_MODE) == 0)) - break; - } -#endif +static bool __is_scan_ignore_exist(const char *path) +{ + const char *scan_ignore = ".scan_ignore"; + char *ignore_path = NULL; + gboolean result = FALSE; - 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; - } - } - } + if (!STRING_VALID(path)) + return false; - return MEDIA_CONTENT_ERROR_NONE; + 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) + content_error("scan ignore file exist [%s]", ignore_path); + + SAFE_FREE(ignore_path); + + return (bool)result; } -int _media_content_replace_path_in_condition(const char *condition, char *replace_condition) +bool _media_util_is_ignorable_dir(const char *dir_path) { int ret = MEDIA_CONTENT_ERROR_NONE; - char *old_path = NULL; + ms_user_storage_type_e storage_type = MS_USER_STORAGE_INTERNAL; - ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &old_path); - if (ret != STORAGE_ERROR_NONE) { - media_content_error("storage_get_directory failed"); - return MEDIA_CONTENT_ERROR_INVALID_OPERATION; - } + content_retvm_if(!STRING_VALID(dir_path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid dir_path"); + content_sec_debug("dir_path : %s", dir_path); - media_content_sec_debug("Old condition[%s]", condition); - if (((strstr(condition, "PATH") != NULL) || (strstr(condition, "path") != NULL)) && (strstr(condition, old_path) != NULL)) { - char *cond = strdup(condition); - char *repl_cond_ptr = replace_condition; - char *cond_ptr = cond; - - if (cond_ptr == NULL) { - SAFE_FREE(old_path); - media_content_error("memory allocation failed"); - return MEDIA_CONTENT_ERROR_INVALID_OPERATION; - } - while (*cond_ptr != '\0') { - if (strlen(cond_ptr) < strlen(old_path)) { - memcpy(repl_cond_ptr, cond_ptr, strlen(cond_ptr)); - break; - } - /* replace path only and keep other condition */ - if (memcmp(cond_ptr, old_path, strlen(old_path)) == 0) { - memcpy(repl_cond_ptr, tzplatform_getenv(TZ_USER_CONTENT), strlen(tzplatform_getenv(TZ_USER_CONTENT))); - cond_ptr += strlen(old_path); - repl_cond_ptr += strlen(tzplatform_getenv(TZ_USER_CONTENT)); - } else { - *repl_cond_ptr = *cond_ptr; - cond_ptr++; - repl_cond_ptr++; - } - } - SAFE_FREE(cond); - } else { - snprintf(replace_condition, MAX_QUERY_SIZE, "%s", condition); + /*1. Check Hidden Directory*/ + if (strstr(dir_path, "/.") != NULL) { + content_error("hidden path"); + return true; } - SAFE_FREE(old_path); - - if (!STRING_VALID(replace_condition)) { - media_content_error("replace failed"); - return MEDIA_CONTENT_ERROR_INVALID_OPERATION; + /*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; } - media_content_sec_debug("repl cond[%s]", replace_condition); + char *leaf_path = NULL; + char search_path[MAX_PATH_LEN] = {0, }; - return MEDIA_CONTENT_ERROR_NONE; -} + SAFE_STRLCPY(search_path, dir_path, sizeof(search_path)); -int _media_content_rollback_path_in_condition(const char *condition, char *replace_condition) -{ - int ret = MEDIA_CONTENT_ERROR_NONE; - char *old_path = NULL; + while (STRING_VALID(search_path)) { + if (__is_scan_ignore_exist(search_path)) + return true; + + leaf_path = strrchr(search_path, '/'); + if (!leaf_path) + break; - ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &old_path); - if (ret != STORAGE_ERROR_NONE) { - media_content_error("storage_get_directory failed"); - return MEDIA_CONTENT_ERROR_INVALID_OPERATION; + search_path[leaf_path - search_path] = '\0'; } - media_content_sec_debug("Old condition[%s]", condition); - if (((strstr(condition, "PATH") != NULL) || (strstr(condition, "path") != NULL)) && (strstr(condition, tzplatform_getenv(TZ_USER_CONTENT)) != NULL)) { - char *cond = strdup(condition); - char *repl_cond_ptr = replace_condition; - char *cond_ptr = cond; + return false; +} - if (cond_ptr == NULL) { - SAFE_FREE(old_path); - media_content_error("memory allocation failed"); - return MEDIA_CONTENT_ERROR_INVALID_OPERATION; - } - while (*cond_ptr != '\0') { - if (strlen(cond_ptr) < strlen(tzplatform_getenv(TZ_USER_CONTENT))) { - memcpy(repl_cond_ptr, cond_ptr, strlen(cond_ptr)); - break; - } - /* replace path only and keep other condition */ - if (memcmp(cond_ptr, tzplatform_getenv(TZ_USER_CONTENT), strlen(tzplatform_getenv(TZ_USER_CONTENT))) == 0) { - memcpy(repl_cond_ptr, old_path, strlen(old_path)); - cond_ptr += strlen(tzplatform_getenv(TZ_USER_CONTENT)); - repl_cond_ptr += strlen(old_path); - } else { - *repl_cond_ptr = *cond_ptr; - cond_ptr++; - repl_cond_ptr++; - } +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) { + 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; } - SAFE_FREE(cond); - } else { - snprintf(replace_condition, MAX_QUERY_SIZE, "%s", condition); } - SAFE_FREE(old_path); + closedir(dp); + + /* Check symbolic link directory */ + real = realpath(path, NULL); + /* Get trimmed path */ + _media_util_trim_path(path, &origin); - if (!STRING_VALID(replace_condition)) { - media_content_error("replace failed"); - return MEDIA_CONTENT_ERROR_INVALID_OPERATION; +#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); + 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) { + content_error("symbolic link(directory)"); + 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) { + content_error("symbolic link(directory)"); + content_sec_debug("path[%s] real[%s]", origin, real); + SAFE_FREE(real); + SAFE_FREE(origin); + return MEDIA_CONTENT_ERROR_INVALID_PARAMETER; + } } +#endif - media_content_sec_debug("repl cond[%s]", replace_condition); + SAFE_FREE(real); + SAFE_FREE(origin); return MEDIA_CONTENT_ERROR_NONE; } -int _media_content_replace_path(const char *path, char *replace_path) + +/* 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; - char *old_path = NULL; + return g_strdup(condition); +#if 0 + char **split_list = NULL; + char *result = NULL; - ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &old_path); - if (ret != STORAGE_ERROR_NONE) { - media_content_error("storage_get_directory failed"); - return MEDIA_CONTENT_ERROR_INVALID_OPERATION; - } + if (!STRING_VALID(MEDIA_ROOT_PATH_INTERNAL_OLD) || !STRING_VALID(MEDIA_ROOT_PATH_INTERNAL)) + return NULL; - if (strncmp(path, old_path, strlen(old_path)) == 0) { - media_content_sec_debug("Old path[%s]", path); - snprintf(replace_path, MAX_QUERY_SIZE, "%s%s", tzplatform_getenv(TZ_USER_CONTENT), path + strlen(old_path)); - } else { - snprintf(replace_path, MAX_QUERY_SIZE, "%s", path); - } + content_sec_debug("Old condition[%s]", condition); - SAFE_FREE(old_path); + split_list = g_strsplit(condition, MEDIA_ROOT_PATH_INTERNAL_OLD, -1); + if (!split_list) + return NULL; - if (!STRING_VALID(replace_path)) { - media_content_error("replace failed"); - return MEDIA_CONTENT_ERROR_INVALID_OPERATION; - } + result = g_strjoinv(MEDIA_ROOT_PATH_INTERNAL, split_list); + g_strfreev(split_list); - return MEDIA_CONTENT_ERROR_NONE; + return result; +#endif } -int _media_content_rollback_path(const char *path, char *replace_path) +/* 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) { - int ret = MEDIA_CONTENT_ERROR_NONE; - char *old_path = NULL; - - ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &old_path); - if (ret != STORAGE_ERROR_NONE) { - media_content_error("storage_get_directory failed"); - return MEDIA_CONTENT_ERROR_INVALID_OPERATION; - } + content_retvm_if(!STRING_VALID(path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid path"); - if (strncmp(path, tzplatform_getenv(TZ_USER_CONTENT), strlen(tzplatform_getenv(TZ_USER_CONTENT))) == 0) { - media_content_sec_debug("new path[%s]", path); - snprintf(replace_path, MAX_QUERY_SIZE, "%s%s", old_path, path + strlen(tzplatform_getenv(TZ_USER_CONTENT))); + 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_QUERY_SIZE, "%s", path); - } - - SAFE_FREE(old_path); - - if (!STRING_VALID(replace_path)) { - media_content_error("replace failed"); - return MEDIA_CONTENT_ERROR_INVALID_OPERATION; + snprintf(replace_path, MAX_PATH_LEN, "%s", path); } +#endif return MEDIA_CONTENT_ERROR_NONE; } @@ -344,10 +369,10 @@ 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) { - media_content_debug("Get senior mode support failed"); + content_debug("Get senior mode support failed"); return false; } - /* media_content_debug("Senior mode Support : [%d]", bSupportSeniorMode); */ + /* content_debug("Senior mode Support : [%d]", bSupportSeniorMode); */ return bSupportSeniorMode; } #endif