Fix format error in logs
[platform/core/api/media-content.git] / src / media_util_private.c
index 8587baa..1415b4f 100755 (executable)
 * limitations under the License.
 */
 
+
 #include <dirent.h>
-#include <string.h>
-#include <sys/stat.h>
-#include <sys/types.h>
 #include <fcntl.h>
-#include <media_util_private.h>
 #include <media_info_private.h>
-#include <media_content_type.h>
+#include <storage.h>
+#include <system_info.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)
+{
+       int ret = SYSTEM_INFO_ERROR_NONE;
+       int media_type = -1;
+       bool is_supported = false;
+
+       media_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) {
+                       media_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);
+               media_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)
 {
@@ -29,14 +63,13 @@ int _media_util_check_file_exist(const char *path)
 
        /* check the file exits actually */
        exist = open(path, O_RDONLY);
-       if(exist < 0) {
+       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)
                        return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
-               } else {
+               else
                        return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
-               }
        }
 
        close(exist);
@@ -44,18 +77,106 @@ 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");
 
        *ignore = FALSE;
+       char *tmp_path = NULL;
+       char *org_path = NULL;
+
+#ifndef _USE_TVPD_MODE
+       char replace[MAX_PATH_LEN] = {0, };
+#endif
+
+       /* Check is exist (It may be the path to the deleted file) */
+       if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
+               media_content_sec_debug("removed path[%s]", path);
+               return MEDIA_CONTENT_ERROR_NONE;
+       }
+
+       /* Check symbolic link file */
+       if (g_file_test(path, G_FILE_TEST_IS_SYMLINK)) {
+               *ignore = TRUE;
+               media_content_error("symbolic link(file)");
+               media_content_sec_debug("path : %s", path);
+               return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
+       }
 
-       if(strstr(path, "/.") != NULL)
-       {
+       /* Check hidden path */
+       if (strstr(path, "/.") != NULL) {
                *ignore = TRUE;
                media_content_error("hidden path");
                media_content_sec_debug("path : %s", path);
+               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) {
+               *ignore = TRUE;
+               media_content_error("symbolic link(directory)");
+               media_content_sec_debug("path[%s] real[%s]", org_path, tmp_path);
+               SAFE_FREE(tmp_path);
+               SAFE_FREE(org_path);
+               return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
+       }
+#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) {
+                       *ignore = TRUE;
+                       media_content_error("symbolic link(directory)");
+                       media_content_sec_debug("path[%s] real[%s]", org_path, tmp_path);
+                       SAFE_FREE(tmp_path);
+                       SAFE_FREE(org_path);
+                       return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
+               }
+       } else {
+               if (g_strcmp0(tmp_path, org_path) != 0) {
+                       *ignore = TRUE;
+                       media_content_error("symbolic link(directory)");
+                       media_content_sec_debug("path[%s] real[%s]", org_path, tmp_path);
+                       SAFE_FREE(tmp_path);
+                       SAFE_FREE(org_path);
+                       return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
+               }
        }
+#endif
+       SAFE_FREE(tmp_path);
+       SAFE_FREE(org_path);
 
        return MEDIA_CONTENT_ERROR_NONE;
 }
@@ -63,9 +184,12 @@ 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;
+       GError *error = NULL;
+       const char *name;
 
        media_content_sec_debug("dir_path : %s", dir_path);
 
@@ -73,86 +197,70 @@ int _media_util_check_ignore_dir(const char *dir_path, bool *ignore)
 
        *ignore = FALSE;
        /*1. Check Hidden Directory*/
-       if(strstr(dir_path, "/.") != NULL)
-       {
+       if (strstr(dir_path, "/.") != NULL) {
                *ignore = TRUE;
                media_content_error("hidden path");
                return MEDIA_CONTENT_ERROR_NONE;
        }
 
        /*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);
+       ret = ms_user_get_storage_type(_content_get_uid(), dir_path, &storage_type);
+       if (ret != MS_MEDIA_ERR_NONE) {
+               media_content_error("ms_user_get_storage_type failed : %d", ret);
                return _content_error_capi(MEDIA_CONTENT_TYPE, ret);
        }
 
-       DIR *dp = NULL;
-       struct dirent entry;
-       struct dirent *result = NULL;
-
        char *leaf_path = NULL;
-       char search_path[4096] = {0, };
+       char search_path[MAX_PATH_LEN] = {0, };
 
-       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);
-                       return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
-               }
-
-               media_content_retvm_if(dp == NULL, MEDIA_CONTENT_ERROR_INVALID_OPERATION, "Open Directory fail");
-
-               while (!readdir_r(dp, &entry, &result))
-               {
-                       if (result == NULL)
-                               break;
+       memset(search_path, 0, sizeof(search_path));
+       if (!SAFE_STRLCPY(search_path, dir_path, sizeof(search_path))) {
+               media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
+               return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
+       }
 
-                       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;
+       while (STRING_VALID(search_path)) {
+               dir = g_dir_open(search_path, 0, &error);
+               if (dir != NULL && error == NULL) {
+                       while ((name = g_dir_read_name(dir))) {
+                               if (g_strcmp0(name, scan_ignore) == 0) {
+                                       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 {
+                       *ignore = TRUE;
+                       media_content_error("Open Directory fail");
+                       if (error->code == G_FILE_ERROR_ACCES) {
+                               g_error_free(error);
+                               return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
+                       } else {
+                               g_error_free(error);
+                               return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
                        }
                }
 
-               if (dp) closedir(dp);
-               dp = NULL;
+               if (dir)
+                       g_dir_close(dir);
 
-               if(find)
-               {
+               if (find) {
                        *ignore = TRUE;
                        break;
-               }
-               else
-               {
+               } else {
                        /*If root path, Stop Scanning*/
-                       if((storage_type == MEDIA_SVC_STORAGE_INTERNAL) && (strcmp(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) && (strcmp(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)
-                       {
+                       } 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 == MS_USER_STORAGE_EXTERNAL_USB) {
                                char *parent_folder_path = NULL;
                                bool is_root = FALSE;
 
                                parent_folder_path = g_path_get_dirname(search_path);
-                               if (strcmp(search_path, MEDIA_ROOT_PATH_USB) == 0)
+                               if (STRING_VALID(MEDIA_ROOT_PATH_USB) && STRING_VALID(parent_folder_path) && (g_strcmp0(parent_folder_path, MEDIA_ROOT_PATH_USB) == 0))
                                        is_root = TRUE;
 
                                SAFE_FREE(parent_folder_path);
@@ -160,16 +268,19 @@ int _media_util_check_ignore_dir(const char *dir_path, bool *ignore)
                                if (is_root == TRUE)
                                        break;
                        }
+#ifdef _USE_SENIOR_MODE
+                       if (_media_content_is_support_senior_mode()) {
+                               if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL) && (g_strcmp0(search_path, MEDIA_ROOT_PATH_SENIOR_MODE) == 0))
+                                       break;
+                       }
+#endif
 
                        leaf_path = strrchr(search_path, '/');
-                       if(leaf_path != NULL)
-                       {
+                       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_sec_debug("go to other dir [%s]", search_path);*/
+                       } else {
                                media_content_debug("Fail to find leaf path");
                                break;
                        }
@@ -178,3 +289,202 @@ 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;
+
+#ifdef _USE_TVPD_MODE
+       snprintf(replace_condition, MAX_QUERY_SIZE, "%s", condition);
+#else
+       char old_condition[MAX_QUERY_SIZE] = {0, };
+       char new_condition[MAX_QUERY_SIZE] = {0, };
+       char *find = NULL;
+       unsigned int str_len = 0;
+
+       char *find_str = NULL;
+       char *to_replace_str = NULL;
+
+       if (replace == TRUE) {  //change User session path to System session path
+               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;
+               }
+
+               to_replace_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL);
+               if (!STRING_VALID(to_replace_str)) {
+                       media_content_error("Get TZ_USER_CONTENT failed");
+                       ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
+                       goto ERROR;
+               }
+       } else {
+               find_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL);
+               if (!STRING_VALID(find_str)) {
+                       media_content_error("Get TZ_USER_CONTENT failed");
+                       ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
+                       goto ERROR;
+               }
+
+               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;
+               }
+       }
+
+       memset(old_condition, 0, sizeof(old_condition));
+       memset(new_condition, 0, sizeof(new_condition));
+
+       media_content_sec_debug("Old condition[%s]", condition);
+
+       if (!SAFE_STRLCPY(new_condition, condition, sizeof(new_condition))) {
+               media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
+               ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
+               goto ERROR;
+       }
+
+       if (g_strcmp0(find_str, to_replace_str))
+               find = strstr(new_condition, find_str);
+
+       while (find != NULL) {
+               str_len = find - new_condition;
+
+               memset(old_condition, 0, sizeof(old_condition));
+               if (!SAFE_STRLCPY(old_condition, new_condition, sizeof(old_condition))) {
+                       media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
+                       ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
+                       goto ERROR;
+               }
+               memset(new_condition, 0, sizeof(new_condition));
+
+               snprintf(new_condition, str_len + 1, "%s", old_condition);
+
+               SAFE_STRLCAT(new_condition, to_replace_str, sizeof(new_condition));
+               SAFE_STRLCAT(new_condition, old_condition + str_len + strlen(find_str), sizeof(new_condition));
+
+               find = strstr(new_condition, find_str);
+       }
+
+       if (!SAFE_STRLCPY(replace_condition, new_condition, MAX_QUERY_SIZE)) {
+               media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
+               ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
+               goto ERROR;
+       }
+
+       media_content_sec_debug("repl cond[%s]", replace_condition);
+
+       if (!STRING_VALID(replace_condition)) {
+               media_content_error("replace failed");
+               ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
+               goto ERROR;
+       }
+
+ERROR:
+       SAFE_FREE(find_str);
+       SAFE_FREE(to_replace_str);
+#endif
+
+       return ret;
+}
+
+int _media_content_replace_path(const char *path, char *replace_path)
+{
+#ifdef _USE_TVPD_MODE
+       snprintf(replace_path, MAX_PATH_LEN, "%s", path);
+#else
+       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(MEDIA_ROOT_PATH_INTERNAL_OLD));
+       } else {
+               snprintf(replace_path, MAX_PATH_LEN, "%s", path);
+       }
+#endif
+
+       if (!STRING_VALID(replace_path)) {
+               media_content_error("replace failed");
+               return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
+       }
+
+       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) {
+               media_content_debug("Get senior mode support failed");
+               return false;
+       }
+       /* media_content_debug("Senior mode Support : [%d]", bSupportSeniorMode); */
+       return bSupportSeniorMode;
+}
+#endif
+