fix the restore logic when db file is damaged 50/303250/1
authorSejun Park <sejun79.park@samsung.com>
Mon, 18 Dec 2023 08:56:50 +0000 (17:56 +0900)
committerTizen AI <ai.tzn.sec@samsung.com>
Fri, 22 Dec 2023 05:30:40 +0000 (14:30 +0900)
Change-Id: Ifabc36db0e6be49c8ee234269258e1617b827bd3

common/vc_cmd_db.c

index dff7ec0..6250dfb 100644 (file)
@@ -72,6 +72,7 @@ static int __vc_db_begin_transaction(sqlite3* db_handle);
 static int __vc_db_create_table(sqlite3* db_handle, const char* table);
 static int __vc_db_rollback_transaction(sqlite3* db_handle);
 static int __vc_db_commit_transaction(sqlite3* db_handle);
+static int __vc_db_restore_table(sqlite3* db_handle, const char* table);
 
 
 int __vc_db_reset_handle(void)
@@ -1528,6 +1529,24 @@ static int __vc_db_open_db_for_daemon(const char* path, sqlite3** db_handle)
                return VC_DB_ERROR_OPERATION_FAILED;
        }
 
+       ret = sqlite3_exec(*db_handle, "PRAGMA integrity_check", 0, 0, 0);
+       if (SQLITE_OK != ret) {
+               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open DB for daemon");
+
+               ret = db_util_close(*db_handle);
+               if (ret != SQLITE_OK) {
+                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to close db, ret %d: %s for daemon", ret, sqlite3_errmsg(*db_handle));
+               }
+               *db_handle = NULL;
+
+               if (0 != remove(path)) {
+                       SLOG(LOG_ERROR, vc_db_tag(), "[Error] remove file(%s) is failed for daemon", path);
+                       g_db_cnt = (g_db_cnt + 1) % 1000;
+                       snprintf((char *)path, _POSIX_PATH_MAX, "%s/.vc_info_%d.db", VC_RUNTIME_INFO_ROOT, g_db_cnt);
+               }
+               return VC_DB_ERROR_OPERATION_FAILED;
+       }
+
        if (!stat.st_size) {
                __vc_db_begin_transaction(*db_handle);
 
@@ -1544,6 +1563,18 @@ static int __vc_db_open_db_for_daemon(const char* path, sqlite3** db_handle)
                        __vc_db_rollback_transaction(*db_handle);
                        return VC_DB_ERROR_OPERATION_FAILED;
                }
+               ret = __vc_db_restore_table(*db_handle, VC_INFO_TABLE);
+               if (0 != ret) {
+                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table (%s), %d", VC_INFO_TABLE, ret);
+                       __vc_db_rollback_transaction(*db_handle);
+                       return VC_DB_ERROR_OPERATION_FAILED;
+               }
+               ret = __vc_db_restore_table(*db_handle, VC_RESULT_TABLE);
+               if (0 != ret) {
+                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table (%s), %d", VC_RESULT_TABLE, ret);
+                       __vc_db_rollback_transaction(*db_handle);
+                       return VC_DB_ERROR_OPERATION_FAILED;
+               }
 
                __vc_db_commit_transaction(*db_handle);
        }
@@ -1603,47 +1634,6 @@ static bool __vc_db_connect_db_for_daemon(const char* path, sqlite3** db_handle)
        return is_connect;
 }
 
-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");
-
-       int ret;
-       char *check_str = "ok";
-       if (0 != strncmp(argv[0], check_str, strlen(check_str))) {
-               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Doesn't have integrity(%s), retry to connect after removing", argv[0]);
-               if (0 != remove(g_path)) {
-                       SLOG(LOG_ERROR, vc_db_tag(), "[Error] remove file(%s) is failed for daemon", g_path);
-                       g_db_cnt = (g_db_cnt + 1) % 1000;
-                       snprintf(g_path, _POSIX_PATH_MAX, "%s/.vc_info_%d.db", VC_RUNTIME_INFO_ROOT, g_db_cnt);
-               }
-               bool is_connect = __vc_db_connect_db_for_daemon(g_path, &g_db_handle);
-               if (true == is_connect) {
-                       SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect main DB for daemon");
-                       ret = __vc_db_restore_table(g_db_handle, VC_INFO_TABLE);
-                       if (0 != ret) {
-                               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table (%s)", VC_INFO_TABLE);
-                       }
-                       ret = __vc_db_restore_table(g_db_handle, VC_RESULT_TABLE);
-                       if (0 != ret) {
-                               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table (%s)", VC_RESULT_TABLE);
-                       }
-                       is_connect = __vc_db_connect_db_for_daemon(g_backup_path, &g_db_backup_handle);
-                       if (true == is_connect) {
-                               SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect backup DB for daemon");
-                               if (0 != vc_db_restore_command()) {
-                                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command");
-                               }
-                       }
-               } else {
-                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to connect main DB for daemon");
-                       return VC_DB_ERROR_OPERATION_FAILED;
-               }
-       }
-
-       SLOG(LOG_INFO, vc_db_tag(), "db integrity result : %s", argv[0]);
-       return VC_DB_ERROR_NONE;
-}
-
 int vc_db_initialize_for_daemon(void)
 {
        SLOG(LOG_INFO, vc_db_tag(), "DB on initialization for daemon");
@@ -1683,42 +1673,6 @@ int vc_db_initialize_for_daemon(void)
                        return VC_DB_ERROR_OPERATION_FAILED;
                }
        }
-
-       int ret = sqlite3_exec(g_db_handle, "pragma integrity_check", __vc_db_integrity_check_cb, NULL, NULL);
-       if (SQLITE_CORRUPT == ret) {
-               SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open DB for daemon");
-
-               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 for daemon", 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 for daemon", g_path);
-                       g_db_cnt = (g_db_cnt + 1) % 1000;
-                       snprintf(g_path, _POSIX_PATH_MAX, "%s/.vc_info_%d.db", VC_RUNTIME_INFO_ROOT, g_db_cnt);
-               }
-               is_connect = __vc_db_connect_db_for_daemon(g_path, &g_db_handle);
-               if (true == is_connect) {
-                       SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect main DB for daemon");
-                       is_connect = __vc_db_connect_db_for_daemon(g_backup_path, &g_db_backup_handle);
-                       if (true == is_connect) {
-                               SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect backup DB for daemon");
-                               if (0 != vc_db_restore_command()) {
-                                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command for daemon");
-                               }
-                       }
-               } else {
-                       SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to connect main DB for daemon");
-                       return VC_DB_ERROR_OPERATION_FAILED;
-               }
-
-               g_ref_cnt++;
-               SLOG(LOG_INFO, vc_db_tag(), "[SUCCESS] DB initialization after restore for daemon");
-               return VC_DB_ERROR_NONE;
-       }
-
        is_connect = __vc_db_connect_db_for_daemon(g_backup_path, &g_db_backup_handle);
        if (false == is_connect) {
                SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open backup DB, retry to connect after removing file for daemon");