Clean up included header files
[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
18 #include <dirent.h>
19 #include <fcntl.h>
20 #include <media_info_private.h>
21 #include <storage.h>
22 #ifdef _USE_SENIOR_MODE
23 #include <system_info.h>
24 #include <media_util_private.h>
25 #endif
26
27 static char *g_old_path = NULL;
28
29 int _media_util_check_file_exist(const char *path)
30 {
31         int exist;
32
33         /* check the file exits actually */
34         exist = open(path, O_RDONLY);
35         if (exist < 0) {
36                 media_content_sec_debug("path [%s]", path);
37                 media_content_stderror("open file fail");
38                 if (errno == EACCES || errno == EPERM)
39                         return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
40                 else
41                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
42         }
43
44         close(exist);
45
46         return MEDIA_CONTENT_ERROR_NONE;
47 }
48
49 int _media_util_check_ignore_file(const char *path, bool *ignore)
50 {
51         media_content_retvm_if(!STRING_VALID(path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid path");
52
53         *ignore = FALSE;
54
55         if (strstr(path, "/.") != NULL) {
56                 *ignore = TRUE;
57                 media_content_error("hidden path");
58                 media_content_sec_debug("path : %s", path);
59         }
60
61         return MEDIA_CONTENT_ERROR_NONE;
62 }
63
64 int _media_util_check_ignore_dir(const char *dir_path, bool *ignore)
65 {
66         int ret = MEDIA_CONTENT_ERROR_NONE;
67         media_svc_storage_type_e storage_type = 0;
68         const char *scan_ignore = ".scan_ignore";
69         bool find = false;
70
71         media_content_sec_debug("dir_path : %s", dir_path);
72
73         media_content_retvm_if(!STRING_VALID(dir_path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid dir_path");
74
75         *ignore = FALSE;
76         /*1. Check Hidden Directory*/
77         if (strstr(dir_path, "/.") != NULL) {
78                 *ignore = TRUE;
79                 media_content_error("hidden path");
80                 return MEDIA_CONTENT_ERROR_NONE;
81         }
82
83         /*2. Check Scan Ignore Directory*/
84         ret = media_svc_get_storage_type(dir_path, &storage_type, tzplatform_getuid(TZ_USER_NAME));
85         if (ret != MS_MEDIA_ERR_NONE) {
86                 media_content_error("media_svc_get_storage_type failed : %d", ret);
87                 return _content_error_capi(MEDIA_CONTENT_TYPE, ret);
88         }
89
90         DIR *dp = NULL;
91         struct dirent entry;
92         struct dirent *result = NULL;
93
94         char *leaf_path = NULL;
95         char search_path[MAX_PATH_LEN] = {0, };
96
97         strncpy(search_path, dir_path, sizeof(search_path));
98         while (STRING_VALID(search_path)) {
99                 dp = opendir(search_path);
100                 if (dp == NULL) {
101                         *ignore = TRUE;
102                         media_content_error("Open Directory fail");
103                         media_content_sec_debug("Open fail path[%s]", search_path);
104                         if (errno == EACCES || errno == EPERM)
105                                 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
106                         else
107                                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
108                 }
109
110                 media_content_retvm_if(dp == NULL, MEDIA_CONTENT_ERROR_INVALID_OPERATION, "Open Directory fail");
111
112                 while (!readdir_r(dp, &entry, &result)) {
113                         if (result == NULL)
114                                 break;
115
116                         if (STRING_VALID(entry.d_name) && (strcmp(entry.d_name, scan_ignore) == 0)) {
117                                 media_content_error("Find Ignore path");
118                                 media_content_sec_debug("Ignore path[%s]", search_path);
119                                 find = TRUE;
120                                 break;
121                         } else {
122                                 /*media_content_sec_debug("entry.d_name[%s]", entry.d_name);*/
123                                 continue;
124                         }
125                 }
126
127                 if (dp) closedir(dp);
128                 dp = NULL;
129
130                 if (find) {
131                         *ignore = TRUE;
132                         break;
133                 } else {
134                         /*If root path, Stop Scanning*/
135                         if ((storage_type == MEDIA_SVC_STORAGE_INTERNAL) && (STRING_VALID(MEDIA_ROOT_PATH_INTERNAL) && strcmp(search_path, MEDIA_ROOT_PATH_INTERNAL) == 0)) {
136                                 break;
137                         } else if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL) && (STRING_VALID(MEDIA_ROOT_PATH_SDCARD)) && (strcmp(search_path, MEDIA_ROOT_PATH_SDCARD) == 0)) {
138                                 break;
139                         } else if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) && (STRING_VALID(MEDIA_ROOT_PATH_DISC)) && (strcmp(search_path, MEDIA_ROOT_PATH_DISC) == 0)) {
140                                 break;
141                         } else if (storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) {
142                                 char *parent_folder_path = NULL;
143                                 bool is_root = FALSE;
144
145                                 parent_folder_path = g_path_get_dirname(search_path);
146                                 if (STRING_VALID(MEDIA_ROOT_PATH_USB) && (strcmp(parent_folder_path, MEDIA_ROOT_PATH_USB) == 0))
147                                         is_root = TRUE;
148
149                                 SAFE_FREE(parent_folder_path);
150
151                                 if (is_root == TRUE)
152                                         break;
153                         }
154 #ifdef _USE_SENIOR_MODE
155                         if (_media_content_is_support_senior_mode()) {
156                                 if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL) && (strcmp(search_path, MEDIA_ROOT_PATH_SENIOR_MODE) == 0))
157                                         break;
158                         }
159 #endif
160
161                         leaf_path = strrchr(search_path, '/');
162                         if (leaf_path != NULL) {
163                                 int seek_len = leaf_path -search_path;
164                                 search_path[seek_len] = '\0';
165                                 /*media_content_sec_debug("go to other dir [%s]", search_path);*/
166                         } else {
167                                 media_content_debug("Fail to find leaf path");
168                                 break;
169                         }
170                 }
171         }
172
173         return MEDIA_CONTENT_ERROR_NONE;
174 }
175
176 int _media_content_replace_path_in_condition(const char *condition, char *replace_condition)
177 {
178         int ret = MEDIA_CONTENT_ERROR_NONE;
179
180         if (!STRING_VALID(g_old_path)) {
181                 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &g_old_path);
182                 if (ret != STORAGE_ERROR_NONE) {
183                         media_content_error("storage_get_directory failed");
184                         return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
185                 }
186         }
187
188         media_content_sec_debug("Old condition[%s]", condition);
189         if (((strstr(condition, "PATH") != NULL) || (strstr(condition, "path") != NULL)) && (strstr(condition, g_old_path) != NULL)) {
190                 char *cond = strdup(condition);
191                 char *repl_cond_ptr = replace_condition;
192                 char *cond_ptr = cond;
193
194                 if (cond_ptr == NULL) {
195                         media_content_error("memory allocation failed");
196                         return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
197                 }
198                 while (*cond_ptr != '\0') {
199                         if (strlen(cond_ptr) < strlen(g_old_path)) {
200                                 memcpy(repl_cond_ptr, cond_ptr, strlen(cond_ptr));
201                                 break;
202                         }
203                         /* replace path only and keep other condition */
204                         if (memcmp(cond_ptr, g_old_path, strlen(g_old_path)) == 0) {
205                                 memcpy(repl_cond_ptr, tzplatform_getenv(TZ_USER_CONTENT), strlen(tzplatform_getenv(TZ_USER_CONTENT)));
206                                 cond_ptr += strlen(g_old_path);
207                                 repl_cond_ptr += strlen(tzplatform_getenv(TZ_USER_CONTENT));
208                         } else {
209                                 *repl_cond_ptr = *cond_ptr;
210                                 cond_ptr++;
211                                 repl_cond_ptr++;
212                         }
213                 }
214                 SAFE_FREE(cond);
215         } else {
216                 snprintf(replace_condition, MAX_QUERY_SIZE, "%s", condition);
217         }
218
219         if (!STRING_VALID(replace_condition)) {
220                 media_content_error("replace failed");
221                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
222         }
223
224         media_content_sec_debug("repl cond[%s]", replace_condition);
225
226         return MEDIA_CONTENT_ERROR_NONE;
227 }
228
229 int _media_content_rollback_path_in_condition(const char *condition, char *replace_condition)
230 {
231         int ret = MEDIA_CONTENT_ERROR_NONE;
232
233         if (!STRING_VALID(g_old_path)) {
234                 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &g_old_path);
235                 if (ret != STORAGE_ERROR_NONE) {
236                         media_content_error("storage_get_directory failed");
237                         return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
238                 }
239         }
240
241         media_content_sec_debug("Old condition[%s]", condition);
242         if (((strstr(condition, "PATH") != NULL) || (strstr(condition, "path") != NULL)) && (strstr(condition, tzplatform_getenv(TZ_USER_CONTENT)) != NULL)) {
243                 char *cond = strdup(condition);
244                 char *repl_cond_ptr = replace_condition;
245                 char *cond_ptr = cond;
246
247                 if (cond_ptr == NULL) {
248                         media_content_error("memory allocation failed");
249                         return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
250                 }
251                 while (*cond_ptr != '\0') {
252                         if (strlen(cond_ptr) < strlen(tzplatform_getenv(TZ_USER_CONTENT))) {
253                                 memcpy(repl_cond_ptr, cond_ptr, strlen(cond_ptr));
254                                 break;
255                         }
256                         /* replace path only and keep other condition */
257                         if (memcmp(cond_ptr, tzplatform_getenv(TZ_USER_CONTENT), strlen(tzplatform_getenv(TZ_USER_CONTENT))) == 0) {
258                                 memcpy(repl_cond_ptr, g_old_path, strlen(g_old_path));
259                                 cond_ptr += strlen(tzplatform_getenv(TZ_USER_CONTENT));
260                                 repl_cond_ptr += strlen(g_old_path);
261                         } else {
262                                 *repl_cond_ptr = *cond_ptr;
263                                 cond_ptr++;
264                                 repl_cond_ptr++;
265                         }
266                 }
267                 SAFE_FREE(cond);
268         } else {
269                 snprintf(replace_condition, MAX_QUERY_SIZE, "%s", condition);
270         }
271
272         if (!STRING_VALID(replace_condition)) {
273                 media_content_error("replace failed");
274                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
275         }
276
277         media_content_sec_debug("repl cond[%s]", replace_condition);
278
279         return MEDIA_CONTENT_ERROR_NONE;
280 }
281
282 int _media_content_replace_path(const char *path, char *replace_path)
283 {
284 #ifdef _USE_TV_PROFILE
285         snprintf(replace_path, MAX_PATH_LEN, "%s", path);
286 #else
287
288         int ret = MEDIA_CONTENT_ERROR_NONE;
289
290         if (!STRING_VALID(g_old_path)) {
291                 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &g_old_path);
292                 if (ret != STORAGE_ERROR_NONE) {
293                         media_content_error("storage_get_directory failed");
294                         return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
295                 }
296         }
297
298         if (strncmp(path, g_old_path, strlen(g_old_path)) == 0) {
299                 media_content_sec_debug("Old path[%s]", path);
300                 snprintf(replace_path, MAX_PATH_LEN, "%s%s", tzplatform_getenv(TZ_USER_CONTENT), path + strlen(g_old_path));
301         } else {
302                 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
303         }
304 #endif
305
306         if (!STRING_VALID(replace_path)) {
307                 media_content_error("replace failed");
308                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
309         }
310
311         return MEDIA_CONTENT_ERROR_NONE;
312 }
313
314 int _media_content_rollback_path(const char *path, char *replace_path)
315 {
316 #ifdef _USE_TV_PROFILE
317                 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
318 #else
319
320         int ret = MEDIA_CONTENT_ERROR_NONE;
321
322         if (!STRING_VALID(g_old_path)) {
323                 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &g_old_path);
324                 if (ret != STORAGE_ERROR_NONE) {
325                         media_content_error("storage_get_directory failed");
326                         return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
327                 }
328         }
329
330         if (strncmp(path, tzplatform_getenv(TZ_USER_CONTENT), strlen(tzplatform_getenv(TZ_USER_CONTENT))) == 0) {
331                 media_content_sec_debug("new path[%s]", path);
332                 snprintf(replace_path, MAX_PATH_LEN, "%s%s", g_old_path, path + strlen(tzplatform_getenv(TZ_USER_CONTENT)));
333         } else {
334                 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
335         }
336 #endif
337
338         if (!STRING_VALID(replace_path)) {
339                 media_content_error("replace failed");
340                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
341         }
342
343         return MEDIA_CONTENT_ERROR_NONE;
344 }
345
346 #ifdef _USE_SENIOR_MODE
347 bool _media_content_is_support_senior_mode()
348 {
349         bool bSupportSeniorMode = false;
350
351         if (system_info_get_value_bool(SYSTEM_INFO_KEY_GET_SENIOR_MODE_SUPPORTED, &bSupportSeniorMode) != SYSTEM_INFO_ERROR_NONE) {
352                 media_content_debug("Get senior mode support failed");
353                 return false;
354         }
355         /* media_content_debug("Senior mode Support : [%d]", bSupportSeniorMode); */
356         return bSupportSeniorMode;
357 }
358 #endif
359