From 32c73af29368dfa3ffbf49f65e5b1319f70b9e61 Mon Sep 17 00:00:00 2001 From: c00514911 Date: Thu, 7 Nov 2024 11:49:20 +0800 Subject: [PATCH] =?UTF-8?q?fix:KV=E6=95=B0=E6=8D=AE=E5=BA=93import?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E5=A4=87=E5=BA=93=E5=AE=8C=E6=95=B4=E6=80=A7?= =?UTF-8?q?=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: c00514911 --- .../interfaces/include/kv_store_nb_delegate.h | 3 ++- .../src/kv_store_nb_delegate_impl.cpp | 5 ++-- .../src/kv_store_nb_delegate_impl.h | 3 ++- .../storage/include/ikvdb_connection.h | 3 ++- .../rd_single_ver_natural_store.cpp | 3 ++- .../gaussdb_rd/rd_single_ver_natural_store.h | 2 +- ...rd_single_ver_natural_store_connection.cpp | 5 ++-- .../rd_single_ver_natural_store_connection.h | 2 +- .../storage/src/kv/generic_kvdb.h | 3 ++- .../storage/src/operation/database_oper.cpp | 4 +-- .../storage/src/operation/database_oper.h | 9 ++++--- .../operation/single_ver_database_oper.cpp | 27 +++++++++++++++---- .../src/operation/single_ver_database_oper.h | 8 +++--- .../kv/sqlite_local_kvdb_connection.cpp | 5 ++-- .../sqlite/kv/sqlite_local_kvdb_connection.h | 2 +- .../sqlite_single_ver_natural_store.cpp | 5 ++-- .../sqlite/sqlite_single_ver_natural_store.h | 3 ++- ...te_single_ver_natural_store_connection.cpp | 3 ++- ...lite_single_ver_natural_store_connection.h | 3 ++- .../storage/src/sqlite/sqlite_utils.cpp | 19 +++++++++++++ .../storage/src/sqlite/sqlite_utils.h | 2 ++ ...b_interfaces_import_and_export_rd_test.cpp | 2 +- ...eddb_interfaces_import_and_export_test.cpp | 2 +- 23 files changed, 89 insertions(+), 34 deletions(-) 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 5a4debaa507..ccc9b6fb66f 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 298f3a29b61..57e32c9706d 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 f166d7b37d8..8f897ee9c0a 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 dd55180a6d4..ea3fda638e6 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 b99d8f84b17..202445c7ade 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 4cb8397dae0..0fbfe89685a 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 6784b71ff87..b3b3615d924 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 5b71d1ebec0..96c1490d774 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 7c32c3be4a8..a34873e53bc 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 0e2b73849f1..43983472c82 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 5e933b0aa61..c46ef085b6d 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 8983e7fcddc..2f57b7bf77c 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 b0ea78704d1..b670b0d9ff6 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 674b72e2bcf..a188ea9b1fe 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 72179df5a15..769ef14fed5 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 d7a13680687..73985539068 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 c97bcf24d79..9c202175773 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 b07d42f0d60..c140adf1b8f 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 1bf4b8286e9..51d903ed53f 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 e5b667ba38b..e7a729087f1 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 13b3b467756..6d4a6afaeea 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 2bd696666c1..65d6201f196 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 2332acf1781..4bc4836b1f6 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(); -- Gitee