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_in_condition(const char *condition, char *replace_condition)
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 media_content_sec_debug("Old condition[%s]", condition);
187 if (((strstr(condition, "PATH") != NULL) || (strstr(condition, "path") != NULL)) && (strstr(condition, old_path) != NULL)) {
188 char *cond = strdup(condition);
189 char *repl_cond_ptr = replace_condition;
190 char *cond_ptr = cond;
192 if (cond_ptr == NULL) {
194 media_content_error("memory allocation failed");
195 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
197 while(*cond_ptr != '\0') {
198 if (strlen(cond_ptr) < strlen(old_path)) {
199 memcpy(repl_cond_ptr, cond_ptr, strlen(cond_ptr));
202 /* replace path only and keep other condition */
203 if (memcmp(cond_ptr, old_path, strlen(old_path)) == 0) {
204 memcpy(repl_cond_ptr, tzplatform_getenv(TZ_USER_CONTENT), strlen(tzplatform_getenv(TZ_USER_CONTENT)));
205 cond_ptr += strlen(old_path);
206 repl_cond_ptr += strlen(tzplatform_getenv(TZ_USER_CONTENT));
208 *repl_cond_ptr = *cond_ptr;
215 snprintf(replace_condition, MAX_QUERY_SIZE, "%s", condition);
220 if (!STRING_VALID(replace_condition)) {
221 media_content_error("replace failed");
222 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
225 media_content_sec_debug("repl cond[%s]", replace_condition);
227 return MEDIA_CONTENT_ERROR_NONE;
230 int _media_content_rollback_path_in_condition(const char *condition, char *replace_condition)
232 int ret = MEDIA_CONTENT_ERROR_NONE;
233 char *old_path = NULL;
235 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &old_path);
236 if (ret != STORAGE_ERROR_NONE) {
237 media_content_error("storage_get_directory failed");
238 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
241 media_content_sec_debug("Old condition[%s]", condition);
242 if (((strstr(condition, "PATH") != NULL) || (strstr(condition, "path") != NULL)) && (strstr(condition, tzplatform_getenv(TZ_USER_CONTENT)) != NULL)) {
243 char *cond = strdup(condition);
244 char *repl_cond_ptr = replace_condition;
245 char *cond_ptr = cond;
247 if (cond_ptr == NULL) {
249 media_content_error("memory allocation failed");
250 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
252 while(*cond_ptr != '\0') {
253 if (strlen(cond_ptr) < strlen(tzplatform_getenv(TZ_USER_CONTENT))) {
254 memcpy(repl_cond_ptr, cond_ptr, strlen(cond_ptr));
257 /* replace path only and keep other condition */
258 if (memcmp(cond_ptr, tzplatform_getenv(TZ_USER_CONTENT), strlen(tzplatform_getenv(TZ_USER_CONTENT))) == 0) {
259 memcpy(repl_cond_ptr, old_path, strlen(old_path));
260 cond_ptr += strlen(tzplatform_getenv(TZ_USER_CONTENT));
261 repl_cond_ptr += strlen(old_path);
263 *repl_cond_ptr = *cond_ptr;
270 snprintf(replace_condition, MAX_QUERY_SIZE, "%s", condition);
275 if (!STRING_VALID(replace_condition)) {
276 media_content_error("replace failed");
277 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
280 media_content_sec_debug("repl cond[%s]", replace_condition);
282 return MEDIA_CONTENT_ERROR_NONE;
285 int _media_content_replace_path(const char *path, char *replace_path)
287 int ret = MEDIA_CONTENT_ERROR_NONE;
288 char *old_path = NULL;
290 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &old_path);
291 if (ret != STORAGE_ERROR_NONE) {
292 media_content_error("storage_get_directory failed");
293 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
296 if (strncmp(path, old_path, strlen(old_path)) == 0) {
297 media_content_sec_debug("Old path[%s]", path);
298 snprintf(replace_path, MAX_QUERY_SIZE, "%s%s", tzplatform_getenv(TZ_USER_CONTENT), path + strlen(old_path));
300 snprintf(replace_path, MAX_QUERY_SIZE, "%s", path);
305 if (!STRING_VALID(replace_path)) {
306 media_content_error("replace failed");
307 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
310 return MEDIA_CONTENT_ERROR_NONE;
313 int _media_content_rollback_path(const char *path, char *replace_path)
315 int ret = MEDIA_CONTENT_ERROR_NONE;
316 char *old_path = NULL;
318 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &old_path);
319 if (ret != STORAGE_ERROR_NONE) {
320 media_content_error("storage_get_directory failed");
321 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
324 if (strncmp(path, tzplatform_getenv(TZ_USER_CONTENT), strlen(tzplatform_getenv(TZ_USER_CONTENT))) == 0) {
325 media_content_sec_debug("new path[%s]", path);
326 snprintf(replace_path, MAX_QUERY_SIZE, "%s%s", old_path, path + strlen(tzplatform_getenv(TZ_USER_CONTENT)));
328 snprintf(replace_path, MAX_QUERY_SIZE, "%s", path);
333 if (!STRING_VALID(replace_path)) {
334 media_content_error("replace failed");
335 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
338 return MEDIA_CONTENT_ERROR_NONE;
341 #ifdef _USE_SENIOR_MODE
342 bool _media_content_is_support_senior_mode()
344 bool bSupportSeniorMode = false;
346 if (system_info_get_value_bool(SYSTEM_INFO_KEY_GET_SENIOR_MODE_SUPPORTED, &bSupportSeniorMode) != SYSTEM_INFO_ERROR_NONE) {
347 media_content_debug("Get senior mode support failed");
350 /* media_content_debug("Senior mode Support : [%d]", bSupportSeniorMode); */
351 return bSupportSeniorMode;