From a84711e9aea54277e25391e7c6b0ce5f7854b5a7 Mon Sep 17 00:00:00 2001 From: illybyy Date: Mon, 7 Mar 2022 11:57:48 +0800 Subject: [PATCH 1/2] bugfix for empty device id procedure Signed-off-by: illybyy --- .../app/src/kvstore_data_service.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/services/distributeddataservice/app/src/kvstore_data_service.cpp b/services/distributeddataservice/app/src/kvstore_data_service.cpp index f2616e143..732ca52b1 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -246,8 +246,14 @@ Status KvStoreDataService::GetSingleKvStore(const Options &options, const AppId KvStoreAppManager::ConvertPathType(param.uid, param.bundleName, options.securityLevel), store); } if (param.status == Status::SUCCESS) { + status = UpdateMetaData(options, param, keyPara.metaKey, it->second); + if (status != Status::SUCCESS) { + ZLOGE("failed to write meta"); + callback(nullptr); + return status; + } callback(std::move(store)); - return UpdateMetaData(options, param, keyPara.metaKey, it->second); + return status; } param.status = GetSingleKvStoreFailDo(options, param, keyPara, it->second, store); @@ -340,12 +346,17 @@ Status KvStoreDataService::RecoverSecretKey(const Status &alreadyCreated, bool & Status KvStoreDataService::UpdateMetaData(const Options &options, const KvStoreParam &kvParas, const std::vector &metaKey, KvStoreUserManager &kvStoreUserManager) { + auto localDeviceId = DeviceKvStoreImpl::GetLocalDeviceId(); + if (localDeviceId.empty()) { + ZLOGE("failed to get local device id"); + return Status::ERROR; + } KvStoreMetaData metaData; metaData.appId = kvParas.trueAppId; metaData.appType = "harmony"; metaData.bundleName = kvParas.bundleName; metaData.deviceAccountId = kvParas.userId; - metaData.deviceId = DeviceKvStoreImpl::GetLocalDeviceId(); + metaData.deviceId = localDeviceId; metaData.isAutoSync = options.autoSync; metaData.isBackup = options.backup; metaData.isEncrypt = options.encrypt; @@ -833,7 +844,7 @@ void KvStoreDataService::OnStart() void KvStoreDataService::StartService() { static constexpr int32_t RETRY_TIMES = 10; - static constexpr int32_t RETRY_INTERVAL = 500; // unit is ms + static constexpr int32_t RETRY_INTERVAL = 500 * 1000; // unit is ms for (BlockInteger retry(RETRY_INTERVAL); retry < RETRY_TIMES; ++retry) { if (!DeviceKvStoreImpl::GetLocalDeviceId().empty()) { break; -- Gitee From 66cbadc3d2843d5453539097b2ca4f24a6a9f355 Mon Sep 17 00:00:00 2001 From: illybyy Date: Mon, 7 Mar 2022 20:20:08 +0800 Subject: [PATCH 2/2] refactor function lines count Signed-off-by: illybyy --- .../app/src/kvstore_data_service.cpp | 48 ++++++++++++------- .../app/src/kvstore_data_service.h | 3 ++ 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/services/distributeddataservice/app/src/kvstore_data_service.cpp b/services/distributeddataservice/app/src/kvstore_data_service.cpp index 732ca52b1..e7bbf7a1a 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -202,27 +202,16 @@ Status KvStoreDataService::GetSingleKvStore(const Options &options, const AppId { DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); ZLOGI("begin."); - if (!appId.IsValid() || !storeId.IsValid() || !options.IsValidType() - || options.kvStoreType == KvStoreType::MULTI_VERSION) { - ZLOGE("invalid argument type."); - return Status::INVALID_ARGUMENT; - } - KVSTORE_ACCOUNT_EVENT_PROCESSING_CHECKER(Status::SYSTEM_ACCOUNT_EVENT_PROCESSING); KvStoreParam param; - param.bundleName = appId.appId; - param.storeId = storeId.storeId; - const int32_t uid = IPCSkeleton::GetCallingUid(); - param.trueAppId = CheckerManager::GetInstance().GetAppId(appId.appId, uid); - ZLOGI("%{public}s, %{public}s", param.trueAppId.c_str(), param.bundleName.c_str()); - if (param.trueAppId.empty()) { - ZLOGW("appId:%{public}s, uid:%{public}d, PERMISSION_DENIED", appId.appId.c_str(), uid); - return Status::PERMISSION_DENIED; + Status status = FillStoreParam(options, appId, storeId, param); + if (status != Status::SUCCESS) { + callback(nullptr); + return status; } - param.userId = AccountDelegate::GetInstance()->GetDeviceAccountIdByUID(uid); SecretKeyPara keyPara; - Status status = KvStoreDataService::GetSecretKey(options, param, keyPara); + status = KvStoreDataService::GetSecretKey(options, param, keyPara); if (status != Status::SUCCESS) { callback(nullptr); return status; @@ -240,7 +229,8 @@ Status KvStoreDataService::GetSingleKvStore(const Options &options, const AppId it = result.first; } sptr store; - param.status = (it->second).GetKvStore(options, param.bundleName, param.storeId, uid, keyPara.secretKey, store); + param.status = + (it->second).GetKvStore(options, param.bundleName, param.storeId, param.uid, keyPara.secretKey, store); if (keyPara.outdated) { KvStoreMetaManager::GetInstance().ReKey(param.userId, param.bundleName, param.storeId, KvStoreAppManager::ConvertPathType(param.uid, param.bundleName, options.securityLevel), store); @@ -261,6 +251,28 @@ Status KvStoreDataService::GetSingleKvStore(const Options &options, const AppId return param.status; } +Status KvStoreDataService::FillStoreParam( + const Options &options, const AppId &appId, const StoreId &storeId, KvStoreParam ¶m) +{ + if (!appId.IsValid() || !storeId.IsValid() || !options.IsValidType() + || options.kvStoreType == KvStoreType::MULTI_VERSION) { + ZLOGE("invalid argument type."); + return Status::INVALID_ARGUMENT; + } + param.bundleName = appId.appId; + param.storeId = storeId.storeId; + param.uid = IPCSkeleton::GetCallingUid(); + param.trueAppId = CheckerManager::GetInstance().GetAppId(appId.appId, param.uid); + ZLOGI("%{public}s, %{public}s", param.trueAppId.c_str(), param.bundleName.c_str()); + if (param.trueAppId.empty()) { + ZLOGW("appId:%{public}s, uid:%{public}d, PERMISSION_DENIED", appId.appId.c_str(), param.uid); + return PERMISSION_DENIED; + } + + param.userId = AccountDelegate::GetInstance()->GetDeviceAccountIdByUID(param.uid); + return SUCCESS; +} + Status KvStoreDataService::GetSecretKey(const Options &options, const KvStoreParam &kvParas, SecretKeyPara &secretKeyParas) { @@ -849,7 +861,7 @@ void KvStoreDataService::StartService() if (!DeviceKvStoreImpl::GetLocalDeviceId().empty()) { break; } - ZLOGE("GetLocalDeviceId failed, reties: %{public}d", static_cast(retry)); + ZLOGE("GetLocalDeviceId failed, retry count:%{public}d", static_cast(retry)); } // register this to ServiceManager. diff --git a/services/distributeddataservice/app/src/kvstore_data_service.h b/services/distributeddataservice/app/src/kvstore_data_service.h index f83c57944..1f7fbc0be 100755 --- a/services/distributeddataservice/app/src/kvstore_data_service.h +++ b/services/distributeddataservice/app/src/kvstore_data_service.h @@ -178,6 +178,9 @@ private: bool CheckOptions(const Options &options, const std::vector &metaKey) const; void CreateRdbService(); bool IsStoreOpened(const std::string &userId, const std::string &appId, const std::string &storeId); + static Status FillStoreParam( + const Options &options, const AppId &appId, const StoreId &storeId, KvStoreParam ¶m); + static constexpr int TEN_SEC = 10; std::mutex accountMutex_; -- Gitee