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