Update query-related code
[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, const char *storage_name, 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         if (storage_name != NULL)
68                 sql = sqlite3_mprintf("SELECT * FROM '%s' WHERE storage_uuid=%Q AND storage_name=%Q", MEDIA_SVC_DB_TABLE_STORAGE, storage_id, storage_name);
69         else
70                 sql = sqlite3_mprintf("SELECT * FROM '%s' WHERE storage_uuid=%Q", MEDIA_SVC_DB_TABLE_STORAGE, storage_id);
71
72         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
73
74         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
75
76         *storage_path = g_strdup((const char *)sqlite3_column_text(sql_stmt, 2));
77         *validity = sqlite3_column_int(sql_stmt, 6);
78
79         SQLITE3_FINALIZE(sql_stmt);
80
81         /*check storage media table*/
82         if (STRING_VALID(storage_id)) {
83                 int table_cnt = 0;
84
85                 /*Select list of storage*/
86                 sql = sqlite3_mprintf("SELECT COUNT(*) FROM SQLITE_MASTER WHERE type='table' and name='%q'", storage_id);
87                 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
88                 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
89
90                 table_cnt = sqlite3_column_int(sql_stmt, 0);
91                 SQLITE3_FINALIZE(sql_stmt);
92
93                 if (table_cnt > 0) {
94                         /*DO NOT THING*/
95                 } else {
96                         media_svc_error("media table not exist for storage [%s]", storage_id);
97                         /*make storage media table*/
98                         ret = _media_svc_create_media_table_with_id(storage_id, uid);
99                         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "create media table failed : %d", ret);
100                 }
101         }
102
103         return MS_MEDIA_ERR_NONE;
104 }
105
106 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)
107 {
108         int ret = MS_MEDIA_ERR_NONE;
109         char *sql = sqlite3_mprintf("INSERT INTO %s (storage_uuid, storage_name, storage_path, storage_type) values (%Q, %Q, %Q, %d); ",
110                                                 MEDIA_SVC_DB_TABLE_STORAGE, storage_id, storage_name, storage_path, storage_type);
111
112         ret = _media_svc_sql_query(sql, uid);
113         SQLITE3_SAFE_FREE(sql);
114
115         return ret;
116 }
117
118 int _media_svc_update_storage_path(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
119 {
120         int ret = MS_MEDIA_ERR_NONE;
121         char *sql = NULL;
122         char *old_storage_path = NULL;
123         int validity = 0;
124
125         /*Get old path*/
126         ret = _media_svc_check_storage(handle, storage_id, NULL, &old_storage_path, &validity, uid);
127         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
128
129         /*Storage table update*/
130         sql = sqlite3_mprintf("UPDATE '%s' SET storage_path=%Q WHERE storage_uuid=%Q;", MEDIA_SVC_DB_TABLE_STORAGE, path, storage_id);
131         ret = _media_svc_sql_query(sql, uid);
132         SQLITE3_SAFE_FREE(sql);
133         if (ret != MS_MEDIA_ERR_NONE) {
134                 G_SAFE_FREE(old_storage_path);
135                 return ret;
136         }
137
138         /*Folder table update*/
139         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);
140         ret = _media_svc_sql_query(sql, uid);
141         SQLITE3_SAFE_FREE(sql);
142         if (ret != MS_MEDIA_ERR_NONE) {
143                 G_SAFE_FREE(old_storage_path);
144                 return ret;
145         }
146
147         /*Media table update*/
148         sql = sqlite3_mprintf("UPDATE '%s' SET path=REPLACE(path, %Q, %Q);", storage_id, old_storage_path, path);
149         ret = _media_svc_sql_query(sql, uid);
150         SQLITE3_SAFE_FREE(sql);
151         G_SAFE_FREE(old_storage_path);
152         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
153
154         return ret;
155 }
156
157 int _media_svc_delete_storage(const char *storage_id, const char *storage_name, uid_t uid)
158 {
159         int ret = MS_MEDIA_ERR_NONE;
160         char *sql = NULL;
161
162         if (storage_name != NULL)
163                         sql = sqlite3_mprintf("DELETE FROM '%s' WHERE storage_uuid=%Q AND storage_name=%Q;", MEDIA_SVC_DB_TABLE_STORAGE, storage_id, storage_name);
164         else if (storage_id != NULL)
165                 sql = sqlite3_mprintf("DELETE FROM '%s' WHERE storage_uuid=%Q;", MEDIA_SVC_DB_TABLE_STORAGE, storage_id);
166
167         ret = _media_svc_sql_query(sql, uid);
168         SQLITE3_SAFE_FREE(sql);
169
170         return ret;
171 }
172
173 int _media_svc_update_storage_validity(const char *storage_id, int validity, uid_t uid)
174 {
175         int ret = MS_MEDIA_ERR_NONE;
176         char *sql = NULL;
177
178         if (storage_id == NULL)
179                 sql = sqlite3_mprintf("UPDATE '%s' SET validity=%d WHERE storage_uuid != 'media';", MEDIA_SVC_DB_TABLE_STORAGE, validity);
180         else
181                 sql = sqlite3_mprintf("UPDATE '%s' SET validity=%d WHERE storage_uuid=%Q;", MEDIA_SVC_DB_TABLE_STORAGE, validity, storage_id);
182
183         ret = _media_svc_sql_query(sql, uid);
184         SQLITE3_SAFE_FREE(sql);
185
186         return ret;
187 }
188
189 int _media_svc_get_storage_uuid(sqlite3 *handle, const char *path, char *storage_id, uid_t uid)
190 {
191         int ret = MS_MEDIA_ERR_NONE;
192         sqlite3_stmt *sql_stmt = NULL;
193         char *sql = NULL;
194         char *storage_path = NULL;
195         char *remain_path = NULL;
196         int remain_len = 0;
197         char *internal_path = NULL;
198
199         ret = ms_user_get_internal_root_path(uid, &internal_path);
200         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Fail to get root path");
201
202         if (STRING_VALID(internal_path) && strncmp(path, internal_path, strlen(internal_path)) == 0) {
203                 SAFE_STRLCPY(storage_id, MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_UUID_SIZE+1);
204                 SAFE_FREE(internal_path);
205                 return MS_MEDIA_ERR_NONE;
206         }
207
208         SAFE_FREE(internal_path);
209
210         remain_path = strstr(path + (STRING_VALID(MEDIA_ROOT_PATH_USB) ? strlen(MEDIA_ROOT_PATH_USB) : 0) + 1, "/");
211         if (remain_path != NULL)
212                 remain_len = strlen(remain_path);
213
214         storage_path = strndup(path, strlen(path) - remain_len);
215
216         sql = sqlite3_mprintf("SELECT storage_uuid FROM '%s' WHERE validity=1 AND storage_path = '%s'", MEDIA_SVC_DB_TABLE_STORAGE, storage_path);
217
218         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
219         SAFE_FREE(storage_path);
220         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
221
222         if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 0)))
223                 SAFE_STRLCPY(storage_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE+1);
224
225         SQLITE3_FINALIZE(sql_stmt);
226
227         if (!STRING_VALID(storage_id)) {
228                 media_svc_error("Not found valid storage id [%s]", path);
229                 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
230         }
231
232         return ret;
233 }
234
235 int _media_svc_get_storage_type(sqlite3 *handle, const char *storage_id, media_svc_storage_type_e *storage_type)
236 {
237         int ret = MS_MEDIA_ERR_NONE;
238         sqlite3_stmt *sql_stmt = NULL;
239         char *sql = NULL;
240
241         if (!STRING_VALID(storage_id)) {
242                 media_svc_error("Invalid storage_id");
243                 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
244         }
245
246         sql = sqlite3_mprintf("SELECT storage_type FROM '%s' WHERE storage_uuid=%Q", MEDIA_SVC_DB_TABLE_STORAGE, storage_id);
247
248         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
249
250         if (ret != MS_MEDIA_ERR_NONE) {
251                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
252                         media_svc_debug("there is no storage.");
253                 else
254                         media_svc_error("error when _media_svc_get_storage_type. err = [%d]", ret);
255
256                 return ret;
257         }
258
259         *storage_type = sqlite3_column_int(sql_stmt, 0);
260
261         SQLITE3_FINALIZE(sql_stmt);
262
263         return ret;
264 }
265
266 int _media_svc_get_storage_path(sqlite3 *handle, const char *storage_id, char **storage_path)
267 {
268         int ret = MS_MEDIA_ERR_NONE;
269         sqlite3_stmt *sql_stmt = NULL;
270         char *sql = NULL;
271
272         if (!STRING_VALID(storage_id)) {
273                 media_svc_error("Invalid storage_id");
274                 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
275         }
276
277         sql = sqlite3_mprintf("SELECT storage_path FROM '%s' WHERE (storage_uuid=%Q AND validity=1)", MEDIA_SVC_DB_TABLE_STORAGE, storage_id);
278
279         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
280
281         if (ret != MS_MEDIA_ERR_NONE) {
282                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
283                         media_svc_debug("there is no storage.");
284                 else
285                         media_svc_error("error when _media_svc_get_storage_type. err = [%d]", ret);
286
287                 return ret;
288         }
289
290         *storage_path = g_strdup((char *)sqlite3_column_text(sql_stmt, 0));
291
292         SQLITE3_FINALIZE(sql_stmt);
293
294         return ret;
295 }
296
297 int _media_svc_get_storage_scan_status(sqlite3 *handle, const char *storage_id, media_svc_scan_status_type_e *scan_status)
298 {
299         int ret = MS_MEDIA_ERR_NONE;
300         sqlite3_stmt *sql_stmt = NULL;
301         char *sql = NULL;
302
303         if (!STRING_VALID(storage_id)) {
304                 media_svc_error("Invalid storage_id");
305                 return MS_MEDIA_ERR_INVALID_PARAMETER;
306         }
307
308         sql = sqlite3_mprintf("SELECT scan_status FROM '%s' WHERE (storage_uuid=%Q AND validity=1)", MEDIA_SVC_DB_TABLE_STORAGE, storage_id);
309
310         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
311         if (ret != MS_MEDIA_ERR_NONE) {
312                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
313                         media_svc_debug("there is no storage.");
314                 else
315                         media_svc_error("error when _media_svc_get_storage_scan_status. err = [%d]", ret);
316
317                 return ret;
318         }
319
320         *scan_status = sqlite3_column_int(sql_stmt, 0);
321
322         SQLITE3_FINALIZE(sql_stmt);
323
324         return ret;
325 }
326
327 int _media_svc_set_storage_scan_status(const char *storage_id, media_svc_scan_status_type_e scan_status, uid_t uid)
328 {
329         int ret = MS_MEDIA_ERR_NONE;
330         char *sql = NULL;
331
332         if (storage_id == NULL)
333                 sql = sqlite3_mprintf("UPDATE '%s' SET scan_status=%d WHERE storage_uuid != 'media';", MEDIA_SVC_DB_TABLE_STORAGE, scan_status);
334         else
335                 sql = sqlite3_mprintf("UPDATE '%s' SET scan_status=%d WHERE storage_uuid=%Q;", MEDIA_SVC_DB_TABLE_STORAGE, scan_status, storage_id);
336
337         ret = _media_svc_sql_query(sql, uid);
338         SQLITE3_SAFE_FREE(sql);
339
340         return ret;
341 }
342
343 static int __media_svc_count_all_storage(sqlite3 *handle, int *count)
344 {
345         int ret = MS_MEDIA_ERR_NONE;
346         sqlite3_stmt *sql_stmt = NULL;
347         char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE validity = 1", MEDIA_SVC_DB_TABLE_STORAGE);
348
349         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
350         if (ret != MS_MEDIA_ERR_NONE) {
351                 media_svc_error("error when _media_svc_sql_prepare_to_step. err = [%d]", ret);
352                 return ret;
353         }
354
355         *count = sqlite3_column_int(sql_stmt, 0);
356
357         SQLITE3_FINALIZE(sql_stmt);
358
359         return MS_MEDIA_ERR_NONE;
360 }
361
362 int _media_svc_get_all_storage(sqlite3 *handle, char ***storage_list, char ***storage_id_list, int **scan_status_list, int *count)
363 {
364         int ret = MS_MEDIA_ERR_NONE;
365         int idx = 0;
366         sqlite3_stmt *sql_stmt = NULL;
367         char *sql = NULL;
368         int cnt = 0;
369
370         ret = __media_svc_count_all_storage(handle, &cnt);
371         if (ret != MS_MEDIA_ERR_NONE) {
372                 media_svc_error("error when __media_svc_count_all_folders. err = [%d]", ret);
373                 return ret;
374         }
375
376         if (cnt > 0) {
377                 sql = sqlite3_mprintf("SELECT storage_path, storage_uuid, scan_status FROM '%s' WHERE validity = 1", MEDIA_SVC_DB_TABLE_STORAGE);
378         } else {
379                 *storage_list = NULL;
380                 *scan_status_list = NULL;
381                 return MS_MEDIA_ERR_NONE;
382         }
383
384         *storage_list = malloc(sizeof(char *) * cnt);
385         *storage_id_list = malloc(sizeof(char *) * cnt);
386         *scan_status_list = malloc(sizeof(int) * cnt);
387         if (*storage_list == NULL || *storage_id_list == NULL || *scan_status_list == NULL) {
388                 media_svc_error("Allocation failed");
389                 SAFE_FREE(*storage_list);
390                 SAFE_FREE(*storage_id_list);
391                 SAFE_FREE(*scan_status_list);
392                 SQLITE3_SAFE_FREE(sql);
393                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
394         }
395
396         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
397         if (ret != MS_MEDIA_ERR_NONE) {
398                 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
399                 SAFE_FREE(*storage_list);
400                 SAFE_FREE(*storage_id_list);
401                 SAFE_FREE(*scan_status_list);
402                 return ret;
403         }
404
405         media_svc_debug("QEURY OK");
406
407         while (1) {
408                 (*storage_list)[idx] = g_strdup((char *)sqlite3_column_text(sql_stmt, 0));
409                 (*storage_id_list)[idx] = g_strdup((char *)sqlite3_column_text(sql_stmt, 1));
410                 (*scan_status_list)[idx] = (int)sqlite3_column_int(sql_stmt, 2);
411                 if (sqlite3_step(sql_stmt) != SQLITE_ROW)
412                         break;
413                 idx++;
414         }
415
416         if (cnt == idx + 1) {
417                 *count = cnt;
418                 media_svc_debug("OK");
419         } else {
420                 /* free all data */
421                 int i = 0;
422                 for (i = 0; i < idx; i++) {
423                         SAFE_FREE((*storage_list)[i]);
424                         SAFE_FREE((*storage_id_list)[i]);
425                 }
426                 SAFE_FREE(*storage_list);
427                 SAFE_FREE(*storage_id_list);
428                 SAFE_FREE(*scan_status_list);
429                 *count = 0;
430                 ret = MS_MEDIA_ERR_INTERNAL;
431         }
432
433         SQLITE3_FINALIZE(sql_stmt);
434
435         return ret;
436 }