*
* 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 <glib/gstdio.h>
-#include <media-util-err.h>
+#include <media-util-user.h>
+
#include "media-svc-media-folder.h"
#include "media-svc-debug.h"
#include "media-svc-env.h"
#include "media-svc-util.h"
#include "media-svc-db-utils.h"
-static __thread GList *g_media_svc_insert_folder_query_list;
-
-int _media_svc_get_folder_id_by_foldername(sqlite3 *handle, const char *storage_id, const char *folder_name, char *folder_id, uid_t uid)
-{
- int ret = MS_MEDIA_ERR_NONE;
- sqlite3_stmt *sql_stmt = NULL;
- char *sql = NULL;
-
- sql = sqlite3_mprintf("SELECT folder_uuid FROM %q WHERE storage_uuid = '%q' AND path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, folder_name);
-
- ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
- if (ret != MS_MEDIA_ERR_NONE) {
- if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
- media_svc_debug("there is no folder.");
- else
- media_svc_error("error when _media_svc_get_folder_id_by_foldername. err = [%d]", ret);
-
- return ret;
- }
-
- SAFE_STRLCPY(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE + 1);
-
- SQLITE3_FINALIZE(sql_stmt);
-
- return ret;
-}
-
-int _media_svc_get_folder_id_by_foldername_with_validity(sqlite3 *handle, const char *storage_id, const char *folder_name, char *folder_id, uid_t uid, int *validity)
+static int __media_svc_get_folder_id(sqlite3 *handle, const char *path, long long int *folder_id)
{
int ret = MS_MEDIA_ERR_NONE;
sqlite3_stmt *sql_stmt = NULL;
char *sql = NULL;
- sql = sqlite3_mprintf("SELECT folder_uuid, validity FROM %q WHERE storage_uuid = '%q' AND path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, folder_name);
+ sql = sqlite3_mprintf("SELECT folder_id FROM %q WHERE folder_path=%Q", DB_TABLE_FOLDER, path);
ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
- if (ret != MS_MEDIA_ERR_NONE) {
- if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
- media_svc_debug("there is no folder.");
- else
- media_svc_error("error when _media_svc_get_folder_id_by_foldername_with_validity. err = [%d]", ret);
+ media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_sql_prepare_to_step failed [%d]", ret);
- return ret;
- }
-
- SAFE_STRLCPY(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE + 1);
- *validity = (int)sqlite3_column_int(sql_stmt, 1);
+ *folder_id = sqlite3_column_int64(sql_stmt, 0);
SQLITE3_FINALIZE(sql_stmt);
return ret;
}
-static int __media_svc_append_folder(const char *storage_id, ms_user_storage_type_e storage_type,
- const char *folder_id, const char *folder_path, bool stack_query, uid_t uid)
+static int __media_svc_append_folder(bool is_direct, const char *storage_id, const char *folder_path, uid_t uid)
{
int ret = MS_MEDIA_ERR_NONE;
char *folder_name = NULL;
folder_name = g_path_get_basename(folder_path);
folder_modified_date = _media_svc_get_file_time(folder_path);
- /*Update Pinyin If Support Pinyin*/
- char *folder_name_pinyin = NULL;
- if (_media_svc_check_pinyin_support())
- _media_svc_get_pinyin_str(folder_name, &folder_name_pinyin);
/* Sometime SQLITE3 returns NO_RECORD, so need to consider conflict case.. */
- media_svc_debug("UNIQUE:path[%s], storage_uuid[%s]", folder_path, storage_id);
- char *sql = sqlite3_mprintf("INSERT OR IGNORE INTO %q (folder_uuid, path, name, storage_uuid, storage_type, modified_time, name_pinyin) values (%Q, %Q, %Q, %Q, '%d', '%d', %Q); ",
- MEDIA_SVC_DB_TABLE_FOLDER, folder_id, folder_path, folder_name, storage_id, storage_type, folder_modified_date, folder_name_pinyin);
+ char *sql = sqlite3_mprintf("INSERT OR IGNORE INTO %q (folder_path, folder_name, storage_uuid, folder_modified_time) VALUES (%Q, %Q, %Q, '%d');",
+ DB_TABLE_FOLDER, folder_path, folder_name, storage_id, folder_modified_date);
- if (!stack_query) {
+ if (is_direct)
+ ret = _media_svc_sql_query_direct(sql, uid);
+ else
ret = _media_svc_sql_query(sql, uid);
- SQLITE3_SAFE_FREE(sql);
- } else {
- _media_svc_sql_query_add(&g_media_svc_insert_folder_query_list, &sql);
- }
- SAFE_FREE(folder_name);
- SAFE_FREE(folder_name_pinyin);
+ SQLITE3_SAFE_FREE(sql);
+
+ g_free(folder_name);
return ret;
}
-int _media_svc_update_folder_modified_time_by_folder_uuid(const char *folder_uuid, const char *folder_path, uid_t uid)
+int _media_svc_update_folder_modified_time(const char *folder_path, uid_t uid)
{
int ret = MS_MEDIA_ERR_NONE;
- int modified_time = 0;
+ int time = 0;
+ char *query = NULL;
- modified_time = _media_svc_get_file_time(folder_path);
+ time = _media_svc_get_file_time(folder_path);
+ query = sqlite3_mprintf("UPDATE %q SET folder_modified_time=%d WHERE folder_path=%Q", DB_TABLE_FOLDER, time, folder_path);
- char *sql = sqlite3_mprintf("UPDATE %q SET modified_time=%d WHERE folder_uuid=%Q;", MEDIA_SVC_DB_TABLE_FOLDER, modified_time, folder_uuid);
-
- ret = _media_svc_sql_query(sql, uid);
- SQLITE3_SAFE_FREE(sql);
+ ret = _media_svc_sql_query(query, uid);
+ SQLITE3_SAFE_FREE(query);
return ret;
}
-static int __media_svc_get_and_append_parent_folder(sqlite3 *handle, const char *storage_id, const char *path, ms_user_storage_type_e storage_type, char *folder_id, uid_t uid)
+static int __media_svc_append_parent_folder(sqlite3 *handle, bool is_direct, const char *storage_id, const char *path, uid_t uid)
{
int ret = MS_MEDIA_ERR_NONE;
- unsigned int next_pos = 0;
+ size_t next_pos = ms_user_get_root_length(path, uid);
char *next = NULL;
char *dir_path = NULL;
- const char *token = "/";
- char *folder_uuid = NULL;
- char tmp_folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
- bool folder_search_end = FALSE;
- char *internal_path = NULL;
-
- memset(tmp_folder_uuid, 0, sizeof(tmp_folder_uuid));
- 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))
- next_pos = strlen(internal_path);
- else if (STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0)
- next_pos = strlen(MEDIA_ROOT_PATH_SDCARD);
- else if (STRING_VALID(MEDIA_ROOT_PATH_EXTERNAL) && strncmp(path, MEDIA_ROOT_PATH_EXTERNAL, strlen(MEDIA_ROOT_PATH_EXTERNAL)) == 0)
- next_pos = strlen(MEDIA_ROOT_PATH_EXTERNAL);
- else if (STRING_VALID(MEDIA_ROOT_PATH_DISC) && strncmp(path, MEDIA_ROOT_PATH_DISC, strlen(MEDIA_ROOT_PATH_DISC)) == 0)
- next_pos = strlen(MEDIA_ROOT_PATH_DISC);
- else {
- media_svc_error("Invalid Path");
- media_svc_sec_error("Invalid Path [%s], internal_path [%s]", path, internal_path);
- SAFE_FREE(internal_path);
- return MS_MEDIA_ERR_INTERNAL;
- }
- SAFE_FREE(internal_path);
-
- while (!folder_search_end) {
- next = strstr(path + next_pos, token);
- if (next != NULL) {
+ do {
+ next = strstr(path + next_pos, "/");
+ if (next) {
next_pos = (next - path);
- dir_path = strndup(path, next_pos);
+ dir_path = g_strndup(path, next_pos);
next_pos++;
} else {
- dir_path = strndup(path, strlen(path));
- folder_search_end = TRUE;
- media_svc_error("[No-Error] End Path [%s]", dir_path);
- }
-
- if (STRING_VALID(MEDIA_ROOT_PATH_EXTERNAL) && (g_strcmp0(dir_path, MEDIA_ROOT_PATH_EXTERNAL) == 0)) {
- /*To avoid insert MEDIA_ROOT_PATH_EXTERNAL path*/
- SAFE_FREE(dir_path);
- continue;
+ dir_path = g_strdup(path);
}
- ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, dir_path, tmp_folder_uuid, uid);
+ ret = _media_svc_check_folder_by_path(handle, dir_path);
if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
- folder_uuid = _media_info_generate_uuid();
- if (folder_uuid == NULL) {
- media_svc_error("Invalid UUID");
- SAFE_FREE(dir_path);
- return MS_MEDIA_ERR_INTERNAL;
- }
-
- ret = __media_svc_append_folder(storage_id, storage_type, folder_uuid, dir_path, FALSE, uid);
+ ret = __media_svc_append_folder(is_direct, storage_id, dir_path, uid);
if (ret != MS_MEDIA_ERR_NONE)
media_svc_error("__media_svc_append_folder is failed");
-
- media_svc_error("[No-Error] New Appended folder path [%s], folder_uuid [%s]", dir_path, folder_uuid);
- } else {
- media_svc_error("EXIST dir path : %s", dir_path);
- }
-
- SAFE_FREE(dir_path);
- }
-
- if (STRING_VALID(folder_uuid)) {
- SAFE_STRLCPY(folder_id, folder_uuid, MEDIA_SVC_UUID_SIZE + 1);
- } else {
- media_svc_error("Fail to get folder_uuid");
- return MS_MEDIA_ERR_INTERNAL;
- }
-
- return MS_MEDIA_ERR_NONE;
-}
-
-static int __media_svc_update_parent_folder_validity(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
-{
- int ret = MS_MEDIA_ERR_NONE;
- unsigned int next_pos = 0;
- char *next = NULL;
- char *dir_path = NULL;
- const char *token = "/";
- bool folder_search_end = FALSE;
- char *internal_path = 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))
- next_pos = strlen(internal_path);
- else if (STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0)
- next_pos = strlen(MEDIA_ROOT_PATH_SDCARD);
- else if (STRING_VALID(MEDIA_ROOT_PATH_EXTERNAL) && strncmp(path, MEDIA_ROOT_PATH_EXTERNAL, strlen(MEDIA_ROOT_PATH_EXTERNAL)) == 0)
- next_pos = strlen(MEDIA_ROOT_PATH_EXTERNAL);
- else if (STRING_VALID(MEDIA_ROOT_PATH_DISC) && strncmp(path, MEDIA_ROOT_PATH_DISC, strlen(MEDIA_ROOT_PATH_DISC)) == 0)
- next_pos = strlen(MEDIA_ROOT_PATH_DISC);
- else {
- media_svc_error("Invalid Path");
- media_svc_sec_error("Invalid Path [%s], internal_path [%s]", path, internal_path);
- SAFE_FREE(internal_path);
- return MS_MEDIA_ERR_INTERNAL;
- }
-
- SAFE_FREE(internal_path);
-
- while (!folder_search_end) {
- next = strstr(path + next_pos, token);
- if (next != NULL) {
- next_pos = (next - path);
- dir_path = strndup(path, next_pos);
- next_pos++;
- } else {
- dir_path = strndup(path, strlen(path));
- folder_search_end = TRUE;
- media_svc_error("[No-Error] End Path [%s]", dir_path);
- }
-
- if (STRING_VALID(MEDIA_ROOT_PATH_EXTERNAL) && (g_strcmp0(dir_path, MEDIA_ROOT_PATH_EXTERNAL) == 0)) {
- /*To avoid insert MEDIA_ROOT_PATH_EXTERNAL path*/
- SAFE_FREE(dir_path);
- continue;
+ else
+ media_svc_sec_debug("Append new folder path[%s]", dir_path);
}
- ret = _media_svc_set_folder_validity(handle, storage_id, dir_path, 1, false, uid);
- SAFE_FREE(dir_path);
- media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Fail to set folder validity");
- }
+ g_free(dir_path);
+ } while (next);
return MS_MEDIA_ERR_NONE;
}
-int _media_svc_get_and_append_folder(sqlite3 *handle, const char *storage_id, const char *path, ms_user_storage_type_e storage_type, char *folder_id, uid_t uid)
-{
- int ret = MS_MEDIA_ERR_NONE;
- int validity = -1;
-
- ret = _media_svc_get_folder_id_by_foldername_with_validity(handle, storage_id, path, folder_id, uid, &validity);
-
- if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
- ret = __media_svc_get_and_append_parent_folder(handle, storage_id, path, storage_type, folder_id, uid);
- else if (ret == MS_MEDIA_ERR_NONE && validity == 0)
- ret = __media_svc_update_parent_folder_validity(handle, storage_id, path, uid);
-
- return ret;
-}
-
-int _media_svc_get_and_append_folder_id_by_path(sqlite3 *handle, const char *storage_id, const char *path, ms_user_storage_type_e storage_type, char *folder_id, uid_t uid)
+int _media_svc_get_and_append_folder_id_by_path(sqlite3 *handle, bool is_direct, const char *storage_id, const char *path, long long int *folder_id, uid_t uid)
{
char *dir_path = NULL;
int ret = MS_MEDIA_ERR_NONE;
dir_path = g_path_get_dirname(path);
- ret = _media_svc_get_and_append_folder(handle, storage_id, dir_path, storage_type, folder_id, uid);
+ ret = __media_svc_get_folder_id(handle, dir_path, folder_id);
+ if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
+ ret = __media_svc_append_parent_folder(handle, is_direct, storage_id, dir_path, uid);
+ if (ret != MS_MEDIA_ERR_NONE) {
+ media_svc_error("__media_svc_append_parent_folder failed");
+ goto FINALIZE;
+ }
- SAFE_FREE(dir_path);
+ ret = __media_svc_get_folder_id(handle, dir_path, folder_id);
+ }
+FINALIZE:
+ g_free(dir_path);
return ret;
}
-int _media_svc_get_and_append_folder_id_by_folder_path(sqlite3 *handle, const char *storage_id, const char *path, ms_user_storage_type_e storage_type, char *folder_id, bool stack_query, uid_t uid)
+int _media_svc_append_by_folder_path(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
{
- char *path_name = NULL;
int ret = MS_MEDIA_ERR_NONE;
- char *sql = NULL;
-
- path_name = strdup(path);
- if (path_name == NULL) {
- media_svc_error("out of memory");
- return MS_MEDIA_ERR_OUT_OF_MEMORY;
- }
- ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, path_name, folder_id, uid);
- if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
- ret = __media_svc_get_and_append_parent_folder(handle, storage_id, path_name, storage_type, folder_id, uid);
- } else {
- sql = sqlite3_mprintf("UPDATE %q SET validity=1 WHERE storage_uuid = '%q' AND path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
- if (!stack_query) {
- ret = _media_svc_sql_query(sql, uid);
- SQLITE3_SAFE_FREE(sql);
- } else {
- _media_svc_sql_query_add(&g_media_svc_insert_folder_query_list, &sql);
- }
- }
-
- SAFE_FREE(path_name);
+ ret = _media_svc_check_folder_by_path(handle, path);
+ if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
+ ret = __media_svc_append_parent_folder(handle, true, storage_id, path, uid);
+ else
+ ret = _media_svc_set_folder_validity(true, path, 1, false, uid);
return ret;
}
-int _media_svc_set_folder_validity(sqlite3 *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
+int _media_svc_set_folder_validity(bool is_direct, const char *start_path, int validity, bool is_recursive, uid_t uid)
{
int ret = MS_MEDIA_ERR_NONE;
char *sql = NULL;
- char start_path_id[MEDIA_SVC_UUID_SIZE+1] = {0,};
if (is_recursive) {
- ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, start_path, start_path_id, uid);
- media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_folder_id_by_foldername fail");
- media_svc_retvm_if(!STRING_VALID(start_path_id), MS_MEDIA_ERR_INVALID_PARAMETER, "start_path_id is NULL");
-
- sql = sqlite3_mprintf("UPDATE %q SET validity = %d WHERE storage_uuid = '%q' AND (path LIKE '%q/%%' OR folder_uuid ='%q');",
- MEDIA_SVC_DB_TABLE_FOLDER, validity, storage_id, start_path, start_path_id);
+ sql = sqlite3_mprintf("UPDATE %q SET validity=%d WHERE folder_path LIKE '%q/%%' OR folder_path=%Q",
+ DB_TABLE_FOLDER, validity, start_path, start_path);
} else {
- sql = sqlite3_mprintf("UPDATE %q SET validity = %d WHERE storage_uuid = '%q' AND path = '%q';",
- MEDIA_SVC_DB_TABLE_FOLDER, validity, storage_id, start_path);
+ sql = sqlite3_mprintf("UPDATE %q SET validity=%d WHERE folder_path=%Q", DB_TABLE_FOLDER, validity, start_path);
}
- ret = _media_svc_sql_query(sql, uid);
+ if (is_direct)
+ ret = _media_svc_sql_query_direct(sql, uid);
+ else
+ ret = _media_svc_sql_query(sql, uid);
SQLITE3_SAFE_FREE(sql);
return ret;
}
-GList **_media_svc_get_folder_list_ptr(void)
-{
- return &g_media_svc_insert_folder_query_list;
-}
-
-int _media_svc_count_folder_with_path(sqlite3 *handle, const char *storage_id, const char *path, int *count)
+int _media_svc_check_folder_by_path(sqlite3 *handle, const char *path)
{
int ret = MS_MEDIA_ERR_NONE;
sqlite3_stmt *sql_stmt = NULL;
char *sql = NULL;
- media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
- sql = sqlite3_mprintf("SELECT count(*) FROM %q WHERE (storage_uuid='%q' AND path='%q')", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
-
+ sql = sqlite3_mprintf("SELECT 1 FROM %q WHERE folder_path=%Q", DB_TABLE_FOLDER, path);
ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
-
media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
- *count = sqlite3_column_int(sql_stmt, 0);
-
SQLITE3_FINALIZE(sql_stmt);
return MS_MEDIA_ERR_NONE;