From: Myungki Lee Date: Thu, 26 Oct 2017 03:24:51 +0000 (+0900) Subject: Add DB validation check when db init X-Git-Tag: submit/tizen/20171102.093920~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=3dd75c48e713fd61f24357955a20b155ab8ca15b;p=platform%2Fcore%2Fapi%2Fnotification.git 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 --- diff --git a/src/notification_db.c b/src/notification_db.c index 19ee0701..ff37d90a 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 */ }