add new api: insert folder item into database
[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 void _media_util_trim_path(const char *input_path, char **output_path)
80 {
81         char buf[4096] = {0,};
82         char tmp[4096] = {0,};
83         char *pos = NULL;
84
85         memset(buf, 0, sizeof(buf));
86         if (!SAFE_STRLCPY(buf, input_path, sizeof(buf)))
87                 media_content_sec_debug("Truncation occurred[%d]", strlen(input_path));
88
89         while ((pos = strstr(buf, "//")) != NULL) {
90                 memset(tmp, 0, sizeof(tmp));
91                 if (!SAFE_STRLCPY(tmp, buf, pos - buf + 1))
92                         media_content_sec_debug("Truncation occurred");
93                 SAFE_STRLCAT(tmp, pos + 1, sizeof(tmp));
94
95                 memset(buf, 0, sizeof(buf));
96                 if (!SAFE_STRLCPY(buf, tmp, sizeof(buf)))
97                         media_content_sec_debug("Truncation occurred[%d]", strlen(tmp));
98         }
99
100         if (g_str_has_suffix(buf, "/"))
101                 *output_path = g_strndup(buf, strlen(buf) - 1);
102         else
103                 *output_path = g_strdup(buf);
104 }
105
106 int _media_util_check_ignore_file(const char *path, bool *ignore)
107 {
108         media_content_retvm_if(!STRING_VALID(path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid path");
109
110         *ignore = FALSE;
111         char *tmp_path = NULL;
112         char *org_path = NULL;
113
114 #ifndef _USE_TVPD_MODE
115         char replace[MAX_PATH_LEN] = {0, };
116 #endif
117
118         /* Check is exist (It may be the path to the deleted file) */
119         if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
120                 media_content_sec_debug("removed path[%s]", path);
121                 return MEDIA_CONTENT_ERROR_NONE;
122         }
123
124         /* Check symbolic link file */
125         if (g_file_test(path, G_FILE_TEST_IS_SYMLINK)) {
126                 *ignore = TRUE;
127                 media_content_error("symbolic link(file)");
128                 media_content_sec_debug("path : %s", path);
129                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
130         }
131
132         /* Check hidden path */
133         if (strstr(path, "/.") != NULL) {
134                 *ignore = TRUE;
135                 media_content_error("hidden path");
136                 media_content_sec_debug("path : %s", path);
137                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
138         }
139
140         /* Check symbolic directory */
141         tmp_path = realpath(path, NULL);
142         /* Get trimmed path */
143         _media_util_trim_path(path, &org_path);
144
145 #ifdef _USE_TVPD_MODE
146         if (g_strcmp0(tmp_path, org_path) != 0) {
147                 *ignore = TRUE;
148                 media_content_error("symbolic link(directory)");
149                 media_content_sec_debug("path[%s] real[%s]", org_path, tmp_path);
150                 SAFE_FREE(tmp_path);
151                 SAFE_FREE(org_path);
152                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
153         }
154 #else
155         if (g_str_has_prefix(tmp_path, MEDIA_SHARE_PATH)) {
156                 /* If shared dirctory, it should be change path to TZ_USER_SHARE from realpath */
157                 snprintf(replace, MAX_PATH_LEN, "%s%s", tzplatform_getenv(TZ_USER_MEDIASHARED), tmp_path + strlen(MEDIA_SHARE_PATH));
158                 if (g_strcmp0(replace, org_path) != 0) {
159                         *ignore = TRUE;
160                         media_content_error("symbolic link(directory)");
161                         media_content_sec_debug("path[%s] real[%s]", org_path, tmp_path);
162                         SAFE_FREE(tmp_path);
163                         SAFE_FREE(org_path);
164                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
165                 }
166         } else {
167                 if (g_strcmp0(tmp_path, org_path) != 0) {
168                         *ignore = TRUE;
169                         media_content_error("symbolic link(directory)");
170                         media_content_sec_debug("path[%s] real[%s]", org_path, tmp_path);
171                         SAFE_FREE(tmp_path);
172                         SAFE_FREE(org_path);
173                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
174                 }
175         }
176 #endif
177         SAFE_FREE(tmp_path);
178         SAFE_FREE(org_path);
179
180         return MEDIA_CONTENT_ERROR_NONE;
181 }
182
183 int _media_util_check_ignore_dir(const char *dir_path, bool *ignore)
184 {
185         int ret = MEDIA_CONTENT_ERROR_NONE;
186         media_svc_storage_type_e storage_type = 0;
187         const char *scan_ignore = ".scan_ignore";
188         bool find = false;
189         GDir *dir = NULL;
190         GError *error = NULL;
191         const char *name;
192
193         media_content_sec_debug("dir_path : %s", dir_path);
194
195         media_content_retvm_if(!STRING_VALID(dir_path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid dir_path");
196
197         *ignore = FALSE;
198         /*1. Check Hidden Directory*/
199         if (strstr(dir_path, "/.") != NULL) {
200                 *ignore = TRUE;
201                 media_content_error("hidden path");
202                 return MEDIA_CONTENT_ERROR_NONE;
203         }
204
205         /*2. Check Scan Ignore Directory*/
206         ret = media_svc_get_storage_type(dir_path, &storage_type, _content_get_uid());
207         if (ret != MS_MEDIA_ERR_NONE) {
208                 media_content_error("media_svc_get_storage_type failed : %d", ret);
209                 return _content_error_capi(MEDIA_CONTENT_TYPE, ret);
210         }
211
212         char *leaf_path = NULL;
213         char search_path[MAX_PATH_LEN] = {0, };
214
215         memset(search_path, 0, sizeof(search_path));
216         if (!SAFE_STRLCPY(search_path, dir_path, sizeof(search_path))) {
217                 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
218                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
219         }
220
221         while (STRING_VALID(search_path)) {
222                 dir = g_dir_open(search_path, 0, &error);
223                 if (dir != NULL && error == NULL) {
224                         while ((name = g_dir_read_name(dir))) {
225                                 if (g_strcmp0(name, scan_ignore) == 0) {
226                                         media_content_sec_debug("Ignore path[%s]", search_path);
227                                         find = TRUE;
228                                         break;
229                                 }
230                         }
231                 } else {
232                         *ignore = TRUE;
233                         media_content_error("Open Directory fail");
234                         if (error->code == G_FILE_ERROR_ACCES) {
235                                 g_error_free(error);
236                                 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
237                         } else {
238                                 g_error_free(error);
239                                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
240                         }
241                 }
242
243                 if (dir)
244                         g_dir_close(dir);
245
246                 if (find) {
247                         *ignore = TRUE;
248                         break;
249                 } else {
250                         /*If root path, Stop Scanning*/
251                         if ((storage_type == MEDIA_SVC_STORAGE_INTERNAL) && (STRING_VALID(MEDIA_ROOT_PATH_INTERNAL) && g_strcmp0(search_path, MEDIA_ROOT_PATH_INTERNAL) == 0)) {
252                                 break;
253                         } else if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL) && (STRING_VALID(MEDIA_ROOT_PATH_SDCARD)) && (g_strcmp0(search_path, MEDIA_ROOT_PATH_SDCARD) == 0)) {
254                                 break;
255                         } else if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) && (STRING_VALID(MEDIA_ROOT_PATH_DISC)) && (g_strcmp0(search_path, MEDIA_ROOT_PATH_DISC) == 0)) {
256                                 break;
257                         } else if (storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) {
258                                 char *parent_folder_path = NULL;
259                                 bool is_root = FALSE;
260
261                                 parent_folder_path = g_path_get_dirname(search_path);
262                                 if (STRING_VALID(MEDIA_ROOT_PATH_USB) && STRING_VALID(parent_folder_path) && (g_strcmp0(parent_folder_path, MEDIA_ROOT_PATH_USB) == 0))
263                                         is_root = TRUE;
264
265                                 SAFE_FREE(parent_folder_path);
266
267                                 if (is_root == TRUE)
268                                         break;
269                         }
270 #ifdef _USE_SENIOR_MODE
271                         if (_media_content_is_support_senior_mode()) {
272                                 if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL) && (g_strcmp0(search_path, MEDIA_ROOT_PATH_SENIOR_MODE) == 0))
273                                         break;
274                         }
275 #endif
276
277                         leaf_path = strrchr(search_path, '/');
278                         if (leaf_path != NULL) {
279                                 int seek_len = leaf_path -search_path;
280                                 search_path[seek_len] = '\0';
281                                 /*media_content_sec_debug("go to other dir [%s]", search_path);*/
282                         } else {
283                                 media_content_debug("Fail to find leaf path");
284                                 break;
285                         }
286                 }
287         }
288
289         return MEDIA_CONTENT_ERROR_NONE;
290 }
291
292 int _media_content_check_dir(const char *path)
293 {
294         DIR *dp = NULL;
295         char *real = NULL;
296         char *origin = NULL;
297 #ifndef _USE_TVPD_MODE
298         char result_path[MAX_PATH_LEN] = {0, };
299 #endif
300         dp = opendir(path);
301         if (dp == NULL) {
302                 media_content_sec_error("path [%s]", path);
303                 media_content_stderror("open dir fail");
304
305                 if (errno == EACCES || errno == EPERM)
306                         return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
307                 else
308                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
309         }
310
311         closedir(dp);
312
313         /* Check symbolic link directory */
314         real = realpath(path, NULL);
315         /* Get trimmed path */
316         _media_util_trim_path(path, &origin);
317
318 #ifdef _USE_TVPD_MODE
319         if (g_strcmp0(real, origin) != 0) {
320                 media_content_error("symbolic link(directory)");
321                 media_content_sec_debug("path[%s] real[%s]", origin, real);
322                 SAFE_FREE(real);
323                 SAFE_FREE(origin);
324                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
325         }
326 #else
327         if (g_str_has_prefix(real, MEDIA_SHARE_PATH)) {
328                 /* If shared dirctory, it should be change path to TZ_USER_SHARE from realpath */
329                 snprintf(result_path, MAX_PATH_LEN, "%s%s", tzplatform_getenv(TZ_USER_MEDIASHARED), real + strlen(MEDIA_SHARE_PATH));
330                 if (g_strcmp0(result_path, origin) != 0) {
331                         media_content_error("symbolic link(directory)");
332                         media_content_sec_debug("path[%s] real[%s]", origin, real);
333                         SAFE_FREE(real);
334                         SAFE_FREE(origin);
335                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
336                 }
337         } else {
338                 if (g_strcmp0(real, origin) != 0) {
339                         media_content_error("symbolic link(directory)");
340                         media_content_sec_debug("path[%s] real[%s]", origin, real);
341                         SAFE_FREE(real);
342                         SAFE_FREE(origin);
343                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
344                 }
345         }
346 #endif
347
348         SAFE_FREE(real);
349         SAFE_FREE(origin);
350
351         return MEDIA_CONTENT_ERROR_NONE;
352 }
353
354
355 int _media_content_replace_path_in_condition(const char *condition, char *replace_condition, bool replace)
356 {
357         int ret = MEDIA_CONTENT_ERROR_NONE;
358
359 #ifdef _USE_TVPD_MODE
360         snprintf(replace_condition, MAX_QUERY_SIZE, "%s", condition);
361 #else
362         char old_condition[MAX_QUERY_SIZE] = {0, };
363         char new_condition[MAX_QUERY_SIZE] = {0, };
364         char *find = NULL;
365         unsigned int str_len = 0;
366
367         char *find_str = NULL;
368         char *to_replace_str = NULL;
369
370         if (replace == TRUE) {  //change User session path to System session path
371                 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &find_str);
372                 if (ret != STORAGE_ERROR_NONE) {
373                         media_content_error("storage_get_directory failed");
374                         ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
375                         goto ERROR;
376                 }
377
378                 to_replace_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL);
379                 if (!STRING_VALID(to_replace_str)) {
380                         media_content_error("Get TZ_USER_CONTENT failed");
381                         ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
382                         goto ERROR;
383                 }
384         } else {
385                 find_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL);
386                 if (!STRING_VALID(find_str)) {
387                         media_content_error("Get TZ_USER_CONTENT failed");
388                         ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
389                         goto ERROR;
390                 }
391
392                 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &to_replace_str);
393                 if (ret != STORAGE_ERROR_NONE) {
394                         media_content_error("storage_get_directory failed");
395                         ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
396                         goto ERROR;
397                 }
398         }
399
400         memset(old_condition, 0, sizeof(old_condition));
401         memset(new_condition, 0, sizeof(new_condition));
402
403         media_content_sec_debug("Old condition[%s]", condition);
404
405         if (!SAFE_STRLCPY(new_condition, condition, sizeof(new_condition))) {
406                 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
407                 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
408                 goto ERROR;
409         }
410
411         if (g_strcmp0(find_str, to_replace_str))
412                 find = strstr(new_condition, find_str);
413
414         while (find != NULL) {
415                 str_len = find - new_condition;
416
417                 memset(old_condition, 0, sizeof(old_condition));
418                 if (!SAFE_STRLCPY(old_condition, new_condition, sizeof(old_condition))) {
419                         media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
420                         ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
421                         goto ERROR;
422                 }
423                 memset(new_condition, 0, sizeof(new_condition));
424
425                 snprintf(new_condition, str_len + 1, "%s", old_condition);
426
427                 SAFE_STRLCAT(new_condition, to_replace_str, sizeof(new_condition));
428                 SAFE_STRLCAT(new_condition, old_condition + str_len + strlen(find_str), sizeof(new_condition));
429
430                 find = strstr(new_condition, find_str);
431         }
432
433         if (!SAFE_STRLCPY(replace_condition, new_condition, MAX_QUERY_SIZE)) {
434                 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
435                 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
436                 goto ERROR;
437         }
438
439         media_content_sec_debug("repl cond[%s]", replace_condition);
440
441         if (!STRING_VALID(replace_condition)) {
442                 media_content_error("replace failed");
443                 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
444                 goto ERROR;
445         }
446
447 ERROR:
448         SAFE_FREE(find_str);
449         SAFE_FREE(to_replace_str);
450 #endif
451
452         return ret;
453 }
454
455 int _media_content_replace_path(const char *path, char *replace_path)
456 {
457 #ifdef _USE_TVPD_MODE
458         snprintf(replace_path, MAX_PATH_LEN, "%s", path);
459 #else
460
461         int ret = MEDIA_CONTENT_ERROR_NONE;
462         char *old_internal_path =  NULL;
463
464         ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &old_internal_path);
465         if (ret != STORAGE_ERROR_NONE) {
466                 media_content_error("storage_get_directory failed");
467                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
468         }
469
470         if (strncmp(path, old_internal_path, strlen(old_internal_path)) == 0) {
471                 media_content_sec_debug("Old path[%s]", path);
472                 snprintf(replace_path, MAX_PATH_LEN, "%s%s", MEDIA_ROOT_PATH_INTERNAL, path + strlen(old_internal_path));
473         } else {
474                 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
475         }
476
477         SAFE_FREE(old_internal_path);
478 #endif
479
480         if (!STRING_VALID(replace_path)) {
481                 media_content_error("replace failed");
482                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
483         }
484
485         return MEDIA_CONTENT_ERROR_NONE;
486 }
487
488 int _media_content_rollback_path(const char *path, char *replace_path)
489 {
490 #ifdef _USE_TVPD_MODE
491                 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
492 #else
493
494         int ret = MEDIA_CONTENT_ERROR_NONE;
495         char *old_internal_path =  NULL;
496
497         ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &old_internal_path);
498         if (ret != STORAGE_ERROR_NONE) {
499                 media_content_error("storage_get_directory failed");
500                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
501         }
502
503         if (strncmp(path, MEDIA_ROOT_PATH_INTERNAL, strlen(MEDIA_ROOT_PATH_INTERNAL)) == 0) {
504                 media_content_sec_debug("new path[%s]", path);
505                 snprintf(replace_path, MAX_PATH_LEN, "%s%s", old_internal_path, path + strlen(MEDIA_ROOT_PATH_INTERNAL));
506         } else {
507                 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
508         }
509
510         SAFE_FREE(old_internal_path);
511 #endif
512
513         if (!STRING_VALID(replace_path)) {
514                 media_content_error("replace failed");
515                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
516         }
517
518         return MEDIA_CONTENT_ERROR_NONE;
519 }
520
521 #ifdef _USE_SENIOR_MODE
522 bool _media_content_is_support_senior_mode()
523 {
524         bool bSupportSeniorMode = false;
525
526         if (system_info_get_value_bool(SYSTEM_INFO_KEY_GET_SENIOR_MODE_SUPPORTED, &bSupportSeniorMode) != SYSTEM_INFO_ERROR_NONE) {
527                 media_content_debug("Get senior mode support failed");
528                 return false;
529         }
530         /* media_content_debug("Senior mode Support : [%d]", bSupportSeniorMode); */
531         return bSupportSeniorMode;
532 }
533 #endif
534