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 static char *g_old_path = NULL;
32 int _media_util_check_file_exist(const char *path)
36 /* check the file exits actually */
37 exist = open(path, O_RDONLY);
39 media_content_sec_debug("path [%s]", path);
40 media_content_stderror("open file fail");
41 if (errno == EACCES || errno == EPERM)
42 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
44 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
49 return MEDIA_CONTENT_ERROR_NONE;
52 int _media_util_check_ignore_file(const char *path, bool *ignore)
54 media_content_retvm_if(!STRING_VALID(path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid path");
58 if (strstr(path, "/.") != NULL) {
60 media_content_error("hidden path");
61 media_content_sec_debug("path : %s", path);
64 return MEDIA_CONTENT_ERROR_NONE;
67 int _media_util_check_ignore_dir(const char *dir_path, bool *ignore)
69 int ret = MEDIA_CONTENT_ERROR_NONE;
70 media_svc_storage_type_e storage_type = 0;
71 const char *scan_ignore = ".scan_ignore";
74 media_content_sec_debug("dir_path : %s", dir_path);
76 media_content_retvm_if(!STRING_VALID(dir_path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid dir_path");
79 /*1. Check Hidden Directory*/
80 if (strstr(dir_path, "/.") != NULL) {
82 media_content_error("hidden path");
83 return MEDIA_CONTENT_ERROR_NONE;
86 /*2. Check Scan Ignore Directory*/
87 ret = media_svc_get_storage_type(dir_path, &storage_type, tzplatform_getuid(TZ_USER_NAME));
88 if (ret != MS_MEDIA_ERR_NONE) {
89 media_content_error("media_svc_get_storage_type failed : %d", ret);
90 return _content_error_capi(MEDIA_CONTENT_TYPE, ret);
95 struct dirent *result = NULL;
97 char *leaf_path = NULL;
98 char search_path[4096] = {0, };
100 strncpy(search_path, dir_path, strlen(dir_path));
101 while (STRING_VALID(search_path)) {
102 dp = opendir(search_path);
105 media_content_error("Open Directory fail");
106 media_content_sec_debug("Open fail path[%s]", search_path);
107 if (errno == EACCES || errno == EPERM)
108 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
110 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
113 media_content_retvm_if(dp == NULL, MEDIA_CONTENT_ERROR_INVALID_OPERATION, "Open Directory fail");
115 while (!readdir_r(dp, &entry, &result)) {
119 if (STRING_VALID(entry.d_name) && (strcmp(entry.d_name, scan_ignore) == 0)) {
120 media_content_error("Find Ignore path");
121 media_content_sec_debug("Ignore path[%s]", search_path);
125 /*media_content_sec_debug("entry.d_name[%s]", entry.d_name);*/
130 if (dp) closedir(dp);
137 /*If root path, Stop Scanning*/
138 if ((storage_type == MEDIA_SVC_STORAGE_INTERNAL) && (STRING_VALID(MEDIA_ROOT_PATH_INTERNAL) && strcmp(search_path, MEDIA_ROOT_PATH_INTERNAL) == 0)) {
140 } else if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL) && (STRING_VALID(MEDIA_ROOT_PATH_SDCARD)) && (strcmp(search_path, MEDIA_ROOT_PATH_SDCARD) == 0)) {
142 } else if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) && (STRING_VALID(MEDIA_ROOT_PATH_DISC)) && (strcmp(search_path, MEDIA_ROOT_PATH_DISC) == 0)) {
144 } else if (storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) {
145 char *parent_folder_path = NULL;
146 bool is_root = FALSE;
148 parent_folder_path = g_path_get_dirname(search_path);
149 if (STRING_VALID(MEDIA_ROOT_PATH_USB) && (strcmp(parent_folder_path, MEDIA_ROOT_PATH_USB) == 0))
152 SAFE_FREE(parent_folder_path);
157 #ifdef _USE_SENIOR_MODE
158 if (_media_content_is_support_senior_mode()) {
159 if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL) && (strcmp(search_path, MEDIA_ROOT_PATH_SENIOR_MODE) == 0))
164 leaf_path = strrchr(search_path, '/');
165 if (leaf_path != NULL) {
166 int seek_len = leaf_path -search_path;
167 search_path[seek_len] = '\0';
168 /*media_content_sec_debug("go to other dir [%s]", search_path);*/
170 media_content_debug("Fail to find leaf path");
176 return MEDIA_CONTENT_ERROR_NONE;
179 int _media_content_replace_path_in_condition(const char *condition, char *replace_condition)
181 int ret = MEDIA_CONTENT_ERROR_NONE;
183 if (!STRING_VALID(g_old_path)) {
184 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &g_old_path);
185 if (ret != STORAGE_ERROR_NONE) {
186 media_content_error("storage_get_directory failed");
187 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
191 media_content_sec_debug("Old condition[%s]", condition);
192 if (((strstr(condition, "PATH") != NULL) || (strstr(condition, "path") != NULL)) && (strstr(condition, g_old_path) != NULL)) {
193 char *cond = strdup(condition);
194 char *repl_cond_ptr = replace_condition;
195 char *cond_ptr = cond;
197 if (cond_ptr == NULL) {
198 media_content_error("memory allocation failed");
199 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
201 while (*cond_ptr != '\0') {
202 if (strlen(cond_ptr) < strlen(g_old_path)) {
203 memcpy(repl_cond_ptr, cond_ptr, strlen(cond_ptr));
206 /* replace path only and keep other condition */
207 if (memcmp(cond_ptr, g_old_path, strlen(g_old_path)) == 0) {
208 memcpy(repl_cond_ptr, tzplatform_getenv(TZ_USER_CONTENT), strlen(tzplatform_getenv(TZ_USER_CONTENT)));
209 cond_ptr += strlen(g_old_path);
210 repl_cond_ptr += strlen(tzplatform_getenv(TZ_USER_CONTENT));
212 *repl_cond_ptr = *cond_ptr;
219 snprintf(replace_condition, MAX_QUERY_SIZE, "%s", condition);
222 if (!STRING_VALID(replace_condition)) {
223 media_content_error("replace failed");
224 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
227 media_content_sec_debug("repl cond[%s]", replace_condition);
229 return MEDIA_CONTENT_ERROR_NONE;
232 int _media_content_rollback_path_in_condition(const char *condition, char *replace_condition)
234 int ret = MEDIA_CONTENT_ERROR_NONE;
236 if (!STRING_VALID(g_old_path)) {
237 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &g_old_path);
238 if (ret != STORAGE_ERROR_NONE) {
239 media_content_error("storage_get_directory failed");
240 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
244 media_content_sec_debug("Old condition[%s]", condition);
245 if (((strstr(condition, "PATH") != NULL) || (strstr(condition, "path") != NULL)) && (strstr(condition, tzplatform_getenv(TZ_USER_CONTENT)) != NULL)) {
246 char *cond = strdup(condition);
247 char *repl_cond_ptr = replace_condition;
248 char *cond_ptr = cond;
250 if (cond_ptr == NULL) {
251 media_content_error("memory allocation failed");
252 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
254 while (*cond_ptr != '\0') {
255 if (strlen(cond_ptr) < strlen(tzplatform_getenv(TZ_USER_CONTENT))) {
256 memcpy(repl_cond_ptr, cond_ptr, strlen(cond_ptr));
259 /* replace path only and keep other condition */
260 if (memcmp(cond_ptr, tzplatform_getenv(TZ_USER_CONTENT), strlen(tzplatform_getenv(TZ_USER_CONTENT))) == 0) {
261 memcpy(repl_cond_ptr, g_old_path, strlen(g_old_path));
262 cond_ptr += strlen(tzplatform_getenv(TZ_USER_CONTENT));
263 repl_cond_ptr += strlen(g_old_path);
265 *repl_cond_ptr = *cond_ptr;
272 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;
289 #ifdef _USE_TV_PROFILE
290 snprintf(replace_path, MAX_QUERY_SIZE, "%s", path);
292 if (!STRING_VALID(g_old_path)) {
293 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &g_old_path);
294 if (ret != STORAGE_ERROR_NONE) {
295 media_content_error("storage_get_directory failed");
296 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
300 if (strncmp(path, g_old_path, strlen(g_old_path)) == 0) {
301 media_content_sec_debug("Old path[%s]", path);
302 snprintf(replace_path, MAX_QUERY_SIZE, "%s%s", tzplatform_getenv(TZ_USER_CONTENT), path + strlen(g_old_path));
304 snprintf(replace_path, MAX_QUERY_SIZE, "%s", path);
308 if (!STRING_VALID(replace_path)) {
309 media_content_error("replace failed");
310 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
313 return MEDIA_CONTENT_ERROR_NONE;
316 int _media_content_rollback_path(const char *path, char *replace_path)
318 int ret = MEDIA_CONTENT_ERROR_NONE;
320 #ifdef _USE_TV_PROFILE
321 snprintf(replace_path, MAX_QUERY_SIZE, "%s", path);
324 if (!STRING_VALID(g_old_path)) {
325 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &g_old_path);
326 if (ret != STORAGE_ERROR_NONE) {
327 media_content_error("storage_get_directory failed");
328 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
332 if (strncmp(path, tzplatform_getenv(TZ_USER_CONTENT), strlen(tzplatform_getenv(TZ_USER_CONTENT))) == 0) {
333 media_content_sec_debug("new path[%s]", path);
334 snprintf(replace_path, MAX_QUERY_SIZE, "%s%s", g_old_path, path + strlen(tzplatform_getenv(TZ_USER_CONTENT)));
336 snprintf(replace_path, MAX_QUERY_SIZE, "%s", path);
340 if (!STRING_VALID(replace_path)) {
341 media_content_error("replace failed");
342 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
345 return MEDIA_CONTENT_ERROR_NONE;
348 #ifdef _USE_SENIOR_MODE
349 bool _media_content_is_support_senior_mode()
351 bool bSupportSeniorMode = false;
353 if (system_info_get_value_bool(SYSTEM_INFO_KEY_GET_SENIOR_MODE_SUPPORTED, &bSupportSeniorMode) != SYSTEM_INFO_ERROR_NONE) {
354 media_content_debug("Get senior mode support failed");
357 /* media_content_debug("Senior mode Support : [%d]", bSupportSeniorMode); */
358 return bSupportSeniorMode;