diff options
author | Seungha Son <seungha.son@samsung.com> | 2017-11-28 18:48:57 +0900 |
---|---|---|
committer | Seungha Son <seungha.son@samsung.com> | 2017-11-29 14:35:55 +0900 |
commit | d1bf08388a93c4b2ea008a45bda4d13f8489721c (patch) | |
tree | e013c2cf711b7e33870ef3a90cfa0dc45331d849 | |
parent | 3aeba618890330efa00cf1831e9f285fbc0db66f (diff) | |
download | shortcut-d1bf08388a93c4b2ea008a45bda4d13f8489721c.tar.gz shortcut-d1bf08388a93c4b2ea008a45bda4d13f8489721c.tar.bz2 shortcut-d1bf08388a93c4b2ea008a45bda4d13f8489721c.zip |
Fix logic to recover corrupted db
Signed-off-by: Seungha Son <seungha.son@samsung.com>
Change-Id: I40859b001ce53cbaa5fcdfdb907b9869ac9c8280
-rwxr-xr-x | lib/src/shortcut_db.c | 112 |
1 files changed, 62 insertions, 50 deletions
diff --git a/lib/src/shortcut_db.c b/lib/src/shortcut_db.c index fd73d8b..0520fb4 100755 --- a/lib/src/shortcut_db.c +++ b/lib/src/shortcut_db.c @@ -29,7 +29,6 @@ #define QUERY_CREATE_SHORTCUT_TABLE \ "PRAGMA journal_mode = PERSIST;" \ - "PRAGMA integrity_check;" \ "CREATE TABLE IF NOT EXISTS shortcut_service (\n" \ " id INTEGER PRIMARY KEY AUTOINCREMENT,\n" \ " pkgid TEXT,\n" \ @@ -45,6 +44,54 @@ " name TEXT,\n" \ " icon TEXT);" +static bool is_db_corrupted = false; + +static int __check_integrity_cb(void *pid, int argc, char **argv, char **notUsed) +{ + if (!strcmp(argv[0], "ok")) { + SHORTCUT_ERR("db integrity result : %s" , argv[0]); + is_db_corrupted = true; + return -1; + } + + SHORTCUT_INFO("db integrity result : %s" , argv[0]); + return 0; +} + +static int __recover_corrupted_db(sqlite3 *db) +{ + int ret = SHORTCUT_ERROR_NONE; + int sql_ret; + char *errmsg = NULL; + + SHORTCUT_INFO("DB is corrupted, start to recover corrupted db"); + if (db) + sqlite3_close(db); + unlink(DB_PATH); + + sql_ret = sqlite3_open_v2(DB_PATH, &db, + SQLITE_OPEN_CREATE |SQLITE_OPEN_READWRITE, + NULL); + if (sql_ret != SQLITE_OK) { + SHORTCUT_ERR("Failed to open db[%d]", sql_ret); + unlink(DB_PATH); + ret = SHORTCUT_ERROR_IO_ERROR; + goto out; + } + + sql_ret = sqlite3_exec(db, QUERY_CREATE_SHORTCUT_TABLE, NULL, NULL, &errmsg); + if (sql_ret != SQLITE_OK) { + SHORTCUT_ERR("Failed to exec query[%d][%s]", sql_ret, errmsg); + ret = SHORTCUT_ERROR_IO_ERROR; + } + +out: + if (errmsg) + sqlite3_free(errmsg); + + return ret; +} + EAPI int shortcut_db_init() { int ret = SHORTCUT_ERROR_NONE; @@ -56,63 +103,28 @@ EAPI int shortcut_db_init() SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL); if (sql_ret != SQLITE_OK) { SHORTCUT_ERR("Failed to open db[%d]", sql_ret); - - if (sqlite3_errcode(db) == SQLITE_CORRUPT) { - if (db) - sqlite3_close(db); - unlink(DB_PATH); - - sql_ret = sqlite3_open_v2(DB_PATH, &db, - SQLITE_OPEN_CREATE |SQLITE_OPEN_READWRITE, - NULL); - if (sql_ret != SQLITE_OK) { - SHORTCUT_ERR("Failed to open db[%d]", sql_ret); - unlink(DB_PATH); - ret = SHORTCUT_ERROR_IO_ERROR; - goto out; - } - } else { - ret = SHORTCUT_ERROR_IO_ERROR; - goto out; - } + ret = SHORTCUT_ERROR_IO_ERROR; + goto out; } - sql_ret = sqlite3_exec(db, QUERY_CREATE_SHORTCUT_TABLE, NULL, NULL, - &errmsg); + sql_ret = sqlite3_exec(db, QUERY_CREATE_SHORTCUT_TABLE, + NULL, NULL, &errmsg); if (sql_ret != SQLITE_OK) { SHORTCUT_ERR("Failed to exec query [%d][%s]", sql_ret, errmsg); + ret = SHORTCUT_ERROR_IO_ERROR; + goto out; + } - if (sql_ret == SQLITE_CORRUPT || sql_ret == SQLITE_NOTADB) { - sqlite3_close(db); - unlink(DB_PATH); - - sql_ret = sqlite3_open_v2(DB_PATH, &db, - SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, - NULL); - if (sql_ret != SQLITE_OK) { - SHORTCUT_ERR("Failed to open db[%d]", sql_ret); - unlink(DB_PATH); - ret = SHORTCUT_ERROR_IO_ERROR; - goto out; - } - - sqlite3_free(errmsg); - sql_ret = sqlite3_exec(db, QUERY_CREATE_SHORTCUT_TABLE, - NULL, NULL, &errmsg); - if (sql_ret != SQLITE_OK) { - SHORTCUT_ERR("Failed to exec sqlite, again[%d][%s]", - sql_ret, errmsg); - unlink(DB_PATH); - ret = SHORTCUT_ERROR_IO_ERROR; - goto out; - } - } else { - ret = SHORTCUT_ERROR_IO_ERROR; - goto out; - } + sql_ret = sqlite3_exec(db, "PRAGMA integrity_check", + __check_integrity_cb, NULL, &errmsg); + if (sql_ret != SQLITE_OK || is_db_corrupted) { + SHORTCUT_ERR("Failed to exec query[%d][%s]", sql_ret, errmsg); + ret = SHORTCUT_ERROR_IO_ERROR; } out: + if (sql_ret == SQLITE_CORRUPT || sql_ret == SQLITE_NOTADB || is_db_corrupted) + ret = __recover_corrupted_db(db); if (errmsg) sqlite3_free(errmsg); if (db) |