Improve path trim 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         gchar **name_list = NULL;
87         gchar *tmp_path = NULL;
88
89         if (!STRING_VALID(input_path) || output_path == NULL)
90                 return;
91
92         /* Workflow example
93                 Input : /a/b//c/
94                 After g_strsplit() : {'','a','b','','c',''}
95                 After g_build_pathv() : a/b/c
96                 After g_strdup_printf() : /a/b/c
97         */
98         name_list = g_strsplit(input_path, "/", -1);
99         if (!name_list)
100                 return;
101
102         tmp_path = g_build_pathv(G_DIR_SEPARATOR_S, name_list);
103         g_strfreev(name_list);
104         if (!tmp_path)
105                 return;
106
107         /* g_build_pathv does not add root '/' */
108         *output_path = g_strdup_printf("/%s", tmp_path);
109         g_free(tmp_path);
110 }
111
112
113 int _media_util_get_file_time(const char *path)
114 {
115         struct stat statbuf;
116         int ret = 0;
117
118         memset(&statbuf, 0, sizeof(struct stat));
119         ret = stat(path, &statbuf);
120         if (ret == -1) {
121                 media_content_stderror("stat failed");
122                 return ret;
123         }
124
125         return statbuf.st_mtime;
126 }
127
128 int _media_util_check_ignore_file(const char *path, bool *ignore)
129 {
130         media_content_retvm_if(!STRING_VALID(path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid path");
131
132         *ignore = FALSE;
133         char *tmp_path = NULL;
134         char *org_path = NULL;
135
136 #ifndef _USE_TVPD_MODE
137         char replace[MAX_PATH_LEN] = {0, };
138 #endif
139
140         /* Check is exist (It may be the path to the deleted file) */
141         if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
142                 media_content_sec_debug("removed path[%s]", path);
143                 return MEDIA_CONTENT_ERROR_NONE;
144         }
145
146         /* Check symbolic link file */
147         if (g_file_test(path, G_FILE_TEST_IS_SYMLINK)) {
148                 *ignore = TRUE;
149                 media_content_error("symbolic link(file)");
150                 media_content_sec_debug("path : %s", path);
151                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
152         }
153
154         /* Check hidden path */
155         if (strstr(path, "/.") != NULL) {
156                 *ignore = TRUE;
157                 media_content_error("hidden path");
158                 media_content_sec_debug("path : %s", path);
159                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
160         }
161
162         /* Check symbolic directory */
163         tmp_path = realpath(path, NULL);
164         /* Get trimmed path */
165         _media_util_trim_path(path, &org_path);
166
167 #ifdef _USE_TVPD_MODE
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 #else
177         if (g_str_has_prefix(tmp_path, MEDIA_SHARE_PATH)) {
178                 /* If shared dirctory, it should be change path to TZ_USER_SHARE from realpath */
179                 snprintf(replace, MAX_PATH_LEN, "%s%s", tzplatform_getenv(TZ_USER_MEDIASHARED), tmp_path + strlen(MEDIA_SHARE_PATH));
180                 if (g_strcmp0(replace, org_path) != 0) {
181                         *ignore = TRUE;
182                         media_content_error("symbolic link(directory)");
183                         media_content_sec_debug("path[%s] real[%s]", org_path, tmp_path);
184                         SAFE_FREE(tmp_path);
185                         SAFE_FREE(org_path);
186                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
187                 }
188         } else {
189                 if (g_strcmp0(tmp_path, org_path) != 0) {
190                         *ignore = TRUE;
191                         media_content_error("symbolic link(directory)");
192                         media_content_sec_debug("path[%s] real[%s]", org_path, tmp_path);
193                         SAFE_FREE(tmp_path);
194                         SAFE_FREE(org_path);
195                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
196                 }
197         }
198 #endif
199         SAFE_FREE(tmp_path);
200         SAFE_FREE(org_path);
201
202         return MEDIA_CONTENT_ERROR_NONE;
203 }
204
205 static bool __is_scan_ignore_exist(const char *path)
206 {
207         const char *scan_ignore = ".scan_ignore";
208         char *ignore_path = NULL;
209         gboolean result = FALSE;
210
211         if (!STRING_VALID(path))
212                 return false;
213
214         ignore_path = g_build_path(G_DIR_SEPARATOR_S, path, scan_ignore, NULL);
215         result = g_file_test(ignore_path, G_FILE_TEST_EXISTS);
216
217         if (result)
218                 media_content_error("scan ignore file exist [%s]", ignore_path);
219
220         SAFE_FREE(ignore_path);
221
222         return (bool)result;
223 }
224
225 int _media_util_check_ignore_dir(const char *dir_path, bool *ignore)
226 {
227         int ret = MEDIA_CONTENT_ERROR_NONE;
228         ms_user_storage_type_e storage_type = MS_USER_STORAGE_INTERNAL;
229
230         media_content_retvm_if(!STRING_VALID(dir_path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid dir_path");
231         media_content_sec_debug("dir_path : %s", dir_path);
232
233         *ignore = false;
234         /*1. Check Hidden Directory*/
235         if (strstr(dir_path, "/.") != NULL) {
236                 *ignore = true;
237                 media_content_error("hidden path");
238                 return MEDIA_CONTENT_ERROR_NONE;
239         }
240
241         /*2. Check Scan Ignore Directory*/
242         ret = ms_user_get_storage_type(_content_get_uid(), dir_path, &storage_type);
243         if (ret != MS_MEDIA_ERR_NONE) {
244                 media_content_error("ms_user_get_storage_type failed : %d", ret);
245                 return _content_error_capi(ret);
246         }
247
248         char *leaf_path = NULL;
249         char search_path[MAX_PATH_LEN] = {0, };
250
251         memset(search_path, 0, sizeof(search_path));
252         if (!SAFE_STRLCPY(search_path, dir_path, sizeof(search_path))) {
253                 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
254                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
255         }
256
257         while (STRING_VALID(search_path)) {
258                 if ((*ignore = __is_scan_ignore_exist(search_path)))
259                         break;
260
261                 leaf_path = strrchr(search_path, '/');
262                 if (!leaf_path)
263                         break;
264
265                 search_path[leaf_path - search_path] = '\0';
266         }
267
268         return MEDIA_CONTENT_ERROR_NONE;
269 }
270
271 int _media_content_check_dir(const char *path)
272 {
273         DIR *dp = NULL;
274         char *real = NULL;
275         char *origin = NULL;
276 #ifndef _USE_TVPD_MODE
277         char result_path[MAX_PATH_LEN] = {0, };
278 #endif
279         dp = opendir(path);
280         if (dp == NULL) {
281                 if (errno == EACCES || errno == EPERM) {
282                         media_content_stderror("open dir fail");
283                         media_content_sec_error("path [%s]", path);
284                         return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
285                 } else {
286                         media_content_stderror("open dir fail");
287                         media_content_sec_error("path [%s]", path);
288                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
289                 }
290         }
291
292         closedir(dp);
293
294         /* Check symbolic link directory */
295         real = realpath(path, NULL);
296         /* Get trimmed path */
297         _media_util_trim_path(path, &origin);
298
299 #ifdef _USE_TVPD_MODE
300         if (g_strcmp0(real, origin) != 0) {
301                 media_content_error("symbolic link(directory)");
302                 media_content_sec_debug("path[%s] real[%s]", origin, real);
303                 SAFE_FREE(real);
304                 SAFE_FREE(origin);
305                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
306         }
307 #else
308         if (g_str_has_prefix(real, MEDIA_SHARE_PATH)) {
309                 /* If shared dirctory, it should be change path to TZ_USER_SHARE from realpath */
310                 snprintf(result_path, MAX_PATH_LEN, "%s%s", tzplatform_getenv(TZ_USER_MEDIASHARED), real + strlen(MEDIA_SHARE_PATH));
311                 if (g_strcmp0(result_path, origin) != 0) {
312                         media_content_error("symbolic link(directory)");
313                         media_content_sec_debug("path[%s] real[%s]", origin, real);
314                         SAFE_FREE(real);
315                         SAFE_FREE(origin);
316                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
317                 }
318         } else {
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         }
327 #endif
328
329         SAFE_FREE(real);
330         SAFE_FREE(origin);
331
332         return MEDIA_CONTENT_ERROR_NONE;
333 }
334
335
336 int _media_content_replace_path_in_condition(const char *condition, char *replace_condition, bool replace)
337 {
338         int ret = MEDIA_CONTENT_ERROR_NONE;
339
340 #ifdef _USE_TVPD_MODE
341         snprintf(replace_condition, MAX_QUERY_SIZE, "%s", condition);
342 #else
343         char old_condition[MAX_QUERY_SIZE] = {0, };
344         char new_condition[MAX_QUERY_SIZE] = {0, };
345         char *find = NULL;
346         unsigned int str_len = 0;
347
348         char *find_str = NULL;
349         char *to_replace_str = NULL;
350
351         if (replace == TRUE) {  //change User session path to System session path
352                 find_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL_OLD);
353                 if (!STRING_VALID(find_str)) {
354                         media_content_error("strdup failed");
355                         ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
356                         goto ERROR;
357                 }
358
359                 to_replace_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL);
360                 if (!STRING_VALID(to_replace_str)) {
361                         media_content_error("Get TZ_USER_CONTENT failed");
362                         ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
363                         goto ERROR;
364                 }
365         } else {
366                 find_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL);
367                 if (!STRING_VALID(find_str)) {
368                         media_content_error("Get TZ_USER_CONTENT failed");
369                         ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
370                         goto ERROR;
371                 }
372
373                 to_replace_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL_OLD);
374                 if (!STRING_VALID(to_replace_str)) {
375                         media_content_error("strdup failed");
376                         ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
377                         goto ERROR;
378                 }
379         }
380
381         memset(old_condition, 0, sizeof(old_condition));
382         memset(new_condition, 0, sizeof(new_condition));
383
384         media_content_sec_debug("Old condition[%s]", condition);
385
386         if (!SAFE_STRLCPY(new_condition, condition, sizeof(new_condition))) {
387                 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
388                 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
389                 goto ERROR;
390         }
391
392         if (g_strcmp0(find_str, to_replace_str))
393                 find = strstr(new_condition, find_str);
394
395         while (find != NULL) {
396                 str_len = find - new_condition;
397
398                 memset(old_condition, 0, sizeof(old_condition));
399                 if (!SAFE_STRLCPY(old_condition, new_condition, sizeof(old_condition))) {
400                         media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
401                         ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
402                         goto ERROR;
403                 }
404                 memset(new_condition, 0, sizeof(new_condition));
405
406                 snprintf(new_condition, str_len + 1, "%s", old_condition);
407
408                 SAFE_STRLCAT(new_condition, to_replace_str, sizeof(new_condition));
409                 SAFE_STRLCAT(new_condition, old_condition + str_len + strlen(find_str), sizeof(new_condition));
410
411                 find = strstr(new_condition, find_str);
412         }
413
414         if (!SAFE_STRLCPY(replace_condition, new_condition, MAX_QUERY_SIZE)) {
415                 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
416                 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
417                 goto ERROR;
418         }
419
420         media_content_sec_debug("repl cond[%s]", replace_condition);
421
422         if (!STRING_VALID(replace_condition)) {
423                 media_content_error("replace failed");
424                 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
425                 goto ERROR;
426         }
427
428 ERROR:
429         SAFE_FREE(find_str);
430         SAFE_FREE(to_replace_str);
431 #endif
432
433         return ret;
434 }
435
436 int _media_content_replace_path(const char *path, char *replace_path)
437 {
438 #ifdef _USE_TVPD_MODE
439         snprintf(replace_path, MAX_PATH_LEN, "%s", path);
440 #else
441         if (strncmp(path, MEDIA_ROOT_PATH_INTERNAL_OLD, strlen(MEDIA_ROOT_PATH_INTERNAL_OLD)) == 0) {
442                 media_content_sec_debug("Old path[%s]", path);
443                 snprintf(replace_path, MAX_PATH_LEN, "%s%s", MEDIA_ROOT_PATH_INTERNAL, path + strlen(MEDIA_ROOT_PATH_INTERNAL_OLD));
444         } else {
445                 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
446         }
447 #endif
448
449         if (!STRING_VALID(replace_path)) {
450                 media_content_error("replace failed");
451                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
452         }
453
454         return MEDIA_CONTENT_ERROR_NONE;
455 }
456
457 #ifdef _USE_SENIOR_MODE
458 bool _media_content_is_support_senior_mode()
459 {
460         bool bSupportSeniorMode = false;
461
462         if (system_info_get_value_bool(SYSTEM_INFO_KEY_GET_SENIOR_MODE_SUPPORTED, &bSupportSeniorMode) != SYSTEM_INFO_ERROR_NONE) {
463                 media_content_debug("Get senior mode support failed");
464                 return false;
465         }
466         /* media_content_debug("Senior mode Support : [%d]", bSupportSeniorMode); */
467         return bSupportSeniorMode;
468 }
469 #endif
470