From 2599b06f70b047ae647eca655326bef49a845206 Mon Sep 17 00:00:00 2001 From: xianghengliang Date: Fri, 14 Jan 2022 16:57:55 +0800 Subject: [PATCH 1/9] use udid to get hichain groups Signed-off-by: xianghengliang --- .../include/device/device_manager_agent.h | 2 +- .../src/device/device_manager_agent.cpp | 58 ++++++++++++++++--- 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/services/distributedfiledaemon/include/device/device_manager_agent.h b/services/distributedfiledaemon/include/device/device_manager_agent.h index ad9ae0c36..3f5d941c1 100644 --- a/services/distributedfiledaemon/include/device/device_manager_agent.h +++ b/services/distributedfiledaemon/include/device/device_manager_agent.h @@ -84,7 +84,7 @@ private: void UnregisterFromExternalDm(); void AuthGroupOfflineProc(const DeviceInfo &info); - void QueryRelatedGroups(const std::string &networkId, std::vector &groupList); + void QueryRelatedGroups(const std::string &udid, std::vector &groupList); bool CheckIsAuthGroup(const GroupInfo &group); void AllAuthGroupsOfflineProc(); // We use a mutex instead of a shared_mutex to serialize online/offline procedures diff --git a/services/distributedfiledaemon/src/device/device_manager_agent.cpp b/services/distributedfiledaemon/src/device/device_manager_agent.cpp index d5129fae1..eae0ea5a4 100644 --- a/services/distributedfiledaemon/src/device/device_manager_agent.cpp +++ b/services/distributedfiledaemon/src/device/device_manager_agent.cpp @@ -32,7 +32,6 @@ namespace Storage { namespace DistributedFile { namespace { constexpr int MAX_RETRY_COUNT = 7; -constexpr int IDENTICAL_ACCOUNT_GROUP = 1; constexpr int PEER_TO_PEER_GROUP = 256; constexpr int ACROSS_ACCOUNT_AUTHORIZE_GROUP = 1282; } // namespace @@ -137,7 +136,7 @@ void DeviceManagerAgent::ReconnectOnlineDevices() void DeviceManagerAgent::OnDeviceOnline(const DistributedHardware::DmDeviceInfo &deviceInfo) { - LOGI("OnDeviceOnline begin"); + LOGI("netwrorkId %{public}s, OnDeviceOnline begin", deviceInfo.deviceId); DeviceInfo info(deviceInfo); { unique_lock lock(mpToNetworksMutex_); @@ -193,19 +192,60 @@ void from_json(const nlohmann::json &jsonObject, GroupInfo &groupInfo) } } -void DeviceManagerAgent::QueryRelatedGroups(const std::string &networkId, std::vector &groupList) +void DeviceManagerAgent::QueryRelatedGroups(const std::string &udid, std::vector &groupList) { - // piling test - GroupInfo g1("auth_group_test1", "groupId_12345", "wps_package", PEER_TO_PEER_GROUP); - groupList.emplace_back(g1); - GroupInfo g2("auth_group_test2", "groupId_987654", "qqMail_package", IDENTICAL_ACCOUNT_GROUP); - groupList.emplace_back(g2); + LOGI("use udid %{public}s query hichain related groups", udid.c_str()); + int ret = InitDeviceAuthService(); + if (ret != 0) { + LOGE("InitDeviceAuthService failed, ret %{public}d", ret); + return; + } + + hichainDeviceGroupManager_ = GetGmInstance(); + if (hichainDeviceGroupManager_ == nullptr) { + LOGE("failed to get hichain device group manager"); + return; + } + + char *returnGroupVec = nullptr; + uint32_t groupNum = 0; + ret = hichainDeviceGroupManager_->getRelatedGroups(IDaemon::SERVICE_NAME.c_str(), udid.c_str(), &returnGroupVec, &groupNum); + if (ret != 0 || returnGroupVec == nullptr) { + LOGE("failed to get related groups, ret %{public}d", ret); + return; + } + + if (groupNum == 0) { + LOGE("failed to get related groups, groupNum is %{public}d", groupNum); + return; + } + + std::string groups = std::string(returnGroupVec); + nlohmann::json jsonObject = nlohmann::json::parse(groups); // transform from cjson to cppjson + if (jsonObject.is_discarded()) { + LOGE("returnGroupVec parse failed"); + return; + } + + groupList = jsonObject.get>(); + for (auto &a : groupList) { + LOGI("group info:[groupName] %{public}s, [groupId] %{public}s, [groupOwner] %{public}s,[groupId] %{public}d,", a.groupName.c_str(), a.groupId.c_str(), a.groupOwner.c_str(), a.groupType); + } + return; } void DeviceManagerAgent::AuthGroupOnlineProc(const DeviceInfo info) { + // convert networkId to uuid + // todo: udid获取放到DeviceInfo类中实现 + std::string udid; + auto &deviceManager = DistributedHardware::DeviceManager::GetInstance(); + int32_t ret = deviceManager.GetUdidByNetworkId(IDaemon::SERVICE_NAME, info.GetCid(), udid); + std::string uuid; + ret += deviceManager.GetUuidByNetworkId(IDaemon::SERVICE_NAME, info.GetCid(), uuid); + LOGI("ret %{public}d, get udid %{public}s, uuid %{public}s", ret, udid.c_str(), uuid.c_str()); std::vector groupList; - QueryRelatedGroups(info.GetCid(), groupList); + QueryRelatedGroups(udid, groupList); for (const auto &group : groupList) { if (!CheckIsAuthGroup(group)) { continue; -- Gitee From 08415fd768f9d11bd9f93cf477807ecd97446109 Mon Sep 17 00:00:00 2001 From: xianghengliang Date: Mon, 17 Jan 2022 15:36:59 +0800 Subject: [PATCH 2/9] add query group info by groupid interface Signed-off-by: xianghengliang --- .../include/device/device_manager_agent.h | 6 +- .../src/device/device_manager_agent.cpp | 71 +++++++++++++++---- 2 files changed, 61 insertions(+), 16 deletions(-) diff --git a/services/distributedfiledaemon/include/device/device_manager_agent.h b/services/distributedfiledaemon/include/device/device_manager_agent.h index 3f5d941c1..5e9839ce4 100644 --- a/services/distributedfiledaemon/include/device/device_manager_agent.h +++ b/services/distributedfiledaemon/include/device/device_manager_agent.h @@ -85,14 +85,16 @@ private: void AuthGroupOfflineProc(const DeviceInfo &info); void QueryRelatedGroups(const std::string &udid, std::vector &groupList); + bool QueryGroupInfoById(const std::string &groupId, std::vector &groupList); bool CheckIsAuthGroup(const GroupInfo &group); void AllAuthGroupsOfflineProc(); // We use a mutex instead of a shared_mutex to serialize online/offline procedures std::mutex mpToNetworksMutex_; std::map> mpToNetworks_; DeviceInfo localDeviceInfo_; - std::unordered_map> authGroupMap_; - const DeviceGroupManager *hichainDeviceGroupManager_{nullptr}; + + // key:groupId val:groupIdMark, the set of networkId + std::unordered_map>> authGroupMap_; }; } // namespace DistributedFile } // namespace Storage diff --git a/services/distributedfiledaemon/src/device/device_manager_agent.cpp b/services/distributedfiledaemon/src/device/device_manager_agent.cpp index eae0ea5a4..7d095ae56 100644 --- a/services/distributedfiledaemon/src/device/device_manager_agent.cpp +++ b/services/distributedfiledaemon/src/device/device_manager_agent.cpp @@ -191,6 +191,43 @@ void from_json(const nlohmann::json &jsonObject, GroupInfo &groupInfo) groupInfo.groupType = jsonObject.at(FIELD_GROUP_TYPE).get(); } } +bool DeviceManagerAgent::QueryGroupInfoById(const std::string &groupId, std::vector &groupList) +{ + LOGI("use groupId %{public}s query hichain related groups", groupId.c_str()); + int ret = InitDeviceAuthService(); + if (ret != 0) { + LOGE("InitDeviceAuthService failed, ret %{public}d", ret); + return false; + } + + auto hichainDevGroupMgr_ = GetGmInstance(); + if (hichainDevGroupMgr_ == nullptr) { + LOGE("failed to get hichain device group manager"); + return false; + } + + char *returnGroupVec = nullptr; + ret = hichainDevGroupMgr_->getGroupInfoById(IDaemon::SERVICE_NAME.c_str(), groupId.c_str(), &returnGroupVec); + if (ret != 0 || returnGroupVec == nullptr) { + LOGE("failed to get related groups, ret %{public}d", ret); + return false; + } + + std::string groups = std::string(returnGroupVec); + nlohmann::json jsonObject = nlohmann::json::parse(groups); // transform from cjson to cppjson + if (jsonObject.is_discarded()) { + LOGE("returnGroupVec parse failed"); + return false; + } + + groupList = jsonObject.get>(); + for (auto &a : groupList) { + LOGI("group info:[groupName] %{public}s, [groupId] %{public}s, [groupOwner] %{public}s,[groupId] %{public}d,", + a.groupName.c_str(), a.groupId.c_str(), a.groupOwner.c_str(), a.groupType); + } + + return true; +} void DeviceManagerAgent::QueryRelatedGroups(const std::string &udid, std::vector &groupList) { @@ -201,15 +238,16 @@ void DeviceManagerAgent::QueryRelatedGroups(const std::string &udid, std::vector return; } - hichainDeviceGroupManager_ = GetGmInstance(); - if (hichainDeviceGroupManager_ == nullptr) { + auto hichainDevGroupMgr_ = GetGmInstance(); + if (hichainDevGroupMgr_ == nullptr) { LOGE("failed to get hichain device group manager"); return; } char *returnGroupVec = nullptr; uint32_t groupNum = 0; - ret = hichainDeviceGroupManager_->getRelatedGroups(IDaemon::SERVICE_NAME.c_str(), udid.c_str(), &returnGroupVec, &groupNum); + ret = hichainDevGroupMgr_->getRelatedGroups(IDaemon::SERVICE_NAME.c_str(), udid.c_str(), &returnGroupVec, + &groupNum); if (ret != 0 || returnGroupVec == nullptr) { LOGE("failed to get related groups, ret %{public}d", ret); return; @@ -229,7 +267,8 @@ void DeviceManagerAgent::QueryRelatedGroups(const std::string &udid, std::vector groupList = jsonObject.get>(); for (auto &a : groupList) { - LOGI("group info:[groupName] %{public}s, [groupId] %{public}s, [groupOwner] %{public}s,[groupId] %{public}d,", a.groupName.c_str(), a.groupId.c_str(), a.groupOwner.c_str(), a.groupType); + LOGI("group info:[groupName] %{public}s, [groupId] %{public}s, [groupOwner] %{public}s,[groupId] %{public}d,", + a.groupName.c_str(), a.groupId.c_str(), a.groupOwner.c_str(), a.groupType); } return; } @@ -251,11 +290,15 @@ void DeviceManagerAgent::AuthGroupOnlineProc(const DeviceInfo info) continue; } if (authGroupMap_.find(group.groupId) == authGroupMap_.end()) { - LOGI("groupId %{public}s not exist, then mount", group.groupId.c_str()); + LOGI("groupId %{public}s not exist, map size %{public}d, then mount", group.groupId.c_str(), + authGroupMap_.size()); + std::stringstream ss; + ss << "groupId_" << authGroupMap_.size(); MountManager::GetInstance()->Mount(make_unique( - Utils::MountArgumentDescriptors::SetAuthGroupMountArgument(group.groupId, group.groupOwner, true))); + Utils::MountArgumentDescriptors::SetAuthGroupMountArgument(ss.str(), group.groupOwner, true))); + std::get<0>(authGroupMap_[group.groupId]) = ss.str(); } - auto [iter, status] = authGroupMap_[group.groupId].insert(info.GetCid()); + auto [iter, status] = std::get<1>(authGroupMap_[group.groupId]).insert(info.GetCid()); if (status == false) { LOGI("cid %{public}s has already inserted into groupId %{public}s", info.GetCid().c_str(), group.groupId.c_str()); @@ -267,7 +310,7 @@ void DeviceManagerAgent::AuthGroupOnlineProc(const DeviceInfo info) void DeviceManagerAgent::AuthGroupOfflineProc(const DeviceInfo &info) { for (auto iter = authGroupMap_.begin(); iter != authGroupMap_.end();) { - auto set = iter->second; + auto [groupIdMark, set] = iter->second; auto groupId = iter->first; if (set.find(info.GetCid()) == set.end()) { continue; @@ -277,11 +320,11 @@ void DeviceManagerAgent::AuthGroupOfflineProc(const DeviceInfo &info) LOGI("can not find groupId %{public}s ", groupId.c_str()); continue; } - authGroupMap_[groupId].erase(info.GetCid()); - if (authGroupMap_[groupId].empty()) { + std::get<1>(authGroupMap_[groupId]).erase(info.GetCid()); + if (std::get<1>(authGroupMap_[groupId]).empty()) { std::vector groupList; - if (groupList.size() == 0) { - MountManager::GetInstance()->Umount(groupId); + if (QueryGroupInfoById(groupId, groupList) != 0 || groupList.size() == 0) { + MountManager::GetInstance()->Umount(groupIdMark); iter = authGroupMap_.erase(iter); continue; } @@ -293,8 +336,8 @@ void DeviceManagerAgent::AuthGroupOfflineProc(const DeviceInfo &info) void DeviceManagerAgent::AllAuthGroupsOfflineProc() { for (auto iter = authGroupMap_.begin(); iter != authGroupMap_.end();) { - auto groupId = iter->first; - MountManager::GetInstance()->Umount(groupId); + auto [groupIdMark, ignore] = iter->second; + MountManager::GetInstance()->Umount(groupIdMark); authGroupMap_.erase(iter++); } } -- Gitee From 11f836bd660247766c1ae3f6fd0fbec2a19016db Mon Sep 17 00:00:00 2001 From: xianghengliang Date: Fri, 14 Jan 2022 16:57:55 +0800 Subject: [PATCH 3/9] use udid to get hichain groups Signed-off-by: xianghengliang --- .../include/device/device_manager_agent.h | 2 +- .../src/device/device_manager_agent.cpp | 58 ++++++++++++++++--- 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/services/distributedfiledaemon/include/device/device_manager_agent.h b/services/distributedfiledaemon/include/device/device_manager_agent.h index ad9ae0c36..3f5d941c1 100644 --- a/services/distributedfiledaemon/include/device/device_manager_agent.h +++ b/services/distributedfiledaemon/include/device/device_manager_agent.h @@ -84,7 +84,7 @@ private: void UnregisterFromExternalDm(); void AuthGroupOfflineProc(const DeviceInfo &info); - void QueryRelatedGroups(const std::string &networkId, std::vector &groupList); + void QueryRelatedGroups(const std::string &udid, std::vector &groupList); bool CheckIsAuthGroup(const GroupInfo &group); void AllAuthGroupsOfflineProc(); // We use a mutex instead of a shared_mutex to serialize online/offline procedures diff --git a/services/distributedfiledaemon/src/device/device_manager_agent.cpp b/services/distributedfiledaemon/src/device/device_manager_agent.cpp index af568a2fd..618585a49 100644 --- a/services/distributedfiledaemon/src/device/device_manager_agent.cpp +++ b/services/distributedfiledaemon/src/device/device_manager_agent.cpp @@ -32,7 +32,6 @@ namespace Storage { namespace DistributedFile { namespace { constexpr int MAX_RETRY_COUNT = 7; -constexpr int IDENTICAL_ACCOUNT_GROUP = 1; constexpr int PEER_TO_PEER_GROUP = 256; constexpr int ACROSS_ACCOUNT_AUTHORIZE_GROUP = 1282; } // namespace @@ -143,7 +142,7 @@ void DeviceManagerAgent::ReconnectOnlineDevices() void DeviceManagerAgent::OnDeviceOnline(const DistributedHardware::DmDeviceInfo &deviceInfo) { - LOGI("OnDeviceOnline begin"); + LOGI("netwrorkId %{public}s, OnDeviceOnline begin", deviceInfo.deviceId); DeviceInfo info(deviceInfo); { unique_lock lock(mpToNetworksMutex_); @@ -199,19 +198,60 @@ void from_json(const nlohmann::json &jsonObject, GroupInfo &groupInfo) } } -void DeviceManagerAgent::QueryRelatedGroups(const std::string &networkId, std::vector &groupList) +void DeviceManagerAgent::QueryRelatedGroups(const std::string &udid, std::vector &groupList) { - // piling test - GroupInfo g1("auth_group_test1", "groupId_12345", "wps_package", PEER_TO_PEER_GROUP); - groupList.emplace_back(g1); - GroupInfo g2("auth_group_test2", "groupId_987654", "qqMail_package", IDENTICAL_ACCOUNT_GROUP); - groupList.emplace_back(g2); + LOGI("use udid %{public}s query hichain related groups", udid.c_str()); + int ret = InitDeviceAuthService(); + if (ret != 0) { + LOGE("InitDeviceAuthService failed, ret %{public}d", ret); + return; + } + + hichainDeviceGroupManager_ = GetGmInstance(); + if (hichainDeviceGroupManager_ == nullptr) { + LOGE("failed to get hichain device group manager"); + return; + } + + char *returnGroupVec = nullptr; + uint32_t groupNum = 0; + ret = hichainDeviceGroupManager_->getRelatedGroups(IDaemon::SERVICE_NAME.c_str(), udid.c_str(), &returnGroupVec, &groupNum); + if (ret != 0 || returnGroupVec == nullptr) { + LOGE("failed to get related groups, ret %{public}d", ret); + return; + } + + if (groupNum == 0) { + LOGE("failed to get related groups, groupNum is %{public}d", groupNum); + return; + } + + std::string groups = std::string(returnGroupVec); + nlohmann::json jsonObject = nlohmann::json::parse(groups); // transform from cjson to cppjson + if (jsonObject.is_discarded()) { + LOGE("returnGroupVec parse failed"); + return; + } + + groupList = jsonObject.get>(); + for (auto &a : groupList) { + LOGI("group info:[groupName] %{public}s, [groupId] %{public}s, [groupOwner] %{public}s,[groupId] %{public}d,", a.groupName.c_str(), a.groupId.c_str(), a.groupOwner.c_str(), a.groupType); + } + return; } void DeviceManagerAgent::AuthGroupOnlineProc(const DeviceInfo info) { + // convert networkId to uuid + // todo: udid获取放到DeviceInfo类中实现 + std::string udid; + auto &deviceManager = DistributedHardware::DeviceManager::GetInstance(); + int32_t ret = deviceManager.GetUdidByNetworkId(IDaemon::SERVICE_NAME, info.GetCid(), udid); + std::string uuid; + ret += deviceManager.GetUuidByNetworkId(IDaemon::SERVICE_NAME, info.GetCid(), uuid); + LOGI("ret %{public}d, get udid %{public}s, uuid %{public}s", ret, udid.c_str(), uuid.c_str()); std::vector groupList; - QueryRelatedGroups(info.GetCid(), groupList); + QueryRelatedGroups(udid, groupList); for (const auto &group : groupList) { if (!CheckIsAuthGroup(group)) { continue; -- Gitee From b664ae7121091275a1942a695f4c69826c9ee8eb Mon Sep 17 00:00:00 2001 From: xianghengliang Date: Mon, 17 Jan 2022 15:36:59 +0800 Subject: [PATCH 4/9] add query group info by groupid interface Signed-off-by: xianghengliang --- .../include/device/device_manager_agent.h | 6 +- .../src/device/device_manager_agent.cpp | 71 +++++++++++++++---- 2 files changed, 61 insertions(+), 16 deletions(-) diff --git a/services/distributedfiledaemon/include/device/device_manager_agent.h b/services/distributedfiledaemon/include/device/device_manager_agent.h index 3f5d941c1..5e9839ce4 100644 --- a/services/distributedfiledaemon/include/device/device_manager_agent.h +++ b/services/distributedfiledaemon/include/device/device_manager_agent.h @@ -85,14 +85,16 @@ private: void AuthGroupOfflineProc(const DeviceInfo &info); void QueryRelatedGroups(const std::string &udid, std::vector &groupList); + bool QueryGroupInfoById(const std::string &groupId, std::vector &groupList); bool CheckIsAuthGroup(const GroupInfo &group); void AllAuthGroupsOfflineProc(); // We use a mutex instead of a shared_mutex to serialize online/offline procedures std::mutex mpToNetworksMutex_; std::map> mpToNetworks_; DeviceInfo localDeviceInfo_; - std::unordered_map> authGroupMap_; - const DeviceGroupManager *hichainDeviceGroupManager_{nullptr}; + + // key:groupId val:groupIdMark, the set of networkId + std::unordered_map>> authGroupMap_; }; } // namespace DistributedFile } // namespace Storage diff --git a/services/distributedfiledaemon/src/device/device_manager_agent.cpp b/services/distributedfiledaemon/src/device/device_manager_agent.cpp index 618585a49..829bfad1c 100644 --- a/services/distributedfiledaemon/src/device/device_manager_agent.cpp +++ b/services/distributedfiledaemon/src/device/device_manager_agent.cpp @@ -197,6 +197,43 @@ void from_json(const nlohmann::json &jsonObject, GroupInfo &groupInfo) groupInfo.groupType = jsonObject.at(FIELD_GROUP_TYPE).get(); } } +bool DeviceManagerAgent::QueryGroupInfoById(const std::string &groupId, std::vector &groupList) +{ + LOGI("use groupId %{public}s query hichain related groups", groupId.c_str()); + int ret = InitDeviceAuthService(); + if (ret != 0) { + LOGE("InitDeviceAuthService failed, ret %{public}d", ret); + return false; + } + + auto hichainDevGroupMgr_ = GetGmInstance(); + if (hichainDevGroupMgr_ == nullptr) { + LOGE("failed to get hichain device group manager"); + return false; + } + + char *returnGroupVec = nullptr; + ret = hichainDevGroupMgr_->getGroupInfoById(IDaemon::SERVICE_NAME.c_str(), groupId.c_str(), &returnGroupVec); + if (ret != 0 || returnGroupVec == nullptr) { + LOGE("failed to get related groups, ret %{public}d", ret); + return false; + } + + std::string groups = std::string(returnGroupVec); + nlohmann::json jsonObject = nlohmann::json::parse(groups); // transform from cjson to cppjson + if (jsonObject.is_discarded()) { + LOGE("returnGroupVec parse failed"); + return false; + } + + groupList = jsonObject.get>(); + for (auto &a : groupList) { + LOGI("group info:[groupName] %{public}s, [groupId] %{public}s, [groupOwner] %{public}s,[groupId] %{public}d,", + a.groupName.c_str(), a.groupId.c_str(), a.groupOwner.c_str(), a.groupType); + } + + return true; +} void DeviceManagerAgent::QueryRelatedGroups(const std::string &udid, std::vector &groupList) { @@ -207,15 +244,16 @@ void DeviceManagerAgent::QueryRelatedGroups(const std::string &udid, std::vector return; } - hichainDeviceGroupManager_ = GetGmInstance(); - if (hichainDeviceGroupManager_ == nullptr) { + auto hichainDevGroupMgr_ = GetGmInstance(); + if (hichainDevGroupMgr_ == nullptr) { LOGE("failed to get hichain device group manager"); return; } char *returnGroupVec = nullptr; uint32_t groupNum = 0; - ret = hichainDeviceGroupManager_->getRelatedGroups(IDaemon::SERVICE_NAME.c_str(), udid.c_str(), &returnGroupVec, &groupNum); + ret = hichainDevGroupMgr_->getRelatedGroups(IDaemon::SERVICE_NAME.c_str(), udid.c_str(), &returnGroupVec, + &groupNum); if (ret != 0 || returnGroupVec == nullptr) { LOGE("failed to get related groups, ret %{public}d", ret); return; @@ -235,7 +273,8 @@ void DeviceManagerAgent::QueryRelatedGroups(const std::string &udid, std::vector groupList = jsonObject.get>(); for (auto &a : groupList) { - LOGI("group info:[groupName] %{public}s, [groupId] %{public}s, [groupOwner] %{public}s,[groupId] %{public}d,", a.groupName.c_str(), a.groupId.c_str(), a.groupOwner.c_str(), a.groupType); + LOGI("group info:[groupName] %{public}s, [groupId] %{public}s, [groupOwner] %{public}s,[groupId] %{public}d,", + a.groupName.c_str(), a.groupId.c_str(), a.groupOwner.c_str(), a.groupType); } return; } @@ -257,11 +296,15 @@ void DeviceManagerAgent::AuthGroupOnlineProc(const DeviceInfo info) continue; } if (authGroupMap_.find(group.groupId) == authGroupMap_.end()) { - LOGI("groupId %{public}s not exist, then mount", group.groupId.c_str()); + LOGI("groupId %{public}s not exist, map size %{public}d, then mount", group.groupId.c_str(), + authGroupMap_.size()); + std::stringstream ss; + ss << "groupId_" << authGroupMap_.size(); MountManager::GetInstance()->Mount(make_unique( - Utils::MountArgumentDescriptors::SetAuthGroupMountArgument(group.groupId, group.groupOwner, true))); + Utils::MountArgumentDescriptors::SetAuthGroupMountArgument(ss.str(), group.groupOwner, true))); + std::get<0>(authGroupMap_[group.groupId]) = ss.str(); } - auto [iter, status] = authGroupMap_[group.groupId].insert(info.GetCid()); + auto [iter, status] = std::get<1>(authGroupMap_[group.groupId]).insert(info.GetCid()); if (status == false) { LOGI("cid %{public}s has already inserted into groupId %{public}s", info.GetCid().c_str(), group.groupId.c_str()); @@ -273,7 +316,7 @@ void DeviceManagerAgent::AuthGroupOnlineProc(const DeviceInfo info) void DeviceManagerAgent::AuthGroupOfflineProc(const DeviceInfo &info) { for (auto iter = authGroupMap_.begin(); iter != authGroupMap_.end();) { - auto set = iter->second; + auto [groupIdMark, set] = iter->second; auto groupId = iter->first; if (set.find(info.GetCid()) == set.end()) { continue; @@ -283,11 +326,11 @@ void DeviceManagerAgent::AuthGroupOfflineProc(const DeviceInfo &info) LOGI("can not find groupId %{public}s ", groupId.c_str()); continue; } - authGroupMap_[groupId].erase(info.GetCid()); - if (authGroupMap_[groupId].empty()) { + std::get<1>(authGroupMap_[groupId]).erase(info.GetCid()); + if (std::get<1>(authGroupMap_[groupId]).empty()) { std::vector groupList; - if (groupList.size() == 0) { - MountManager::GetInstance()->Umount(groupId); + if (QueryGroupInfoById(groupId, groupList) != 0 || groupList.size() == 0) { + MountManager::GetInstance()->Umount(groupIdMark); iter = authGroupMap_.erase(iter); continue; } @@ -299,8 +342,8 @@ void DeviceManagerAgent::AuthGroupOfflineProc(const DeviceInfo &info) void DeviceManagerAgent::AllAuthGroupsOfflineProc() { for (auto iter = authGroupMap_.begin(); iter != authGroupMap_.end();) { - auto groupId = iter->first; - MountManager::GetInstance()->Umount(groupId); + auto [groupIdMark, ignore] = iter->second; + MountManager::GetInstance()->Umount(groupIdMark); authGroupMap_.erase(iter++); } } -- Gitee From e33c7f41f6a1f90beec87c96101991a635648929 Mon Sep 17 00:00:00 2001 From: xianghengliang Date: Mon, 24 Jan 2022 16:01:13 +0800 Subject: [PATCH 5/9] at device_info class get udid Signed-off-by: xianghengliang --- .../include/device/device_info.h | 4 ++++ .../src/device/device_info.cpp | 6 +++++- .../src/device/device_manager_agent.cpp | 18 +++++------------- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/services/distributedfiledaemon/include/device/device_info.h b/services/distributedfiledaemon/include/device/device_info.h index 57ea917dd..0864dc2cc 100644 --- a/services/distributedfiledaemon/include/device/device_info.h +++ b/services/distributedfiledaemon/include/device/device_info.h @@ -18,7 +18,9 @@ #include #include +#include "device_manager.h" #include "dm_device_info.h" +#include "ipc/i_daemon.h" namespace OHOS { namespace Storage { @@ -36,8 +38,10 @@ public: const std::string &GetCid() const; private: + friend class DeviceManagerAgent; std::atomic initCidFlag_{false}; std::string cid_; + std::string udid_; }; } // namespace DistributedFile } // namespace Storage diff --git a/services/distributedfiledaemon/src/device/device_info.cpp b/services/distributedfiledaemon/src/device/device_info.cpp index 3a0c268ea..ff14ae31c 100644 --- a/services/distributedfiledaemon/src/device/device_info.cpp +++ b/services/distributedfiledaemon/src/device/device_info.cpp @@ -26,6 +26,10 @@ DeviceInfo::DeviceInfo(const DistributedHardware::DmDeviceInfo &nodeInfo) { cid_ = string(nodeInfo.deviceId); initCidFlag_ = true; + // convert networkId to udid + auto &deviceManager = DistributedHardware::DeviceManager::GetInstance(); + int32_t ret = deviceManager.GetUdidByNetworkId(IDaemon::SERVICE_NAME, cid_, udid_); + LOGI("ret %{public}d, get udid %{public}s", ret, udid_.c_str()); } DeviceInfo &DeviceInfo::operator=(const DistributedHardware::DmDeviceInfo &nodeInfo) @@ -35,7 +39,7 @@ DeviceInfo &DeviceInfo::operator=(const DistributedHardware::DmDeviceInfo &nodeI return *this; } -DeviceInfo::DeviceInfo(const DeviceInfo &nodeInfo) : cid_(nodeInfo.cid_) +DeviceInfo::DeviceInfo(const DeviceInfo &nodeInfo) : cid_(nodeInfo.cid_), udid_(nodeInfo.udid_) { initCidFlag_.store(nodeInfo.initCidFlag_.load()); } diff --git a/services/distributedfiledaemon/src/device/device_manager_agent.cpp b/services/distributedfiledaemon/src/device/device_manager_agent.cpp index 829bfad1c..621a6f0c2 100644 --- a/services/distributedfiledaemon/src/device/device_manager_agent.cpp +++ b/services/distributedfiledaemon/src/device/device_manager_agent.cpp @@ -228,7 +228,7 @@ bool DeviceManagerAgent::QueryGroupInfoById(const std::string &groupId, std::vec groupList = jsonObject.get>(); for (auto &a : groupList) { - LOGI("group info:[groupName] %{public}s, [groupId] %{public}s, [groupOwner] %{public}s,[groupId] %{public}d,", + LOGI("group info:[groupName] %{public}s, [groupId] %{public}s, [groupOwner] %{public}s,[groupType] %{public}d,", a.groupName.c_str(), a.groupId.c_str(), a.groupOwner.c_str(), a.groupType); } @@ -273,7 +273,7 @@ void DeviceManagerAgent::QueryRelatedGroups(const std::string &udid, std::vector groupList = jsonObject.get>(); for (auto &a : groupList) { - LOGI("group info:[groupName] %{public}s, [groupId] %{public}s, [groupOwner] %{public}s,[groupId] %{public}d,", + LOGI("group info:[groupName] %{public}s, [groupId] %{public}s, [groupOwner] %{public}s,[groupType] %{public}d,", a.groupName.c_str(), a.groupId.c_str(), a.groupOwner.c_str(), a.groupType); } return; @@ -281,16 +281,8 @@ void DeviceManagerAgent::QueryRelatedGroups(const std::string &udid, std::vector void DeviceManagerAgent::AuthGroupOnlineProc(const DeviceInfo info) { - // convert networkId to uuid - // todo: udid获取放到DeviceInfo类中实现 - std::string udid; - auto &deviceManager = DistributedHardware::DeviceManager::GetInstance(); - int32_t ret = deviceManager.GetUdidByNetworkId(IDaemon::SERVICE_NAME, info.GetCid(), udid); - std::string uuid; - ret += deviceManager.GetUuidByNetworkId(IDaemon::SERVICE_NAME, info.GetCid(), uuid); - LOGI("ret %{public}d, get udid %{public}s, uuid %{public}s", ret, udid.c_str(), uuid.c_str()); std::vector groupList; - QueryRelatedGroups(udid, groupList); + QueryRelatedGroups(info.udid_, groupList); for (const auto &group : groupList) { if (!CheckIsAuthGroup(group)) { continue; @@ -304,9 +296,9 @@ void DeviceManagerAgent::AuthGroupOnlineProc(const DeviceInfo info) Utils::MountArgumentDescriptors::SetAuthGroupMountArgument(ss.str(), group.groupOwner, true))); std::get<0>(authGroupMap_[group.groupId]) = ss.str(); } - auto [iter, status] = std::get<1>(authGroupMap_[group.groupId]).insert(info.GetCid()); + auto [iter, status] = std::get<1>(authGroupMap_[group.groupId]).insert(info.cid_); if (status == false) { - LOGI("cid %{public}s has already inserted into groupId %{public}s", info.GetCid().c_str(), + LOGI("cid %{public}s has already inserted into groupId %{public}s", info.cid_.c_str(), group.groupId.c_str()); continue; } -- Gitee From a8c98dbe151d961dec284a179006dd885d865c61 Mon Sep 17 00:00:00 2001 From: xianghengliang Date: Mon, 24 Jan 2022 17:19:12 +0800 Subject: [PATCH 6/9] remove device_info constructor log Signed-off-by: xianghengliang --- services/distributedfiledaemon/src/device/device_info.cpp | 3 +-- .../src/device/device_manager_agent.cpp | 8 +------- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/services/distributedfiledaemon/src/device/device_info.cpp b/services/distributedfiledaemon/src/device/device_info.cpp index ff14ae31c..bf15d03be 100644 --- a/services/distributedfiledaemon/src/device/device_info.cpp +++ b/services/distributedfiledaemon/src/device/device_info.cpp @@ -28,8 +28,7 @@ DeviceInfo::DeviceInfo(const DistributedHardware::DmDeviceInfo &nodeInfo) initCidFlag_ = true; // convert networkId to udid auto &deviceManager = DistributedHardware::DeviceManager::GetInstance(); - int32_t ret = deviceManager.GetUdidByNetworkId(IDaemon::SERVICE_NAME, cid_, udid_); - LOGI("ret %{public}d, get udid %{public}s", ret, udid_.c_str()); + deviceManager.GetUdidByNetworkId(IDaemon::SERVICE_NAME, cid_, udid_); } DeviceInfo &DeviceInfo::operator=(const DistributedHardware::DmDeviceInfo &nodeInfo) diff --git a/services/distributedfiledaemon/src/device/device_manager_agent.cpp b/services/distributedfiledaemon/src/device/device_manager_agent.cpp index 621a6f0c2..98a6e6604 100644 --- a/services/distributedfiledaemon/src/device/device_manager_agent.cpp +++ b/services/distributedfiledaemon/src/device/device_manager_agent.cpp @@ -226,12 +226,6 @@ bool DeviceManagerAgent::QueryGroupInfoById(const std::string &groupId, std::vec return false; } - groupList = jsonObject.get>(); - for (auto &a : groupList) { - LOGI("group info:[groupName] %{public}s, [groupId] %{public}s, [groupOwner] %{public}s,[groupType] %{public}d,", - a.groupName.c_str(), a.groupId.c_str(), a.groupOwner.c_str(), a.groupType); - } - return true; } @@ -321,7 +315,7 @@ void DeviceManagerAgent::AuthGroupOfflineProc(const DeviceInfo &info) std::get<1>(authGroupMap_[groupId]).erase(info.GetCid()); if (std::get<1>(authGroupMap_[groupId]).empty()) { std::vector groupList; - if (QueryGroupInfoById(groupId, groupList) != 0 || groupList.size() == 0) { + if (QueryGroupInfoById(groupId, groupList) == false || groupList.size() == 0) { MountManager::GetInstance()->Umount(groupIdMark); iter = authGroupMap_.erase(iter); continue; -- Gitee From de8e380959e27abd2e00dbbb731603bc3aab1b41 Mon Sep 17 00:00:00 2001 From: xianghengliang Date: Tue, 25 Jan 2022 15:09:16 +0800 Subject: [PATCH 7/9] remove query on offline and add hmdfs/data dir Signed-off-by: xianghengliang --- .../include/device/device_manager_agent.h | 1 - .../src/device/device_manager_agent.cpp | 33 +------------------ .../src/mountpoint/mount_point.cpp | 3 ++ utils/system/include/utils_mount_argument.h | 1 + utils/system/src/utils_mount_argument.cpp | 13 +++++++- 5 files changed, 17 insertions(+), 34 deletions(-) diff --git a/services/distributedfiledaemon/include/device/device_manager_agent.h b/services/distributedfiledaemon/include/device/device_manager_agent.h index 5e9839ce4..ef5fdc57a 100644 --- a/services/distributedfiledaemon/include/device/device_manager_agent.h +++ b/services/distributedfiledaemon/include/device/device_manager_agent.h @@ -85,7 +85,6 @@ private: void AuthGroupOfflineProc(const DeviceInfo &info); void QueryRelatedGroups(const std::string &udid, std::vector &groupList); - bool QueryGroupInfoById(const std::string &groupId, std::vector &groupList); bool CheckIsAuthGroup(const GroupInfo &group); void AllAuthGroupsOfflineProc(); // We use a mutex instead of a shared_mutex to serialize online/offline procedures diff --git a/services/distributedfiledaemon/src/device/device_manager_agent.cpp b/services/distributedfiledaemon/src/device/device_manager_agent.cpp index 98a6e6604..568d1d33d 100644 --- a/services/distributedfiledaemon/src/device/device_manager_agent.cpp +++ b/services/distributedfiledaemon/src/device/device_manager_agent.cpp @@ -197,37 +197,6 @@ void from_json(const nlohmann::json &jsonObject, GroupInfo &groupInfo) groupInfo.groupType = jsonObject.at(FIELD_GROUP_TYPE).get(); } } -bool DeviceManagerAgent::QueryGroupInfoById(const std::string &groupId, std::vector &groupList) -{ - LOGI("use groupId %{public}s query hichain related groups", groupId.c_str()); - int ret = InitDeviceAuthService(); - if (ret != 0) { - LOGE("InitDeviceAuthService failed, ret %{public}d", ret); - return false; - } - - auto hichainDevGroupMgr_ = GetGmInstance(); - if (hichainDevGroupMgr_ == nullptr) { - LOGE("failed to get hichain device group manager"); - return false; - } - - char *returnGroupVec = nullptr; - ret = hichainDevGroupMgr_->getGroupInfoById(IDaemon::SERVICE_NAME.c_str(), groupId.c_str(), &returnGroupVec); - if (ret != 0 || returnGroupVec == nullptr) { - LOGE("failed to get related groups, ret %{public}d", ret); - return false; - } - - std::string groups = std::string(returnGroupVec); - nlohmann::json jsonObject = nlohmann::json::parse(groups); // transform from cjson to cppjson - if (jsonObject.is_discarded()) { - LOGE("returnGroupVec parse failed"); - return false; - } - - return true; -} void DeviceManagerAgent::QueryRelatedGroups(const std::string &udid, std::vector &groupList) { @@ -315,7 +284,7 @@ void DeviceManagerAgent::AuthGroupOfflineProc(const DeviceInfo &info) std::get<1>(authGroupMap_[groupId]).erase(info.GetCid()); if (std::get<1>(authGroupMap_[groupId]).empty()) { std::vector groupList; - if (QueryGroupInfoById(groupId, groupList) == false || groupList.size() == 0) { + if (groupList.size() == 0) { MountManager::GetInstance()->Umount(groupIdMark); iter = authGroupMap_.erase(iter); continue; diff --git a/services/distributedfiledaemon/src/mountpoint/mount_point.cpp b/services/distributedfiledaemon/src/mountpoint/mount_point.cpp index 91755a469..1586751ea 100644 --- a/services/distributedfiledaemon/src/mountpoint/mount_point.cpp +++ b/services/distributedfiledaemon/src/mountpoint/mount_point.cpp @@ -49,6 +49,9 @@ void MountPoint::Mount() const string cache = mntArg.GetCachePath(); Utils::ForceCreateDirectory(cache, S_IRWXU | S_IRWXG | S_IXOTH); + string data = mntArg.GetDataPath(); + Utils::ForceCreateDirectory(data, S_IRWXU | S_IRWXG | S_IXOTH); + unsigned long flags = mntArg.GetFlags(); string options = mntArg.OptionsToString(); int ret = mount(src.c_str(), dst.c_str(), "hmdfs", flags, options.c_str()); diff --git a/utils/system/include/utils_mount_argument.h b/utils/system/include/utils_mount_argument.h index 8a1229229..2047fdd89 100644 --- a/utils/system/include/utils_mount_argument.h +++ b/utils/system/include/utils_mount_argument.h @@ -40,6 +40,7 @@ struct MountArgument final { std::string GetCtrlPath() const; std::string GetCachePath() const; std::string OptionsToString() const; + std::string GetDataPath() const; unsigned long GetFlags() const; }; diff --git a/utils/system/src/utils_mount_argument.cpp b/utils/system/src/utils_mount_argument.cpp index 4580239a2..c45a60c1c 100644 --- a/utils/system/src/utils_mount_argument.cpp +++ b/utils/system/src/utils_mount_argument.cpp @@ -34,7 +34,7 @@ string MountArgument::GetFullSrc() const { stringstream ss; if (!accountless_) { - ss << DATA_POINT << userId_ << "/hmdfs/storage"; + ss << DATA_POINT << userId_ << "/hmdfs"; } else { ss << DATA_POINT << userId_ << "/hmdfs/auth_groups/" << groupId_; } @@ -63,6 +63,17 @@ string MountArgument::GetCachePath() const return ss.str(); } +string MountArgument::GetDataPath() const +{ + stringstream ss; + if (!accountless_) { + ss << DATA_POINT << userId_ << "/hmdfs/data/"; + } else { + ss << DATA_POINT << userId_ << "/hmdfs/auth_groups/" << groupId_ << "/data/"; + } + return ss.str(); +} + static uint64_t MocklispHash(const string &str) { uint64_t res = 0; -- Gitee From 65c3364aad1f493f652f90fde45042d82e95fa27 Mon Sep 17 00:00:00 2001 From: xianghengliang Date: Tue, 25 Jan 2022 16:04:55 +0800 Subject: [PATCH 8/9] test build add ipc Signed-off-by: xianghengliang --- services/distributedfiledaemon/test/unittest/BUILD.gn | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/services/distributedfiledaemon/test/unittest/BUILD.gn b/services/distributedfiledaemon/test/unittest/BUILD.gn index cf078929a..5b8cf0227 100644 --- a/services/distributedfiledaemon/test/unittest/BUILD.gn +++ b/services/distributedfiledaemon/test/unittest/BUILD.gn @@ -45,7 +45,13 @@ ohos_unittest("DeviceManagerAgentTest") { "//third_party/googletest:gtest_main", ] - external_deps = [ "samgr_standard:samgr_proxy" ] + external_deps = [ + "dsoftbus_standard:softbus_client", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_standard:samgr_proxy", + ] + defines = [ "private=public" ] } -- Gitee From 9c4d603b7d898c20e6a58863a058aa9e2914692e Mon Sep 17 00:00:00 2001 From: xianghengliang Date: Tue, 25 Jan 2022 16:31:26 +0800 Subject: [PATCH 9/9] code check fix Signed-off-by: xianghengliang --- .../distributedfiledaemon/src/device/device_manager_agent.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/distributedfiledaemon/src/device/device_manager_agent.cpp b/services/distributedfiledaemon/src/device/device_manager_agent.cpp index 568d1d33d..25e12ff25 100644 --- a/services/distributedfiledaemon/src/device/device_manager_agent.cpp +++ b/services/distributedfiledaemon/src/device/device_manager_agent.cpp @@ -215,8 +215,8 @@ void DeviceManagerAgent::QueryRelatedGroups(const std::string &udid, std::vector char *returnGroupVec = nullptr; uint32_t groupNum = 0; - ret = hichainDevGroupMgr_->getRelatedGroups(IDaemon::SERVICE_NAME.c_str(), udid.c_str(), &returnGroupVec, - &groupNum); + ret = + hichainDevGroupMgr_->getRelatedGroups(IDaemon::SERVICE_NAME.c_str(), udid.c_str(), &returnGroupVec, &groupNum); if (ret != 0 || returnGroupVec == nullptr) { LOGE("failed to get related groups, ret %{public}d", ret); return; -- Gitee