1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "task_signal.h"
17
18 #include "distributed_file_daemon_manager.h"
19 #include "filemgmt_libhilog.h"
20 #include "copy/file_copy_manager.h"
21
22 namespace OHOS {
23 namespace DistributedFS {
24 namespace ModuleTaskSignal {
25 using namespace FileManagement;
Cancel()26 int32_t TaskSignal::Cancel()
27 {
28 HILOGD("TaskSignal Cancel in.");
29 if (dfsCopyTask_.load()) {
30 auto ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Cancel(srcUri_, dstUri_, true);
31 if (ret != 0) {
32 HILOGE("Cancel failed, ret = %{public}d", ret);
33 return ret;
34 }
35 OnCancel();
36 return ret;
37 }
38 if (remoteTask_.load()) {
39 int32_t ret = 0;
40 if (sessionName_.empty()) {
41 ret = Storage::DistributedFile::FileCopyManager::GetInstance()->Cancel(srcUri_, dstUri_, true);
42 } else {
43 ret = Storage::DistributedFile::DistributedFileDaemonManager::GetInstance().
44 CancelCopyTask(sessionName_);
45 }
46 HILOGD("taskSignal.cancel sessionName = %{public}s", sessionName_.c_str());
47 if (ret != 0) {
48 HILOGI("CancelCopyTask failed, ret = %{public}d", ret);
49 return ret;
50 }
51 OnCancel();
52 return ret;
53 }
54 needCancel_.store(true);
55 return 0;
56 }
57
IsCanceled()58 bool TaskSignal::IsCanceled()
59 {
60 return needCancel_.load() || remoteTask_.load();
61 }
62
SetTaskSignalListener(TaskSignalListener * signalListener)63 void TaskSignal::SetTaskSignalListener(TaskSignalListener *signalListener)
64 {
65 if (signalListener_ == nullptr) {
66 signalListener_ = std::move(signalListener);
67 }
68 }
69
OnCancel()70 void TaskSignal::OnCancel()
71 {
72 if (signalListener_ != nullptr) {
73 signalListener_->OnCancel();
74 }
75 }
76
CheckCancelIfNeed(const std::string & path)77 bool TaskSignal::CheckCancelIfNeed(const std::string &path)
78 {
79 if (!needCancel_.load()) {
80 return false;
81 }
82 OnCancel();
83 return true;
84 }
85
MarkRemoteTask()86 void TaskSignal::MarkRemoteTask()
87 {
88 remoteTask_.store(true);
89 }
90
MarkDfsTask()91 void TaskSignal::MarkDfsTask()
92 {
93 dfsCopyTask_.store(true);
94 }
95
SetFileInfoOfRemoteTask(const std::string & sessionName,const std::string & filePath)96 void TaskSignal::SetFileInfoOfRemoteTask(const std::string &sessionName, const std::string &filePath)
97 {
98 HILOGD("SetFileInfoOfRemoteTask sessionName = %{public}s", sessionName.c_str());
99 sessionName_ = sessionName;
100 filePath_ = filePath;
101 }
102
SetCopyTaskUri(const std::string & srcUri,const std::string & dstUri)103 void TaskSignal::SetCopyTaskUri(const std::string &srcUri, const std::string &dstUri)
104 {
105 srcUri_ = srcUri;
106 dstUri_ = dstUri;
107 }
108 } // namespace ModuleTaskSignal
109 } // namespace DistributedFS
110 } // namespace OHOS