Add restore step in opening backend db 41/113141/6
authorJiwoong Im <jiwoong.im@samsung.com>
Mon, 6 Feb 2017 08:04:42 +0000 (17:04 +0900)
committerJiwoong Im <jiwoong.im@samsung.com>
Sun, 12 Feb 2017 23:07:39 +0000 (15:07 -0800)
- If db is corrupted, delete db file and restore to default data.

Change-Id: Ic60bf38d64ff5683790dce8d9541aa2e01e5be75
Signed-off-by: Jiwoong Im <jiwoong.im@samsung.com>
backend/sqlite.c

index 2f78e42..46fb0f3 100644 (file)
@@ -79,6 +79,25 @@ static sqlite3 *open_sqlite3(const char *dbpath, bool readonly)
        r = sqlite3_open_v2(dbpath, &db,
                        readonly ? SQLITE_OPEN_READONLY :
                        SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
+
+       if (db_exist && r == SQLITE_CORRUPT) {
+               bxt_err("Open '%s' failed: %s", dbpath, sqlite3_errmsg(db));
+
+               if (!strncmp("/etc", dbpath, 4))
+                       return NULL;
+
+               bxt_err("Try to remove %s and restore to default value.",
+                               dbpath);
+
+               if (unlink(dbpath))
+                       return NULL;
+
+               db_exist = false;
+               r = sqlite3_open_v2(dbpath, &db,
+                               readonly ? SQLITE_OPEN_READONLY :
+                               SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
+       }
+
        if (r) {
                bxt_err("Open '%s' failed: %s", dbpath, sqlite3_errmsg(db));
                errno = EIO;