2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <media_info_private.h>
21 #include <system_info.h>
24 static int MEDIA_CONTENT_OTHER_SUPPORT = -1;
26 bool _media_util_check_support_media_type(const char *path)
28 int ret = SYSTEM_INFO_ERROR_NONE;
30 bool is_supported = false;
32 content_retvm_if(!STRING_VALID(path), false, "path is empty");
34 if (MEDIA_CONTENT_OTHER_SUPPORT == -1) {
35 ret = system_info_get_platform_bool("http://tizen.org/feature/content.scanning.others", &is_supported);
36 if (ret != SYSTEM_INFO_ERROR_NONE) {
37 content_debug("SYSTEM_INFO_ERROR: content.scanning.others [%d]", ret);
41 MEDIA_CONTENT_OTHER_SUPPORT = is_supported;
44 /* If not, check media type */
45 if (!MEDIA_CONTENT_OTHER_SUPPORT) {
46 ret = media_svc_get_media_type(path, &media_type);
47 content_retvm_if(ret != MS_MEDIA_ERR_NONE, false, "Failed to get media type");
49 if (media_type == MEDIA_CONTENT_TYPE_OTHERS)
56 int _media_util_check_file_exist(const char *path)
60 /* check the file exits actually */
61 exist = open(path, O_RDONLY);
63 if (errno == EACCES || errno == EPERM) {
64 content_stderror("open file fail");
65 content_sec_debug("path [%s]", path);
66 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
68 content_stderror("open file fail");
69 content_sec_debug("path [%s]", path);
70 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
76 return MEDIA_CONTENT_ERROR_NONE;
79 int _media_util_get_file_time(const char *path)
84 memset(&statbuf, 0, sizeof(struct stat));
85 ret = stat(path, &statbuf);
87 content_stderror("stat failed");
91 return statbuf.st_mtime;
94 bool _media_util_is_ignorable_file(const char *path)
96 char *tmp_path = NULL;
97 char *org_path = NULL;
99 #ifndef _USE_TVPD_MODE
100 char replace[MAX_PATH_LEN] = {0, };
103 content_retip_if_fail(STRING_VALID(path));
105 /* Check is exist (It may be the path to the deleted file) */
106 if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
107 content_sec_debug("removed path[%s]", path);
111 /* Check symbolic link file */
112 if (g_file_test(path, G_FILE_TEST_IS_SYMLINK)) {
113 content_error("symbolic link(file)");
114 content_sec_debug("path : %s", path);
118 /* Check hidden path */
119 if (strstr(path, "/.") != NULL) {
120 content_error("hidden path");
121 content_sec_debug("path : %s", path);
125 /* Check symbolic directory */
126 tmp_path = realpath(path, NULL);
127 /* Get trimmed path */
128 org_path = g_canonicalize_filename(path, NULL);
130 #ifdef _USE_TVPD_MODE
131 if (g_strcmp0(tmp_path, org_path) != 0) {
132 content_error("symbolic link(directory)");
133 content_sec_debug("path[%s] real[%s]", org_path, tmp_path);
139 if (g_str_has_prefix(tmp_path, MEDIA_SHARE_PATH)) {
140 /* If shared directory, it should be change path to TZ_USER_SHARE from realpath */
141 snprintf(replace, MAX_PATH_LEN, "%s%s", tzplatform_getenv(TZ_USER_MEDIASHARED), tmp_path + strlen(MEDIA_SHARE_PATH));
142 if (g_strcmp0(replace, org_path) != 0) {
143 content_error("symbolic link(directory)");
144 content_sec_debug("path[%s] real[%s]", org_path, tmp_path);
150 if (g_strcmp0(tmp_path, org_path) != 0) {
151 content_error("symbolic link(directory)");
152 content_sec_debug("path[%s] real[%s]", org_path, tmp_path);
165 static bool __is_scan_ignore_exist(const char *path)
167 const char *scan_ignore = ".scan_ignore";
168 char *ignore_path = NULL;
169 gboolean result = FALSE;
171 if (!STRING_VALID(path))
174 ignore_path = g_build_path(G_DIR_SEPARATOR_S, path, scan_ignore, NULL);
175 result = g_file_test(ignore_path, G_FILE_TEST_EXISTS);
178 content_error("scan ignore file exist [%s]", ignore_path);
185 bool _media_util_is_ignorable_dir(const char *dir_path)
187 int ret = MEDIA_CONTENT_ERROR_NONE;
188 ms_user_storage_type_e storage_type = MS_USER_STORAGE_INTERNAL;
190 content_retip_if_fail(STRING_VALID(dir_path));
192 content_sec_debug("dir_path : %s", dir_path);
194 /*1. Check Hidden Directory*/
195 if (strstr(dir_path, "/.") != NULL) {
196 content_error("hidden path");
200 /*2. Check Scan Ignore Directory*/
201 ret = ms_user_get_storage_type(_content_get_uid(), dir_path, &storage_type);
202 if (ret != MS_MEDIA_ERR_NONE) {
203 content_error("ms_user_get_storage_type failed : %d", ret);
207 char *leaf_path = NULL;
208 char search_path[MAX_PATH_LEN] = {0, };
210 SAFE_STRLCPY(search_path, dir_path, sizeof(search_path));
212 while (STRING_VALID(search_path)) {
213 if (__is_scan_ignore_exist(search_path))
216 leaf_path = strrchr(search_path, '/');
220 search_path[leaf_path - search_path] = '\0';
226 int _media_content_check_dir(const char *path)
231 #ifndef _USE_TVPD_MODE
232 char result_path[MAX_PATH_LEN] = {0, };
236 if (errno == EACCES || errno == EPERM) {
237 content_stderror("open dir fail");
238 content_sec_error("path [%s]", path);
239 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
241 content_stderror("open dir fail");
242 content_sec_error("path [%s]", path);
243 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
249 /* Check symbolic link directory */
250 real = realpath(path, NULL);
251 /* Get trimmed path */
252 origin = g_canonicalize_filename(path, NULL);
254 #ifdef _USE_TVPD_MODE
255 if (g_strcmp0(real, origin) != 0) {
256 content_error("symbolic link(directory)");
257 content_sec_debug("path[%s] real[%s]", origin, real);
260 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
263 if (g_str_has_prefix(real, MEDIA_SHARE_PATH)) {
264 /* If shared directory, it should be change path to TZ_USER_SHARE from realpath */
265 snprintf(result_path, MAX_PATH_LEN, "%s%s", tzplatform_getenv(TZ_USER_MEDIASHARED), real + strlen(MEDIA_SHARE_PATH));
266 if (g_strcmp0(result_path, origin) != 0) {
267 content_error("symbolic link(directory)");
268 content_sec_debug("path[%s] real[%s]", origin, real);
271 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
274 if (g_strcmp0(real, origin) != 0) {
275 content_error("symbolic link(directory)");
276 content_sec_debug("path[%s] real[%s]", origin, real);
279 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
287 return MEDIA_CONTENT_ERROR_NONE;