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)
235 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
237 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
247 /*If root path, Stop Scanning*/
248 if ((storage_type == MEDIA_SVC_STORAGE_INTERNAL) && (STRING_VALID(MEDIA_ROOT_PATH_INTERNAL) && g_strcmp0(search_path, MEDIA_ROOT_PATH_INTERNAL) == 0)) {
250 } else if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL) && (STRING_VALID(MEDIA_ROOT_PATH_SDCARD)) && (g_strcmp0(search_path, MEDIA_ROOT_PATH_SDCARD) == 0)) {
252 } else if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) && (STRING_VALID(MEDIA_ROOT_PATH_DISC)) && (g_strcmp0(search_path, MEDIA_ROOT_PATH_DISC) == 0)) {
254 } else if (storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) {
255 char *parent_folder_path = NULL;
256 bool is_root = FALSE;
258 parent_folder_path = g_path_get_dirname(search_path);
259 if (STRING_VALID(MEDIA_ROOT_PATH_USB) && STRING_VALID(parent_folder_path) && (g_strcmp0(parent_folder_path, MEDIA_ROOT_PATH_USB) == 0))
262 SAFE_FREE(parent_folder_path);
267 #ifdef _USE_SENIOR_MODE
268 if (_media_content_is_support_senior_mode()) {
269 if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL) && (g_strcmp0(search_path, MEDIA_ROOT_PATH_SENIOR_MODE) == 0))
274 leaf_path = strrchr(search_path, '/');
275 if (leaf_path != NULL) {
276 int seek_len = leaf_path -search_path;
277 search_path[seek_len] = '\0';
278 /*media_content_sec_debug("go to other dir [%s]", search_path);*/
280 media_content_debug("Fail to find leaf path");
286 return MEDIA_CONTENT_ERROR_NONE;
289 int _media_content_replace_path_in_condition(const char *condition, char *replace_condition, bool replace)
291 int ret = MEDIA_CONTENT_ERROR_NONE;
293 #ifdef _USE_TVPD_MODE
294 snprintf(replace_condition, MAX_QUERY_SIZE, "%s", condition);
296 char old_condition[MAX_QUERY_SIZE] = {0, };
297 char new_condition[MAX_QUERY_SIZE] = {0, };
299 unsigned int str_len = 0;
301 char *find_str = NULL;
302 char *to_replace_str = NULL;
304 if (replace == TRUE) { //change User session path to System session path
305 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &find_str);
306 if (ret != STORAGE_ERROR_NONE) {
307 media_content_error("storage_get_directory failed");
308 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
312 to_replace_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL);
313 if (!STRING_VALID(to_replace_str)) {
314 media_content_error("Get TZ_USER_CONTENT failed");
315 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
319 find_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL);
320 if (!STRING_VALID(find_str)) {
321 media_content_error("Get TZ_USER_CONTENT failed");
322 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
326 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &to_replace_str);
327 if (ret != STORAGE_ERROR_NONE) {
328 media_content_error("storage_get_directory failed");
329 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
334 memset(old_condition, 0, sizeof(old_condition));
335 memset(new_condition, 0, sizeof(new_condition));
337 media_content_sec_debug("Old condition[%s]", condition);
339 if (!SAFE_STRLCPY(new_condition, condition, sizeof(new_condition))) {
340 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
341 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
345 if (g_strcmp0(find_str, to_replace_str))
346 find = strstr(new_condition, find_str);
348 while (find != NULL) {
349 str_len = find - new_condition;
351 memset(old_condition, 0, sizeof(old_condition));
352 if (!SAFE_STRLCPY(old_condition, new_condition, sizeof(old_condition))) {
353 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
354 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
357 memset(new_condition, 0, sizeof(new_condition));
359 snprintf(new_condition, str_len + 1, "%s", old_condition);
361 SAFE_STRLCAT(new_condition, to_replace_str, sizeof(new_condition));
362 SAFE_STRLCAT(new_condition, old_condition + str_len + strlen(find_str), sizeof(new_condition));
364 find = strstr(new_condition, find_str);
367 if (!SAFE_STRLCPY(replace_condition, new_condition, MAX_QUERY_SIZE)) {
368 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
369 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
373 media_content_sec_debug("repl cond[%s]", replace_condition);
375 if (!STRING_VALID(replace_condition)) {
376 media_content_error("replace failed");
377 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
383 SAFE_FREE(to_replace_str);
389 int _media_content_replace_path(const char *path, char *replace_path)
391 #ifdef _USE_TVPD_MODE
392 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
395 int ret = MEDIA_CONTENT_ERROR_NONE;
396 char *old_internal_path = NULL;
398 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &old_internal_path);
399 if (ret != STORAGE_ERROR_NONE) {
400 media_content_error("storage_get_directory failed");
401 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
404 if (strncmp(path, old_internal_path, strlen(old_internal_path)) == 0) {
405 media_content_sec_debug("Old path[%s]", path);
406 snprintf(replace_path, MAX_PATH_LEN, "%s%s", MEDIA_ROOT_PATH_INTERNAL, path + strlen(old_internal_path));
408 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
411 SAFE_FREE(old_internal_path);
414 if (!STRING_VALID(replace_path)) {
415 media_content_error("replace failed");
416 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
419 return MEDIA_CONTENT_ERROR_NONE;
422 int _media_content_rollback_path(const char *path, char *replace_path)
424 #ifdef _USE_TVPD_MODE
425 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
428 int ret = MEDIA_CONTENT_ERROR_NONE;
429 char *old_internal_path = NULL;
431 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &old_internal_path);
432 if (ret != STORAGE_ERROR_NONE) {
433 media_content_error("storage_get_directory failed");
434 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
437 if (strncmp(path, MEDIA_ROOT_PATH_INTERNAL, strlen(MEDIA_ROOT_PATH_INTERNAL)) == 0) {
438 media_content_sec_debug("new path[%s]", path);
439 snprintf(replace_path, MAX_PATH_LEN, "%s%s", old_internal_path, path + strlen(MEDIA_ROOT_PATH_INTERNAL));
441 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
444 SAFE_FREE(old_internal_path);
447 if (!STRING_VALID(replace_path)) {
448 media_content_error("replace failed");
449 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
452 return MEDIA_CONTENT_ERROR_NONE;
455 #ifdef _USE_SENIOR_MODE
456 bool _media_content_is_support_senior_mode()
458 bool bSupportSeniorMode = false;
460 if (system_info_get_value_bool(SYSTEM_INFO_KEY_GET_SENIOR_MODE_SUPPORTED, &bSupportSeniorMode) != SYSTEM_INFO_ERROR_NONE) {
461 media_content_debug("Get senior mode support failed");
464 /* media_content_debug("Senior mode Support : [%d]", bSupportSeniorMode); */
465 return bSupportSeniorMode;