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 <sys/types.h>
22 #include <media_util_private.h>
23 #include <media_info_private.h>
24 #include <media_content_type.h>
26 #ifdef _USE_TV_PROFILE
27 #include <system_info.h>
30 int _media_util_check_file_exist(const char *path)
34 /* check the file exits actually */
35 exist = open(path, O_RDONLY);
37 media_content_sec_debug("path [%s]", path);
38 media_content_stderror("open file fail");
39 if (errno == EACCES || errno == EPERM)
40 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
42 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
47 return MEDIA_CONTENT_ERROR_NONE;
50 int _media_util_check_ignore_file(const char *path, bool *ignore)
52 media_content_retvm_if(!STRING_VALID(path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid path");
56 if (strstr(path, "/.") != NULL) {
58 media_content_error("hidden path");
59 media_content_sec_debug("path : %s", path);
62 return MEDIA_CONTENT_ERROR_NONE;
65 int _media_util_check_ignore_dir(const char *dir_path, bool *ignore)
67 int ret = MEDIA_CONTENT_ERROR_NONE;
68 media_svc_storage_type_e storage_type = 0;
69 const char *scan_ignore = ".scan_ignore";
72 media_content_sec_debug("dir_path : %s", dir_path);
74 media_content_retvm_if(!STRING_VALID(dir_path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid dir_path");
77 /*1. Check Hidden Directory*/
78 if (strstr(dir_path, "/.") != NULL) {
80 media_content_error("hidden path");
81 return MEDIA_CONTENT_ERROR_NONE;
84 /*2. Check Scan Ignore Directory*/
85 ret = media_svc_get_storage_type(dir_path, &storage_type, tzplatform_getuid(TZ_USER_NAME));
86 if (ret != MS_MEDIA_ERR_NONE) {
87 media_content_error("media_svc_get_storage_type failed : %d", ret);
88 return _content_error_capi(MEDIA_CONTENT_TYPE, ret);
93 struct dirent *result = NULL;
95 char *leaf_path = NULL;
96 char search_path[4096] = {0, };
98 strncpy(search_path, dir_path, strlen(dir_path));
99 while (STRING_VALID(search_path)) {
100 dp = opendir(search_path);
103 media_content_error("Open Directory fail");
104 media_content_sec_debug("Open fail path[%s]", search_path);
105 if (errno == EACCES || errno == EPERM)
106 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
108 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
111 media_content_retvm_if(dp == NULL, MEDIA_CONTENT_ERROR_INVALID_OPERATION, "Open Directory fail");
113 while (!readdir_r(dp, &entry, &result)) {
117 if (STRING_VALID(entry.d_name) && (strcmp(entry.d_name, scan_ignore) == 0)) {
118 media_content_error("Find Ignore path");
119 media_content_sec_debug("Ignore path[%s]", search_path);
123 /*media_content_sec_debug("entry.d_name[%s]", entry.d_name);*/
128 if (dp) closedir(dp);
135 /*If root path, Stop Scanning*/
136 if ((storage_type == MEDIA_SVC_STORAGE_INTERNAL) && (STRING_VALID(MEDIA_ROOT_PATH_INTERNAL) && strcmp(search_path, MEDIA_ROOT_PATH_INTERNAL) == 0)) {
138 } else if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL) && (STRING_VALID(MEDIA_ROOT_PATH_SDCARD)) && (strcmp(search_path, MEDIA_ROOT_PATH_SDCARD) == 0)) {
140 } else if (storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) {
141 char *parent_folder_path = NULL;
142 bool is_root = FALSE;
144 parent_folder_path = g_path_get_dirname(search_path);
145 if (STRING_VALID(MEDIA_ROOT_PATH_USB) && (strcmp(parent_folder_path, MEDIA_ROOT_PATH_USB) == 0))
148 SAFE_FREE(parent_folder_path);
153 #ifdef _USE_SENIOR_MODE
154 if (_media_content_is_support_senior_mode()) {
155 if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL) && (strcmp(search_path, MEDIA_ROOT_PATH_SENIOR_MODE) == 0))
160 leaf_path = strrchr(search_path, '/');
161 if (leaf_path != NULL) {
162 int seek_len = leaf_path -search_path;
163 search_path[seek_len] = '\0';
164 /*media_content_sec_debug("go to other dir [%s]", search_path);*/
166 media_content_debug("Fail to find leaf path");
172 return MEDIA_CONTENT_ERROR_NONE;
175 int _media_content_replace_path(const char *path, char *replace_path)
177 int ret = MEDIA_CONTENT_ERROR_NONE;
178 char *old_path = NULL;
180 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &old_path);
181 if (ret != STORAGE_ERROR_NONE) {
182 media_content_error("storage_get_directory failed");
183 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
186 if (strncmp(path, old_path, strlen(old_path)) == 0) {
187 media_content_sec_debug("Old path[%s]", path);
188 snprintf(replace_path, MAX_QUERY_SIZE, "%s%s", tzplatform_getenv(TZ_USER_CONTENT), path + strlen(old_path));
190 snprintf(replace_path, MAX_QUERY_SIZE, "%s", path);
195 if (!STRING_VALID(replace_path)) {
196 media_content_error("replace failed");
197 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
200 return MEDIA_CONTENT_ERROR_NONE;
203 int _media_content_rollback_path(const char *path, char *replace_path)
205 int ret = MEDIA_CONTENT_ERROR_NONE;
206 char *old_path = NULL;
208 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &old_path);
209 if (ret != STORAGE_ERROR_NONE) {
210 media_content_error("storage_get_directory failed");
211 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
214 if (strncmp(path, tzplatform_getenv(TZ_USER_CONTENT), strlen(tzplatform_getenv(TZ_USER_CONTENT))) == 0) {
215 media_content_sec_debug("new path[%s]", path);
216 snprintf(replace_path, MAX_QUERY_SIZE, "%s%s", old_path, path + strlen(tzplatform_getenv(TZ_USER_CONTENT)));
218 snprintf(replace_path, MAX_QUERY_SIZE, "%s", path);
223 if (!STRING_VALID(replace_path)) {
224 media_content_error("replace failed");
225 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
228 return MEDIA_CONTENT_ERROR_NONE;
231 #ifdef _USE_SENIOR_MODE
232 bool _media_content_is_support_senior_mode()
234 bool bSupportSeniorMode = false;
236 if (system_info_get_value_bool(SYSTEM_INFO_KEY_GET_SENIOR_MODE_SUPPORTED, &bSupportSeniorMode) != SYSTEM_INFO_ERROR_NONE) {
237 media_content_debug("Get senior mode support failed");
240 /* media_content_debug("Senior mode Support : [%d]", bSupportSeniorMode); */
241 return bSupportSeniorMode;