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