Improve _media_util_check_ignore_dir function
[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 failed");
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 static bool __is_scan_ignore_exist(const char *path)
205 {
206         const char *scan_ignore = ".scan_ignore";
207         char *ignore_path = NULL;
208         gboolean result = FALSE;
209
210         if (!STRING_VALID(path))
211                 return false;
212
213         ignore_path = g_build_path(G_DIR_SEPARATOR_S, path, scan_ignore, NULL);
214         result = g_file_test(ignore_path, G_FILE_TEST_EXISTS);
215
216         if (result)
217                 media_content_error("scan ignore file exist [%s]", ignore_path);
218
219         SAFE_FREE(ignore_path);
220
221         return (bool)result;
222 }
223
224 int _media_util_check_ignore_dir(const char *dir_path, bool *ignore)
225 {
226         int ret = MEDIA_CONTENT_ERROR_NONE;
227         ms_user_storage_type_e storage_type = MS_USER_STORAGE_INTERNAL;
228
229         media_content_retvm_if(!STRING_VALID(dir_path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid dir_path");
230         media_content_sec_debug("dir_path : %s", dir_path);
231
232         *ignore = false;
233         /*1. Check Hidden Directory*/
234         if (strstr(dir_path, "/.") != NULL) {
235                 *ignore = true;
236                 media_content_error("hidden path");
237                 return MEDIA_CONTENT_ERROR_NONE;
238         }
239
240         /*2. Check Scan Ignore Directory*/
241         ret = ms_user_get_storage_type(_content_get_uid(), dir_path, &storage_type);
242         if (ret != MS_MEDIA_ERR_NONE) {
243                 media_content_error("ms_user_get_storage_type failed : %d", ret);
244                 return _content_error_capi(ret);
245         }
246
247         char *leaf_path = NULL;
248         char search_path[MAX_PATH_LEN] = {0, };
249
250         memset(search_path, 0, sizeof(search_path));
251         if (!SAFE_STRLCPY(search_path, dir_path, sizeof(search_path))) {
252                 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
253                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
254         }
255
256         while (STRING_VALID(search_path)) {
257                 if ((*ignore = __is_scan_ignore_exist(search_path)))
258                         break;
259
260                 leaf_path = strrchr(search_path, '/');
261                 if (!leaf_path)
262                         break;
263
264                 search_path[leaf_path - search_path] = '\0';
265         }
266
267         return MEDIA_CONTENT_ERROR_NONE;
268 }
269
270 int _media_content_check_dir(const char *path)
271 {
272         DIR *dp = NULL;
273         char *real = NULL;
274         char *origin = NULL;
275 #ifndef _USE_TVPD_MODE
276         char result_path[MAX_PATH_LEN] = {0, };
277 #endif
278         dp = opendir(path);
279         if (dp == NULL) {
280                 if (errno == EACCES || errno == EPERM) {
281                         media_content_stderror("open dir fail");
282                         media_content_sec_error("path [%s]", path);
283                         return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
284                 } else {
285                         media_content_stderror("open dir fail");
286                         media_content_sec_error("path [%s]", path);
287                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
288                 }
289         }
290
291         closedir(dp);
292
293         /* Check symbolic link directory */
294         real = realpath(path, NULL);
295         /* Get trimmed path */
296         _media_util_trim_path(path, &origin);
297
298 #ifdef _USE_TVPD_MODE
299         if (g_strcmp0(real, origin) != 0) {
300                 media_content_error("symbolic link(directory)");
301                 media_content_sec_debug("path[%s] real[%s]", origin, real);
302                 SAFE_FREE(real);
303                 SAFE_FREE(origin);
304                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
305         }
306 #else
307         if (g_str_has_prefix(real, MEDIA_SHARE_PATH)) {
308                 /* If shared dirctory, it should be change path to TZ_USER_SHARE from realpath */
309                 snprintf(result_path, MAX_PATH_LEN, "%s%s", tzplatform_getenv(TZ_USER_MEDIASHARED), real + strlen(MEDIA_SHARE_PATH));
310                 if (g_strcmp0(result_path, origin) != 0) {
311                         media_content_error("symbolic link(directory)");
312                         media_content_sec_debug("path[%s] real[%s]", origin, real);
313                         SAFE_FREE(real);
314                         SAFE_FREE(origin);
315                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
316                 }
317         } else {
318                 if (g_strcmp0(real, origin) != 0) {
319                         media_content_error("symbolic link(directory)");
320                         media_content_sec_debug("path[%s] real[%s]", origin, real);
321                         SAFE_FREE(real);
322                         SAFE_FREE(origin);
323                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
324                 }
325         }
326 #endif
327
328         SAFE_FREE(real);
329         SAFE_FREE(origin);
330
331         return MEDIA_CONTENT_ERROR_NONE;
332 }
333
334
335 int _media_content_replace_path_in_condition(const char *condition, char *replace_condition, bool replace)
336 {
337         int ret = MEDIA_CONTENT_ERROR_NONE;
338
339 #ifdef _USE_TVPD_MODE
340         snprintf(replace_condition, MAX_QUERY_SIZE, "%s", condition);
341 #else
342         char old_condition[MAX_QUERY_SIZE] = {0, };
343         char new_condition[MAX_QUERY_SIZE] = {0, };
344         char *find = NULL;
345         unsigned int str_len = 0;
346
347         char *find_str = NULL;
348         char *to_replace_str = NULL;
349
350         if (replace == TRUE) {  //change User session path to System session path
351                 find_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL_OLD);
352                 if (!STRING_VALID(find_str)) {
353                         media_content_error("strdup failed");
354                         ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
355                         goto ERROR;
356                 }
357
358                 to_replace_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL);
359                 if (!STRING_VALID(to_replace_str)) {
360                         media_content_error("Get TZ_USER_CONTENT failed");
361                         ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
362                         goto ERROR;
363                 }
364         } else {
365                 find_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL);
366                 if (!STRING_VALID(find_str)) {
367                         media_content_error("Get TZ_USER_CONTENT failed");
368                         ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
369                         goto ERROR;
370                 }
371
372                 to_replace_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL_OLD);
373                 if (!STRING_VALID(to_replace_str)) {
374                         media_content_error("strdup failed");
375                         ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
376                         goto ERROR;
377                 }
378         }
379
380         memset(old_condition, 0, sizeof(old_condition));
381         memset(new_condition, 0, sizeof(new_condition));
382
383         media_content_sec_debug("Old condition[%s]", condition);
384
385         if (!SAFE_STRLCPY(new_condition, condition, sizeof(new_condition))) {
386                 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
387                 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
388                 goto ERROR;
389         }
390
391         if (g_strcmp0(find_str, to_replace_str))
392                 find = strstr(new_condition, find_str);
393
394         while (find != NULL) {
395                 str_len = find - new_condition;
396
397                 memset(old_condition, 0, sizeof(old_condition));
398                 if (!SAFE_STRLCPY(old_condition, new_condition, sizeof(old_condition))) {
399                         media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
400                         ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
401                         goto ERROR;
402                 }
403                 memset(new_condition, 0, sizeof(new_condition));
404
405                 snprintf(new_condition, str_len + 1, "%s", old_condition);
406
407                 SAFE_STRLCAT(new_condition, to_replace_str, sizeof(new_condition));
408                 SAFE_STRLCAT(new_condition, old_condition + str_len + strlen(find_str), sizeof(new_condition));
409
410                 find = strstr(new_condition, find_str);
411         }
412
413         if (!SAFE_STRLCPY(replace_condition, new_condition, MAX_QUERY_SIZE)) {
414                 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
415                 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
416                 goto ERROR;
417         }
418
419         media_content_sec_debug("repl cond[%s]", replace_condition);
420
421         if (!STRING_VALID(replace_condition)) {
422                 media_content_error("replace failed");
423                 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
424                 goto ERROR;
425         }
426
427 ERROR:
428         SAFE_FREE(find_str);
429         SAFE_FREE(to_replace_str);
430 #endif
431
432         return ret;
433 }
434
435 int _media_content_replace_path(const char *path, char *replace_path)
436 {
437 #ifdef _USE_TVPD_MODE
438         snprintf(replace_path, MAX_PATH_LEN, "%s", path);
439 #else
440         if (strncmp(path, MEDIA_ROOT_PATH_INTERNAL_OLD, strlen(MEDIA_ROOT_PATH_INTERNAL_OLD)) == 0) {
441                 media_content_sec_debug("Old path[%s]", path);
442                 snprintf(replace_path, MAX_PATH_LEN, "%s%s", MEDIA_ROOT_PATH_INTERNAL, path + strlen(MEDIA_ROOT_PATH_INTERNAL_OLD));
443         } else {
444                 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
445         }
446 #endif
447
448         if (!STRING_VALID(replace_path)) {
449                 media_content_error("replace failed");
450                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
451         }
452
453         return MEDIA_CONTENT_ERROR_NONE;
454 }
455
456 #ifdef _USE_SENIOR_MODE
457 bool _media_content_is_support_senior_mode()
458 {
459         bool bSupportSeniorMode = false;
460
461         if (system_info_get_value_bool(SYSTEM_INFO_KEY_GET_SENIOR_MODE_SUPPORTED, &bSupportSeniorMode) != SYSTEM_INFO_ERROR_NONE) {
462                 media_content_debug("Get senior mode support failed");
463                 return false;
464         }
465         /* media_content_debug("Senior mode Support : [%d]", bSupportSeniorMode); */
466         return bSupportSeniorMode;
467 }
468 #endif
469