2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include <media_info_private.h>
21 #include <system_info.h>
24 static int MEDIA_CONTENT_OTHER_SUPPORT = -1;
26 bool _media_util_check_support_media_type(const char *path)
28 int ret = SYSTEM_INFO_ERROR_NONE;
30 bool is_supported = false;
32 content_retvm_if(!STRING_VALID(path), false, "path is empty");
34 if (MEDIA_CONTENT_OTHER_SUPPORT == -1) {
35 ret = system_info_get_platform_bool("http://tizen.org/feature/content.scanning.others", &is_supported);
36 content_retvm_if(ret != SYSTEM_INFO_ERROR_NONE, false, "SYSTEM_INFO_ERROR: content.scanning.others [%d]", ret);
38 MEDIA_CONTENT_OTHER_SUPPORT = is_supported;
41 /* If not, check media type */
42 if (!MEDIA_CONTENT_OTHER_SUPPORT) {
43 ret = media_svc_get_media_type(path, &media_type);
44 content_retvm_if(ret != MS_MEDIA_ERR_NONE, false, "Failed to get media type");
45 content_retv_if(media_type == MEDIA_CONTENT_TYPE_OTHERS, false);
51 int _media_util_check_file_exist(const char *path)
55 /* check the file exits actually */
56 exist = open(path, O_RDONLY);
58 if (errno == EACCES || errno == EPERM) {
59 content_stderror("open file fail");
60 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
62 content_stderror("open file fail");
63 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
69 return MEDIA_CONTENT_ERROR_NONE;
72 int _media_util_get_file_time(const char *path)
74 struct stat statbuf = { 0, };
76 content_retvm_if(stat(path, &statbuf) == -1, 0, "stat failed");
78 return statbuf.st_mtime;
81 bool _media_util_is_ignorable_file(const char *path)
83 g_autofree gchar *real = NULL;
84 g_autofree gchar *org_path = NULL;
85 #ifndef _USE_TVPD_MODE
86 char replace[MAX_PATH_LEN] = {0, };
89 content_retip_if_fail(STRING_VALID(path));
91 /* Check is exist (It may be the path to the deleted file) */
92 content_retvm_if(!g_file_test(path, G_FILE_TEST_EXISTS), false, "removed path");
94 /* Check symbolic link file */
95 content_retvm_if(g_file_test(path, G_FILE_TEST_IS_SYMLINK), true, "symbolic link(file)");
97 /* Check hidden path */
98 content_retvm_if(strstr(path, "/."), true, "hidden path");
100 /* Check symbolic directory */
101 real = realpath(path, NULL);
102 /* Get trimmed path */
103 org_path = g_canonicalize_filename(path, NULL);
105 #ifdef _USE_TVPD_MODE
106 if (g_strcmp0(real, org_path) != 0) {
107 content_error("symbolic link(directory)");
111 if (g_str_has_prefix(real, tzplatform_getenv(TZ_SYS_MEDIASHARED))) {
112 /* If shared directory, it should be change path to TZ_USER_SHARE from realpath */
113 snprintf(replace, MAX_PATH_LEN, "%s%s", tzplatform_getenv(TZ_USER_MEDIASHARED), real + strlen(tzplatform_getenv(TZ_SYS_MEDIASHARED)));
114 if (g_strcmp0(replace, org_path) != 0) {
115 content_error("symbolic link(directory)");
119 if (g_strcmp0(real, org_path) != 0) {
120 content_error("symbolic link(directory)");
129 static bool __is_scan_ignore_exist(const char *path)
131 const char *scan_ignore = ".scan_ignore";
132 char *ignore_path = NULL;
133 gboolean result = FALSE;
135 if (!STRING_VALID(path))
138 ignore_path = g_build_path(G_DIR_SEPARATOR_S, path, scan_ignore, NULL);
139 result = g_file_test(ignore_path, G_FILE_TEST_EXISTS);
142 content_error("scan ignore file exist [%s]", ignore_path);
149 bool _media_util_is_ignorable_dir(const char *dir_path)
151 if (!STRING_VALID(dir_path))
154 /*1. Check Hidden Directory*/
155 content_retvm_if(strstr(dir_path, "/."), true, "hidden path");
157 /*2. Check Scan Ignore Directory*/
158 content_retvm_if(!ms_user_is_valid_path(_content_get_uid(), dir_path), true, "ms_user_is_valid_path failed");
160 char *leaf_path = NULL;
161 char search_path[MAX_PATH_LEN] = {0, };
163 g_strlcpy(search_path, dir_path, sizeof(search_path));
165 while (strlen(search_path) > 0) {
166 if (__is_scan_ignore_exist(search_path))
169 leaf_path = strrchr(search_path, '/');
173 search_path[leaf_path - search_path] = '\0';
179 int _media_content_check_dir(const char *path)
182 g_autofree gchar *real = NULL;
183 g_autofree gchar *origin = NULL;
184 #ifndef _USE_TVPD_MODE
185 char result_path[MAX_PATH_LEN] = {0, };
189 if (errno == EACCES || errno == EPERM) {
190 content_stderror("open dir fail");
191 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
193 content_stderror("open dir fail");
194 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
200 /* Check symbolic link directory */
201 real = realpath(path, NULL);
202 /* Get trimmed path */
203 origin = g_canonicalize_filename(path, NULL);
205 #ifdef _USE_TVPD_MODE
206 if (g_strcmp0(real, origin) != 0) {
207 content_error("symbolic link(directory)");
208 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
211 if (g_str_has_prefix(real, tzplatform_getenv(TZ_SYS_MEDIASHARED))) {
212 /* If shared directory, it should be change path to TZ_USER_SHARE from realpath */
213 snprintf(result_path, MAX_PATH_LEN, "%s%s", tzplatform_getenv(TZ_USER_MEDIASHARED), real + strlen(tzplatform_getenv(TZ_SYS_MEDIASHARED)));
214 if (g_strcmp0(result_path, origin) != 0) {
215 content_error("symbolic link(directory)");
216 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
219 if (g_strcmp0(real, origin) != 0) {
220 content_error("symbolic link(directory)");
221 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
226 return MEDIA_CONTENT_ERROR_NONE;