*
* 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"
-extern __thread GList *g_media_svc_move_item_query_list;
-
-int _media_svc_get_folder_id_by_foldername(sqlite3 *handle, const char *folder_name, char *folder_id)
+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;
- char *sql = sqlite3_mprintf("SELECT folder_uuid FROM %s WHERE path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, 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);
+ media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_sql_prepare_to_step failed [%d]", ret);
- 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;
- }
-
- _strncpy_safe(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE+1);
+ *folder_id = sqlite3_column_int64(sql_stmt, 0);
SQLITE3_FINALIZE(sql_stmt);
return ret;
}
-int _media_svc_append_folder(sqlite3 *handle, media_svc_storage_type_e storage_type,
- const char *folder_id, const char *path_name, const char *folder_name, int modified_date, 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;
+ int folder_modified_date = 0;
+
+ 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.. */
+ 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);
- char *sql = sqlite3_mprintf("INSERT INTO %s (folder_uuid, path, name, storage_type, modified_time, name_pinyin) values (%Q, %Q, %Q, '%d', '%d', %Q); ",
- MEDIA_SVC_DB_TABLE_FOLDER, folder_id, path_name, folder_name, storage_type, modified_date, folder_name_pinyin);
- ret = _media_svc_sql_query(handle, sql, uid);
- sqlite3_free(sql);
+ if (is_direct)
+ ret = _media_svc_sql_query_direct(sql, uid);
+ else
+ ret = _media_svc_sql_query(sql, uid);
- 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(sqlite3 *handle, const char *folder_uuid, const char *folder_path, bool stack_query, 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;
-
- modified_time = _media_svc_get_file_time(folder_path);
+ int time = 0;
+ char *query = NULL;
- char *sql = sqlite3_mprintf("UPDATE %s SET modified_time=%d WHERE folder_uuid=%Q;", MEDIA_SVC_DB_TABLE_FOLDER, modified_time, folder_uuid);
+ 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);
- if(!stack_query) {
- ret = _media_svc_sql_query(handle, sql, uid);
- sqlite3_free(sql);
- } else {
- _media_svc_sql_query_add(&g_media_svc_move_item_query_list, &sql);
- }
+ ret = _media_svc_sql_query(query, uid);
+ SQLITE3_SAFE_FREE(query);
return ret;
}
-int _media_svc_get_and_append_folder_id_by_path(sqlite3 *handle, const char *path, media_svc_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)
{
- char *path_name = NULL;
int ret = MS_MEDIA_ERR_NONE;
+ size_t next_pos = ms_user_get_root_length(path, uid);
+ char *next = NULL;
+ char *dir_path = NULL;
+
+ do {
+ next = strstr(path + next_pos, "/");
+ if (next) {
+ next_pos = (next - path);
+ dir_path = g_strndup(path, next_pos);
+ next_pos++;
+ } else {
+ dir_path = g_strdup(path);
+ }
- path_name = g_path_get_dirname(path);
+ ret = _media_svc_check_folder_by_path(handle, dir_path);
+ if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
+ 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");
+ else
+ media_svc_sec_debug("Append new folder path[%s]", dir_path);
+ }
- ret = _media_svc_get_folder_id_by_foldername(handle, path_name, folder_id);
+ g_free(dir_path);
+ } while (next);
- if(ret == MS_MEDIA_ERR_DB_NO_RECORD) {
- char *folder_name = NULL;
- int folder_modified_date = 0;
- char *folder_uuid = _media_info_generate_uuid();
- if(folder_uuid == NULL ) {
- media_svc_error("Invalid UUID");
- SAFE_FREE(path_name);
- return MS_MEDIA_ERR_INTERNAL;
- }
+ return MS_MEDIA_ERR_NONE;
+}
- folder_name = g_path_get_basename(path_name);
- folder_modified_date = _media_svc_get_file_time(path_name);
+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_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;
+ }
- ret = _media_svc_append_folder(handle, storage_type, folder_uuid, path_name, folder_name, folder_modified_date, uid);
- SAFE_FREE(folder_name);
- _strncpy_safe(folder_id, folder_uuid, MEDIA_SVC_UUID_SIZE+1);
+ ret = __media_svc_get_folder_id(handle, dir_path, folder_id);
}
+FINALIZE:
+ g_free(dir_path);
- SAFE_FREE(path_name);
+ return ret;
+}
+
+int _media_svc_append_by_folder_path(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
+{
+ int ret = MS_MEDIA_ERR_NONE;
+
+ 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_update_folder_table(sqlite3 *handle, 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;
- sql = sqlite3_mprintf("DELETE FROM %s WHERE folder_uuid IN (SELECT folder_uuid FROM %s WHERE folder_uuid NOT IN (SELECT folder_uuid FROM %s))",
- MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_TABLE_MEDIA);
+ if (is_recursive) {
+ 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 folder_path=%Q", DB_TABLE_FOLDER, validity, start_path);
+ }
+
+ if (is_direct)
+ ret = _media_svc_sql_query_direct(sql, uid);
+ else
+ ret = _media_svc_sql_query(sql, uid);
- ret = _media_svc_sql_query(handle, sql, uid);
- sqlite3_free(sql);
+ SQLITE3_SAFE_FREE(sql);
return ret;
}
+
+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(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
+
+ 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);
+
+ SQLITE3_FINALIZE(sql_stmt);
+
+ return MS_MEDIA_ERR_NONE;
+}