Apply tizen coding rule
[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
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                 *ignore = TRUE;
55                 media_content_error("hidden path");
56                 media_content_sec_debug("path : %s", path);
57         }
58
59         return MEDIA_CONTENT_ERROR_NONE;
60 }
61
62 int _media_util_check_ignore_dir(const char *dir_path, bool *ignore)
63 {
64         int ret = MEDIA_CONTENT_ERROR_NONE;
65         media_svc_storage_type_e storage_type = 0;
66         const char *scan_ignore = ".scan_ignore";
67         bool find = false;
68
69         media_content_sec_debug("dir_path : %s", dir_path);
70
71         media_content_retvm_if(!STRING_VALID(dir_path), MEDIA_CONTENT_ERROR_INVALID_PARAMETER, "invalid dir_path");
72
73         *ignore = FALSE;
74         /*1. Check Hidden Directory*/
75         if (strstr(dir_path, "/.") != NULL) {
76                 *ignore = TRUE;
77                 media_content_error("hidden path");
78                 return MEDIA_CONTENT_ERROR_NONE;
79         }
80
81         /*2. Check Scan Ignore Directory*/
82         ret = media_svc_get_storage_type(dir_path, &storage_type, tzplatform_getuid(TZ_USER_NAME));
83         if (ret != MS_MEDIA_ERR_NONE) {
84                 media_content_error("media_svc_get_storage_type failed : %d", ret);
85                 return _content_error_capi(MEDIA_CONTENT_TYPE, ret);
86         }
87
88         DIR *dp = NULL;
89         struct dirent entry;
90         struct dirent *result = NULL;
91
92         char *leaf_path = NULL;
93         char search_path[4096] = {0, };
94
95         strncpy(search_path, dir_path, strlen(dir_path));
96         while (STRING_VALID(search_path)) {
97                 dp = opendir(search_path);
98                 if (dp == NULL) {
99                         *ignore = TRUE;
100                         media_content_error("Open Directory fail");
101                         media_content_sec_debug("Open fail path[%s]", search_path);
102                         return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
103                 }
104
105                 media_content_retvm_if(dp == NULL, MEDIA_CONTENT_ERROR_INVALID_OPERATION, "Open Directory fail");
106
107                 while (!readdir_r(dp, &entry, &result)) {
108                         if (result == NULL)
109                                 break;
110
111                         if (STRING_VALID(entry.d_name) && (strcmp(entry.d_name, scan_ignore) == 0)) {
112                                 media_content_error("Find Ignore path");
113                                 media_content_sec_debug("Ignore path[%s]", search_path);
114                                 find = TRUE;
115                                 break;
116                         } else {
117                                 //media_content_sec_debug("entry.d_name[%s]", entry.d_name);
118                                 continue;
119                         }
120                 }
121
122                 if (dp) closedir(dp);
123                 dp = NULL;
124
125                 if (find) {
126                         *ignore = TRUE;
127                         break;
128                 } else {
129                         /*If root path, Stop Scanning*/
130                         if ((storage_type == MEDIA_SVC_STORAGE_INTERNAL) && (STRING_VALID(MEDIA_ROOT_PATH_INTERNAL) && strcmp(search_path, MEDIA_ROOT_PATH_INTERNAL) == 0)) {
131                                 break;
132                         } else if ((storage_type == MEDIA_SVC_STORAGE_EXTERNAL) && (STRING_VALID(MEDIA_ROOT_PATH_SDCARD)) && (strcmp(search_path, MEDIA_ROOT_PATH_SDCARD) == 0)) {
133                                 break;
134                         } else if (storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB) {
135                                 char *parent_folder_path = NULL;
136                                 bool is_root = FALSE;
137
138                                 parent_folder_path = g_path_get_dirname(search_path);
139                                 if (STRING_VALID(MEDIA_ROOT_PATH_USB) && (strcmp(parent_folder_path, MEDIA_ROOT_PATH_USB) == 0))
140                                         is_root = TRUE;
141
142                                 SAFE_FREE(parent_folder_path);
143
144                                 if (is_root == TRUE)
145                                         break;
146                         }
147
148                         leaf_path = strrchr(search_path, '/');
149                         if (leaf_path != NULL) {
150                                 int seek_len = leaf_path -search_path;
151                                 search_path[seek_len] = '\0';
152                                 //media_content_sec_debug("go to other dir [%s]", search_path);
153                         } else {
154                                 media_content_debug("Fail to find leaf path");
155                                 break;
156                         }
157                 }
158         }
159
160         return MEDIA_CONTENT_ERROR_NONE;
161 }