Improve readability
[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 (!MEDIA_CONTENT_OTHER_SUPPORT) {
42                 ret = media_svc_get_media_type(path, &media_type);
43                 content_retvm_if(ret != MS_MEDIA_ERR_NONE, false, "Failed to get media type");
44                 content_retv_if(media_type == MEDIA_CONTENT_TYPE_OTHERS, false);
45         }
46
47         return true;
48 }
49
50 int _media_util_check_file_exist(const char *path)
51 {
52         int fd = open(path, O_RDONLY);
53         if (fd < 0) {
54                 content_retvm_if(errno == EACCES || errno == EPERM, MEDIA_CONTENT_ERROR_PERMISSION_DENIED, "permission denied");
55                 content_stderror("open file failed");
56                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
57         }
58
59         close(fd);
60
61         return MEDIA_CONTENT_ERROR_NONE;
62 }
63
64 int _media_util_get_file_time(const char *path)
65 {
66         struct stat statbuf = { 0, };
67
68         content_retvm_if(stat(path, &statbuf) == -1, 0, "stat failed");
69
70         return statbuf.st_mtime;
71 }
72
73 static bool __media_util_is_valid_path(const char *path)
74 {
75         g_autofree gchar *real = NULL;
76         g_autofree gchar *org_path = NULL;
77 #ifndef _USE_TVPD_MODE
78         char replace[MAX_PATH_LEN] = {0, };
79 #endif
80         content_retv_if(!STRING_VALID(path), false);
81
82         real = realpath(path, NULL);
83         org_path = g_canonicalize_filename(path, NULL);
84
85 #ifndef _USE_TVPD_MODE
86         if (g_str_has_prefix(real, tzplatform_getenv(TZ_SYS_MEDIASHARED))) {
87                 snprintf(replace, MAX_PATH_LEN, "%s%s", tzplatform_getenv(TZ_USER_MEDIASHARED), real + strlen(tzplatform_getenv(TZ_SYS_MEDIASHARED)));
88                 g_free(real);
89                 real = g_strdup(replace);
90         }
91 #endif
92         content_retvm_if(g_strcmp0(real, org_path) != 0, false, "symbolic link(directory)");
93
94         return true;
95 }
96
97 bool _media_util_is_ignorable_file(const char *path)
98 {
99         content_retv_if(!STRING_VALID(path), true);
100         content_retvm_if(!g_file_test(path, G_FILE_TEST_EXISTS), false, "[no error] removed path");
101         content_retvm_if(g_file_test(path, G_FILE_TEST_IS_SYMLINK), true, "[no error] symbolic link(file)");
102         content_retvm_if(strstr(path, "/."), true, "[no error] hidden path");
103
104         return (!__media_util_is_valid_path(path));
105 }
106
107 static bool __is_scan_ignore_exist(const char *path)
108 {
109         g_autofree gchar *ignore_path = g_build_path(G_DIR_SEPARATOR_S, path, ".scan_ignore", NULL);
110         return (bool)g_file_test(ignore_path, G_FILE_TEST_EXISTS);
111 }
112
113 bool _media_util_is_ignorable_dir(const char *dir_path)
114 {
115         char *leaf_path = NULL;
116         char search_path[MAX_PATH_LEN] = {0, };
117
118         content_retv_if(!STRING_VALID(dir_path), true);
119         content_retvm_if(strstr(dir_path, "/."), true, "hidden path");
120         content_retvm_if(!ms_user_is_valid_path(_content_get_uid(), dir_path), true, "ms_user_is_valid_path failed");
121
122         g_strlcpy(search_path, dir_path, sizeof(search_path));
123
124         while (strlen(search_path) > 0) {
125                 content_retvm_if(__is_scan_ignore_exist(search_path), true, "scan ignore file exist");
126
127                 leaf_path = strrchr(search_path, '/');
128                 if (!leaf_path)
129                         break;
130
131                 search_path[leaf_path - search_path] = '\0';
132         }
133
134         return false;
135 }
136
137 int _media_content_check_dir(const char *path)
138 {
139         DIR *d = opendir(path);
140         if (!d) {
141                 content_retvm_if(errno == EACCES, MEDIA_CONTENT_ERROR_PERMISSION_DENIED, "permission denied");
142                 content_stderror("opendir failed");
143                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
144         }
145
146         closedir(d);
147
148         content_retv_if(!__media_util_is_valid_path(path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
149
150         return MEDIA_CONTENT_ERROR_NONE;
151 }