diff --git a/native/cpp/CommonCpp/DatabaseManagers/SQLiteBackup.h b/native/cpp/CommonCpp/DatabaseManagers/SQLiteBackup.h --- a/native/cpp/CommonCpp/DatabaseManagers/SQLiteBackup.h +++ b/native/cpp/CommonCpp/DatabaseManagers/SQLiteBackup.h @@ -16,6 +16,11 @@ static void cleanupDatabaseExceptAllowlist(sqlite3 *db); + // Checks if file exists and if database is queryable + static void validateMainCompaction( + std::string mainCompactionPath, + std::string mainCompactionEncryptionKey); + // Restores a database from a main compaction file that was previously backed // up. // This function takes a path to the encrypted main compaction file, decrypts diff --git a/native/cpp/CommonCpp/DatabaseManagers/SQLiteBackup.cpp b/native/cpp/CommonCpp/DatabaseManagers/SQLiteBackup.cpp --- a/native/cpp/CommonCpp/DatabaseManagers/SQLiteBackup.cpp +++ b/native/cpp/CommonCpp/DatabaseManagers/SQLiteBackup.cpp @@ -43,25 +43,31 @@ } } -std::string SQLiteBackup::restoreFromMainCompaction( +void SQLiteBackup::validateMainCompaction( std::string mainCompactionPath, - std::string mainCompactionEncryptionKey, - std::optional plaintextDatabasePath, - std::string maxVersion) { - + std::string mainCompactionEncryptionKey) { if (!SQLiteUtils::fileExists(mainCompactionPath)) { std::string errorMessage{"Restore attempt but backup file does not exist"}; Logger::log(errorMessage); throw std::runtime_error(errorMessage); } - sqlite3 *backupDB; if (!SQLiteUtils::isDatabaseQueryable( - backupDB, true, mainCompactionPath, mainCompactionEncryptionKey)) { + true, mainCompactionPath, mainCompactionEncryptionKey)) { std::string errorMessage{"Backup file or encryption key corrupted"}; Logger::log(errorMessage); throw std::runtime_error(errorMessage); } +} + +std::string SQLiteBackup::restoreFromMainCompaction( + std::string mainCompactionPath, + std::string mainCompactionEncryptionKey, + std::optional plaintextDatabasePath, + std::string maxVersion) { + + SQLiteBackup::validateMainCompaction( + mainCompactionPath, mainCompactionEncryptionKey); std::string plaintextBackupPath = plaintextDatabasePath.value_or(mainCompactionPath + "_plaintext"); @@ -72,6 +78,7 @@ "attempt."); } + sqlite3 *backupDB; sqlite3_open(mainCompactionPath.c_str(), &backupDB); std::string plaintextMigrationDBQuery = "PRAGMA key = \"x'" + mainCompactionEncryptionKey + diff --git a/native/cpp/CommonCpp/DatabaseManagers/SQLiteUtils.h b/native/cpp/CommonCpp/DatabaseManagers/SQLiteUtils.h --- a/native/cpp/CommonCpp/DatabaseManagers/SQLiteUtils.h +++ b/native/cpp/CommonCpp/DatabaseManagers/SQLiteUtils.h @@ -21,7 +21,6 @@ const std::string &newPath, const char *errorMessage); static bool isDatabaseQueryable( - sqlite3 *db, bool use_encryption_key, const std::string &path, const std::string &encryptionKey); diff --git a/native/cpp/CommonCpp/DatabaseManagers/SQLiteUtils.cpp b/native/cpp/CommonCpp/DatabaseManagers/SQLiteUtils.cpp --- a/native/cpp/CommonCpp/DatabaseManagers/SQLiteUtils.cpp +++ b/native/cpp/CommonCpp/DatabaseManagers/SQLiteUtils.cpp @@ -102,11 +102,11 @@ } bool SQLiteUtils::isDatabaseQueryable( - sqlite3 *db, bool useEncryptionKey, const std::string &path, const std::string &encryptionKey) { char *err_msg; + sqlite3 *db; sqlite3_open(path.c_str(), &db); // According to SQLCipher documentation running some SELECT is the only way to // check for key validity @@ -150,16 +150,13 @@ return; } - sqlite3 *db; - if (SQLiteUtils::isDatabaseQueryable( - db, true, sqliteFilePath, encryptionKey)) { + if (SQLiteUtils::isDatabaseQueryable(true, sqliteFilePath, encryptionKey)) { Logger::log( "Database exists under default path and it is correctly encrypted."); return; } - if (!SQLiteUtils::isDatabaseQueryable( - db, false, sqliteFilePath, encryptionKey)) { + if (!SQLiteUtils::isDatabaseQueryable(false, sqliteFilePath, encryptionKey)) { Logger::log( "Database exists but it is encrypted with key that was lost. " "Attempting database deletion. New encrypted one will be created."); @@ -172,6 +169,8 @@ "Database exists but it is not encrypted. Attempting encryption " "process."); } + + sqlite3 *db; sqlite3_open(sqliteFilePath.c_str(), &db); std::string createEncryptedCopySQL = "ATTACH DATABASE '" + diff --git a/web/shared-worker/_generated/comm_query_executor.wasm b/web/shared-worker/_generated/comm_query_executor.wasm index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 GIT binary patch literal 0 Hc$@