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