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) {
143 char *parent_folder_path = NULL;
144 bool is_root = FALSE;
146 parent_folder_path = g_path_get_dirname(search_path);
147 if (STRING_VALID(MEDIA_ROOT_PATH_USB) && (strcmp(parent_folder_path, MEDIA_ROOT_PATH_USB) == 0))
150 SAFE_FREE(parent_folder_path);
155 #ifdef _USE_SENIOR_MODE
156 if (_media_content_is_support_senior_mode()) {
157 if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL) && (strcmp(search_path, MEDIA_ROOT_PATH_SENIOR_MODE) == 0))
162 leaf_path = strrchr(search_path, '/');
163 if (leaf_path != NULL) {
164 int seek_len = leaf_path -search_path;
165 search_path[seek_len] = '\0';
166 /*media_content_sec_debug("go to other dir [%s]", search_path);*/
168 media_content_debug("Fail to find leaf path");
174 return MEDIA_CONTENT_ERROR_NONE;
177 int _media_content_replace_path_in_condition(const char *condition, char *replace_condition)
179 int ret = MEDIA_CONTENT_ERROR_NONE;
181 if (!STRING_VALID(g_old_path)) {
182 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &g_old_path);
183 if (ret != STORAGE_ERROR_NONE) {
184 media_content_error("storage_get_directory failed");
185 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
189 media_content_sec_debug("Old condition[%s]", condition);
190 if (((strstr(condition, "PATH") != NULL) || (strstr(condition, "path") != NULL)) && (strstr(condition, g_old_path) != NULL)) {
191 char *cond = strdup(condition);
192 char *repl_cond_ptr = replace_condition;
193 char *cond_ptr = cond;
195 if (cond_ptr == NULL) {
196 media_content_error("memory allocation failed");
197 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
199 while (*cond_ptr != '\0') {
200 if (strlen(cond_ptr) < strlen(g_old_path)) {
201 memcpy(repl_cond_ptr, cond_ptr, strlen(cond_ptr));
204 /* replace path only and keep other condition */
205 if (memcmp(cond_ptr, g_old_path, strlen(g_old_path)) == 0) {
206 memcpy(repl_cond_ptr, tzplatform_getenv(TZ_USER_CONTENT), strlen(tzplatform_getenv(TZ_USER_CONTENT)));
207 cond_ptr += strlen(g_old_path);
208 repl_cond_ptr += strlen(tzplatform_getenv(TZ_USER_CONTENT));
210 *repl_cond_ptr = *cond_ptr;
217 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;
234 if (!STRING_VALID(g_old_path)) {
235 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &g_old_path);
236 if (ret != STORAGE_ERROR_NONE) {
237 media_content_error("storage_get_directory failed");
238 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
242 media_content_sec_debug("Old condition[%s]", condition);
243 if (((strstr(condition, "PATH") != NULL) || (strstr(condition, "path") != NULL)) && (strstr(condition, tzplatform_getenv(TZ_USER_CONTENT)) != NULL)) {
244 char *cond = strdup(condition);
245 char *repl_cond_ptr = replace_condition;
246 char *cond_ptr = cond;
248 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, g_old_path, strlen(g_old_path));
260 cond_ptr += strlen(tzplatform_getenv(TZ_USER_CONTENT));
261 repl_cond_ptr += strlen(g_old_path);
263 *repl_cond_ptr = *cond_ptr;
270 snprintf(replace_condition, MAX_QUERY_SIZE, "%s", condition);
273 if (!STRING_VALID(replace_condition)) {
274 media_content_error("replace failed");
275 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
278 media_content_sec_debug("repl cond[%s]", replace_condition);
280 return MEDIA_CONTENT_ERROR_NONE;
283 int _media_content_replace_path(const char *path, char *replace_path)
285 int ret = MEDIA_CONTENT_ERROR_NONE;
287 if (!STRING_VALID(g_old_path)) {
288 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &g_old_path);
289 if (ret != STORAGE_ERROR_NONE) {
290 media_content_error("storage_get_directory failed");
291 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
295 if (strncmp(path, g_old_path, strlen(g_old_path)) == 0) {
296 media_content_sec_debug("Old path[%s]", path);
297 snprintf(replace_path, MAX_QUERY_SIZE, "%s%s", tzplatform_getenv(TZ_USER_CONTENT), path + strlen(g_old_path));
299 snprintf(replace_path, MAX_QUERY_SIZE, "%s", path);
302 if (!STRING_VALID(replace_path)) {
303 media_content_error("replace failed");
304 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
307 return MEDIA_CONTENT_ERROR_NONE;
310 int _media_content_rollback_path(const char *path, char *replace_path)
312 int ret = MEDIA_CONTENT_ERROR_NONE;
314 if (!STRING_VALID(g_old_path)) {
315 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &g_old_path);
316 if (ret != STORAGE_ERROR_NONE) {
317 media_content_error("storage_get_directory failed");
318 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
322 if (strncmp(path, tzplatform_getenv(TZ_USER_CONTENT), strlen(tzplatform_getenv(TZ_USER_CONTENT))) == 0) {
323 media_content_sec_debug("new path[%s]", path);
324 snprintf(replace_path, MAX_QUERY_SIZE, "%s%s", g_old_path, path + strlen(tzplatform_getenv(TZ_USER_CONTENT)));
326 snprintf(replace_path, MAX_QUERY_SIZE, "%s", path);
329 if (!STRING_VALID(replace_path)) {
330 media_content_error("replace failed");
331 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
334 return MEDIA_CONTENT_ERROR_NONE;
337 #ifdef _USE_SENIOR_MODE
338 bool _media_content_is_support_senior_mode()
340 bool bSupportSeniorMode = false;
342 if (system_info_get_value_bool(SYSTEM_INFO_KEY_GET_SENIOR_MODE_SUPPORTED, &bSupportSeniorMode) != SYSTEM_INFO_ERROR_NONE) {
343 media_content_debug("Get senior mode support failed");
346 /* media_content_debug("Senior mode Support : [%d]", bSupportSeniorMode); */
347 return bSupportSeniorMode;