Add parameter for getting user path
[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(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", MEDIA_SVC_DB_TABLE_STORAGE, 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(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(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(sql, uid);
142         sqlite3_free(sql);
143         if (ret != MS_MEDIA_ERR_NONE) {
144                 G_SAFE_FREE(old_storage_path);
145                 return ret;
146         }
147
148         /*Folder table update*/
149         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);
150         ret = _media_svc_sql_query(sql, uid);
151         sqlite3_free(sql);
152         if (ret != MS_MEDIA_ERR_NONE) {
153                 G_SAFE_FREE(old_storage_path);
154                 return ret;
155         }
156
157         /*Media table update*/
158         sql = sqlite3_mprintf("UPDATE '%s' SET path=REPLACE(path, %Q, %Q);", storage_id, old_storage_path, path);
159         ret = _media_svc_sql_query(sql, uid);
160         sqlite3_free(sql);
161         G_SAFE_FREE(old_storage_path);
162         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
163
164         return ret;
165 }
166
167 int _media_svc_delete_storage(const char *storage_id, const char *storage_name, uid_t uid)
168 {
169         int ret = MS_MEDIA_ERR_NONE;
170         char *sql = NULL;
171
172         if (storage_name != NULL)
173                         sql = sqlite3_mprintf("DELETE FROM '%s' WHERE storage_uuid=%Q AND storage_name=%Q;", MEDIA_SVC_DB_TABLE_STORAGE, storage_id, storage_name);
174         else if (storage_id != NULL)
175                 sql = sqlite3_mprintf("DELETE FROM '%s' WHERE storage_uuid=%Q;", MEDIA_SVC_DB_TABLE_STORAGE, storage_id);
176
177         ret = _media_svc_sql_query(sql, uid);
178         sqlite3_free(sql);
179
180         return ret;
181 }
182
183 int _media_svc_update_storage_validity(const char *storage_id, int validity, uid_t uid)
184 {
185         int ret = MS_MEDIA_ERR_NONE;
186         char *sql = NULL;
187
188         if (storage_id == NULL)
189                 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);
190         else
191                 sql = sqlite3_mprintf("UPDATE '%s' SET validity=%d WHERE storage_uuid=%Q;", MEDIA_SVC_DB_TABLE_STORAGE, validity, storage_id);
192
193         ret = _media_svc_sql_query(sql, uid);
194         sqlite3_free(sql);
195
196         return ret;
197 }
198
199 int _media_svc_get_storage_uuid(sqlite3 *handle, const char *path, char *storage_id, uid_t uid)
200 {
201         int ret = MS_MEDIA_ERR_NONE;
202         sqlite3_stmt *sql_stmt = NULL;
203         char *sql = NULL;
204         char *storage_path = NULL;
205         char *remain_path = NULL;
206         int remain_len = 0;
207         char *internal_path = NULL;
208
209         internal_path = _media_svc_get_path(uid);
210
211         if (STRING_VALID(internal_path) && strncmp(path, internal_path, strlen(internal_path)) == 0) {
212                 _strncpy_safe(storage_id, MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_UUID_SIZE+1);
213                 SAFE_FREE(internal_path);
214                 return MS_MEDIA_ERR_NONE;
215         }
216
217         SAFE_FREE(internal_path);
218
219         remain_path = strstr(path + (STRING_VALID(MEDIA_ROOT_PATH_USB) ? strlen(MEDIA_ROOT_PATH_USB) : 0) + 1, "/");
220         if (remain_path != NULL)
221                 remain_len = strlen(remain_path);
222
223         storage_path = strndup(path, strlen(path) - remain_len);
224
225         sql = sqlite3_mprintf("SELECT storage_uuid FROM '%s' WHERE validity=1 AND storage_path = '%s'", MEDIA_SVC_DB_TABLE_STORAGE, storage_path);
226
227         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
228
229         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
230         if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 0)))
231                 _strncpy_safe(storage_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE+1);
232
233         SQLITE3_FINALIZE(sql_stmt);
234         SAFE_FREE(storage_path);
235
236         if (!STRING_VALID(storage_id)) {
237                 media_svc_error("Not found valid storage id [%s]", path);
238                 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
239         }
240
241         return ret;
242 }
243
244 int _media_svc_get_storage_type(sqlite3 *handle, const char *storage_id, media_svc_storage_type_e *storage_type)
245 {
246         int ret = MS_MEDIA_ERR_NONE;
247         sqlite3_stmt *sql_stmt = NULL;
248         char *sql = NULL;
249
250         if (!STRING_VALID(storage_id)) {
251                 media_svc_error("Invalid storage_idid");
252                 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
253         }
254
255         sql = sqlite3_mprintf("SELECT storage_type FROM '%s' WHERE storage_uuid=%Q", MEDIA_SVC_DB_TABLE_STORAGE, storage_id);
256
257         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
258
259         if (ret != MS_MEDIA_ERR_NONE) {
260                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
261                         media_svc_debug("there is no storage.");
262                 else
263                         media_svc_error("error when _media_svc_get_storage_type. err = [%d]", ret);
264
265                 return ret;
266         }
267
268         *storage_type = sqlite3_column_int(sql_stmt, 0);
269
270         SQLITE3_FINALIZE(sql_stmt);
271
272         return ret;
273 }
274
275 int _media_svc_get_storage_path(sqlite3 *handle, const char *storage_id, char **storage_path)
276 {
277         int ret = MS_MEDIA_ERR_NONE;
278         sqlite3_stmt *sql_stmt = NULL;
279         char *sql = NULL;
280
281         if (!STRING_VALID(storage_id)) {
282                 media_svc_error("Invalid storage_idid");
283                 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
284         }
285
286         sql = sqlite3_mprintf("SELECT storage_path FROM '%s' WHERE (storage_uuid=%Q AND validity=1)", MEDIA_SVC_DB_TABLE_STORAGE, storage_id);
287
288         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
289
290         if (ret != MS_MEDIA_ERR_NONE) {
291                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
292                         media_svc_debug("there is no storage.");
293                 else
294                         media_svc_error("error when _media_svc_get_storage_type. err = [%d]", ret);
295
296                 return ret;
297         }
298
299         *storage_path = g_strdup((char *)sqlite3_column_text(sql_stmt, 0));
300
301         SQLITE3_FINALIZE(sql_stmt);
302
303         return ret;
304 }
305
306 int _media_svc_get_storage_scan_status(sqlite3 *handle, const char *storage_id, media_svc_scan_status_type_e *scan_status)
307 {
308         int ret = MS_MEDIA_ERR_NONE;
309         sqlite3_stmt *sql_stmt = NULL;
310         char *sql = NULL;
311
312         if (!STRING_VALID(storage_id)) {
313                 media_svc_error("Invalid storage_id");
314                 return MS_MEDIA_ERR_INVALID_PARAMETER;
315         }
316
317         sql = sqlite3_mprintf("SELECT scan_status FROM '%s' WHERE (storage_uuid=%Q AND validity=1)", MEDIA_SVC_DB_TABLE_STORAGE, storage_id);
318
319         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
320         if (ret != MS_MEDIA_ERR_NONE) {
321                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
322                         media_svc_debug("there is no storage.");
323                 else
324                         media_svc_error("error when _media_svc_get_storage_scan_status. err = [%d]", ret);
325
326                 return ret;
327         }
328
329         *scan_status = sqlite3_column_int(sql_stmt, 0);
330
331         SQLITE3_FINALIZE(sql_stmt);
332
333         return ret;
334 }
335
336 int _media_svc_set_storage_scan_status(const char *storage_id, media_svc_scan_status_type_e scan_status, uid_t uid)
337 {
338         int ret = MS_MEDIA_ERR_NONE;
339         char *sql = NULL;
340
341         if (storage_id == NULL)
342                 sql = sqlite3_mprintf("UPDATE '%s' SET scan_status=%d WHERE storage_uuid != 'media';", MEDIA_SVC_DB_TABLE_STORAGE, scan_status);
343         else
344                 sql = sqlite3_mprintf("UPDATE '%s' SET scan_status=%d WHERE storage_uuid=%Q;", MEDIA_SVC_DB_TABLE_STORAGE, scan_status, storage_id);
345
346         ret = _media_svc_sql_query(sql, uid);
347         sqlite3_free(sql);
348
349         return ret;
350 }
351
352 static int __media_svc_count_all_storage(sqlite3 *handle, int *count)
353 {
354         int ret = MS_MEDIA_ERR_NONE;
355         sqlite3_stmt *sql_stmt = NULL;
356         char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE validity = 1", MEDIA_SVC_DB_TABLE_STORAGE);
357
358         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
359         if (ret != MS_MEDIA_ERR_NONE) {
360                 media_svc_error("error when _media_svc_sql_prepare_to_step. err = [%d]", ret);
361                 return ret;
362         }
363
364         *count = sqlite3_column_int(sql_stmt, 0);
365
366         SQLITE3_FINALIZE(sql_stmt);
367
368         return MS_MEDIA_ERR_NONE;
369 }
370
371 int _media_svc_get_all_storage(sqlite3 *handle, char ***storage_list, char ***storage_id_list, int **scan_status_list, int *count)
372 {
373         int ret = MS_MEDIA_ERR_NONE;
374         int idx = 0;
375         sqlite3_stmt *sql_stmt = NULL;
376         char *sql = NULL;
377         int cnt = 0;
378
379         ret = __media_svc_count_all_storage(handle, &cnt);
380         if (ret != MS_MEDIA_ERR_NONE) {
381                 media_svc_error("error when __media_svc_count_all_folders. err = [%d]", ret);
382                 return ret;
383         }
384
385         if (cnt > 0) {
386                 sql = sqlite3_mprintf("SELECT storage_path, storage_uuid, scan_status FROM '%s' WHERE validity = 1", MEDIA_SVC_DB_TABLE_STORAGE);
387         } else {
388                 *storage_list = NULL;
389                 *scan_status_list = NULL;
390                 return MS_MEDIA_ERR_NONE;
391         }
392
393         *storage_list = malloc(sizeof(char *) * cnt);
394         *storage_id_list = malloc(sizeof(char *) * cnt);
395         *scan_status_list = malloc(sizeof(int) * cnt);
396
397         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
398         if (ret != MS_MEDIA_ERR_NONE) {
399                 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
400                 SAFE_FREE(*storage_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 }