[ACR-1167] Exception handling when path contains symbolic link
[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 #include <system_info.h>
23 #ifdef _USE_SENIOR_MODE
24 #include <media_util_private.h>
25 #endif
26
27 static int MEDIA_CONTENT_OTHER_SUPPORT = -1;
28
29 bool _media_util_check_support_media_type(const char *path)
30 {
31         int ret = SYSTEM_INFO_ERROR_NONE;
32         int media_type = -1;
33         bool is_supported = false;
34
35         media_content_retvm_if(!STRING_VALID(path), false, "path is empty");
36
37         if (MEDIA_CONTENT_OTHER_SUPPORT == -1) {
38                 ret = system_info_get_platform_bool("http://tizen.org/feature/content.scanning.others", &is_supported);
39                 if (ret != SYSTEM_INFO_ERROR_NONE) {
40                         media_content_debug("SYSTEM_INFO_ERROR: content.scanning.others [%d]", ret);
41                         return false;
42                 }
43
44                 MEDIA_CONTENT_OTHER_SUPPORT = is_supported;
45         }
46
47         /* If not, check media type */
48         if (!MEDIA_CONTENT_OTHER_SUPPORT) {
49                 ret = media_svc_get_media_type(path, &media_type);
50                 media_content_retvm_if(ret != MS_MEDIA_ERR_NONE, false, "Failed to get media type");
51
52                 if (media_type == MEDIA_CONTENT_TYPE_OTHERS)
53                         return false;
54         }
55
56         return true;
57 }
58
59 int _media_util_check_file_exist(const char *path)
60 {
61         int exist;
62
63         /* check the file exits actually */
64         exist = open(path, O_RDONLY);
65         if (exist < 0) {
66                 media_content_sec_debug("path [%s]", path);
67                 media_content_stderror("open file fail");
68                 if (errno == EACCES || errno == EPERM)
69                         return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
70                 else
71                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
72         }
73
74         close(exist);
75
76         return MEDIA_CONTENT_ERROR_NONE;
77 }
78
79 int _media_util_check_ignore_file(const char *path, bool *ignore)
80 {
81         media_content_retvm_if(!STRING_VALID(path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid path");
82
83         *ignore = FALSE;
84         char *tmp_path = NULL;
85
86 #ifndef _USE_TVPD_MODE
87         char replace[MAX_PATH_LEN] = {0, };
88 #endif
89
90         /* Check symbolic link file */
91         if (g_file_test(path, G_FILE_TEST_IS_SYMLINK)) {
92                 *ignore = TRUE;
93                 media_content_error("symbolic link(file)");
94                 media_content_sec_debug("path : %s", path);
95                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
96         }
97
98         /* Check hidden path */
99         if (strstr(path, "/.") != NULL) {
100                 *ignore = TRUE;
101                 media_content_error("hidden path");
102                 media_content_sec_debug("path : %s", path);
103                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
104         }
105
106         /* Check symbolic directory */
107         tmp_path = realpath(path, NULL);
108
109 #ifdef _USE_TVPD_MODE
110         if (g_strcmp0(tmp_path, path) != 0) {
111                 *ignore = TRUE;
112                 SAFE_FREE(tmp_path);
113                 media_content_error("symbolic link(directory)");
114                 media_content_sec_debug("path : %s", path);
115                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
116         }
117 #else
118         if (g_str_has_prefix(tmp_path, MEDIA_SHARE_PATH)) {
119                 /* If shared dirctory, it should be change path to TZ_USER_SHARE from realpath */
120                 snprintf(replace, MAX_PATH_LEN, "%s%s", tzplatform_getenv(TZ_USER_MEDIASHARED), tmp_path + strlen(MEDIA_SHARE_PATH));
121                 if (g_strcmp0(replace, path) != 0) {
122                         *ignore = TRUE;
123                         SAFE_FREE(tmp_path);
124                         media_content_error("symbolic link(directory)");
125                         media_content_sec_debug("path : %s", path);
126                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
127                 }
128         } else {
129                 if (g_strcmp0(tmp_path, path) != 0) {
130                         *ignore = TRUE;
131                         SAFE_FREE(tmp_path);
132                         media_content_error("symbolic link(directory)");
133                         media_content_sec_debug("path : %s", path);
134                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
135                 }
136         }
137 #endif
138         SAFE_FREE(tmp_path);
139
140         return MEDIA_CONTENT_ERROR_NONE;
141 }
142
143 int _media_util_check_ignore_dir(const char *dir_path, bool *ignore)
144 {
145         int ret = MEDIA_CONTENT_ERROR_NONE;
146         media_svc_storage_type_e storage_type = 0;
147         const char *scan_ignore = ".scan_ignore";
148         bool find = false;
149         GDir *dir = NULL;
150         GError *error = NULL;
151         const char *name;
152
153         media_content_sec_debug("dir_path : %s", dir_path);
154
155         media_content_retvm_if(!STRING_VALID(dir_path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid dir_path");
156
157         *ignore = FALSE;
158         /*1. Check Hidden Directory*/
159         if (strstr(dir_path, "/.") != NULL) {
160                 *ignore = TRUE;
161                 media_content_error("hidden path");
162                 return MEDIA_CONTENT_ERROR_NONE;
163         }
164
165         /*2. Check Scan Ignore Directory*/
166         ret = media_svc_get_storage_type(dir_path, &storage_type, _content_get_uid());
167         if (ret != MS_MEDIA_ERR_NONE) {
168                 media_content_error("media_svc_get_storage_type failed : %d", ret);
169                 return _content_error_capi(MEDIA_CONTENT_TYPE, ret);
170         }
171
172         char *leaf_path = NULL;
173         char search_path[MAX_PATH_LEN] = {0, };
174
175         memset(search_path, 0, sizeof(search_path));
176         if (!SAFE_STRLCPY(search_path, dir_path, sizeof(search_path))) {
177                 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
178                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
179         }
180
181         while (STRING_VALID(search_path)) {
182                 dir = g_dir_open(search_path, 0, &error);
183                 if (dir != NULL && error == NULL) {
184                         while ((name = g_dir_read_name(dir))) {
185                                 if (g_strcmp0(name, scan_ignore) == 0) {
186                                         media_content_sec_debug("Ignore path[%s]", search_path);
187                                         find = TRUE;
188                                         break;
189                                 }
190                         }
191                 } else {
192                         *ignore = TRUE;
193                         media_content_error("Open Directory fail");
194                         if (error->code == G_FILE_ERROR_ACCES)
195                                 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
196                         else
197                                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
198                 }
199
200                 if (dir)
201                         g_dir_close(dir);
202
203                 if (find) {
204                         *ignore = TRUE;
205                         break;
206                 } else {
207                         /*If root path, Stop Scanning*/
208                         if ((storage_type == MEDIA_SVC_STORAGE_INTERNAL) && (STRING_VALID(MEDIA_ROOT_PATH_INTERNAL) && g_strcmp0(search_path, MEDIA_ROOT_PATH_INTERNAL) == 0)) {
209                                 break;
210                         } else if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL) && (STRING_VALID(MEDIA_ROOT_PATH_SDCARD)) && (g_strcmp0(search_path, MEDIA_ROOT_PATH_SDCARD) == 0)) {
211                                 break;
212                         } else if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) && (STRING_VALID(MEDIA_ROOT_PATH_DISC)) && (g_strcmp0(search_path, MEDIA_ROOT_PATH_DISC) == 0)) {
213                                 break;
214                         } else if (storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) {
215                                 char *parent_folder_path = NULL;
216                                 bool is_root = FALSE;
217
218                                 parent_folder_path = g_path_get_dirname(search_path);
219                                 if (STRING_VALID(MEDIA_ROOT_PATH_USB) && STRING_VALID(parent_folder_path) && (g_strcmp0(parent_folder_path, MEDIA_ROOT_PATH_USB) == 0))
220                                         is_root = TRUE;
221
222                                 SAFE_FREE(parent_folder_path);
223
224                                 if (is_root == TRUE)
225                                         break;
226                         }
227 #ifdef _USE_SENIOR_MODE
228                         if (_media_content_is_support_senior_mode()) {
229                                 if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL) && (g_strcmp0(search_path, MEDIA_ROOT_PATH_SENIOR_MODE) == 0))
230                                         break;
231                         }
232 #endif
233
234                         leaf_path = strrchr(search_path, '/');
235                         if (leaf_path != NULL) {
236                                 int seek_len = leaf_path -search_path;
237                                 search_path[seek_len] = '\0';
238                                 /*media_content_sec_debug("go to other dir [%s]", search_path);*/
239                         } else {
240                                 media_content_debug("Fail to find leaf path");
241                                 break;
242                         }
243                 }
244         }
245
246         return MEDIA_CONTENT_ERROR_NONE;
247 }
248
249 int _media_content_replace_path_in_condition(const char *condition, char *replace_condition, bool replace)
250 {
251         int ret = MEDIA_CONTENT_ERROR_NONE;
252
253 #ifdef _USE_TVPD_MODE
254         snprintf(replace_condition, MAX_QUERY_SIZE, "%s", condition);
255 #else
256         char old_condition[MAX_QUERY_SIZE] = {0, };
257         char new_condition[MAX_QUERY_SIZE] = {0, };
258         char *find = NULL;
259         unsigned int str_len = 0;
260
261         char *find_str = NULL;
262         char *to_replace_str = NULL;
263
264         if (replace == TRUE) {  //change User session path to System session path
265                 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &find_str);
266                 if (ret != STORAGE_ERROR_NONE) {
267                         media_content_error("storage_get_directory failed");
268                         ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
269                         goto ERROR;
270                 }
271
272                 to_replace_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL);
273                 if (!STRING_VALID(to_replace_str)) {
274                         media_content_error("Get TZ_USER_CONTENT failed");
275                         ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
276                         goto ERROR;
277                 }
278         } else {
279                 find_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL);
280                 if (!STRING_VALID(find_str)) {
281                         media_content_error("Get TZ_USER_CONTENT failed");
282                         ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
283                         goto ERROR;
284                 }
285
286                 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &to_replace_str);
287                 if (ret != STORAGE_ERROR_NONE) {
288                         media_content_error("storage_get_directory failed");
289                         ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
290                         goto ERROR;
291                 }
292         }
293
294         memset(old_condition, 0, sizeof(old_condition));
295         memset(new_condition, 0, sizeof(new_condition));
296
297         media_content_sec_debug("Old condition[%s]", condition);
298
299         if (!SAFE_STRLCPY(new_condition, condition, sizeof(new_condition))) {
300                 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
301                 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
302                 goto ERROR;
303         }
304
305         if (g_strcmp0(find_str, to_replace_str))
306                 find = strstr(new_condition, find_str);
307
308         while (find != NULL) {
309                 str_len = find - new_condition;
310
311                 memset(old_condition, 0, sizeof(old_condition));
312                 if (!SAFE_STRLCPY(old_condition, new_condition, sizeof(old_condition))) {
313                         media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
314                         ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
315                         goto ERROR;
316                 }
317                 memset(new_condition, 0, sizeof(new_condition));
318
319                 snprintf(new_condition, str_len + 1, "%s", old_condition);
320
321                 SAFE_STRLCAT(new_condition, to_replace_str, sizeof(new_condition));
322                 SAFE_STRLCAT(new_condition, old_condition + str_len + strlen(find_str), sizeof(new_condition));
323
324                 find = strstr(new_condition, find_str);
325         }
326
327         if (!SAFE_STRLCPY(replace_condition, new_condition, MAX_QUERY_SIZE)) {
328                 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
329                 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
330                 goto ERROR;
331         }
332
333         media_content_sec_debug("repl cond[%s]", replace_condition);
334
335         if (!STRING_VALID(replace_condition)) {
336                 media_content_error("replace failed");
337                 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
338                 goto ERROR;
339         }
340
341 ERROR:
342         SAFE_FREE(find_str);
343         SAFE_FREE(to_replace_str);
344 #endif
345
346         return ret;
347 }
348
349 int _media_content_replace_path(const char *path, char *replace_path)
350 {
351 #ifdef _USE_TVPD_MODE
352         snprintf(replace_path, MAX_PATH_LEN, "%s", path);
353 #else
354
355         int ret = MEDIA_CONTENT_ERROR_NONE;
356         char *old_internal_path =  NULL;
357
358         ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &old_internal_path);
359         if (ret != STORAGE_ERROR_NONE) {
360                 media_content_error("storage_get_directory failed");
361                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
362         }
363
364         if (strncmp(path, old_internal_path, strlen(old_internal_path)) == 0) {
365                 media_content_sec_debug("Old path[%s]", path);
366                 snprintf(replace_path, MAX_PATH_LEN, "%s%s", MEDIA_ROOT_PATH_INTERNAL, path + strlen(old_internal_path));
367         } else {
368                 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
369         }
370
371         SAFE_FREE(old_internal_path);
372 #endif
373
374         if (!STRING_VALID(replace_path)) {
375                 media_content_error("replace failed");
376                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
377         }
378
379         return MEDIA_CONTENT_ERROR_NONE;
380 }
381
382 int _media_content_rollback_path(const char *path, char *replace_path)
383 {
384 #ifdef _USE_TVPD_MODE
385                 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
386 #else
387
388         int ret = MEDIA_CONTENT_ERROR_NONE;
389         char *old_internal_path =  NULL;
390
391         ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &old_internal_path);
392         if (ret != STORAGE_ERROR_NONE) {
393                 media_content_error("storage_get_directory failed");
394                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
395         }
396
397         if (strncmp(path, MEDIA_ROOT_PATH_INTERNAL, strlen(MEDIA_ROOT_PATH_INTERNAL)) == 0) {
398                 media_content_sec_debug("new path[%s]", path);
399                 snprintf(replace_path, MAX_PATH_LEN, "%s%s", old_internal_path, path + strlen(MEDIA_ROOT_PATH_INTERNAL));
400         } else {
401                 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
402         }
403
404         SAFE_FREE(old_internal_path);
405 #endif
406
407         if (!STRING_VALID(replace_path)) {
408                 media_content_error("replace failed");
409                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
410         }
411
412         return MEDIA_CONTENT_ERROR_NONE;
413 }
414
415 #ifdef _USE_SENIOR_MODE
416 bool _media_content_is_support_senior_mode()
417 {
418         bool bSupportSeniorMode = false;
419
420         if (system_info_get_value_bool(SYSTEM_INFO_KEY_GET_SENIOR_MODE_SUPPORTED, &bSupportSeniorMode) != SYSTEM_INFO_ERROR_NONE) {
421                 media_content_debug("Get senior mode support failed");
422                 return false;
423         }
424         /* media_content_debug("Senior mode Support : [%d]", bSupportSeniorMode); */
425         return bSupportSeniorMode;
426 }
427 #endif
428