37786b4a95671a9e3dd1a6534d883eda7b89cf60
[framework/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
26 int _media_util_check_file_exist(const char *path)
27 {
28         int exist;
29
30         /* check the file exits actually */
31         exist = open(path, O_RDONLY);
32         if(exist < 0) {
33                 media_content_sec_debug("path [%s]", path);
34                 media_content_stderror("open file fail");
35                 if (errno == EACCES || errno == EPERM) {
36                         return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
37                 } else {
38                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
39                 }
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         {
55                 *ignore = TRUE;
56                 media_content_error("hidden path");
57                 media_content_sec_debug("path : %s", path);
58         }
59
60         return MEDIA_CONTENT_ERROR_NONE;
61 }
62
63 int _media_util_check_ignore_dir(const char *dir_path, bool *ignore)
64 {
65         int ret = MEDIA_CONTENT_ERROR_NONE;
66         media_svc_storage_type_e storage_type = 0;
67         const char *scan_ignore = ".scan_ignore";
68         bool find = false;
69
70         media_content_sec_debug("dir_path : %s", dir_path);
71
72         media_content_retvm_if(!STRING_VALID(dir_path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid dir_path");
73
74         *ignore = FALSE;
75         /*1. Check Hidden Directory*/
76         if(strstr(dir_path, "/.") != NULL)
77         {
78                 *ignore = TRUE;
79                 media_content_error("hidden path");
80                 return MEDIA_CONTENT_ERROR_NONE;
81         }
82
83         /*2. Check Scan Ignore Directory*/
84         ret = media_svc_get_storage_type(dir_path, &storage_type);
85         if(ret != MS_MEDIA_ERR_NONE)
86         {
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         {
101                 dp = opendir(search_path);
102                 if (dp == NULL) {
103                         *ignore = TRUE;
104                         media_content_error("Open Directory fail");
105                         media_content_sec_debug("Open fail path[%s]", search_path);
106                         if (errno == EACCES || errno == EPERM) {
107                                 return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
108                         } else {
109                                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
110                         }
111                 }
112
113                 media_content_retvm_if(dp == NULL, MEDIA_CONTENT_ERROR_INVALID_OPERATION, "Open Directory fail");
114
115                 while (!readdir_r(dp, &entry, &result))
116                 {
117                         if (result == NULL)
118                                 break;
119
120                         if(STRING_VALID(entry.d_name) && (strcmp(entry.d_name, scan_ignore) == 0))
121                         {
122                                 media_content_error("Find Ignore path");
123                                 media_content_sec_debug("Ignore path[%s]", search_path);
124                                 find = TRUE;
125                                 break;
126                         }
127                         else
128                         {
129                                 //media_content_sec_debug("entry.d_name[%s]", entry.d_name);
130                                 continue;
131                         }
132                 }
133
134                 if (dp) closedir(dp);
135                 dp = NULL;
136
137                 if(find)
138                 {
139                         *ignore = TRUE;
140                         break;
141                 }
142                 else
143                 {
144                         /*If root path, Stop Scanning*/
145                         if((storage_type == MEDIA_SVC_STORAGE_INTERNAL) && (strcmp(search_path, MEDIA_ROOT_PATH_INTERNAL) == 0))
146                         {
147                                 break;
148                         }
149                         else if((storage_type == MEDIA_SVC_STORAGE_EXTERNAL) && (strcmp(search_path, MEDIA_ROOT_PATH_SDCARD) == 0))
150                         {
151                                 break;
152                         }
153                         else if(storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB)
154                         {
155                                 char *parent_folder_path = NULL;
156                                 bool is_root = FALSE;
157
158                                 parent_folder_path = g_path_get_dirname(search_path);
159                                 if (strcmp(search_path, MEDIA_ROOT_PATH_USB) == 0)
160                                         is_root = TRUE;
161
162                                 SAFE_FREE(parent_folder_path);
163
164                                 if (is_root == TRUE)
165                                         break;
166                         }
167
168                         leaf_path = strrchr(search_path, '/');
169                         if(leaf_path != NULL)
170                         {
171                                 int seek_len = leaf_path -search_path;
172                                 search_path[seek_len] = '\0';
173                                 //media_content_sec_debug("go to other dir [%s]", search_path);
174                         }
175                         else
176                         {
177                                 media_content_debug("Fail to find leaf path");
178                                 break;
179                         }
180                 }
181         }
182
183         return MEDIA_CONTENT_ERROR_NONE;
184 }