Modified query about check storage
[platform/core/multimedia/libmedia-service.git] / src / common / media-svc-storage.c
1 /*
2  * libmedia-service
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include "media-util-err.h"
23 #include "media-svc-debug.h"
24 #include "media-svc-env.h"
25 #include "media-svc-db-utils.h"
26 #include "media-svc-util.h"
27 #include "media-svc-storage.h"
28
29 int _media_svc_get_mmc_info(MediaSvcHandle *handle, char **storage_name, char **storage_path, int *validity, bool *info_exist)
30 {
31         int ret = MS_MEDIA_ERR_NONE;
32         sqlite3_stmt *sql_stmt = NULL;
33         char *sql = NULL;
34
35         sql = sqlite3_mprintf("SELECT storage_name, storage_path, validity FROM '%s' WHERE storage_uuid=%Q", MEDIA_SVC_DB_TABLE_STORAGE, MEDIA_SVC_DB_TABLE_MEDIA);
36
37         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
38         if (ret != MS_MEDIA_ERR_NONE) {
39                 *storage_name = NULL;
40                 *storage_path = NULL;
41                 *validity = 0;
42                 *info_exist = FALSE;
43
44                 return ret;
45         }
46
47         *storage_name = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
48         *storage_path = g_strdup((const char *)sqlite3_column_text(sql_stmt, 1));
49         *validity = sqlite3_column_int(sql_stmt, 2);
50
51         *info_exist = TRUE;
52
53         SQLITE3_FINALIZE(sql_stmt);
54
55         return MS_MEDIA_ERR_NONE;
56 }
57
58 int _media_svc_check_storage(sqlite3 *handle, const char *storage_id, char **storage_path, int *validity, uid_t uid)
59 {
60         int ret = MS_MEDIA_ERR_NONE;
61         sqlite3_stmt *sql_stmt = NULL;
62         char *sql = NULL;
63
64         *storage_path = NULL;
65         *validity = 0;
66
67         sql = sqlite3_mprintf("SELECT storage_path, validity FROM '%s' WHERE storage_uuid=%Q", MEDIA_SVC_DB_TABLE_STORAGE, storage_id);
68         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
69         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
70
71         *storage_path = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
72         *validity = sqlite3_column_int(sql_stmt, 1);
73
74         SQLITE3_FINALIZE(sql_stmt);
75
76         /*check storage media table*/
77         if (STRING_VALID(storage_id)) {
78                 int table_cnt = 0;
79
80                 /*Select list of storage*/
81                 sql = sqlite3_mprintf("SELECT COUNT(*) FROM SQLITE_MASTER WHERE type='table' and name='%q'", storage_id);
82                 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
83                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
84
85                 table_cnt = sqlite3_column_int(sql_stmt, 0);
86                 SQLITE3_FINALIZE(sql_stmt);
87
88                 if (table_cnt > 0) {
89                         /*DO NOT THING*/
90                 } else {
91                         media_svc_error("media table not exist for storage [%s]", storage_id);
92                         /*make storage media table*/
93                         ret = _media_svc_create_media_table_with_id(storage_id, uid);
94                         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "create media table failed : %d", ret);
95                 }
96         }
97
98         return MS_MEDIA_ERR_NONE;
99 }
100
101 int _media_svc_append_storage(const char *storage_id, const char *storage_name, const char *storage_path, media_svc_storage_type_e storage_type, uid_t uid)
102 {
103         int ret = MS_MEDIA_ERR_NONE;
104         char *sql = sqlite3_mprintf("INSERT INTO %s (storage_uuid, storage_name, storage_path, storage_type) values (%Q, %Q, %Q, %d); ",
105                                                 MEDIA_SVC_DB_TABLE_STORAGE, storage_id, storage_name, storage_path, storage_type);
106
107         ret = _media_svc_sql_query(sql, uid);
108         SQLITE3_SAFE_FREE(sql);
109
110         return ret;
111 }
112
113 int _media_svc_update_storage_path(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
114 {
115         int ret = MS_MEDIA_ERR_NONE;
116         char *sql = NULL;
117         char *old_storage_path = NULL;
118         int validity = 0;
119
120         /*Get old path*/
121         ret = _media_svc_check_storage(handle, storage_id, &old_storage_path, &validity, uid);
122         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
123
124         /*Storage table update*/
125         sql = sqlite3_mprintf("UPDATE '%s' SET storage_path=%Q WHERE storage_uuid=%Q;", MEDIA_SVC_DB_TABLE_STORAGE, path, storage_id);
126         ret = _media_svc_sql_query(sql, uid);
127         SQLITE3_SAFE_FREE(sql);
128         if (ret != MS_MEDIA_ERR_NONE) {
129                 G_SAFE_FREE(old_storage_path);
130                 return ret;
131         }
132
133         /*Folder table update*/
134         sql = sqlite3_mprintf("UPDATE '%s' SET path=REPLACE(path, %Q, %Q) WHERE storage_uuid=%Q;", MEDIA_SVC_DB_TABLE_FOLDER, old_storage_path, path, storage_id);
135         ret = _media_svc_sql_query(sql, uid);
136         SQLITE3_SAFE_FREE(sql);
137         if (ret != MS_MEDIA_ERR_NONE) {
138                 G_SAFE_FREE(old_storage_path);
139                 return ret;
140         }
141
142         /*Media table update*/
143         sql = sqlite3_mprintf("UPDATE '%s' SET path=REPLACE(path, %Q, %Q);", storage_id, old_storage_path, path);
144         ret = _media_svc_sql_query(sql, uid);
145         SQLITE3_SAFE_FREE(sql);
146         G_SAFE_FREE(old_storage_path);
147         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
148
149         return ret;
150 }
151
152 int _media_svc_delete_storage(const char *storage_id, uid_t uid)
153 {
154         int ret = MS_MEDIA_ERR_NONE;
155         char *sql = NULL;
156
157         if (storage_id != NULL)
158                 sql = sqlite3_mprintf("DELETE FROM '%s' WHERE storage_uuid=%Q;", MEDIA_SVC_DB_TABLE_STORAGE, storage_id);
159
160         ret = _media_svc_sql_query(sql, uid);
161         SQLITE3_SAFE_FREE(sql);
162
163         return ret;
164 }
165
166 int _media_svc_update_storage_validity(const char *storage_id, int validity, uid_t uid)
167 {
168         int ret = MS_MEDIA_ERR_NONE;
169         char *sql = NULL;
170
171         if (storage_id == NULL)
172                 sql = sqlite3_mprintf("UPDATE '%s' SET validity=%d WHERE storage_uuid != 'media';", MEDIA_SVC_DB_TABLE_STORAGE, validity);
173         else
174                 sql = sqlite3_mprintf("UPDATE '%s' SET validity=%d WHERE storage_uuid=%Q;", MEDIA_SVC_DB_TABLE_STORAGE, validity, storage_id);
175
176         ret = _media_svc_sql_query(sql, uid);
177         SQLITE3_SAFE_FREE(sql);
178
179         return ret;
180 }
181
182 int _media_svc_get_storage_uuid(sqlite3 *handle, const char *path, char *storage_id, uid_t uid)
183 {
184         int ret = MS_MEDIA_ERR_NONE;
185         sqlite3_stmt *sql_stmt = NULL;
186         char *sql = NULL;
187         char *storage_path = NULL;
188         char *remain_path = NULL;
189         int remain_len = 0;
190         char *internal_path = NULL;
191
192         ret = ms_user_get_internal_root_path(uid, &internal_path);
193         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Fail to get root path");
194
195         if (STRING_VALID(internal_path) && strncmp(path, internal_path, strlen(internal_path)) == 0) {
196                 SAFE_STRLCPY(storage_id, MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_UUID_SIZE+1);
197                 SAFE_FREE(internal_path);
198                 return MS_MEDIA_ERR_NONE;
199         }
200
201         SAFE_FREE(internal_path);
202
203         remain_path = strstr(path + (STRING_VALID(MEDIA_ROOT_PATH_USB) ? strlen(MEDIA_ROOT_PATH_USB) : 0) + 1, "/");
204         if (remain_path != NULL)
205                 remain_len = strlen(remain_path);
206
207         storage_path = strndup(path, strlen(path) - remain_len);
208
209         sql = sqlite3_mprintf("SELECT storage_uuid FROM '%s' WHERE validity=1 AND storage_path = '%s'", MEDIA_SVC_DB_TABLE_STORAGE, storage_path);
210
211         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
212         SAFE_FREE(storage_path);
213         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
214
215         if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 0)))
216                 SAFE_STRLCPY(storage_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE+1);
217
218         SQLITE3_FINALIZE(sql_stmt);
219
220         if (!STRING_VALID(storage_id)) {
221                 media_svc_error("Not found valid storage id [%s]", path);
222                 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
223         }
224
225         return ret;
226 }
227
228 int _media_svc_get_storage_type(sqlite3 *handle, const char *storage_id, media_svc_storage_type_e *storage_type)
229 {
230         int ret = MS_MEDIA_ERR_NONE;
231         sqlite3_stmt *sql_stmt = NULL;
232         char *sql = NULL;
233
234         if (!STRING_VALID(storage_id)) {
235                 media_svc_error("Invalid storage_id");
236                 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
237         }
238
239         sql = sqlite3_mprintf("SELECT storage_type FROM '%s' WHERE storage_uuid=%Q", MEDIA_SVC_DB_TABLE_STORAGE, storage_id);
240
241         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
242
243         if (ret != MS_MEDIA_ERR_NONE) {
244                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
245                         media_svc_debug("there is no storage.");
246                 else
247                         media_svc_error("error when _media_svc_get_storage_type. err = [%d]", ret);
248
249                 return ret;
250         }
251
252         *storage_type = sqlite3_column_int(sql_stmt, 0);
253
254         SQLITE3_FINALIZE(sql_stmt);
255
256         return ret;
257 }
258
259 int _media_svc_get_storage_path(sqlite3 *handle, const char *storage_id, char **storage_path)
260 {
261         int ret = MS_MEDIA_ERR_NONE;
262         sqlite3_stmt *sql_stmt = NULL;
263         char *sql = NULL;
264
265         if (!STRING_VALID(storage_id)) {
266                 media_svc_error("Invalid storage_id");
267                 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
268         }
269
270         sql = sqlite3_mprintf("SELECT storage_path FROM '%s' WHERE (storage_uuid=%Q AND validity=1)", MEDIA_SVC_DB_TABLE_STORAGE, storage_id);
271
272         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
273
274         if (ret != MS_MEDIA_ERR_NONE) {
275                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
276                         media_svc_debug("there is no storage.");
277                 else
278                         media_svc_error("error when _media_svc_get_storage_type. err = [%d]", ret);
279
280                 return ret;
281         }
282
283         *storage_path = g_strdup((char *)sqlite3_column_text(sql_stmt, 0));
284
285         SQLITE3_FINALIZE(sql_stmt);
286
287         return ret;
288 }
289
290 int _media_svc_get_storage_scan_status(sqlite3 *handle, const char *storage_id, media_svc_scan_status_type_e *scan_status)
291 {
292         int ret = MS_MEDIA_ERR_NONE;
293         sqlite3_stmt *sql_stmt = NULL;
294         char *sql = NULL;
295
296         if (!STRING_VALID(storage_id)) {
297                 media_svc_error("Invalid storage_id");
298                 return MS_MEDIA_ERR_INVALID_PARAMETER;
299         }
300
301         sql = sqlite3_mprintf("SELECT scan_status FROM '%s' WHERE (storage_uuid=%Q AND validity=1)", MEDIA_SVC_DB_TABLE_STORAGE, storage_id);
302
303         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
304         if (ret != MS_MEDIA_ERR_NONE) {
305                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
306                         media_svc_debug("there is no storage.");
307                 else
308                         media_svc_error("error when _media_svc_get_storage_scan_status. err = [%d]", ret);
309
310                 return ret;
311         }
312
313         *scan_status = sqlite3_column_int(sql_stmt, 0);
314
315         SQLITE3_FINALIZE(sql_stmt);
316
317         return ret;
318 }
319
320 int _media_svc_set_storage_scan_status(const char *storage_id, media_svc_scan_status_type_e scan_status, uid_t uid)
321 {
322         int ret = MS_MEDIA_ERR_NONE;
323         char *sql = NULL;
324
325         if (storage_id == NULL)
326                 sql = sqlite3_mprintf("UPDATE '%s' SET scan_status=%d WHERE storage_uuid != 'media';", MEDIA_SVC_DB_TABLE_STORAGE, scan_status);
327         else
328                 sql = sqlite3_mprintf("UPDATE '%s' SET scan_status=%d WHERE storage_uuid=%Q;", MEDIA_SVC_DB_TABLE_STORAGE, scan_status, storage_id);
329
330         ret = _media_svc_sql_query(sql, uid);
331         SQLITE3_SAFE_FREE(sql);
332
333         return ret;
334 }
335
336 static int __media_svc_count_all_storage(sqlite3 *handle, int *count)
337 {
338         int ret = MS_MEDIA_ERR_NONE;
339         sqlite3_stmt *sql_stmt = NULL;
340         char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE validity = 1", MEDIA_SVC_DB_TABLE_STORAGE);
341
342         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
343         if (ret != MS_MEDIA_ERR_NONE) {
344                 media_svc_error("error when _media_svc_sql_prepare_to_step. err = [%d]", ret);
345                 return ret;
346         }
347
348         *count = sqlite3_column_int(sql_stmt, 0);
349
350         SQLITE3_FINALIZE(sql_stmt);
351
352         return MS_MEDIA_ERR_NONE;
353 }
354
355 int _media_svc_get_all_storage(sqlite3 *handle, char ***storage_list, char ***storage_id_list, int **scan_status_list, int *count)
356 {
357         int ret = MS_MEDIA_ERR_NONE;
358         int idx = 0;
359         sqlite3_stmt *sql_stmt = NULL;
360         char *sql = NULL;
361         int cnt = 0;
362
363         ret = __media_svc_count_all_storage(handle, &cnt);
364         if (ret != MS_MEDIA_ERR_NONE) {
365                 media_svc_error("error when __media_svc_count_all_folders. err = [%d]", ret);
366                 return ret;
367         }
368
369         if (cnt > 0) {
370                 sql = sqlite3_mprintf("SELECT storage_path, storage_uuid, scan_status FROM '%s' WHERE validity = 1", MEDIA_SVC_DB_TABLE_STORAGE);
371         } else {
372                 *storage_list = NULL;
373                 *scan_status_list = NULL;
374                 return MS_MEDIA_ERR_NONE;
375         }
376
377         *storage_list = malloc(sizeof(char *) * cnt);
378         *storage_id_list = malloc(sizeof(char *) * cnt);
379         *scan_status_list = malloc(sizeof(int) * cnt);
380         if (*storage_list == NULL || *storage_id_list == NULL || *scan_status_list == NULL) {
381                 media_svc_error("Allocation failed");
382                 SAFE_FREE(*storage_list);
383                 SAFE_FREE(*storage_id_list);
384                 SAFE_FREE(*scan_status_list);
385                 SQLITE3_SAFE_FREE(sql);
386                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
387         }
388
389         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
390         if (ret != MS_MEDIA_ERR_NONE) {
391                 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
392                 SAFE_FREE(*storage_list);
393                 SAFE_FREE(*storage_id_list);
394                 SAFE_FREE(*scan_status_list);
395                 return ret;
396         }
397
398         media_svc_debug("QEURY OK");
399
400         while (1) {
401                 (*storage_list)[idx] = g_strdup((char *)sqlite3_column_text(sql_stmt, 0));
402                 (*storage_id_list)[idx] = g_strdup((char *)sqlite3_column_text(sql_stmt, 1));
403                 (*scan_status_list)[idx] = (int)sqlite3_column_int(sql_stmt, 2);
404                 if (sqlite3_step(sql_stmt) != SQLITE_ROW)
405                         break;
406                 idx++;
407         }
408
409         if (cnt == idx + 1) {
410                 *count = cnt;
411                 media_svc_debug("OK");
412         } else {
413                 /* free all data */
414                 int i = 0;
415                 for (i = 0; i < idx; i++) {
416                         SAFE_FREE((*storage_list)[i]);
417                         SAFE_FREE((*storage_id_list)[i]);
418                 }
419                 SAFE_FREE(*storage_list);
420                 SAFE_FREE(*storage_id_list);
421                 SAFE_FREE(*scan_status_list);
422                 *count = 0;
423                 ret = MS_MEDIA_ERR_INTERNAL;
424         }
425
426         SQLITE3_FINALIZE(sql_stmt);
427
428         return ret;
429 }