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>
22 #include <system_info.h>
25 static int MEDIA_CONTENT_OTHER_SUPPORT = -1;
27 bool _media_util_check_support_media_type(const char *path)
29 int ret = SYSTEM_INFO_ERROR_NONE;
31 bool is_supported = false;
33 content_retvm_if(!STRING_VALID(path), false, "path is empty");
35 if (MEDIA_CONTENT_OTHER_SUPPORT == -1) {
36 ret = system_info_get_platform_bool("http://tizen.org/feature/content.scanning.others", &is_supported);
37 if (ret != SYSTEM_INFO_ERROR_NONE) {
38 content_debug("SYSTEM_INFO_ERROR: content.scanning.others [%d]", ret);
42 MEDIA_CONTENT_OTHER_SUPPORT = is_supported;
45 /* If not, check media type */
46 if (!MEDIA_CONTENT_OTHER_SUPPORT) {
47 ret = media_svc_get_media_type(path, &media_type);
48 content_retvm_if(ret != MS_MEDIA_ERR_NONE, false, "Failed to get media type");
50 if (media_type == MEDIA_CONTENT_TYPE_OTHERS)
57 int _media_util_check_file_exist(const char *path)
61 /* check the file exits actually */
62 exist = open(path, O_RDONLY);
64 if (errno == EACCES || errno == EPERM) {
65 content_stderror("open file fail");
66 content_sec_debug("path [%s]", path);
67 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
69 content_stderror("open file fail");
70 content_sec_debug("path [%s]", path);
71 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
77 return MEDIA_CONTENT_ERROR_NONE;
80 int _media_util_get_file_time(const char *path)
85 memset(&statbuf, 0, sizeof(struct stat));
86 ret = stat(path, &statbuf);
88 content_stderror("stat failed");
92 return statbuf.st_mtime;
95 bool _media_util_is_ignorable_file(const char *path)
97 char *tmp_path = NULL;
98 char *org_path = NULL;
100 #ifndef _USE_TVPD_MODE
101 char replace[MAX_PATH_LEN] = {0, };
104 content_retip_if_fail(STRING_VALID(path));
106 /* Check is exist (It may be the path to the deleted file) */
107 if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
108 content_sec_debug("removed path[%s]", path);
112 /* Check symbolic link file */
113 if (g_file_test(path, G_FILE_TEST_IS_SYMLINK)) {
114 content_error("symbolic link(file)");
115 content_sec_debug("path : %s", path);
119 /* Check hidden path */
120 if (strstr(path, "/.") != NULL) {
121 content_error("hidden path");
122 content_sec_debug("path : %s", path);
126 /* Check symbolic directory */
127 tmp_path = realpath(path, NULL);
128 /* Get trimmed path */
129 org_path = g_canonicalize_filename(path, NULL);
131 #ifdef _USE_TVPD_MODE
132 if (g_strcmp0(tmp_path, org_path) != 0) {
133 content_error("symbolic link(directory)");
134 content_sec_debug("path[%s] real[%s]", org_path, tmp_path);
140 if (g_str_has_prefix(tmp_path, MEDIA_SHARE_PATH)) {
141 /* If shared dirctory, it should be change path to TZ_USER_SHARE from realpath */
142 snprintf(replace, MAX_PATH_LEN, "%s%s", tzplatform_getenv(TZ_USER_MEDIASHARED), tmp_path + strlen(MEDIA_SHARE_PATH));
143 if (g_strcmp0(replace, org_path) != 0) {
144 content_error("symbolic link(directory)");
145 content_sec_debug("path[%s] real[%s]", org_path, tmp_path);
151 if (g_strcmp0(tmp_path, org_path) != 0) {
152 content_error("symbolic link(directory)");
153 content_sec_debug("path[%s] real[%s]", org_path, tmp_path);
166 static bool __is_scan_ignore_exist(const char *path)
168 const char *scan_ignore = ".scan_ignore";
169 char *ignore_path = NULL;
170 gboolean result = FALSE;
172 if (!STRING_VALID(path))
175 ignore_path = g_build_path(G_DIR_SEPARATOR_S, path, scan_ignore, NULL);
176 result = g_file_test(ignore_path, G_FILE_TEST_EXISTS);
179 content_error("scan ignore file exist [%s]", ignore_path);
186 bool _media_util_is_ignorable_dir(const char *dir_path)
188 int ret = MEDIA_CONTENT_ERROR_NONE;
189 ms_user_storage_type_e storage_type = MS_USER_STORAGE_INTERNAL;
191 content_retip_if_fail(STRING_VALID(dir_path));
193 content_sec_debug("dir_path : %s", dir_path);
195 /*1. Check Hidden Directory*/
196 if (strstr(dir_path, "/.") != NULL) {
197 content_error("hidden path");
201 /*2. Check Scan Ignore Directory*/
202 ret = ms_user_get_storage_type(_content_get_uid(), dir_path, &storage_type);
203 if (ret != MS_MEDIA_ERR_NONE) {
204 content_error("ms_user_get_storage_type failed : %d", ret);
208 char *leaf_path = NULL;
209 char search_path[MAX_PATH_LEN] = {0, };
211 SAFE_STRLCPY(search_path, dir_path, sizeof(search_path));
213 while (STRING_VALID(search_path)) {
214 if (__is_scan_ignore_exist(search_path))
217 leaf_path = strrchr(search_path, '/');
221 search_path[leaf_path - search_path] = '\0';
227 int _media_content_check_dir(const char *path)
232 #ifndef _USE_TVPD_MODE
233 char result_path[MAX_PATH_LEN] = {0, };
237 if (errno == EACCES || errno == EPERM) {
238 content_stderror("open dir fail");
239 content_sec_error("path [%s]", path);
240 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
242 content_stderror("open dir fail");
243 content_sec_error("path [%s]", path);
244 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
250 /* Check symbolic link directory */
251 real = realpath(path, NULL);
252 /* Get trimmed path */
253 origin = g_canonicalize_filename(path, NULL);
255 #ifdef _USE_TVPD_MODE
256 if (g_strcmp0(real, origin) != 0) {
257 content_error("symbolic link(directory)");
258 content_sec_debug("path[%s] real[%s]", origin, real);
261 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
264 if (g_str_has_prefix(real, MEDIA_SHARE_PATH)) {
265 /* If shared dirctory, it should be change path to TZ_USER_SHARE from realpath */
266 snprintf(result_path, MAX_PATH_LEN, "%s%s", tzplatform_getenv(TZ_USER_MEDIASHARED), real + strlen(MEDIA_SHARE_PATH));
267 if (g_strcmp0(result_path, origin) != 0) {
268 content_error("symbolic link(directory)");
269 content_sec_debug("path[%s] real[%s]", origin, real);
272 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
275 if (g_strcmp0(real, origin) != 0) {
276 content_error("symbolic link(directory)");
277 content_sec_debug("path[%s] real[%s]", origin, real);
280 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
288 return MEDIA_CONTENT_ERROR_NONE;
292 /* FIXME : If there are no issue reports related to this, it will be deleted in tizen 6.5 or after. */
293 char * _media_content_replace_path_in_condition(const char *condition)
295 return g_strdup(condition);
297 char **split_list = NULL;
300 if (!STRING_VALID(MEDIA_ROOT_PATH_INTERNAL_OLD) || !STRING_VALID(MEDIA_ROOT_PATH_INTERNAL))
303 content_sec_debug("Old condition[%s]", condition);
305 split_list = g_strsplit(condition, MEDIA_ROOT_PATH_INTERNAL_OLD, -1);
309 result = g_strjoinv(MEDIA_ROOT_PATH_INTERNAL, split_list);
310 g_strfreev(split_list);
316 /* FIXME : If there are no issue reports related to this, it will be deleted in Tizen 6.5 or after. */
317 int _media_content_replace_path(const char *path, char *replace_path)
319 content_retip_if_fail(STRING_VALID(path));
321 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
323 if (strncmp(path, MEDIA_ROOT_PATH_INTERNAL_OLD, strlen(MEDIA_ROOT_PATH_INTERNAL_OLD)) == 0) {
324 content_sec_debug("Old path[%s]", path);
325 snprintf(replace_path, MAX_PATH_LEN, "%s%s", MEDIA_ROOT_PATH_INTERNAL, path + strlen(MEDIA_ROOT_PATH_INTERNAL_OLD));
327 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
331 return MEDIA_CONTENT_ERROR_NONE;