*
* 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_init_storage(sqlite3 *handle, 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;
- char *sql = NULL;
sqlite3_stmt *sql_stmt = NULL;
- int storage_cnt = 0;
+ char *sql = NULL;
+
+ media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
+ media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
+ media_svc_retvm_if(validity == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "validity is NULL");
- /*Add Internal storage*/
- sql = sqlite3_mprintf("SELECT COUNT(*) FROM '%s' WHERE storage_uuid='%s'", MEDIA_SVC_DB_TABLE_STORAGE, MEDIA_SVC_DB_TABLE_MEDIA);
+ *storage_path = NULL;
+ *validity = 0;
+
+ 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);
- storage_cnt = sqlite3_column_int(sql_stmt, 0);
+ *storage_path = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
+ *validity = sqlite3_column_int(sql_stmt, 1);
+
SQLITE3_FINALIZE(sql_stmt);
- if(storage_cnt == 0)
- {
- sql = sqlite3_mprintf("INSERT INTO %s (storage_uuid, storage_name, storage_path, storage_type) VALUES ('%s', '%s', '%s', 0);",
- MEDIA_SVC_DB_TABLE_STORAGE, MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_DB_TABLE_MEDIA, _media_svc_get_path(uid));
+ return MS_MEDIA_ERR_NONE;
+}
+
+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_id, storage_path) values (%Q, %Q);",
+ DB_TABLE_STORAGE, storage_id, storage_path);
+
+ ret = _media_svc_sql_query_direct(sql, uid);
+ SQLITE3_SAFE_FREE(sql);
+
+ return ret;
+}
+
+int _media_svc_update_storage_path(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
+{
+ int ret = MS_MEDIA_ERR_NONE;
+ char *sql = NULL;
+ char *old_storage_path = NULL;
+ int validity = 0;
+
+ media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
+ 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);
+ media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
+
+ /*Storage table update*/
+ 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_free(old_storage_path);
+ return ret;
+ }
- ret = _media_svc_sql_query(handle, sql, uid);
- sqlite3_free(sql);
- media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
+ /*Folder table update*/
+ 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_free(old_storage_path);
+ return ret;
}
+ /*Media table update*/
+ 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_free(old_storage_path);
+ media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
+
return ret;
}
-int _media_svc_append_storage(sqlite3 *handle, 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)
+static int __media_svc_delete_thumbnail(sqlite3 *handle)
{
int ret = MS_MEDIA_ERR_NONE;
- char *sql = sqlite3_mprintf("INSERT INTO %s (storage_uuid, storage_name, storage_path, storage_account, storage_type) values (%Q, %Q, %Q, %Q, %d); ",
- MEDIA_SVC_DB_TABLE_STORAGE, storage_id, storage_name, storage_path, storage_account, storage_type);
+ char *sql = NULL;
+ sqlite3_stmt *sql_stmt = NULL;
- ret = _media_svc_sql_query(handle, sql, uid);
- sqlite3_free(sql);
+ 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);
+
+ while (sqlite3_step(sql_stmt) == SQLITE_ROW)
+ _media_svc_remove_file((const char *)sqlite3_column_text(sql_stmt, 0));
+
+ SQLITE3_FINALIZE(sql_stmt);
return ret;
}
-int _media_svc_delete_storage(sqlite3 *handle, const char *storage_id, uid_t uid)
+int _media_svc_delete_invalid_storage(sqlite3 *handle, uid_t uid)
{
int ret = MS_MEDIA_ERR_NONE;
- char *sql = sqlite3_mprintf("DELETE FROM '%s' WHERE storage_uuid=%Q", MEDIA_SVC_DB_TABLE_STORAGE, storage_id);
+ char *sql = NULL;
- ret = _media_svc_sql_query(handle, sql, uid);
- sqlite3_free(sql);
+ ret = __media_svc_delete_thumbnail(handle);
+ media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Fail to remove thumbnail");
+
+ 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_direct(sql, uid);
+ SQLITE3_SAFE_FREE(sql);
return ret;
}
-int _media_svc_update_storage_validity(sqlite3 *handle, const char *storage_id, int validity, uid_t uid)
+int _media_svc_update_storage_validity(const char *storage_id, int validity, uid_t uid)
{
int ret = MS_MEDIA_ERR_NONE;
char *sql = NULL;
if (storage_id == NULL) {
- sql = sqlite3_mprintf("UPDATE '%s' SET validity=%d WHERE storage_uuid != 'media';", MEDIA_SVC_DB_TABLE_STORAGE, validity);
+ 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 '%s' SET validity=%d WHERE storage_uuid=%Q;", MEDIA_SVC_DB_TABLE_STORAGE, validity, storage_id);
+ 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 _media_svc_get_storage_uuid(sqlite3 *handle, const char *path, char *storage_id, uid_t uid)
+{
+ int ret = MS_MEDIA_ERR_NONE;
+ sqlite3_stmt *sql_stmt = NULL;
+ char *sql = NULL;
+ char *internal_path = NULL;
- ret = _media_svc_sql_query(handle, sql, uid);
- sqlite3_free(sql);
+ media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
+
+ ret = ms_user_get_internal_root_path(uid, &internal_path);
+ 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, DB_TABLE_MEDIA, MEDIA_SVC_UUID_SIZE + 1);
+ g_free(internal_path);
+ return MS_MEDIA_ERR_NONE;
+ }
+
+ g_free(internal_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);
+ 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);
+
+ SQLITE3_FINALIZE(sql_stmt);
+
+ if (!STRING_VALID(storage_id)) {
+ media_svc_error("Not found valid storage id [%s]", path);
+ ret = MS_MEDIA_ERR_INVALID_PARAMETER;
+ }
return ret;
}