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_replace_path_in_condition(const char *condition, char *replace_condition, bool replace)
294 int ret = MEDIA_CONTENT_ERROR_NONE;
296 #ifdef _USE_TVPD_MODE
297 snprintf(replace_condition, MAX_QUERY_SIZE, "%s", condition);
299 char old_condition[MAX_QUERY_SIZE] = {0, };
300 char new_condition[MAX_QUERY_SIZE] = {0, };
302 unsigned int str_len = 0;
304 char *find_str = NULL;
305 char *to_replace_str = NULL;
307 if (replace == TRUE) { //change User session path to System session path
308 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &find_str);
309 if (ret != STORAGE_ERROR_NONE) {
310 media_content_error("storage_get_directory failed");
311 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
315 to_replace_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL);
316 if (!STRING_VALID(to_replace_str)) {
317 media_content_error("Get TZ_USER_CONTENT failed");
318 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
322 find_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL);
323 if (!STRING_VALID(find_str)) {
324 media_content_error("Get TZ_USER_CONTENT failed");
325 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
329 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &to_replace_str);
330 if (ret != STORAGE_ERROR_NONE) {
331 media_content_error("storage_get_directory failed");
332 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
337 memset(old_condition, 0, sizeof(old_condition));
338 memset(new_condition, 0, sizeof(new_condition));
340 media_content_sec_debug("Old condition[%s]", condition);
342 if (!SAFE_STRLCPY(new_condition, condition, sizeof(new_condition))) {
343 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
344 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
348 if (g_strcmp0(find_str, to_replace_str))
349 find = strstr(new_condition, find_str);
351 while (find != NULL) {
352 str_len = find - new_condition;
354 memset(old_condition, 0, sizeof(old_condition));
355 if (!SAFE_STRLCPY(old_condition, new_condition, sizeof(old_condition))) {
356 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
357 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
360 memset(new_condition, 0, sizeof(new_condition));
362 snprintf(new_condition, str_len + 1, "%s", old_condition);
364 SAFE_STRLCAT(new_condition, to_replace_str, sizeof(new_condition));
365 SAFE_STRLCAT(new_condition, old_condition + str_len + strlen(find_str), sizeof(new_condition));
367 find = strstr(new_condition, find_str);
370 if (!SAFE_STRLCPY(replace_condition, new_condition, MAX_QUERY_SIZE)) {
371 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
372 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
376 media_content_sec_debug("repl cond[%s]", replace_condition);
378 if (!STRING_VALID(replace_condition)) {
379 media_content_error("replace failed");
380 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
386 SAFE_FREE(to_replace_str);
392 int _media_content_replace_path(const char *path, char *replace_path)
394 #ifdef _USE_TVPD_MODE
395 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
398 int ret = MEDIA_CONTENT_ERROR_NONE;
399 char *old_internal_path = NULL;
401 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &old_internal_path);
402 if (ret != STORAGE_ERROR_NONE) {
403 media_content_error("storage_get_directory failed");
404 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
407 if (strncmp(path, old_internal_path, strlen(old_internal_path)) == 0) {
408 media_content_sec_debug("Old path[%s]", path);
409 snprintf(replace_path, MAX_PATH_LEN, "%s%s", MEDIA_ROOT_PATH_INTERNAL, path + strlen(old_internal_path));
411 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
414 SAFE_FREE(old_internal_path);
417 if (!STRING_VALID(replace_path)) {
418 media_content_error("replace failed");
419 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
422 return MEDIA_CONTENT_ERROR_NONE;
425 int _media_content_rollback_path(const char *path, char *replace_path)
427 #ifdef _USE_TVPD_MODE
428 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
431 int ret = MEDIA_CONTENT_ERROR_NONE;
432 char *old_internal_path = NULL;
434 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &old_internal_path);
435 if (ret != STORAGE_ERROR_NONE) {
436 media_content_error("storage_get_directory failed");
437 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
440 if (strncmp(path, MEDIA_ROOT_PATH_INTERNAL, strlen(MEDIA_ROOT_PATH_INTERNAL)) == 0) {
441 media_content_sec_debug("new path[%s]", path);
442 snprintf(replace_path, MAX_PATH_LEN, "%s%s", old_internal_path, path + strlen(MEDIA_ROOT_PATH_INTERNAL));
444 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
447 SAFE_FREE(old_internal_path);
450 if (!STRING_VALID(replace_path)) {
451 media_content_error("replace failed");
452 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
455 return MEDIA_CONTENT_ERROR_NONE;
458 #ifdef _USE_SENIOR_MODE
459 bool _media_content_is_support_senior_mode()
461 bool bSupportSeniorMode = false;
463 if (system_info_get_value_bool(SYSTEM_INFO_KEY_GET_SENIOR_MODE_SUPPORTED, &bSupportSeniorMode) != SYSTEM_INFO_ERROR_NONE) {
464 media_content_debug("Get senior mode support failed");
467 /* media_content_debug("Senior mode Support : [%d]", bSupportSeniorMode); */
468 return bSupportSeniorMode;