Reinforce line coverage
[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 <system_info.h>
22 #include <sys/stat.h>
23
24 static int MEDIA_CONTENT_OTHER_SUPPORT = -1;
25
26 bool _media_util_check_support_media_type(const char *path)
27 {
28         int ret = SYSTEM_INFO_ERROR_NONE;
29         int media_type = -1;
30         bool is_supported = false;
31
32         content_retvm_if(!STRING_VALID(path), false, "path is empty");
33
34         if (MEDIA_CONTENT_OTHER_SUPPORT == -1) {
35                 ret = system_info_get_platform_bool("http://tizen.org/feature/content.scanning.others", &is_supported);
36                 content_retvm_if(ret != SYSTEM_INFO_ERROR_NONE, false, "SYSTEM_INFO_ERROR: content.scanning.others [%d]", ret);
37
38                 MEDIA_CONTENT_OTHER_SUPPORT = is_supported;
39         }
40
41         /* If not, check media type */
42         if (!MEDIA_CONTENT_OTHER_SUPPORT) {
43                 ret = media_svc_get_media_type(path, &media_type);
44                 content_retvm_if(ret != MS_MEDIA_ERR_NONE, false, "Failed to get media type");
45                 content_retv_if(media_type == MEDIA_CONTENT_TYPE_OTHERS, false);
46         }
47
48         return true;
49 }
50
51 int _media_util_check_file_exist(const char *path)
52 {
53         int exist;
54
55         /* check the file exits actually */
56         exist = open(path, O_RDONLY);
57         if (exist < 0) {
58                 if (errno == EACCES || errno == EPERM) {
59                         content_stderror("open file fail");
60                         return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
61                 } else {
62                         content_stderror("open file fail");
63                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
64                 }
65         }
66
67         close(exist);
68
69         return MEDIA_CONTENT_ERROR_NONE;
70 }
71
72 int _media_util_get_file_time(const char *path)
73 {
74         struct stat statbuf = { 0, };
75
76         content_retvm_if(stat(path, &statbuf) == -1, 0, "stat failed");
77
78         return statbuf.st_mtime;
79 }
80
81 bool _media_util_is_ignorable_file(const char *path)
82 {
83         char *tmp_path = NULL;
84         g_autofree gchar *org_path = NULL;
85
86 #ifndef _USE_TVPD_MODE
87         char replace[MAX_PATH_LEN] = {0, };
88 #endif
89
90         content_retip_if_fail(STRING_VALID(path));
91
92         /* Check is exist (It may be the path to the deleted file) */
93         content_retvm_if(!g_file_test(path, G_FILE_TEST_EXISTS), false, "removed path");
94
95         /* Check symbolic link file */
96         content_retvm_if(g_file_test(path, G_FILE_TEST_IS_SYMLINK), true, "symbolic link(file)");
97
98         /* Check hidden path */
99         content_retvm_if(strstr(path, "/."), true, "hidden path");
100
101         /* Check symbolic directory */
102         tmp_path = realpath(path, NULL);
103         /* Get trimmed path */
104         org_path = g_canonicalize_filename(path, NULL);
105
106 #ifdef _USE_TVPD_MODE
107         if (g_strcmp0(tmp_path, org_path) != 0) {
108                 content_error("symbolic link(directory)");
109                 SAFE_FREE(tmp_path);
110                 return true;
111         }
112 #else
113         if (g_str_has_prefix(tmp_path, tzplatform_getenv(TZ_SYS_MEDIASHARED))) {
114                 /* If shared directory, it should be change path to TZ_USER_SHARE from realpath */
115                 snprintf(replace, MAX_PATH_LEN, "%s%s", tzplatform_getenv(TZ_USER_MEDIASHARED), tmp_path + strlen(tzplatform_getenv(TZ_SYS_MEDIASHARED)));
116                 if (g_strcmp0(replace, org_path) != 0) {
117                         content_error("symbolic link(directory)");
118                         SAFE_FREE(tmp_path);
119                         return true;
120                 }
121         } else {
122                 if (g_strcmp0(tmp_path, org_path) != 0) {
123                         content_error("symbolic link(directory)");
124                         SAFE_FREE(tmp_path);
125                         return true;
126                 }
127         }
128 #endif
129         SAFE_FREE(tmp_path);
130
131         return false;
132 }
133
134 static bool __is_scan_ignore_exist(const char *path)
135 {
136         const char *scan_ignore = ".scan_ignore";
137         char *ignore_path = NULL;
138         gboolean result = FALSE;
139
140         if (!STRING_VALID(path))
141                 return false;
142
143         ignore_path = g_build_path(G_DIR_SEPARATOR_S, path, scan_ignore, NULL);
144         result = g_file_test(ignore_path, G_FILE_TEST_EXISTS);
145
146         if (result)
147                 content_error("scan ignore file exist [%s]", ignore_path);
148
149         g_free(ignore_path);
150
151         return (bool)result;
152 }
153
154 bool _media_util_is_ignorable_dir(const char *dir_path)
155 {
156         if (!STRING_VALID(dir_path))
157                 return true;
158
159         /*1. Check Hidden Directory*/
160         content_retvm_if(strstr(dir_path, "/."), true, "hidden path");
161
162         /*2. Check Scan Ignore Directory*/
163         content_retvm_if(!ms_user_is_valid_path(_content_get_uid(), dir_path), true, "ms_user_is_valid_path failed");
164
165         char *leaf_path = NULL;
166         char search_path[MAX_PATH_LEN] = {0, };
167
168         SAFE_STRLCPY(search_path, dir_path, sizeof(search_path));
169
170         while (STRING_VALID(search_path)) {
171                 if (__is_scan_ignore_exist(search_path))
172                         return true;
173
174                 leaf_path = strrchr(search_path, '/');
175                 if (!leaf_path)
176                         break;
177
178                 search_path[leaf_path - search_path] = '\0';
179         }
180
181         return false;
182 }
183
184 int _media_content_check_dir(const char *path)
185 {
186         DIR *dp = NULL;
187         char *real = NULL;
188         g_autofree gchar *origin = NULL;
189 #ifndef _USE_TVPD_MODE
190         char result_path[MAX_PATH_LEN] = {0, };
191 #endif
192         dp = opendir(path);
193         if (dp == NULL) {
194                 if (errno == EACCES || errno == EPERM) {
195                         content_stderror("open dir fail");
196                         return MEDIA_CONTENT_ERROR_PERMISSION_DENIED;
197                 } else {
198                         content_stderror("open dir fail");
199                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
200                 }
201         }
202
203         closedir(dp);
204
205         /* Check symbolic link directory */
206         real = realpath(path, NULL);
207         /* Get trimmed path */
208         origin = g_canonicalize_filename(path, NULL);
209
210 #ifdef _USE_TVPD_MODE
211         if (g_strcmp0(real, origin) != 0) {
212                 content_error("symbolic link(directory)");
213                 SAFE_FREE(real);
214                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
215         }
216 #else
217         if (g_str_has_prefix(real, tzplatform_getenv(TZ_SYS_MEDIASHARED))) {
218                 /* If shared directory, it should be change path to TZ_USER_SHARE from realpath */
219                 snprintf(result_path, MAX_PATH_LEN, "%s%s", tzplatform_getenv(TZ_USER_MEDIASHARED), real + strlen(tzplatform_getenv(TZ_SYS_MEDIASHARED)));
220                 if (g_strcmp0(result_path, origin) != 0) {
221                         content_error("symbolic link(directory)");
222                         SAFE_FREE(real);
223                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
224                 }
225         } else {
226                 if (g_strcmp0(real, origin) != 0) {
227                         content_error("symbolic link(directory)");
228                         SAFE_FREE(real);
229                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
230                 }
231         }
232 #endif
233
234         SAFE_FREE(real);
235
236         return MEDIA_CONTENT_ERROR_NONE;
237 }