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>
24 #ifdef _USE_SENIOR_MODE
25 #include <media_util_private.h>
28 static int MEDIA_CONTENT_OTHER_SUPPORT = -1;
30 bool _media_util_check_support_media_type(const char *path)
32 int ret = SYSTEM_INFO_ERROR_NONE;
34 bool is_supported = false;
36 media_content_retvm_if(!STRING_VALID(path), false, "path is empty");
38 if (MEDIA_CONTENT_OTHER_SUPPORT == -1) {
39 ret = system_info_get_platform_bool("http://tizen.org/feature/content.scanning.others", &is_supported);
40 if (ret != SYSTEM_INFO_ERROR_NONE) {
41 media_content_debug("SYSTEM_INFO_ERROR: content.scanning.others [%d]", ret);
45 MEDIA_CONTENT_OTHER_SUPPORT = is_supported;
48 /* If not, check media type */
49 if (!MEDIA_CONTENT_OTHER_SUPPORT) {
50 ret = media_svc_get_media_type(path, &media_type);
51 media_content_retvm_if(ret != MS_MEDIA_ERR_NONE, false, "Failed to get media type");
53 if (media_type == MEDIA_CONTENT_TYPE_OTHERS)
60 int _media_util_check_file_exist(const char *path)
64 /* check the file exits actually */
65 exist = open(path, O_RDONLY);
67 if (errno == EACCES || errno == EPERM) {
68 media_content_stderror("open file fail");
69 media_content_sec_debug("path [%s]", path);
70 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
72 media_content_stderror("open file fail");
73 media_content_sec_debug("path [%s]", path);
74 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
80 return MEDIA_CONTENT_ERROR_NONE;
83 void _media_util_trim_path(const char *input_path, char **output_path)
85 char buf[4096] = {0,};
86 char tmp[4096] = {0,};
89 memset(buf, 0, sizeof(buf));
90 if (!SAFE_STRLCPY(buf, input_path, sizeof(buf)))
91 media_content_sec_debug("Truncation occurred[%zu]", strlen(input_path));
93 while ((pos = strstr(buf, "//")) != NULL) {
94 memset(tmp, 0, sizeof(tmp));
95 if (!SAFE_STRLCPY(tmp, buf, pos - buf + 1))
96 media_content_sec_debug("Truncation occurred");
97 SAFE_STRLCAT(tmp, pos + 1, sizeof(tmp));
99 memset(buf, 0, sizeof(buf));
100 if (!SAFE_STRLCPY(buf, tmp, sizeof(buf)))
101 media_content_sec_debug("Truncation occurred[%zu]", strlen(tmp));
104 if (g_str_has_suffix(buf, "/"))
105 *output_path = g_strndup(buf, strlen(buf) - 1);
107 *output_path = g_strdup(buf);
110 int _media_util_check_ignore_file(const char *path, bool *ignore)
112 media_content_retvm_if(!STRING_VALID(path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid path");
115 char *tmp_path = NULL;
116 char *org_path = NULL;
118 #ifndef _USE_TVPD_MODE
119 char replace[MAX_PATH_LEN] = {0, };
122 /* Check is exist (It may be the path to the deleted file) */
123 if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
124 media_content_sec_debug("removed path[%s]", path);
125 return MEDIA_CONTENT_ERROR_NONE;
128 /* Check symbolic link file */
129 if (g_file_test(path, G_FILE_TEST_IS_SYMLINK)) {
131 media_content_error("symbolic link(file)");
132 media_content_sec_debug("path : %s", path);
133 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
136 /* Check hidden path */
137 if (strstr(path, "/.") != NULL) {
139 media_content_error("hidden path");
140 media_content_sec_debug("path : %s", path);
141 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
144 /* Check symbolic directory */
145 tmp_path = realpath(path, NULL);
146 /* Get trimmed path */
147 _media_util_trim_path(path, &org_path);
149 #ifdef _USE_TVPD_MODE
150 if (g_strcmp0(tmp_path, org_path) != 0) {
152 media_content_error("symbolic link(directory)");
153 media_content_sec_debug("path[%s] real[%s]", org_path, tmp_path);
156 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
159 if (g_str_has_prefix(tmp_path, MEDIA_SHARE_PATH)) {
160 /* If shared dirctory, it should be change path to TZ_USER_SHARE from realpath */
161 snprintf(replace, MAX_PATH_LEN, "%s%s", tzplatform_getenv(TZ_USER_MEDIASHARED), tmp_path + strlen(MEDIA_SHARE_PATH));
162 if (g_strcmp0(replace, org_path) != 0) {
164 media_content_error("symbolic link(directory)");
165 media_content_sec_debug("path[%s] real[%s]", org_path, tmp_path);
168 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
171 if (g_strcmp0(tmp_path, org_path) != 0) {
173 media_content_error("symbolic link(directory)");
174 media_content_sec_debug("path[%s] real[%s]", org_path, tmp_path);
177 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
184 return MEDIA_CONTENT_ERROR_NONE;
187 int _media_util_check_ignore_dir(const char *dir_path, bool *ignore)
189 int ret = MEDIA_CONTENT_ERROR_NONE;
190 ms_user_storage_type_e storage_type = MS_USER_STORAGE_INTERNAL;
191 const char *scan_ignore = ".scan_ignore";
194 GError *error = NULL;
197 media_content_sec_debug("dir_path : %s", dir_path);
199 media_content_retvm_if(!STRING_VALID(dir_path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid dir_path");
202 /*1. Check Hidden Directory*/
203 if (strstr(dir_path, "/.") != NULL) {
205 media_content_error("hidden path");
206 return MEDIA_CONTENT_ERROR_NONE;
209 /*2. Check Scan Ignore Directory*/
210 ret = ms_user_get_storage_type(_content_get_uid(), dir_path, &storage_type);
211 if (ret != MS_MEDIA_ERR_NONE) {
212 media_content_error("ms_user_get_storage_type failed : %d", ret);
213 return _content_error_capi(MEDIA_CONTENT_TYPE, ret);
216 char *leaf_path = NULL;
217 char search_path[MAX_PATH_LEN] = {0, };
219 memset(search_path, 0, sizeof(search_path));
220 if (!SAFE_STRLCPY(search_path, dir_path, sizeof(search_path))) {
221 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
222 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
225 while (STRING_VALID(search_path)) {
226 dir = g_dir_open(search_path, 0, &error);
227 if (dir != NULL && error == NULL) {
228 while ((name = g_dir_read_name(dir))) {
229 if (g_strcmp0(name, scan_ignore) == 0) {
230 media_content_sec_debug("Ignore path[%s]", search_path);
237 media_content_error("Open Directory fail");
238 if (error->code == G_FILE_ERROR_ACCES) {
240 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
243 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
254 /*If root path, Stop Scanning*/
255 if ((storage_type == MS_USER_STORAGE_INTERNAL) && (STRING_VALID(MEDIA_ROOT_PATH_INTERNAL) && g_strcmp0(search_path, MEDIA_ROOT_PATH_INTERNAL) == 0)) {
257 } else if ((storage_type == MS_USER_STORAGE_EXTERNAL) && (STRING_VALID(MEDIA_ROOT_PATH_SDCARD)) && (g_strcmp0(search_path, MEDIA_ROOT_PATH_SDCARD) == 0)) {
259 } else if ((storage_type == MS_USER_STORAGE_EXTERNAL_USB) && (STRING_VALID(MEDIA_ROOT_PATH_DISC)) && (g_strcmp0(search_path, MEDIA_ROOT_PATH_DISC) == 0)) {
261 } else if (storage_type == MS_USER_STORAGE_EXTERNAL_USB) {
262 char *parent_folder_path = NULL;
263 bool is_root = FALSE;
265 parent_folder_path = g_path_get_dirname(search_path);
266 if (STRING_VALID(MEDIA_ROOT_PATH_USB) && STRING_VALID(parent_folder_path) && (g_strcmp0(parent_folder_path, MEDIA_ROOT_PATH_USB) == 0))
269 SAFE_FREE(parent_folder_path);
274 #ifdef _USE_SENIOR_MODE
275 if (_media_content_is_support_senior_mode()) {
276 if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL) && (g_strcmp0(search_path, MEDIA_ROOT_PATH_SENIOR_MODE) == 0))
281 leaf_path = strrchr(search_path, '/');
282 if (leaf_path != NULL) {
283 int seek_len = leaf_path -search_path;
284 search_path[seek_len] = '\0';
285 /*media_content_sec_debug("go to other dir [%s]", search_path);*/
287 media_content_debug("Fail to find leaf path");
293 return MEDIA_CONTENT_ERROR_NONE;
296 int _media_content_check_dir(const char *path)
301 #ifndef _USE_TVPD_MODE
302 char result_path[MAX_PATH_LEN] = {0, };
306 if (errno == EACCES || errno == EPERM) {
307 media_content_stderror("open dir fail");
308 media_content_sec_error("path [%s]", path);
309 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
311 media_content_stderror("open dir fail");
312 media_content_sec_error("path [%s]", path);
313 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
319 /* Check symbolic link directory */
320 real = realpath(path, NULL);
321 /* Get trimmed path */
322 _media_util_trim_path(path, &origin);
324 #ifdef _USE_TVPD_MODE
325 if (g_strcmp0(real, origin) != 0) {
326 media_content_error("symbolic link(directory)");
327 media_content_sec_debug("path[%s] real[%s]", origin, real);
330 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
333 if (g_str_has_prefix(real, MEDIA_SHARE_PATH)) {
334 /* If shared dirctory, it should be change path to TZ_USER_SHARE from realpath */
335 snprintf(result_path, MAX_PATH_LEN, "%s%s", tzplatform_getenv(TZ_USER_MEDIASHARED), real + strlen(MEDIA_SHARE_PATH));
336 if (g_strcmp0(result_path, origin) != 0) {
337 media_content_error("symbolic link(directory)");
338 media_content_sec_debug("path[%s] real[%s]", origin, real);
341 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
344 if (g_strcmp0(real, origin) != 0) {
345 media_content_error("symbolic link(directory)");
346 media_content_sec_debug("path[%s] real[%s]", origin, real);
349 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
357 return MEDIA_CONTENT_ERROR_NONE;
361 int _media_content_replace_path_in_condition(const char *condition, char *replace_condition, bool replace)
363 int ret = MEDIA_CONTENT_ERROR_NONE;
365 #ifdef _USE_TVPD_MODE
366 snprintf(replace_condition, MAX_QUERY_SIZE, "%s", condition);
368 char old_condition[MAX_QUERY_SIZE] = {0, };
369 char new_condition[MAX_QUERY_SIZE] = {0, };
371 unsigned int str_len = 0;
373 char *find_str = NULL;
374 char *to_replace_str = NULL;
376 if (replace == TRUE) { //change User session path to System session path
377 find_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL_OLD);
378 if (!STRING_VALID(find_str)) {
379 media_content_error("strdup failed");
380 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
384 to_replace_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL);
385 if (!STRING_VALID(to_replace_str)) {
386 media_content_error("Get TZ_USER_CONTENT failed");
387 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
391 find_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL);
392 if (!STRING_VALID(find_str)) {
393 media_content_error("Get TZ_USER_CONTENT failed");
394 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
398 to_replace_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL_OLD);
399 if (!STRING_VALID(to_replace_str)) {
400 media_content_error("strdup failed");
401 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
406 memset(old_condition, 0, sizeof(old_condition));
407 memset(new_condition, 0, sizeof(new_condition));
409 media_content_sec_debug("Old condition[%s]", condition);
411 if (!SAFE_STRLCPY(new_condition, condition, sizeof(new_condition))) {
412 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
413 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
417 if (g_strcmp0(find_str, to_replace_str))
418 find = strstr(new_condition, find_str);
420 while (find != NULL) {
421 str_len = find - new_condition;
423 memset(old_condition, 0, sizeof(old_condition));
424 if (!SAFE_STRLCPY(old_condition, new_condition, sizeof(old_condition))) {
425 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
426 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
429 memset(new_condition, 0, sizeof(new_condition));
431 snprintf(new_condition, str_len + 1, "%s", old_condition);
433 SAFE_STRLCAT(new_condition, to_replace_str, sizeof(new_condition));
434 SAFE_STRLCAT(new_condition, old_condition + str_len + strlen(find_str), sizeof(new_condition));
436 find = strstr(new_condition, find_str);
439 if (!SAFE_STRLCPY(replace_condition, new_condition, MAX_QUERY_SIZE)) {
440 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
441 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
445 media_content_sec_debug("repl cond[%s]", replace_condition);
447 if (!STRING_VALID(replace_condition)) {
448 media_content_error("replace failed");
449 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
455 SAFE_FREE(to_replace_str);
461 int _media_content_replace_path(const char *path, char *replace_path)
463 #ifdef _USE_TVPD_MODE
464 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
466 if (strncmp(path, MEDIA_ROOT_PATH_INTERNAL_OLD, strlen(MEDIA_ROOT_PATH_INTERNAL_OLD)) == 0) {
467 media_content_sec_debug("Old path[%s]", path);
468 snprintf(replace_path, MAX_PATH_LEN, "%s%s", MEDIA_ROOT_PATH_INTERNAL, path + strlen(MEDIA_ROOT_PATH_INTERNAL_OLD));
470 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
474 if (!STRING_VALID(replace_path)) {
475 media_content_error("replace failed");
476 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
479 return MEDIA_CONTENT_ERROR_NONE;
482 #ifdef _USE_SENIOR_MODE
483 bool _media_content_is_support_senior_mode()
485 bool bSupportSeniorMode = false;
487 if (system_info_get_value_bool(SYSTEM_INFO_KEY_GET_SENIOR_MODE_SUPPORTED, &bSupportSeniorMode) != SYSTEM_INFO_ERROR_NONE) {
488 media_content_debug("Get senior mode support failed");
491 /* media_content_debug("Senior mode Support : [%d]", bSupportSeniorMode); */
492 return bSupportSeniorMode;