[MMFWN-734]Add internal API for system service
[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 #ifdef _USE_SENIOR_MODE
23 #include <system_info.h>
24 #include <media_util_private.h>
25 #endif
26
27 int _media_util_check_file_exist(const char *path)
28 {
29         int exist;
30
31         /* check the file exits actually */
32         exist = open(path, O_RDONLY);
33         if (exist < 0) {
34                 media_content_sec_debug("path [%s]", path);
35                 media_content_stderror("open file fail");
36                 if (errno == EACCES || errno == EPERM)
37                         return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
38                 else
39                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
40         }
41
42         close(exist);
43
44         return MEDIA_CONTENT_ERROR_NONE;
45 }
46
47 int _media_util_check_ignore_file(const char *path, bool *ignore)
48 {
49         media_content_retvm_if(!STRING_VALID(path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid path");
50
51         *ignore = FALSE;
52
53         if (strstr(path, "/.") != NULL) {
54                 *ignore = TRUE;
55                 media_content_error("hidden path");
56                 media_content_sec_debug("path : %s", path);
57         }
58
59         return MEDIA_CONTENT_ERROR_NONE;
60 }
61
62 int _media_util_check_ignore_dir(const char *dir_path, bool *ignore)
63 {
64         int ret = MEDIA_CONTENT_ERROR_NONE;
65         media_svc_storage_type_e storage_type = 0;
66         const char *scan_ignore = ".scan_ignore";
67         bool find = false;
68
69         media_content_sec_debug("dir_path : %s", dir_path);
70
71         media_content_retvm_if(!STRING_VALID(dir_path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid dir_path");
72
73         *ignore = FALSE;
74         /*1. Check Hidden Directory*/
75         if (strstr(dir_path, "/.") != NULL) {
76                 *ignore = TRUE;
77                 media_content_error("hidden path");
78                 return MEDIA_CONTENT_ERROR_NONE;
79         }
80
81         /*2. Check Scan Ignore Directory*/
82         ret = media_svc_get_storage_type(dir_path, &storage_type, _content_get_uid());
83         if (ret != MS_MEDIA_ERR_NONE) {
84                 media_content_error("media_svc_get_storage_type failed : %d", ret);
85                 return _content_error_capi(MEDIA_CONTENT_TYPE, ret);
86         }
87
88         DIR *dp = NULL;
89         struct dirent entry;
90         struct dirent *result = NULL;
91
92         char *leaf_path = NULL;
93         char search_path[MAX_PATH_LEN] = {0, };
94
95         memset(search_path, 0, sizeof(search_path));
96         if (!SAFE_STRLCPY(search_path, dir_path, sizeof(search_path))) {
97                 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
98                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
99         }
100
101         while (STRING_VALID(search_path)) {
102                 dp = opendir(search_path);
103                 if (dp == NULL) {
104                         *ignore = TRUE;
105                         media_content_error("Open Directory fail");
106                         media_content_sec_debug("Open fail path[%s]", search_path);
107                         if (errno == EACCES || errno == EPERM)
108                                 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
109                         else
110                                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
111                 }
112
113                 media_content_retvm_if(dp == NULL, MEDIA_CONTENT_ERROR_INVALID_OPERATION, "Open Directory fail");
114
115                 while (!readdir_r(dp, &entry, &result)) {
116                         if (result == NULL)
117                                 break;
118
119                         if (STRING_VALID(entry.d_name) && (strcmp(entry.d_name, scan_ignore) == 0)) {
120                                 media_content_error("Find Ignore path");
121                                 media_content_sec_debug("Ignore path[%s]", search_path);
122                                 find = TRUE;
123                                 break;
124                         } else {
125                                 /*media_content_sec_debug("entry.d_name[%s]", entry.d_name);*/
126                                 continue;
127                         }
128                 }
129
130                 if (dp) closedir(dp);
131                 dp = NULL;
132
133                 if (find) {
134                         *ignore = TRUE;
135                         break;
136                 } else {
137                         /*If root path, Stop Scanning*/
138                         if ((storage_type == MEDIA_SVC_STORAGE_INTERNAL) && (STRING_VALID(MEDIA_ROOT_PATH_INTERNAL) && strcmp(search_path, MEDIA_ROOT_PATH_INTERNAL) == 0)) {
139                                 break;
140                         } else if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL) && (STRING_VALID(MEDIA_ROOT_PATH_SDCARD)) && (strcmp(search_path, MEDIA_ROOT_PATH_SDCARD) == 0)) {
141                                 break;
142                         } else if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) && (STRING_VALID(MEDIA_ROOT_PATH_DISC)) && (strcmp(search_path, MEDIA_ROOT_PATH_DISC) == 0)) {
143                                 break;
144                         } else if (storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) {
145                                 char *parent_folder_path = NULL;
146                                 bool is_root = FALSE;
147
148                                 parent_folder_path = g_path_get_dirname(search_path);
149                                 if (STRING_VALID(MEDIA_ROOT_PATH_USB) && STRING_VALID(parent_folder_path) && (strcmp(parent_folder_path, MEDIA_ROOT_PATH_USB) == 0))
150                                         is_root = TRUE;
151
152                                 SAFE_FREE(parent_folder_path);
153
154                                 if (is_root == TRUE)
155                                         break;
156                         }
157 #ifdef _USE_SENIOR_MODE
158                         if (_media_content_is_support_senior_mode()) {
159                                 if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL) && (strcmp(search_path, MEDIA_ROOT_PATH_SENIOR_MODE) == 0))
160                                         break;
161                         }
162 #endif
163
164                         leaf_path = strrchr(search_path, '/');
165                         if (leaf_path != NULL) {
166                                 int seek_len = leaf_path -search_path;
167                                 search_path[seek_len] = '\0';
168                                 /*media_content_sec_debug("go to other dir [%s]", search_path);*/
169                         } else {
170                                 media_content_debug("Fail to find leaf path");
171                                 break;
172                         }
173                 }
174         }
175
176         return MEDIA_CONTENT_ERROR_NONE;
177 }
178
179 int _media_content_replace_path_in_condition(const char *condition, char *replace_condition, bool replace)
180 {
181         int ret = MEDIA_CONTENT_ERROR_NONE;
182
183 #ifdef _USE_TV_PROFILE
184         snprintf(replace_condition, MAX_QUERY_SIZE, "%s", condition);
185 #else
186         char old_condition[MAX_QUERY_SIZE] = {0, };
187         char new_condition[MAX_QUERY_SIZE] = {0, };
188         char *find = NULL;
189         unsigned int str_len = 0;
190
191         char *find_str = NULL;
192         char *to_replace_str = NULL;
193
194         if (replace == TRUE) {  //change User session path to System session path
195                 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &find_str);
196                 if (ret != STORAGE_ERROR_NONE) {
197                         media_content_error("storage_get_directory failed");
198                         ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
199                         goto ERROR;
200                 }
201
202                 to_replace_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL);
203                 if (!STRING_VALID(to_replace_str)) {
204                         media_content_error("Get TZ_USER_CONTENT failed");
205                         ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
206                         goto ERROR;
207                 }
208         } else {
209                 find_str = g_strdup(MEDIA_ROOT_PATH_INTERNAL);
210                 if (!STRING_VALID(find_str)) {
211                         media_content_error("Get TZ_USER_CONTENT failed");
212                         ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
213                         goto ERROR;
214                 }
215
216                 ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &to_replace_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
224         memset(old_condition, 0, sizeof(old_condition));
225         memset(new_condition, 0, sizeof(new_condition));
226
227         media_content_sec_debug("Old condition[%s]", condition);
228
229         if (!SAFE_STRLCPY(new_condition, condition, sizeof(new_condition))) {
230                 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
231                 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
232                 goto ERROR;
233         }
234
235         if (strcmp(find_str, to_replace_str))
236                 find = strstr(new_condition, find_str);
237
238         while (find != NULL) {
239                 str_len = find - new_condition;
240
241                 memset(old_condition, 0, sizeof(old_condition));
242                 if (!SAFE_STRLCPY(old_condition, new_condition, sizeof(old_condition))) {
243                         media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
244                         ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
245                         goto ERROR;
246                 }
247                 memset(new_condition, 0, sizeof(new_condition));
248
249                 snprintf(new_condition, str_len + 1, "%s", old_condition);
250
251                 SAFE_STRLCAT(new_condition, to_replace_str, sizeof(new_condition));
252                 SAFE_STRLCAT(new_condition, old_condition + str_len + strlen(find_str), sizeof(new_condition));
253
254                 find = strstr(new_condition, find_str);
255         }
256
257         if (!SAFE_STRLCPY(replace_condition, new_condition, MAX_QUERY_SIZE)) {
258                 media_content_error("MEDIA_CONTENT_ERROR_INVALID_OPERATION(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_OPERATION);
259                 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
260                 goto ERROR;
261         }
262
263         media_content_sec_debug("repl cond[%s]", replace_condition);
264
265         if (!STRING_VALID(replace_condition)) {
266                 media_content_error("replace failed");
267                 ret = MEDIA_CONTENT_ERROR_INVALID_OPERATION;
268                 goto ERROR;
269         }
270
271 ERROR:
272         SAFE_FREE(find_str);
273         SAFE_FREE(to_replace_str);
274 #endif
275
276         return ret;
277 }
278
279 int _media_content_replace_path(const char *path, char *replace_path)
280 {
281 #ifdef _USE_TV_PROFILE
282         snprintf(replace_path, MAX_PATH_LEN, "%s", path);
283 #else
284
285         int ret = MEDIA_CONTENT_ERROR_NONE;
286         char *old_internal_path =  NULL;
287
288         ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &old_internal_path);
289         if (ret != STORAGE_ERROR_NONE) {
290                 media_content_error("storage_get_directory failed");
291                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
292         }
293
294         if (strncmp(path, old_internal_path, strlen(old_internal_path)) == 0) {
295                 media_content_sec_debug("Old path[%s]", path);
296                 snprintf(replace_path, MAX_PATH_LEN, "%s%s", MEDIA_ROOT_PATH_INTERNAL, path + strlen(old_internal_path));
297         } else {
298                 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
299         }
300
301         SAFE_FREE(old_internal_path);
302 #endif
303
304         if (!STRING_VALID(replace_path)) {
305                 media_content_error("replace failed");
306                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
307         }
308
309         return MEDIA_CONTENT_ERROR_NONE;
310 }
311
312 int _media_content_rollback_path(const char *path, char *replace_path)
313 {
314 #ifdef _USE_TV_PROFILE
315                 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
316 #else
317
318         int ret = MEDIA_CONTENT_ERROR_NONE;
319         char *old_internal_path =  NULL;
320
321         ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &old_internal_path);
322         if (ret != STORAGE_ERROR_NONE) {
323                 media_content_error("storage_get_directory failed");
324                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
325         }
326
327         if (strncmp(path, MEDIA_ROOT_PATH_INTERNAL, strlen(MEDIA_ROOT_PATH_INTERNAL)) == 0) {
328                 media_content_sec_debug("new path[%s]", path);
329                 snprintf(replace_path, MAX_PATH_LEN, "%s%s", old_internal_path, path + strlen(MEDIA_ROOT_PATH_INTERNAL));
330         } else {
331                 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
332         }
333
334         SAFE_FREE(old_internal_path);
335 #endif
336
337         if (!STRING_VALID(replace_path)) {
338                 media_content_error("replace failed");
339                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
340         }
341
342         return MEDIA_CONTENT_ERROR_NONE;
343 }
344
345 #ifdef _USE_SENIOR_MODE
346 bool _media_content_is_support_senior_mode()
347 {
348         bool bSupportSeniorMode = false;
349
350         if (system_info_get_value_bool(SYSTEM_INFO_KEY_GET_SENIOR_MODE_SUPPORTED, &bSupportSeniorMode) != SYSTEM_INFO_ERROR_NONE) {
351                 media_content_debug("Get senior mode support failed");
352                 return false;
353         }
354         /* media_content_debug("Senior mode Support : [%d]", bSupportSeniorMode); */
355         return bSupportSeniorMode;
356 }
357 #endif
358