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_init_storage(sqlite3 *handle, uid_t uid)
31 int ret = MS_MEDIA_ERR_NONE;
33 sqlite3_stmt *sql_stmt = NULL;
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);
41 storage_cnt = sqlite3_column_int(sql_stmt, 0);
42 SQLITE3_FINALIZE(sql_stmt);
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);
49 ret = _media_svc_sql_query(sql, uid);
51 SAFE_FREE(internal_path);
52 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
58 int _media_svc_get_mmc_info(MediaSvcHandle *handle, char **storage_name, char **storage_path, int *validity, bool *info_exist)
60 int ret = MS_MEDIA_ERR_NONE;
61 sqlite3_stmt *sql_stmt = NULL;
64 sql = sqlite3_mprintf("SELECT * FROM '%s' WHERE storage_uuid=%Q", MEDIA_SVC_DB_TABLE_STORAGE, MEDIA_SVC_DB_TABLE_MEDIA);
66 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
67 if (ret != MS_MEDIA_ERR_NONE) {
73 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
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);
85 SQLITE3_FINALIZE(sql_stmt);
87 return MS_MEDIA_ERR_NONE;
90 int _media_svc_check_storage(sqlite3 *handle, const char *storage_id, const char *storage_name, char **storage_path, int *validity)
92 int ret = MS_MEDIA_ERR_NONE;
93 sqlite3_stmt *sql_stmt = NULL;
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);
102 sql = sqlite3_mprintf("SELECT * FROM '%s' WHERE storage_uuid=%Q", MEDIA_SVC_DB_TABLE_STORAGE, storage_id);
104 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
106 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
108 *storage_path = g_strdup((const char *)sqlite3_column_text(sql_stmt, 2));
109 *validity = sqlite3_column_int(sql_stmt, 6);
111 SQLITE3_FINALIZE(sql_stmt);
113 return MS_MEDIA_ERR_NONE;
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)
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);
122 ret = _media_svc_sql_query(sql, uid);
128 int _media_svc_update_storage_path(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
130 int ret = MS_MEDIA_ERR_NONE;
132 char *old_storage_path = NULL;
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);
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);
143 if (ret != MS_MEDIA_ERR_NONE) {
144 G_SAFE_FREE(old_storage_path);
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);
152 if (ret != MS_MEDIA_ERR_NONE) {
153 G_SAFE_FREE(old_storage_path);
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);
161 G_SAFE_FREE(old_storage_path);
162 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
167 int _media_svc_delete_storage(const char *storage_id, const char *storage_name, uid_t uid)
169 int ret = MS_MEDIA_ERR_NONE;
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);
177 ret = _media_svc_sql_query(sql, uid);
183 int _media_svc_update_storage_validity(const char *storage_id, int validity, uid_t uid)
185 int ret = MS_MEDIA_ERR_NONE;
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);
191 sql = sqlite3_mprintf("UPDATE '%s' SET validity=%d WHERE storage_uuid=%Q;", MEDIA_SVC_DB_TABLE_STORAGE, validity, storage_id);
193 ret = _media_svc_sql_query(sql, uid);
199 int _media_svc_get_storage_uuid(sqlite3 *handle, const char *path, char *storage_id)
201 int ret = MS_MEDIA_ERR_NONE;
202 sqlite3_stmt *sql_stmt = NULL;
204 char *storage_path = NULL;
205 char *remain_path = NULL;
208 if (STRING_VALID(MEDIA_ROOT_PATH_INTERNAL) && strncmp(path, MEDIA_ROOT_PATH_INTERNAL, strlen(MEDIA_ROOT_PATH_INTERNAL)) == 0) {
209 _strncpy_safe(storage_id, MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_UUID_SIZE+1);
210 return MS_MEDIA_ERR_NONE;
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);
217 storage_path = strndup(path, strlen(path) - remain_len);
219 sql = sqlite3_mprintf("SELECT storage_uuid FROM '%s' WHERE validity=1 AND storage_path = '%s'", MEDIA_SVC_DB_TABLE_STORAGE, storage_path);
221 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
223 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
224 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 0)))
225 _strncpy_safe(storage_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE+1);
227 SQLITE3_FINALIZE(sql_stmt);
228 SAFE_FREE(storage_path);
230 if (!STRING_VALID(storage_id)) {
231 media_svc_error("Not found valid storage id [%s]", path);
232 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
238 int _media_svc_get_storage_type(sqlite3 *handle, const char *storage_id, media_svc_storage_type_e *storage_type)
240 int ret = MS_MEDIA_ERR_NONE;
241 sqlite3_stmt *sql_stmt = NULL;
244 if (!STRING_VALID(storage_id)) {
245 media_svc_error("Invalid storage_idid");
246 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
249 sql = sqlite3_mprintf("SELECT storage_type FROM '%s' WHERE storage_uuid=%Q", MEDIA_SVC_DB_TABLE_STORAGE, storage_id);
251 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
253 if (ret != MS_MEDIA_ERR_NONE) {
254 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
255 media_svc_debug("there is no storage.");
257 media_svc_error("error when _media_svc_get_storage_type. err = [%d]", ret);
262 *storage_type = sqlite3_column_int(sql_stmt, 0);
264 SQLITE3_FINALIZE(sql_stmt);
269 int _media_svc_get_storage_path(sqlite3 *handle, const char *storage_id, char **storage_path)
271 int ret = MS_MEDIA_ERR_NONE;
272 sqlite3_stmt *sql_stmt = NULL;
275 if (!STRING_VALID(storage_id)) {
276 media_svc_error("Invalid storage_idid");
277 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
280 sql = sqlite3_mprintf("SELECT storage_path FROM '%s' WHERE (storage_uuid=%Q AND validity=1)", MEDIA_SVC_DB_TABLE_STORAGE, storage_id);
282 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
284 if (ret != MS_MEDIA_ERR_NONE) {
285 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
286 media_svc_debug("there is no storage.");
288 media_svc_error("error when _media_svc_get_storage_type. err = [%d]", ret);
293 *storage_path = g_strdup((char *)sqlite3_column_text(sql_stmt, 0));
295 SQLITE3_FINALIZE(sql_stmt);
300 int _media_svc_get_storage_scan_status(sqlite3 *handle, const char *storage_id, media_svc_scan_status_type_e *scan_status)
302 int ret = MS_MEDIA_ERR_NONE;
303 sqlite3_stmt *sql_stmt = NULL;
306 if (!STRING_VALID(storage_id)) {
307 media_svc_error("Invalid storage_id");
308 return MS_MEDIA_ERR_INVALID_PARAMETER;
311 sql = sqlite3_mprintf("SELECT scan_status FROM '%s' WHERE (storage_uuid=%Q AND validity=1)", MEDIA_SVC_DB_TABLE_STORAGE, storage_id);
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.");
318 media_svc_error("error when _media_svc_get_storage_scan_status. err = [%d]", ret);
323 *scan_status = sqlite3_column_int(sql_stmt, 0);
325 SQLITE3_FINALIZE(sql_stmt);
330 int _media_svc_set_storage_scan_status(const char *storage_id, media_svc_scan_status_type_e scan_status, uid_t uid)
332 int ret = MS_MEDIA_ERR_NONE;
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);
338 sql = sqlite3_mprintf("UPDATE '%s' SET scan_status=%d WHERE storage_uuid=%Q;", MEDIA_SVC_DB_TABLE_STORAGE, scan_status, storage_id);
340 ret = _media_svc_sql_query(sql, uid);
346 static int __media_svc_count_all_storage(sqlite3 *handle, int *count)
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);
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);
358 *count = sqlite3_column_int(sql_stmt, 0);
360 SQLITE3_FINALIZE(sql_stmt);
362 return MS_MEDIA_ERR_NONE;
365 int _media_svc_get_all_storage(sqlite3 *handle, char ***storage_list, char ***storage_id_list, int **scan_status_list, int *count)
367 int ret = MS_MEDIA_ERR_NONE;
369 sqlite3_stmt *sql_stmt = NULL;
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);
380 sql = sqlite3_mprintf("SELECT storage_path, storage_uuid, scan_status FROM '%s' WHERE validity = 1", MEDIA_SVC_DB_TABLE_STORAGE);
382 *storage_list = NULL;
383 *scan_status_list = NULL;
384 return MS_MEDIA_ERR_NONE;
387 *storage_list = malloc(sizeof(char *) * cnt);
388 *storage_id_list = malloc(sizeof(char *) * cnt);
389 *scan_status_list = malloc(sizeof(int) * cnt);
391 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
392 if (ret != MS_MEDIA_ERR_NONE) {
393 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
394 SAFE_FREE(*storage_list);
395 SAFE_FREE(*scan_status_list);
399 media_svc_debug("QEURY OK");
402 (*storage_list)[idx] = g_strdup((char *)sqlite3_column_text(sql_stmt, 0));
403 (*storage_id_list)[idx] = g_strdup((char *)sqlite3_column_text(sql_stmt, 1));
404 (*scan_status_list)[idx] = (int)sqlite3_column_int(sql_stmt, 2);
405 if (sqlite3_step(sql_stmt) != SQLITE_ROW)
410 if (cnt == idx + 1) {
412 media_svc_debug("OK");
416 for (i = 0; i < idx; i++) {
417 SAFE_FREE((*storage_list)[i]);
418 SAFE_FREE((*storage_id_list)[i]);
420 SAFE_FREE(*storage_list);
421 SAFE_FREE(*storage_id_list);
422 SAFE_FREE(*scan_status_list);
424 ret = MS_MEDIA_ERR_INTERNAL;
427 SQLITE3_FINALIZE(sql_stmt);