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