*
* 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"
-#define FOLDER_SCAN_DONE 4
-
-extern __thread GList *g_media_svc_move_item_query_list;
-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 '%s' 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;
- }
-
- _strncpy_safe(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 '%s' 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);
-
- return ret;
- }
+ media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_sql_prepare_to_step failed [%d]", ret);
- _strncpy_safe(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, media_svc_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 %s (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, 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;
+ 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 %s SET modified_time=%d WHERE folder_uuid=%Q;", MEDIA_SVC_DB_TABLE_FOLDER, modified_time, folder_uuid);
-
- if (!stack_query) {
- ret = _media_svc_sql_query(sql, uid);
- SQLITE3_SAFE_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;
}
-static int __media_svc_get_and_append_parent_folder(sqlite3 *handle, const char *storage_id, 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)
{
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);
+ dir_path = g_strdup(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;
- }
-
- 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\n", dir_path);
- }
-
- SAFE_FREE(dir_path);
- }
-
- if (STRING_VALID(folder_uuid)) {
- _strncpy_safe(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, media_svc_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, media_svc_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);
-
- SAFE_FREE(dir_path);
-
- return ret;
-}
-
-int _media_svc_update_folder_table(const char *storage_id, 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, storage_id);
-
- ret = _media_svc_sql_query(sql, uid);
- SQLITE3_SAFE_FREE(sql);
-
- return ret;
-}
-
-static int __media_svc_count_all_folders(sqlite3 *handle, char *start_path, int *count)
-{
- int ret = MS_MEDIA_ERR_NONE;
- sqlite3_stmt *sql_stmt = NULL;
- char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE path LIKE '%q%%'", MEDIA_SVC_DB_TABLE_FOLDER, start_path);
-
- 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_folders(sqlite3 *handle, char *start_path, char ***folder_list, time_t **modified_time_list, int **item_num_list, int *count)
-{
- int ret = MS_MEDIA_ERR_NONE;
- int idx = 0;
- sqlite3_stmt *sql_stmt = NULL;
- char *sql = NULL;
- int cnt = 0;
- char **folder_uuid = NULL;
- int i = 0;
-
- ret = __media_svc_count_all_folders(handle, start_path, &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 path, modified_time, folder_uuid FROM '%s' WHERE path LIKE '%q%%'", MEDIA_SVC_DB_TABLE_FOLDER, start_path);
- } else {
- *folder_list = NULL;
- *modified_time_list = NULL;
- *item_num_list = NULL;
- return MS_MEDIA_ERR_NONE;
- }
-
- *folder_list = malloc(sizeof(char *) * cnt);
- *modified_time_list = malloc(sizeof(int) * cnt);
- *item_num_list = malloc(sizeof(int) * cnt);
- folder_uuid = malloc(sizeof(char *) * cnt);
-
- if ((*folder_list == NULL) || (*modified_time_list == NULL) || (*item_num_list == NULL) || (folder_uuid == NULL)) {
- media_svc_error("Out of memory");
- goto ERROR;
- }
- memset(folder_uuid, 0x0, sizeof(char *) * cnt);
- memset(*folder_list, 0x0, sizeof(char *) * cnt);
-
- 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));
- goto ERROR;
- }
-
- media_svc_debug("QEURY OK");
-
- while (idx < cnt) {
- (*folder_list)[idx] = g_strdup((char *)sqlite3_column_text(sql_stmt, 0));
- (*modified_time_list)[idx] = (int)sqlite3_column_int(sql_stmt, 1);
-
- /* get the folder's id */
- folder_uuid[idx] = g_strdup((char *)sqlite3_column_text(sql_stmt, 2));
-
- idx++;
-
- if (sqlite3_step(sql_stmt) != SQLITE_ROW)
- break;
- }
- SQLITE3_FINALIZE(sql_stmt);
-
- /*get the numbder of item in the folder by using folder's id */
- for (i = 0; i < idx; i++) {
- if (STRING_VALID(folder_uuid[i])) {
- sql = sqlite3_mprintf("SELECT COUNT(*) FROM %s WHERE (folder_uuid='%q' AND validity = 1)", MEDIA_SVC_DB_TABLE_MEDIA, folder_uuid[i]);
- 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));
- goto ERROR;
- }
-
- (*item_num_list)[i] = (int)sqlite3_column_int(sql_stmt, 0);
-
- SQLITE3_FINALIZE(sql_stmt);
- } else {
- media_svc_error("Invalid Folder Id");
- }
- }
-
- if (cnt == idx) {
- *count = cnt;
- media_svc_debug("Get Folder is OK");
- } else {
- media_svc_error("Fail to get folder");
- ret = MS_MEDIA_ERR_INTERNAL;
- goto ERROR;
- }
-
- /* free all data */
- for (i = 0; i < idx; i++)
- SAFE_FREE(folder_uuid[i]);
-
- SAFE_FREE(folder_uuid);
-
- return ret;
-
-ERROR:
-
- /* free all data */
- for (i = 0; i < idx; i++) {
- SAFE_FREE((*folder_list)[i]);
- SAFE_FREE(folder_uuid[i]);
- }
- SAFE_FREE(*folder_list);
- SAFE_FREE(*modified_time_list);
- SAFE_FREE(*item_num_list);
- SAFE_FREE(folder_uuid);
-
- *count = 0;
-
- return ret;
-}
-
-int _media_svc_get_folder_info_by_foldername(sqlite3 *handle, const char *storage_id, const char *folder_name, char *folder_id, time_t *modified_time)
-{
- int ret = MS_MEDIA_ERR_NONE;
- sqlite3_stmt *sql_stmt = NULL;
-
- char *sql = sqlite3_mprintf("SELECT folder_uuid, modified_time FROM %s 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;
- }
-
- _strncpy_safe(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE + 1);
- *modified_time = (int)sqlite3_column_int(sql_stmt, 1);
-
- SQLITE3_FINALIZE(sql_stmt);
-
- return ret;
-}
-
-int _media_svc_get_and_append_folder_id_by_folder_path(sqlite3 *handle, const char *storage_id, const char *path, media_svc_storage_type_e storage_type, char *folder_id, bool stack_query, 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);
+ ret = __media_svc_get_folder_id(handle, dir_path, folder_id);
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 '%s' 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);
+ 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(path_name);
+ ret = __media_svc_get_folder_id(handle, dir_path, folder_id);
+ }
+FINALIZE:
+ g_free(dir_path);
return ret;
}
-int _media_svc_delete_invalid_folder(const char *storage_id, int storage_type, uid_t uid)
+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;
- char *sql = NULL;
-
- sql = sqlite3_mprintf("DELETE FROM '%s' WHERE storage_uuid = '%q' AND storage_type = %d AND validity = 0;", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, storage_type);
- ret = _media_svc_sql_query(sql, uid);
- SQLITE3_SAFE_FREE(sql);
+ 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 '%s' 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 '%s' SET validity = %d WHERE storage_uuid = '%q' AND path = '%q';",
- MEDIA_SVC_DB_TABLE_FOLDER, validity, storage_id, start_path);
- }
-
- ret = _media_svc_sql_query(sql, uid);
-
- SQLITE3_SAFE_FREE(sql);
-
- return ret;
-}
-
-int _media_svc_delete_folder_by_storage_id(const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
-{
- int ret = MS_MEDIA_ERR_NONE;
- char *sql = NULL;
-
- sql = sqlite3_mprintf("DELETE FROM '%s' WHERE storage_uuid = '%q' AND storage_type = %d;", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, storage_type);
- 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_get_folder_scan_status(sqlite3 *handle, const char *storage_id, const char *path, int *scan_status)
-{
- int ret = MS_MEDIA_ERR_NONE;
- sqlite3_stmt *sql_stmt = NULL;
- char *sql = NULL;
-
- if (!STRING_VALID(path)) {
- media_svc_error("Not found valid path");
- ret = MS_MEDIA_ERR_INVALID_PARAMETER;
+ sql = sqlite3_mprintf("UPDATE %q SET validity=%d WHERE folder_path=%Q", DB_TABLE_FOLDER, validity, start_path);
}
- sql = sqlite3_mprintf("SELECT scan_status FROM '%s' WHERE path = '%q' AND storage_uuid = '%q'", MEDIA_SVC_DB_TABLE_FOLDER, path, storage_id);
-
- 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)
- *scan_status = sqlite3_column_int(sql_stmt, 0);
-
- SQLITE3_FINALIZE(sql_stmt);
-
- return ret;
-}
-
-int _media_svc_set_folder_scan_status(const char *storage_id, const char *path, int scan_status, uid_t uid)
-{
- int ret = MS_MEDIA_ERR_NONE;
- char *sql = NULL;
-
- if (path)
- sql = sqlite3_mprintf("UPDATE '%s' SET scan_status=%d WHERE path = '%q' AND storage_uuid = '%q'", MEDIA_SVC_DB_TABLE_FOLDER, scan_status, path, storage_id);
+ if (is_direct)
+ ret = _media_svc_sql_query_direct(sql, uid);
else
- sql = sqlite3_mprintf("UPDATE '%s' SET scan_status=%d WHERE storage_uuid = '%q'", MEDIA_SVC_DB_TABLE_FOLDER, scan_status, storage_id);
+ ret = _media_svc_sql_query(sql, uid);
- ret = _media_svc_sql_query(sql, uid);
SQLITE3_SAFE_FREE(sql);
return ret;
}
-int _media_svc_get_folder_modified_time_by_path(sqlite3 *handle, const char *path, const char *storage_id, time_t *modified_time)
+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 = sqlite3_mprintf("SELECT modified_time FROM '%s' WHERE path = '%q' AND storage_uuid = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, path, storage_id);
-
- 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_modified_time_by_path. err = [%d]", ret);
-
- return ret;
- }
-
- *modified_time = (int)sqlite3_column_int(sql_stmt, 0);
-
- SQLITE3_FINALIZE(sql_stmt);
-
- return ret;
-}
-
-int _media_svc_get_null_scan_folder_list(sqlite3 *handle, const char *storage_id, const char *path, char ***folder_list, int *count)
-{
- int ret = MS_MEDIA_ERR_NONE;
- int idx = 0;
- int cnt = 0;
- char *sql = NULL;
- sqlite3_stmt *sql_stmt = NULL;
-
- if (path == NULL) {
- sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE storage_uuid = '%q' AND scan_status!=%d", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, FOLDER_SCAN_DONE);
- 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 get folder_id by path. err = [%d]", ret);
-
- *folder_list = NULL;
- *count = 0;
-
- return ret;
- }
-
- cnt = sqlite3_column_int(sql_stmt, 0);
- SQLITE3_FINALIZE(sql_stmt);
- if (cnt > 0) {
- sql = sqlite3_mprintf("SELECT path FROM '%s' WHERE storage_uuid = '%q' AND scan_status!=%d", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, FOLDER_SCAN_DONE);
- } else {
- *folder_list = NULL;
- *count = 0;
-
- return MS_MEDIA_ERR_INTERNAL;
- }
-
- 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));
-
- *folder_list = NULL;
- *count = 0;
-
- return ret;
- }
- } else {
- sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE path IN (SELECT DISTINCT '%q/'||name from '%s' WHERE path LIKE '%q/%%')",
- MEDIA_SVC_DB_TABLE_FOLDER, path, MEDIA_SVC_DB_TABLE_FOLDER, path);
- 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;
- }
-
- cnt = sqlite3_column_int(sql_stmt, 0);
- SQLITE3_FINALIZE(sql_stmt);
- if (cnt > 0) {
- sql = sqlite3_mprintf("SELECT path FROM '%s' WHERE path IN (SELECT DISTINCT '%q/'||name from '%s' WHERE path LIKE '%q/%%')",
- MEDIA_SVC_DB_TABLE_FOLDER, path, MEDIA_SVC_DB_TABLE_FOLDER, path);
- } else {
- *folder_list = NULL;
- *count = 0;
-
- return MS_MEDIA_ERR_NONE;
- }
-
- 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));
-
- *folder_list = NULL;
- *count = 0;
-
- return ret;
- }
- }
-
- *folder_list = malloc(sizeof(char *) * cnt);
- if (*folder_list == NULL) {
- media_svc_error("Allocation failed");
-
- *folder_list = NULL;
- *count = 0;
-
- return MS_MEDIA_ERR_OUT_OF_MEMORY;
- }
-
- while (1) {
- (*folder_list)[idx] = strdup((char *)sqlite3_column_text(sql_stmt, 0));
-
- 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((*folder_list)[i]);
-
- SAFE_FREE(*folder_list);
- *count = 0;
- ret = MS_MEDIA_ERR_INTERNAL;
- }
-
- SQLITE3_FINALIZE(sql_stmt);
-
- return ret;
-}
-
-int _media_svc_delete_invalid_folder_by_path(sqlite3 *handle, const char *storage_id, const char *folder_path, uid_t uid, int *delete_count)
-{
- int ret = MS_MEDIA_ERR_NONE;
char *sql = NULL;
- int del_count = 0;
- sqlite3_stmt *sql_stmt = NULL;
-
- if (folder_path == NULL)
- return MS_MEDIA_ERR_INVALID_PARAMETER;
-
- /*check the number of the deleted folder*/
- sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE (storage_uuid = '%q' AND validity = 0 AND PATH LIKE '%q/%%');", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, folder_path);
-
- ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
- media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
-
- del_count = sqlite3_column_int(sql_stmt, 0);
-
- SQLITE3_FINALIZE(sql_stmt);
- sql = NULL;
-
- /*delete invalid folder*/
- sql = sqlite3_mprintf("DELETE FROM '%s' WHERE (storage_uuid = '%q' AND validity = 0 AND PATH LIKE '%q%%');", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, folder_path);
- ret = _media_svc_sql_query(sql, uid);
-
- SQLITE3_SAFE_FREE(sql);
-
- *delete_count = del_count;
-
- return ret;
-}
-
-int _media_svc_count_folder_with_path(sqlite3 *handle, const char *storage_id, const char *path, int *count)
-{
- int ret = MS_MEDIA_ERR_NONE;
- sqlite3_stmt *sql_stmt = NULL;
-
- char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE (storage_uuid='%q' AND path='%q')", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, 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;
-}
-
-int _media_svc_count_subfolder_with_path(sqlite3 *handle, const char *storage_id, const char *path, int *count)
-{
- int ret = MS_MEDIA_ERR_NONE;
- sqlite3_stmt *sql_stmt = NULL;
- char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE (storage_uuid='%q' AND path LIKE'%q/%%')", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
+ 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);
- *count = sqlite3_column_int(sql_stmt, 0);
-
SQLITE3_FINALIZE(sql_stmt);
return MS_MEDIA_ERR_NONE;
}
-
-int _media_svc_get_folder_uuid(sqlite3 *handle, const char *storage_id, const char *path, char *folder_id)
-{
- int ret = MS_MEDIA_ERR_NONE;
- sqlite3_stmt *sql_stmt = NULL;
- char *sql = NULL;
-
- sql = sqlite3_mprintf("SELECT folder_uuid FROM '%s' WHERE (storage_uuid='%q' AND path='%q')", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
-
- ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
-
- media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
-
- _strncpy_safe(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE+1);
-
- SQLITE3_FINALIZE(sql_stmt);
-
- if (!STRING_VALID(folder_id)) {
- media_svc_error("Not found valid storage id [%s]", path);
- ret = MS_MEDIA_ERR_INVALID_PARAMETER;
- }
-
- return ret;
-}
-