*/
-#include <string.h>
-#include <sys/stat.h>
-#include <media_util_private.h>
+#include <dirent.h>
+#include <fcntl.h>
#include <media_info_private.h>
-#include <media_content_type.h>
+#include <system_info.h>
+#include <sys/stat.h>
+static int MEDIA_CONTENT_OTHER_SUPPORT = -1;
-int _media_util_get_store_type_by_path(const char *path, int *storage_type)
+bool _media_util_check_support_media_type(const char *path)
{
- if(STRING_VALID(path))
- {
- if(strncmp(path, MEDIA_CONTENT_PATH_PHONE, strlen(MEDIA_CONTENT_PATH_PHONE)) == 0)
- {
- *storage_type = MEDIA_CONTENT_STORAGE_INTERNAL;
+ 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;
}
- else if(strncmp (path, MEDIA_CONTENT_PATH_MMC, strlen(MEDIA_CONTENT_PATH_MMC)) == 0)
- {
- *storage_type = MEDIA_CONTENT_STORAGE_EXTERNAL;
+
+ 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;
+
+ /* check the file exits actually */
+ exist = open(path, O_RDONLY);
+ 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;
}
}
- else
- {
- media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
+
+ close(exist);
+
+ return MEDIA_CONTENT_ERROR_NONE;
+}
+
+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 statbuf.st_mtime;
+}
+
+bool _media_util_is_ignorable_file(const char *path)
+{
+ char *tmp_path = NULL;
+ char *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;
+ }
+
+ /* 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;
+ }
+
+ /* Check hidden path */
+ if (strstr(path, "/.") != NULL) {
+ content_error("hidden path");
+ content_sec_debug("path : %s", path);
+ return true;
+ }
+
+ /* Check symbolic directory */
+ tmp_path = realpath(path, NULL);
+ /* Get trimmed 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 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(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);
+ 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;
+}
+
+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)
+ content_error("scan ignore file exist [%s]", ignore_path);
+
+ g_free(ignore_path);
+
+ return (bool)result;
+}
+
+bool _media_util_is_ignorable_dir(const char *dir_path)
+{
+ int ret = MEDIA_CONTENT_ERROR_NONE;
+
+ if (!STRING_VALID(dir_path))
+ return true;
+
+ content_sec_debug("dir_path : %s", dir_path);
+
+ /*1. Check Hidden Directory*/
+ if (strstr(dir_path, "/.") != NULL) {
+ content_error("hidden path");
+ return true;
+ }
+
+ /*2. Check Scan Ignore Directory*/
+ if (!ms_user_is_valid_path(_content_get_uid(), dir_path)) {
+ content_error("ms_user_is_valid_path failed : %d", ret);
+ return true;
+ }
+
+ char *leaf_path = NULL;
+ char search_path[MAX_PATH_LEN] = {0, };
+
+ SAFE_STRLCPY(search_path, dir_path, sizeof(search_path));
+
+ while (STRING_VALID(search_path)) {
+ if (__is_scan_ignore_exist(search_path))
+ return true;
+
+ leaf_path = strrchr(search_path, '/');
+ if (!leaf_path)
+ break;
+
+ search_path[leaf_path - search_path] = '\0';
+ }
+
+ return false;
+}
+
+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;
+ }
+ }
+
+ closedir(dp);
+
+ /* Check symbolic link directory */
+ real = realpath(path, NULL);
+ /* Get trimmed path */
+ 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 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(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);
+ 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;
}