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