//#define DB_PATH tzplatform_mkpath(TZ_USER_DB, ".vc_info.db")
static sqlite3* g_db_handle = NULL;
static sqlite3* g_db_backup_handle = NULL;
-char* g_path = NULL;
-char* g_backup_path = NULL;
+static char* g_path = NULL;
+static char* g_backup_path = NULL;
+static int g_ref_cnt = 0;
+static bool is_db_corrupted = false;
int g_fpid = -1;
-int g_ref_cnt = 0;
int g_db_cnt = 0;
int g_backup_db_cnt = 0;
static int __vc_db_rollback_transaction(sqlite3* db_handle);
static int __vc_db_commit_transaction(sqlite3* db_handle);
+
+int __vc_db_reset_handle(void)
+{
+ if (!g_db_handle || !g_db_backup_handle) {
+ vc_db_finalize();
+ vc_db_initialize();
+ }
+ return 0;
+}
+
static int __vc_db_transaction(sqlite3* db_handle, const char* transaction)
{
sqlite3_stmt* pStmt = NULL;
static int __vc_db_integrity_check_cb(void *NotUsed, int argc, char **argv, char **azColName)
{
SLOG(LOG_INFO, vc_db_tag(), "integrity check cb is called");
+
+ char *check_str = "ok";
+ if (0 != strncmp(argv[0], check_str, strlen(check_str))) {
+ SLOG(LOG_ERROR, vc_db_tag(), "db integrity result : %s", argv[0]);
+ is_db_corrupted = true;
+ return -1;
+ }
+
+ SLOG(LOG_INFO, vc_db_tag(), "db integrity result : %s", argv[0]);
return 0;
}
}
}
- if (SQLITE_CORRUPT == sqlite3_exec(g_db_handle, "pragma integrity_check", __vc_db_integrity_check_cb, NULL, NULL)) {
+ int ret = sqlite3_exec(g_db_handle, "pragma integrity_check", __vc_db_integrity_check_cb, NULL, NULL);
+ if (true == is_db_corrupted || SQLITE_CORRUPT == ret) {
SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open DB");
+ if (g_db_handle) {
+ ret = db_util_close(g_db_handle);
+ if (ret != SQLITE_OK) {
+ SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to close db, ret %d: %s", ret, sqlite3_errmsg(g_db_handle));
+ }
+ g_db_handle = NULL;
+ }
if (0 != remove(g_path)) {
SLOG(LOG_ERROR, vc_db_tag(), "[Error] remove file(%s) is failed", g_path);
g_db_cnt = (g_db_cnt + 1) % 1000;
SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to connect main DB");
return VC_DB_ERROR_OPERATION_FAILED;
}
+
+ g_ref_cnt++;
+ SLOG(LOG_INFO, vc_db_tag(), "[SUCCESS] DB initialization after restore");
+ return 0;
}
is_connect = __vc_db_connect_db(&g_backup_path, &g_db_backup_handle);
}
g_ref_cnt++;
+
+ SLOG(LOG_INFO, vc_db_tag(), "[SUCCESS] DB initialization");
return VC_DB_ERROR_NONE;
}
int vc_db_create_table()
{
+ __vc_db_reset_handle();
__vc_db_begin_transaction(g_db_handle);
int ret = __vc_db_create_table(g_db_handle, VC_INFO_TABLE);
int vc_db_delete_table(const char* table)
{
+ __vc_db_reset_handle();
__vc_db_begin_transaction(g_db_handle);
int ret = __vc_db_delete_table(g_db_handle, table);
int vc_db_begin_transaction(void)
{
+ __vc_db_reset_handle();
int ret = __vc_db_begin_transaction(g_db_handle);
return ret;
}
int vc_db_rollback_transaction(void)
{
+ __vc_db_reset_handle();
int ret = __vc_db_rollback_transaction(g_db_handle);
return ret;
}
int vc_db_commit_transaction(void)
{
+ __vc_db_reset_handle();
int ret = __vc_db_commit_transaction(g_db_handle);
return ret;
}
return ret;
}
-
static int __vc_db_insert_commands_list(sqlite3* db_handle, int pid, vc_cmd_type_e type, GSList* cmd_list, char* invocation_name, bool skip_invocation)
{
GSList *iter = NULL;
int vc_db_insert_commands_list(int pid, vc_cmd_type_e type, GSList* cmd_list, char* invocation_name, bool skip_invocation)
{
+ __vc_db_reset_handle();
__vc_db_begin_transaction(g_db_handle);
int ret = __vc_db_insert_commands_list(g_db_handle, pid, type, cmd_list, invocation_name, false);
int vc_db_get_commands(int pid, vc_cmd_type_e type, GSList** cmd_list)
{
+ __vc_db_reset_handle();
__vc_db_begin_transaction(g_db_handle);
int ret = __vc_db_get_commands(g_db_handle, pid, type, cmd_list);
LOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table");
if (NULL == vc_cmd_list) {
+ __vc_db_reset_handle();
__vc_db_begin_transaction(g_db_handle);
int ret = __vc_db_insert_result(g_db_handle, result_text, event, msg, exclusive, NULL);
if (ret != VC_DB_ERROR_NONE) {
vc_cmd_s* temp_cmd = NULL;
temp_cmd = (vc_cmd_s*)vc_command;
+ __vc_db_reset_handle();
__vc_db_begin_transaction(g_db_handle);
ret = __vc_db_insert_result(g_db_handle, result_text, event, msg, exclusive, temp_cmd);
if (ret != VC_DB_ERROR_NONE) {
int vc_db_get_result(char** result_text, int* event, char** msg, int pid, vc_cmd_list_h vc_cmd_list, bool exclusive)
{
+ __vc_db_reset_handle();
__vc_db_begin_transaction(g_db_handle);
int ret = __vc_db_get_result(g_db_handle, result_text, event, msg, pid, NULL, vc_cmd_list, exclusive);
int vc_db_get_appid_list(const char* result, GSList** app_list)
{
+ __vc_db_reset_handle();
__vc_db_begin_transaction(g_db_handle);
int ret = __vc_db_get_appid(g_db_handle, result, app_list);
int vc_db_get_result_pid_list(const char* result, GSList** pid_list)
{
+ __vc_db_reset_handle();
__vc_db_begin_transaction(g_db_handle);
int ret = __vc_db_get_result_pid_list(g_db_handle, result, pid_list);
int vc_db_append_commands(int pid, int type, vc_cmd_list_h vc_cmd_list)
{
+ __vc_db_reset_handle();
__vc_db_begin_transaction(g_db_handle);
int ret = __vc_db_append_commands(g_db_handle, pid, type, vc_cmd_list);
int vc_db_delete_commands(int pid, vc_cmd_type_e type, char* appid)
{
+ __vc_db_reset_handle();
__vc_db_begin_transaction(g_db_handle);
int ret = 0;
{
GSList* list = NULL;
+ __vc_db_reset_handle();
__vc_db_begin_transaction(g_db_backup_handle);
int ret = __vc_db_delete_table(g_db_backup_handle, VC_INFO_TABLE);
{
GSList* list = NULL;
+ __vc_db_reset_handle();
__vc_db_begin_transaction(g_db_backup_handle);
int ret = __vc_db_get_commands(g_db_backup_handle, -1, VC_COMMAND_TYPE_BACKGROUND, &list);