fix assertion failed: hash_table->ref_count >0, add lock to db query
[platform/core/api/media-content.git] / src / media_storage.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 <media_info_private.h>
19
20 int media_storage_get_storage_info_from_db(const char *storage_id, media_storage_h *storage)
21 {
22         int ret = MEDIA_CONTENT_ERROR_NONE;
23         char select_query[DEFAULT_QUERY_SIZE] = {0, };
24         sqlite3_stmt *stmt = NULL;
25
26         if (!STRING_VALID(storage_id) || (storage == NULL)) {
27                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
28                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
29         }
30
31         memset(select_query, 0x00, sizeof(select_query));
32         snprintf(select_query, sizeof(select_query), SELECT_STORAGE_INFO_FROM_STORAGE, storage_id);
33
34         ret = _content_query_prepare(&stmt, select_query, NULL, NULL);
35         media_content_retv_if(ret != MEDIA_CONTENT_ERROR_NONE, ret);
36
37         while (sqlite3_step(stmt) == SQLITE_ROW) {
38                 media_storage_s *_storage = (media_storage_s*)calloc(1, sizeof(media_storage_s));
39
40                 if (_storage == NULL) {
41                         media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
42                         SQLITE3_FINALIZE(stmt);
43                         return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
44                 }
45
46                 _storage->storage_id = g_strdup((const char *)sqlite3_column_text(stmt, 0));
47                 _storage->storage_path = g_strdup((const char *)sqlite3_column_text(stmt, 1));
48                 _storage->storage_type = (int)sqlite3_column_int(stmt, 2);
49
50                 *storage = (media_storage_h)_storage;
51         }
52
53         SQLITE3_FINALIZE(stmt);
54
55         return ret;
56 }
57
58 int media_storage_get_storage_count_from_db(filter_h filter, int *storage_count)
59 {
60         int ret = MEDIA_CONTENT_ERROR_NONE;
61
62         if (storage_count) {
63                 ret = _media_db_get_group_count(filter, MEDIA_GROUP_STORAGE, storage_count);
64         } else {
65                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
66                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
67         }
68
69         return ret;
70 }
71
72 int media_storage_foreach_storage_from_db(filter_h filter, media_storage_cb callback, void *user_data)
73 {
74         int ret = MEDIA_CONTENT_ERROR_NONE;
75
76         if (callback != NULL) {
77 #ifdef _USE_TVPD_MODE
78         g_mutex_lock(_content_get_db_mutex());
79 #endif
80
81                 ret = _media_db_get_storage(filter, callback, user_data);
82
83 #ifdef _USE_TVPD_MODE
84         g_mutex_unlock(_content_get_db_mutex());
85 #endif
86         } else {
87                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
88                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
89         }
90
91         return ret;
92 }
93
94 int media_storage_get_media_count_from_db(const char *storage_id, filter_h filter, int *media_count)
95 {
96         int ret = MEDIA_CONTENT_ERROR_NONE;
97
98         if (STRING_VALID(storage_id) && media_count) {
99                 ret = _media_db_get_group_item_count(storage_id, filter, MEDIA_GROUP_STORAGE, media_count);
100         } else {
101                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
102                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
103         }
104
105         return ret;
106 }
107
108 int media_storage_foreach_media_from_db(const char *storage_id, filter_h filter, media_info_cb callback, void *user_data)
109 {
110         int ret = MEDIA_CONTENT_ERROR_NONE;
111
112         if ((callback != NULL) && STRING_VALID(storage_id)) {
113                 ret = _media_db_get_group_item(storage_id, filter, callback, user_data, MEDIA_GROUP_STORAGE);
114         } else {
115                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
116                 return MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
117         }
118
119         return ret;
120 }
121
122 int media_storage_destroy(media_storage_h storage)
123 {
124         int ret = MEDIA_CONTENT_ERROR_NONE;
125         media_storage_s *_storage = (media_storage_s*)storage;
126         if (_storage) {
127                 SAFE_FREE(_storage->storage_id);
128                 SAFE_FREE(_storage->storage_path);
129                 SAFE_FREE(_storage);
130
131                 ret = MEDIA_CONTENT_ERROR_NONE;
132         } else {
133                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
134                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
135         }
136
137         return ret;
138 }
139
140 int media_storage_clone(media_storage_h *dst, media_storage_h src)
141 {
142         int ret = MEDIA_CONTENT_ERROR_NONE;
143         media_storage_s *_src = (media_storage_s*)src;
144
145         if (_src != NULL) {
146                 media_storage_s *_dst = (media_storage_s*)calloc(1, sizeof(media_storage_s));
147                 media_content_retvm_if(_dst == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
148
149                 if (STRING_VALID(_src->storage_id)) {
150                         _dst->storage_id = strdup(_src->storage_id);
151                         if (_dst->storage_id == NULL) {
152                                 media_storage_destroy((media_storage_h)_dst);
153                                 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
154                                 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
155                         }
156                 }
157
158                 if (STRING_VALID(_src->storage_path)) {
159                         _dst->storage_path = strdup(_src->storage_path);
160                         if (_dst->storage_path == NULL) {
161                                 media_content_error("OUT_OF_MEMORY(0x%08x)", MEDIA_CONTENT_ERROR_OUT_OF_MEMORY);
162                                 media_storage_destroy((media_storage_h)_dst);
163                                 return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
164                         }
165                 }
166
167                 _dst->storage_type = _src->storage_type;
168
169                 *dst = (media_storage_h)_dst;
170
171                 ret = MEDIA_CONTENT_ERROR_NONE;
172         } else {
173                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
174                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
175         }
176
177         return ret;
178 }
179
180 int media_storage_get_id(media_storage_h storage, char **storage_id)
181 {
182         int ret = MEDIA_CONTENT_ERROR_NONE;
183         media_storage_s *_storage = (media_storage_s*)storage;
184
185         if (_storage && storage_id) {
186                 if (STRING_VALID(_storage->storage_id)) {
187                         *storage_id = strdup(_storage->storage_id);
188                         media_content_retvm_if(*storage_id == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
189                 } else {
190                         *storage_id = NULL;
191                 }
192
193                 ret = MEDIA_CONTENT_ERROR_NONE;
194         } else {
195                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
196                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
197         }
198
199         return ret;
200 }
201
202 int media_storage_get_name(media_storage_h storage, char **storage_name)
203 {
204         int ret = MEDIA_CONTENT_ERROR_NONE;
205         media_content_warn("DEPRECATION WARNING: media_storage_get_name() is deprecated and will be removed from next release.");
206         media_storage_s *_storage = (media_storage_s*)storage;
207
208         if (_storage && storage_name) {
209                 *storage_name = NULL;
210         } else {
211                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
212                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
213         }
214
215         return ret;
216 }
217
218 int media_storage_get_path(media_storage_h storage, char **storage_path)
219 {
220         int ret = MEDIA_CONTENT_ERROR_NONE;
221         media_storage_s *_storage = (media_storage_s*)storage;
222
223         if (_storage && storage_path) {
224                 if (STRING_VALID(_storage->storage_path)) {
225                         *storage_path = strdup(_storage->storage_path);
226                         media_content_retvm_if(*storage_path == NULL, MEDIA_CONTENT_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
227                 } else {
228                         *storage_path = NULL;
229                 }
230
231                 ret = MEDIA_CONTENT_ERROR_NONE;
232         } else {
233                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
234                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
235         }
236
237         return ret;
238 }
239
240 int media_storage_get_type(media_storage_h storage, media_content_storage_e *storage_type)
241 {
242         int ret = MEDIA_CONTENT_ERROR_NONE;
243         media_storage_s *_storage = (media_storage_s*)storage;
244
245         if (_storage && storage_type) {
246                 *storage_type = _storage->storage_type;
247                 ret = MEDIA_CONTENT_ERROR_NONE;
248         } else {
249                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
250                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
251         }
252
253         return ret;
254 }
255
256 #ifdef _USE_TVPD_MODE
257 int media_storage_get_scan_status(const char *storage_uuid, media_storage_scan_status_e *scan_status)
258 {
259         int ret = MEDIA_CONTENT_ERROR_NONE;
260         media_svc_scan_status_type_e status = MEDIA_STORAGE_SCAN_NONE;
261
262         if (STRING_VALID(storage_uuid)) {
263                 ret = media_svc_get_storage_scan_status(_content_get_db_handle(), storage_uuid, &status);
264                 if (ret != MS_MEDIA_ERR_NONE) {
265                         media_content_error("media_svc_get_storage_scan_status failed");
266                         ret = _content_error_capi(MEDIA_CONTENT_TYPE, ret);
267                 } else {
268                         *scan_status = status;
269                 }
270         } else {
271                 media_content_error("INVALID_PARAMETER(0x%08x)", MEDIA_CONTENT_ERROR_INVALID_PARAMETER);
272                 ret = MEDIA_CONTENT_ERROR_INVALID_PARAMETER;
273         }
274
275         return ret;
276 }
277 #endif