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