From dc96dba275ce9ebe84d9c06e0da755fd93f7a08e Mon Sep 17 00:00:00 2001 From: zhaowenli Date: Tue, 26 Nov 2024 20:23:08 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E9=87=8D=E5=8A=A0=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhaowenli Change-Id: I09da85ffdf53e244739afea6b5ea9ba553ef1b15 --- .../cpp/include/database/access_token_db.h | 1 + .../main/cpp/src/database/access_token_db.cpp | 49 +++++++++++++++---- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/services/accesstokenmanager/main/cpp/include/database/access_token_db.h b/services/accesstokenmanager/main/cpp/include/database/access_token_db.h index f171479f4..bcda7a254 100644 --- a/services/accesstokenmanager/main/cpp/include/database/access_token_db.h +++ b/services/accesstokenmanager/main/cpp/include/database/access_token_db.h @@ -41,6 +41,7 @@ public: int32_t Find(AtmDataType type, const GenericValues& conditionValue, std::vector& results); int32_t DeleteAndInsertHap(AccessTokenID tokenId, const std::vector& hapInfoValues, const std::vector& permDefValues, const std::vector& permStateValues); + std::shared_ptr GetRdb(); private: AccessTokenDb(); diff --git a/services/accesstokenmanager/main/cpp/src/database/access_token_db.cpp b/services/accesstokenmanager/main/cpp/src/database/access_token_db.cpp index 3ec9e117a..51bb379f3 100644 --- a/services/accesstokenmanager/main/cpp/src/database/access_token_db.cpp +++ b/services/accesstokenmanager/main/cpp/src/database/access_token_db.cpp @@ -92,6 +92,20 @@ int32_t AccessTokenDb::RestoreAndInsertIfCorrupt(const int32_t resultCode, int64 return 0; } +std::shared_ptr AccessTokenDb::GetRdb() +{ + std::string dbPath = std::string(DATABASE_PATH) + std::string(DATABASE_NAME); + NativeRdb::RdbStoreConfig config(dbPath); + config.SetSecurityLevel(NativeRdb::SecurityLevel::S3); + config.SetAllowRebuild(true); + config.SetHaMode(NativeRdb::HAMode::MAIN_REPLICA); + config.SetServiceName(std::string(ACCESSTOKEN_SERVICE_NAME)); + AccessTokenOpenCallback callback; + int32_t res = NativeRdb::E_OK; + std::shared_ptr db = NativeRdb::RdbHelper::GetRdbStore(config, DATABASE_VERSION_4, callback, res); + return db; +} + int32_t AccessTokenDb::Add(const AtmDataType type, const std::vector& values) { int64_t beginTime = TimeUtil::GetCurrentTimestamp(); @@ -113,8 +127,11 @@ int32_t AccessTokenDb::Add(const AtmDataType type, const std::vector lock(this->rwLock_); if (db_ == nullptr) { - ACCESSTOKEN_LOG_ERROR(LABEL, "db is nullptr."); - return AccessTokenError::ERR_DATABASE_OPERATE_FAILED; + db_ = GetRdb(); + if (db_ == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "db is nullptr."); + return AccessTokenError::ERR_DATABASE_OPERATE_FAILED; + } } int32_t res = db_->BatchInsert(outInsertNum, tableName, buckets); if (res != NativeRdb::E_OK) { @@ -180,8 +197,11 @@ int32_t AccessTokenDb::Remove(const AtmDataType type, const GenericValues& condi { OHOS::Utils::UniqueWriteGuard lock(this->rwLock_); if (db_ == nullptr) { - ACCESSTOKEN_LOG_ERROR(LABEL, "db is nullptr."); - return AccessTokenError::ERR_DATABASE_OPERATE_FAILED; + db_ = GetRdb(); + if (db_ == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "db is nullptr."); + return AccessTokenError::ERR_DATABASE_OPERATE_FAILED; + } } int32_t res = db_->Delete(deletedRows, predicates); @@ -251,8 +271,11 @@ int32_t AccessTokenDb::Modify(const AtmDataType type, const GenericValues& modif { OHOS::Utils::UniqueWriteGuard lock(this->rwLock_); if (db_ == nullptr) { - ACCESSTOKEN_LOG_ERROR(LABEL, "db is nullptr."); - return AccessTokenError::ERR_DATABASE_OPERATE_FAILED; + db_ = GetRdb(); + if (db_ == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "db is nullptr."); + return AccessTokenError::ERR_DATABASE_OPERATE_FAILED; + } } int32_t res = db_->Update(changedRows, bucket, predicates); @@ -324,8 +347,11 @@ int32_t AccessTokenDb::Find(AtmDataType type, const GenericValues& conditionValu { OHOS::Utils::UniqueReadGuard lock(this->rwLock_); if (db_ == nullptr) { - ACCESSTOKEN_LOG_ERROR(LABEL, "db is nullptr."); - return AccessTokenError::ERR_DATABASE_OPERATE_FAILED; + db_ = GetRdb(); + if (db_ == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "db is nullptr."); + return AccessTokenError::ERR_DATABASE_OPERATE_FAILED; + } } auto queryResultSet = db_->Query(predicates, columns); @@ -436,8 +462,11 @@ int32_t AccessTokenDb::DeleteAndInsertHap(AccessTokenID tokenId, const std::vect { OHOS::Utils::UniqueWriteGuard lock(this->rwLock_); if (db_ == nullptr) { - ACCESSTOKEN_LOG_ERROR(LABEL, "db is nullptr."); - return AccessTokenError::ERR_DATABASE_OPERATE_FAILED; + db_ = GetRdb(); + if (db_ == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "db is nullptr."); + return AccessTokenError::ERR_DATABASE_OPERATE_FAILED; + } } db_->BeginTransaction(); -- Gitee From b1815024f42ad16acf71df9489435a7ab09b8b22 Mon Sep 17 00:00:00 2001 From: zhaowenli Date: Wed, 27 Nov 2024 11:49:48 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E9=87=8D=E5=8A=A0=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhaowenli Change-Id: Id1ae24e7495dde7e0f315522c563c80f6261182e --- .../cpp/include/database/access_token_db.h | 3 +- .../main/cpp/src/database/access_token_db.cpp | 44 ++++++++++--------- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/services/accesstokenmanager/main/cpp/include/database/access_token_db.h b/services/accesstokenmanager/main/cpp/include/database/access_token_db.h index bcda7a254..53cac449a 100644 --- a/services/accesstokenmanager/main/cpp/include/database/access_token_db.h +++ b/services/accesstokenmanager/main/cpp/include/database/access_token_db.h @@ -41,7 +41,7 @@ public: int32_t Find(AtmDataType type, const GenericValues& conditionValue, std::vector& results); int32_t DeleteAndInsertHap(AccessTokenID tokenId, const std::vector& hapInfoValues, const std::vector& permDefValues, const std::vector& permStateValues); - std::shared_ptr GetRdb(); + bool GetRdb(); private: AccessTokenDb(); @@ -62,6 +62,7 @@ private: OHOS::Utils::RWLock rwLock_; std::shared_ptr db_ = nullptr; + std::mutex dbLock_; }; } // namespace AccessToken } // namespace Security diff --git a/services/accesstokenmanager/main/cpp/src/database/access_token_db.cpp b/services/accesstokenmanager/main/cpp/src/database/access_token_db.cpp index 51bb379f3..244237948 100644 --- a/services/accesstokenmanager/main/cpp/src/database/access_token_db.cpp +++ b/services/accesstokenmanager/main/cpp/src/database/access_token_db.cpp @@ -92,18 +92,25 @@ int32_t AccessTokenDb::RestoreAndInsertIfCorrupt(const int32_t resultCode, int64 return 0; } -std::shared_ptr AccessTokenDb::GetRdb() +bool AccessTokenDb::GetRdb() { - std::string dbPath = std::string(DATABASE_PATH) + std::string(DATABASE_NAME); - NativeRdb::RdbStoreConfig config(dbPath); - config.SetSecurityLevel(NativeRdb::SecurityLevel::S3); - config.SetAllowRebuild(true); - config.SetHaMode(NativeRdb::HAMode::MAIN_REPLICA); - config.SetServiceName(std::string(ACCESSTOKEN_SERVICE_NAME)); - AccessTokenOpenCallback callback; - int32_t res = NativeRdb::E_OK; - std::shared_ptr db = NativeRdb::RdbHelper::GetRdbStore(config, DATABASE_VERSION_4, callback, res); - return db; + std::lock_guard lock(dbLock_); + if (db_ == nullptr) { + std::string dbPath = std::string(DATABASE_PATH) + std::string(DATABASE_NAME); + NativeRdb::RdbStoreConfig config(dbPath); + config.SetSecurityLevel(NativeRdb::SecurityLevel::S3); + config.SetAllowRebuild(true); + config.SetHaMode(NativeRdb::HAMode::MAIN_REPLICA); + config.SetServiceName(std::string(ACCESSTOKEN_SERVICE_NAME)); + AccessTokenOpenCallback callback; + int32_t res = NativeRdb::E_OK; + db_ = NativeRdb::RdbHelper::GetRdbStore(config, DATABASE_VERSION_4, callback, res); + if ((res != NativeRdb::E_OK) || (db_ == nullptr)) { + ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to get rdb, res is %{public}d.", res); + return false; + } + } + return true; } int32_t AccessTokenDb::Add(const AtmDataType type, const std::vector& values) @@ -127,8 +134,7 @@ int32_t AccessTokenDb::Add(const AtmDataType type, const std::vector lock(this->rwLock_); if (db_ == nullptr) { - db_ = GetRdb(); - if (db_ == nullptr) { + if (!GetRdb()) { ACCESSTOKEN_LOG_ERROR(LABEL, "db is nullptr."); return AccessTokenError::ERR_DATABASE_OPERATE_FAILED; } @@ -197,8 +203,7 @@ int32_t AccessTokenDb::Remove(const AtmDataType type, const GenericValues& condi { OHOS::Utils::UniqueWriteGuard lock(this->rwLock_); if (db_ == nullptr) { - db_ = GetRdb(); - if (db_ == nullptr) { + if (!GetRdb()) { ACCESSTOKEN_LOG_ERROR(LABEL, "db is nullptr."); return AccessTokenError::ERR_DATABASE_OPERATE_FAILED; } @@ -271,8 +276,7 @@ int32_t AccessTokenDb::Modify(const AtmDataType type, const GenericValues& modif { OHOS::Utils::UniqueWriteGuard lock(this->rwLock_); if (db_ == nullptr) { - db_ = GetRdb(); - if (db_ == nullptr) { + if (!GetRdb()) { ACCESSTOKEN_LOG_ERROR(LABEL, "db is nullptr."); return AccessTokenError::ERR_DATABASE_OPERATE_FAILED; } @@ -347,8 +351,7 @@ int32_t AccessTokenDb::Find(AtmDataType type, const GenericValues& conditionValu { OHOS::Utils::UniqueReadGuard lock(this->rwLock_); if (db_ == nullptr) { - db_ = GetRdb(); - if (db_ == nullptr) { + if (!GetRdb()) { ACCESSTOKEN_LOG_ERROR(LABEL, "db is nullptr."); return AccessTokenError::ERR_DATABASE_OPERATE_FAILED; } @@ -462,8 +465,7 @@ int32_t AccessTokenDb::DeleteAndInsertHap(AccessTokenID tokenId, const std::vect { OHOS::Utils::UniqueWriteGuard lock(this->rwLock_); if (db_ == nullptr) { - db_ = GetRdb(); - if (db_ == nullptr) { + if (!GetRdb()) { ACCESSTOKEN_LOG_ERROR(LABEL, "db is nullptr."); return AccessTokenError::ERR_DATABASE_OPERATE_FAILED; } -- Gitee From 4fc0f8f86380910e8fa1413f3370caf5d1e10479 Mon Sep 17 00:00:00 2001 From: zhaowenli Date: Wed, 27 Nov 2024 16:24:53 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E9=87=8D=E5=8A=A0=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhaowenli Change-Id: I6d1a0ee08035ed34d70306133ac42f3d2f6e6414 --- .../cpp/include/database/access_token_db.h | 16 +- .../main/cpp/src/database/access_token_db.cpp | 165 +++++++++--------- 2 files changed, 90 insertions(+), 91 deletions(-) diff --git a/services/accesstokenmanager/main/cpp/include/database/access_token_db.h b/services/accesstokenmanager/main/cpp/include/database/access_token_db.h index 53cac449a..30df1dbcf 100644 --- a/services/accesstokenmanager/main/cpp/include/database/access_token_db.h +++ b/services/accesstokenmanager/main/cpp/include/database/access_token_db.h @@ -41,24 +41,28 @@ public: int32_t Find(AtmDataType type, const GenericValues& conditionValue, std::vector& results); int32_t DeleteAndInsertHap(AccessTokenID tokenId, const std::vector& hapInfoValues, const std::vector& permDefValues, const std::vector& permStateValues); - bool GetRdb(); + std::shared_ptr GetRdb(); private: AccessTokenDb(); DISALLOW_COPY_AND_MOVE(AccessTokenDb); int32_t RestoreAndInsertIfCorrupt(const int32_t resultCode, int64_t& outInsertNum, - const std::string& tableName, const std::vector& buckets); + const std::string& tableName, const std::vector& buckets, + const std::shared_ptr& db); int32_t RestoreAndDeleteIfCorrupt(const int32_t resultCode, int32_t& deletedRows, - const NativeRdb::RdbPredicates& predicates); + const NativeRdb::RdbPredicates& predicates, const std::shared_ptr& db); int32_t RestoreAndUpdateIfCorrupt(const int32_t resultCode, int32_t& changedRows, - const NativeRdb::ValuesBucket& bucket, const NativeRdb::RdbPredicates& predicates); + const NativeRdb::ValuesBucket& bucket, const NativeRdb::RdbPredicates& predicates, + const std::shared_ptr& db); int32_t RestoreAndQueryIfCorrupt(const NativeRdb::RdbPredicates& predicates, - const std::vector& columns, std::shared_ptr& queryResultSet); + const std::vector& columns, std::shared_ptr& queryResultSet, + const std::shared_ptr& db); int32_t DeleteAndAddSingleTable(const GenericValues delCondition, const std::string& tableName, - const std::vector& addValues); + const std::vector& addValues, const std::shared_ptr& db); int32_t DeleteAndAddRecord(AccessTokenID tokenId, const std::vector& hapInfoValues, const std::vector& permDefValues, const std::vector& permStateValues); + void InitRdb(); OHOS::Utils::RWLock rwLock_; std::shared_ptr db_ = nullptr; diff --git a/services/accesstokenmanager/main/cpp/src/database/access_token_db.cpp b/services/accesstokenmanager/main/cpp/src/database/access_token_db.cpp index 244237948..1d8aedbb2 100644 --- a/services/accesstokenmanager/main/cpp/src/database/access_token_db.cpp +++ b/services/accesstokenmanager/main/cpp/src/database/access_token_db.cpp @@ -51,38 +51,26 @@ AccessTokenDb& AccessTokenDb::GetInstance() AccessTokenDb::AccessTokenDb() { - std::string dbPath = std::string(DATABASE_PATH) + std::string(DATABASE_NAME); - NativeRdb::RdbStoreConfig config(dbPath); - config.SetSecurityLevel(NativeRdb::SecurityLevel::S3); - config.SetAllowRebuild(true); - config.SetHaMode(NativeRdb::HAMode::MAIN_REPLICA); // Real-time dual-write backup database - config.SetServiceName(std::string(ACCESSTOKEN_SERVICE_NAME)); - AccessTokenOpenCallback callback; - int32_t res = NativeRdb::E_OK; - // pragma user_version will done by rdb, they store path and db_ as pair in RdbStoreManager - db_ = NativeRdb::RdbHelper::GetRdbStore(config, DATABASE_VERSION_4, callback, res); - if ((res != NativeRdb::E_OK) || (db_ == nullptr)) { - ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to init rdb, res is %{public}d.", res); - return; - } + InitRdb(); } int32_t AccessTokenDb::RestoreAndInsertIfCorrupt(const int32_t resultCode, int64_t& outInsertNum, - const std::string& tableName, const std::vector& buckets) + const std::string& tableName, const std::vector& buckets, + const std::shared_ptr& db) { if (resultCode != NativeRdb::E_SQLITE_CORRUPT) { return resultCode; } ACCESSTOKEN_LOG_WARN(LABEL, "Detech database corrupt, restore from backup!"); - int32_t res = db_->Restore(""); + int32_t res = db->Restore(""); if (res != NativeRdb::E_OK) { ACCESSTOKEN_LOG_ERROR(LABEL, "Db restore failed, res is %{public}d.", res); return res; } ACCESSTOKEN_LOG_INFO(LABEL, "Database restore success, try insert again!"); - res = db_->BatchInsert(outInsertNum, tableName, buckets); + res = db->BatchInsert(outInsertNum, tableName, buckets); if (res != NativeRdb::E_OK) { ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to batch insert into table %{public}s again, res is %{public}d.", tableName.c_str(), res); @@ -92,25 +80,30 @@ int32_t AccessTokenDb::RestoreAndInsertIfCorrupt(const int32_t resultCode, int64 return 0; } -bool AccessTokenDb::GetRdb() +void AccessTokenDb::InitRdb() +{ + std::string dbPath = std::string(DATABASE_PATH) + std::string(DATABASE_NAME); + NativeRdb::RdbStoreConfig config(dbPath); + config.SetSecurityLevel(NativeRdb::SecurityLevel::S3); + config.SetAllowRebuild(true); + config.SetHaMode(NativeRdb::HAMode::MAIN_REPLICA); // Real-time dual-write backup database + config.SetServiceName(std::string(ACCESSTOKEN_SERVICE_NAME)); + AccessTokenOpenCallback callback; + int32_t res = NativeRdb::E_OK; + // pragma user_version will done by rdb, they store path and db_ as pair in RdbStoreManager + db_ = NativeRdb::RdbHelper::GetRdbStore(config, DATABASE_VERSION_4, callback, res); + if ((res != NativeRdb::E_OK) || (db_ == nullptr)) { + ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to init rdb, res is %{public}d.", res); + } +} + +std::shared_ptr AccessTokenDb::GetRdb() { std::lock_guard lock(dbLock_); if (db_ == nullptr) { - std::string dbPath = std::string(DATABASE_PATH) + std::string(DATABASE_NAME); - NativeRdb::RdbStoreConfig config(dbPath); - config.SetSecurityLevel(NativeRdb::SecurityLevel::S3); - config.SetAllowRebuild(true); - config.SetHaMode(NativeRdb::HAMode::MAIN_REPLICA); - config.SetServiceName(std::string(ACCESSTOKEN_SERVICE_NAME)); - AccessTokenOpenCallback callback; - int32_t res = NativeRdb::E_OK; - db_ = NativeRdb::RdbHelper::GetRdbStore(config, DATABASE_VERSION_4, callback, res); - if ((res != NativeRdb::E_OK) || (db_ == nullptr)) { - ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to get rdb, res is %{public}d.", res); - return false; - } + InitRdb(); } - return true; + return db_; } int32_t AccessTokenDb::Add(const AtmDataType type, const std::vector& values) @@ -133,17 +126,16 @@ int32_t AccessTokenDb::Add(const AtmDataType type, const std::vector lock(this->rwLock_); - if (db_ == nullptr) { - if (!GetRdb()) { - ACCESSTOKEN_LOG_ERROR(LABEL, "db is nullptr."); - return AccessTokenError::ERR_DATABASE_OPERATE_FAILED; - } + auto db = GetRdb(); + if (db == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "db is nullptr."); + return AccessTokenError::ERR_DATABASE_OPERATE_FAILED; } - int32_t res = db_->BatchInsert(outInsertNum, tableName, buckets); + int32_t res = db->BatchInsert(outInsertNum, tableName, buckets); if (res != NativeRdb::E_OK) { ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to batch insert into table %{public}s, res is %{public}d.", tableName.c_str(), res); - int32_t result = RestoreAndInsertIfCorrupt(res, outInsertNum, tableName, buckets); + int32_t result = RestoreAndInsertIfCorrupt(res, outInsertNum, tableName, buckets, db); if (result != NativeRdb::E_OK) { return result; } @@ -163,21 +155,21 @@ int32_t AccessTokenDb::Add(const AtmDataType type, const std::vector& db) { if (resultCode != NativeRdb::E_SQLITE_CORRUPT) { return resultCode; } ACCESSTOKEN_LOG_WARN(LABEL, "Detech database corrupt, restore from backup!"); - int32_t res = db_->Restore(""); + int32_t res = db->Restore(""); if (res != NativeRdb::E_OK) { ACCESSTOKEN_LOG_ERROR(LABEL, "Db restore failed, res is %{public}d.", res); return res; } ACCESSTOKEN_LOG_INFO(LABEL, "Database restore success, try delete again!"); - res = db_->Delete(deletedRows, predicates); + res = db->Delete(deletedRows, predicates); if (res != NativeRdb::E_OK) { ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to delete record from table %{public}s again, res is %{public}d.", predicates.GetTableName().c_str(), res); @@ -202,18 +194,17 @@ int32_t AccessTokenDb::Remove(const AtmDataType type, const GenericValues& condi int32_t deletedRows = 0; { OHOS::Utils::UniqueWriteGuard lock(this->rwLock_); - if (db_ == nullptr) { - if (!GetRdb()) { - ACCESSTOKEN_LOG_ERROR(LABEL, "db is nullptr."); - return AccessTokenError::ERR_DATABASE_OPERATE_FAILED; - } + auto db = GetRdb(); + if (db == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "db is nullptr."); + return AccessTokenError::ERR_DATABASE_OPERATE_FAILED; } - int32_t res = db_->Delete(deletedRows, predicates); + int32_t res = db->Delete(deletedRows, predicates); if (res != NativeRdb::E_OK) { ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to delete record from table %{public}s, res is %{public}d.", tableName.c_str(), res); - int32_t result = RestoreAndDeleteIfCorrupt(res, deletedRows, predicates); + int32_t result = RestoreAndDeleteIfCorrupt(res, deletedRows, predicates, db); if (result != NativeRdb::E_OK) { return result; } @@ -228,21 +219,22 @@ int32_t AccessTokenDb::Remove(const AtmDataType type, const GenericValues& condi } int32_t AccessTokenDb::RestoreAndUpdateIfCorrupt(const int32_t resultCode, int32_t& changedRows, - const NativeRdb::ValuesBucket& bucket, const NativeRdb::RdbPredicates& predicates) + const NativeRdb::ValuesBucket& bucket, const NativeRdb::RdbPredicates& predicates, + const std::shared_ptr& db) { if (resultCode != NativeRdb::E_SQLITE_CORRUPT) { return resultCode; } ACCESSTOKEN_LOG_WARN(LABEL, "Detech database corrupt, restore from backup!"); - int32_t res = db_->Restore(""); + int32_t res = db->Restore(""); if (res != NativeRdb::E_OK) { ACCESSTOKEN_LOG_ERROR(LABEL, "Db restore failed, res is %{public}d.", res); return res; } ACCESSTOKEN_LOG_INFO(LABEL, "Database restore success, try update again!"); - res = db_->Update(changedRows, bucket, predicates); + res = db->Update(changedRows, bucket, predicates); if (res != NativeRdb::E_OK) { ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to update record from table %{public}s again, res is %{public}d.", predicates.GetTableName().c_str(), res); @@ -275,18 +267,17 @@ int32_t AccessTokenDb::Modify(const AtmDataType type, const GenericValues& modif int32_t changedRows = 0; { OHOS::Utils::UniqueWriteGuard lock(this->rwLock_); - if (db_ == nullptr) { - if (!GetRdb()) { - ACCESSTOKEN_LOG_ERROR(LABEL, "db is nullptr."); - return AccessTokenError::ERR_DATABASE_OPERATE_FAILED; - } + auto db = GetRdb(); + if (db == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "db is nullptr."); + return AccessTokenError::ERR_DATABASE_OPERATE_FAILED; } - int32_t res = db_->Update(changedRows, bucket, predicates); + int32_t res = db->Update(changedRows, bucket, predicates); if (res != NativeRdb::E_OK) { ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to update record from table %{public}s, res is %{public}d.", tableName.c_str(), res); - int32_t result = RestoreAndUpdateIfCorrupt(res, changedRows, bucket, predicates); + int32_t result = RestoreAndUpdateIfCorrupt(res, changedRows, bucket, predicates, db); if (result != NativeRdb::E_OK) { return result; } @@ -301,7 +292,8 @@ int32_t AccessTokenDb::Modify(const AtmDataType type, const GenericValues& modif } int32_t AccessTokenDb::RestoreAndQueryIfCorrupt(const NativeRdb::RdbPredicates& predicates, - const std::vector& columns, std::shared_ptr& queryResultSet) + const std::vector& columns, std::shared_ptr& queryResultSet, + const std::shared_ptr& db) { int32_t count = 0; int32_t res = queryResultSet->GetRowCount(count); @@ -311,14 +303,14 @@ int32_t AccessTokenDb::RestoreAndQueryIfCorrupt(const NativeRdb::RdbPredicates& queryResultSet = nullptr; ACCESSTOKEN_LOG_WARN(LABEL, "Detech database corrupt, restore from backup!"); - res = db_->Restore(""); + res = db->Restore(""); if (res != NativeRdb::E_OK) { ACCESSTOKEN_LOG_ERROR(LABEL, "Db restore failed, res is %{public}d.", res); return res; } ACCESSTOKEN_LOG_INFO(LABEL, "Database restore success, try query again!"); - queryResultSet = db_->Query(predicates, columns); + queryResultSet = db->Query(predicates, columns); if (queryResultSet == nullptr) { ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to find records from table %{public}s again.", predicates.GetTableName().c_str()); @@ -350,21 +342,20 @@ int32_t AccessTokenDb::Find(AtmDataType type, const GenericValues& conditionValu int count = 0; { OHOS::Utils::UniqueReadGuard lock(this->rwLock_); - if (db_ == nullptr) { - if (!GetRdb()) { - ACCESSTOKEN_LOG_ERROR(LABEL, "db is nullptr."); - return AccessTokenError::ERR_DATABASE_OPERATE_FAILED; - } + auto db = GetRdb(); + if (db == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "db is nullptr."); + return AccessTokenError::ERR_DATABASE_OPERATE_FAILED; } - auto queryResultSet = db_->Query(predicates, columns); + auto queryResultSet = db->Query(predicates, columns); if (queryResultSet == nullptr) { ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to find records from table %{public}s.", tableName.c_str()); return AccessTokenError::ERR_DATABASE_OPERATE_FAILED; } - int32_t res = RestoreAndQueryIfCorrupt(predicates, columns, queryResultSet); + int32_t res = RestoreAndQueryIfCorrupt(predicates, columns, queryResultSet, db); if (res != 0) { return res; } @@ -389,16 +380,16 @@ int32_t AccessTokenDb::Find(AtmDataType type, const GenericValues& conditionValu } int32_t AccessTokenDb::DeleteAndAddSingleTable(const GenericValues delCondition, const std::string& tableName, - const std::vector& addValues) + const std::vector& addValues, const std::shared_ptr& db) { NativeRdb::RdbPredicates predicates(tableName); AccessTokenDbUtil::ToRdbPredicates(delCondition, predicates); // fill predicates with delCondition int32_t deletedRows = 0; - int32_t res = db_->Delete(deletedRows, predicates); + int32_t res = db->Delete(deletedRows, predicates); if (res != NativeRdb::E_OK) { ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to delete record from table %{public}s, res is %{public}d.", tableName.c_str(), res); - int32_t result = RestoreAndDeleteIfCorrupt(res, deletedRows, predicates); + int32_t result = RestoreAndDeleteIfCorrupt(res, deletedRows, predicates, db); if (result != NativeRdb::E_OK) { return result; } @@ -413,11 +404,11 @@ int32_t AccessTokenDb::DeleteAndAddSingleTable(const GenericValues delCondition, std::vector buckets; AccessTokenDbUtil::ToRdbValueBuckets(addValues, buckets); // fill buckets with addValues int64_t outInsertNum = 0; - res = db_->BatchInsert(outInsertNum, tableName, buckets); + res = db->BatchInsert(outInsertNum, tableName, buckets); if (res != NativeRdb::E_OK) { ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to batch insert into table %{public}s, res is %{public}d.", tableName.c_str(), res); - int32_t result = RestoreAndInsertIfCorrupt(res, outInsertNum, tableName, buckets); + int32_t result = RestoreAndInsertIfCorrupt(res, outInsertNum, tableName, buckets, db); if (result != NativeRdb::E_OK) { return result; } @@ -440,21 +431,26 @@ int32_t AccessTokenDb::DeleteAndAddRecord(AccessTokenID tokenId, const std::vect std::string hapTableName; AccessTokenDbUtil::GetTableNameByType(AtmDataType::ACCESSTOKEN_HAP_INFO, hapTableName); - int32_t res = DeleteAndAddSingleTable(conditionValue, hapTableName, hapInfoValues); + auto db = GetRdb(); + if (db == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "db is nullptr."); + return AccessTokenError::ERR_DATABASE_OPERATE_FAILED; + } + int32_t res = DeleteAndAddSingleTable(conditionValue, hapTableName, hapInfoValues,db); if (res != NativeRdb::E_OK) { return res; } std::string defTableName; AccessTokenDbUtil::GetTableNameByType(AtmDataType::ACCESSTOKEN_PERMISSION_DEF, defTableName); - res = DeleteAndAddSingleTable(conditionValue, defTableName, permDefValues); + res = DeleteAndAddSingleTable(conditionValue, defTableName, permDefValues,db); if (res != NativeRdb::E_OK) { return res; } std::string stateTableName; AccessTokenDbUtil::GetTableNameByType(AtmDataType::ACCESSTOKEN_PERMISSION_STATE, stateTableName); - return DeleteAndAddSingleTable(conditionValue, stateTableName, permStateValues); + return DeleteAndAddSingleTable(conditionValue, stateTableName, permStateValues,db); } int32_t AccessTokenDb::DeleteAndInsertHap(AccessTokenID tokenId, const std::vector& hapInfoValues, @@ -464,22 +460,21 @@ int32_t AccessTokenDb::DeleteAndInsertHap(AccessTokenID tokenId, const std::vect { OHOS::Utils::UniqueWriteGuard lock(this->rwLock_); - if (db_ == nullptr) { - if (!GetRdb()) { - ACCESSTOKEN_LOG_ERROR(LABEL, "db is nullptr."); - return AccessTokenError::ERR_DATABASE_OPERATE_FAILED; - } + auto db = GetRdb(); + if (db == nullptr) { + ACCESSTOKEN_LOG_ERROR(LABEL, "db is nullptr."); + return AccessTokenError::ERR_DATABASE_OPERATE_FAILED; } - db_->BeginTransaction(); + db->BeginTransaction(); int32_t res = DeleteAndAddRecord(tokenId, hapInfoValues, permDefValues, permStateValues); if (res != NativeRdb::E_OK) { - db_->RollBack(); + db->RollBack(); return res; } - db_->Commit(); + db->Commit(); } int64_t endTime = TimeUtil::GetCurrentTimestamp(); -- Gitee From 1ee3fe9963d578b914f1f2cbe962329c8600889a Mon Sep 17 00:00:00 2001 From: zhaowenli Date: Wed, 27 Nov 2024 17:01:56 +0800 Subject: [PATCH 4/6] =?UTF-8?q?AI=E6=A3=80=E8=A7=86=E5=91=8A=E8=AD=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhaowenli Change-Id: I2c7af8497b6f2bc7101ea50b13914b51a110e72e --- interfaces/innerkits/privacy/src/privacy_manager_client.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/interfaces/innerkits/privacy/src/privacy_manager_client.cpp b/interfaces/innerkits/privacy/src/privacy_manager_client.cpp index ec27452cf..ae1ce75d8 100644 --- a/interfaces/innerkits/privacy/src/privacy_manager_client.cpp +++ b/interfaces/innerkits/privacy/src/privacy_manager_client.cpp @@ -42,7 +42,8 @@ PrivacyManagerClient& PrivacyManagerClient::GetInstance() if (instance == nullptr) { std::lock_guard lock(g_instanceMutex); if (instance == nullptr) { - instance = new PrivacyManagerClient(); + PrivacyManagerClient* tmp = new PrivacyManagerClient(); + instance = std::move(tmp); } } return *instance; -- Gitee From ee38e9a23827de2f7f13b0bd1a05b81eccbfa864 Mon Sep 17 00:00:00 2001 From: zhaowenli Date: Wed, 27 Nov 2024 17:37:49 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E9=87=8D=E5=8A=A0=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhaowenli Change-Id: Id57afaa63570af69813f9fc3e7e185ab5d3a9b95 --- .../main/cpp/src/database/access_token_db.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/services/accesstokenmanager/main/cpp/src/database/access_token_db.cpp b/services/accesstokenmanager/main/cpp/src/database/access_token_db.cpp index 1d8aedbb2..d0fabf68e 100644 --- a/services/accesstokenmanager/main/cpp/src/database/access_token_db.cpp +++ b/services/accesstokenmanager/main/cpp/src/database/access_token_db.cpp @@ -436,21 +436,21 @@ int32_t AccessTokenDb::DeleteAndAddRecord(AccessTokenID tokenId, const std::vect ACCESSTOKEN_LOG_ERROR(LABEL, "db is nullptr."); return AccessTokenError::ERR_DATABASE_OPERATE_FAILED; } - int32_t res = DeleteAndAddSingleTable(conditionValue, hapTableName, hapInfoValues,db); + int32_t res = DeleteAndAddSingleTable(conditionValue, hapTableName, hapInfoValues, db); if (res != NativeRdb::E_OK) { return res; } std::string defTableName; AccessTokenDbUtil::GetTableNameByType(AtmDataType::ACCESSTOKEN_PERMISSION_DEF, defTableName); - res = DeleteAndAddSingleTable(conditionValue, defTableName, permDefValues,db); + res = DeleteAndAddSingleTable(conditionValue, defTableName, permDefValues, db); if (res != NativeRdb::E_OK) { return res; } std::string stateTableName; AccessTokenDbUtil::GetTableNameByType(AtmDataType::ACCESSTOKEN_PERMISSION_STATE, stateTableName); - return DeleteAndAddSingleTable(conditionValue, stateTableName, permStateValues,db); + return DeleteAndAddSingleTable(conditionValue, stateTableName, permStateValues, db); } int32_t AccessTokenDb::DeleteAndInsertHap(AccessTokenID tokenId, const std::vector& hapInfoValues, -- Gitee From 7bb623572e3019d426e5e8e9d9e7c0f15b67eb23 Mon Sep 17 00:00:00 2001 From: zhaowenli Date: Wed, 27 Nov 2024 18:50:03 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E9=87=8D=E5=8A=A0=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhaowenli Change-Id: Id71d4cd59ae87214485488d9e7896079a91b6849 --- interfaces/innerkits/privacy/src/privacy_manager_client.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/interfaces/innerkits/privacy/src/privacy_manager_client.cpp b/interfaces/innerkits/privacy/src/privacy_manager_client.cpp index ae1ce75d8..ec27452cf 100644 --- a/interfaces/innerkits/privacy/src/privacy_manager_client.cpp +++ b/interfaces/innerkits/privacy/src/privacy_manager_client.cpp @@ -42,8 +42,7 @@ PrivacyManagerClient& PrivacyManagerClient::GetInstance() if (instance == nullptr) { std::lock_guard lock(g_instanceMutex); if (instance == nullptr) { - PrivacyManagerClient* tmp = new PrivacyManagerClient(); - instance = std::move(tmp); + instance = new PrivacyManagerClient(); } } return *instance; -- Gitee