diff --git a/services/distributedfiledaemon/include/device/device_info.h b/services/distributedfiledaemon/include/device/device_info.h index 57ea917ddd298651a2e27cab886bc17bb3d2a25e..0864dc2ccff40e48a71bd47796f1a37a1437c905 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/include/device/device_manager_agent.h b/services/distributedfiledaemon/include/device/device_manager_agent.h index ad9ae0c36f77d3ea0fb7536b2bf49440ebdc54a9..ef5fdc57a85daa12e758ec7a15c4461fcba7b61c 100644 --- a/services/distributedfiledaemon/include/device/device_manager_agent.h +++ b/services/distributedfiledaemon/include/device/device_manager_agent.h @@ -84,15 +84,16 @@ 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 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_info.cpp b/services/distributedfiledaemon/src/device/device_info.cpp index 3a0c268ea8beb762c545b9705642570f11513eeb..bf15d03be8631631176225272d33f5955494b831 100644 --- a/services/distributedfiledaemon/src/device/device_info.cpp +++ b/services/distributedfiledaemon/src/device/device_info.cpp @@ -26,6 +26,9 @@ DeviceInfo::DeviceInfo(const DistributedHardware::DmDeviceInfo &nodeInfo) { cid_ = string(nodeInfo.deviceId); initCidFlag_ = true; + // convert networkId to udid + auto &deviceManager = DistributedHardware::DeviceManager::GetInstance(); + deviceManager.GetUdidByNetworkId(IDaemon::SERVICE_NAME, cid_, udid_); } DeviceInfo &DeviceInfo::operator=(const DistributedHardware::DmDeviceInfo &nodeInfo) @@ -35,7 +38,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 af568a2fdc44fae49e2585c323d89c756a808256..25e12ff25da402b6122869a174fc1a994beb6ab6 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,31 +198,70 @@ 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; + } + + auto hichainDevGroupMgr_ = GetGmInstance(); + if (hichainDevGroupMgr_ == nullptr) { + LOGE("failed to get hichain device group manager"); + return; + } + + char *returnGroupVec = nullptr; + uint32_t groupNum = 0; + 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; + } + + 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,[groupType] %{public}d,", + a.groupName.c_str(), a.groupId.c_str(), a.groupOwner.c_str(), a.groupType); + } + return; } void DeviceManagerAgent::AuthGroupOnlineProc(const DeviceInfo info) { std::vector groupList; - QueryRelatedGroups(info.GetCid(), groupList); + QueryRelatedGroups(info.udid_, groupList); for (const auto &group : groupList) { if (!CheckIsAuthGroup(group)) { 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.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; } @@ -233,7 +271,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; @@ -243,11 +281,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); + MountManager::GetInstance()->Umount(groupIdMark); iter = authGroupMap_.erase(iter); continue; } @@ -259,8 +297,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++); } } diff --git a/services/distributedfiledaemon/src/mountpoint/mount_point.cpp b/services/distributedfiledaemon/src/mountpoint/mount_point.cpp index 91755a469231294ce0d137ab8c91659783d63f29..1586751eabcf219eff8ca75a034d387ce6d20977 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/services/distributedfiledaemon/test/unittest/BUILD.gn b/services/distributedfiledaemon/test/unittest/BUILD.gn index cf078929ad7d4802383913e57f2e6e76bb712496..5b8cf02272d9309d59f008c8257a5051e8141480 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" ] } diff --git a/utils/system/include/utils_mount_argument.h b/utils/system/include/utils_mount_argument.h index 8a12292290c8228ffe2918738187fb9eae4bb305..2047fdd8970a24622e46fd3cc0127d4dba09ef88 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 4580239a23d3869252d3c55ce7c3c7b4f43dc8af..c45a60c1c587c4da2e02ea878554c51e79287277 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;