diff --git a/tddb/td/db/SqliteDb.cpp b/tddb/td/db/SqliteDb.cpp index 656a633c0..c8bbc748a 100644 --- a/tddb/td/db/SqliteDb.cpp +++ b/tddb/td/db/SqliteDb.cpp @@ -150,23 +150,21 @@ Status SqliteDb::commit_transaction() { return Status::OK(); } -bool SqliteDb::is_encrypted() { - return exec("SELECT count(*) FROM sqlite_master").is_error(); +Status SqliteDb::check_encryption() { + return exec("SELECT count(*) FROM sqlite_master"); } Result SqliteDb::open_with_key(CSlice path, const DbKey &db_key) { SqliteDb db; TRY_STATUS(db.init(path)); if (!db_key.is_empty()) { - if (!db.is_encrypted()) { + if (db.check_encryption().is_ok()) { return Status::Error("No key is needed"); } auto key = db_key_to_sqlcipher_key(db_key); TRY_STATUS(db.exec(PSLICE() << "PRAGMA key = " << key)); } - if (db.is_encrypted()) { - return Status::Error("Wrong key or database is corrupted"); - } + TRY_STATUS_PREFIX(db.check_encryption(), "Can't open database: "); return std::move(db); } diff --git a/tddb/td/db/SqliteDb.h b/tddb/td/db/SqliteDb.h index 057a5e4a4..9f7955bd4 100644 --- a/tddb/td/db/SqliteDb.h +++ b/tddb/td/db/SqliteDb.h @@ -81,7 +81,7 @@ class SqliteDb { } std::shared_ptr raw_; - bool is_encrypted(); + Status check_encryption(); }; } // namespace td