Better error message.

GitOrigin-RevId: 2779dcf8fa07fbc2e191141774b34fb9c2282b64
This commit is contained in:
levlam 2020-01-27 15:12:22 +03:00
parent e60cf58d68
commit 38b3c1b52c
2 changed files with 5 additions and 7 deletions

View File

@ -150,23 +150,21 @@ Status SqliteDb::commit_transaction() {
return Status::OK(); return Status::OK();
} }
bool SqliteDb::is_encrypted() { Status SqliteDb::check_encryption() {
return exec("SELECT count(*) FROM sqlite_master").is_error(); return exec("SELECT count(*) FROM sqlite_master");
} }
Result<SqliteDb> SqliteDb::open_with_key(CSlice path, const DbKey &db_key) { Result<SqliteDb> SqliteDb::open_with_key(CSlice path, const DbKey &db_key) {
SqliteDb db; SqliteDb db;
TRY_STATUS(db.init(path)); TRY_STATUS(db.init(path));
if (!db_key.is_empty()) { if (!db_key.is_empty()) {
if (!db.is_encrypted()) { if (db.check_encryption().is_ok()) {
return Status::Error("No key is needed"); return Status::Error("No key is needed");
} }
auto key = db_key_to_sqlcipher_key(db_key); auto key = db_key_to_sqlcipher_key(db_key);
TRY_STATUS(db.exec(PSLICE() << "PRAGMA key = " << key)); TRY_STATUS(db.exec(PSLICE() << "PRAGMA key = " << key));
} }
if (db.is_encrypted()) { TRY_STATUS_PREFIX(db.check_encryption(), "Can't open database: ");
return Status::Error("Wrong key or database is corrupted");
}
return std::move(db); return std::move(db);
} }

View File

@ -81,7 +81,7 @@ class SqliteDb {
} }
std::shared_ptr<detail::RawSqliteDb> raw_; std::shared_ptr<detail::RawSqliteDb> raw_;
bool is_encrypted(); Status check_encryption();
}; };
} // namespace td } // namespace td