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 media_content_sec_debug("path [%s]", path);
68 media_content_stderror("open file fail");
69 if (errno == EACCES || errno == EPERM)
70 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
72 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
77 return MEDIA_CONTENT_ERROR_NONE;
80 void _media_util_trim_path(const char *input_path, char **output_path)
82 char buf[4096] = {0,};
83 char tmp[4096] = {0,};
86 memset(buf, 0, sizeof(buf));
87 if (!SAFE_STRLCPY(buf, input_path, sizeof(buf)))
88 media_content_sec_debug("Truncation occurred[%d]", strlen(input_path));
90 while ((pos = strstr(buf, "//")) != NULL) {
91 memset(tmp, 0, sizeof(tmp));
92 if (!SAFE_STRLCPY(tmp, buf, pos - buf + 1))
93 media_content_sec_debug("Truncation occurred");
94 SAFE_STRLCAT(tmp, pos + 1, sizeof(tmp));
96 memset(buf, 0, sizeof(buf));
97 if (!SAFE_STRLCPY(buf, tmp, sizeof(buf)))
98 media_content_sec_debug("Truncation occurred[%d]", strlen(tmp));
101 if (g_str_has_suffix(buf, "/"))
102 *output_path = g_strndup(buf, strlen(buf) - 1);
104 *output_path = g_strdup(buf);
107 int _media_util_check_ignore_file(const char *path, bool *ignore)
109 media_content_retvm_if(!STRING_VALID(path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid path");
112 char *tmp_path = NULL;
113 char *org_path = NULL;
115 #ifndef _USE_TVPD_MODE
116 char replace[MAX_PATH_LEN] = {0, };
119 /* Check is exist (It may be the path to the deleted file) */
120 if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
121 media_content_sec_debug("removed path[%s]", path);
122 return MEDIA_CONTENT_ERROR_NONE;
125 /* Check symbolic link file */
126 if (g_file_test(path, G_FILE_TEST_IS_SYMLINK)) {
128 media_content_error("symbolic link(file)");
129 media_content_sec_debug("path : %s", path);
130 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
133 /* Check hidden path */
134 if (strstr(path, "/.") != NULL) {
136 media_content_error("hidden path");
137 media_content_sec_debug("path : %s", path);
138 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
141 /* Check symbolic directory */
142 tmp_path = realpath(path, NULL);
143 /* Get trimmed path */
144 _media_util_trim_path(path, &org_path);
146 #ifdef _USE_TVPD_MODE
147 if (g_strcmp0(tmp_path, org_path) != 0) {
149 media_content_error("symbolic link(directory)");
150 media_content_sec_debug("path[%s] real[%s]", org_path, tmp_path);
153 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
156 if (g_str_has_prefix(tmp_path, MEDIA_SHARE_PATH)) {
157 /* If shared dirctory, it should be change path to TZ_USER_SHARE from realpath */
158 snprintf(replace, MAX_PATH_LEN, "%s%s", tzplatform_getenv(TZ_USER_MEDIASHARED), tmp_path + strlen(MEDIA_SHARE_PATH));
159 if (g_strcmp0(replace, org_path) != 0) {
161 media_content_error("symbolic link(directory)");
162 media_content_sec_debug("path[%s] real[%s]", org_path, tmp_path);
165 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
168 if (g_strcmp0(tmp_path, org_path) != 0) {
170 media_content_error("symbolic link(directory)");
171 media_content_sec_debug("path[%s] real[%s]", org_path, tmp_path);
174 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
181 return MEDIA_CONTENT_ERROR_NONE;
184 int _media_util_check_ignore_dir(const char *dir_path, bool *ignore)
186 int ret = MEDIA_CONTENT_ERROR_NONE;
187 media_svc_storage_type_e storage_type = 0;
188 const char *scan_ignore = ".scan_ignore";
191 GError *error = NULL;
194 media_content_sec_debug("dir_path : %s", dir_path);
196 media_content_retvm_if(!STRING_VALID(dir_path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid dir_path");
199 /*1. Check Hidden Directory*/
200 if (strstr(dir_path, "/.") != NULL) {
202 media_content_error("hidden path");
203 return MEDIA_CONTENT_ERROR_NONE;
206 /*2. Check Scan Ignore Directory*/
207 ret = media_svc_get_storage_type(dir_path, &storage_type, _content_get_uid());
208 if (ret != MS_MEDIA_ERR_NONE) {
209 media_content_error("media_svc_get_storage_type failed : %d", ret);
210 return _content_error_capi(MEDIA_CONTENT_TYPE, ret);
213 char *leaf_path = NULL;
214 char search_path[MAX_PATH_LEN] = {0, };
216 memset(search_path, 0, sizeof(search_path));
217 if (!SAFE_STRLCPY(search_path, dir_path, sizeof(search_path))) {
218 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
219 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
222 while (STRING_VALID(search_path)) {
223 dir = g_dir_open(search_path, 0, &error);
224 if (dir != NULL && error == NULL) {
225 while ((name = g_dir_read_name(dir))) {
226 if (g_strcmp0(name, scan_ignore) == 0) {
227 media_content_sec_debug("Ignore path[%s]", search_path);
234 media_content_error("Open Directory fail");
235 if (error->code == G_FILE_ERROR_ACCES) {
237 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
240 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
251 /*If root path, Stop Scanning*/
252 if ((storage_type == MEDIA_SVC_STORAGE_INTERNAL) && (STRING_VALID(MEDIA_ROOT_PATH_INTERNAL) && g_strcmp0(search_path, MEDIA_ROOT_PATH_INTERNAL) == 0)) {
254 } else if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL) && (STRING_VALID(MEDIA_ROOT_PATH_SDCARD)) && (g_strcmp0(search_path, MEDIA_ROOT_PATH_SDCARD) == 0)) {
256 } else if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) && (STRING_VALID(MEDIA_ROOT_PATH_DISC)) && (g_strcmp0(search_path, MEDIA_ROOT_PATH_DISC) == 0)) {
258 } else if (storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) {
259 char *parent_folder_path = NULL;
260 bool is_root = FALSE;
262 parent_folder_path = g_path_get_dirname(search_path);
263 if (STRING_VALID(MEDIA_ROOT_PATH_USB) && STRING_VALID(parent_folder_path) && (g_strcmp0(parent_folder_path, MEDIA_ROOT_PATH_USB) == 0))
266 SAFE_FREE(parent_folder_path);
271 #ifdef _USE_SENIOR_MODE
272 if (_media_content_is_support_senior_mode()) {
273 if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL) && (g_strcmp0(search_path, MEDIA_ROOT_PATH_SENIOR_MODE) == 0))
278 leaf_path = strrchr(search_path, '/');
279 if (leaf_path != NULL) {
280 int seek_len = leaf_path -search_path;
281 search_path[seek_len] = '\0';
282 /*media_content_sec_debug("go to other dir [%s]", search_path);*/
284 media_content_debug("Fail to find leaf path");
290 return MEDIA_CONTENT_ERROR_NONE;
293 int _media_content_check_dir(const char *path)
298 #ifndef _USE_TVPD_MODE
299 char result_path[MAX_PATH_LEN] = {0, };
303 media_content_sec_error("path [%s]", path);
304 media_content_stderror("open dir fail");
306 if (errno == EACCES || errno == EPERM)
307 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
309 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
314 /* Check symbolic link directory */
315 real = realpath(path, NULL);
316 /* Get trimmed path */
317 _media_util_trim_path(path, &origin);
319 #ifdef _USE_TVPD_MODE
320 if (g_strcmp0(real, origin) != 0) {
321 media_content_error("symbolic link(directory)");
322 media_content_sec_debug("path[%s] real[%s]", origin, real);
325 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
328 if (g_str_has_prefix(real, MEDIA_SHARE_PATH)) {
329 /* If shared dirctory, it should be change path to TZ_USER_SHARE from realpath */
330 snprintf(result_path, MAX_PATH_LEN, "%s%s", tzplatform_getenv(TZ_USER_MEDIASHARED), real + strlen(MEDIA_SHARE_PATH));
331 if (g_strcmp0(result_path, origin) != 0) {
332 media_content_error("symbolic link(directory)");
333 media_content_sec_debug("path[%s] real[%s]", origin, real);
336 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
339 if (g_strcmp0(real, origin) != 0) {
340 media_content_error("symbolic link(directory)");
341 media_content_sec_debug("path[%s] real[%s]", origin, real);
344 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
352 return MEDIA_CONTENT_ERROR_NONE;
356 int _media_content_replace_path_in_condition(const char *condition, char *replace_condition, bool replace)
358 int ret = MEDIA_CONTENT_ERROR_NONE;
360 #ifdef _USE_TVPD_MODE
361 snprintf(replace_condition, MAX_QUERY_SIZE, "%s", condition);
363 char old_condition[MAX_QUERY_SIZE] = {0, };
364 char new_condition[MAX_QUERY_SIZE] = {0, };
366 unsigned int str_len = 0;
368 char *find_str = NULL;
369 char *to_replace_str = NULL;
371 if (replace == TRUE) { //change User session path to System session path
372 find_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL_OLD);
373 if (!STRING_VALID(find_str)) {
374 media_content_error("strdup failed");
375 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
379 to_replace_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL);
380 if (!STRING_VALID(to_replace_str)) {
381 media_content_error("Get TZ_USER_CONTENT failed");
382 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
386 find_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL);
387 if (!STRING_VALID(find_str)) {
388 media_content_error("Get TZ_USER_CONTENT failed");
389 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
393 to_replace_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL_OLD);
394 if (!STRING_VALID(to_replace_str)) {
395 media_content_error("strdup failed");
396 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
401 memset(old_condition, 0, sizeof(old_condition));
402 memset(new_condition, 0, sizeof(new_condition));
404 media_content_sec_debug("Old condition[%s]", condition);
406 if (!SAFE_STRLCPY(new_condition, condition, sizeof(new_condition))) {
407 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
408 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
412 if (g_strcmp0(find_str, to_replace_str))
413 find = strstr(new_condition, find_str);
415 while (find != NULL) {
416 str_len = find - new_condition;
418 memset(old_condition, 0, sizeof(old_condition));
419 if (!SAFE_STRLCPY(old_condition, new_condition, sizeof(old_condition))) {
420 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
421 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
424 memset(new_condition, 0, sizeof(new_condition));
426 snprintf(new_condition, str_len + 1, "%s", old_condition);
428 SAFE_STRLCAT(new_condition, to_replace_str, sizeof(new_condition));
429 SAFE_STRLCAT(new_condition, old_condition + str_len + strlen(find_str), sizeof(new_condition));
431 find = strstr(new_condition, find_str);
434 if (!SAFE_STRLCPY(replace_condition, new_condition, MAX_QUERY_SIZE)) {
435 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
436 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
440 media_content_sec_debug("repl cond[%s]", replace_condition);
442 if (!STRING_VALID(replace_condition)) {
443 media_content_error("replace failed");
444 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
450 SAFE_FREE(to_replace_str);
456 int _media_content_replace_path(const char *path, char *replace_path)
458 #ifdef _USE_TVPD_MODE
459 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
461 if (strncmp(path, MEDIA_ROOT_PATH_INTERNAL_OLD, strlen(MEDIA_ROOT_PATH_INTERNAL_OLD)) == 0) {
462 media_content_sec_debug("Old path[%s]", path);
463 snprintf(replace_path, MAX_PATH_LEN, "%s%s", MEDIA_ROOT_PATH_INTERNAL, path + strlen(MEDIA_ROOT_PATH_INTERNAL_OLD));
465 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
469 if (!STRING_VALID(replace_path)) {
470 media_content_error("replace failed");
471 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
474 return MEDIA_CONTENT_ERROR_NONE;
477 #ifdef _USE_SENIOR_MODE
478 bool _media_content_is_support_senior_mode()
480 bool bSupportSeniorMode = false;
482 if (system_info_get_value_bool(SYSTEM_INFO_KEY_GET_SENIOR_MODE_SUPPORTED, &bSupportSeniorMode) != SYSTEM_INFO_ERROR_NONE) {
483 media_content_debug("Get senior mode support failed");
486 /* media_content_debug("Senior mode Support : [%d]", bSupportSeniorMode); */
487 return bSupportSeniorMode;