8aea3fa964e72da9184de1a65574acc98198a930
[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 #include <system_info.h>
23 #include <sys/stat.h>
24
25 static int MEDIA_CONTENT_OTHER_SUPPORT = -1;
26
27 bool _media_util_check_support_media_type(const char *path)
28 {
29         int ret = SYSTEM_INFO_ERROR_NONE;
30         int media_type = -1;
31         bool is_supported = false;
32
33         content_retvm_if(!STRING_VALID(path), false, "path is empty");
34
35         if (MEDIA_CONTENT_OTHER_SUPPORT == -1) {
36                 ret = system_info_get_platform_bool("http://tizen.org/feature/content.scanning.others", &is_supported);
37                 if (ret != SYSTEM_INFO_ERROR_NONE) {
38                         content_debug("SYSTEM_INFO_ERROR: content.scanning.others [%d]", ret);
39                         return false;
40                 }
41
42                 MEDIA_CONTENT_OTHER_SUPPORT = is_supported;
43         }
44
45         /* If not, check media type */
46         if (!MEDIA_CONTENT_OTHER_SUPPORT) {
47                 ret = media_svc_get_media_type(path, &media_type);
48                 content_retvm_if(ret != MS_MEDIA_ERR_NONE, false, "Failed to get media type");
49
50                 if (media_type == MEDIA_CONTENT_TYPE_OTHERS)
51                         return false;
52         }
53
54         return true;
55 }
56
57 int _media_util_check_file_exist(const char *path)
58 {
59         int exist;
60
61         /* check the file exits actually */
62         exist = open(path, O_RDONLY);
63         if (exist < 0) {
64                 if (errno == EACCES || errno == EPERM) {
65                         content_stderror("open file fail");
66                         content_sec_debug("path [%s]", path);
67                         return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
68                 } else {
69                         content_stderror("open file fail");
70                         content_sec_debug("path [%s]", path);
71                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
72                 }
73         }
74
75         close(exist);
76
77         return MEDIA_CONTENT_ERROR_NONE;
78 }
79
80 void _media_util_trim_path(const char *input_path, char **output_path)
81 {
82         gchar **name_list = NULL;
83         gchar *tmp_path = NULL;
84
85         if (!STRING_VALID(input_path) || output_path == NULL)
86                 return;
87
88         /* Workflow example
89                 Input : /a/b//c/
90                 After g_strsplit() : {'','a','b','','c',''}
91                 After g_build_pathv() : a/b/c
92                 After g_strdup_printf() : /a/b/c
93         */
94         name_list = g_strsplit(input_path, "/", -1);
95         if (!name_list)
96                 return;
97
98         tmp_path = g_build_pathv(G_DIR_SEPARATOR_S, name_list);
99         g_strfreev(name_list);
100         if (!tmp_path)
101                 return;
102
103         /* g_build_pathv does not add root '/' */
104         *output_path = g_strdup_printf("/%s", tmp_path);
105         g_free(tmp_path);
106 }
107
108
109 int _media_util_get_file_time(const char *path)
110 {
111         struct stat statbuf;
112         int ret = 0;
113
114         memset(&statbuf, 0, sizeof(struct stat));
115         ret = stat(path, &statbuf);
116         if (ret == -1) {
117                 content_stderror("stat failed");
118                 return ret;
119         }
120
121         return statbuf.st_mtime;
122 }
123
124 bool _media_util_is_ignorable_file(const char *path)
125 {
126         char *tmp_path = NULL;
127         char *org_path = NULL;
128
129 #ifndef _USE_TVPD_MODE
130         char replace[MAX_PATH_LEN] = {0, };
131 #endif
132
133         content_retip_if_fail(STRING_VALID(path));
134
135         /* Check is exist (It may be the path to the deleted file) */
136         if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
137                 content_sec_debug("removed path[%s]", path);
138                 return false;
139         }
140
141         /* Check symbolic link file */
142         if (g_file_test(path, G_FILE_TEST_IS_SYMLINK)) {
143                 content_error("symbolic link(file)");
144                 content_sec_debug("path : %s", path);
145                 return true;
146         }
147
148         /* Check hidden path */
149         if (strstr(path, "/.") != NULL) {
150                 content_error("hidden path");
151                 content_sec_debug("path : %s", path);
152                 return true;
153         }
154
155         /* Check symbolic directory */
156         tmp_path = realpath(path, NULL);
157         /* Get trimmed path */
158         _media_util_trim_path(path, &org_path);
159
160 #ifdef _USE_TVPD_MODE
161         if (g_strcmp0(tmp_path, org_path) != 0) {
162                 content_error("symbolic link(directory)");
163                 content_sec_debug("path[%s] real[%s]", org_path, tmp_path);
164                 SAFE_FREE(tmp_path);
165                 g_free(org_path);
166                 return true;
167         }
168 #else
169         if (g_str_has_prefix(tmp_path, MEDIA_SHARE_PATH)) {
170                 /* If shared dirctory, it should be change path to TZ_USER_SHARE from realpath */
171                 snprintf(replace, MAX_PATH_LEN, "%s%s", tzplatform_getenv(TZ_USER_MEDIASHARED), tmp_path + strlen(MEDIA_SHARE_PATH));
172                 if (g_strcmp0(replace, org_path) != 0) {
173                         content_error("symbolic link(directory)");
174                         content_sec_debug("path[%s] real[%s]", org_path, tmp_path);
175                         SAFE_FREE(tmp_path);
176                         g_free(org_path);
177                         return true;
178                 }
179         } else {
180                 if (g_strcmp0(tmp_path, org_path) != 0) {
181                         content_error("symbolic link(directory)");
182                         content_sec_debug("path[%s] real[%s]", org_path, tmp_path);
183                         SAFE_FREE(tmp_path);
184                         g_free(org_path);
185                         return true;
186                 }
187         }
188 #endif
189         SAFE_FREE(tmp_path);
190         g_free(org_path);
191
192         return false;
193 }
194
195 static bool __is_scan_ignore_exist(const char *path)
196 {
197         const char *scan_ignore = ".scan_ignore";
198         char *ignore_path = NULL;
199         gboolean result = FALSE;
200
201         if (!STRING_VALID(path))
202                 return false;
203
204         ignore_path = g_build_path(G_DIR_SEPARATOR_S, path, scan_ignore, NULL);
205         result = g_file_test(ignore_path, G_FILE_TEST_EXISTS);
206
207         if (result)
208                 content_error("scan ignore file exist [%s]", ignore_path);
209
210         g_free(ignore_path);
211
212         return (bool)result;
213 }
214
215 bool _media_util_is_ignorable_dir(const char *dir_path)
216 {
217         int ret = MEDIA_CONTENT_ERROR_NONE;
218         ms_user_storage_type_e storage_type = MS_USER_STORAGE_INTERNAL;
219
220         content_retip_if_fail(STRING_VALID(dir_path));
221
222         content_sec_debug("dir_path : %s", dir_path);
223
224         /*1. Check Hidden Directory*/
225         if (strstr(dir_path, "/.") != NULL) {
226                 content_error("hidden path");
227                 return true;
228         }
229
230         /*2. Check Scan Ignore Directory*/
231         ret = ms_user_get_storage_type(_content_get_uid(), dir_path, &storage_type);
232         if (ret != MS_MEDIA_ERR_NONE) {
233                 content_error("ms_user_get_storage_type failed : %d", ret);
234                 return false;
235         }
236
237         char *leaf_path = NULL;
238         char search_path[MAX_PATH_LEN] = {0, };
239
240         SAFE_STRLCPY(search_path, dir_path, sizeof(search_path));
241
242         while (STRING_VALID(search_path)) {
243                 if (__is_scan_ignore_exist(search_path))
244                         return true;
245
246                 leaf_path = strrchr(search_path, '/');
247                 if (!leaf_path)
248                         break;
249
250                 search_path[leaf_path - search_path] = '\0';
251         }
252
253         return false;
254 }
255
256 int _media_content_check_dir(const char *path)
257 {
258         DIR *dp = NULL;
259         char *real = NULL;
260         char *origin = NULL;
261 #ifndef _USE_TVPD_MODE
262         char result_path[MAX_PATH_LEN] = {0, };
263 #endif
264         dp = opendir(path);
265         if (dp == NULL) {
266                 if (errno == EACCES || errno == EPERM) {
267                         content_stderror("open dir fail");
268                         content_sec_error("path [%s]", path);
269                         return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
270                 } else {
271                         content_stderror("open dir fail");
272                         content_sec_error("path [%s]", path);
273                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
274                 }
275         }
276
277         closedir(dp);
278
279         /* Check symbolic link directory */
280         real = realpath(path, NULL);
281         /* Get trimmed path */
282         _media_util_trim_path(path, &origin);
283
284 #ifdef _USE_TVPD_MODE
285         if (g_strcmp0(real, origin) != 0) {
286                 content_error("symbolic link(directory)");
287                 content_sec_debug("path[%s] real[%s]", origin, real);
288                 SAFE_FREE(real);
289                 g_free(origin);
290                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
291         }
292 #else
293         if (g_str_has_prefix(real, MEDIA_SHARE_PATH)) {
294                 /* If shared dirctory, it should be change path to TZ_USER_SHARE from realpath */
295                 snprintf(result_path, MAX_PATH_LEN, "%s%s", tzplatform_getenv(TZ_USER_MEDIASHARED), real + strlen(MEDIA_SHARE_PATH));
296                 if (g_strcmp0(result_path, origin) != 0) {
297                         content_error("symbolic link(directory)");
298                         content_sec_debug("path[%s] real[%s]", origin, real);
299                         SAFE_FREE(real);
300                         g_free(origin);
301                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
302                 }
303         } else {
304                 if (g_strcmp0(real, origin) != 0) {
305                         content_error("symbolic link(directory)");
306                         content_sec_debug("path[%s] real[%s]", origin, real);
307                         SAFE_FREE(real);
308                         g_free(origin);
309                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
310                 }
311         }
312 #endif
313
314         SAFE_FREE(real);
315         g_free(origin);
316
317         return MEDIA_CONTENT_ERROR_NONE;
318 }
319
320
321 /* FIXME : If there are no issue reports related to this, it will be deleted in tizen 6.5 or after. */
322 char * _media_content_replace_path_in_condition(const char *condition)
323 {
324         return g_strdup(condition);
325 #if 0
326         char **split_list = NULL;
327         char *result = NULL;
328
329         if (!STRING_VALID(MEDIA_ROOT_PATH_INTERNAL_OLD) || !STRING_VALID(MEDIA_ROOT_PATH_INTERNAL))
330                 return NULL;
331
332         content_sec_debug("Old condition[%s]", condition);
333
334         split_list = g_strsplit(condition, MEDIA_ROOT_PATH_INTERNAL_OLD, -1);
335         if (!split_list)
336                 return NULL;
337
338         result = g_strjoinv(MEDIA_ROOT_PATH_INTERNAL, split_list);
339         g_strfreev(split_list);
340
341         return result;
342 #endif
343 }
344
345 /* FIXME : If there are no issue reports related to this, it will be deleted in Tizen 6.5 or after. */
346 int _media_content_replace_path(const char *path, char *replace_path)
347 {
348         content_retip_if_fail(STRING_VALID(path));
349
350         snprintf(replace_path, MAX_PATH_LEN, "%s", path);
351 #if 0
352         if (strncmp(path, MEDIA_ROOT_PATH_INTERNAL_OLD, strlen(MEDIA_ROOT_PATH_INTERNAL_OLD)) == 0) {
353                 content_sec_debug("Old path[%s]", path);
354                 snprintf(replace_path, MAX_PATH_LEN, "%s%s", MEDIA_ROOT_PATH_INTERNAL, path + strlen(MEDIA_ROOT_PATH_INTERNAL_OLD));
355         } else {
356                 snprintf(replace_path, MAX_PATH_LEN, "%s", path);
357         }
358 #endif
359
360         return MEDIA_CONTENT_ERROR_NONE;
361 }
362