1cb7eb8c9Sopenharmony_ci/*
2cb7eb8c9Sopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3cb7eb8c9Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4cb7eb8c9Sopenharmony_ci * you may not use this file except in compliance with the License.
5cb7eb8c9Sopenharmony_ci * You may obtain a copy of the License at
6cb7eb8c9Sopenharmony_ci *
7cb7eb8c9Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8cb7eb8c9Sopenharmony_ci *
9cb7eb8c9Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10cb7eb8c9Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11cb7eb8c9Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12cb7eb8c9Sopenharmony_ci * See the License for the specific language governing permissions and
13cb7eb8c9Sopenharmony_ci * limitations under the License.
14cb7eb8c9Sopenharmony_ci */
15cb7eb8c9Sopenharmony_ci
16cb7eb8c9Sopenharmony_ci#include "ipc/daemon.h"
17cb7eb8c9Sopenharmony_ci
18cb7eb8c9Sopenharmony_ci#include <exception>
19cb7eb8c9Sopenharmony_ci#include <regex>
20cb7eb8c9Sopenharmony_ci#include <stdexcept>
21cb7eb8c9Sopenharmony_ci#include <string>
22cb7eb8c9Sopenharmony_ci#include <sys/stat.h>
23cb7eb8c9Sopenharmony_ci#include <unistd.h>
24cb7eb8c9Sopenharmony_ci#include <unordered_set>
25cb7eb8c9Sopenharmony_ci
26cb7eb8c9Sopenharmony_ci#include "accesstoken_kit.h"
27cb7eb8c9Sopenharmony_ci#include "all_connect/all_connect_manager.h"
28cb7eb8c9Sopenharmony_ci#include "asset_callback_manager.h"
29cb7eb8c9Sopenharmony_ci#include "common_event_manager.h"
30cb7eb8c9Sopenharmony_ci#include "common_event_support.h"
31cb7eb8c9Sopenharmony_ci#include "connection_detector.h"
32cb7eb8c9Sopenharmony_ci#include "device/device_manager_agent.h"
33cb7eb8c9Sopenharmony_ci#include "dfs_daemon_event_dfx.h"
34cb7eb8c9Sopenharmony_ci#include "dfs_error.h"
35cb7eb8c9Sopenharmony_ci#include "dfsu_access_token_helper.h"
36cb7eb8c9Sopenharmony_ci#include "i_file_dfs_listener.h"
37cb7eb8c9Sopenharmony_ci#include "ipc_skeleton.h"
38cb7eb8c9Sopenharmony_ci#include "iremote_object.h"
39cb7eb8c9Sopenharmony_ci#include "iservice_registry.h"
40cb7eb8c9Sopenharmony_ci#include "mountpoint/mount_manager.h"
41cb7eb8c9Sopenharmony_ci#include "network/softbus/softbus_handler_asset.h"
42cb7eb8c9Sopenharmony_ci#include "network/softbus/softbus_handler.h"
43cb7eb8c9Sopenharmony_ci#include "network/softbus/softbus_session_dispatcher.h"
44cb7eb8c9Sopenharmony_ci#include "network/softbus/softbus_session_listener.h"
45cb7eb8c9Sopenharmony_ci#include "network/softbus/softbus_session_pool.h"
46cb7eb8c9Sopenharmony_ci#include "sandbox_helper.h"
47cb7eb8c9Sopenharmony_ci#include "system_ability_definition.h"
48cb7eb8c9Sopenharmony_ci#include "trans_mananger.h"
49cb7eb8c9Sopenharmony_ci#include "utils_directory.h"
50cb7eb8c9Sopenharmony_ci#include "utils_log.h"
51cb7eb8c9Sopenharmony_ci
52cb7eb8c9Sopenharmony_cinamespace OHOS {
53cb7eb8c9Sopenharmony_cinamespace Storage {
54cb7eb8c9Sopenharmony_cinamespace DistributedFile {
55cb7eb8c9Sopenharmony_ciusing namespace std;
56cb7eb8c9Sopenharmony_ciusing namespace OHOS::AppFileService;
57cb7eb8c9Sopenharmony_ciusing namespace OHOS::FileManagement;
58cb7eb8c9Sopenharmony_ciusing namespace OHOS::Storage::DistributedFile;
59cb7eb8c9Sopenharmony_ciusing HapTokenInfo = OHOS::Security::AccessToken::HapTokenInfo;
60cb7eb8c9Sopenharmony_ciusing AccessTokenKit = OHOS::Security::AccessToken::AccessTokenKit;
61cb7eb8c9Sopenharmony_ci
62cb7eb8c9Sopenharmony_cinamespace {
63cb7eb8c9Sopenharmony_ciconst string FILE_MANAGER_AUTHORITY = "docs";
64cb7eb8c9Sopenharmony_ciconst string MEDIA_AUTHORITY = "media";
65cb7eb8c9Sopenharmony_ciconst int32_t E_PERMISSION_DENIED_NAPI = 201;
66cb7eb8c9Sopenharmony_ciconst int32_t E_INVAL_ARG_NAPI = 401;
67cb7eb8c9Sopenharmony_ciconst int32_t E_CONNECTION_FAILED = 13900045;
68cb7eb8c9Sopenharmony_ciconst int32_t E_UNMOUNT = 13600004;
69cb7eb8c9Sopenharmony_ciconstexpr int32_t CHECK_SESSION_DELAY_TIME_TWICE = 5000000;
70cb7eb8c9Sopenharmony_ciconstexpr mode_t DEFAULT_UMASK = 0002;
71cb7eb8c9Sopenharmony_ciconstexpr int32_t BLOCK_INTERVAL_SEND_FILE = 8 * 1000;
72cb7eb8c9Sopenharmony_ci}
73cb7eb8c9Sopenharmony_ci
74cb7eb8c9Sopenharmony_ciREGISTER_SYSTEM_ABILITY_BY_ID(Daemon, FILEMANAGEMENT_DISTRIBUTED_FILE_DAEMON_SA_ID, true);
75cb7eb8c9Sopenharmony_ci
76cb7eb8c9Sopenharmony_civoid Daemon::PublishSA()
77cb7eb8c9Sopenharmony_ci{
78cb7eb8c9Sopenharmony_ci    LOGI("Begin to init");
79cb7eb8c9Sopenharmony_ci    if (!registerToService_) {
80cb7eb8c9Sopenharmony_ci        bool ret = SystemAbility::Publish(this);
81cb7eb8c9Sopenharmony_ci        if (!ret) {
82cb7eb8c9Sopenharmony_ci            throw runtime_error("Failed to publish the daemon");
83cb7eb8c9Sopenharmony_ci        }
84cb7eb8c9Sopenharmony_ci        registerToService_ = true;
85cb7eb8c9Sopenharmony_ci    }
86cb7eb8c9Sopenharmony_ci    LOGI("Init finished successfully");
87cb7eb8c9Sopenharmony_ci}
88cb7eb8c9Sopenharmony_ci
89cb7eb8c9Sopenharmony_civoid Daemon::RegisterOsAccount()
90cb7eb8c9Sopenharmony_ci{
91cb7eb8c9Sopenharmony_ci    EventFwk::MatchingSkills matchingSkills;
92cb7eb8c9Sopenharmony_ci    matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
93cb7eb8c9Sopenharmony_ci    EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
94cb7eb8c9Sopenharmony_ci    subScriber_ = std::make_shared<OsAccountObserver>(subscribeInfo);
95cb7eb8c9Sopenharmony_ci    bool subRet = EventFwk::CommonEventManager::SubscribeCommonEvent(subScriber_);
96cb7eb8c9Sopenharmony_ci    if (!subRet) {
97cb7eb8c9Sopenharmony_ci        LOGE("Subscribe common event failed");
98cb7eb8c9Sopenharmony_ci    }
99cb7eb8c9Sopenharmony_ci}
100cb7eb8c9Sopenharmony_ci
101cb7eb8c9Sopenharmony_civoid Daemon::OnStart()
102cb7eb8c9Sopenharmony_ci{
103cb7eb8c9Sopenharmony_ci    LOGI("Begin to start service");
104cb7eb8c9Sopenharmony_ci    if (state_ == ServiceRunningState::STATE_RUNNING) {
105cb7eb8c9Sopenharmony_ci        LOGD("Daemon has already started");
106cb7eb8c9Sopenharmony_ci        return;
107cb7eb8c9Sopenharmony_ci    }
108cb7eb8c9Sopenharmony_ci
109cb7eb8c9Sopenharmony_ci    try {
110cb7eb8c9Sopenharmony_ci        PublishSA();
111cb7eb8c9Sopenharmony_ci        StartEventHandler();
112cb7eb8c9Sopenharmony_ci        AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID);
113cb7eb8c9Sopenharmony_ci        AddSystemAbilityListener(SOFTBUS_SERVER_SA_ID);
114cb7eb8c9Sopenharmony_ci        umask(DEFAULT_UMASK);
115cb7eb8c9Sopenharmony_ci        AllConnectManager::GetInstance().InitAllConnectManager();
116cb7eb8c9Sopenharmony_ci    } catch (const exception &e) {
117cb7eb8c9Sopenharmony_ci        LOGE("%{public}s", e.what());
118cb7eb8c9Sopenharmony_ci    }
119cb7eb8c9Sopenharmony_ci
120cb7eb8c9Sopenharmony_ci    state_ = ServiceRunningState::STATE_RUNNING;
121cb7eb8c9Sopenharmony_ci    LOGI("Start service successfully");
122cb7eb8c9Sopenharmony_ci}
123cb7eb8c9Sopenharmony_ci
124cb7eb8c9Sopenharmony_civoid Daemon::OnStop()
125cb7eb8c9Sopenharmony_ci{
126cb7eb8c9Sopenharmony_ci    LOGI("Begin to stop");
127cb7eb8c9Sopenharmony_ci    state_ = ServiceRunningState::STATE_NOT_START;
128cb7eb8c9Sopenharmony_ci    registerToService_ = false;
129cb7eb8c9Sopenharmony_ci    bool subRet = EventFwk::CommonEventManager::UnSubscribeCommonEvent(subScriber_);
130cb7eb8c9Sopenharmony_ci    if (!subRet) {
131cb7eb8c9Sopenharmony_ci        LOGE("UnSubscribe common event failed");
132cb7eb8c9Sopenharmony_ci    }
133cb7eb8c9Sopenharmony_ci    subScriber_ = nullptr;
134cb7eb8c9Sopenharmony_ci    daemonExecute_ = nullptr;
135cb7eb8c9Sopenharmony_ci    eventHandler_ = nullptr;
136cb7eb8c9Sopenharmony_ci    SoftBusHandlerAsset::GetInstance().DeleteAssetLocalSessionServer();
137cb7eb8c9Sopenharmony_ci    AllConnectManager::GetInstance().UnInitAllConnectManager();
138cb7eb8c9Sopenharmony_ci    LOGI("Stop finished successfully");
139cb7eb8c9Sopenharmony_ci}
140cb7eb8c9Sopenharmony_ci
141cb7eb8c9Sopenharmony_civoid Daemon::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
142cb7eb8c9Sopenharmony_ci{
143cb7eb8c9Sopenharmony_ci    if (systemAbilityId == COMMON_EVENT_SERVICE_ID) {
144cb7eb8c9Sopenharmony_ci        (void)systemAbilityId;
145cb7eb8c9Sopenharmony_ci        (void)deviceId;
146cb7eb8c9Sopenharmony_ci        RegisterOsAccount();
147cb7eb8c9Sopenharmony_ci    } else if (systemAbilityId == SOFTBUS_SERVER_SA_ID) {
148cb7eb8c9Sopenharmony_ci        SoftBusHandlerAsset::GetInstance().CreateAssetLocalSessionServer();
149cb7eb8c9Sopenharmony_ci    }
150cb7eb8c9Sopenharmony_ci}
151cb7eb8c9Sopenharmony_ci
152cb7eb8c9Sopenharmony_civoid Daemon::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
153cb7eb8c9Sopenharmony_ci{
154cb7eb8c9Sopenharmony_ci    (void)deviceId;
155cb7eb8c9Sopenharmony_ci    if (systemAbilityId == COMMON_EVENT_SERVICE_ID) {
156cb7eb8c9Sopenharmony_ci        if (subScriber_ == nullptr) {
157cb7eb8c9Sopenharmony_ci            LOGE("Daemon::OnRemoveSystemAbility subscriberPtr is nullptr");
158cb7eb8c9Sopenharmony_ci            return;
159cb7eb8c9Sopenharmony_ci        }
160cb7eb8c9Sopenharmony_ci
161cb7eb8c9Sopenharmony_ci        bool subscribeResult = EventFwk::CommonEventManager::UnSubscribeCommonEvent(subScriber_);
162cb7eb8c9Sopenharmony_ci        LOGI("Daemon::OnRemoveSystemAbility subscribeResult = %{public}d", subscribeResult);
163cb7eb8c9Sopenharmony_ci        subScriber_ = nullptr;
164cb7eb8c9Sopenharmony_ci    } else if (systemAbilityId == SOFTBUS_SERVER_SA_ID) {
165cb7eb8c9Sopenharmony_ci        SoftBusHandlerAsset::GetInstance().DeleteAssetLocalSessionServer();
166cb7eb8c9Sopenharmony_ci    }
167cb7eb8c9Sopenharmony_ci}
168cb7eb8c9Sopenharmony_ci
169cb7eb8c9Sopenharmony_ciint32_t Daemon::OpenP2PConnection(const DistributedHardware::DmDeviceInfo &deviceInfo)
170cb7eb8c9Sopenharmony_ci{
171cb7eb8c9Sopenharmony_ci    LOGI("OpenP2PConnection networkId %{public}s", Utils::GetAnonyString(deviceInfo.networkId).c_str());
172cb7eb8c9Sopenharmony_ci    RADAR_REPORT(RadarReporter::DFX_SET_DFS, RadarReporter::DFX_BUILD__LINK, RadarReporter::DFX_SUCCESS,
173cb7eb8c9Sopenharmony_ci        RadarReporter::BIZ_STATE, RadarReporter::DFX_BEGIN);
174cb7eb8c9Sopenharmony_ci    auto path = ConnectionDetector::ParseHmdfsPath();
175cb7eb8c9Sopenharmony_ci    stringstream ss;
176cb7eb8c9Sopenharmony_ci    ss << ConnectionDetector::MocklispHash(path);
177cb7eb8c9Sopenharmony_ci    auto targetDir = ss.str();
178cb7eb8c9Sopenharmony_ci    auto networkId = std::string(deviceInfo.networkId);
179cb7eb8c9Sopenharmony_ci    int32_t ret = 0;
180cb7eb8c9Sopenharmony_ci    if (!ConnectionDetector::GetConnectionStatus(targetDir, networkId)) {
181cb7eb8c9Sopenharmony_ci        DeviceManagerAgent::GetInstance()->ClearCount(deviceInfo);
182cb7eb8c9Sopenharmony_ci        LOGI("Get connection status not ok, try again.");
183cb7eb8c9Sopenharmony_ci        ret = DeviceManagerAgent::GetInstance()->OnDeviceP2POnline(deviceInfo);
184cb7eb8c9Sopenharmony_ci        if (ret != NO_ERROR) {
185cb7eb8c9Sopenharmony_ci            LOGE("OpenP2PConnection failed, ret = %{public}d", ret);
186cb7eb8c9Sopenharmony_ci        } else {
187cb7eb8c9Sopenharmony_ci            ret = ConnectionDetector::RepeatGetConnectionStatus(targetDir, networkId);
188cb7eb8c9Sopenharmony_ci            LOGI("RepeatGetConnectionStatus end, ret = %{public}d", ret);
189cb7eb8c9Sopenharmony_ci        }
190cb7eb8c9Sopenharmony_ci    }
191cb7eb8c9Sopenharmony_ci    if (ret == FileManagement::ERR_BAD_VALUE) {
192cb7eb8c9Sopenharmony_ci        LOGI("OpenP2PConnection check connection status failed, start to clean up");
193cb7eb8c9Sopenharmony_ci        CloseP2PConnection(deviceInfo);
194cb7eb8c9Sopenharmony_ci    }
195cb7eb8c9Sopenharmony_ci    RADAR_REPORT(RadarReporter::DFX_SET_DFS, RadarReporter::DFX_BUILD__LINK, RadarReporter::DFX_SUCCESS,
196cb7eb8c9Sopenharmony_ci        RadarReporter::BIZ_STATE, RadarReporter::DFX_END);
197cb7eb8c9Sopenharmony_ci    return ret;
198cb7eb8c9Sopenharmony_ci}
199cb7eb8c9Sopenharmony_ci
200cb7eb8c9Sopenharmony_ciint32_t Daemon::CloseP2PConnection(const DistributedHardware::DmDeviceInfo &deviceInfo)
201cb7eb8c9Sopenharmony_ci{
202cb7eb8c9Sopenharmony_ci    LOGI("Close P2P Connection networkId %{public}s", Utils::GetAnonyString(deviceInfo.networkId).c_str());
203cb7eb8c9Sopenharmony_ci    std::thread([=]() {
204cb7eb8c9Sopenharmony_ci        int32_t ret = DeviceManagerAgent::GetInstance()->OnDeviceP2POffline(deviceInfo);
205cb7eb8c9Sopenharmony_ci        LOGI("Close P2P Connection result %d", ret);
206cb7eb8c9Sopenharmony_ci        return ret;
207cb7eb8c9Sopenharmony_ci        }).detach();
208cb7eb8c9Sopenharmony_ci    return 0;
209cb7eb8c9Sopenharmony_ci}
210cb7eb8c9Sopenharmony_ci
211cb7eb8c9Sopenharmony_ciint32_t Daemon::ConnectionCount(const DistributedHardware::DmDeviceInfo &deviceInfo)
212cb7eb8c9Sopenharmony_ci{
213cb7eb8c9Sopenharmony_ci    auto path = ConnectionDetector::ParseHmdfsPath();
214cb7eb8c9Sopenharmony_ci    stringstream ss;
215cb7eb8c9Sopenharmony_ci    ss << ConnectionDetector::MocklispHash(path);
216cb7eb8c9Sopenharmony_ci    auto targetDir = ss.str();
217cb7eb8c9Sopenharmony_ci    auto networkId = std::string(deviceInfo.networkId);
218cb7eb8c9Sopenharmony_ci    int32_t ret = 0;
219cb7eb8c9Sopenharmony_ci    ret = DeviceManagerAgent::GetInstance()->OnDeviceP2POnline(deviceInfo);
220cb7eb8c9Sopenharmony_ci    if (ret != NO_ERROR) {
221cb7eb8c9Sopenharmony_ci        LOGE("OpenP2PConnection failed, ret = %{public}d", ret);
222cb7eb8c9Sopenharmony_ci    } else {
223cb7eb8c9Sopenharmony_ci        ret = ConnectionDetector::RepeatGetConnectionStatus(targetDir, networkId);
224cb7eb8c9Sopenharmony_ci        LOGI("RepeatGetConnectionStatus first time, ret = %{public}d", ret);
225cb7eb8c9Sopenharmony_ci    }
226cb7eb8c9Sopenharmony_ci    ret = ConnectionDetector::RepeatGetConnectionStatus(targetDir, networkId);
227cb7eb8c9Sopenharmony_ci    if (ret != NO_ERROR) {
228cb7eb8c9Sopenharmony_ci        LOGI("RepeatGetConnectionStatus third times, ret = %{public}d", ret);
229cb7eb8c9Sopenharmony_ci        usleep(CHECK_SESSION_DELAY_TIME_TWICE);
230cb7eb8c9Sopenharmony_ci        ret = ConnectionDetector::RepeatGetConnectionStatus(targetDir, networkId);
231cb7eb8c9Sopenharmony_ci    }
232cb7eb8c9Sopenharmony_ci    return ret;
233cb7eb8c9Sopenharmony_ci}
234cb7eb8c9Sopenharmony_ci
235cb7eb8c9Sopenharmony_ciint32_t Daemon::CleanUp(const DistributedHardware::DmDeviceInfo &deviceInfo,
236cb7eb8c9Sopenharmony_ci                        const std::string &networkId, uint32_t callingTokenId)
237cb7eb8c9Sopenharmony_ci{
238cb7eb8c9Sopenharmony_ci    LOGI("CleanUp start");
239cb7eb8c9Sopenharmony_ci    auto deviceManager = DeviceManagerAgent::GetInstance();
240cb7eb8c9Sopenharmony_ci    if (deviceManager->RemoveRemoteReverseObj(false, callingTokenId) != E_OK) {
241cb7eb8c9Sopenharmony_ci        LOGE("fail to RemoveRemoteReverseObj");
242cb7eb8c9Sopenharmony_ci    }
243cb7eb8c9Sopenharmony_ci    deviceManager->RemoveNetworkIdByOne(callingTokenId, networkId);
244cb7eb8c9Sopenharmony_ci    int32_t ret = CloseP2PConnection(deviceInfo);
245cb7eb8c9Sopenharmony_ci    if (ret != NO_ERROR) {
246cb7eb8c9Sopenharmony_ci        LOGE("Daemon::CleanUp CloseP2PConnection failed");
247cb7eb8c9Sopenharmony_ci        return E_CONNECTION_FAILED;
248cb7eb8c9Sopenharmony_ci    }
249cb7eb8c9Sopenharmony_ci    return 0;
250cb7eb8c9Sopenharmony_ci}
251cb7eb8c9Sopenharmony_ci
252cb7eb8c9Sopenharmony_ciint32_t Daemon::ConnectionAndMount(const DistributedHardware::DmDeviceInfo &deviceInfo,
253cb7eb8c9Sopenharmony_ci                                   const std::string &networkId, uint32_t callingTokenId)
254cb7eb8c9Sopenharmony_ci{
255cb7eb8c9Sopenharmony_ci    LOGI("ConnectionAndMount start");
256cb7eb8c9Sopenharmony_ci    int32_t ret = NO_ERROR;
257cb7eb8c9Sopenharmony_ci    ret = ConnectionCount(deviceInfo);
258cb7eb8c9Sopenharmony_ci    if (ret != NO_ERROR) {
259cb7eb8c9Sopenharmony_ci        LOGE("connection failed");
260cb7eb8c9Sopenharmony_ci        return ret;
261cb7eb8c9Sopenharmony_ci    }
262cb7eb8c9Sopenharmony_ci    auto deviceManager = DeviceManagerAgent::GetInstance();
263cb7eb8c9Sopenharmony_ci    deviceManager->AddNetworkId(callingTokenId, networkId);
264cb7eb8c9Sopenharmony_ci    if (!DfsuAccessTokenHelper::CheckCallerPermission(FILE_ACCESS_MANAGER_PERMISSION)) {
265cb7eb8c9Sopenharmony_ci        LOGW("permission denied: FILE_ACCESS_MANAGER_PERMISSION");
266cb7eb8c9Sopenharmony_ci        return ret;
267cb7eb8c9Sopenharmony_ci    }
268cb7eb8c9Sopenharmony_ci    std::string deviceId = deviceManager->GetDeviceIdByNetworkId(networkId);
269cb7eb8c9Sopenharmony_ci    ret = deviceManager->MountDfsDocs(networkId, deviceId);
270cb7eb8c9Sopenharmony_ci    if (ret != NO_ERROR) {
271cb7eb8c9Sopenharmony_ci        LOGE("[MountDfsDocs] failed");
272cb7eb8c9Sopenharmony_ci    }
273cb7eb8c9Sopenharmony_ci    return ret;
274cb7eb8c9Sopenharmony_ci}
275cb7eb8c9Sopenharmony_ci
276cb7eb8c9Sopenharmony_ciint32_t Daemon::OpenP2PConnectionEx(const std::string &networkId, sptr<IFileDfsListener> remoteReverseObj)
277cb7eb8c9Sopenharmony_ci{
278cb7eb8c9Sopenharmony_ci    LOGI("Daemon::OpenP2PConnectionEx start, networkId %{public}s", Utils::GetAnonyString(networkId).c_str());
279cb7eb8c9Sopenharmony_ci    if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_DISTRIBUTED_DATASYNC)) {
280cb7eb8c9Sopenharmony_ci        LOGE("[OpenP2PConnectionEx] DATASYNC permission denied");
281cb7eb8c9Sopenharmony_ci        return E_PERMISSION_DENIED_NAPI;
282cb7eb8c9Sopenharmony_ci    }
283cb7eb8c9Sopenharmony_ci    if (dfsListenerDeathRecipient_ == nullptr) {
284cb7eb8c9Sopenharmony_ci        LOGE("Daemon::OpenP2PConnectionEx, new death recipient");
285cb7eb8c9Sopenharmony_ci        dfsListenerDeathRecipient_ = new (std::nothrow) DfsListenerDeathRecipient();
286cb7eb8c9Sopenharmony_ci    }
287cb7eb8c9Sopenharmony_ci    if (remoteReverseObj == nullptr) {
288cb7eb8c9Sopenharmony_ci        LOGE("Daemon::OpenP2PConnectionEx remoteReverseObj is nullptr");
289cb7eb8c9Sopenharmony_ci        return E_INVAL_ARG_NAPI;
290cb7eb8c9Sopenharmony_ci    }
291cb7eb8c9Sopenharmony_ci    remoteReverseObj->AsObject()->AddDeathRecipient(dfsListenerDeathRecipient_);
292cb7eb8c9Sopenharmony_ci    auto deviceManager = DeviceManagerAgent::GetInstance();
293cb7eb8c9Sopenharmony_ci    if (networkId.empty()) {
294cb7eb8c9Sopenharmony_ci        LOGE("Daemon::OpenP2PConnectionEx networkId is null");
295cb7eb8c9Sopenharmony_ci        return E_INVAL_ARG_NAPI;
296cb7eb8c9Sopenharmony_ci    }
297cb7eb8c9Sopenharmony_ci    DistributedHardware::DmDeviceInfo deviceInfo{};
298cb7eb8c9Sopenharmony_ci    auto res = strcpy_s(deviceInfo.networkId, networkId.size() + 1, networkId.c_str());
299cb7eb8c9Sopenharmony_ci    if (res != NO_ERROR) {
300cb7eb8c9Sopenharmony_ci        LOGE("OpenP2PConnectionEx strcpy failed, res = %{public}d", res);
301cb7eb8c9Sopenharmony_ci        return E_INVAL_ARG_NAPI;
302cb7eb8c9Sopenharmony_ci    }
303cb7eb8c9Sopenharmony_ci    auto callingTokenId = IPCSkeleton::GetCallingTokenID();
304cb7eb8c9Sopenharmony_ci    if (deviceManager->AddRemoteReverseObj(callingTokenId, remoteReverseObj) != E_OK) {
305cb7eb8c9Sopenharmony_ci        LOGE("Daemon::OpenP2PConnectionEx::fail to AddRemoteReverseObj");
306cb7eb8c9Sopenharmony_ci    }
307cb7eb8c9Sopenharmony_ci    int32_t ret = ConnectionAndMount(deviceInfo, networkId, callingTokenId);
308cb7eb8c9Sopenharmony_ci    if (ret != NO_ERROR) {
309cb7eb8c9Sopenharmony_ci        CleanUp(deviceInfo, networkId, callingTokenId);
310cb7eb8c9Sopenharmony_ci        return E_CONNECTION_FAILED;
311cb7eb8c9Sopenharmony_ci    }
312cb7eb8c9Sopenharmony_ci    LOGI("Daemon::OpenP2PConnectionEx end");
313cb7eb8c9Sopenharmony_ci    return ret;
314cb7eb8c9Sopenharmony_ci}
315cb7eb8c9Sopenharmony_ci
316cb7eb8c9Sopenharmony_ciint32_t Daemon::CloseP2PConnectionEx(const std::string &networkId)
317cb7eb8c9Sopenharmony_ci{
318cb7eb8c9Sopenharmony_ci    LOGI("Daemon::CloseP2PConnectionEx start, networkId: %{public}s", Utils::GetAnonyString(networkId).c_str());
319cb7eb8c9Sopenharmony_ci    if (!DfsuAccessTokenHelper::CheckCallerPermission(PERM_DISTRIBUTED_DATASYNC)) {
320cb7eb8c9Sopenharmony_ci        LOGE("[CloseP2PConnectionEx] DATASYNC permission denied");
321cb7eb8c9Sopenharmony_ci        return E_PERMISSION_DENIED_NAPI;
322cb7eb8c9Sopenharmony_ci    }
323cb7eb8c9Sopenharmony_ci    auto deviceManager = DeviceManagerAgent::GetInstance();
324cb7eb8c9Sopenharmony_ci    auto callingTokenId = IPCSkeleton::GetCallingTokenID();
325cb7eb8c9Sopenharmony_ci    if (networkId.empty()) {
326cb7eb8c9Sopenharmony_ci        LOGE("[OpenP2PConnectionEx] networkId is null");
327cb7eb8c9Sopenharmony_ci        return E_INVAL_ARG_NAPI;
328cb7eb8c9Sopenharmony_ci    }
329cb7eb8c9Sopenharmony_ci    std::string deviceId = deviceManager->GetDeviceIdByNetworkId(networkId);
330cb7eb8c9Sopenharmony_ci    if (deviceId.empty()) {
331cb7eb8c9Sopenharmony_ci        LOGE("fail to get deviceId");
332cb7eb8c9Sopenharmony_ci        return E_CONNECTION_FAILED;
333cb7eb8c9Sopenharmony_ci    }
334cb7eb8c9Sopenharmony_ci    if (DfsuAccessTokenHelper::CheckCallerPermission(FILE_ACCESS_MANAGER_PERMISSION)) {
335cb7eb8c9Sopenharmony_ci        LOGE("[UMountDfsDocs] permission ok: FILE_ACCESS_MANAGER_PERMISSION");
336cb7eb8c9Sopenharmony_ci        int32_t ret_umount = deviceManager->UMountDfsDocs(networkId, deviceId, false);
337cb7eb8c9Sopenharmony_ci        if (ret_umount != E_OK) {
338cb7eb8c9Sopenharmony_ci            LOGE("[UMountDfsDocs] failed");
339cb7eb8c9Sopenharmony_ci            return E_UNMOUNT;
340cb7eb8c9Sopenharmony_ci        }
341cb7eb8c9Sopenharmony_ci    }
342cb7eb8c9Sopenharmony_ci    DistributedHardware::DmDeviceInfo deviceInfo{};
343cb7eb8c9Sopenharmony_ci    auto res = strcpy_s(deviceInfo.networkId, networkId.size() + 1, networkId.c_str());
344cb7eb8c9Sopenharmony_ci    if (res != NO_ERROR) {
345cb7eb8c9Sopenharmony_ci        LOGE("strcpy failed, res = %{public}d", res);
346cb7eb8c9Sopenharmony_ci        return E_INVAL_ARG_NAPI;
347cb7eb8c9Sopenharmony_ci    }
348cb7eb8c9Sopenharmony_ci    int32_t ret = CleanUp(deviceInfo, networkId, callingTokenId);
349cb7eb8c9Sopenharmony_ci    if (ret != NO_ERROR) {
350cb7eb8c9Sopenharmony_ci        LOGE("Daemon::CloseP2PConnectionEx disconnection failed");
351cb7eb8c9Sopenharmony_ci        return E_CONNECTION_FAILED;
352cb7eb8c9Sopenharmony_ci    }
353cb7eb8c9Sopenharmony_ci    LOGI("Daemon::CloseP2PConnectionEx end");
354cb7eb8c9Sopenharmony_ci    return 0;
355cb7eb8c9Sopenharmony_ci}
356cb7eb8c9Sopenharmony_ci
357cb7eb8c9Sopenharmony_ciint32_t Daemon::RequestSendFile(const std::string &srcUri,
358cb7eb8c9Sopenharmony_ci                                const std::string &dstPath,
359cb7eb8c9Sopenharmony_ci                                const std::string &dstDeviceId,
360cb7eb8c9Sopenharmony_ci                                const std::string &sessionName)
361cb7eb8c9Sopenharmony_ci{
362cb7eb8c9Sopenharmony_ci    LOGI("RequestSendFile begin dstDeviceId: %{public}s", Utils::GetAnonyString(dstDeviceId).c_str());
363cb7eb8c9Sopenharmony_ci
364cb7eb8c9Sopenharmony_ci    auto requestSendFileBlock = std::make_shared<BlockObject<int32_t>>(BLOCK_INTERVAL_SEND_FILE, ERR_BAD_VALUE);
365cb7eb8c9Sopenharmony_ci    auto requestSendFileData = std::make_shared<RequestSendFileData>(
366cb7eb8c9Sopenharmony_ci        srcUri, dstPath, dstDeviceId, sessionName, requestSendFileBlock);
367cb7eb8c9Sopenharmony_ci    auto msgEvent = AppExecFwk::InnerEvent::Get(DEAMON_EXECUTE_REQUEST_SEND_FILE, requestSendFileData, 0);
368cb7eb8c9Sopenharmony_ci    {
369cb7eb8c9Sopenharmony_ci        std::lock_guard<std::mutex> lock(eventHandlerMutex_);
370cb7eb8c9Sopenharmony_ci        if (eventHandler_ == nullptr) {
371cb7eb8c9Sopenharmony_ci            LOGE("eventHandler has not find");
372cb7eb8c9Sopenharmony_ci            return E_EVENT_HANDLER;
373cb7eb8c9Sopenharmony_ci        }
374cb7eb8c9Sopenharmony_ci        bool isSucc = eventHandler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE);
375cb7eb8c9Sopenharmony_ci        if (!isSucc) {
376cb7eb8c9Sopenharmony_ci            LOGE("Daemon event handler post push asset event fail.");
377cb7eb8c9Sopenharmony_ci            return E_EVENT_HANDLER;
378cb7eb8c9Sopenharmony_ci        }
379cb7eb8c9Sopenharmony_ci    }
380cb7eb8c9Sopenharmony_ci
381cb7eb8c9Sopenharmony_ci    auto ret = requestSendFileBlock->GetValue();
382cb7eb8c9Sopenharmony_ci    LOGI("RequestSendFile end, ret is %{public}d", ret);
383cb7eb8c9Sopenharmony_ci    return ret;
384cb7eb8c9Sopenharmony_ci}
385cb7eb8c9Sopenharmony_ci
386cb7eb8c9Sopenharmony_ciint32_t Daemon::PrepareSession(const std::string &srcUri,
387cb7eb8c9Sopenharmony_ci                               const std::string &dstUri,
388cb7eb8c9Sopenharmony_ci                               const std::string &srcDeviceId,
389cb7eb8c9Sopenharmony_ci                               const sptr<IRemoteObject> &listener,
390cb7eb8c9Sopenharmony_ci                               HmdfsInfo &info)
391cb7eb8c9Sopenharmony_ci{
392cb7eb8c9Sopenharmony_ci    LOGI("PrepareSession begin srcDeviceId: %{public}s", Utils::GetAnonyString(srcDeviceId).c_str());
393cb7eb8c9Sopenharmony_ci    AllConnectManager::GetInstance().PublishServiceState(srcDeviceId,
394cb7eb8c9Sopenharmony_ci        ServiceCollaborationManagerBussinessStatus::SCM_IDLE);
395cb7eb8c9Sopenharmony_ci
396cb7eb8c9Sopenharmony_ci    auto listenerCallback = iface_cast<IFileTransListener>(listener);
397cb7eb8c9Sopenharmony_ci    if (listenerCallback == nullptr) {
398cb7eb8c9Sopenharmony_ci        LOGE("ListenerCallback is nullptr");
399cb7eb8c9Sopenharmony_ci        return E_NULLPTR;
400cb7eb8c9Sopenharmony_ci    }
401cb7eb8c9Sopenharmony_ci
402cb7eb8c9Sopenharmony_ci    auto daemon = GetRemoteSA(srcDeviceId);
403cb7eb8c9Sopenharmony_ci    if (daemon == nullptr) {
404cb7eb8c9Sopenharmony_ci        LOGE("Daemon is nullptr");
405cb7eb8c9Sopenharmony_ci        return E_SA_LOAD_FAILED;
406cb7eb8c9Sopenharmony_ci    }
407cb7eb8c9Sopenharmony_ci
408cb7eb8c9Sopenharmony_ci    std::string physicalPath;
409cb7eb8c9Sopenharmony_ci    auto ret = GetRealPath(srcUri, dstUri, physicalPath, info, daemon);
410cb7eb8c9Sopenharmony_ci    if (ret != E_OK) {
411cb7eb8c9Sopenharmony_ci        LOGE("GetRealPath failed, ret = %{public}d", ret);
412cb7eb8c9Sopenharmony_ci        return ret;
413cb7eb8c9Sopenharmony_ci    }
414cb7eb8c9Sopenharmony_ci
415cb7eb8c9Sopenharmony_ci    SoftBusSessionPool::SessionInfo sessionInfo{.dstPath = physicalPath, .uid = IPCSkeleton::GetCallingUid()};
416cb7eb8c9Sopenharmony_ci    auto sessionName = SoftBusSessionPool::GetInstance().GenerateSessionName(sessionInfo);
417cb7eb8c9Sopenharmony_ci    if (sessionName.empty()) {
418cb7eb8c9Sopenharmony_ci        LOGE("SessionServer exceed max");
419cb7eb8c9Sopenharmony_ci        return E_SOFTBUS_SESSION_FAILED;
420cb7eb8c9Sopenharmony_ci    }
421cb7eb8c9Sopenharmony_ci    info.sessionName = sessionName;
422cb7eb8c9Sopenharmony_ci    StoreSessionAndListener(physicalPath, sessionName, listenerCallback);
423cb7eb8c9Sopenharmony_ci    auto socketId = SoftBusHandler::GetInstance().CreateSessionServer(IDaemon::SERVICE_NAME, sessionName,
424cb7eb8c9Sopenharmony_ci        DFS_CHANNLE_ROLE_SINK, physicalPath);
425cb7eb8c9Sopenharmony_ci    if (socketId <= 0) {
426cb7eb8c9Sopenharmony_ci        LOGE("CreateSessionServer failed, ret = %{public}d", ret);
427cb7eb8c9Sopenharmony_ci        DeleteSessionAndListener(sessionName, socketId);
428cb7eb8c9Sopenharmony_ci        return E_SOFTBUS_SESSION_FAILED;
429cb7eb8c9Sopenharmony_ci    }
430cb7eb8c9Sopenharmony_ci    physicalPath.clear();
431cb7eb8c9Sopenharmony_ci    if (info.authority == FILE_MANAGER_AUTHORITY || info.authority == MEDIA_AUTHORITY) {
432cb7eb8c9Sopenharmony_ci        LOGI("authority is media or docs");
433cb7eb8c9Sopenharmony_ci        physicalPath = "??" + info.dstPhysicalPath;
434cb7eb8c9Sopenharmony_ci    }
435cb7eb8c9Sopenharmony_ci    ret = Copy(srcUri, physicalPath, daemon, sessionName);
436cb7eb8c9Sopenharmony_ci    if (ret != E_OK) {
437cb7eb8c9Sopenharmony_ci        LOGE("Remote copy failed,ret = %{public}d", ret);
438cb7eb8c9Sopenharmony_ci        DeleteSessionAndListener(sessionName, socketId);
439cb7eb8c9Sopenharmony_ci        return ret;
440cb7eb8c9Sopenharmony_ci    }
441cb7eb8c9Sopenharmony_ci    return ret;
442cb7eb8c9Sopenharmony_ci}
443cb7eb8c9Sopenharmony_ci
444cb7eb8c9Sopenharmony_civoid Daemon::StoreSessionAndListener(const std::string &physicalPath,
445cb7eb8c9Sopenharmony_ci                                     const std::string &sessionName,
446cb7eb8c9Sopenharmony_ci                                     const sptr<IFileTransListener> &listener)
447cb7eb8c9Sopenharmony_ci{
448cb7eb8c9Sopenharmony_ci    TransManager::GetInstance().AddTransTask(sessionName, listener);
449cb7eb8c9Sopenharmony_ci}
450cb7eb8c9Sopenharmony_ci
451cb7eb8c9Sopenharmony_ciint32_t Daemon::GetRealPath(const std::string &srcUri,
452cb7eb8c9Sopenharmony_ci                            const std::string &dstUri,
453cb7eb8c9Sopenharmony_ci                            std::string &physicalPath,
454cb7eb8c9Sopenharmony_ci                            HmdfsInfo &info,
455cb7eb8c9Sopenharmony_ci                            const sptr<IDaemon> &daemon)
456cb7eb8c9Sopenharmony_ci{
457cb7eb8c9Sopenharmony_ci    auto start = std::chrono::high_resolution_clock::now();
458cb7eb8c9Sopenharmony_ci    bool isSrcFile = false;
459cb7eb8c9Sopenharmony_ci    bool isSrcDir = false;
460cb7eb8c9Sopenharmony_ci    if (daemon == nullptr) {
461cb7eb8c9Sopenharmony_ci        LOGE("Daemon::GetRealPath daemon is nullptr");
462cb7eb8c9Sopenharmony_ci        return E_INVAL_ARG_NAPI;
463cb7eb8c9Sopenharmony_ci    }
464cb7eb8c9Sopenharmony_ci    auto ret = daemon->GetRemoteCopyInfo(srcUri, isSrcFile, isSrcDir);
465cb7eb8c9Sopenharmony_ci    if (ret != E_OK) {
466cb7eb8c9Sopenharmony_ci        LOGE("GetRemoteCopyInfo failed, ret = %{public}d", ret);
467cb7eb8c9Sopenharmony_ci        RADAR_REPORT(RadarReporter::DFX_SET_DFS, RadarReporter::DFX_SET_BIZ_SCENE, RadarReporter::DFX_FAILED,
468cb7eb8c9Sopenharmony_ci            RadarReporter::BIZ_STATE, RadarReporter::DFX_END, RadarReporter::ERROR_CODE,
469cb7eb8c9Sopenharmony_ci            RadarReporter::GET_REMOTE_COPY_INFO_ERROR);
470cb7eb8c9Sopenharmony_ci        return E_SOFTBUS_SESSION_FAILED;
471cb7eb8c9Sopenharmony_ci    }
472cb7eb8c9Sopenharmony_ci
473cb7eb8c9Sopenharmony_ci    HapTokenInfo hapTokenInfo;
474cb7eb8c9Sopenharmony_ci    int result = AccessTokenKit::GetHapTokenInfo(IPCSkeleton::GetCallingTokenID(), hapTokenInfo);
475cb7eb8c9Sopenharmony_ci    if (result != Security::AccessToken::AccessTokenKitRet::RET_SUCCESS) {
476cb7eb8c9Sopenharmony_ci        LOGE("GetHapTokenInfo failed, errCode = %{public}d", result);
477cb7eb8c9Sopenharmony_ci        RADAR_REPORT(RadarReporter::DFX_SET_DFS, RadarReporter::DFX_SET_BIZ_SCENE, RadarReporter::DFX_FAILED,
478cb7eb8c9Sopenharmony_ci            RadarReporter::BIZ_STATE, RadarReporter::DFX_END, RadarReporter::ERROR_CODE,
479cb7eb8c9Sopenharmony_ci            RadarReporter::GET_HAP_TOKEN_INFO_ERROR, RadarReporter::PACKAGE_NAME,
480cb7eb8c9Sopenharmony_ci            RadarReporter::accessTokenKit + to_string(result));
481cb7eb8c9Sopenharmony_ci        return E_GET_USER_ID;
482cb7eb8c9Sopenharmony_ci    }
483cb7eb8c9Sopenharmony_ci    ret = SandboxHelper::GetPhysicalPath(dstUri, std::to_string(hapTokenInfo.userID), physicalPath);
484cb7eb8c9Sopenharmony_ci    if (ret != E_OK) {
485cb7eb8c9Sopenharmony_ci        LOGE("invalid uri, ret = %{public}d", ret);
486cb7eb8c9Sopenharmony_ci        return E_GET_PHYSICAL_PATH_FAILED;
487cb7eb8c9Sopenharmony_ci    }
488cb7eb8c9Sopenharmony_ci
489cb7eb8c9Sopenharmony_ci    LOGI("physicalPath %{public}s, userId %{public}s", GetAnonyString(physicalPath).c_str(),
490cb7eb8c9Sopenharmony_ci         std::to_string(hapTokenInfo.userID).c_str());
491cb7eb8c9Sopenharmony_ci    info.dstPhysicalPath = physicalPath;
492cb7eb8c9Sopenharmony_ci    ret = CheckCopyRule(physicalPath, dstUri, hapTokenInfo, isSrcFile, info);
493cb7eb8c9Sopenharmony_ci    if (ret != E_OK) {
494cb7eb8c9Sopenharmony_ci        LOGE("CheckCopyRule failed, ret = %{public}d", ret);
495cb7eb8c9Sopenharmony_ci        return E_GET_PHYSICAL_PATH_FAILED;
496cb7eb8c9Sopenharmony_ci    }
497cb7eb8c9Sopenharmony_ci    auto end = std::chrono::high_resolution_clock::now();
498cb7eb8c9Sopenharmony_ci    auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
499cb7eb8c9Sopenharmony_ci    Utils::SysEventFileParse(duration.count());
500cb7eb8c9Sopenharmony_ci    return E_OK;
501cb7eb8c9Sopenharmony_ci}
502cb7eb8c9Sopenharmony_ci
503cb7eb8c9Sopenharmony_ciint32_t Daemon::CheckCopyRule(std::string &physicalPath,
504cb7eb8c9Sopenharmony_ci                              const std::string &dstUri,
505cb7eb8c9Sopenharmony_ci                              HapTokenInfo &hapTokenInfo,
506cb7eb8c9Sopenharmony_ci                              const bool &isSrcFile,
507cb7eb8c9Sopenharmony_ci                              HmdfsInfo &info)
508cb7eb8c9Sopenharmony_ci{
509cb7eb8c9Sopenharmony_ci    auto checkPath = physicalPath;
510cb7eb8c9Sopenharmony_ci    if (isSrcFile && !Utils::IsFolder(physicalPath)) {
511cb7eb8c9Sopenharmony_ci        auto pos = physicalPath.rfind('/');
512cb7eb8c9Sopenharmony_ci        if (pos == std::string::npos) {
513cb7eb8c9Sopenharmony_ci            LOGE("invalid file path");
514cb7eb8c9Sopenharmony_ci            return E_GET_PHYSICAL_PATH_FAILED;
515cb7eb8c9Sopenharmony_ci        }
516cb7eb8c9Sopenharmony_ci        physicalPath = physicalPath.substr(0, pos);
517cb7eb8c9Sopenharmony_ci    }
518cb7eb8c9Sopenharmony_ci
519cb7eb8c9Sopenharmony_ci    std::error_code errCode;
520cb7eb8c9Sopenharmony_ci    if (!std::filesystem::exists(physicalPath, errCode) && info.dirExistFlag) {
521cb7eb8c9Sopenharmony_ci        LOGI("Not CheckValidPath, physicalPath %{public}s", GetAnonyString(physicalPath).c_str());
522cb7eb8c9Sopenharmony_ci    } else {
523cb7eb8c9Sopenharmony_ci        auto pos = checkPath.rfind('/');
524cb7eb8c9Sopenharmony_ci        if (pos == std::string::npos) {
525cb7eb8c9Sopenharmony_ci            LOGE("invalid file path");
526cb7eb8c9Sopenharmony_ci            return E_GET_PHYSICAL_PATH_FAILED;
527cb7eb8c9Sopenharmony_ci        }
528cb7eb8c9Sopenharmony_ci        checkPath = checkPath.substr(0, pos);
529cb7eb8c9Sopenharmony_ci        if (!SandboxHelper::CheckValidPath(checkPath)) {
530cb7eb8c9Sopenharmony_ci            LOGE("invalid path.");
531cb7eb8c9Sopenharmony_ci            return E_GET_PHYSICAL_PATH_FAILED;
532cb7eb8c9Sopenharmony_ci        }
533cb7eb8c9Sopenharmony_ci    }
534cb7eb8c9Sopenharmony_ci
535cb7eb8c9Sopenharmony_ci    Uri uri(dstUri);
536cb7eb8c9Sopenharmony_ci    auto authority = uri.GetAuthority();
537cb7eb8c9Sopenharmony_ci    info.authority = authority;
538cb7eb8c9Sopenharmony_ci    if (authority != FILE_MANAGER_AUTHORITY && authority != MEDIA_AUTHORITY) {
539cb7eb8c9Sopenharmony_ci        auto bundleName = SoftBusSessionListener::GetBundleName(dstUri);
540cb7eb8c9Sopenharmony_ci        physicalPath = "/data/service/el2/" + to_string(hapTokenInfo.userID) + "/hmdfs/account/data/" + bundleName +
541cb7eb8c9Sopenharmony_ci                       "/" + info.copyPath;
542cb7eb8c9Sopenharmony_ci        if (!SandboxHelper::CheckValidPath(physicalPath)) {
543cb7eb8c9Sopenharmony_ci            LOGE("invalid path.");
544cb7eb8c9Sopenharmony_ci            return E_GET_PHYSICAL_PATH_FAILED;
545cb7eb8c9Sopenharmony_ci        }
546cb7eb8c9Sopenharmony_ci    } else {
547cb7eb8c9Sopenharmony_ci        std::regex pathRegex("^[a-zA-Z0-9_\\-/\\\\]*$");
548cb7eb8c9Sopenharmony_ci        if (!std::filesystem::exists(physicalPath, errCode) && std::regex_match(physicalPath.c_str(), pathRegex)) {
549cb7eb8c9Sopenharmony_ci            std::filesystem::create_directory(physicalPath, errCode);
550cb7eb8c9Sopenharmony_ci            if (errCode.value() != 0) {
551cb7eb8c9Sopenharmony_ci                LOGE("Create directory failed, physicalPath %{public}s", GetAnonyString(physicalPath).c_str());
552cb7eb8c9Sopenharmony_ci                return E_GET_PHYSICAL_PATH_FAILED;
553cb7eb8c9Sopenharmony_ci            }
554cb7eb8c9Sopenharmony_ci        }
555cb7eb8c9Sopenharmony_ci    }
556cb7eb8c9Sopenharmony_ci    return E_OK;
557cb7eb8c9Sopenharmony_ci}
558cb7eb8c9Sopenharmony_ci
559cb7eb8c9Sopenharmony_ciint32_t Daemon::GetRemoteCopyInfo(const std::string &srcUri, bool &isSrcFile, bool &srcIsDir)
560cb7eb8c9Sopenharmony_ci{
561cb7eb8c9Sopenharmony_ci    LOGI("GetRemoteCopyInfo begin.");
562cb7eb8c9Sopenharmony_ci    auto physicalPath = SoftBusSessionListener::GetRealPath(srcUri);
563cb7eb8c9Sopenharmony_ci    if (physicalPath.empty()) {
564cb7eb8c9Sopenharmony_ci        LOGE("GetRemoteCopyInfo GetRealPath failed.");
565cb7eb8c9Sopenharmony_ci        return E_SOFTBUS_SESSION_FAILED;
566cb7eb8c9Sopenharmony_ci    }
567cb7eb8c9Sopenharmony_ci    isSrcFile = Utils::IsFile(physicalPath);
568cb7eb8c9Sopenharmony_ci    srcIsDir = Utils::IsFolder(physicalPath);
569cb7eb8c9Sopenharmony_ci    return E_OK;
570cb7eb8c9Sopenharmony_ci}
571cb7eb8c9Sopenharmony_ci
572cb7eb8c9Sopenharmony_cisptr<IDaemon> Daemon::GetRemoteSA(const std::string &remoteDeviceId)
573cb7eb8c9Sopenharmony_ci{
574cb7eb8c9Sopenharmony_ci    LOGI("GetRemoteSA begin, DeviceId: %{public}s", Utils::GetAnonyString(remoteDeviceId).c_str());
575cb7eb8c9Sopenharmony_ci    auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
576cb7eb8c9Sopenharmony_ci    if (sam == nullptr) {
577cb7eb8c9Sopenharmony_ci        LOGE("Sam is nullptr");
578cb7eb8c9Sopenharmony_ci        return nullptr;
579cb7eb8c9Sopenharmony_ci    }
580cb7eb8c9Sopenharmony_ci
581cb7eb8c9Sopenharmony_ci    auto object = sam->GetSystemAbility(FILEMANAGEMENT_DISTRIBUTED_FILE_DAEMON_SA_ID, remoteDeviceId);
582cb7eb8c9Sopenharmony_ci    if (object == nullptr) {
583cb7eb8c9Sopenharmony_ci        LOGE("GetSystemAbility failed");
584cb7eb8c9Sopenharmony_ci        RADAR_REPORT(RadarReporter::DFX_SET_DFS, RadarReporter::DFX_SET_BIZ_SCENE, RadarReporter::DFX_FAILED,
585cb7eb8c9Sopenharmony_ci            RadarReporter::BIZ_STATE, RadarReporter::DFX_END, RadarReporter::ERROR_CODE,
586cb7eb8c9Sopenharmony_ci            RadarReporter::GET_SYSTEM_ABILITY_ERROR, RadarReporter::PACKAGE_NAME, RadarReporter::saMgr);
587cb7eb8c9Sopenharmony_ci        return nullptr;
588cb7eb8c9Sopenharmony_ci    }
589cb7eb8c9Sopenharmony_ci    auto daemon = iface_cast<IDaemon>(object);
590cb7eb8c9Sopenharmony_ci    if (daemon == nullptr) {
591cb7eb8c9Sopenharmony_ci        LOGE("Connect service nullptr");
592cb7eb8c9Sopenharmony_ci        return nullptr;
593cb7eb8c9Sopenharmony_ci    }
594cb7eb8c9Sopenharmony_ci    LOGI("GetRemoteSA success, DeviceId: %{public}s", Utils::GetAnonyString(remoteDeviceId).c_str());
595cb7eb8c9Sopenharmony_ci    return daemon;
596cb7eb8c9Sopenharmony_ci}
597cb7eb8c9Sopenharmony_ci
598cb7eb8c9Sopenharmony_ciint32_t Daemon::Copy(const std::string &srcUri,
599cb7eb8c9Sopenharmony_ci                     const std::string &dstPath,
600cb7eb8c9Sopenharmony_ci                     const sptr<IDaemon> &daemon,
601cb7eb8c9Sopenharmony_ci                     const std::string &sessionName)
602cb7eb8c9Sopenharmony_ci{
603cb7eb8c9Sopenharmony_ci    auto &deviceManager = DistributedHardware::DeviceManager::GetInstance();
604cb7eb8c9Sopenharmony_ci    DistributedHardware::DmDeviceInfo localDeviceInfo{};
605cb7eb8c9Sopenharmony_ci    int errCode = deviceManager.GetLocalDeviceInfo(IDaemon::SERVICE_NAME, localDeviceInfo);
606cb7eb8c9Sopenharmony_ci    if (errCode != E_OK) {
607cb7eb8c9Sopenharmony_ci        LOGE("GetLocalDeviceInfo failed, errCode = %{public}d", errCode);
608cb7eb8c9Sopenharmony_ci        return E_GET_DEVICE_ID;
609cb7eb8c9Sopenharmony_ci    }
610cb7eb8c9Sopenharmony_ci    if (daemon == nullptr) {
611cb7eb8c9Sopenharmony_ci        LOGE("Daemon::Copy daemon is nullptr");
612cb7eb8c9Sopenharmony_ci        return E_INVAL_ARG_NAPI;
613cb7eb8c9Sopenharmony_ci    }
614cb7eb8c9Sopenharmony_ci    LOGI("Copy localDeviceInfo.networkId: %{public}s", Utils::GetAnonyString(localDeviceInfo.networkId).c_str());
615cb7eb8c9Sopenharmony_ci    auto ret = daemon->RequestSendFile(srcUri, dstPath, localDeviceInfo.networkId, sessionName);
616cb7eb8c9Sopenharmony_ci    if (ret != E_OK) {
617cb7eb8c9Sopenharmony_ci        LOGE("RequestSendFile failed, ret = %{public}d", ret);
618cb7eb8c9Sopenharmony_ci        RADAR_REPORT(RadarReporter::DFX_SET_DFS, RadarReporter::DFX_SET_BIZ_SCENE, RadarReporter::DFX_FAILED,
619cb7eb8c9Sopenharmony_ci            RadarReporter::BIZ_STATE, RadarReporter::DFX_END, RadarReporter::ERROR_CODE,
620cb7eb8c9Sopenharmony_ci            RadarReporter::REQUEST_SEND_FILE_ERROR, RadarReporter::PACKAGE_NAME, ret);
621cb7eb8c9Sopenharmony_ci        return E_SA_LOAD_FAILED;
622cb7eb8c9Sopenharmony_ci    }
623cb7eb8c9Sopenharmony_ci    return E_OK;
624cb7eb8c9Sopenharmony_ci}
625cb7eb8c9Sopenharmony_ci
626cb7eb8c9Sopenharmony_ciint32_t Daemon::CancelCopyTask(const std::string &sessionName)
627cb7eb8c9Sopenharmony_ci{
628cb7eb8c9Sopenharmony_ci    LOGI("Cancel copy task in. sessionName = %{public}s", sessionName.c_str());
629cb7eb8c9Sopenharmony_ci    SoftBusSessionPool::SessionInfo sessionInfo{};
630cb7eb8c9Sopenharmony_ci    bool isExist = SoftBusSessionPool::GetInstance().GetSessionInfo(sessionName, sessionInfo);
631cb7eb8c9Sopenharmony_ci    if (!isExist) {
632cb7eb8c9Sopenharmony_ci        LOGE("CancelCopyTask failed, cannot get session info for input session name=%{public}s.", sessionName.c_str());
633cb7eb8c9Sopenharmony_ci        return E_INVAL_ARG;
634cb7eb8c9Sopenharmony_ci    }
635cb7eb8c9Sopenharmony_ci    auto callingUid = IPCSkeleton::GetCallingUid();
636cb7eb8c9Sopenharmony_ci    if (callingUid != sessionInfo.uid) {
637cb7eb8c9Sopenharmony_ci        LOGE("CancelCopyTask failed, calling uid=%{public}d has no permission to cancel copy for uid=%{public}d.",
638cb7eb8c9Sopenharmony_ci             callingUid, sessionInfo.uid);
639cb7eb8c9Sopenharmony_ci        return E_PERMISSION_DENIED;
640cb7eb8c9Sopenharmony_ci    }
641cb7eb8c9Sopenharmony_ci    SoftBusHandler::GetInstance().CloseSessionWithSessionName(sessionName);
642cb7eb8c9Sopenharmony_ci    return E_OK;
643cb7eb8c9Sopenharmony_ci}
644cb7eb8c9Sopenharmony_ci
645cb7eb8c9Sopenharmony_civoid Daemon::DeleteSessionAndListener(const std::string &sessionName, const int32_t socketId)
646cb7eb8c9Sopenharmony_ci{
647cb7eb8c9Sopenharmony_ci    SoftBusSessionPool::GetInstance().DeleteSessionInfo(sessionName);
648cb7eb8c9Sopenharmony_ci    TransManager::GetInstance().DeleteTransTask(sessionName);
649cb7eb8c9Sopenharmony_ci    SoftBusHandler::GetInstance().CloseSession(socketId, sessionName);
650cb7eb8c9Sopenharmony_ci}
651cb7eb8c9Sopenharmony_ci
652cb7eb8c9Sopenharmony_civoid Daemon::DfsListenerDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
653cb7eb8c9Sopenharmony_ci{
654cb7eb8c9Sopenharmony_ci    sptr<IRemoteObject> diedRemote = remote.promote();
655cb7eb8c9Sopenharmony_ci    if (diedRemote == nullptr) {
656cb7eb8c9Sopenharmony_ci        LOGE("Daemon::DfsListenerDeathRecipient OnremoteDied received died notify nullptr");
657cb7eb8c9Sopenharmony_ci        return;
658cb7eb8c9Sopenharmony_ci    }
659cb7eb8c9Sopenharmony_ci    auto deviceManager = DeviceManagerAgent::GetInstance();
660cb7eb8c9Sopenharmony_ci    uint32_t callingTokenId = 0;
661cb7eb8c9Sopenharmony_ci    sptr<IFileDfsListener> listener = nullptr;
662cb7eb8c9Sopenharmony_ci    deviceManager->FindListenerByObject(remote, callingTokenId, listener);
663cb7eb8c9Sopenharmony_ci    if (callingTokenId==0 || listener == nullptr) {
664cb7eb8c9Sopenharmony_ci        LOGE("fail to FindListenerByObject");
665cb7eb8c9Sopenharmony_ci        return;
666cb7eb8c9Sopenharmony_ci    }
667cb7eb8c9Sopenharmony_ci    if (deviceManager->RemoveRemoteReverseObj(false, callingTokenId) != E_OK) {
668cb7eb8c9Sopenharmony_ci        LOGE("fail to RemoveRemoteReverseObj");
669cb7eb8c9Sopenharmony_ci    }
670cb7eb8c9Sopenharmony_ci    auto networkIdSet = deviceManager->GetNetworkIds(callingTokenId);
671cb7eb8c9Sopenharmony_ci    if (networkIdSet.empty()) {
672cb7eb8c9Sopenharmony_ci        LOGE("fail to get networkIdSet");
673cb7eb8c9Sopenharmony_ci        return;
674cb7eb8c9Sopenharmony_ci    }
675cb7eb8c9Sopenharmony_ci    for (auto it = networkIdSet.begin(); it != networkIdSet.end(); ++it) {
676cb7eb8c9Sopenharmony_ci        if (it->empty()) {
677cb7eb8c9Sopenharmony_ci            LOGE("[DfsListenerDeathRecipient] networkId is null");
678cb7eb8c9Sopenharmony_ci            continue;
679cb7eb8c9Sopenharmony_ci        }
680cb7eb8c9Sopenharmony_ci        std::string deviceId = deviceManager->GetDeviceIdByNetworkId(*it);
681cb7eb8c9Sopenharmony_ci        if (deviceId.empty()) {
682cb7eb8c9Sopenharmony_ci            LOGE("fail to get deviceId, networkId: %{public}s", Utils::GetAnonyString(*it).c_str());
683cb7eb8c9Sopenharmony_ci            continue;
684cb7eb8c9Sopenharmony_ci        }
685cb7eb8c9Sopenharmony_ci        deviceManager->UMountDfsDocs(*it, deviceId, false);
686cb7eb8c9Sopenharmony_ci        DistributedHardware::DmDeviceInfo deviceInfo{};
687cb7eb8c9Sopenharmony_ci        auto res = strcpy_s(deviceInfo.networkId, it->size() + 1, it->c_str());
688cb7eb8c9Sopenharmony_ci        if (res != 0) {
689cb7eb8c9Sopenharmony_ci            LOGE("strcpy failed, res = %{public}d", res);
690cb7eb8c9Sopenharmony_ci            return;
691cb7eb8c9Sopenharmony_ci        }
692cb7eb8c9Sopenharmony_ci        std::thread([=]() {
693cb7eb8c9Sopenharmony_ci            int32_t ret = deviceManager->OnDeviceP2POffline(deviceInfo);
694cb7eb8c9Sopenharmony_ci            LOGI("Close P2P Connection result %d", ret);
695cb7eb8c9Sopenharmony_ci            }).detach();
696cb7eb8c9Sopenharmony_ci    }
697cb7eb8c9Sopenharmony_ci    deviceManager->RemoveNetworkId(callingTokenId);
698cb7eb8c9Sopenharmony_ci    LOGI("Daemon::DfsListenerDeathRecipient OnremoteDied end");
699cb7eb8c9Sopenharmony_ci    return;
700cb7eb8c9Sopenharmony_ci}
701cb7eb8c9Sopenharmony_ci
702cb7eb8c9Sopenharmony_ciint32_t Daemon::PushAsset(int32_t userId,
703cb7eb8c9Sopenharmony_ci                          const sptr<AssetObj> &assetObj,
704cb7eb8c9Sopenharmony_ci                          const sptr<IAssetSendCallback> &sendCallback)
705cb7eb8c9Sopenharmony_ci{
706cb7eb8c9Sopenharmony_ci    LOGI("Daemon::PushAsset begin.");
707cb7eb8c9Sopenharmony_ci    if (assetObj == nullptr || sendCallback == nullptr) {
708cb7eb8c9Sopenharmony_ci        LOGE("param is nullptr.");
709cb7eb8c9Sopenharmony_ci        return E_NULLPTR;
710cb7eb8c9Sopenharmony_ci    }
711cb7eb8c9Sopenharmony_ci    auto taskId = assetObj->srcBundleName_ + assetObj->sessionId_;
712cb7eb8c9Sopenharmony_ci    if (taskId.empty()) {
713cb7eb8c9Sopenharmony_ci        LOGE("assetObj info is null.");
714cb7eb8c9Sopenharmony_ci        return E_NULLPTR;
715cb7eb8c9Sopenharmony_ci    }
716cb7eb8c9Sopenharmony_ci    AssetCallbackManager::GetInstance().AddSendCallback(taskId, sendCallback);
717cb7eb8c9Sopenharmony_ci    auto pushData = std::make_shared<PushAssetData>(userId, assetObj);
718cb7eb8c9Sopenharmony_ci    auto msgEvent = AppExecFwk::InnerEvent::Get(DEAMON_EXECUTE_PUSH_ASSET, pushData, 0);
719cb7eb8c9Sopenharmony_ci
720cb7eb8c9Sopenharmony_ci    std::lock_guard<std::mutex> lock(eventHandlerMutex_);
721cb7eb8c9Sopenharmony_ci    if (eventHandler_ == nullptr) {
722cb7eb8c9Sopenharmony_ci        LOGE("eventHandler has not find");
723cb7eb8c9Sopenharmony_ci        AssetCallbackManager::GetInstance().RemoveSendCallback(taskId);
724cb7eb8c9Sopenharmony_ci        return E_EVENT_HANDLER;
725cb7eb8c9Sopenharmony_ci    }
726cb7eb8c9Sopenharmony_ci    bool isSucc = eventHandler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE);
727cb7eb8c9Sopenharmony_ci    if (!isSucc) {
728cb7eb8c9Sopenharmony_ci        LOGE("Daemon event handler post push asset event fail.");
729cb7eb8c9Sopenharmony_ci        AssetCallbackManager::GetInstance().RemoveSendCallback(taskId);
730cb7eb8c9Sopenharmony_ci        return E_EVENT_HANDLER;
731cb7eb8c9Sopenharmony_ci    }
732cb7eb8c9Sopenharmony_ci    return E_OK;
733cb7eb8c9Sopenharmony_ci}
734cb7eb8c9Sopenharmony_ci
735cb7eb8c9Sopenharmony_ciint32_t Daemon::RegisterAssetCallback(const sptr<IAssetRecvCallback> &recvCallback)
736cb7eb8c9Sopenharmony_ci{
737cb7eb8c9Sopenharmony_ci    LOGI("Daemon::RegisterAssetCallback begin.");
738cb7eb8c9Sopenharmony_ci    if (recvCallback == nullptr) {
739cb7eb8c9Sopenharmony_ci        LOGE("recvCallback is nullptr.");
740cb7eb8c9Sopenharmony_ci        return E_NULLPTR;
741cb7eb8c9Sopenharmony_ci    }
742cb7eb8c9Sopenharmony_ci    AssetCallbackManager::GetInstance().AddRecvCallback(recvCallback);
743cb7eb8c9Sopenharmony_ci    return E_OK;
744cb7eb8c9Sopenharmony_ci}
745cb7eb8c9Sopenharmony_ci
746cb7eb8c9Sopenharmony_ciint32_t Daemon::UnRegisterAssetCallback(const sptr<IAssetRecvCallback> &recvCallback)
747cb7eb8c9Sopenharmony_ci{
748cb7eb8c9Sopenharmony_ci    LOGI("Daemon::UnRegisterAssetCallback begin.");
749cb7eb8c9Sopenharmony_ci    if (recvCallback == nullptr) {
750cb7eb8c9Sopenharmony_ci        LOGE("recvCallback is nullptr.");
751cb7eb8c9Sopenharmony_ci        return E_NULLPTR;
752cb7eb8c9Sopenharmony_ci    }
753cb7eb8c9Sopenharmony_ci    AssetCallbackManager::GetInstance().RemoveRecvCallback(recvCallback);
754cb7eb8c9Sopenharmony_ci    return E_OK;
755cb7eb8c9Sopenharmony_ci}
756cb7eb8c9Sopenharmony_ci
757cb7eb8c9Sopenharmony_civoid Daemon::StartEventHandler()
758cb7eb8c9Sopenharmony_ci{
759cb7eb8c9Sopenharmony_ci    LOGI("StartEventHandler begin");
760cb7eb8c9Sopenharmony_ci    if (daemonExecute_ == nullptr) {
761cb7eb8c9Sopenharmony_ci        daemonExecute_ = std::make_shared<DaemonExecute>();
762cb7eb8c9Sopenharmony_ci    }
763cb7eb8c9Sopenharmony_ci
764cb7eb8c9Sopenharmony_ci    std::lock_guard<std::mutex> lock(eventHandlerMutex_);
765cb7eb8c9Sopenharmony_ci    auto runer = AppExecFwk::EventRunner::Create(IDaemon::SERVICE_NAME.c_str());
766cb7eb8c9Sopenharmony_ci    eventHandler_ = std::make_shared<DaemonEventHandler>(runer, daemonExecute_);
767cb7eb8c9Sopenharmony_ci}
768cb7eb8c9Sopenharmony_ci} // namespace DistributedFile
769cb7eb8c9Sopenharmony_ci} // namespace Storage
770cb7eb8c9Sopenharmony_ci} // namespace OHOS