Check integrity of db file 53/160453/3
authorSeonah Moon <seonah1.moon@samsung.com>
Thu, 16 Nov 2017 07:56:47 +0000 (16:56 +0900)
committerSeonah Moon <seonah1.moon@samsung.com>
Mon, 20 Nov 2017 01:15:05 +0000 (10:15 +0900)
Change-Id: If81942d173abbb1440b72e0253a5a8185df64584
Signed-off-by: Seonah Moon <seonah1.moon@samsung.com>
packaging/download-provider.spec
provider/download-provider-db.c

index ae384be..14c90f8 100755 (executable)
@@ -1,7 +1,7 @@
 %define _ux_define tizen2.3
 Name:       download-provider
 Summary:    Download the contents in background
-Version:    2.1.87
+Version:    2.1.88
 Release:    0
 Group:      Development/Libraries
 License:    Apache-2.0
index 8e1d06d..80185eb 100755 (executable)
@@ -47,6 +47,38 @@ static void __dp_finalize(sqlite3_stmt *stmt)
        }
 }
 
+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;
@@ -183,16 +215,27 @@ int dp_db_open_client_manager(void **handle, int *errorcode)
         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;
 }