1 /*
2 * Copyright (c) 2024 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 "b_incremental_restore_session.h"
17
18 #include "b_error/b_error.h"
19 #include "b_radar/b_radar.h"
20 #include "filemgmt_libhilog.h"
21 #include "service_proxy.h"
22 #include "service_reverse.h"
23
24 namespace OHOS::FileManagement::Backup {
25 using namespace std;
26
~BIncrementalRestoreSession()27 BIncrementalRestoreSession::~BIncrementalRestoreSession()
28 {
29 if (!deathRecipient_) {
30 HILOGI("Death Recipient is nullptr");
31 return;
32 }
33 auto proxy = ServiceProxy::GetServiceProxyPointer();
34 if (proxy == nullptr) {
35 return;
36 }
37 auto remoteObject = proxy->AsObject();
38 if (remoteObject != nullptr) {
39 remoteObject->RemoveDeathRecipient(deathRecipient_);
40 }
41 deathRecipient_ = nullptr;
42 }
43
Init(Callbacks callbacks)44 unique_ptr<BIncrementalRestoreSession> BIncrementalRestoreSession::Init(Callbacks callbacks)
45 {
46 try {
47 HILOGI("Init IncrementalRestoreSession Begin");
48 auto restore = make_unique<BIncrementalRestoreSession>();
49 ServiceProxy::InvaildInstance();
50 auto proxy = ServiceProxy::GetInstance();
51 if (proxy == nullptr) {
52 HILOGI("Failed to get backup service");
53 return nullptr;
54 }
55 int32_t res = proxy->InitRestoreSession(sptr(new ServiceReverse(callbacks)));
56 if (res != ERR_OK) {
57 HILOGE("Failed to Restore because of %{public}d", res);
58 AppRadar::Info info ("", "", "create restore session failed");
59 AppRadar::GetInstance().RecordRestoreFuncRes(info, "BIncrementalRestoreSession::Init",
60 AppRadar::GetInstance().GetUserId(), BizStageRestore::BIZ_STAGE_CREATE_RESTORE_SESSION_FAIL, res);
61 return nullptr;
62 }
63
64 restore->RegisterBackupServiceDied(callbacks.onBackupServiceDied);
65 return restore;
66 } catch (const exception &e) {
67 HILOGE("Failed to Restore because of %{public}s", e.what());
68 }
69 return nullptr;
70 }
71
Init(Callbacks callbacks,std::string & errMsg,ErrCode & errCode)72 unique_ptr<BIncrementalRestoreSession> BIncrementalRestoreSession::Init(Callbacks callbacks,
73 std::string &errMsg, ErrCode &errCode)
74 {
75 try {
76 HILOGI("Init IncrementalRestoreSession Begin");
77 auto restore = make_unique<BIncrementalRestoreSession>();
78 ServiceProxy::InvaildInstance();
79 auto proxy = ServiceProxy::GetInstance();
80 if (proxy == nullptr) {
81 errMsg = "Failed to get backup service";
82 errCode = BError(BError::Codes::SDK_BROKEN_IPC);
83 HILOGE("Init IncrementalRestoreSession failed, %{public}s", errMsg.c_str());
84 return nullptr;
85 }
86 errCode = proxy->InitRestoreSession(sptr(new ServiceReverse(callbacks)), errMsg);
87 if (errCode != ERR_OK) {
88 HILOGE("Failed to Restore because of %{public}d", errCode);
89 AppRadar::Info info ("", "", "create restore session failed");
90 AppRadar::GetInstance().RecordRestoreFuncRes(info, "BIncrementalRestoreSession::Init",
91 AppRadar::GetInstance().GetUserId(), BizStageRestore::BIZ_STAGE_CREATE_RESTORE_SESSION_FAIL, errCode);
92 return nullptr;
93 }
94
95 restore->RegisterBackupServiceDied(callbacks.onBackupServiceDied);
96 return restore;
97 } catch (const exception &e) {
98 HILOGE("Failed to Restore because of %{public}s", e.what());
99 errCode = BError(BError::Codes::SDK_INVAL_ARG);
100 }
101 return nullptr;
102 }
103
GetLocalCapabilities()104 UniqueFd BIncrementalRestoreSession::GetLocalCapabilities()
105 {
106 HILOGI("GetLocalCapabilities begin");
107 auto proxy = ServiceProxy::GetInstance();
108 if (proxy == nullptr) {
109 HILOGE("Failed to get backup service");
110 return UniqueFd(-EPERM);
111 }
112 UniqueFd fd = proxy->GetLocalCapabilitiesForBundleInfos();
113 if (fd < 0) {
114 HILOGE("Failed to get local capabilities for bundleinfos");
115 return UniqueFd(-EPERM);
116 }
117 return fd;
118 }
119
PublishFile(BFileInfo fileInfo)120 ErrCode BIncrementalRestoreSession::PublishFile(BFileInfo fileInfo)
121 {
122 auto proxy = ServiceProxy::GetInstance();
123 if (proxy == nullptr) {
124 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
125 }
126 return proxy->PublishIncrementalFile(fileInfo);
127 }
128
PublishSAFile(BFileInfo fileInfo,UniqueFd fd)129 ErrCode BIncrementalRestoreSession::PublishSAFile(BFileInfo fileInfo, UniqueFd fd)
130 {
131 auto proxy = ServiceProxy::GetInstance();
132 if (proxy == nullptr) {
133 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
134 }
135 return proxy->PublishSAIncrementalFile(fileInfo, move(fd));
136 }
137
GetFileHandle(const string & bundleName,const string & fileName)138 ErrCode BIncrementalRestoreSession::GetFileHandle(const string &bundleName, const string &fileName)
139 {
140 auto proxy = ServiceProxy::GetInstance();
141 if (proxy == nullptr) {
142 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
143 }
144
145 return proxy->GetIncrementalFileHandle(bundleName, fileName);
146 }
147
AppendBundles(UniqueFd remoteCap,vector<BundleName> bundlesToRestore)148 ErrCode BIncrementalRestoreSession::AppendBundles(UniqueFd remoteCap, vector<BundleName> bundlesToRestore)
149 {
150 auto proxy = ServiceProxy::GetInstance();
151 if (proxy == nullptr) {
152 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
153 }
154 ErrCode res = proxy->AppendBundlesRestoreSession(move(remoteCap), bundlesToRestore);
155 if (res != ERR_OK) {
156 std::string ss;
157 for (const auto &bundle : bundlesToRestore) {
158 ss += bundle + ", ";
159 }
160 AppRadar::Info info(ss.c_str(), "", "");
161 AppRadar::GetInstance().RecordRestoreFuncRes(info, "BIncrementalRestoreSession::AppendBundles",
162 AppRadar::GetInstance().GetUserId(), BizStageRestore::BIZ_STAGE_APPEND_BUNDLES_FAIL, res);
163 }
164 return res;
165 }
166
AppendBundles(UniqueFd remoteCap,vector<BundleName> bundlesToRestore,std::vector<std::string> detailInfos)167 ErrCode BIncrementalRestoreSession::AppendBundles(UniqueFd remoteCap, vector<BundleName> bundlesToRestore,
168 std::vector<std::string> detailInfos)
169 {
170 auto proxy = ServiceProxy::GetInstance();
171 if (proxy == nullptr) {
172 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
173 }
174 ErrCode res = proxy->AppendBundlesRestoreSession(move(remoteCap), bundlesToRestore, detailInfos);
175 if (res != ERR_OK) {
176 std::string ss;
177 for (const auto &bundle : bundlesToRestore) {
178 ss += bundle + ", ";
179 }
180 AppRadar::Info info(ss.c_str(), "", "AppendBundles with infos");
181 AppRadar::GetInstance().RecordRestoreFuncRes(info, "BIncrementalRestoreSession::AppendBundles",
182 AppRadar::GetInstance().GetUserId(), BizStageRestore::BIZ_STAGE_APPEND_BUNDLES_FAIL, res);
183 }
184 return res;
185 }
186
Release()187 ErrCode BIncrementalRestoreSession::Release()
188 {
189 auto proxy = ServiceProxy::GetInstance();
190 if (proxy == nullptr) {
191 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
192 }
193
194 return proxy->Release();
195 }
196
RegisterBackupServiceDied(function<void ()> functor)197 void BIncrementalRestoreSession::RegisterBackupServiceDied(function<void()> functor)
198 {
199 auto proxy = ServiceProxy::GetInstance();
200 if (proxy == nullptr || !functor) {
201 return;
202 }
203 auto remoteObj = proxy->AsObject();
204 if (!remoteObj) {
205 throw BError(BError::Codes::SA_BROKEN_IPC, "Proxy's remote object can't be nullptr");
206 }
207
208 auto callback = [functor](const wptr<IRemoteObject> &obj) {
209 HILOGI("Backup service died");
210 ServiceProxy::InvaildInstance();
211 functor();
212 };
213 deathRecipient_ = sptr(new SvcDeathRecipient(callback));
214 remoteObj->AddDeathRecipient(deathRecipient_);
215 }
216
Cancel(std::string bundleName)217 ErrCode BIncrementalRestoreSession::Cancel(std::string bundleName)
218 {
219 ErrCode result = BError::BackupErrorCode::E_CANCEL_UNSTARTED_TASK;
220 auto proxy = ServiceProxy::GetInstance();
221 if (proxy == nullptr) {
222 HILOGE("Called Cancel, failed to get proxy.");
223 return result;
224 }
225
226 ErrCode errCode = proxy->Cancel(bundleName, result);
227 if (errCode != 0) {
228 HILOGE("proxy->Cancel failed, errCode:%{public}d.", errCode);
229 return result;
230 }
231 return result;
232 }
233 } // namespace OHOS::FileManagement::Backup