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