*
* Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
*
- * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@samsung.com>
- *
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
#include "media-svc-util.h"
#include "media-svc-storage.h"
-int _media_svc_check_storage(sqlite3 *handle, const char *storage_id, char **storage_path, int *validity, uid_t uid)
+int _media_svc_check_storage(sqlite3 *handle, const char *storage_id, char **storage_path, int *validity)
{
int ret = MS_MEDIA_ERR_NONE;
sqlite3_stmt *sql_stmt = NULL;
*storage_path = NULL;
*validity = 0;
- sql = sqlite3_mprintf("SELECT storage_path, validity FROM '%q' WHERE storage_uuid=%Q", MEDIA_SVC_DB_TABLE_STORAGE, storage_id);
+ sql = sqlite3_mprintf("SELECT storage_path, validity FROM %q WHERE storage_id=%Q", DB_TABLE_STORAGE, storage_id);
ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
SQLITE3_FINALIZE(sql_stmt);
- /*check storage media table*/
- if (STRING_VALID(storage_id)) {
- int table_cnt = 0;
-
- /*Select list of storage*/
- sql = sqlite3_mprintf("SELECT COUNT(*) FROM SQLITE_MASTER WHERE type='table' and name='%q'", storage_id);
- ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
- media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
-
- table_cnt = sqlite3_column_int(sql_stmt, 0);
- SQLITE3_FINALIZE(sql_stmt);
-
- if (table_cnt > 0) {
- /*DO NOT THING*/
- } else {
- media_svc_error("media table not exist for storage [%s]", storage_id);
- /*make storage media table*/
- ret = _media_svc_create_media_table_with_id(storage_id, uid);
- media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "create media table failed : %d", ret);
- }
- }
-
return MS_MEDIA_ERR_NONE;
}
-int _media_svc_append_storage(const char *storage_id, const char *storage_path, ms_user_storage_type_e storage_type, uid_t uid)
+int _media_svc_append_storage(const char *storage_id, const char *storage_path, uid_t uid)
{
int ret = MS_MEDIA_ERR_NONE;
- char *sql = sqlite3_mprintf("INSERT INTO %q (storage_uuid, storage_path, storage_type) values (%Q, %Q, %d);",
- MEDIA_SVC_DB_TABLE_STORAGE, storage_id, storage_path, storage_type);
+ char *sql = sqlite3_mprintf("INSERT INTO %q (storage_id, storage_path) values (%Q, %Q);",
+ DB_TABLE_STORAGE, storage_id, storage_path);
- ret = _media_svc_sql_query(sql, uid);
+ ret = _media_svc_sql_query_direct(sql, uid);
SQLITE3_SAFE_FREE(sql);
return ret;
media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
/*Get old path*/
- ret = _media_svc_check_storage(handle, storage_id, &old_storage_path, &validity, uid);
+ ret = _media_svc_check_storage(handle, storage_id, &old_storage_path, &validity);
media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
/*Storage table update*/
- sql = sqlite3_mprintf("UPDATE '%q' SET storage_path=%Q WHERE storage_uuid=%Q;", MEDIA_SVC_DB_TABLE_STORAGE, path, storage_id);
- ret = _media_svc_sql_query(sql, uid);
+ sql = sqlite3_mprintf("UPDATE %q SET storage_path=%Q WHERE storage_id=%Q", DB_TABLE_STORAGE, path, storage_id);
+ ret = _media_svc_sql_query_direct(sql, uid);
SQLITE3_SAFE_FREE(sql);
if (ret != MS_MEDIA_ERR_NONE) {
- G_SAFE_FREE(old_storage_path);
+ g_free(old_storage_path);
return ret;
}
/*Folder table update*/
- 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);
- ret = _media_svc_sql_query(sql, uid);
+ sql = sqlite3_mprintf("UPDATE %q SET folder_path=REPLACE(folder_path, %Q, %Q) WHERE storage_uuid=%Q", DB_TABLE_FOLDER, old_storage_path, path, storage_id);
+ ret = _media_svc_sql_query_direct(sql, uid);
SQLITE3_SAFE_FREE(sql);
if (ret != MS_MEDIA_ERR_NONE) {
- G_SAFE_FREE(old_storage_path);
+ g_free(old_storage_path);
return ret;
}
/*Media table update*/
- sql = sqlite3_mprintf("UPDATE '%q' SET path=REPLACE(path, %Q, %Q);", storage_id, old_storage_path, path);
- ret = _media_svc_sql_query(sql, uid);
+ sql = sqlite3_mprintf("UPDATE %q SET media_path=REPLACE(media_path, %Q, %Q) WHERE storage_uuid=%Q", DB_TABLE_MEDIA, old_storage_path, path, storage_id);
+ ret = _media_svc_sql_query_direct(sql, uid);
SQLITE3_SAFE_FREE(sql);
- G_SAFE_FREE(old_storage_path);
+ g_free(old_storage_path);
media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
return ret;
}
-int __media_svc_delete_thumbnail(sqlite3 *handle, const char *storage_id)
+static int __media_svc_delete_thumbnail(sqlite3 *handle)
{
int ret = MS_MEDIA_ERR_NONE;
char *sql = NULL;
sqlite3_stmt *sql_stmt = NULL;
- sql = sqlite3_mprintf("SELECT thumbnail_path FROM '%q' WHERE thumbnail_path is not null;", storage_id);
+ sql = sqlite3_mprintf("SELECT media_thumbnail_path FROM %q WHERE validity=0 AND media_thumbnail_path is not null", DB_TABLE_MEDIA);
ret = _media_svc_sql_prepare_to_step_simple(handle, sql, &sql_stmt);
media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
return ret;
}
-int _media_svc_delete_invalid_storage(sqlite3 *handle, ms_user_storage_type_e storage_type, uid_t uid)
+int _media_svc_delete_invalid_storage(sqlite3 *handle, uid_t uid)
{
int ret = MS_MEDIA_ERR_NONE;
char *sql = NULL;
- char *storage_id = NULL;
- char *thumb_path = NULL;
- sqlite3_stmt *sql_stmt = NULL;
- GArray *storage_list = NULL;
-
- /* Get invalid storage_uuid (with storage_type)*/
- sql = sqlite3_mprintf("SELECT storage_uuid FROM '%q' WHERE validity = 0 AND storage_type=%d;", MEDIA_SVC_DB_TABLE_STORAGE, storage_type);
- ret = _media_svc_sql_prepare_to_step_simple(handle, sql, &sql_stmt);
- media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
-
- storage_list = g_array_new(false, false, sizeof(char *));
-
- while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
- storage_id = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
- if (storage_id != NULL)
- g_array_append_val(storage_list, storage_id);
- storage_id = NULL;
- }
-
- SQLITE3_FINALIZE(sql_stmt);
-
- while ((storage_list != NULL) && (storage_list->len > 0)) {
- storage_id = g_array_index(storage_list, char *, 0);
- storage_list = g_array_remove_index(storage_list, 0);
-
- ret = __media_svc_delete_thumbnail(handle, storage_id);
- if (ret != MS_MEDIA_ERR_NONE) {
- media_svc_error("Fail to remove thumbnail");
- goto ERROR;
- }
-
- /* remove media before drop table (for clear playlist, and tag table)*/
- sql = sqlite3_mprintf("DELETE FROM '%q';DROP TABLE '%q';", storage_id, storage_id);
- ret = _media_svc_sql_query(sql, uid);
- SQLITE3_SAFE_FREE(sql);
- if (ret != MS_MEDIA_ERR_NONE) {
- media_svc_error("Fail to drop table[%s]", storage_id);
- goto ERROR;
- }
-
- SAFE_FREE(storage_id);
- }
+ ret = __media_svc_delete_thumbnail(handle);
+ media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Fail to remove thumbnail");
- /* Update storage, folder table */
- sql = sqlite3_mprintf("DELETE FROM '%q' WHERE validity=0 AND storage_type=%d;DELETE FROM '%q' WHERE validity=0 AND storage_type=%d;",
- MEDIA_SVC_DB_TABLE_STORAGE, storage_type, MEDIA_SVC_DB_TABLE_FOLDER, storage_type);
+ sql = sqlite3_mprintf("DELETE FROM %q WHERE validity=0;DELETE FROM %q WHERE validity=0;DELETE FROM %q WHERE validity=0;",
+ DB_TABLE_MEDIA, DB_TABLE_STORAGE, DB_TABLE_FOLDER);
- ret = _media_svc_sql_query(sql, uid);
+ ret = _media_svc_sql_query_direct(sql, uid);
SQLITE3_SAFE_FREE(sql);
- if (ret != MS_MEDIA_ERR_NONE) {
- media_svc_error("Fail to update storage table");
- goto ERROR;
- }
-
-ERROR:
- SAFE_FREE(storage_id);
- SAFE_FREE(thumb_path);
-
- while ((storage_list != NULL) && (storage_list->len > 0)) {
- storage_id = g_array_index(storage_list, char *, 0);
- storage_list = g_array_remove_index(storage_list, 0);
- SAFE_FREE(storage_id);
- }
-
- g_array_free(storage_list, false);
- storage_list = NULL;
return ret;
}
int ret = MS_MEDIA_ERR_NONE;
char *sql = NULL;
- if (storage_id == NULL)
- sql = sqlite3_mprintf("UPDATE '%q' SET validity=%d WHERE storage_uuid!='media';", MEDIA_SVC_DB_TABLE_STORAGE, validity);
- else
- sql = sqlite3_mprintf("UPDATE '%q' SET validity=%d WHERE storage_uuid=%Q;", MEDIA_SVC_DB_TABLE_STORAGE, validity, storage_id);
-
- ret = _media_svc_sql_query(sql, uid);
+ if (storage_id == NULL) {
+ sql = sqlite3_mprintf("UPDATE %q SET validity=%d;UPDATE %q SET validity=%d WHERE storage_uuid IS NOT 'media';UPDATE %q SET validity=%d WHERE storage_uuid IS NOT 'media';",
+ DB_TABLE_STORAGE, validity, DB_TABLE_FOLDER, validity, DB_TABLE_MEDIA, validity);
+ } else {
+ sql = sqlite3_mprintf("UPDATE %q SET validity=%d WHERE storage_id=%Q;UPDATE %q SET validity=%d WHERE storage_uuid=%Q;UPDATE %q SET validity=%d WHERE storage_uuid=%Q;",
+ DB_TABLE_STORAGE, validity, storage_id, DB_TABLE_FOLDER, validity, storage_id, DB_TABLE_MEDIA, validity, storage_id);
+ }
+ ret = _media_svc_sql_query_direct(sql, uid);
SQLITE3_SAFE_FREE(sql);
return ret;
int ret = MS_MEDIA_ERR_NONE;
sqlite3_stmt *sql_stmt = NULL;
char *sql = NULL;
- char *storage_path = NULL;
- char *remain_path = NULL;
- int remain_len = 0;
char *internal_path = NULL;
media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Fail to get root path");
if (STRING_VALID(internal_path) && strncmp(path, internal_path, strlen(internal_path)) == 0) {
- SAFE_STRLCPY(storage_id, MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_UUID_SIZE+1);
- SAFE_FREE(internal_path);
+ SAFE_STRLCPY(storage_id, DB_TABLE_MEDIA, MEDIA_SVC_UUID_SIZE + 1);
+ g_free(internal_path);
return MS_MEDIA_ERR_NONE;
}
- SAFE_FREE(internal_path);
+ g_free(internal_path);
- remain_path = strstr(path + (STRING_VALID(MEDIA_ROOT_PATH_USB) ? strlen(MEDIA_ROOT_PATH_USB) : 0) + 1, "/");
- if (remain_path != NULL)
- remain_len = strlen(remain_path);
-
- storage_path = strndup(path, strlen(path) - remain_len);
-
- sql = sqlite3_mprintf("SELECT storage_uuid FROM '%q' WHERE validity=1 AND storage_path = '%q'", MEDIA_SVC_DB_TABLE_STORAGE, storage_path);
+ sql = sqlite3_mprintf("SELECT storage_id FROM %q WHERE validity=1 AND instr(%Q, storage_path)", DB_TABLE_STORAGE, path);
ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
- SAFE_FREE(storage_path);
media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 0)))
- SAFE_STRLCPY(storage_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE+1);
+ SAFE_STRLCPY(storage_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE + 1);
SQLITE3_FINALIZE(sql_stmt);
return ret;
}
-
-static int __media_svc_count_all_storage(sqlite3 *handle, int *count)
-{
- int ret = MS_MEDIA_ERR_NONE;
- sqlite3_stmt *sql_stmt = NULL;
- char *sql = sqlite3_mprintf("SELECT count(*) FROM '%q' WHERE validity = 1", MEDIA_SVC_DB_TABLE_STORAGE);
-
- ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
- if (ret != MS_MEDIA_ERR_NONE) {
- media_svc_error("error when _media_svc_sql_prepare_to_step. err = [%d]", ret);
- return ret;
- }
-
- *count = sqlite3_column_int(sql_stmt, 0);
-
- SQLITE3_FINALIZE(sql_stmt);
-
- return MS_MEDIA_ERR_NONE;
-}
-
-int _media_svc_get_all_storage(sqlite3 *handle, char ***storage_list, char ***storage_id_list, int *count)
-{
- int ret = MS_MEDIA_ERR_NONE;
- int idx = 0;
- sqlite3_stmt *sql_stmt = NULL;
- char *sql = NULL;
- int cnt = 0;
-
- media_svc_retvm_if(count == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "count is NULL");
-
- ret = __media_svc_count_all_storage(handle, &cnt);
- if (ret != MS_MEDIA_ERR_NONE) {
- media_svc_error("error when __media_svc_count_all_folders. err = [%d]", ret);
- return ret;
- }
-
- if (cnt > 0) {
- sql = sqlite3_mprintf("SELECT storage_path, storage_uuid FROM '%q' WHERE validity = 1", MEDIA_SVC_DB_TABLE_STORAGE);
- } else {
- *storage_list = NULL;
- return MS_MEDIA_ERR_NONE;
- }
-
- *storage_list = malloc(sizeof(char *) * cnt);
- *storage_id_list = malloc(sizeof(char *) * cnt);
- if (*storage_list == NULL || *storage_id_list == NULL) {
- media_svc_error("Allocation failed");
- SAFE_FREE(*storage_list);
- SAFE_FREE(*storage_id_list);
- SQLITE3_SAFE_FREE(sql);
- return MS_MEDIA_ERR_OUT_OF_MEMORY;
- }
-
- ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
- if (ret != MS_MEDIA_ERR_NONE) {
- media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
- SAFE_FREE(*storage_list);
- SAFE_FREE(*storage_id_list);
- return ret;
- }
-
- media_svc_debug("QEURY OK");
-
- while (1) {
- (*storage_list)[idx] = g_strdup((char *)sqlite3_column_text(sql_stmt, 0));
- (*storage_id_list)[idx] = g_strdup((char *)sqlite3_column_text(sql_stmt, 1));
- if (sqlite3_step(sql_stmt) != SQLITE_ROW)
- break;
- idx++;
- }
-
- if (cnt == idx + 1) {
- *count = cnt;
- media_svc_debug("OK");
- } else {
- /* free all data */
- int i = 0;
- for (i = 0; i < idx; i++) {
- SAFE_FREE((*storage_list)[i]);
- SAFE_FREE((*storage_id_list)[i]);
- }
- SAFE_FREE(*storage_list);
- SAFE_FREE(*storage_id_list);
- *count = 0;
- ret = MS_MEDIA_ERR_INTERNAL;
- }
-
- SQLITE3_FINALIZE(sql_stmt);
-
- return ret;
-}