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