From 3ce455f68c236ead6f76530045c0b5f1d2c76f9f Mon Sep 17 00:00:00 2001 From: luqing Date: Sun, 8 Jun 2025 16:01:48 +0800 Subject: [PATCH] add acl object Signed-off-by: luqing --- .../service/object/BUILD.gn | 1 + .../service/object/include/object_manager.h | 5 + .../service/object/src/object_manager.cpp | 101 +++++++++++++--- .../object/src/object_service_impl.cpp | 1 + .../service/test/object_manager_test.cpp | 114 ++++++++++++++++++ 5 files changed, 204 insertions(+), 18 deletions(-) diff --git a/services/distributeddataservice/service/object/BUILD.gn b/services/distributeddataservice/service/object/BUILD.gn index e76a7510e..50fbf0f82 100644 --- a/services/distributeddataservice/service/object/BUILD.gn +++ b/services/distributeddataservice/service/object/BUILD.gn @@ -17,6 +17,7 @@ config("object_public_config") { visibility = [ ":*" ] include_dirs = [ + "${data_service_path}/service/matrix/include", "${data_service_path}/service/common", "${data_service_path}/adapter/include/communicator", "${data_service_path}/adapter/include/utils", diff --git a/services/distributeddataservice/service/object/include/object_manager.h b/services/distributeddataservice/service/object/include/object_manager.h index 158b5c50a..3eb55fb97 100644 --- a/services/distributeddataservice/service/object/include/object_manager.h +++ b/services/distributeddataservice/service/object/include/object_manager.h @@ -21,6 +21,7 @@ #include "device_manager_adapter.h" #include "kv_store_delegate_manager.h" #include "kvstore_sync_callback.h" +#include "metadata/store_meta_data.h" #include "object_asset_loader.h" #include "object_callback.h" #include "object_callback_proxy.h" @@ -64,6 +65,7 @@ class ObjectStoreManager { public: using DmAdaper = OHOS::DistributedData::DeviceManagerAdapter; using UriToSnapshot = std::shared_ptr>>; + using StoreMetaData = OHOS::DistributedData::StoreMetaData; enum RestoreStatus : int32_t { NONE = 0, @@ -163,6 +165,9 @@ private: void ProcessSyncCallback(const std::map &results, const std::string &appId, const std::string &sessionId, const std::string &deviceId); void SaveUserToMeta(); + int32_t StoreMetaAndDoSync(const std::string &dstBundleName, const std::string &deviceId, + const std::string &sessionId, sptr proxy); + bool IsNeedMetaSync(const StoreMetaData &meta, const std::vector &networkIds); std::string GetCurrentUser(); void DoNotify(uint32_t tokenId, const CallbackInfo& value, const std::map& data, bool allReady); diff --git a/services/distributeddataservice/service/object/src/object_manager.cpp b/services/distributeddataservice/service/object/src/object_manager.cpp index 859de7085..71c4b1638 100644 --- a/services/distributeddataservice/service/object/src/object_manager.cpp +++ b/services/distributeddataservice/service/object/src/object_manager.cpp @@ -25,9 +25,12 @@ #include "common/bytes.h" #include "common/string_utils.h" #include "datetime_ex.h" +#include "device_matrix.h" #include "distributed_file_daemon_manager.h" #include "kvstore_utils.h" #include "log_print.h" +#include "ipc_skeleton.h" +#include "metadata/capability_meta_data.h" #include "metadata/meta_data_manager.h" #include "metadata/object_user_meta_data.h" #include "metadata/store_meta_data.h" @@ -44,7 +47,9 @@ using Account = OHOS::DistributedData::AccountDelegate; using AccessTokenKit = Security::AccessToken::AccessTokenKit; using ValueProxy = OHOS::DistributedData::ValueProxy; using DistributedFileDaemonManager = Storage::DistributedFile::DistributedFileDaemonManager; +using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter; constexpr const char *SAVE_INFO = "p_###SAVEINFO###"; +constexpr const char *DEFAULT_USERID = "0"; ObjectStoreManager::ObjectStoreManager() { ZLOGI("ObjectStoreManager construct"); @@ -156,23 +161,12 @@ int32_t ObjectStoreManager::Save(const std::string &appId, const std::string &se proxy->Completed(std::map()); return result; } - ZLOGI("Sync data, bundleName: %{public}s, sessionId: %{public}s, deviceId: %{public}s", dstBundleName.c_str(), - sessionId.c_str(), Anonymous::Change(deviceId).c_str()); - SyncCallBack syncCallback = - [proxy, dstBundleName, sessionId, deviceId, this](const std::map &results) { - ProcessSyncCallback(results, dstBundleName, sessionId, deviceId); - proxy->Completed(results); - }; - result = SyncOnStore(GetPropertyPrefix(dstBundleName, sessionId, deviceId), {deviceId}, syncCallback); + result = StoreMetaAndDoSync(dstBundleName, deviceId, sessionId, proxy); if (result != OBJECT_SUCCESS) { - ZLOGE("Sync data failed, result: %{public}d", result); - ObjectStore::RadarReporter::ReportStateError(std::string(__FUNCTION__), ObjectStore::SAVE, - ObjectStore::SYNC_DATA, ObjectStore::RADAR_FAILED, result, ObjectStore::FINISHED); - Close(); - proxy->Completed(std::map()); + ZLOGE("Save meta data and sync datafailed, result: %{public}d", result); return result; } - Close(); + return PushAssets(appId, dstBundleName, sessionId, data, deviceId); } @@ -919,10 +913,6 @@ int32_t ObjectStoreManager::SyncOnStore( callback({{LOCAL_DEVICE, OBJECT_SUCCESS}}); return OBJECT_SUCCESS; } - if (!DeviceManagerAdapter::GetInstance().IsSameAccount(device)) { - ZLOGE("IsSameAccount failed. device:%{public}s", Anonymous::Change(device).c_str()); - continue; - } syncDevices.emplace_back(DmAdaper::GetInstance().GetUuidByNetworkId(device)); } if (syncDevices.empty()) { @@ -1128,6 +1118,81 @@ void ObjectStoreManager::SaveUserToMeta() } } +bool ObjectStoreManager::IsNeedMetaSync(const StoreMetaData &meta, const std::vector &uuids) +{ + bool isAfterMeta = false; + for (const auto &uuid : uuids) { + auto metaData = meta; + metaData.deviceId = uuid; + CapMetaData capMeta; + auto capKey = CapMetaRow::GetKeyFor(uuid); + if (!MetaDataManager::GetInstance().LoadMeta(std::string(capKey.begin(), capKey.end()), capMeta) || + !MetaDataManager::GetInstance().LoadMeta(metaData.GetKey(), metaData)) { + isAfterMeta = true; + break; + } + auto [exist, mask] = DeviceMatrix::GetInstance().GetRemoteMask(uuid); + if ((mask & DeviceMatrix::META_STORE_MASK) == DeviceMatrix::META_STORE_MASK) { + isAfterMeta = true; + break; + } + auto [existLocal, localMask] = DeviceMatrix::GetInstance().GetMask(uuid); + if ((localMask & DeviceMatrix::META_STORE_MASK) == DeviceMatrix::META_STORE_MASK) { + isAfterMeta = true; + break; + } + } + return isAfterMeta; +} + +int32_t ObjectStoreManager::StoreMetaAndDoSync(const std::string &dstBundleName, const std::string &deviceId, + const std::string &sessionId, sptr proxy) +{ + const auto &localDeviceId = DmAdapter::GetInstance().GetLocalDevice().uuid; + if (localDeviceId.empty()) { + ZLOGE("localDeviceId is empty"); + return OBJECT_INNER_ERROR; + } + StoreMetaData meta; + meta.deviceId = localDeviceId; + meta.user = DEFAULT_USERID; + meta.storeId = ObjectCommon::OBJECTSTORE_DB_STOREID; + meta.bundleName = Bootstrap::GetInstance().GetProcessLabel(); + if (!MetaDataManager::GetInstance().LoadMeta(meta.GetKey(), meta, true)) { + ZLOGE("Failed to load meta data, key: %{public}s", meta.GetKey().c_str()); + return OBJECT_INNER_ERROR; + } + + const auto &uuid = DmAdapter::GetInstance().ToUUID(deviceId); + const std::vector uuids = {uuid}; + if (IsNeedMetaSync(meta, uuids) && + !DistributedData::MetaDataManager::GetInstance().Sync(uuids, + [proxy, dstBundleName, sessionId, deviceId, this](auto &results) { + ZLOGI("Sync data, bundleName: %{public}s, sessionId: %{public}s, deviceId: %{public}s", + dstBundleName.c_str(), sessionId.c_str(), Anonymous::Change(deviceId).c_str()); + SyncCallBack syncCallback = + [proxy, dstBundleName, sessionId, deviceId, this](const std::map &results) { + ProcessSyncCallback(results, dstBundleName, sessionId, deviceId); + proxy->Completed(results); + }; + int32_t result = SyncOnStore(GetPropertyPrefix(dstBundleName, sessionId, deviceId), + {deviceId}, syncCallback); + if (result != OBJECT_SUCCESS) { + ZLOGE("Sync data failed, result: %{public}d", result); + ObjectStore::RadarReporter::ReportStateError(std::string(__FUNCTION__), ObjectStore::SAVE, + ObjectStore::SYNC_DATA, ObjectStore::RADAR_FAILED, result, ObjectStore::FINISHED); + Close(); + proxy->Completed(std::map()); + return; + } + Close(); + })) { + ZLOGE("bundleName: %{public}s, meta sync failed", dstBundleName.c_str()); + return OBJECT_INNER_ERROR; + } + return OBJECT_SUCCESS; +} + void ObjectStoreManager::CloseAfterMinute() { executors_->Schedule(std::chrono::minutes(INTERVAL), [this]() { diff --git a/services/distributeddataservice/service/object/src/object_service_impl.cpp b/services/distributeddataservice/service/object/src/object_service_impl.cpp index 71f6195e7..8149e0086 100644 --- a/services/distributeddataservice/service/object/src/object_service_impl.cpp +++ b/services/distributeddataservice/service/object/src/object_service_impl.cpp @@ -170,6 +170,7 @@ int32_t ObjectServiceImpl::SaveMetaData(StoreMetaData &saveMeta, const std::stri saveMeta.storeType = ObjectDistributedType::OBJECT_SINGLE_VERSION; saveMeta.dataType = DistributedKv::DataType::TYPE_DYNAMICAL; saveMeta.dataDir = DistributedData::DirectoryManager::GetInstance().GetStorePath(saveMeta); + saveMeta.authType = DistributedKv::AuthType::IDENTICAL_ACCOUNT; bool isSaved = DistributedData::MetaDataManager::GetInstance().SaveMeta(saveMeta.GetKey(), saveMeta) && DistributedData::MetaDataManager::GetInstance().SaveMeta(saveMeta.GetKey(), saveMeta, true); if (!isSaved) { diff --git a/services/distributeddataservice/service/test/object_manager_test.cpp b/services/distributeddataservice/service/test/object_manager_test.cpp index 61eb66a80..05a5dcc5e 100644 --- a/services/distributeddataservice/service/test/object_manager_test.cpp +++ b/services/distributeddataservice/service/test/object_manager_test.cpp @@ -1111,4 +1111,118 @@ HWTEST_F(ObjectManagerTest, InitUserMeta001, TestSize.Level1) auto status = manager->InitUserMeta(); ASSERT_EQ(status, DistributedObject::OBJECT_SUCCESS); } + +/** +* @tc.name: IsNeedMetaSync001 +* @tc.desc: test is need meta sync. +* @tc.type: FUNC +*/ +HWTEST_F(ObjectManagerTest, IsNeedMetaSync001, TestSize.Level0) +{ + auto manager = ObjectStoreManager::GetInstance(); + StoreMetaData meta; + meta.deviceId = "test_device_id"; + meta.user = "0"; + meta.storeId = "distributedObject_"; + meta.bundleName = "test_bundle"; + + std::vectorstd::string uuids = {"test_uuid"}; + + EXPECT_CALL(*metaDataManagerMock, LoadMeta(testing::, testing::, testing::_)) + .WillOnce(Return(false)); + + bool result = manager->IsNeedMetaSync(meta, uuids); + EXPECT_TRUE(result); +} + +/** +* @tc.name: IsNeedMetaSync002 +* @tc.desc: test is need meta sync. +* @tc.type: FUNC +*/ +HWTEST_F(ObjectManagerTest, IsNeedMetaSync002, TestSize.Level0) +{ + auto manager = ObjectStoreManager::GetInstance(); + StoreMetaData meta; + meta.deviceId = "test_device_id"; + meta.user = "0"; + meta.storeId = "distributedObject_"; + meta.bundleName = "test_bundle"; + std::vectorstd::string uuids = {"test_uuid"}; + + EXPECT_CALL(*metaDataManagerMock, LoadMeta(testing::, testing::, testing::_)) + .WillRepeatedly(Return(true)); + + bool result = manager->IsNeedMetaSync(meta, uuids); + EXPECT_EQ(result, false); +} + +/** +* @tc.name: StoreMetaAndDoSync001 +* @tc.desc: test store meta and do sync. +* @tc.type: FUNC +*/ +HWTEST_F(ObjectManagerTest, StoreMetaAndDoSync001, TestSize.Level0) +{ + auto manager = ObjectStoreManager::GetInstance(); + std::string dstBundleName = "test_bundle"; + std::string deviceId = "test_device"; + std::string sessionId = "test_session"; + sptr impl = nullptr; + sptr proxy = new ObjectSaveCallbackProxy(impl); + + EXPECT_CALL(*devMgrAdapterMock, GetLocalDevice()) + .WillOnce(Return(DeviceInfo{""})); + + int32_t result = manager->StoreMetaAndDoSync(dstBundleName, deviceId, sessionId, proxy); + EXPECT_EQ(result, OBJECT_INNER_ERROR); +} + +/** +* @tc.name: StoreMetaAndDoSync002 +* @tc.desc: test store meta and do sync. +* @tc.type: FUNC +*/ +HWTEST_F(ObjectManagerTest, StoreMetaAndDoSync002, TestSize.Level0) +{ + auto manager = ObjectStoreManager::GetInstance(); + std::string dstBundleName = "test_bundle"; + std::string deviceId = "test_device"; + std::string sessionId = "test_session"; + sptr impl = nullptr; + sptr proxy = new ObjectSaveCallbackProxy(impl); + + EXPECT_CALL(*devMgrAdapterMock, GetLocalDevice()) + .WillOnce(Return(DeviceInfo{"test_local_device"})); + + EXPECT_CALL(*metaDataManagerMock, LoadMeta(testing::, testing::, testing::_)) + .WillOnce(Return(false)); + + int32_t result = manager->StoreMetaAndDoSync(dstBundleName, deviceId, sessionId, proxy); + EXPECT_EQ(result, OBJECT_INNER_ERROR); +} + +/** +* @tc.name: StoreMetaAndDoSync003 +* @tc.desc: test store meta and do sync. +* @tc.type: FUNC +*/ +HWTEST_F(ObjectManagerTest, StoreMetaAndDoSync003, TestSize.Level0) +{ + auto manager = ObjectStoreManager::GetInstance(); + std::string dstBundleName = "test_bundle"; + std::string deviceId = "test_device"; + std::string sessionId = "test_session"; + sptr impl = nullptr; + sptr proxy = new ObjectSaveCallbackProxy(impl); + + EXPECT_CALL(*devMgrAdapterMock, GetLocalDevice()) + .WillOnce(Return(DeviceInfo{"test_local_device"})); + + EXPECT_CALL(*metaDataManagerMock, LoadMeta(testing::, testing::, testing::_)) + .WillRepeatedly(Return(true)); + + int32_t result = manager->StoreMetaAndDoSync(dstBundleName, deviceId, sessionId, proxy); + EXPECT_TRUE(result); +} } // namespace OHOS::Test -- Gitee