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 #ifdef _USE_SENIOR_MODE
23 #include <system_info.h>
24 #include <media_util_private.h>
27 static char *g_old_path = NULL;
28 #define TIZEN_USER_CONTENT_PATH tzplatform_getenv(TZ_USER_CONTENT)
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[MAX_PATH_LEN] = {0, };
98 memset(search_path, 0, sizeof(search_path));
99 if (!SAFE_STRLCPY(search_path, dir_path, sizeof(search_path))) {
100 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
101 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
104 while (STRING_VALID(search_path)) {
105 dp = opendir(search_path);
108 media_content_error("Open Directory fail");
109 media_content_sec_debug("Open fail path[%s]", search_path);
110 if (errno == EACCES || errno == EPERM)
111 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
113 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
116 media_content_retvm_if(dp == NULL, MEDIA_CONTENT_ERROR_INVALID_OPERATION, "Open Directory fail");
118 while (!readdir_r(dp, &entry, &result)) {
122 if (STRING_VALID(entry.d_name) && (strcmp(entry.d_name, scan_ignore) == 0)) {
123 media_content_error("Find Ignore path");
124 media_content_sec_debug("Ignore path[%s]", search_path);
128 /*media_content_sec_debug("entry.d_name[%s]", entry.d_name);*/
133 if (dp) closedir(dp);
140 /*If root path, Stop Scanning*/
141 if ((storage_type == MEDIA_SVC_STORAGE_INTERNAL) && (STRING_VALID(MEDIA_ROOT_PATH_INTERNAL) && strcmp(search_path, MEDIA_ROOT_PATH_INTERNAL) == 0)) {
143 } else if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL) && (STRING_VALID(MEDIA_ROOT_PATH_SDCARD)) && (strcmp(search_path, MEDIA_ROOT_PATH_SDCARD) == 0)) {
145 } else if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) && (STRING_VALID(MEDIA_ROOT_PATH_DISC)) && (strcmp(search_path, MEDIA_ROOT_PATH_DISC) == 0)) {
147 } else if (storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) {
148 char *parent_folder_path = NULL;
149 bool is_root = FALSE;
151 parent_folder_path = g_path_get_dirname(search_path);
152 if (STRING_VALID(MEDIA_ROOT_PATH_USB) && (strcmp(parent_folder_path, MEDIA_ROOT_PATH_USB) == 0))
155 SAFE_FREE(parent_folder_path);
160 #ifdef _USE_SENIOR_MODE
161 if (_media_content_is_support_senior_mode()) {
162 if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL) && (strcmp(search_path, MEDIA_ROOT_PATH_SENIOR_MODE) == 0))
167 leaf_path = strrchr(search_path, '/');
168 if (leaf_path != NULL) {
169 int seek_len = leaf_path -search_path;
170 search_path[seek_len] = '\0';
171 /*media_content_sec_debug("go to other dir [%s]", search_path);*/
173 media_content_debug("Fail to find leaf path");
179 return MEDIA_CONTENT_ERROR_NONE;
182 int _media_content_replace_path_in_condition(const char *condition, char *replace_condition, bool replace)
184 int ret = MEDIA_CONTENT_ERROR_NONE;
186 #ifdef _USE_TV_PROFILE
187 snprintf(replace_condition, MAX_QUERY_SIZE, "%s", condition);
189 char old_condition[MAX_QUERY_SIZE] = {0, };
190 char new_condition[MAX_QUERY_SIZE] = {0, };
192 unsigned int str_len = 0;
194 char *find_str = NULL;
195 char *to_replace_str = NULL;
197 if (replace == TRUE) { //change User session path to System session path
198 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &find_str);
199 if (ret != STORAGE_ERROR_NONE) {
200 media_content_error("storage_get_directory failed");
201 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
205 to_replace_str = g_strdup(TIZEN_USER_CONTENT_PATH);
206 if (!STRING_VALID(to_replace_str)) {
207 media_content_error("Get TZ_USER_CONTENT failed");
208 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
212 find_str = g_strdup(TIZEN_USER_CONTENT_PATH);
213 if (!STRING_VALID(find_str)) {
214 media_content_error("Get TZ_USER_CONTENT failed");
215 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
219 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &to_replace_str);
220 if (ret != STORAGE_ERROR_NONE) {
221 media_content_error("storage_get_directory failed");
222 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
227 memset(old_condition, 0, sizeof(old_condition));
228 memset(new_condition, 0, sizeof(new_condition));
230 media_content_sec_debug("Old condition[%s]", condition);
232 if (!SAFE_STRLCPY(new_condition, condition, sizeof(new_condition))) {
233 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
234 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
238 find = strstr(new_condition, find_str);
240 while (find != NULL) {
241 str_len = find - new_condition;
243 memset(old_condition, 0, sizeof(old_condition));
244 if (!SAFE_STRLCPY(old_condition, new_condition, sizeof(old_condition))) {
245 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
246 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
249 memset(new_condition, 0, sizeof(new_condition));
251 snprintf(new_condition, str_len + 1, "%s", old_condition);
253 SAFE_STRLCAT(new_condition, to_replace_str, sizeof(new_condition));
254 SAFE_STRLCAT(new_condition, old_condition + str_len + strlen(find_str), sizeof(new_condition));
256 find = strstr(new_condition, find_str);
259 if (!SAFE_STRLCPY(replace_condition, new_condition, MAX_QUERY_SIZE)) {
260 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
261 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
265 media_content_sec_debug("repl cond[%s]", replace_condition);
267 if (!STRING_VALID(replace_condition)) {
268 media_content_error("replace failed");
269 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
275 SAFE_FREE(to_replace_str);
281 int _media_content_replace_path(const char *path, char *replace_path)
283 #ifdef _USE_TV_PROFILE
284 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
287 int ret = MEDIA_CONTENT_ERROR_NONE;
289 if (!STRING_VALID(g_old_path)) {
290 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &g_old_path);
291 if (ret != STORAGE_ERROR_NONE) {
292 media_content_error("storage_get_directory failed");
293 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
297 if (strncmp(path, g_old_path, strlen(g_old_path)) == 0) {
298 media_content_sec_debug("Old path[%s]", path);
299 snprintf(replace_path, MAX_PATH_LEN, "%s%s", tzplatform_getenv(TZ_USER_CONTENT), path + strlen(g_old_path));
301 snprintf(replace_path, MAX_PATH_LEN, "%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 #ifdef _USE_TV_PROFILE
316 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
319 int ret = MEDIA_CONTENT_ERROR_NONE;
321 if (!STRING_VALID(g_old_path)) {
322 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &g_old_path);
323 if (ret != STORAGE_ERROR_NONE) {
324 media_content_error("storage_get_directory failed");
325 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
329 if (strncmp(path, tzplatform_getenv(TZ_USER_CONTENT), strlen(tzplatform_getenv(TZ_USER_CONTENT))) == 0) {
330 media_content_sec_debug("new path[%s]", path);
331 snprintf(replace_path, MAX_PATH_LEN, "%s%s", g_old_path, path + strlen(tzplatform_getenv(TZ_USER_CONTENT)));
333 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
337 if (!STRING_VALID(replace_path)) {
338 media_content_error("replace failed");
339 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
342 return MEDIA_CONTENT_ERROR_NONE;
345 #ifdef _USE_SENIOR_MODE
346 bool _media_content_is_support_senior_mode()
348 bool bSupportSeniorMode = false;
350 if (system_info_get_value_bool(SYSTEM_INFO_KEY_GET_SENIOR_MODE_SUPPORTED, &bSupportSeniorMode) != SYSTEM_INFO_ERROR_NONE) {
351 media_content_debug("Get senior mode support failed");
354 /* media_content_debug("Senior mode Support : [%d]", bSupportSeniorMode); */
355 return bSupportSeniorMode;