Support user session path
[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(const char *path, char *replace_path)
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         if (strncmp(path, old_path, strlen(old_path)) == 0) {
187                 media_content_sec_debug("Old path[%s]", path);
188                 snprintf(replace_path, MAX_QUERY_SIZE, "%s%s", tzplatform_getenv(TZ_USER_CONTENT), path + strlen(old_path));
189         } else {
190                 snprintf(replace_path, MAX_QUERY_SIZE, "%s", path);
191         }
192
193         SAFE_FREE(old_path);
194
195         if (!STRING_VALID(replace_path)) {
196                 media_content_error("replace failed");
197                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
198         }
199
200         return MEDIA_CONTENT_ERROR_NONE;
201 }
202
203 int _media_content_rollback_path(const char *path, char *replace_path)
204 {
205         int ret = MEDIA_CONTENT_ERROR_NONE;
206         char *old_path = NULL;
207
208         ret = storage_get_root_directory(STORAGE_TYPE_INTERNAL, &old_path);
209         if (ret != STORAGE_ERROR_NONE) {
210                 media_content_error("storage_get_directory failed");
211                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
212         }
213
214         if (strncmp(path, tzplatform_getenv(TZ_USER_CONTENT), strlen(tzplatform_getenv(TZ_USER_CONTENT))) == 0) {
215                 media_content_sec_debug("new path[%s]", path);
216                 snprintf(replace_path, MAX_QUERY_SIZE, "%s%s", old_path, path + strlen(tzplatform_getenv(TZ_USER_CONTENT)));
217         } else {
218                 snprintf(replace_path, MAX_QUERY_SIZE, "%s", path);
219         }
220
221         SAFE_FREE(old_path);
222
223         if (!STRING_VALID(replace_path)) {
224                 media_content_error("replace failed");
225                 return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
226         }
227
228         return MEDIA_CONTENT_ERROR_NONE;
229 }
230
231 #ifdef _USE_SENIOR_MODE
232 bool _media_content_is_support_senior_mode()
233 {
234         bool bSupportSeniorMode = false;
235
236         if (system_info_get_value_bool(SYSTEM_INFO_KEY_GET_SENIOR_MODE_SUPPORTED, &bSupportSeniorMode) != SYSTEM_INFO_ERROR_NONE) {
237                 media_content_debug("Get senior mode support failed");
238                 return false;
239         }
240         /* media_content_debug("Senior mode Support : [%d]", bSupportSeniorMode); */
241         return bSupportSeniorMode;
242 }
243 #endif
244