4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
29 int _media_svc_get_mmc_info(MediaSvcHandle *handle, char **storage_name, char **storage_path, int *validity, bool *info_exist)
31 int ret = MS_MEDIA_ERR_NONE;
32 sqlite3_stmt *sql_stmt = NULL;
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);
37 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
38 if (ret != MS_MEDIA_ERR_NONE) {
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);
53 SQLITE3_FINALIZE(sql_stmt);
55 return MS_MEDIA_ERR_NONE;
58 int _media_svc_check_storage(sqlite3 *handle, const char *storage_id, char **storage_path, int *validity, uid_t uid)
60 int ret = MS_MEDIA_ERR_NONE;
61 sqlite3_stmt *sql_stmt = NULL;
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);
71 *storage_path = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
72 *validity = sqlite3_column_int(sql_stmt, 1);
74 SQLITE3_FINALIZE(sql_stmt);
76 /*check storage media table*/
77 if (STRING_VALID(storage_id)) {
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);
85 table_cnt = sqlite3_column_int(sql_stmt, 0);
86 SQLITE3_FINALIZE(sql_stmt);
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);
98 return MS_MEDIA_ERR_NONE;
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)
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);
107 ret = _media_svc_sql_query(sql, uid);
108 SQLITE3_SAFE_FREE(sql);
113 int _media_svc_update_storage_path(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
115 int ret = MS_MEDIA_ERR_NONE;
117 char *old_storage_path = NULL;
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);
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);
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);
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);
152 int _media_svc_delete_storage(const char *storage_id, uid_t uid)
154 int ret = MS_MEDIA_ERR_NONE;
157 if (storage_id != NULL)
158 sql = sqlite3_mprintf("DELETE FROM '%q' WHERE storage_uuid=%Q;", MEDIA_SVC_DB_TABLE_STORAGE, storage_id);
160 ret = _media_svc_sql_query(sql, uid);
161 SQLITE3_SAFE_FREE(sql);
166 int _media_svc_update_storage_validity(const char *storage_id, int validity, uid_t uid)
168 int ret = MS_MEDIA_ERR_NONE;
171 if (storage_id == NULL)
172 sql = sqlite3_mprintf("UPDATE '%q' SET validity=%d WHERE storage_uuid != 'media';", MEDIA_SVC_DB_TABLE_STORAGE, validity);
174 sql = sqlite3_mprintf("UPDATE '%q' SET validity=%d WHERE storage_uuid=%Q;", MEDIA_SVC_DB_TABLE_STORAGE, validity, storage_id);
176 ret = _media_svc_sql_query(sql, uid);
177 SQLITE3_SAFE_FREE(sql);
182 int _media_svc_get_storage_uuid(sqlite3 *handle, const char *path, char *storage_id, uid_t uid)
184 int ret = MS_MEDIA_ERR_NONE;
185 sqlite3_stmt *sql_stmt = NULL;
187 char *storage_path = NULL;
188 char *remain_path = NULL;
190 char *internal_path = NULL;
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");
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;
201 SAFE_FREE(internal_path);
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);
207 storage_path = strndup(path, strlen(path) - remain_len);
209 sql = sqlite3_mprintf("SELECT storage_uuid FROM '%q' WHERE validity=1 AND storage_path = '%q'", MEDIA_SVC_DB_TABLE_STORAGE, storage_path);
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);
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);
218 SQLITE3_FINALIZE(sql_stmt);
220 if (!STRING_VALID(storage_id)) {
221 media_svc_error("Not found valid storage id [%s]", path);
222 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
228 int _media_svc_get_storage_type(sqlite3 *handle, const char *storage_id, media_svc_storage_type_e *storage_type)
230 int ret = MS_MEDIA_ERR_NONE;
231 sqlite3_stmt *sql_stmt = NULL;
234 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_id");
236 sql = sqlite3_mprintf("SELECT storage_type FROM '%q' WHERE storage_uuid=%Q", MEDIA_SVC_DB_TABLE_STORAGE, storage_id);
238 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
240 if (ret != MS_MEDIA_ERR_NONE) {
241 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
242 media_svc_debug("there is no storage.");
244 media_svc_error("error when _media_svc_get_storage_type. err = [%d]", ret);
249 *storage_type = sqlite3_column_int(sql_stmt, 0);
251 SQLITE3_FINALIZE(sql_stmt);
256 int _media_svc_get_storage_path(sqlite3 *handle, const char *storage_id, char **storage_path)
258 int ret = MS_MEDIA_ERR_NONE;
259 sqlite3_stmt *sql_stmt = NULL;
262 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_id");
264 sql = sqlite3_mprintf("SELECT storage_path FROM '%q' WHERE (storage_uuid=%Q AND validity=1)", MEDIA_SVC_DB_TABLE_STORAGE, storage_id);
266 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
268 if (ret != MS_MEDIA_ERR_NONE) {
269 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
270 media_svc_debug("there is no storage.");
272 media_svc_error("error when _media_svc_get_storage_type. err = [%d]", ret);
277 *storage_path = g_strdup((char *)sqlite3_column_text(sql_stmt, 0));
279 SQLITE3_FINALIZE(sql_stmt);
284 int _media_svc_get_storage_scan_status(sqlite3 *handle, const char *storage_id, media_svc_scan_status_type_e *scan_status)
286 int ret = MS_MEDIA_ERR_NONE;
287 sqlite3_stmt *sql_stmt = NULL;
290 if (!STRING_VALID(storage_id)) {
291 media_svc_error("Invalid storage_id");
292 return MS_MEDIA_ERR_INVALID_PARAMETER;
295 sql = sqlite3_mprintf("SELECT scan_status FROM '%q' WHERE (storage_uuid=%Q AND validity=1)", MEDIA_SVC_DB_TABLE_STORAGE, storage_id);
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.");
302 media_svc_error("error when _media_svc_get_storage_scan_status. err = [%d]", ret);
307 *scan_status = sqlite3_column_int(sql_stmt, 0);
309 SQLITE3_FINALIZE(sql_stmt);
314 int _media_svc_set_storage_scan_status(const char *storage_id, media_svc_scan_status_type_e scan_status, uid_t uid)
316 int ret = MS_MEDIA_ERR_NONE;
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);
322 sql = sqlite3_mprintf("UPDATE '%q' SET scan_status=%d WHERE storage_uuid=%Q;", MEDIA_SVC_DB_TABLE_STORAGE, scan_status, storage_id);
324 ret = _media_svc_sql_query(sql, uid);
325 SQLITE3_SAFE_FREE(sql);
330 static int __media_svc_count_all_storage(sqlite3 *handle, int *count)
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);
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);
342 *count = sqlite3_column_int(sql_stmt, 0);
344 SQLITE3_FINALIZE(sql_stmt);
346 return MS_MEDIA_ERR_NONE;
349 int _media_svc_get_all_storage(sqlite3 *handle, char ***storage_list, char ***storage_id_list, int **scan_status_list, int *count)
351 int ret = MS_MEDIA_ERR_NONE;
353 sqlite3_stmt *sql_stmt = NULL;
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);
364 sql = sqlite3_mprintf("SELECT storage_path, storage_uuid, scan_status FROM '%q' WHERE validity = 1", MEDIA_SVC_DB_TABLE_STORAGE);
366 *storage_list = NULL;
367 *scan_status_list = NULL;
368 return MS_MEDIA_ERR_NONE;
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;
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);
392 media_svc_debug("QEURY OK");
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)
403 if (cnt == idx + 1) {
405 media_svc_debug("OK");
409 for (i = 0; i < idx; i++) {
410 SAFE_FREE((*storage_list)[i]);
411 SAFE_FREE((*storage_id_list)[i]);
413 SAFE_FREE(*storage_list);
414 SAFE_FREE(*storage_id_list);
415 SAFE_FREE(*scan_status_list);
417 ret = MS_MEDIA_ERR_INTERNAL;
420 SQLITE3_FINALIZE(sql_stmt);