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