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