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