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>
23 #ifdef _USE_SENIOR_MODE
24 #include <media_util_private.h>
27 static int MEDIA_CONTENT_OTHER_SUPPORT = -1;
29 bool _media_util_check_support_media_type(const char *path)
31 int ret = SYSTEM_INFO_ERROR_NONE;
33 bool is_supported = false;
35 media_content_retvm_if(!STRING_VALID(path), false, "path is empty");
37 if (MEDIA_CONTENT_OTHER_SUPPORT == -1) {
38 ret = system_info_get_platform_bool("http://tizen.org/feature/content.scanning.others", &is_supported);
39 if (ret != SYSTEM_INFO_ERROR_NONE) {
40 media_content_debug("SYSTEM_INFO_ERROR: content.scanning.others [%d]", ret);
44 MEDIA_CONTENT_OTHER_SUPPORT = is_supported;
47 /* If not, check media type */
48 if (!MEDIA_CONTENT_OTHER_SUPPORT) {
49 ret = media_svc_get_media_type(path, &media_type);
50 media_content_retvm_if(ret != MS_MEDIA_ERR_NONE, false, "Failed to get media type");
52 if (media_type == MEDIA_CONTENT_TYPE_OTHERS)
59 int _media_util_check_file_exist(const char *path)
63 /* check the file exits actually */
64 exist = open(path, O_RDONLY);
66 media_content_sec_debug("path [%s]", path);
67 media_content_stderror("open file fail");
68 if (errno == EACCES || errno == EPERM)
69 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
71 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
76 return MEDIA_CONTENT_ERROR_NONE;
79 void _media_util_trim_path(const char *input_path, char **output_path)
81 char buf[4096] = {0,};
82 char tmp[4096] = {0,};
85 memset(buf, 0, sizeof(buf));
86 if (!SAFE_STRLCPY(buf, input_path, sizeof(buf)))
87 media_content_sec_debug("Truncation occurred[%d]", strlen(input_path));
89 while ((pos = strstr(buf, "//")) != NULL) {
90 memset(tmp, 0, sizeof(tmp));
91 if (!SAFE_STRLCPY(tmp, buf, pos - buf + 1))
92 media_content_sec_debug("Truncation occurred");
93 SAFE_STRLCAT(tmp, pos + 1, sizeof(tmp));
95 memset(buf, 0, sizeof(buf));
96 if (!SAFE_STRLCPY(buf, tmp, sizeof(buf)))
97 media_content_sec_debug("Truncation occurred[%d]", strlen(tmp));
100 if (g_str_has_suffix(buf, "/"))
101 *output_path = g_strndup(buf, strlen(buf) - 1);
103 *output_path = g_strdup(buf);
106 int _media_util_check_ignore_file(const char *path, bool *ignore)
108 media_content_retvm_if(!STRING_VALID(path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid path");
111 char *tmp_path = NULL;
112 char *org_path = NULL;
114 #ifndef _USE_TVPD_MODE
115 char replace[MAX_PATH_LEN] = {0, };
118 /* Check is exist (It may be the path to the deleted file) */
119 if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
120 media_content_sec_debug("removed path[%s]", path);
121 return MEDIA_CONTENT_ERROR_NONE;
124 /* Check symbolic link file */
125 if (g_file_test(path, G_FILE_TEST_IS_SYMLINK)) {
127 media_content_error("symbolic link(file)");
128 media_content_sec_debug("path : %s", path);
129 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
132 /* Check hidden path */
133 if (strstr(path, "/.") != NULL) {
135 media_content_error("hidden path");
136 media_content_sec_debug("path : %s", path);
137 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
140 /* Check symbolic directory */
141 tmp_path = realpath(path, NULL);
142 /* Get trimmed path */
143 _media_util_trim_path(path, &org_path);
145 #ifdef _USE_TVPD_MODE
146 if (g_strcmp0(tmp_path, org_path) != 0) {
148 media_content_error("symbolic link(directory)");
149 media_content_sec_debug("path[%s] real[%s]", org_path, tmp_path);
152 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
155 if (g_str_has_prefix(tmp_path, MEDIA_SHARE_PATH)) {
156 /* If shared dirctory, it should be change path to TZ_USER_SHARE from realpath */
157 snprintf(replace, MAX_PATH_LEN, "%s%s", tzplatform_getenv(TZ_USER_MEDIASHARED), tmp_path + strlen(MEDIA_SHARE_PATH));
158 if (g_strcmp0(replace, org_path) != 0) {
160 media_content_error("symbolic link(directory)");
161 media_content_sec_debug("path[%s] real[%s]", org_path, tmp_path);
164 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
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;
180 return MEDIA_CONTENT_ERROR_NONE;
183 int _media_util_check_ignore_dir(const char *dir_path, bool *ignore)
185 int ret = MEDIA_CONTENT_ERROR_NONE;
186 media_svc_storage_type_e storage_type = 0;
187 const char *scan_ignore = ".scan_ignore";
190 GError *error = NULL;
193 media_content_sec_debug("dir_path : %s", dir_path);
195 media_content_retvm_if(!STRING_VALID(dir_path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid dir_path");
198 /*1. Check Hidden Directory*/
199 if (strstr(dir_path, "/.") != NULL) {
201 media_content_error("hidden path");
202 return MEDIA_CONTENT_ERROR_NONE;
205 /*2. Check Scan Ignore Directory*/
206 ret = media_svc_get_storage_type(dir_path, &storage_type, _content_get_uid());
207 if (ret != MS_MEDIA_ERR_NONE) {
208 media_content_error("media_svc_get_storage_type failed : %d", ret);
209 return _content_error_capi(MEDIA_CONTENT_TYPE, ret);
212 char *leaf_path = NULL;
213 char search_path[MAX_PATH_LEN] = {0, };
215 memset(search_path, 0, sizeof(search_path));
216 if (!SAFE_STRLCPY(search_path, dir_path, sizeof(search_path))) {
217 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
218 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
221 while (STRING_VALID(search_path)) {
222 dir = g_dir_open(search_path, 0, &error);
223 if (dir != NULL && error == NULL) {
224 while ((name = g_dir_read_name(dir))) {
225 if (g_strcmp0(name, scan_ignore) == 0) {
226 media_content_sec_debug("Ignore path[%s]", search_path);
233 media_content_error("Open Directory fail");
234 if (error->code == G_FILE_ERROR_ACCES) {
236 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
239 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
250 /*If root path, Stop Scanning*/
251 if ((storage_type == MEDIA_SVC_STORAGE_INTERNAL) && (STRING_VALID(MEDIA_ROOT_PATH_INTERNAL) && g_strcmp0(search_path, MEDIA_ROOT_PATH_INTERNAL) == 0)) {
253 } else if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL) && (STRING_VALID(MEDIA_ROOT_PATH_SDCARD)) && (g_strcmp0(search_path, MEDIA_ROOT_PATH_SDCARD) == 0)) {
255 } else if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) && (STRING_VALID(MEDIA_ROOT_PATH_DISC)) && (g_strcmp0(search_path, MEDIA_ROOT_PATH_DISC) == 0)) {
257 } else if (storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) {
258 char *parent_folder_path = NULL;
259 bool is_root = FALSE;
261 parent_folder_path = g_path_get_dirname(search_path);
262 if (STRING_VALID(MEDIA_ROOT_PATH_USB) && STRING_VALID(parent_folder_path) && (g_strcmp0(parent_folder_path, MEDIA_ROOT_PATH_USB) == 0))
265 SAFE_FREE(parent_folder_path);
270 #ifdef _USE_SENIOR_MODE
271 if (_media_content_is_support_senior_mode()) {
272 if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL) && (g_strcmp0(search_path, MEDIA_ROOT_PATH_SENIOR_MODE) == 0))
277 leaf_path = strrchr(search_path, '/');
278 if (leaf_path != NULL) {
279 int seek_len = leaf_path -search_path;
280 search_path[seek_len] = '\0';
281 /*media_content_sec_debug("go to other dir [%s]", search_path);*/
283 media_content_debug("Fail to find leaf path");
289 return MEDIA_CONTENT_ERROR_NONE;
292 int _media_content_check_dir(const char *path)
297 #ifndef _USE_TVPD_MODE
298 char result_path[MAX_PATH_LEN] = {0, };
302 media_content_sec_error("path [%s]", path);
303 media_content_stderror("open dir fail");
305 if (errno == EACCES || errno == EPERM)
306 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
308 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
313 /* Check symbolic link directory */
314 real = realpath(path, NULL);
315 /* Get trimmed path */
316 _media_util_trim_path(path, &origin);
318 #ifdef _USE_TVPD_MODE
319 if (g_strcmp0(real, origin) != 0) {
320 media_content_error("symbolic link(directory)");
321 media_content_sec_debug("path[%s] real[%s]", origin, real);
324 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
327 if (g_str_has_prefix(real, MEDIA_SHARE_PATH)) {
328 /* If shared dirctory, it should be change path to TZ_USER_SHARE from realpath */
329 snprintf(result_path, MAX_PATH_LEN, "%s%s", tzplatform_getenv(TZ_USER_MEDIASHARED), real + strlen(MEDIA_SHARE_PATH));
330 if (g_strcmp0(result_path, origin) != 0) {
331 media_content_error("symbolic link(directory)");
332 media_content_sec_debug("path[%s] real[%s]", origin, real);
335 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
338 if (g_strcmp0(real, origin) != 0) {
339 media_content_error("symbolic link(directory)");
340 media_content_sec_debug("path[%s] real[%s]", origin, real);
343 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
351 return MEDIA_CONTENT_ERROR_NONE;
355 int _media_content_replace_path_in_condition(const char *condition, char *replace_condition, bool replace)
357 int ret = MEDIA_CONTENT_ERROR_NONE;
359 #ifdef _USE_TVPD_MODE
360 snprintf(replace_condition, MAX_QUERY_SIZE, "%s", condition);
362 char old_condition[MAX_QUERY_SIZE] = {0, };
363 char new_condition[MAX_QUERY_SIZE] = {0, };
365 unsigned int str_len = 0;
367 char *find_str = NULL;
368 char *to_replace_str = NULL;
370 if (replace == TRUE) { //change User session path to System session path
371 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &find_str);
372 if (ret != STORAGE_ERROR_NONE) {
373 media_content_error("storage_get_root_directory failed");
374 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
378 to_replace_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL);
379 if (!STRING_VALID(to_replace_str)) {
380 media_content_error("Get TZ_USER_CONTENT failed");
381 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
385 find_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL);
386 if (!STRING_VALID(find_str)) {
387 media_content_error("Get TZ_USER_CONTENT failed");
388 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
392 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &to_replace_str);
393 if (ret != STORAGE_ERROR_NONE) {
394 media_content_error("storage_get_root_directory failed");
395 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
400 memset(old_condition, 0, sizeof(old_condition));
401 memset(new_condition, 0, sizeof(new_condition));
403 media_content_sec_debug("Old condition[%s]", condition);
405 if (!SAFE_STRLCPY(new_condition, condition, sizeof(new_condition))) {
406 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
407 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
411 if (g_strcmp0(find_str, to_replace_str))
412 find = strstr(new_condition, find_str);
414 while (find != NULL) {
415 str_len = find - new_condition;
417 memset(old_condition, 0, sizeof(old_condition));
418 if (!SAFE_STRLCPY(old_condition, new_condition, sizeof(old_condition))) {
419 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
420 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
423 memset(new_condition, 0, sizeof(new_condition));
425 snprintf(new_condition, str_len + 1, "%s", old_condition);
427 SAFE_STRLCAT(new_condition, to_replace_str, sizeof(new_condition));
428 SAFE_STRLCAT(new_condition, old_condition + str_len + strlen(find_str), sizeof(new_condition));
430 find = strstr(new_condition, find_str);
433 if (!SAFE_STRLCPY(replace_condition, new_condition, MAX_QUERY_SIZE)) {
434 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
435 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
439 media_content_sec_debug("repl cond[%s]", replace_condition);
441 if (!STRING_VALID(replace_condition)) {
442 media_content_error("replace failed");
443 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
449 SAFE_FREE(to_replace_str);
455 int _media_content_replace_path(const char *path, char *replace_path)
457 #ifdef _USE_TVPD_MODE
458 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
461 int ret = MEDIA_CONTENT_ERROR_NONE;
462 char *old_internal_path = NULL;
464 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &old_internal_path);
465 if (ret != STORAGE_ERROR_NONE) {
466 media_content_error("storage_get_root_directory failed");
467 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
470 if (strncmp(path, old_internal_path, strlen(old_internal_path)) == 0) {
471 media_content_sec_debug("Old path[%s]", path);
472 snprintf(replace_path, MAX_PATH_LEN, "%s%s", MEDIA_ROOT_PATH_INTERNAL, path + strlen(old_internal_path));
474 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
477 SAFE_FREE(old_internal_path);
480 if (!STRING_VALID(replace_path)) {
481 media_content_error("replace failed");
482 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
485 return MEDIA_CONTENT_ERROR_NONE;
488 int _media_content_rollback_path(const char *path, char *replace_path)
490 #ifdef _USE_TVPD_MODE
491 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
494 int ret = MEDIA_CONTENT_ERROR_NONE;
495 char *old_internal_path = NULL;
497 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &old_internal_path);
498 if (ret != STORAGE_ERROR_NONE) {
499 media_content_error("storage_get_root_directory failed");
500 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
503 if (strncmp(path, MEDIA_ROOT_PATH_INTERNAL, strlen(MEDIA_ROOT_PATH_INTERNAL)) == 0) {
504 media_content_sec_debug("new path[%s]", path);
505 snprintf(replace_path, MAX_PATH_LEN, "%s%s", old_internal_path, path + strlen(MEDIA_ROOT_PATH_INTERNAL));
507 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
510 SAFE_FREE(old_internal_path);
513 if (!STRING_VALID(replace_path)) {
514 media_content_error("replace failed");
515 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
518 return MEDIA_CONTENT_ERROR_NONE;
521 #ifdef _USE_SENIOR_MODE
522 bool _media_content_is_support_senior_mode()
524 bool bSupportSeniorMode = false;
526 if (system_info_get_value_bool(SYSTEM_INFO_KEY_GET_SENIOR_MODE_SUPPORTED, &bSupportSeniorMode) != SYSTEM_INFO_ERROR_NONE) {
527 media_content_debug("Get senior mode support failed");
530 /* media_content_debug("Senior mode Support : [%d]", bSupportSeniorMode); */
531 return bSupportSeniorMode;