X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=src%2Fcommon%2Fmedia-svc-storage.c;h=7ab1b07f29c811a659a2abaab9ec7ae2fcffd6a2;hb=6174112bc81ab241463dd54c45816c796188173a;hp=30d2c24213613153f88eb62f9a683ab0b9219299;hpb=7a7d3ff636d3e1f1f76fb630de046d5b8e7debe0;p=platform%2Fcore%2Fmultimedia%2Flibmedia-service.git diff --git a/src/common/media-svc-storage.c b/src/common/media-svc-storage.c index 30d2c24..7ab1b07 100755 --- a/src/common/media-svc-storage.c +++ b/src/common/media-svc-storage.c @@ -26,101 +26,61 @@ #include "media-svc-util.h" #include "media-svc-storage.h" -int _media_svc_init_storage(sqlite3 *handle, uid_t uid) -{ - int ret = MS_MEDIA_ERR_NONE; - char *sql = NULL; - sqlite3_stmt *sql_stmt = NULL; - int storage_cnt = 0; - - /*Add Internal storage*/ - sql = sqlite3_mprintf("SELECT COUNT(*) FROM '%s' WHERE storage_uuid='%s' AND storage_name='%s'", MEDIA_SVC_DB_TABLE_STORAGE, MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_DB_TABLE_MEDIA); - 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); - SQLITE3_FINALIZE(sql_stmt); - - if (storage_cnt == 0) { - char *internal_path = _media_svc_get_path(uid); - 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, internal_path); - - ret = _media_svc_sql_query(handle, sql, uid); - sqlite3_free(sql); - SAFE_FREE(internal_path); - media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret); - } - - return ret; -} - -int _media_svc_get_mmc_info(MediaSvcHandle *handle, char **storage_name, char **storage_path, int *validity, bool *info_exist) +int _media_svc_check_storage(sqlite3 *handle, const char *storage_id, char **storage_path, int *validity, uid_t uid) { int ret = MS_MEDIA_ERR_NONE; sqlite3_stmt *sql_stmt = NULL; char *sql = NULL; - sql = sqlite3_mprintf("SELECT * FROM '%s' WHERE storage_uuid=%Q AND storage_name IS NOT '%s'", MEDIA_SVC_DB_TABLE_STORAGE, MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_DB_TABLE_MEDIA); - - ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt); - if (ret != MS_MEDIA_ERR_NONE) { - *storage_name = NULL; - *storage_path = NULL; - *validity = 0; - *info_exist = FALSE; - - if (ret == MS_MEDIA_ERR_DB_NO_RECORD) - *info_exist = FALSE; - - return ret; - } - - *storage_name = g_strdup((const char *)sqlite3_column_text(sql_stmt, 1)); - *storage_path = g_strdup((const char *)sqlite3_column_text(sql_stmt, 2)); - *validity = sqlite3_column_int(sql_stmt, 6); - - *info_exist = TRUE; - - SQLITE3_FINALIZE(sql_stmt); - - return MS_MEDIA_ERR_NONE; -} - -int _media_svc_check_storage(sqlite3 *handle, const char *storage_id, const char *storage_name, char **storage_path, int *validity) -{ - int ret = MS_MEDIA_ERR_NONE; - sqlite3_stmt *sql_stmt = NULL; - 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"); *storage_path = NULL; *validity = 0; - if (storage_name != NULL) - sql = sqlite3_mprintf("SELECT * FROM '%s' WHERE storage_uuid=%Q AND storage_name=%Q", MEDIA_SVC_DB_TABLE_STORAGE, storage_id, storage_name); - else - sql = sqlite3_mprintf("SELECT * FROM '%s' WHERE storage_uuid=%Q", MEDIA_SVC_DB_TABLE_STORAGE, storage_id); - + sql = sqlite3_mprintf("SELECT storage_path, validity FROM '%q' WHERE storage_uuid=%Q", MEDIA_SVC_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_path = g_strdup((const char *)sqlite3_column_text(sql_stmt, 2)); - *validity = sqlite3_column_int(sql_stmt, 6); + *storage_path = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0)); + *validity = sqlite3_column_int(sql_stmt, 1); 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(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) +int _media_svc_append_storage(const char *storage_id, const char *storage_path, ms_user_storage_type_e storage_type, uid_t uid) { 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 = 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); - ret = _media_svc_sql_query(handle, sql, uid); - sqlite3_free(sql); + ret = _media_svc_sql_query(sql, uid); + SQLITE3_SAFE_FREE(sql); return ret; } @@ -132,303 +92,194 @@ int _media_svc_update_storage_path(sqlite3 *handle, const char *storage_id, cons 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, NULL, &old_storage_path, &validity); + ret = _media_svc_check_storage(handle, storage_id, &old_storage_path, &validity, uid); media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret); /*Storage table update*/ - sql = sqlite3_mprintf("UPDATE '%s' SET storage_path=%Q WHERE storage_uuid=%Q", MEDIA_SVC_DB_TABLE_STORAGE, path, storage_id); - ret = _media_svc_sql_query(handle, sql, uid); - sqlite3_free(sql); - media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret); + 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); + SQLITE3_SAFE_FREE(sql); + if (ret != MS_MEDIA_ERR_NONE) { + G_SAFE_FREE(old_storage_path); + return ret; + } /*Folder table update*/ - sql = sqlite3_mprintf("UPDATE '%s' 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(handle, sql, uid); - sqlite3_free(sql); - media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret); + sql = sqlite3_mprintf("UPDATE '%q' SET folder_path=REPLACE(folder_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); + SQLITE3_SAFE_FREE(sql); + if (ret != MS_MEDIA_ERR_NONE) { + G_SAFE_FREE(old_storage_path); + return ret; + } /*Media table update*/ - sql = sqlite3_mprintf("UPDATE '%s' SET path=REPLACE(path, %Q, %Q)", storage_id, old_storage_path, path); - ret = _media_svc_sql_query(handle, sql, uid); - sqlite3_free(sql); + sql = sqlite3_mprintf("UPDATE '%q' SET path=REPLACE(path, %Q, %Q);", storage_id, old_storage_path, path); + ret = _media_svc_sql_query(sql, uid); + SQLITE3_SAFE_FREE(sql); + G_SAFE_FREE(old_storage_path); media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret); return ret; } -int _media_svc_delete_storage(sqlite3 *handle, const char *storage_id, const char *storage_name, uid_t uid) -{ - int ret = MS_MEDIA_ERR_NONE; - char *sql = NULL; - - if (storage_name != NULL) - sql = sqlite3_mprintf("DELETE FROM '%s' WHERE storage_uuid=%Q AND storage_name=%Q", MEDIA_SVC_DB_TABLE_STORAGE, storage_id, storage_name); - else if (storage_id != NULL) - sql = sqlite3_mprintf("DELETE FROM '%s' WHERE storage_uuid=%Q", MEDIA_SVC_DB_TABLE_STORAGE, storage_id); - - ret = _media_svc_sql_query(handle, sql, uid); - sqlite3_free(sql); - - return ret; -} - -int _media_svc_update_storage_validity(sqlite3 *handle, const char *storage_id, int validity, uid_t uid) +int __media_svc_delete_thumbnail(sqlite3 *handle, const char *storage_id) { 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' AND storage_type != %d", MEDIA_SVC_DB_TABLE_STORAGE, validity, MEDIA_SVC_STORAGE_CLOUD); - else - sql = sqlite3_mprintf("UPDATE '%s' SET validity=%d WHERE storage_uuid=%Q;", MEDIA_SVC_DB_TABLE_STORAGE, validity, storage_id); - - ret = _media_svc_sql_query(handle, sql, uid); - sqlite3_free(sql); - - return ret; -} - -int _media_svc_get_storage_uuid(sqlite3 *handle, const char *path, char *storage_id) -{ - 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; - - if (strncmp(path, MEDIA_ROOT_PATH_INTERNAL, strlen(MEDIA_ROOT_PATH_INTERNAL)) == 0) { - _strncpy_safe(storage_id, MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_UUID_SIZE+1); - return MS_MEDIA_ERR_NONE; - } - - sql = sqlite3_mprintf("SELECT storage_uuid, storage_path FROM '%s' WHERE validity=1", MEDIA_SVC_DB_TABLE_STORAGE); + sql = sqlite3_mprintf("SELECT thumbnail_path FROM '%q' WHERE thumbnail_path is not null;", storage_id); ret = _media_svc_sql_prepare_to_step_simple(handle, sql, &sql_stmt); - media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret); - 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); - - while (sqlite3_step(sql_stmt) == SQLITE_ROW) { - if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 1))) { - if (strlen(storage_path) == strlen((const char *)sqlite3_column_text(sql_stmt, 1))) { - if (strncmp(storage_path, (const char *)sqlite3_column_text(sql_stmt, 1), strlen(storage_path)) == 0) { - _strncpy_safe(storage_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE+1); - break; - } - } - } - - } + while (sqlite3_step(sql_stmt) == SQLITE_ROW) + _media_svc_remove_file((const char *)sqlite3_column_text(sql_stmt, 0)); SQLITE3_FINALIZE(sql_stmt); - SAFE_FREE(storage_path); - - if (!STRING_VALID(storage_id)) { - media_svc_error("Not found valid storage id [%s]", path); - ret = MS_MEDIA_ERR_INVALID_PARAMETER; - } return ret; } -int _media_svc_get_storage_type(sqlite3 *handle, const char *storage_id, media_svc_storage_type_e *storage_type) +int _media_svc_delete_invalid_storage(sqlite3 *handle, ms_user_storage_type_e storage_type, uid_t uid) { int ret = MS_MEDIA_ERR_NONE; - sqlite3_stmt *sql_stmt = NULL; char *sql = NULL; + char *storage_id = NULL; + char *thumb_path = NULL; + sqlite3_stmt *sql_stmt = NULL; + GArray *storage_list = NULL; - if (!STRING_VALID(storage_id)) { - media_svc_error("Invalid storage_idid"); - ret = MS_MEDIA_ERR_INVALID_PARAMETER; - } - - sql = sqlite3_mprintf("SELECT storage_type FROM '%s' WHERE (storage_uuid=%Q AND validity=1)", MEDIA_SVC_DB_TABLE_STORAGE, storage_id); + /* 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); - ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt); + storage_list = g_array_new(false, false, sizeof(char *)); - if (ret != MS_MEDIA_ERR_NONE) { - if (ret == MS_MEDIA_ERR_DB_NO_RECORD) - media_svc_debug("there is no storage."); - else - media_svc_error("error when _media_svc_get_storage_type. err = [%d]", ret); + 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); - return ret; + storage_id = NULL; } - *storage_type = sqlite3_column_int(sql_stmt, 0); - SQLITE3_FINALIZE(sql_stmt); - return ret; -} - -int _media_svc_get_storage_path(sqlite3 *handle, const char *storage_id, char **storage_path) -{ - int ret = MS_MEDIA_ERR_NONE; - sqlite3_stmt *sql_stmt = NULL; - char *sql = NULL; + 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); - if (!STRING_VALID(storage_id)) { - media_svc_error("Invalid storage_idid"); - ret = MS_MEDIA_ERR_INVALID_PARAMETER; - } - - sql = sqlite3_mprintf("SELECT storage_path FROM '%s' WHERE (storage_uuid=%Q AND validity=1)", MEDIA_SVC_DB_TABLE_STORAGE, storage_id); - - ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt); + ret = __media_svc_delete_thumbnail(handle, storage_id); + if (ret != MS_MEDIA_ERR_NONE) { + media_svc_error("Fail to remove thumbnail"); + goto ERROR; + } - if (ret != MS_MEDIA_ERR_NONE) { - if (ret == MS_MEDIA_ERR_DB_NO_RECORD) - media_svc_debug("there is no storage."); - else - media_svc_error("error when _media_svc_get_storage_type. err = [%d]", ret); + /* 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; + } - return ret; + SAFE_FREE(storage_id); } - *storage_path = g_strdup((char *)sqlite3_column_text(sql_stmt, 0)); - - SQLITE3_FINALIZE(sql_stmt); + /* 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); - return ret; -} - -int _media_svc_get_storage_scan_status(sqlite3 *handle, const char *storage_id, media_svc_scan_status_type_e *scan_status) -{ - int ret = MS_MEDIA_ERR_NONE; - sqlite3_stmt *sql_stmt = NULL; - char *sql = NULL; - - if (!STRING_VALID(storage_id)) { - media_svc_error("Invalid storage_id"); - return MS_MEDIA_ERR_INVALID_PARAMETER; + ret = _media_svc_sql_query(sql, uid); + SQLITE3_SAFE_FREE(sql); + if (ret != MS_MEDIA_ERR_NONE) { + media_svc_error("Fail to update storage table"); + goto ERROR; } - sql = sqlite3_mprintf("SELECT scan_status FROM '%s' WHERE (storage_uuid=%Q AND validity=1)", MEDIA_SVC_DB_TABLE_STORAGE, 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 storage."); - else - media_svc_error("error when _media_svc_get_storage_scan_status. err = [%d]", ret); +ERROR: + SAFE_FREE(storage_id); + SAFE_FREE(thumb_path); - return ret; + 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); } - *scan_status = sqlite3_column_int(sql_stmt, 0); - - SQLITE3_FINALIZE(sql_stmt); + g_array_free(storage_list, false); + storage_list = NULL; return ret; } -int _media_svc_set_storage_scan_status(sqlite3 *handle, const char *storage_id, media_svc_scan_status_type_e scan_status, 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 scan_status=%d WHERE storage_uuid != 'media'", MEDIA_SVC_DB_TABLE_STORAGE, scan_status); + sql = sqlite3_mprintf("UPDATE '%q' SET validity=%d WHERE storage_uuid!='media';", MEDIA_SVC_DB_TABLE_STORAGE, validity); else - sql = sqlite3_mprintf("UPDATE '%s' SET scan_status=%d WHERE storage_uuid=%Q", MEDIA_SVC_DB_TABLE_STORAGE, scan_status, storage_id); + 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(handle, sql, uid); - sqlite3_free(sql); + ret = _media_svc_sql_query(sql, uid); + SQLITE3_SAFE_FREE(sql); return ret; } -static int __media_svc_count_all_storage(sqlite3 *handle, int *count) +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 = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE validity = 1", MEDIA_SVC_DB_TABLE_STORAGE); + char *sql = NULL; + char *storage_path = NULL; + char *remain_path = NULL; + int remain_len = 0; + char *internal_path = NULL; - 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; - } + media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL"); - *count = sqlite3_column_int(sql_stmt, 0); + 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"); - SQLITE3_FINALIZE(sql_stmt); + 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); + return MS_MEDIA_ERR_NONE; + } - return MS_MEDIA_ERR_NONE; -} + SAFE_FREE(internal_path); -int _media_svc_get_all_storage(sqlite3 *handle, char ***storage_list, char ***storage_id_list, int **scan_status_list, int *count) -{ - int ret = MS_MEDIA_ERR_NONE; - int idx = 0; - sqlite3_stmt *sql_stmt = NULL; - char *sql = NULL; - int cnt = 0; - - 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; - } + 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); - if (cnt > 0) { - sql = sqlite3_mprintf("SELECT storage_path, storage_uuid, scan_status FROM '%s' WHERE validity = 1", MEDIA_SVC_DB_TABLE_STORAGE); - } else { - *storage_list = NULL; - *scan_status_list = NULL; - return MS_MEDIA_ERR_NONE; - } + storage_path = strndup(path, strlen(path) - remain_len); - *storage_list = malloc(sizeof(char *) * cnt); - *storage_id_list = malloc(sizeof(char *) * cnt); - *scan_status_list = malloc(sizeof(int) * cnt); + sql = sqlite3_mprintf("SELECT storage_uuid FROM '%q' WHERE validity=1 AND storage_path = '%q'", MEDIA_SVC_DB_TABLE_STORAGE, storage_path); 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(*scan_status_list); - return ret; - } + SAFE_FREE(storage_path); + media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret); - media_svc_debug("QEURY OK"); + 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); - 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)); - (*scan_status_list)[idx] = (int)sqlite3_column_int(sql_stmt, 2); - if (sqlite3_step(sql_stmt) != SQLITE_ROW) - break; - idx++; - } + SQLITE3_FINALIZE(sql_stmt); - 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); - SAFE_FREE(*scan_status_list); - *count = 0; - ret = MS_MEDIA_ERR_INTERNAL; + if (!STRING_VALID(storage_id)) { + media_svc_error("Not found valid storage id [%s]", path); + ret = MS_MEDIA_ERR_INVALID_PARAMETER; } - SQLITE3_FINALIZE(sql_stmt); - return ret; }