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 #ifdef _USE_SENIOR_MODE
26 #include <media_util_private.h>
29 static int MEDIA_CONTENT_OTHER_SUPPORT = -1;
31 bool _media_util_check_support_media_type(const char *path)
33 int ret = SYSTEM_INFO_ERROR_NONE;
35 bool is_supported = false;
37 content_retvm_if(!STRING_VALID(path), false, "path is empty");
39 if (MEDIA_CONTENT_OTHER_SUPPORT == -1) {
40 ret = system_info_get_platform_bool("http://tizen.org/feature/content.scanning.others", &is_supported);
41 if (ret != SYSTEM_INFO_ERROR_NONE) {
42 content_debug("SYSTEM_INFO_ERROR: content.scanning.others [%d]", ret);
46 MEDIA_CONTENT_OTHER_SUPPORT = is_supported;
49 /* If not, check media type */
50 if (!MEDIA_CONTENT_OTHER_SUPPORT) {
51 ret = media_svc_get_media_type(path, &media_type);
52 content_retvm_if(ret != MS_MEDIA_ERR_NONE, false, "Failed to get media type");
54 if (media_type == MEDIA_CONTENT_TYPE_OTHERS)
61 int _media_util_check_file_exist(const char *path)
65 /* check the file exits actually */
66 exist = open(path, O_RDONLY);
68 if (errno == EACCES || errno == EPERM) {
69 content_stderror("open file fail");
70 content_sec_debug("path [%s]", path);
71 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
73 content_stderror("open file fail");
74 content_sec_debug("path [%s]", path);
75 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
81 return MEDIA_CONTENT_ERROR_NONE;
84 void _media_util_trim_path(const char *input_path, char **output_path)
86 gchar **name_list = NULL;
87 gchar *tmp_path = NULL;
89 if (!STRING_VALID(input_path) || output_path == NULL)
94 After g_strsplit() : {'','a','b','','c',''}
95 After g_build_pathv() : a/b/c
96 After g_strdup_printf() : /a/b/c
98 name_list = g_strsplit(input_path, "/", -1);
102 tmp_path = g_build_pathv(G_DIR_SEPARATOR_S, name_list);
103 g_strfreev(name_list);
107 /* g_build_pathv does not add root '/' */
108 *output_path = g_strdup_printf("/%s", tmp_path);
113 int _media_util_get_file_time(const char *path)
118 memset(&statbuf, 0, sizeof(struct stat));
119 ret = stat(path, &statbuf);
121 content_stderror("stat failed");
125 return statbuf.st_mtime;
128 bool _media_util_is_ignorable_file(const char *path)
130 content_retvm_if(!STRING_VALID(path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid path");
132 char *tmp_path = NULL;
133 char *org_path = NULL;
135 #ifndef _USE_TVPD_MODE
136 char replace[MAX_PATH_LEN] = {0, };
139 /* Check is exist (It may be the path to the deleted file) */
140 if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
141 content_sec_debug("removed path[%s]", path);
145 /* Check symbolic link file */
146 if (g_file_test(path, G_FILE_TEST_IS_SYMLINK)) {
147 content_error("symbolic link(file)");
148 content_sec_debug("path : %s", path);
152 /* Check hidden path */
153 if (strstr(path, "/.") != NULL) {
154 content_error("hidden path");
155 content_sec_debug("path : %s", path);
159 /* Check symbolic directory */
160 tmp_path = realpath(path, NULL);
161 /* Get trimmed path */
162 _media_util_trim_path(path, &org_path);
164 #ifdef _USE_TVPD_MODE
165 if (g_strcmp0(tmp_path, org_path) != 0) {
166 content_error("symbolic link(directory)");
167 content_sec_debug("path[%s] real[%s]", org_path, tmp_path);
173 if (g_str_has_prefix(tmp_path, MEDIA_SHARE_PATH)) {
174 /* If shared dirctory, it should be change path to TZ_USER_SHARE from realpath */
175 snprintf(replace, MAX_PATH_LEN, "%s%s", tzplatform_getenv(TZ_USER_MEDIASHARED), tmp_path + strlen(MEDIA_SHARE_PATH));
176 if (g_strcmp0(replace, org_path) != 0) {
177 content_error("symbolic link(directory)");
178 content_sec_debug("path[%s] real[%s]", org_path, tmp_path);
184 if (g_strcmp0(tmp_path, org_path) != 0) {
185 content_error("symbolic link(directory)");
186 content_sec_debug("path[%s] real[%s]", org_path, tmp_path);
199 static bool __is_scan_ignore_exist(const char *path)
201 const char *scan_ignore = ".scan_ignore";
202 char *ignore_path = NULL;
203 gboolean result = FALSE;
205 if (!STRING_VALID(path))
208 ignore_path = g_build_path(G_DIR_SEPARATOR_S, path, scan_ignore, NULL);
209 result = g_file_test(ignore_path, G_FILE_TEST_EXISTS);
212 content_error("scan ignore file exist [%s]", ignore_path);
214 SAFE_FREE(ignore_path);
219 bool _media_util_is_ignorable_dir(const char *dir_path)
221 int ret = MEDIA_CONTENT_ERROR_NONE;
222 ms_user_storage_type_e storage_type = MS_USER_STORAGE_INTERNAL;
224 content_retvm_if(!STRING_VALID(dir_path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid dir_path");
225 content_sec_debug("dir_path : %s", dir_path);
227 /*1. Check Hidden Directory*/
228 if (strstr(dir_path, "/.") != NULL) {
229 content_error("hidden path");
233 /*2. Check Scan Ignore Directory*/
234 ret = ms_user_get_storage_type(_content_get_uid(), dir_path, &storage_type);
235 if (ret != MS_MEDIA_ERR_NONE) {
236 content_error("ms_user_get_storage_type failed : %d", ret);
240 char *leaf_path = NULL;
241 char search_path[MAX_PATH_LEN] = {0, };
243 memset(search_path, 0, sizeof(search_path));
244 SAFE_STRLCPY(search_path, dir_path, sizeof(search_path));
246 while (STRING_VALID(search_path)) {
247 if (__is_scan_ignore_exist(search_path))
250 leaf_path = strrchr(search_path, '/');
254 search_path[leaf_path - search_path] = '\0';
260 int _media_content_check_dir(const char *path)
265 #ifndef _USE_TVPD_MODE
266 char result_path[MAX_PATH_LEN] = {0, };
270 if (errno == EACCES || errno == EPERM) {
271 content_stderror("open dir fail");
272 content_sec_error("path [%s]", path);
273 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
275 content_stderror("open dir fail");
276 content_sec_error("path [%s]", path);
277 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
283 /* Check symbolic link directory */
284 real = realpath(path, NULL);
285 /* Get trimmed path */
286 _media_util_trim_path(path, &origin);
288 #ifdef _USE_TVPD_MODE
289 if (g_strcmp0(real, origin) != 0) {
290 content_error("symbolic link(directory)");
291 content_sec_debug("path[%s] real[%s]", origin, real);
294 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
297 if (g_str_has_prefix(real, MEDIA_SHARE_PATH)) {
298 /* If shared dirctory, it should be change path to TZ_USER_SHARE from realpath */
299 snprintf(result_path, MAX_PATH_LEN, "%s%s", tzplatform_getenv(TZ_USER_MEDIASHARED), real + strlen(MEDIA_SHARE_PATH));
300 if (g_strcmp0(result_path, origin) != 0) {
301 content_error("symbolic link(directory)");
302 content_sec_debug("path[%s] real[%s]", origin, real);
305 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
308 if (g_strcmp0(real, origin) != 0) {
309 content_error("symbolic link(directory)");
310 content_sec_debug("path[%s] real[%s]", origin, real);
313 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
321 return MEDIA_CONTENT_ERROR_NONE;
325 /* FIXME : If there are no issue reports related to this, it will be deleted in tizen 6.5 or after. */
326 char * _media_content_replace_path_in_condition(const char *condition)
328 return g_strdup(condition);
330 char **split_list = NULL;
333 if (!STRING_VALID(MEDIA_ROOT_PATH_INTERNAL_OLD) || !STRING_VALID(MEDIA_ROOT_PATH_INTERNAL))
336 content_sec_debug("Old condition[%s]", condition);
338 split_list = g_strsplit(condition, MEDIA_ROOT_PATH_INTERNAL_OLD, -1);
342 result = g_strjoinv(MEDIA_ROOT_PATH_INTERNAL, split_list);
343 g_strfreev(split_list);
349 /* FIXME : If there are no issue reports related to this, it will be deleted in Tizen 6.5 or after. */
350 int _media_content_replace_path(const char *path, char *replace_path)
352 content_retvm_if(!STRING_VALID(path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "Invalid path");
354 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
356 if (strncmp(path, MEDIA_ROOT_PATH_INTERNAL_OLD, strlen(MEDIA_ROOT_PATH_INTERNAL_OLD)) == 0) {
357 content_sec_debug("Old path[%s]", path);
358 snprintf(replace_path, MAX_PATH_LEN, "%s%s", MEDIA_ROOT_PATH_INTERNAL, path + strlen(MEDIA_ROOT_PATH_INTERNAL_OLD));
360 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
364 return MEDIA_CONTENT_ERROR_NONE;
367 #ifdef _USE_SENIOR_MODE
368 bool _media_content_is_support_senior_mode()
370 bool bSupportSeniorMode = false;
372 if (system_info_get_value_bool(SYSTEM_INFO_KEY_GET_SENIOR_MODE_SUPPORTED, &bSupportSeniorMode) != SYSTEM_INFO_ERROR_NONE) {
373 content_debug("Get senior mode support failed");
376 /* content_debug("Senior mode Support : [%d]", bSupportSeniorMode); */
377 return bSupportSeniorMode;