}
}
+static int __check_integrity(sqlite3 *handle)
+{
+ sqlite3_stmt *stmt = NULL;
+
+ if (handle == 0) {
+ TRACE_ERROR("failed to check handle");
+ return -1;
+ }
+
+ TRACE_INFO("");
+ int ret = sqlite3_prepare_v2(handle, "PRAGMA integrity_check", -1, &stmt, NULL);
+ if (ret != SQLITE_OK && ret != SQLITE_BUSY) {
+ TRACE_ERROR("failed to check integrity:%s", sqlite3_errmsg(handle));
+ return -1;
+ } else {
+ ret = sqlite3_step(stmt);
+ if (ret == SQLITE_ROW) {
+ const char *ret_val = (const char *)sqlite3_column_text(stmt, 0);
+ TRACE_INFO("ret_val: %s", ret_val);
+ if (ret_val && strcmp(ret_val, "ok") == 0) {
+ return 0;
+ } else {
+ TRACE_ERROR("failed to check integrity");
+ return -1;
+ }
+ } else {
+ return 0;
+ }
+ }
+ return 0;
+}
+
static int __check_table(sqlite3 *handle, char *table)
{
sqlite3_stmt *stmt = NULL;
if (__db_open((sqlite3 **)handle, database, errorcode) < 0) {
TRACE_ERROR("failed to open clients database file");
*handle = 0;
- } else {
- // whenever open new handle, check all tables. it's simple
- if (__rebuild_client_manager_tables(*handle) < 0) {
- *errorcode = DP_ERROR_NO_SPACE;
- dp_db_close(*handle);
- *handle = 0;
- }
- }
- sqlite3_free(database);
- }
+ } else {
+ if (__check_integrity(*handle) < 0) {
+ // recovery database file
+ TRACE_ERROR("Failed to check integrity: %s", database);
+ unlink(database);
+ *handle = 0;
+ if (__db_open((sqlite3 **)handle, database, errorcode) < 0) {
+ TRACE_ERROR("failed to open clients database file");
+ *handle = 0;
+ }
+ }
+
+ // whenever open new handle, check all tables. it's simple
+ if (*handle && __rebuild_client_manager_tables(*handle) < 0) {
+ *errorcode = DP_ERROR_NO_SPACE;
+ dp_db_close(*handle);
+ *handle = 0;
+ }
+ }
+ sqlite3_free(database);
+ }
return *handle ? 0 : -1;
}