diff --git a/frameworks/libs/distributeddb/interfaces/include/kv_store_nb_delegate.h b/frameworks/libs/distributeddb/interfaces/include/kv_store_nb_delegate.h index 5a4debaa5079d27a85a3d02dd8c60ac5fc3c4951..ccc9b6fb66f1f0eef367fc72b2c377dadc29390c 100644 --- a/frameworks/libs/distributeddb/interfaces/include/kv_store_nb_delegate.h +++ b/frameworks/libs/distributeddb/interfaces/include/kv_store_nb_delegate.h @@ -169,7 +169,8 @@ public: // Import the existing database files to the specified database file in the specified directory. // Warning Import may reopen database file in locked state - DB_API virtual DBStatus Import(const std::string &filePath, const CipherPassword &passwd) = 0; + DB_API virtual DBStatus Import(const std::string &filePath, const CipherPassword &passwd, + bool isNeedIntegrityCheck = false) = 0; // Start a transaction DB_API virtual DBStatus StartTransaction() = 0; diff --git a/frameworks/libs/distributeddb/interfaces/src/kv_store_nb_delegate_impl.cpp b/frameworks/libs/distributeddb/interfaces/src/kv_store_nb_delegate_impl.cpp index 298f3a29b611aa39ae36fe33e2fe5963e77ef89b..57e32c9706dfc058f767389d7853d544327f339a 100644 --- a/frameworks/libs/distributeddb/interfaces/src/kv_store_nb_delegate_impl.cpp +++ b/frameworks/libs/distributeddb/interfaces/src/kv_store_nb_delegate_impl.cpp @@ -789,7 +789,8 @@ DBStatus KvStoreNbDelegateImpl::Export(const std::string &filePath, const Cipher return TransferDBErrno(errCode); } -DBStatus KvStoreNbDelegateImpl::Import(const std::string &filePath, const CipherPassword &passwd) +DBStatus KvStoreNbDelegateImpl::Import(const std::string &filePath, const CipherPassword &passwd, + bool isNeedIntegrityCheck) { if (conn_ == nullptr) { LOGE("%s", INVALID_CONNECTION); @@ -811,7 +812,7 @@ DBStatus KvStoreNbDelegateImpl::Import(const std::string &filePath, const Cipher return INVALID_FILE; } - int errCode = conn_->Import(canonicalUrl, passwd); + int errCode = conn_->Import(canonicalUrl, passwd, isNeedIntegrityCheck); if (errCode == E_OK) { LOGI("[KvStoreNbDelegate] Import ok"); return OK; diff --git a/frameworks/libs/distributeddb/interfaces/src/kv_store_nb_delegate_impl.h b/frameworks/libs/distributeddb/interfaces/src/kv_store_nb_delegate_impl.h index f166d7b37d864094621065db179fd2d358bad37e..8f897ee9c0a4de20dbce1fe7c840581c6897c861 100644 --- a/frameworks/libs/distributeddb/interfaces/src/kv_store_nb_delegate_impl.h +++ b/frameworks/libs/distributeddb/interfaces/src/kv_store_nb_delegate_impl.h @@ -101,7 +101,8 @@ public: DBStatus Export(const std::string &filePath, const CipherPassword &passwd, bool force) override; // Import the existing database files to the specified database file in the specified directory. - DBStatus Import(const std::string &filePath, const CipherPassword &passwd) override; + DBStatus Import(const std::string &filePath, const CipherPassword &passwd, + bool isNeedIntegrityCheck = false) override; // Start a transaction DBStatus StartTransaction() override; diff --git a/frameworks/libs/distributeddb/storage/include/ikvdb_connection.h b/frameworks/libs/distributeddb/storage/include/ikvdb_connection.h index dd55180a6d4242092bbb8100b8bd8bb41ce1ba8a..ea3fda638e69eafe29334026a4c4cff543d4f423 100644 --- a/frameworks/libs/distributeddb/storage/include/ikvdb_connection.h +++ b/frameworks/libs/distributeddb/storage/include/ikvdb_connection.h @@ -115,7 +115,8 @@ public: virtual int Export(const std::string &filePath, const CipherPassword &passwd) = 0; // Import the existing database files to the specified database file in the specified directory. - virtual int Import(const std::string &filePath, const CipherPassword &passwd) = 0; + virtual int Import(const std::string &filePath, const CipherPassword &passwd, + bool isNeedIntegrityCheck = false) = 0; // Get the result set virtual int GetResultSet(const IOption &option, const Key &keyPrefix, IKvDBResultSet *&resultSet) const = 0; diff --git a/frameworks/libs/distributeddb/storage/src/gaussdb_rd/rd_single_ver_natural_store.cpp b/frameworks/libs/distributeddb/storage/src/gaussdb_rd/rd_single_ver_natural_store.cpp index b99d8f84b17825159d5cfa350fa0d74bad6fb09c..202445c7ade7741766a36a54f9642942fac81c69 100644 --- a/frameworks/libs/distributeddb/storage/src/gaussdb_rd/rd_single_ver_natural_store.cpp +++ b/frameworks/libs/distributeddb/storage/src/gaussdb_rd/rd_single_ver_natural_store.cpp @@ -311,7 +311,8 @@ int RdSingleVerNaturalStore::PreCheckRdImport(std::string &storePath) return E_OK; } -int RdSingleVerNaturalStore::Import(const std::string &filePath, const CipherPassword &passwd) +int RdSingleVerNaturalStore::Import(const std::string &filePath, const CipherPassword &passwd, + bool isNeedIntegrityCheck) { int errCode = E_OK; std::string storePath; diff --git a/frameworks/libs/distributeddb/storage/src/gaussdb_rd/rd_single_ver_natural_store.h b/frameworks/libs/distributeddb/storage/src/gaussdb_rd/rd_single_ver_natural_store.h index 4cb8397dae0380483f67c9d9d002d2dfec69f2d3..0fbfe89685a766f755e788fa02851c9994cc46bd 100644 --- a/frameworks/libs/distributeddb/storage/src/gaussdb_rd/rd_single_ver_natural_store.h +++ b/frameworks/libs/distributeddb/storage/src/gaussdb_rd/rd_single_ver_natural_store.h @@ -78,7 +78,7 @@ public: int Export(const std::string &filePath, const CipherPassword &passwd) override; - int Import(const std::string &filePath, const CipherPassword &passwd) override; + int Import(const std::string &filePath, const CipherPassword &passwd, bool isNeedIntegrityCheck = false) override; RdSingleVerStorageExecutor *GetHandle(bool isWrite, int &errCode, OperatePerm perm = OperatePerm::NORMAL_PERM) const; diff --git a/frameworks/libs/distributeddb/storage/src/gaussdb_rd/rd_single_ver_natural_store_connection.cpp b/frameworks/libs/distributeddb/storage/src/gaussdb_rd/rd_single_ver_natural_store_connection.cpp index 6784b71ff871d83ba1353dbea5db589bd68db64e..b3b3615d924bcfe27c2c8f5d436b3ab588ac8f3f 100644 --- a/frameworks/libs/distributeddb/storage/src/gaussdb_rd/rd_single_ver_natural_store_connection.cpp +++ b/frameworks/libs/distributeddb/storage/src/gaussdb_rd/rd_single_ver_natural_store_connection.cpp @@ -392,7 +392,8 @@ int RdSingleVerNaturalStoreConnection::Export(const std::string &filePath, const return kvDB_->Export(filePath, passwd); } -int RdSingleVerNaturalStoreConnection::Import(const std::string &filePath, const CipherPassword &passwd) +int RdSingleVerNaturalStoreConnection::Import(const std::string &filePath, const CipherPassword &passwd, + bool isNeedIntegrityCheck) { // not support passwd if (passwd.GetSize() != 0) { @@ -405,7 +406,7 @@ int RdSingleVerNaturalStoreConnection::Import(const std::string &filePath, const if (errCode != E_OK) { return errCode; } - errCode = kvDB_->Import(filePath, passwd); + errCode = kvDB_->Import(filePath, passwd, isNeedIntegrityCheck); if ((errCode == -E_INVALID_PASSWD_OR_CORRUPTED_DB) || (errCode == -E_UNEXPECTED_DATA)) { errCode = -E_INVALID_FILE; // import damaged file or txt file, return -E_INVALID_FILE } diff --git a/frameworks/libs/distributeddb/storage/src/gaussdb_rd/rd_single_ver_natural_store_connection.h b/frameworks/libs/distributeddb/storage/src/gaussdb_rd/rd_single_ver_natural_store_connection.h index 5b71d1ebec0c84bcb17ba20c854101fb209ed8b8..96c1490d774600e9100f535aed9628beba3156b7 100644 --- a/frameworks/libs/distributeddb/storage/src/gaussdb_rd/rd_single_ver_natural_store_connection.h +++ b/frameworks/libs/distributeddb/storage/src/gaussdb_rd/rd_single_ver_natural_store_connection.h @@ -77,7 +77,7 @@ public: int Export(const std::string &filePath, const CipherPassword &passwd) override; - int Import(const std::string &filePath, const CipherPassword &passwd) override; + int Import(const std::string &filePath, const CipherPassword &passwd, bool isNeedIntegrityCheck = false) override; // Get the result set int GetResultSet(const IOption &option, const Key &keyPrefix, IKvDBResultSet *&resultSet) const override; diff --git a/frameworks/libs/distributeddb/storage/src/kv/generic_kvdb.h b/frameworks/libs/distributeddb/storage/src/kv/generic_kvdb.h index 7c32c3be4a8397b390485ab2fb6feefefb4dba4b..a34873e53bcbadbd5ca4fa5a7989365aa1b42d62 100644 --- a/frameworks/libs/distributeddb/storage/src/kv/generic_kvdb.h +++ b/frameworks/libs/distributeddb/storage/src/kv/generic_kvdb.h @@ -85,7 +85,8 @@ public: virtual int Export(const std::string &filePath, const CipherPassword &passwd) = 0; // Import the existing database files to the specified database file in the specified directory. - virtual int Import(const std::string &filePath, const CipherPassword &passwd) = 0; + virtual int Import(const std::string &filePath, const CipherPassword &passwd, + bool isNeedIntegrityCheck = false) = 0; // Release a db connection. void ReleaseDBConnection(GenericKvDBConnection *connection); diff --git a/frameworks/libs/distributeddb/storage/src/operation/database_oper.cpp b/frameworks/libs/distributeddb/storage/src/operation/database_oper.cpp index 0e2b73849f1f7f002f71240357a80e64a6d5bbe9..43983472c82c2f7831abd46200f6f8614565ea81 100644 --- a/frameworks/libs/distributeddb/storage/src/operation/database_oper.cpp +++ b/frameworks/libs/distributeddb/storage/src/operation/database_oper.cpp @@ -172,7 +172,7 @@ int DatabaseOper::CheckSecurityOption(const std::string &filePath, const KvDBPro } int DatabaseOper::ExecuteImport(const std::string &filePath, const CipherPassword &passwd, - const KvDBProperties &property) const + const KvDBProperties &property, bool isNeedIntegrityCheck) const { ImportFileInfo importInfo; InitImportFileInfo(importInfo, property); @@ -212,7 +212,7 @@ int DatabaseOper::ExecuteImport(const std::string &filePath, const CipherPasswor // 3. export the unpacked file to the current database. LOGI("Import the unpacked database."); - errCode = ImportUnpackedDatabase(importInfo, passwd); + errCode = ImportUnpackedDatabase(importInfo, passwd, isNeedIntegrityCheck); if (errCode != E_OK) { LOGE("Failed to import from the unpacked databases, errCode = [%d]", errCode); } diff --git a/frameworks/libs/distributeddb/storage/src/operation/database_oper.h b/frameworks/libs/distributeddb/storage/src/operation/database_oper.h index 5e933b0aa61edf80bcbfe65ff4faa451a391fbc0..c46ef085b6d6d87d7ce4c66ebf256b11d0450439 100644 --- a/frameworks/libs/distributeddb/storage/src/operation/database_oper.h +++ b/frameworks/libs/distributeddb/storage/src/operation/database_oper.h @@ -28,7 +28,8 @@ public: virtual int Rekey(const CipherPassword &passwd) = 0; - virtual int Import(const std::string &filePath, const CipherPassword &passwd) = 0; + virtual int Import(const std::string &filePath, const CipherPassword &passwd, + bool isNeedIntegrityCheck = false) = 0; virtual int Export(const std::string &filePath, const CipherPassword &passwd) const = 0; @@ -59,11 +60,13 @@ protected: static int RemoveFile(const std::string &fileName); // import begin - int ExecuteImport(const std::string &filePath, const CipherPassword &passwd, const KvDBProperties &property) const; + int ExecuteImport(const std::string &filePath, const CipherPassword &passwd, const KvDBProperties &property, + bool isNeedIntegrityCheck = false) const; virtual int BackupCurrentDatabase(const ImportFileInfo &info) const = 0; - virtual int ImportUnpackedDatabase(const ImportFileInfo &info, const CipherPassword &srcPasswd) const = 0; + virtual int ImportUnpackedDatabase(const ImportFileInfo &info, const CipherPassword &srcPasswd, + bool isNeedIntegrityCheck = false) const = 0; virtual int ImportPostHandle() const = 0; diff --git a/frameworks/libs/distributeddb/storage/src/operation/single_ver_database_oper.cpp b/frameworks/libs/distributeddb/storage/src/operation/single_ver_database_oper.cpp index 8983e7fcddc6ab17ac281f60fa545d9c41497b62..2f57b7bf77cd3ce554b12931c49baaa4c9d6a6de 100644 --- a/frameworks/libs/distributeddb/storage/src/operation/single_ver_database_oper.cpp +++ b/frameworks/libs/distributeddb/storage/src/operation/single_ver_database_oper.cpp @@ -62,13 +62,13 @@ int SingleVerDatabaseOper::Rekey(const CipherPassword &passwd) return ExecuteRekey(passwd, singleVerNaturalStore_->GetDbProperties()); } -int SingleVerDatabaseOper::Import(const std::string &filePath, const CipherPassword &passwd) +int SingleVerDatabaseOper::Import(const std::string &filePath, const CipherPassword &passwd, bool isNeedIntegrityCheck) { if (singleVerNaturalStore_ == nullptr || storageEngine_ == nullptr) { return -E_INVALID_DB; } - return ExecuteImport(filePath, passwd, singleVerNaturalStore_->GetDbProperties()); + return ExecuteImport(filePath, passwd, singleVerNaturalStore_->GetDbProperties(), isNeedIntegrityCheck); } int SingleVerDatabaseOper::Export(const std::string &filePath, const CipherPassword &passwd) const @@ -325,7 +325,7 @@ int SingleVerDatabaseOper::ClearCurrentDatabase(const ImportFileInfo &info) cons } int SingleVerDatabaseOper::ImportUnpackedMainDatabase(const ImportFileInfo &info, - const CipherPassword &srcPasswd) const + const CipherPassword &srcPasswd, bool isNeedIntegrityCheck) const { std::string unpackedMainFile = info.unpackedDir + DBConstant::MAINDB_DIR + "/" + DBConstant::SINGLE_VER_DATA_STORE + DBConstant::DB_EXTENSION; @@ -346,6 +346,14 @@ int SingleVerDatabaseOper::ImportUnpackedMainDatabase(const ImportFileInfo &info int errCode = E_OK; if (isMainDbExisted) { + if (isNeedIntegrityCheck) { + // Check integrity before import + errCode = SQLiteUtils::CheckIntegrity(unpackedMainFile, cipherType, srcPasswd); + if (errCode != E_OK) { + LOGE("Check main file integrity error:%d", errCode); + return -E_INVALID_FILE; + } + } errCode = SQLiteUtils::ExportDatabase(unpackedMainFile, cipherType, srcPasswd, currentMainFile, passwd); if (errCode != E_OK) { LOGE("Export the unpacked main database to current error:%d", errCode); @@ -354,6 +362,14 @@ int SingleVerDatabaseOper::ImportUnpackedMainDatabase(const ImportFileInfo &info } if (isOldMainDbExisted) { + if (isNeedIntegrityCheck) { + // Check integrity before import + errCode = SQLiteUtils::CheckIntegrity(unpackedOldMainFile, cipherType, srcPasswd); + if (errCode != E_OK) { + LOGE("Check old main file integrity error:%d", errCode); + return -E_INVALID_FILE; + } + } errCode = SQLiteUtils::ExportDatabase(unpackedOldMainFile, cipherType, srcPasswd, currentMainFile, passwd); if (errCode != E_OK) { LOGE("Export the unpacked old version(<3) main database to current error:%d", errCode); @@ -379,7 +395,8 @@ int SingleVerDatabaseOper::ImportUnpackedMetaDatabase(const ImportFileInfo &info return errCode; } -int SingleVerDatabaseOper::ImportUnpackedDatabase(const ImportFileInfo &info, const CipherPassword &srcPasswd) const +int SingleVerDatabaseOper::ImportUnpackedDatabase(const ImportFileInfo &info, const CipherPassword &srcPasswd, + bool isNeedIntegrityCheck) const { std::string unpackedMetaFile = info.unpackedDir + DBConstant::METADB_DIR + "/" + DBConstant::SINGLE_VER_META_STORE + DBConstant::DB_EXTENSION; @@ -389,7 +406,7 @@ int SingleVerDatabaseOper::ImportUnpackedDatabase(const ImportFileInfo &info, co return errCode; } - errCode = ImportUnpackedMainDatabase(info, srcPasswd); + errCode = ImportUnpackedMainDatabase(info, srcPasswd, isNeedIntegrityCheck); if (errCode != E_OK) { LOGE("import unpacked mainDb fail, errCode = [%d]", errCode); return errCode; diff --git a/frameworks/libs/distributeddb/storage/src/operation/single_ver_database_oper.h b/frameworks/libs/distributeddb/storage/src/operation/single_ver_database_oper.h index b0ea78704d1198f4ff9853c1d6212f80c1856c5f..b670b0d9ff63c97210392968af0761c0ff879d2f 100644 --- a/frameworks/libs/distributeddb/storage/src/operation/single_ver_database_oper.h +++ b/frameworks/libs/distributeddb/storage/src/operation/single_ver_database_oper.h @@ -27,7 +27,7 @@ public: int Rekey(const CipherPassword &passwd) override; - int Import(const std::string &filePath, const CipherPassword &passwd) override; + int Import(const std::string &filePath, const CipherPassword &passwd, bool isNeedIntegrityCheck = false) override; int Export(const std::string &filePath, const CipherPassword &passwd) const override; @@ -45,7 +45,8 @@ protected: int BackupCurrentDatabase(const ImportFileInfo &info) const override; - int ImportUnpackedDatabase(const ImportFileInfo &info, const CipherPassword &srcPasswd) const override; + int ImportUnpackedDatabase(const ImportFileInfo &info, const CipherPassword &srcPasswd, + bool isNeedIntegrityCheck = false) const override; int ImportPostHandle() const override; @@ -64,7 +65,8 @@ private: int ClearCurrentDatabase(const ImportFileInfo &info) const; - int ImportUnpackedMainDatabase(const ImportFileInfo &info, const CipherPassword &srcPasswd) const; + int ImportUnpackedMainDatabase(const ImportFileInfo &info, const CipherPassword &srcPasswd, + bool isNeedIntegrityCheck = false) const; int ImportUnpackedMetaDatabase(const ImportFileInfo &info) const; diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/kv/sqlite_local_kvdb_connection.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/kv/sqlite_local_kvdb_connection.cpp index 674b72e2bcfdc613a0d36681d80d23c9d4b60460..a188ea9b1fee065d603ab3c2ce57de7999f2582e 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/kv/sqlite_local_kvdb_connection.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/kv/sqlite_local_kvdb_connection.cpp @@ -459,7 +459,8 @@ int SQLiteLocalKvDBConnection::Export(const std::string &filePath, const CipherP return kvDB_->Export(filePath, passwd); } -int SQLiteLocalKvDBConnection::Import(const std::string &filePath, const CipherPassword &passwd) +int SQLiteLocalKvDBConnection::Import(const std::string &filePath, const CipherPassword &passwd, + bool isNeedIntegrityCheck) { if (kvDB_ == nullptr) { return -E_INVALID_DB; @@ -483,7 +484,7 @@ int SQLiteLocalKvDBConnection::Import(const std::string &filePath, const CipherP kvDB_->ReEnableConnection(OperatePerm::IMPORT_MONOPOLIZE_PERM); return errCode; } - errCode = kvDB_->Import(filePath, passwd); + errCode = kvDB_->Import(filePath, passwd, isNeedIntegrityCheck); GenericKvDBConnection::ResetExclusiveStatus(); kvDB_->ReEnableConnection(OperatePerm::IMPORT_MONOPOLIZE_PERM); return errCode; diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/kv/sqlite_local_kvdb_connection.h b/frameworks/libs/distributeddb/storage/src/sqlite/kv/sqlite_local_kvdb_connection.h index 72179df5a15f14f9edf92dcf1eed15bb4615de2b..769ef14fed50d0680f7073ec313bfdedfbcc7059 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/kv/sqlite_local_kvdb_connection.h +++ b/frameworks/libs/distributeddb/storage/src/sqlite/kv/sqlite_local_kvdb_connection.h @@ -86,7 +86,7 @@ public: int Export(const std::string &filePath, const CipherPassword &passwd) override; - int Import(const std::string &filePath, const CipherPassword &passwd) override; + int Import(const std::string &filePath, const CipherPassword &passwd, bool isNeedIntegrityCheck = false) override; private: // Start the transaction diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.cpp index d7a13680687a06ffba46fc6e119de05ef44ca188..739855390687e5363eb0fd173bbae9dc3cbdc437 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.cpp @@ -1538,7 +1538,8 @@ int SQLiteSingleVerNaturalStore::Export(const std::string &filePath, const Ciphe return errCode; } -int SQLiteSingleVerNaturalStore::Import(const std::string &filePath, const CipherPassword &passwd) +int SQLiteSingleVerNaturalStore::Import(const std::string &filePath, const CipherPassword &passwd, + bool isNeedIntegrityCheck) { if (storageEngine_ == nullptr) { return -E_INVALID_DB; @@ -1579,7 +1580,7 @@ int SQLiteSingleVerNaturalStore::Import(const std::string &filePath, const Ciphe operation = std::make_unique(this, storageEngine_); operation->SetLocalDevId(localDev); - errCode = operation->Import(filePath, passwd); + errCode = operation->Import(filePath, passwd, isNeedIntegrityCheck); if (errCode != E_OK) { goto END; } diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.h b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.h index c97bcf24d79c671209e53ef9efe2c802c798c322..9c202175773021a79ce1fd951942c1f2d8f09f48 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.h +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.h @@ -104,7 +104,8 @@ public: int Export(const std::string &filePath, const CipherPassword &passwd) override; - int Import(const std::string &filePath, const CipherPassword &passwd) override; + int Import(const std::string &filePath, const CipherPassword &passwd, + bool isNeedIntegrityCheck = false) override; // In sync procedure, call this function int RemoveDeviceData(const std::string &deviceName, bool isNeedNotify) override; diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_connection.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_connection.cpp index b07d42f0d609e3c2269889cd9d83e8a4635904f5..c140adf1b8f9c6b7446d504215450f97f77f2363 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_connection.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_connection.cpp @@ -509,7 +509,8 @@ int SQLiteSingleVerNaturalStoreConnection::Export(const std::string &filePath, c return kvDB_->Export(filePath, passwd); } -int SQLiteSingleVerNaturalStoreConnection::Import(const std::string &filePath, const CipherPassword &passwd) +int SQLiteSingleVerNaturalStoreConnection::Import(const std::string &filePath, const CipherPassword &passwd, + bool isNeedIntegrityCheck) { if (IsFileAccessControlled()) { LOGE("Forbid Import when screen locked and security label [%d]!", kvDB_->GetMyProperties().GetSecLabel()); diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_connection.h b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_connection.h index 1bf4b8286e9a5a46980d9eede025ca032fc9e468..51d903ed53faf21f0b0503810e6eb16491fe18e9 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_connection.h +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_connection.h @@ -83,7 +83,8 @@ public: int Export(const std::string &filePath, const CipherPassword &passwd) override; - int Import(const std::string &filePath, const CipherPassword &passwd) override; + int Import(const std::string &filePath, const CipherPassword &passwd, + bool isNeedIntegrityCheck = false) override; // Get the result set int GetResultSet(const IOption &option, const Key &keyPrefix, IKvDBResultSet *&resultSet) const override; diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp index e5b667ba38b1127b535bc9a72d009bed06f22629..e7a729087f1343e7c32a6f917b469fd96df02676 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_utils.cpp @@ -32,6 +32,7 @@ #include "value_object.h" #include "schema_utils.h" #include "schema_constant.h" +#include "sqlite_single_ver_storage_executor_sql.h" #include "time_helper.h" #include "platform_specific.h" #include "sqlite_relational_utils.h" @@ -642,6 +643,24 @@ int SQLiteUtils::CreateMetaDatabase(const std::string &metaDbPath) return errCode; } +int SQLiteUtils::CheckIntegrity(const std::string dbFile, CipherType type, const CipherPassword &passwd) +{ + std::vector createTableSqls; + OpenDbProperties option = {dbFile, true, false, createTableSqls, type, passwd}; + sqlite3 *db = nullptr; + int errCode = SQLiteUtils::OpenDatabase(option, db); + if (errCode != E_OK) { + LOGE("CheckIntegrity, open db error:%d", errCode); + return errCode; + } + errCode = CheckIntegrity(db, CHECK_DB_INTEGRITY_SQL); + if (db != nullptr) { + (void)sqlite3_close_v2(db); + db = nullptr; + } + return errCode; +} + int SQLiteUtils::CheckIntegrity(sqlite3 *db, const std::string &sql) { sqlite3_stmt *statement = nullptr; diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_utils.h b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_utils.h index 13b3b4677560f09cc157f07d32236af97aa71cf0..6d4a6afaeea0e79a93a5efc9ccfa9dc82a1033b0 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_utils.h +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_utils.h @@ -142,6 +142,8 @@ public: static int CreateMetaDatabase(const std::string &metaDbPath); + static int CheckIntegrity(const std::string dbFile, CipherType type, const CipherPassword &passwd); + static int CheckIntegrity(sqlite3 *db, const std::string &sql); #ifdef RELATIONAL_STORE static int RegisterCalcHash(sqlite3 *db); diff --git a/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_import_and_export_rd_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_import_and_export_rd_test.cpp index 2bd696666c1e485f7a479e87595b900c92dd9660..65d6201f1962e8c80151d8d4a4e4256bd0830475 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_import_and_export_rd_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_import_and_export_rd_test.cpp @@ -1210,7 +1210,7 @@ HWTEST_F(DistributedDBInterfacesImportAndExportRdTest, abortHandle001, TestSize. threads.emplace_back(thread([&]() { g_kvNbDelegatePtr->CheckIntegrity(); })); - threads.emplace_back(&KvStoreNbDelegate::Import, g_kvNbDelegatePtr, singleExportFileName, passwd); + threads.emplace_back(&KvStoreNbDelegate::Import, g_kvNbDelegatePtr, singleExportFileName, passwd, false); threads.emplace_back(thread([&i]() { std::this_thread::sleep_for(std::chrono::milliseconds(i)); g_kvNbDelegatePtr->CheckIntegrity(); diff --git a/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_import_and_export_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_import_and_export_test.cpp index 2332acf1781c98abb6bc650b97ce063c4b6f92f8..4bc4836b1f6b5454fb06e01b1bc561b02687cb9a 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_import_and_export_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_import_and_export_test.cpp @@ -1299,7 +1299,7 @@ HWTEST_F(DistributedDBInterfacesImportAndExportTest, abortHandle001, TestSize.Le threads.emplace_back(thread([&]() { g_kvNbDelegatePtr->CheckIntegrity(); })); - threads.emplace_back(&KvStoreNbDelegate::Import, g_kvNbDelegatePtr, singleExportFileName, passwd); + threads.emplace_back(&KvStoreNbDelegate::Import, g_kvNbDelegatePtr, singleExportFileName, passwd, false); threads.emplace_back(thread([&i]() { std::this_thread::sleep_for(std::chrono::milliseconds(i)); g_kvNbDelegatePtr->CheckIntegrity();