Fix logic when get the internal path
[platform/core/api/media-content.git] / src / media_util_private.c
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #include <dirent.h>
18 #include <string.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 #include <fcntl.h>
22 #include <media_util_private.h>
23 #include <media_info_private.h>
24 #include <media_content_type.h>
25 #include <storage.h>
26 #ifdef _USE_TV_PROFILE
27 #include <system_info.h>
28 #endif
29
30 static char *g_old_path = NULL;
31
32 int _media_util_check_file_exist(const char *path)
33 {
34         int exist;
35
36         /* check the file exits actually */
37         exist = open(path, O_RDONLY);
38         if (exist < 0) {
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;
43                 else
44                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
45         }
46
47         close(exist);
48
49         return MEDIA_CONTENT_ERROR_NONE;
50 }
51
52 int _media_util_check_ignore_file(const char *path, bool *ignore)
53 {
54         media_content_retvm_if(!STRING_VALID(path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid path");
55
56         *ignore = FALSE;
57
58         if (strstr(path, "/.") != NULL) {
59                 *ignore = TRUE;
60                 media_content_error("hidden path");
61                 media_content_sec_debug("path : %s", path);
62         }
63
64         return MEDIA_CONTENT_ERROR_NONE;
65 }
66
67 int _media_util_check_ignore_dir(const char *dir_path, bool *ignore)
68 {
69         int ret = MEDIA_CONTENT_ERROR_NONE;
70         media_svc_storage_type_e storage_type = 0;
71         const char *scan_ignore = ".scan_ignore";
72         bool find = false;
73
74         media_content_sec_debug("dir_path : %s", dir_path);
75
76         media_content_retvm_if(!STRING_VALID(dir_path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid dir_path");
77
78         *ignore = FALSE;
79         /*1. Check Hidden Directory*/
80         if (strstr(dir_path, "/.") != NULL) {
81                 *ignore = TRUE;
82                 media_content_error("hidden path");
83                 return MEDIA_CONTENT_ERROR_NONE;
84         }
85
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);
91         }
92
93         DIR *dp = NULL;
94         struct dirent entry;
95         struct dirent *result = NULL;
96
97         char *leaf_path = NULL;
98         char search_path[4096] = {0, };
99
100         strncpy(search_path, dir_path, strlen(dir_path));
101         while (STRING_VALID(search_path)) {
102                 dp = opendir(search_path);
103                 if (dp == NULL) {
104                         *ignore = TRUE;
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;
109                         else
110                                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
111                 }
112
113                 media_content_retvm_if(dp == NULL, MEDIA_CONTENT_ERROR_INVALID_OPERATION, "Open Directory fail");
114
115                 while (!readdir_r(dp, &entry, &result)) {
116                         if (result == NULL)
117                                 break;
118
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);
122                                 find = TRUE;
123                                 break;
124                         } else {
125                                 /*media_content_sec_debug("entry.d_name[%s]", entry.d_name);*/
126                                 continue;
127                         }
128                 }
129
130                 if (dp) closedir(dp);
131                 dp = NULL;
132
133                 if (find) {
134                         *ignore = TRUE;
135                         break;
136                 } else {
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)) {
139                                 break;
140                         } else if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL) && (STRING_VALID(MEDIA_ROOT_PATH_SDCARD)) && (strcmp(search_path, MEDIA_ROOT_PATH_SDCARD) == 0)) {
141                                 break;
142                         } else if (storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) {
143                                 char *parent_folder_path = NULL;
144                                 bool is_root = FALSE;
145
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))
148                                         is_root = TRUE;
149
150                                 SAFE_FREE(parent_folder_path);
151
152                                 if (is_root == TRUE)
153                                         break;
154                         }
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))
158                                         break;
159                         }
160 #endif
161
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);*/
167                         } else {
168                                 media_content_debug("Fail to find leaf path");
169                                 break;
170                         }
171                 }
172         }
173
174         return MEDIA_CONTENT_ERROR_NONE;
175 }
176
177 int _media_content_replace_path_in_condition(const char *condition, char *replace_condition)
178 {
179         int ret = MEDIA_CONTENT_ERROR_NONE;
180
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;
186                 }
187         }
188
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;
194
195                 if (cond_ptr == NULL) {
196                         media_content_error("memory allocation failed");
197                         return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
198                 }
199                 while (*cond_ptr != '\0') {
200                         if (strlen(cond_ptr) < strlen(g_old_path)) {
201                                 memcpy(repl_cond_ptr, cond_ptr, strlen(cond_ptr));
202                                 break;
203                         }
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));
209                         } else {
210                                 *repl_cond_ptr = *cond_ptr;
211                                 cond_ptr++;
212                                 repl_cond_ptr++;
213                         }
214                 }
215                 SAFE_FREE(cond);
216         } else {
217                 snprintf(replace_condition, MAX_QUERY_SIZE, "%s", condition);
218         }
219
220         if (!STRING_VALID(replace_condition)) {
221                 media_content_error("replace failed");
222                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
223         }
224
225         media_content_sec_debug("repl cond[%s]", replace_condition);
226
227         return MEDIA_CONTENT_ERROR_NONE;
228 }
229
230 int _media_content_rollback_path_in_condition(const char *condition, char *replace_condition)
231 {
232         int ret = MEDIA_CONTENT_ERROR_NONE;
233
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;
239                 }
240         }
241
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;
247
248                 if (cond_ptr == NULL) {
249                         media_content_error("memory allocation failed");
250                         return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
251                 }
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));
255                                 break;
256                         }
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);
262                         } else {
263                                 *repl_cond_ptr = *cond_ptr;
264                                 cond_ptr++;
265                                 repl_cond_ptr++;
266                         }
267                 }
268                 SAFE_FREE(cond);
269         } else {
270                 snprintf(replace_condition, MAX_QUERY_SIZE, "%s", condition);
271         }
272
273         if (!STRING_VALID(replace_condition)) {
274                 media_content_error("replace failed");
275                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
276         }
277
278         media_content_sec_debug("repl cond[%s]", replace_condition);
279
280         return MEDIA_CONTENT_ERROR_NONE;
281 }
282
283 int _media_content_replace_path(const char *path, char *replace_path)
284 {
285         int ret = MEDIA_CONTENT_ERROR_NONE;
286
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;
292                 }
293         }
294
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));
298         } else {
299                 snprintf(replace_path, MAX_QUERY_SIZE, "%s", path);
300         }
301
302         if (!STRING_VALID(replace_path)) {
303                 media_content_error("replace failed");
304                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
305         }
306
307         return MEDIA_CONTENT_ERROR_NONE;
308 }
309
310 int _media_content_rollback_path(const char *path, char *replace_path)
311 {
312         int ret = MEDIA_CONTENT_ERROR_NONE;
313
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;
319                 }
320         }
321
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)));
325         } else {
326                 snprintf(replace_path, MAX_QUERY_SIZE, "%s", path);
327         }
328
329         if (!STRING_VALID(replace_path)) {
330                 media_content_error("replace failed");
331                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
332         }
333
334         return MEDIA_CONTENT_ERROR_NONE;
335 }
336
337 #ifdef _USE_SENIOR_MODE
338 bool _media_content_is_support_senior_mode()
339 {
340         bool bSupportSeniorMode = false;
341
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");
344                 return false;
345         }
346         /* media_content_debug("Senior mode Support : [%d]", bSupportSeniorMode); */
347         return bSupportSeniorMode;
348 }
349 #endif
350