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