diff options
author | Myungki Lee <mk5004.lee@samsung.com> | 2017-10-26 12:24:51 +0900 |
---|---|---|
committer | Myungki Lee <mk5004.lee@samsung.com> | 2017-10-31 10:17:43 +0900 |
commit | 3dd75c48e713fd61f24357955a20b155ab8ca15b (patch) | |
tree | 974b574e99ebfe6a720d5389666259d0001468e5 | |
parent | 28218d6ea86e35695396a4ead81c1f8ca4314bc2 (diff) | |
download | notification-3dd75c48e713fd61f24357955a20b155ab8ca15b.tar.gz notification-3dd75c48e713fd61f24357955a20b155ab8ca15b.tar.bz2 notification-3dd75c48e713fd61f24357955a20b155ab8ca15b.zip |
Add DB validation check when db init
- Added integrity check related logic to db init process to check
if db integrity is broken due to file system error and initialize db.
Change-Id: I8e7f43cdee04e488442556b21f22a14d71230906
Signed-off-by: Myungki Lee <mk5004.lee@samsung.com>
-rwxr-xr-x | src/notification_db.c | 49 |
1 files changed, 45 insertions, 4 deletions
diff --git a/src/notification_db.c b/src/notification_db.c index 19ee070..ff37d90 100755 --- a/src/notification_db.c +++ b/src/notification_db.c @@ -38,8 +38,24 @@ EXPORT_API int notification_db_init() if (ret != SQLITE_OK) { /* LCOV_EXCL_START */ NOTIFICATION_ERR("Failed to open db[%d]", ret); - ret = NOTIFICATION_ERROR_FROM_DB; - goto out; + + if (sqlite3_errcode(db) == SQLITE_CORRUPT) { + if (db) + sqlite3_close(db); + unlink(DBPATH); + + ret = sqlite3_open_v2(DBPATH, &db, SQLITE_OPEN_CREATE | + SQLITE_OPEN_READWRITE, NULL); + if (ret != SQLITE_OK) { + NOTIFICATION_ERR("Failed to open db[%d]", ret); + unlink(DBPATH); + ret = NOTIFICATION_ERROR_FROM_DB; + goto out; + } + } else { + ret = NOTIFICATION_ERROR_FROM_DB; + goto out; + } /* LCOV_EXCL_STOP */ } @@ -47,8 +63,33 @@ EXPORT_API int notification_db_init() if (ret != SQLITE_OK) { /* LCOV_EXCL_START */ NOTIFICATION_ERR("Failed to exec sqlite[%d][%s]", ret, errmsg); - ret = NOTIFICATION_ERROR_FROM_DB; - goto out; + + if (ret == SQLITE_CORRUPT || ret == SQLITE_NOTADB) { + sqlite3_close(db); + unlink(DBPATH); + ret = sqlite3_open_v2(DBPATH, &db, SQLITE_OPEN_CREATE | + SQLITE_OPEN_READWRITE, NULL); + if (ret != SQLITE_OK) { + NOTIFICATION_ERR("Failed to open db[%d]", ret); + unlink(DBPATH); + ret = NOTIFICATION_ERROR_FROM_DB; + goto out; + } + + sqlite3_free(errmsg); + ret = sqlite3_exec(db, CREATE_NOTIFICATION_TABLE, NULL, + NULL, &errmsg); + if (ret != SQLITE_OK) { + NOTIFICATION_ERR("Failed to exec sqlite, \ + again[%d][%s]", ret, errmsg); + unlink(DBPATH); + ret = NOTIFICATION_ERROR_FROM_DB; + goto out; + } + } else { + ret = NOTIFICATION_ERROR_FROM_DB; + goto out; + } /* LCOV_EXCL_STOP */ } |