Add define MAX_PATH_LEN and rearrage MAX_QUERY_SIZE related code
[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[MAX_PATH_LEN] = {0, };
99
100         strncpy(search_path, dir_path, sizeof(search_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) && (STRING_VALID(MEDIA_ROOT_PATH_DISC)) && (strcmp(search_path, MEDIA_ROOT_PATH_DISC) == 0)) {
143                                 break;
144                         } else if (storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) {
145                                 char *parent_folder_path = NULL;
146                                 bool is_root = FALSE;
147
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))
150                                         is_root = TRUE;
151
152                                 SAFE_FREE(parent_folder_path);
153
154                                 if (is_root == TRUE)
155                                         break;
156                         }
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))
160                                         break;
161                         }
162 #endif
163
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);*/
169                         } else {
170                                 media_content_debug("Fail to find leaf path");
171                                 break;
172                         }
173                 }
174         }
175
176         return MEDIA_CONTENT_ERROR_NONE;
177 }
178
179 int _media_content_replace_path_in_condition(const char *condition, char *replace_condition)
180 {
181         int ret = MEDIA_CONTENT_ERROR_NONE;
182
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;
188                 }
189         }
190
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;
196
197                 if (cond_ptr == NULL) {
198                         media_content_error("memory allocation failed");
199                         return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
200                 }
201                 while (*cond_ptr != '\0') {
202                         if (strlen(cond_ptr) < strlen(g_old_path)) {
203                                 memcpy(repl_cond_ptr, cond_ptr, strlen(cond_ptr));
204                                 break;
205                         }
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));
211                         } else {
212                                 *repl_cond_ptr = *cond_ptr;
213                                 cond_ptr++;
214                                 repl_cond_ptr++;
215                         }
216                 }
217                 SAFE_FREE(cond);
218         } else {
219                 snprintf(replace_condition, MAX_QUERY_SIZE, "%s", condition);
220         }
221
222         if (!STRING_VALID(replace_condition)) {
223                 media_content_error("replace failed");
224                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
225         }
226
227         media_content_sec_debug("repl cond[%s]", replace_condition);
228
229         return MEDIA_CONTENT_ERROR_NONE;
230 }
231
232 int _media_content_rollback_path_in_condition(const char *condition, char *replace_condition)
233 {
234         int ret = MEDIA_CONTENT_ERROR_NONE;
235
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;
241                 }
242         }
243
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;
249
250                 if (cond_ptr == NULL) {
251                         media_content_error("memory allocation failed");
252                         return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
253                 }
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));
257                                 break;
258                         }
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);
264                         } else {
265                                 *repl_cond_ptr = *cond_ptr;
266                                 cond_ptr++;
267                                 repl_cond_ptr++;
268                         }
269                 }
270                 SAFE_FREE(cond);
271         } else {
272                 snprintf(replace_condition, MAX_QUERY_SIZE, "%s", condition);
273         }
274
275         if (!STRING_VALID(replace_condition)) {
276                 media_content_error("replace failed");
277                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
278         }
279
280         media_content_sec_debug("repl cond[%s]", replace_condition);
281
282         return MEDIA_CONTENT_ERROR_NONE;
283 }
284
285 int _media_content_replace_path(const char *path, char *replace_path)
286 {
287         int ret = MEDIA_CONTENT_ERROR_NONE;
288
289 #ifdef _USE_TV_PROFILE
290         snprintf(replace_path, MAX_PATH_LEN, "%s", path);
291 #else
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;
297                 }
298         }
299
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_PATH_LEN, "%s%s", tzplatform_getenv(TZ_USER_CONTENT), path + strlen(g_old_path));
303         } else {
304                 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
305         }
306 #endif
307
308         if (!STRING_VALID(replace_path)) {
309                 media_content_error("replace failed");
310                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
311         }
312
313         return MEDIA_CONTENT_ERROR_NONE;
314 }
315
316 int _media_content_rollback_path(const char *path, char *replace_path)
317 {
318         int ret = MEDIA_CONTENT_ERROR_NONE;
319
320 #ifdef _USE_TV_PROFILE
321                 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
322 #else
323
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;
329                 }
330         }
331
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_PATH_LEN, "%s%s", g_old_path, path + strlen(tzplatform_getenv(TZ_USER_CONTENT)));
335         } else {
336                 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
337         }
338 #endif
339
340         if (!STRING_VALID(replace_path)) {
341                 media_content_error("replace failed");
342                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
343         }
344
345         return MEDIA_CONTENT_ERROR_NONE;
346 }
347
348 #ifdef _USE_SENIOR_MODE
349 bool _media_content_is_support_senior_mode()
350 {
351         bool bSupportSeniorMode = false;
352
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");
355                 return false;
356         }
357         /* media_content_debug("Senior mode Support : [%d]", bSupportSeniorMode); */
358         return bSupportSeniorMode;
359 }
360 #endif
361