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 int _media_util_check_ignore_dir(const char *dir_path, bool *ignore)
206 int ret = MEDIA_CONTENT_ERROR_NONE;
207 ms_user_storage_type_e storage_type = MS_USER_STORAGE_INTERNAL;
208 const char *scan_ignore = ".scan_ignore";
211 GError *error = NULL;
214 media_content_sec_debug("dir_path : %s", dir_path);
216 media_content_retvm_if(!STRING_VALID(dir_path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid dir_path");
219 /*1. Check Hidden Directory*/
220 if (strstr(dir_path, "/.") != NULL) {
222 media_content_error("hidden path");
223 return MEDIA_CONTENT_ERROR_NONE;
226 /*2. Check Scan Ignore Directory*/
227 ret = ms_user_get_storage_type(_content_get_uid(), dir_path, &storage_type);
228 if (ret != MS_MEDIA_ERR_NONE) {
229 media_content_error("ms_user_get_storage_type failed : %d", ret);
230 return _content_error_capi(MEDIA_CONTENT_TYPE, ret);
233 char *leaf_path = NULL;
234 char search_path[MAX_PATH_LEN] = {0, };
236 memset(search_path, 0, sizeof(search_path));
237 if (!SAFE_STRLCPY(search_path, dir_path, sizeof(search_path))) {
238 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
239 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
242 while (STRING_VALID(search_path)) {
243 dir = g_dir_open(search_path, 0, &error);
244 if (dir != NULL && error == NULL) {
245 while ((name = g_dir_read_name(dir))) {
246 if (g_strcmp0(name, scan_ignore) == 0) {
247 media_content_sec_debug("Ignore path[%s]", search_path);
254 media_content_error("Open Directory fail");
255 if (error->code == G_FILE_ERROR_ACCES) {
257 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
260 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
271 /*If root path, Stop Scanning*/
272 if ((storage_type == MS_USER_STORAGE_INTERNAL) && (STRING_VALID(MEDIA_ROOT_PATH_INTERNAL) && g_strcmp0(search_path, MEDIA_ROOT_PATH_INTERNAL) == 0)) {
274 } else if ((storage_type == MS_USER_STORAGE_EXTERNAL) && (STRING_VALID(MEDIA_ROOT_PATH_SDCARD)) && (g_strcmp0(search_path, MEDIA_ROOT_PATH_SDCARD) == 0)) {
276 } else if ((storage_type == MS_USER_STORAGE_EXTERNAL_USB) && (STRING_VALID(MEDIA_ROOT_PATH_DISC)) && (g_strcmp0(search_path, MEDIA_ROOT_PATH_DISC) == 0)) {
278 } else if (storage_type == MS_USER_STORAGE_EXTERNAL_USB) {
279 char *parent_folder_path = NULL;
280 bool is_root = FALSE;
282 parent_folder_path = g_path_get_dirname(search_path);
283 if (STRING_VALID(MEDIA_ROOT_PATH_USB) && STRING_VALID(parent_folder_path) && (g_strcmp0(parent_folder_path, MEDIA_ROOT_PATH_USB) == 0))
286 SAFE_FREE(parent_folder_path);
291 #ifdef _USE_SENIOR_MODE
292 if (_media_content_is_support_senior_mode()) {
293 if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL) && (g_strcmp0(search_path, MEDIA_ROOT_PATH_SENIOR_MODE) == 0))
298 leaf_path = strrchr(search_path, '/');
299 if (leaf_path != NULL) {
300 int seek_len = leaf_path -search_path;
301 search_path[seek_len] = '\0';
302 /*media_content_sec_debug("go to other dir [%s]", search_path);*/
304 media_content_debug("Fail to find leaf path");
310 return MEDIA_CONTENT_ERROR_NONE;
313 int _media_content_check_dir(const char *path)
318 #ifndef _USE_TVPD_MODE
319 char result_path[MAX_PATH_LEN] = {0, };
323 if (errno == EACCES || errno == EPERM) {
324 media_content_stderror("open dir fail");
325 media_content_sec_error("path [%s]", path);
326 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
328 media_content_stderror("open dir fail");
329 media_content_sec_error("path [%s]", path);
330 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
336 /* Check symbolic link directory */
337 real = realpath(path, NULL);
338 /* Get trimmed path */
339 _media_util_trim_path(path, &origin);
341 #ifdef _USE_TVPD_MODE
342 if (g_strcmp0(real, origin) != 0) {
343 media_content_error("symbolic link(directory)");
344 media_content_sec_debug("path[%s] real[%s]", origin, real);
347 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
350 if (g_str_has_prefix(real, MEDIA_SHARE_PATH)) {
351 /* If shared dirctory, it should be change path to TZ_USER_SHARE from realpath */
352 snprintf(result_path, MAX_PATH_LEN, "%s%s", tzplatform_getenv(TZ_USER_MEDIASHARED), real + strlen(MEDIA_SHARE_PATH));
353 if (g_strcmp0(result_path, origin) != 0) {
354 media_content_error("symbolic link(directory)");
355 media_content_sec_debug("path[%s] real[%s]", origin, real);
358 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
361 if (g_strcmp0(real, origin) != 0) {
362 media_content_error("symbolic link(directory)");
363 media_content_sec_debug("path[%s] real[%s]", origin, real);
366 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
374 return MEDIA_CONTENT_ERROR_NONE;
378 int _media_content_replace_path_in_condition(const char *condition, char *replace_condition, bool replace)
380 int ret = MEDIA_CONTENT_ERROR_NONE;
382 #ifdef _USE_TVPD_MODE
383 snprintf(replace_condition, MAX_QUERY_SIZE, "%s", condition);
385 char old_condition[MAX_QUERY_SIZE] = {0, };
386 char new_condition[MAX_QUERY_SIZE] = {0, };
388 unsigned int str_len = 0;
390 char *find_str = NULL;
391 char *to_replace_str = NULL;
393 if (replace == TRUE) { //change User session path to System session path
394 find_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL_OLD);
395 if (!STRING_VALID(find_str)) {
396 media_content_error("strdup failed");
397 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
401 to_replace_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL);
402 if (!STRING_VALID(to_replace_str)) {
403 media_content_error("Get TZ_USER_CONTENT failed");
404 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
408 find_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL);
409 if (!STRING_VALID(find_str)) {
410 media_content_error("Get TZ_USER_CONTENT failed");
411 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
415 to_replace_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL_OLD);
416 if (!STRING_VALID(to_replace_str)) {
417 media_content_error("strdup failed");
418 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
423 memset(old_condition, 0, sizeof(old_condition));
424 memset(new_condition, 0, sizeof(new_condition));
426 media_content_sec_debug("Old condition[%s]", condition);
428 if (!SAFE_STRLCPY(new_condition, condition, sizeof(new_condition))) {
429 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
430 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
434 if (g_strcmp0(find_str, to_replace_str))
435 find = strstr(new_condition, find_str);
437 while (find != NULL) {
438 str_len = find - new_condition;
440 memset(old_condition, 0, sizeof(old_condition));
441 if (!SAFE_STRLCPY(old_condition, new_condition, sizeof(old_condition))) {
442 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
443 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
446 memset(new_condition, 0, sizeof(new_condition));
448 snprintf(new_condition, str_len + 1, "%s", old_condition);
450 SAFE_STRLCAT(new_condition, to_replace_str, sizeof(new_condition));
451 SAFE_STRLCAT(new_condition, old_condition + str_len + strlen(find_str), sizeof(new_condition));
453 find = strstr(new_condition, find_str);
456 if (!SAFE_STRLCPY(replace_condition, new_condition, MAX_QUERY_SIZE)) {
457 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
458 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
462 media_content_sec_debug("repl cond[%s]", replace_condition);
464 if (!STRING_VALID(replace_condition)) {
465 media_content_error("replace failed");
466 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
472 SAFE_FREE(to_replace_str);
478 int _media_content_replace_path(const char *path, char *replace_path)
480 #ifdef _USE_TVPD_MODE
481 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
483 if (strncmp(path, MEDIA_ROOT_PATH_INTERNAL_OLD, strlen(MEDIA_ROOT_PATH_INTERNAL_OLD)) == 0) {
484 media_content_sec_debug("Old path[%s]", path);
485 snprintf(replace_path, MAX_PATH_LEN, "%s%s", MEDIA_ROOT_PATH_INTERNAL, path + strlen(MEDIA_ROOT_PATH_INTERNAL_OLD));
487 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
491 if (!STRING_VALID(replace_path)) {
492 media_content_error("replace failed");
493 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
496 return MEDIA_CONTENT_ERROR_NONE;
499 #ifdef _USE_SENIOR_MODE
500 bool _media_content_is_support_senior_mode()
502 bool bSupportSeniorMode = false;
504 if (system_info_get_value_bool(SYSTEM_INFO_KEY_GET_SENIOR_MODE_SUPPORTED, &bSupportSeniorMode) != SYSTEM_INFO_ERROR_NONE) {
505 media_content_debug("Get senior mode support failed");
508 /* media_content_debug("Senior mode Support : [%d]", bSupportSeniorMode); */
509 return bSupportSeniorMode;