diff --git a/BUILD.gn b/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..180b46ec8075e1142513c4a775109aa7584b7c3a --- /dev/null +++ b/BUILD.gn @@ -0,0 +1,37 @@ +# 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. + +import("//build/ohos.gni") +import("//foundation/filemanagement/dfs_service/distributedfile.gni") + +group("libdistributedfile_innerkits") { + deps = [ "${innerkits_native_path}:libdistributedfile_innerkits" ] +} + +group("services_target") { + deps = [ + "${services_path}:distributed_file.para", + "${services_path}:distributedfile_etc", + "${services_path}:distributedfile_sa_profile", + "${services_path}/distributedfiledaemon:libdistributedfiledaemon", + "${services_path}/distributedfileservice:libdistributedfileservice", + ] +} + +group("dfs_test_moudule") { + testonly = true + deps = [ + "${services_path}/distributedfiledaemon/test/unittest:DeviceManagerAgentTest", + "test/moduletest:DistributedFileDaemonServiceTest", + ] +} diff --git a/bundle.json b/bundle.json new file mode 100644 index 0000000000000000000000000000000000000000..fb9e31e5b8067e543f88b38f87264d5f1985739d --- /dev/null +++ b/bundle.json @@ -0,0 +1,57 @@ +{ + "name": "@ohos/dfs_service", + "version": "3.1", + "description": "The dfs_service module belongs to the filemanagement subsystem of OpenHarmony. It provides the ability of accessing distributed files.", + "license": "Apache License 2.0", + "publishAs": "code-segment", + "segment": { + "destPath": "foundation/filemanagement/dfs_service" + }, + "dirs": {}, + "scripts": {}, + "author": {}, + "repository": "", + "component": { + "name": "dfs_service", + "subsystem": "filemanagement", + "syscap": [], + "features": [], + "adapted_system_type": [], + "rom": "", + "ram": "", + "deps": { + "components": [ + "ability_runtime", + "bundle_framework", + "dsoftbus", + "ipc", + "hilog", + "safwk", + "samgr", + "utils" + ], + "third_party": [ + "json_modern_c++" + ] + }, + "build": { + "sub_component": [ + "//foundation/filemanagement/dfs_service:services_target" + ], + "inner_kits": [ + { + "name": "//foundation/filemanagement/dfs_service:libdistributedfile_innerkits", + "header": { + "header_files": [ + "i_distributedfile_service.h" + ], + "header_base": "//foundation/filemanagement/dfs_service/interfaces/innerkits/native" + } + } + ], + "test": [ + "//foundation/filemanagement/dfs_service:dfs_test_moudule" + ] + } + } +} diff --git a/ohos.build b/ohos.build deleted file mode 100755 index b56a401897715fbc40149645442bb2a508ef6288..0000000000000000000000000000000000000000 --- a/ohos.build +++ /dev/null @@ -1,29 +0,0 @@ -{ - "subsystem": "filemanagement", - "parts": { - "dfs_service": { - "variants": [ - "wearable", - "phone" - ], - "module_list": [ - "//foundation/filemanagement/dfs_service/services/:services_target" - ], - "inner_kits":[ - { - "name": "//foundation/filemanagement/dfs_service/interfaces/innerkits/native:libdistributedfile_innerkits", - "header": { - "header_files": [ - "i_distributedfile_service.h" - ], - "header_base": "//foundation/filemanagement/dfs_service/interfaces/innerkits/native" - } - } - ], - "test_list": [ - "//foundation/filemanagement/dfs_service/services/distributedfiledaemon/test/unittest:unittest", - "//foundation/filemanagement/dfs_service/test/moduletest:moduletest" - ] - } - } -} diff --git a/services/BUILD.gn b/services/BUILD.gn index d9afcb9c6a9f95152312c9bcc9b0cbf612283641..f9b13805e077797c3d3d0e16b55a5ff3dc5bae83 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -34,13 +34,3 @@ ohos_prebuilt_etc("distributed_file.para") { part_name = "dfs_service" module_install_dir = "etc/param" } - -group("services_target") { - deps = [ - ":distributed_file.para", - ":distributedfile_etc", - ":distributedfile_sa_profile", - "distributedfiledaemon:distributedfiledaemon_target", - "distributedfileservice:distributedfileservice_target", - ] -} diff --git a/services/distributedfiledaemon/BUILD.gn b/services/distributedfiledaemon/BUILD.gn index 35e054121f1e792977b6717f700374c04163f150..af0d2e29484cc47e92c572714d7ec71e6234bc28 100755 --- a/services/distributedfiledaemon/BUILD.gn +++ b/services/distributedfiledaemon/BUILD.gn @@ -52,7 +52,3 @@ ohos_shared_library("libdistributedfiledaemon") { part_name = "dfs_service" subsystem_name = "filemanagement" } - -group("distributedfiledaemon_target") { - deps = [ ":libdistributedfiledaemon" ] -} 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/services/distributedfileservice/BUILD.gn b/services/distributedfileservice/BUILD.gn index b3ed94eb49d9a781466ed85a50cf997cac7e5d2a..18acb1a250db760756d1dba6796d6e99da3deab5 100644 --- a/services/distributedfileservice/BUILD.gn +++ b/services/distributedfileservice/BUILD.gn @@ -24,7 +24,6 @@ ohos_shared_library("libdistributedfileservice") { configs = [ "${utils_path}:compiler_configs" ] external_deps = [ - "ability_runtime:app_manager", "ability_runtime:want", "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", @@ -41,7 +40,3 @@ ohos_shared_library("libdistributedfileservice") { part_name = "dfs_service" subsystem_name = "filemanagement" } - -group("distributedfileservice_target") { - deps = [ ":libdistributedfileservice" ] -} diff --git a/test/moduletest/BUILD.gn b/test/moduletest/BUILD.gn index 1e39a9a8034215ac8b4587bac959583406d3b1e8..8bc51b4528fad43b1785cbea2206f8644fc4725c 100644 --- a/test/moduletest/BUILD.gn +++ b/test/moduletest/BUILD.gn @@ -72,8 +72,3 @@ ohos_moduletest("DistributedFileDaemonServiceTest") { "samgr_standard:samgr_proxy", ] } - -group("moduletest") { - testonly = true - deps = [ ":DistributedFileDaemonServiceTest" ] -} 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;