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