From 7eb8e058d35b56308af209d22924fa1e0f8f3f43 Mon Sep 17 00:00:00 2001 From: dulei Date: Tue, 14 Jun 2022 16:32:26 +0800 Subject: [PATCH] =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E6=89=93=E7=82=B9=E6=96=B9?= =?UTF-8?q?=E6=A1=88=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: dulei --- hisysevent.yaml | 2 +- .../adapter/dfx/src/hiview_adapter.cpp | 5 +- .../adapter/include/dfx/dfx_types.h | 12 ++-- services/distributeddataservice/app/BUILD.gn | 2 - .../app/src/backup_handler.cpp | 6 +- .../app/src/kvstore_app_manager.cpp | 4 ++ .../app/src/kvstore_data_service.h | 2 + .../app/src/single_kvstore_impl.cpp | 26 +++----- .../distributeddataservice/framework/BUILD.gn | 1 + .../include/metadata/corrupted_meta_data.h | 39 ++++++++++++ .../metadata/corrupted_meta_data.cpp | 59 +++++++++++++++++++ 11 files changed, 130 insertions(+), 28 deletions(-) create mode 100644 services/distributeddataservice/framework/include/metadata/corrupted_meta_data.h create mode 100644 services/distributeddataservice/framework/metadata/corrupted_meta_data.cpp diff --git a/hisysevent.yaml b/hisysevent.yaml index 7835b3a3..44a9a785 100644 --- a/hisysevent.yaml +++ b/hisysevent.yaml @@ -98,7 +98,7 @@ DATABASE_BEHAVIOUR: ANONYMOUS_UID: {type: STRING, desc: uid with anonymous } APP_ID: {type: STRING, desc: app id } STORE_ID: {type: STRING, desc: store id } - BEHAVIOUR_INFO: {type: INT32, desc: behaviour id } + BEHAVIOUR_INFO: {type: STRING, desc: behaviour type and behaviour resulte } OPEN_DATABASE_FAILED: __BASE: {type: FAULT, level: CRITICAL, desc: The database open failed} diff --git a/services/distributeddataservice/adapter/dfx/src/hiview_adapter.cpp b/services/distributeddataservice/adapter/dfx/src/hiview_adapter.cpp index 91139415..f4a80868 100644 --- a/services/distributeddataservice/adapter/dfx/src/hiview_adapter.cpp +++ b/services/distributeddataservice/adapter/dfx/src/hiview_adapter.cpp @@ -146,13 +146,16 @@ void HiViewAdapter::ReportBehaviour(int dfxCode, const BehaviourMsg &msg) return; } KvStoreTask task([dfxCode, msg]() { + std::string message; + message.append("Behaviour type : ").append(std::to_string(static_cast(msg.behaviourType))) + .append(" behaviour result : ").append(std::to_string(static_cast(msg.behaviourResult))); HiSysEvent::Write(HiSysEvent::Domain::DISTRIBUTED_DATAMGR, CoverEventID(dfxCode), HiSysEvent::EventType::BEHAVIOR, USER_ID, msg.userId, APP_ID, msg.appId, STORE_ID, msg.storeId, - BEHAVIOUR_INFO, static_cast(msg.behaviourType)); + BEHAVIOUR_INFO, message); }); pool_->AddTask(std::move(task)); } diff --git a/services/distributeddataservice/adapter/include/dfx/dfx_types.h b/services/distributeddataservice/adapter/include/dfx/dfx_types.h index a7f0ccbc..a2573cbc 100644 --- a/services/distributeddataservice/adapter/include/dfx/dfx_types.h +++ b/services/distributeddataservice/adapter/include/dfx/dfx_types.h @@ -77,10 +77,13 @@ enum class FaultType { }; enum class BehaviourType { - DATABASE_BACKUP_SUCCESS = 0, - DATABASE_BACKUP_FAILED = 1, - DATABASE_RECOVERY_SUCCESS = 3, - DATABASE_RECOVERY_FAILED = 4, + DATABASE_BACKUP = 0, + DATABASE_RECOVERY = 1, +}; + +enum class BehaviourResult { + BEHAVIOUR_SUCCESS = 0, + BEHAVIOUR_FAILED = 1, }; enum class SecurityInfo { @@ -132,6 +135,7 @@ struct BehaviourMsg { std::string appId; std::string storeId; BehaviourType behaviourType; + BehaviourResult behaviourResult; }; struct VisitStat { diff --git a/services/distributeddataservice/app/BUILD.gn b/services/distributeddataservice/app/BUILD.gn index d3e04947..362dbff3 100644 --- a/services/distributeddataservice/app/BUILD.gn +++ b/services/distributeddataservice/app/BUILD.gn @@ -38,7 +38,6 @@ ohos_sa_profile("distributeddata_profile") { config("module_private_config") { visibility = [ ":*" ] include_dirs = [ - "//foundation/distributeddatamgr/appdatamgr/interfaces/inner_api/native/preferences/include", "//foundation/distributeddatamgr/distributeddatamgr/frameworks/common", "//foundation/distributeddatamgr/distributeddatamgr/frameworks/innerkitsimpl/distributeddatafwk/include", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/service/bootstrap/include", @@ -128,7 +127,6 @@ ohos_shared_library("distributeddataservice") { "hiviewdfx_hilog_native:libhilog", "huks:libhukssdk", "ipc:ipc_core", - "preferences:native_preferences", "safwk:system_ability_fwk", "samgr_standard:samgr_proxy", "startup_l2:syspara", diff --git a/services/distributeddataservice/app/src/backup_handler.cpp b/services/distributeddataservice/app/src/backup_handler.cpp index 3f333f8d..38a4ea0b 100644 --- a/services/distributeddataservice/app/src/backup_handler.cpp +++ b/services/distributeddataservice/app/src/backup_handler.cpp @@ -126,12 +126,14 @@ void BackupHandler::SingleKvStoreBackup(const StoreMetaData &metaData) ZLOGD("SingleKvStoreBackup export success."); RemoveFile(backupBackFullName); Reporter::GetInstance()->BehaviourReporter()->Report( - {metaData.account, metaData.appId, metaData.storeId, BehaviourType::DATABASE_BACKUP_SUCCESS}); + {metaData.account, metaData.appId, metaData.storeId, + BehaviourType::DATABASE_BACKUP, BehaviourResult::BEHAVIOUR_SUCCESS}); } else { ZLOGE("SingleKvStoreBackup export failed, status is %d.", status); RenameFile(backupBackFullName, backupFullName); Reporter::GetInstance()->BehaviourReporter()->Report( - {metaData.account, metaData.appId, metaData.storeId, BehaviourType::DATABASE_BACKUP_FAILED}); + {metaData.account, metaData.appId, metaData.storeId, + BehaviourType::DATABASE_BACKUP, BehaviourResult::BEHAVIOUR_FAILED}); } } delegateMgr.CloseKvStore(delegate); diff --git a/services/distributeddataservice/app/src/kvstore_app_manager.cpp b/services/distributeddataservice/app/src/kvstore_app_manager.cpp index 95d0571d..ad6ac310 100644 --- a/services/distributeddataservice/app/src/kvstore_app_manager.cpp +++ b/services/distributeddataservice/app/src/kvstore_app_manager.cpp @@ -33,6 +33,8 @@ #include "kvstore_app_accessor.h" #include "kvstore_utils.h" #include "log_print.h" +#include "metadata/corrupted_meta_data.h" +#include "metadata/meta_data_manager.h" #include "permission_validator.h" #include "reporter.h" #include "route_head_handler_impl.h" @@ -240,6 +242,8 @@ Status KvStoreAppManager::DeleteKvStore(const std::string &storeId) Status statusDE = DeleteKvStore(storeId, PATH_DE); Status statusCE = DeleteKvStore(storeId, PATH_CE); if (statusDE == Status::SUCCESS || statusCE == Status::SUCCESS) { + CorruptedMetaData corruptedMetaData = CorruptedMetaData(trueAppId_, bundleName_, storeId); + MetaDataManager::GetInstance().DelMeta(corruptedMetaData.GetKey(), true); return Status::SUCCESS; } diff --git a/services/distributeddataservice/app/src/kvstore_data_service.h b/services/distributeddataservice/app/src/kvstore_data_service.h index abcf1d0f..732e0925 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.h +++ b/services/distributeddataservice/app/src/kvstore_data_service.h @@ -27,6 +27,7 @@ #include "ikvstore_data_service.h" #include "kvstore_device_listener.h" #include "kvstore_user_manager.h" +#include "metadata/corrupted_meta_data.h" #include "metadata/store_meta_data.h" #include "reporter.h" #include "security/security.h" @@ -52,6 +53,7 @@ class KvStoreDataService : public SystemAbility, public KvStoreDataServiceStub { DECLARE_SYSTEM_ABILITY(KvStoreDataService); public: + using CorruptedMetaData = DistributedData::CorruptedMetaData; using StoreMetaData = DistributedData::StoreMetaData; // record kvstore meta version for compatible, should update when modify kvstore meta structure. static constexpr uint32_t STORE_VERSION = 0x03000001; diff --git a/services/distributeddataservice/app/src/single_kvstore_impl.cpp b/services/distributeddataservice/app/src/single_kvstore_impl.cpp index e6ff0c81..ada3a520 100644 --- a/services/distributeddataservice/app/src/single_kvstore_impl.cpp +++ b/services/distributeddataservice/app/src/single_kvstore_impl.cpp @@ -29,9 +29,6 @@ #include "ipc_skeleton.h" #include "log_print.h" #include "permission_validator.h" -#include "preferences.h" -#include "preferences_errno.h" -#include "preferences_helper.h" #include "query_helper.h" #include "reporter.h" #include "upgrade_manager.h" @@ -41,7 +38,6 @@ namespace OHOS::DistributedKv { using namespace OHOS::DistributedData; -using namespace OHOS::NativePreferences; static bool TaskIsBackground(pid_t pid) { std::ifstream ifs("/proc/" + std::to_string(pid) + "/cgroup", std::ios::in); @@ -136,24 +132,18 @@ Status SingleKvStoreImpl::CheckDbIsCorrupted(DistributedDB::DBStatus status, con bool SingleKvStoreImpl::IsDbCorruptedFirstTime(bool corruptedStatus) const { - int errCode = E_OK; - std::string prefKey = bundleName_ + storeId_; - std::shared_ptr pref = PreferencesHelper::GetPreferences(Constant::ROOT_PATH_PERF, errCode); - if ((errCode =! E_OK) || pref == nullptr) { - return true; - } + CorruptedMetaData corruptedMetaData = CorruptedMetaData(appId_, bundleName_, storeId_); + MetaDataManager::GetInstance().LoadMeta(corruptedMetaData.GetKey(), corruptedMetaData, true); if (corruptedStatus) { - auto ret = pref->GetBool(prefKey, false); - if (ret) { + if (corruptedMetaData.CorruptedStatus == true) { return false; } else { - pref->PutBool(prefKey, corruptedStatus); - pref->Flush(); + corruptedMetaData.CorruptedStatus = true; + MetaDataManager::GetInstance().SaveMeta(corruptedMetaData.GetKey(), corruptedMetaData, true); return true; } } else { - pref->Delete(prefKey); - pref->Flush(); + MetaDataManager::GetInstance().DelMeta(corruptedMetaData.GetKey(), true); return false; } return false; @@ -1439,8 +1429,8 @@ bool SingleKvStoreImpl::Import(const std::string &bundleName) const auto result = std::make_unique()->SingleKvStoreRecover(metaData, kvStoreNbDelegate_); (void) IsDbCorruptedFirstTime((!result)); Reporter::GetInstance()->BehaviourReporter()->Report( - { deviceAccountId_, bundleName_, storeId_, - (result) ? BehaviourType::DATABASE_RECOVERY_SUCCESS : BehaviourType::DATABASE_RECOVERY_FAILED }); + { deviceAccountId_, bundleName_, storeId_, BehaviourType::DATABASE_RECOVERY, + (result) ? BehaviourResult::BEHAVIOUR_FAILED : BehaviourResult::BEHAVIOUR_FAILED }); return result; } diff --git a/services/distributeddataservice/framework/BUILD.gn b/services/distributeddataservice/framework/BUILD.gn index 67b522ce..48c932b1 100644 --- a/services/distributeddataservice/framework/BUILD.gn +++ b/services/distributeddataservice/framework/BUILD.gn @@ -37,6 +37,7 @@ ohos_shared_library("distributeddatasvcfwk") { "eventcenter/event_center.cpp", "metadata/capability_meta_data.cpp", "metadata/capability_range.cpp", + "metadata/corrupted_meta_data.cpp", "metadata/meta_data.cpp", "metadata/meta_data_manager.cpp", "metadata/secret_key_meta_data.cpp", diff --git a/services/distributeddataservice/framework/include/metadata/corrupted_meta_data.h b/services/distributeddataservice/framework/include/metadata/corrupted_meta_data.h new file mode 100644 index 00000000..8a07a36f --- /dev/null +++ b/services/distributeddataservice/framework/include/metadata/corrupted_meta_data.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DISTRIBUTEDDATAMGR_CORRUPTED_META_DATA_H +#define DISTRIBUTEDDATAMGR_CORRUPTED_META_DATA_H +#include "serializable/serializable.h" +namespace OHOS { +namespace DistributedData { +struct API_EXPORT CorruptedMetaData final : public Serializable { + std::string appId = ""; + std::string bundleName = ""; + std::string storeId = ""; + bool CorruptedStatus = false; + API_EXPORT CorruptedMetaData(); + API_EXPORT CorruptedMetaData(const std::string &appId, const std::string &bundleName, const std::string &storeId); + API_EXPORT ~CorruptedMetaData(); + API_EXPORT bool Marshal(json &node) const override; + API_EXPORT bool Unmarshal(const json &node) override; + + API_EXPORT std::string GetKey(); +private: + static constexpr const char *PREFIX = "CorruptedMetaData"; +}; +} // namespace DistributedData +} // namespace OHOS + +#endif // DISTRIBUTEDDATAMGR_CORRUPTED_META_DATA_H diff --git a/services/distributeddataservice/framework/metadata/corrupted_meta_data.cpp b/services/distributeddataservice/framework/metadata/corrupted_meta_data.cpp new file mode 100644 index 00000000..23cd0098 --- /dev/null +++ b/services/distributeddataservice/framework/metadata/corrupted_meta_data.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "metadata/corrupted_meta_data.h" + +#include "utils/constant.h" +namespace OHOS { +namespace DistributedData { + +bool CorruptedMetaData::Marshal(json &node) const +{ + SetValue(node[GET_NAME(appId)], appId); + SetValue(node[GET_NAME(bundleName)], bundleName); + SetValue(node[GET_NAME(storeId)], storeId); + SetValue(node[GET_NAME(CorruptedStatus)], CorruptedStatus); + return true; +} +bool CorruptedMetaData::Unmarshal(const json &node) +{ + GetValue(node, GET_NAME(appId), appId); + GetValue(node, GET_NAME(bundleName), bundleName); + GetValue(node, GET_NAME(storeId), storeId); + GetValue(node, GET_NAME(CorruptedStatus), CorruptedStatus); + return true; +} + +CorruptedMetaData::CorruptedMetaData() +{ +} + +CorruptedMetaData::~CorruptedMetaData() +{ +} + +CorruptedMetaData::CorruptedMetaData( + const std::string &appId, const std::string &bundleName, const std::string &storeId) + : appId(appId), bundleName(bundleName), storeId(storeId) +{ +} + +std::string CorruptedMetaData::GetKey() +{ + return Constant::Join(PREFIX, Constant::KEY_SEPARATOR, + { appId, bundleName, storeId }); +} +} // namespace DistributedData +} // namespace OHOS