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