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 int _media_util_check_ignore_file(const char *path, bool *ignore)
81 media_content_retvm_if(!STRING_VALID(path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid path");
84 char *tmp_path = NULL;
86 #ifndef _USE_TVPD_MODE
87 char replace[MAX_PATH_LEN] = {0, };
90 /* Check symbolic link file */
91 if (g_file_test(path, G_FILE_TEST_IS_SYMLINK)) {
93 media_content_error("symbolic link(file)");
94 media_content_sec_debug("path : %s", path);
95 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
98 /* Check hidden path */
99 if (strstr(path, "/.") != NULL) {
101 media_content_error("hidden path");
102 media_content_sec_debug("path : %s", path);
103 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
106 /* Check symbolic directory */
107 tmp_path = realpath(path, NULL);
109 #ifdef _USE_TVPD_MODE
110 if (g_strcmp0(tmp_path, path) != 0) {
113 media_content_error("symbolic link(directory)");
114 media_content_sec_debug("path : %s", path);
115 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
118 if (g_str_has_prefix(tmp_path, MEDIA_SHARE_PATH)) {
119 /* If shared dirctory, it should be change path to TZ_USER_SHARE from realpath */
120 snprintf(replace, MAX_PATH_LEN, "%s%s", tzplatform_getenv(TZ_USER_MEDIASHARED), tmp_path + strlen(MEDIA_SHARE_PATH));
121 if (g_strcmp0(replace, path) != 0) {
124 media_content_error("symbolic link(directory)");
125 media_content_sec_debug("path : %s", path);
126 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
129 if (g_strcmp0(tmp_path, path) != 0) {
132 media_content_error("symbolic link(directory)");
133 media_content_sec_debug("path : %s", path);
134 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
140 return MEDIA_CONTENT_ERROR_NONE;
143 int _media_util_check_ignore_dir(const char *dir_path, bool *ignore)
145 int ret = MEDIA_CONTENT_ERROR_NONE;
146 media_svc_storage_type_e storage_type = 0;
147 const char *scan_ignore = ".scan_ignore";
150 GError *error = NULL;
153 media_content_sec_debug("dir_path : %s", dir_path);
155 media_content_retvm_if(!STRING_VALID(dir_path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid dir_path");
158 /*1. Check Hidden Directory*/
159 if (strstr(dir_path, "/.") != NULL) {
161 media_content_error("hidden path");
162 return MEDIA_CONTENT_ERROR_NONE;
165 /*2. Check Scan Ignore Directory*/
166 ret = media_svc_get_storage_type(dir_path, &storage_type, _content_get_uid());
167 if (ret != MS_MEDIA_ERR_NONE) {
168 media_content_error("media_svc_get_storage_type failed : %d", ret);
169 return _content_error_capi(MEDIA_CONTENT_TYPE, ret);
172 char *leaf_path = NULL;
173 char search_path[MAX_PATH_LEN] = {0, };
175 memset(search_path, 0, sizeof(search_path));
176 if (!SAFE_STRLCPY(search_path, dir_path, sizeof(search_path))) {
177 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
178 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
181 while (STRING_VALID(search_path)) {
182 dir = g_dir_open(search_path, 0, &error);
183 if (dir != NULL && error == NULL) {
184 while ((name = g_dir_read_name(dir))) {
185 if (g_strcmp0(name, scan_ignore) == 0) {
186 media_content_sec_debug("Ignore path[%s]", search_path);
193 media_content_error("Open Directory fail");
194 if (error->code == G_FILE_ERROR_ACCES)
195 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
197 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
207 /*If root path, Stop Scanning*/
208 if ((storage_type == MEDIA_SVC_STORAGE_INTERNAL) && (STRING_VALID(MEDIA_ROOT_PATH_INTERNAL) && g_strcmp0(search_path, MEDIA_ROOT_PATH_INTERNAL) == 0)) {
210 } else if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL) && (STRING_VALID(MEDIA_ROOT_PATH_SDCARD)) && (g_strcmp0(search_path, MEDIA_ROOT_PATH_SDCARD) == 0)) {
212 } else if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) && (STRING_VALID(MEDIA_ROOT_PATH_DISC)) && (g_strcmp0(search_path, MEDIA_ROOT_PATH_DISC) == 0)) {
214 } else if (storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) {
215 char *parent_folder_path = NULL;
216 bool is_root = FALSE;
218 parent_folder_path = g_path_get_dirname(search_path);
219 if (STRING_VALID(MEDIA_ROOT_PATH_USB) && STRING_VALID(parent_folder_path) && (g_strcmp0(parent_folder_path, MEDIA_ROOT_PATH_USB) == 0))
222 SAFE_FREE(parent_folder_path);
227 #ifdef _USE_SENIOR_MODE
228 if (_media_content_is_support_senior_mode()) {
229 if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL) && (g_strcmp0(search_path, MEDIA_ROOT_PATH_SENIOR_MODE) == 0))
234 leaf_path = strrchr(search_path, '/');
235 if (leaf_path != NULL) {
236 int seek_len = leaf_path -search_path;
237 search_path[seek_len] = '\0';
238 /*media_content_sec_debug("go to other dir [%s]", search_path);*/
240 media_content_debug("Fail to find leaf path");
246 return MEDIA_CONTENT_ERROR_NONE;
249 int _media_content_replace_path_in_condition(const char *condition, char *replace_condition, bool replace)
251 int ret = MEDIA_CONTENT_ERROR_NONE;
253 #ifdef _USE_TVPD_MODE
254 snprintf(replace_condition, MAX_QUERY_SIZE, "%s", condition);
256 char old_condition[MAX_QUERY_SIZE] = {0, };
257 char new_condition[MAX_QUERY_SIZE] = {0, };
259 unsigned int str_len = 0;
261 char *find_str = NULL;
262 char *to_replace_str = NULL;
264 if (replace == TRUE) { //change User session path to System session path
265 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &find_str);
266 if (ret != STORAGE_ERROR_NONE) {
267 media_content_error("storage_get_directory failed");
268 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
272 to_replace_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL);
273 if (!STRING_VALID(to_replace_str)) {
274 media_content_error("Get TZ_USER_CONTENT failed");
275 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
279 find_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL);
280 if (!STRING_VALID(find_str)) {
281 media_content_error("Get TZ_USER_CONTENT failed");
282 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
286 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &to_replace_str);
287 if (ret != STORAGE_ERROR_NONE) {
288 media_content_error("storage_get_directory failed");
289 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
294 memset(old_condition, 0, sizeof(old_condition));
295 memset(new_condition, 0, sizeof(new_condition));
297 media_content_sec_debug("Old condition[%s]", condition);
299 if (!SAFE_STRLCPY(new_condition, condition, sizeof(new_condition))) {
300 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
301 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
305 if (g_strcmp0(find_str, to_replace_str))
306 find = strstr(new_condition, find_str);
308 while (find != NULL) {
309 str_len = find - new_condition;
311 memset(old_condition, 0, sizeof(old_condition));
312 if (!SAFE_STRLCPY(old_condition, new_condition, sizeof(old_condition))) {
313 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
314 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
317 memset(new_condition, 0, sizeof(new_condition));
319 snprintf(new_condition, str_len + 1, "%s", old_condition);
321 SAFE_STRLCAT(new_condition, to_replace_str, sizeof(new_condition));
322 SAFE_STRLCAT(new_condition, old_condition + str_len + strlen(find_str), sizeof(new_condition));
324 find = strstr(new_condition, find_str);
327 if (!SAFE_STRLCPY(replace_condition, new_condition, MAX_QUERY_SIZE)) {
328 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
329 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
333 media_content_sec_debug("repl cond[%s]", replace_condition);
335 if (!STRING_VALID(replace_condition)) {
336 media_content_error("replace failed");
337 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
343 SAFE_FREE(to_replace_str);
349 int _media_content_replace_path(const char *path, char *replace_path)
351 #ifdef _USE_TVPD_MODE
352 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
355 int ret = MEDIA_CONTENT_ERROR_NONE;
356 char *old_internal_path = NULL;
358 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &old_internal_path);
359 if (ret != STORAGE_ERROR_NONE) {
360 media_content_error("storage_get_directory failed");
361 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
364 if (strncmp(path, old_internal_path, strlen(old_internal_path)) == 0) {
365 media_content_sec_debug("Old path[%s]", path);
366 snprintf(replace_path, MAX_PATH_LEN, "%s%s", MEDIA_ROOT_PATH_INTERNAL, path + strlen(old_internal_path));
368 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
371 SAFE_FREE(old_internal_path);
374 if (!STRING_VALID(replace_path)) {
375 media_content_error("replace failed");
376 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
379 return MEDIA_CONTENT_ERROR_NONE;
382 int _media_content_rollback_path(const char *path, char *replace_path)
384 #ifdef _USE_TVPD_MODE
385 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
388 int ret = MEDIA_CONTENT_ERROR_NONE;
389 char *old_internal_path = NULL;
391 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &old_internal_path);
392 if (ret != STORAGE_ERROR_NONE) {
393 media_content_error("storage_get_directory failed");
394 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
397 if (strncmp(path, MEDIA_ROOT_PATH_INTERNAL, strlen(MEDIA_ROOT_PATH_INTERNAL)) == 0) {
398 media_content_sec_debug("new path[%s]", path);
399 snprintf(replace_path, MAX_PATH_LEN, "%s%s", old_internal_path, path + strlen(MEDIA_ROOT_PATH_INTERNAL));
401 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
404 SAFE_FREE(old_internal_path);
407 if (!STRING_VALID(replace_path)) {
408 media_content_error("replace failed");
409 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
412 return MEDIA_CONTENT_ERROR_NONE;
415 #ifdef _USE_SENIOR_MODE
416 bool _media_content_is_support_senior_mode()
418 bool bSupportSeniorMode = false;
420 if (system_info_get_value_bool(SYSTEM_INFO_KEY_GET_SENIOR_MODE_SUPPORTED, &bSupportSeniorMode) != SYSTEM_INFO_ERROR_NONE) {
421 media_content_debug("Get senior mode support failed");
424 /* media_content_debug("Senior mode Support : [%d]", bSupportSeniorMode); */
425 return bSupportSeniorMode;