Apply tizen coding rule
[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 #include <dirent.h>
18 #include <string.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 #include <fcntl.h>
22 #include <media_util_private.h>
23 #include <media_info_private.h>
24 #include <media_content_type.h>
25 #include <storage.h>
26 #ifdef _USE_TV_PROFILE
27 #include <system_info.h>
28 #endif
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[4096] = {0, };
97
98         strncpy(search_path, dir_path, strlen(dir_path));
99         while (STRING_VALID(search_path)) {
100                 dp = opendir(search_path);
101                 if (dp == NULL) {
102                         *ignore = TRUE;
103                         media_content_error("Open Directory fail");
104                         media_content_sec_debug("Open fail path[%s]", search_path);
105                         if (errno == EACCES || errno == EPERM)
106                                 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
107                         else
108                                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
109                 }
110
111                 media_content_retvm_if(dp == NULL, MEDIA_CONTENT_ERROR_INVALID_OPERATION, "Open Directory fail");
112
113                 while (!readdir_r(dp, &entry, &result)) {
114                         if (result == NULL)
115                                 break;
116
117                         if (STRING_VALID(entry.d_name) && (strcmp(entry.d_name, scan_ignore) == 0)) {
118                                 media_content_error("Find Ignore path");
119                                 media_content_sec_debug("Ignore path[%s]", search_path);
120                                 find = TRUE;
121                                 break;
122                         } else {
123                                 /*media_content_sec_debug("entry.d_name[%s]", entry.d_name);*/
124                                 continue;
125                         }
126                 }
127
128                 if (dp) closedir(dp);
129                 dp = NULL;
130
131                 if (find) {
132                         *ignore = TRUE;
133                         break;
134                 } else {
135                         /*If root path, Stop Scanning*/
136                         if ((storage_type == MEDIA_SVC_STORAGE_INTERNAL) && (STRING_VALID(MEDIA_ROOT_PATH_INTERNAL) && strcmp(search_path, MEDIA_ROOT_PATH_INTERNAL) == 0)) {
137                                 break;
138                         } else if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL) && (STRING_VALID(MEDIA_ROOT_PATH_SDCARD)) && (strcmp(search_path, MEDIA_ROOT_PATH_SDCARD) == 0)) {
139                                 break;
140                         } else if (storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) {
141                                 char *parent_folder_path = NULL;
142                                 bool is_root = FALSE;
143
144                                 parent_folder_path = g_path_get_dirname(search_path);
145                                 if (STRING_VALID(MEDIA_ROOT_PATH_USB) && (strcmp(parent_folder_path, MEDIA_ROOT_PATH_USB) == 0))
146                                         is_root = TRUE;
147
148                                 SAFE_FREE(parent_folder_path);
149
150                                 if (is_root == TRUE)
151                                         break;
152                         }
153 #ifdef _USE_SENIOR_MODE
154                         if (_media_content_is_support_senior_mode()) {
155                                 if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL) && (strcmp(search_path, MEDIA_ROOT_PATH_SENIOR_MODE) == 0))
156                                         break;
157                         }
158 #endif
159
160                         leaf_path = strrchr(search_path, '/');
161                         if (leaf_path != NULL) {
162                                 int seek_len = leaf_path -search_path;
163                                 search_path[seek_len] = '\0';
164                                 /*media_content_sec_debug("go to other dir [%s]", search_path);*/
165                         } else {
166                                 media_content_debug("Fail to find leaf path");
167                                 break;
168                         }
169                 }
170         }
171
172         return MEDIA_CONTENT_ERROR_NONE;
173 }
174
175 int _media_content_replace_path_in_condition(const char *condition, char *replace_condition)
176 {
177         int ret = MEDIA_CONTENT_ERROR_NONE;
178         char *old_path = NULL;
179
180         ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &old_path);
181         if (ret != STORAGE_ERROR_NONE) {
182                 media_content_error("storage_get_directory failed");
183                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
184         }
185
186         media_content_sec_debug("Old condition[%s]", condition);
187         if (((strstr(condition, "PATH") != NULL) || (strstr(condition, "path") != NULL)) && (strstr(condition, old_path) != NULL)) {
188                 char *cond = strdup(condition);
189                 char *repl_cond_ptr = replace_condition;
190                 char *cond_ptr = cond;
191
192                 if (cond_ptr == NULL) {
193                         SAFE_FREE(old_path);
194                         media_content_error("memory allocation failed");
195                         return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
196                 }
197                 while (*cond_ptr != '\0') {
198                         if (strlen(cond_ptr) < strlen(old_path)) {
199                                 memcpy(repl_cond_ptr, cond_ptr, strlen(cond_ptr));
200                                 break;
201                         }
202                         /* replace path only and keep other condition */
203                         if (memcmp(cond_ptr, old_path, strlen(old_path)) == 0) {
204                                 memcpy(repl_cond_ptr, tzplatform_getenv(TZ_USER_CONTENT), strlen(tzplatform_getenv(TZ_USER_CONTENT)));
205                                 cond_ptr += strlen(old_path);
206                                 repl_cond_ptr += strlen(tzplatform_getenv(TZ_USER_CONTENT));
207                         } else {
208                                 *repl_cond_ptr = *cond_ptr;
209                                 cond_ptr++;
210                                 repl_cond_ptr++;
211                         }
212                 }
213                 SAFE_FREE(cond);
214         } else {
215                 snprintf(replace_condition, MAX_QUERY_SIZE, "%s", condition);
216         }
217
218         SAFE_FREE(old_path);
219
220         if (!STRING_VALID(replace_condition)) {
221                 media_content_error("replace failed");
222                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
223         }
224
225         media_content_sec_debug("repl cond[%s]", replace_condition);
226
227         return MEDIA_CONTENT_ERROR_NONE;
228 }
229
230 int _media_content_rollback_path_in_condition(const char *condition, char *replace_condition)
231 {
232         int ret = MEDIA_CONTENT_ERROR_NONE;
233         char *old_path = NULL;
234
235         ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &old_path);
236         if (ret != STORAGE_ERROR_NONE) {
237                 media_content_error("storage_get_directory failed");
238                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
239         }
240
241         media_content_sec_debug("Old condition[%s]", condition);
242         if (((strstr(condition, "PATH") != NULL) || (strstr(condition, "path") != NULL)) && (strstr(condition, tzplatform_getenv(TZ_USER_CONTENT)) != NULL)) {
243                 char *cond = strdup(condition);
244                 char *repl_cond_ptr = replace_condition;
245                 char *cond_ptr = cond;
246
247                 if (cond_ptr == NULL) {
248                         SAFE_FREE(old_path);
249                         media_content_error("memory allocation failed");
250                         return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
251                 }
252                 while (*cond_ptr != '\0') {
253                         if (strlen(cond_ptr) < strlen(tzplatform_getenv(TZ_USER_CONTENT))) {
254                                 memcpy(repl_cond_ptr, cond_ptr, strlen(cond_ptr));
255                                 break;
256                         }
257                         /* replace path only and keep other condition */
258                         if (memcmp(cond_ptr, tzplatform_getenv(TZ_USER_CONTENT), strlen(tzplatform_getenv(TZ_USER_CONTENT))) == 0) {
259                                 memcpy(repl_cond_ptr, old_path, strlen(old_path));
260                                 cond_ptr += strlen(tzplatform_getenv(TZ_USER_CONTENT));
261                                 repl_cond_ptr += strlen(old_path);
262                         } else {
263                                 *repl_cond_ptr = *cond_ptr;
264                                 cond_ptr++;
265                                 repl_cond_ptr++;
266                         }
267                 }
268                 SAFE_FREE(cond);
269         } else {
270                 snprintf(replace_condition, MAX_QUERY_SIZE, "%s", condition);
271         }
272
273         SAFE_FREE(old_path);
274
275         if (!STRING_VALID(replace_condition)) {
276                 media_content_error("replace failed");
277                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
278         }
279
280         media_content_sec_debug("repl cond[%s]", replace_condition);
281
282         return MEDIA_CONTENT_ERROR_NONE;
283 }
284
285 int _media_content_replace_path(const char *path, char *replace_path)
286 {
287         int ret = MEDIA_CONTENT_ERROR_NONE;
288         char *old_path = NULL;
289
290         ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &old_path);
291         if (ret != STORAGE_ERROR_NONE) {
292                 media_content_error("storage_get_directory failed");
293                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
294         }
295
296         if (strncmp(path, old_path, strlen(old_path)) == 0) {
297                 media_content_sec_debug("Old path[%s]", path);
298                 snprintf(replace_path, MAX_QUERY_SIZE, "%s%s", tzplatform_getenv(TZ_USER_CONTENT), path + strlen(old_path));
299         } else {
300                 snprintf(replace_path, MAX_QUERY_SIZE, "%s", path);
301         }
302
303         SAFE_FREE(old_path);
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         int ret = MEDIA_CONTENT_ERROR_NONE;
316         char *old_path = NULL;
317
318         ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &old_path);
319         if (ret != STORAGE_ERROR_NONE) {
320                 media_content_error("storage_get_directory failed");
321                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
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_QUERY_SIZE, "%s%s", old_path, path + strlen(tzplatform_getenv(TZ_USER_CONTENT)));
327         } else {
328                 snprintf(replace_path, MAX_QUERY_SIZE, "%s", path);
329         }
330
331         SAFE_FREE(old_path);
332
333         if (!STRING_VALID(replace_path)) {
334                 media_content_error("replace failed");
335                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
336         }
337
338         return MEDIA_CONTENT_ERROR_NONE;
339 }
340
341 #ifdef _USE_SENIOR_MODE
342 bool _media_content_is_support_senior_mode()
343 {
344         bool bSupportSeniorMode = false;
345
346         if (system_info_get_value_bool(SYSTEM_INFO_KEY_GET_SENIOR_MODE_SUPPORTED, &bSupportSeniorMode) != SYSTEM_INFO_ERROR_NONE) {
347                 media_content_debug("Get senior mode support failed");
348                 return false;
349         }
350         /* media_content_debug("Senior mode Support : [%d]", bSupportSeniorMode); */
351         return bSupportSeniorMode;
352 }
353 #endif
354