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 media_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 media_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 media_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 media_content_stderror("open file fail");
70 media_content_sec_debug("path [%s]", path);
71 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
73 media_content_stderror("open file fail");
74 media_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 char buf[4096] = {0,};
87 char tmp[4096] = {0,};
90 memset(buf, 0, sizeof(buf));
91 if (!SAFE_STRLCPY(buf, input_path, sizeof(buf)))
92 media_content_sec_debug("Truncation occurred[%zu]", strlen(input_path));
94 while ((pos = strstr(buf, "//")) != NULL) {
95 memset(tmp, 0, sizeof(tmp));
96 if (!SAFE_STRLCPY(tmp, buf, pos - buf + 1))
97 media_content_sec_debug("Truncation occurred");
98 SAFE_STRLCAT(tmp, pos + 1, sizeof(tmp));
100 memset(buf, 0, sizeof(buf));
101 if (!SAFE_STRLCPY(buf, tmp, sizeof(buf)))
102 media_content_sec_debug("Truncation occurred[%zu]", strlen(tmp));
105 if (g_str_has_suffix(buf, "/"))
106 *output_path = g_strndup(buf, strlen(buf) - 1);
108 *output_path = g_strdup(buf);
112 int _media_util_get_file_time(const char *path)
117 memset(&statbuf, 0, sizeof(struct stat));
118 ret = stat(path, &statbuf);
120 media_content_stderror("stat failed");
124 return statbuf.st_mtime;
127 int _media_util_check_ignore_file(const char *path, bool *ignore)
129 media_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 media_content_sec_debug("removed path[%s]", path);
142 return MEDIA_CONTENT_ERROR_NONE;
145 /* Check symbolic link file */
146 if (g_file_test(path, G_FILE_TEST_IS_SYMLINK)) {
148 media_content_error("symbolic link(file)");
149 media_content_sec_debug("path : %s", path);
150 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
153 /* Check hidden path */
154 if (strstr(path, "/.") != NULL) {
156 media_content_error("hidden path");
157 media_content_sec_debug("path : %s", path);
158 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
161 /* Check symbolic directory */
162 tmp_path = realpath(path, NULL);
163 /* Get trimmed path */
164 _media_util_trim_path(path, &org_path);
166 #ifdef _USE_TVPD_MODE
167 if (g_strcmp0(tmp_path, org_path) != 0) {
169 media_content_error("symbolic link(directory)");
170 media_content_sec_debug("path[%s] real[%s]", org_path, tmp_path);
173 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
176 if (g_str_has_prefix(tmp_path, MEDIA_SHARE_PATH)) {
177 /* If shared dirctory, it should be change path to TZ_USER_SHARE from realpath */
178 snprintf(replace, MAX_PATH_LEN, "%s%s", tzplatform_getenv(TZ_USER_MEDIASHARED), tmp_path + strlen(MEDIA_SHARE_PATH));
179 if (g_strcmp0(replace, org_path) != 0) {
181 media_content_error("symbolic link(directory)");
182 media_content_sec_debug("path[%s] real[%s]", org_path, tmp_path);
185 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
188 if (g_strcmp0(tmp_path, org_path) != 0) {
190 media_content_error("symbolic link(directory)");
191 media_content_sec_debug("path[%s] real[%s]", org_path, tmp_path);
194 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
201 return MEDIA_CONTENT_ERROR_NONE;
204 static bool __is_scan_ignore_exist(const char *path)
206 const char *scan_ignore = ".scan_ignore";
207 char *ignore_path = NULL;
208 gboolean result = FALSE;
210 if (!STRING_VALID(path))
213 ignore_path = g_build_path(G_DIR_SEPARATOR_S, path, scan_ignore, NULL);
214 result = g_file_test(ignore_path, G_FILE_TEST_EXISTS);
217 media_content_error("scan ignore file exist [%s]", ignore_path);
219 SAFE_FREE(ignore_path);
224 int _media_util_check_ignore_dir(const char *dir_path, bool *ignore)
226 int ret = MEDIA_CONTENT_ERROR_NONE;
227 ms_user_storage_type_e storage_type = MS_USER_STORAGE_INTERNAL;
229 media_content_retvm_if(!STRING_VALID(dir_path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid dir_path");
230 media_content_sec_debug("dir_path : %s", dir_path);
233 /*1. Check Hidden Directory*/
234 if (strstr(dir_path, "/.") != NULL) {
236 media_content_error("hidden path");
237 return MEDIA_CONTENT_ERROR_NONE;
240 /*2. Check Scan Ignore Directory*/
241 ret = ms_user_get_storage_type(_content_get_uid(), dir_path, &storage_type);
242 if (ret != MS_MEDIA_ERR_NONE) {
243 media_content_error("ms_user_get_storage_type failed : %d", ret);
244 return _content_error_capi(ret);
247 char *leaf_path = NULL;
248 char search_path[MAX_PATH_LEN] = {0, };
250 memset(search_path, 0, sizeof(search_path));
251 if (!SAFE_STRLCPY(search_path, dir_path, sizeof(search_path))) {
252 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
253 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
256 while (STRING_VALID(search_path)) {
257 if ((*ignore = __is_scan_ignore_exist(search_path)))
260 leaf_path = strrchr(search_path, '/');
264 search_path[leaf_path - search_path] = '\0';
267 return MEDIA_CONTENT_ERROR_NONE;
270 int _media_content_check_dir(const char *path)
275 #ifndef _USE_TVPD_MODE
276 char result_path[MAX_PATH_LEN] = {0, };
280 if (errno == EACCES || errno == EPERM) {
281 media_content_stderror("open dir fail");
282 media_content_sec_error("path [%s]", path);
283 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
285 media_content_stderror("open dir fail");
286 media_content_sec_error("path [%s]", path);
287 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
293 /* Check symbolic link directory */
294 real = realpath(path, NULL);
295 /* Get trimmed path */
296 _media_util_trim_path(path, &origin);
298 #ifdef _USE_TVPD_MODE
299 if (g_strcmp0(real, origin) != 0) {
300 media_content_error("symbolic link(directory)");
301 media_content_sec_debug("path[%s] real[%s]", origin, real);
304 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
307 if (g_str_has_prefix(real, MEDIA_SHARE_PATH)) {
308 /* If shared dirctory, it should be change path to TZ_USER_SHARE from realpath */
309 snprintf(result_path, MAX_PATH_LEN, "%s%s", tzplatform_getenv(TZ_USER_MEDIASHARED), real + strlen(MEDIA_SHARE_PATH));
310 if (g_strcmp0(result_path, origin) != 0) {
311 media_content_error("symbolic link(directory)");
312 media_content_sec_debug("path[%s] real[%s]", origin, real);
315 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
318 if (g_strcmp0(real, origin) != 0) {
319 media_content_error("symbolic link(directory)");
320 media_content_sec_debug("path[%s] real[%s]", origin, real);
323 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
331 return MEDIA_CONTENT_ERROR_NONE;
335 int _media_content_replace_path_in_condition(const char *condition, char *replace_condition, bool replace)
337 int ret = MEDIA_CONTENT_ERROR_NONE;
339 #ifdef _USE_TVPD_MODE
340 snprintf(replace_condition, MAX_QUERY_SIZE, "%s", condition);
342 char old_condition[MAX_QUERY_SIZE] = {0, };
343 char new_condition[MAX_QUERY_SIZE] = {0, };
345 unsigned int str_len = 0;
347 char *find_str = NULL;
348 char *to_replace_str = NULL;
350 if (replace == TRUE) { //change User session path to System session path
351 find_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL_OLD);
352 if (!STRING_VALID(find_str)) {
353 media_content_error("strdup failed");
354 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
358 to_replace_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL);
359 if (!STRING_VALID(to_replace_str)) {
360 media_content_error("Get TZ_USER_CONTENT failed");
361 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
365 find_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL);
366 if (!STRING_VALID(find_str)) {
367 media_content_error("Get TZ_USER_CONTENT failed");
368 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
372 to_replace_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL_OLD);
373 if (!STRING_VALID(to_replace_str)) {
374 media_content_error("strdup failed");
375 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
380 memset(old_condition, 0, sizeof(old_condition));
381 memset(new_condition, 0, sizeof(new_condition));
383 media_content_sec_debug("Old condition[%s]", condition);
385 if (!SAFE_STRLCPY(new_condition, condition, sizeof(new_condition))) {
386 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
387 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
391 if (g_strcmp0(find_str, to_replace_str))
392 find = strstr(new_condition, find_str);
394 while (find != NULL) {
395 str_len = find - new_condition;
397 memset(old_condition, 0, sizeof(old_condition));
398 if (!SAFE_STRLCPY(old_condition, new_condition, sizeof(old_condition))) {
399 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
400 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
403 memset(new_condition, 0, sizeof(new_condition));
405 snprintf(new_condition, str_len + 1, "%s", old_condition);
407 SAFE_STRLCAT(new_condition, to_replace_str, sizeof(new_condition));
408 SAFE_STRLCAT(new_condition, old_condition + str_len + strlen(find_str), sizeof(new_condition));
410 find = strstr(new_condition, find_str);
413 if (!SAFE_STRLCPY(replace_condition, new_condition, MAX_QUERY_SIZE)) {
414 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
415 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
419 media_content_sec_debug("repl cond[%s]", replace_condition);
421 if (!STRING_VALID(replace_condition)) {
422 media_content_error("replace failed");
423 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
429 SAFE_FREE(to_replace_str);
435 int _media_content_replace_path(const char *path, char *replace_path)
437 #ifdef _USE_TVPD_MODE
438 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
440 if (strncmp(path, MEDIA_ROOT_PATH_INTERNAL_OLD, strlen(MEDIA_ROOT_PATH_INTERNAL_OLD)) == 0) {
441 media_content_sec_debug("Old path[%s]", path);
442 snprintf(replace_path, MAX_PATH_LEN, "%s%s", MEDIA_ROOT_PATH_INTERNAL, path + strlen(MEDIA_ROOT_PATH_INTERNAL_OLD));
444 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
448 if (!STRING_VALID(replace_path)) {
449 media_content_error("replace failed");
450 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
453 return MEDIA_CONTENT_ERROR_NONE;
456 #ifdef _USE_SENIOR_MODE
457 bool _media_content_is_support_senior_mode()
459 bool bSupportSeniorMode = false;
461 if (system_info_get_value_bool(SYSTEM_INFO_KEY_GET_SENIOR_MODE_SUPPORTED, &bSupportSeniorMode) != SYSTEM_INFO_ERROR_NONE) {
462 media_content_debug("Get senior mode support failed");
465 /* media_content_debug("Senior mode Support : [%d]", bSupportSeniorMode); */
466 return bSupportSeniorMode;