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;
189 if (!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 if (!ms_user_is_valid_path(_content_get_uid(), dir_path)) {
202 content_error("ms_user_is_valid_path failed : %d", ret);
206 char *leaf_path = NULL;
207 char search_path[MAX_PATH_LEN] = {0, };
209 SAFE_STRLCPY(search_path, dir_path, sizeof(search_path));
211 while (STRING_VALID(search_path)) {
212 if (__is_scan_ignore_exist(search_path))
215 leaf_path = strrchr(search_path, '/');
219 search_path[leaf_path - search_path] = '\0';
225 int _media_content_check_dir(const char *path)
230 #ifndef _USE_TVPD_MODE
231 char result_path[MAX_PATH_LEN] = {0, };
235 if (errno == EACCES || errno == EPERM) {
236 content_stderror("open dir fail");
237 content_sec_error("path [%s]", path);
238 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
240 content_stderror("open dir fail");
241 content_sec_error("path [%s]", path);
242 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
248 /* Check symbolic link directory */
249 real = realpath(path, NULL);
250 /* Get trimmed path */
251 origin = g_canonicalize_filename(path, NULL);
253 #ifdef _USE_TVPD_MODE
254 if (g_strcmp0(real, origin) != 0) {
255 content_error("symbolic link(directory)");
256 content_sec_debug("path[%s] real[%s]", origin, real);
259 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
262 if (g_str_has_prefix(real, MEDIA_SHARE_PATH)) {
263 /* If shared directory, it should be change path to TZ_USER_SHARE from realpath */
264 snprintf(result_path, MAX_PATH_LEN, "%s%s", tzplatform_getenv(TZ_USER_MEDIASHARED), real + strlen(MEDIA_SHARE_PATH));
265 if (g_strcmp0(result_path, origin) != 0) {
266 content_error("symbolic link(directory)");
267 content_sec_debug("path[%s] real[%s]", origin, real);
270 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
273 if (g_strcmp0(real, origin) != 0) {
274 content_error("symbolic link(directory)");
275 content_sec_debug("path[%s] real[%s]", origin, real);
278 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
286 return MEDIA_CONTENT_ERROR_NONE;