modify DB schema.
[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 <string.h>
19 #include <sys/stat.h>
20 #include <media_util_private.h>
21 #include <media_info_private.h>
22 #include <media_content_type.h>
23
24
25 int _media_util_get_store_type_by_path(const char *path, int *storage_type)
26 {
27         if(STRING_VALID(path))
28         {
29                 if(strncmp(path, MEDIA_CONTENT_PATH_PHONE, strlen(MEDIA_CONTENT_PATH_PHONE)) == 0)
30                 {
31                         *storage_type = MEDIA_CONTENT_STORAGE_INTERNAL;
32                 }
33                 else if(strncmp (path, MEDIA_CONTENT_PATH_MMC, strlen(MEDIA_CONTENT_PATH_MMC)) == 0)
34                 {
35                         *storage_type = MEDIA_CONTENT_STORAGE_EXTERNAL;
36                 }
37         }
38         else
39         {
40                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
41                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
42         }
43
44         return MEDIA_CONTENT_ERROR_NONE;
45 }
46
47 int _media_util_check_ignore_dir(const char *dir_path, bool *ignore)
48 {
49         int ret = MEDIA_CONTENT_ERROR_NONE;
50         media_svc_storage_type_e storage_type = 0;
51         char *scan_ignore = ".scan_ignore";
52         bool find = false;
53
54         media_content_info("dir_path : %s", dir_path);
55
56         if(!STRING_VALID(dir_path))
57         {
58                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
59                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
60         }
61
62         *ignore = FALSE;
63         /*1. Check Hidden Directory*/
64         if(strstr(dir_path, "/."))
65         {
66                 *ignore = TRUE;
67                 media_content_info("hidden path");
68                 return MEDIA_CONTENT_ERROR_NONE;
69         }
70
71         /*2. Check Scan Ignore Directory*/
72         ret = media_svc_get_storage_type(dir_path, &storage_type);
73         if(ret != MEDIA_INFO_ERROR_NONE)
74         {
75                 media_content_error("media_svc_get_storage_type failed : %d", ret);
76                 return _content_error_capi(MEDIA_CONTENT_TYPE, ret);
77         }
78
79         DIR *dp = NULL;
80         struct dirent entry;
81         struct dirent *result = NULL;
82
83         char *leaf_path = NULL;
84         char search_path[4096] = {0, };
85
86         strncpy(search_path, dir_path, strlen(dir_path));
87         while(STRING_VALID(search_path))
88         {
89                 dp = opendir(search_path);
90                 if(dp == NULL)
91                 {
92                         media_content_error("Fail Open Directory");
93                         return MEDIA_CONTENT_ERROR_INVALID_OPERATION;
94                 }
95
96                 while (!readdir_r(dp, &entry, &result))
97                 {
98                         if (result == NULL)
99                                 break;
100
101                         if(STRING_VALID(entry.d_name) && (strcmp(entry.d_name, scan_ignore) == 0))
102                         {
103                                 media_content_info("Find Ignore path");
104                                 media_content_info("Ignore path[%s]", search_path);
105                                 find = TRUE;
106                                 break;
107                         }
108                         else
109                         {
110                                 //media_content_info("entry.d_name[%s]", entry.d_name);
111                                 continue;
112                         }
113                 }
114
115                 if (dp) closedir(dp);
116                 dp = NULL;
117
118                 if(find)
119                 {
120                         *ignore = TRUE;
121                         break;
122                 }
123                 else
124                 {
125                         /*If root path, Stop Scanning*/
126                         if((storage_type == MEDIA_SVC_STORAGE_INTERNAL) && (strcmp(search_path, MEDIA_ROOT_PATH_INTERNAL) == 0))
127                         {
128                                 //media_content_debug("Internal root path. Stop Scanning. Not found Ignore information");
129                                 break;
130                         }
131                         else if((storage_type == MEDIA_SVC_STORAGE_EXTERNAL) && (strcmp(search_path, MEDIA_ROOT_PATH_SDCARD) == 0))
132                         {
133                                 //media_content_debug("Enternal root path. Stop Scanning. Not found Ignore information");
134                                 break;
135                         }
136
137                         leaf_path = strrchr(search_path, '/');
138                         if(leaf_path != NULL)
139                         {
140                                 int seek_len = leaf_path -search_path;
141                                 search_path[seek_len] = '\0';
142                                 //media_content_info("go to other dir [%s]", search_path);
143                         }
144                         else
145                         {
146                                 media_content_debug("Fail to find leaf path");
147                                 break;
148                         }
149                 }
150         }
151
152         return MEDIA_CONTENT_ERROR_NONE;
153 }