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 "service_proxy.h"
17
18 #include "iservice_registry.h"
19 #include "system_ability_definition.h"
20
21 #include "b_error/b_error.h"
22 #include "b_error/b_excep_utils.h"
23 #include "b_resources/b_constants.h"
24 #include "filemgmt_libhilog.h"
25 #include "svc_death_recipient.h"
26 #include "unique_fd.h"
27 #include "hitrace_meter.h"
28
29 namespace OHOS::FileManagement::Backup {
30 using namespace std;
31
Release()32 ErrCode ServiceProxy::Release()
33 {
34 HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
35 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
36 MessageParcel data;
37 if (!data.WriteInterfaceToken(GetDescriptor())) {
38 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
39 }
40
41 MessageParcel reply;
42 MessageOption option;
43 int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_RELSEASE_SESSION),
44 data, reply, option);
45 if (ret != NO_ERROR) {
46 string str = "Failed to send out the request because of " + to_string(ret);
47 return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
48 }
49 return reply.ReadInt32();
50 }
51
GetLocalCapabilitiesIncremental(const vector<BIncrementalData> & bundleNames)52 UniqueFd ServiceProxy::GetLocalCapabilitiesIncremental(const vector<BIncrementalData> &bundleNames)
53 {
54 HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
55 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
56 MessageParcel data;
57 if (!data.WriteInterfaceToken(GetDescriptor())) {
58 HILOGE("Failed to write descriptor");
59 return UniqueFd(-EPERM);
60 }
61
62 if (!WriteParcelableVector(bundleNames, data)) {
63 HILOGE("Failed to send the bundleNames");
64 return UniqueFd(-EPERM);
65 }
66
67 MessageParcel reply;
68 MessageOption option;
69 option.SetWaitTime(BConstants::IPC_MAX_WAIT_TIME);
70 int32_t ret = Remote()->SendRequest(
71 static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_GET_LOCAL_CAPABILITIES_INCREMENTAL), data, reply,
72 option);
73 if (ret != NO_ERROR) {
74 HILOGE("Received error %{public}d when doing IPC", ret);
75 return UniqueFd(-ret);
76 }
77 UniqueFd fd(reply.ReadFileDescriptor());
78 return UniqueFd(fd.Release());
79 }
80
GetAppLocalListAndDoIncrementalBackup()81 ErrCode ServiceProxy::GetAppLocalListAndDoIncrementalBackup()
82 {
83 HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
84 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
85 MessageParcel data;
86 if (!data.WriteInterfaceToken(GetDescriptor())) {
87 HILOGE("Failed to write descriptor");
88 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
89 }
90
91 MessageParcel reply;
92 MessageOption option;
93 option.SetWaitTime(BConstants::IPC_MAX_WAIT_TIME);
94 int32_t ret = Remote()->SendRequest(
95 static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_GET_APP_LOCAL_LIST_AND_DO_INCREMENTAL_BACKUP),
96 data, reply, option);
97 if (ret != NO_ERROR) {
98 HILOGE("Received error %{public}d when doing IPC", ret);
99 return BError(BError::Codes::SDK_INVAL_ARG, "Received error when doing IPC").GetCode();
100 }
101 return reply.ReadInt32();
102 }
103
InitIncrementalBackupSession(sptr<IServiceReverse> remote)104 ErrCode ServiceProxy::InitIncrementalBackupSession(sptr<IServiceReverse> remote)
105 {
106 HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
107 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "remote is nullptr");
108 MessageParcel data;
109 if (!data.WriteInterfaceToken(GetDescriptor())) {
110 HILOGE("Failed to write descriptor");
111 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
112 }
113
114 MessageParcel reply;
115 MessageOption option;
116
117 if (!remote) {
118 HILOGE("Empty reverse stub");
119 return BError(BError::Codes::SDK_INVAL_ARG, "Empty reverse stub").GetCode();
120 }
121 if (!data.WriteRemoteObject(remote->AsObject().GetRefPtr())) {
122 HILOGE("Failed to send the reverse stub");
123 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the reverse stub").GetCode();
124 }
125
126 int32_t ret = Remote()->SendRequest(
127 static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_INIT_INCREMENTAL_BACKUP_SESSION), data, reply, option);
128 if (ret != NO_ERROR) {
129 HILOGE("Received error %{public}d when doing IPC", ret);
130 return BError(BError::Codes::SDK_INVAL_ARG, "Received error when doing IPC").GetCode();
131 }
132 return reply.ReadInt32();
133 }
134
AppendBundlesIncrementalBackupSession(const vector<BIncrementalData> & bundlesToBackup)135 ErrCode ServiceProxy::AppendBundlesIncrementalBackupSession(const vector<BIncrementalData> &bundlesToBackup)
136 {
137 HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
138 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "remote is nullptr");
139 MessageParcel data;
140 if (!data.WriteInterfaceToken(GetDescriptor())) {
141 HILOGE("Failed to write descriptor");
142 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
143 }
144
145 if (!WriteParcelableVector(bundlesToBackup, data)) {
146 HILOGE("Failed to send the bundleNames");
147 return UniqueFd(-EPERM);
148 }
149
150 MessageParcel reply;
151 MessageOption option;
152
153 int32_t ret = Remote()->SendRequest(
154 static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_INCREMENTAL_BACKUP_SESSION), data,
155 reply, option);
156 if (ret != NO_ERROR) {
157 HILOGE("Received error %{public}d when doing IPC", ret);
158 return BError(BError::Codes::SDK_INVAL_ARG, "Received error when doing IPC").GetCode();
159 }
160 return reply.ReadInt32();
161 }
162
AppendBundlesIncrementalBackupSession(const vector<BIncrementalData> & bundlesToBackup,const std::vector<std::string> & infos)163 ErrCode ServiceProxy::AppendBundlesIncrementalBackupSession(const vector<BIncrementalData> &bundlesToBackup,
164 const std::vector<std::string> &infos)
165 {
166 HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
167 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "remote is nullptr");
168 MessageParcel data;
169 if (!data.WriteInterfaceToken(GetDescriptor())) {
170 HILOGE("Failed to write descriptor");
171 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
172 }
173
174 if (!WriteParcelableVector(bundlesToBackup, data)) {
175 HILOGE("Failed to send the bundleNames");
176 return UniqueFd(-EPERM);
177 }
178
179 if (!data.WriteStringVector(infos)) {
180 HILOGE("Failed to write infos");
181 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send bundleNames").GetCode();
182 }
183 MessageParcel reply;
184 MessageOption option;
185
186 int32_t ret = Remote()->SendRequest(
187 static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_INCREMENTAL_BACKUP_SESSION_DETAILS),
188 data, reply, option);
189 if (ret != NO_ERROR) {
190 HILOGE("Received error %{public}d when doing IPC", ret);
191 return BError(BError::Codes::SDK_INVAL_ARG, "Received error when doing IPC").GetCode();
192 }
193 return reply.ReadInt32();
194 }
195
PublishIncrementalFile(const BFileInfo & fileInfo)196 ErrCode ServiceProxy::PublishIncrementalFile(const BFileInfo &fileInfo)
197 {
198 HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
199 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
200 MessageParcel data;
201 if (!data.WriteInterfaceToken(GetDescriptor())) {
202 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
203 }
204
205 if (!data.WriteParcelable(&fileInfo)) {
206 HILOGE("Failed to send the fileInfo");
207 return -EPIPE;
208 }
209
210 MessageParcel reply;
211 MessageOption option;
212 int32_t ret = Remote()->SendRequest(
213 static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_PUBLISH_INCREMENTAL_FILE), data, reply, option);
214 if (ret != NO_ERROR) {
215 string str = "Failed to send out the request because of " + to_string(ret);
216 return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
217 }
218 return reply.ReadInt32();
219 }
220
PublishSAIncrementalFile(const BFileInfo & fileInfo,UniqueFd fd)221 ErrCode ServiceProxy::PublishSAIncrementalFile(const BFileInfo &fileInfo, UniqueFd fd)
222 {
223 HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
224 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
225 MessageParcel data;
226 if (!data.WriteInterfaceToken(GetDescriptor())) {
227 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
228 }
229
230 if (!data.WriteParcelable(&fileInfo)) {
231 HILOGE("Failed to send the fileInfo");
232 return -EPIPE;
233 }
234
235 if (!data.WriteFileDescriptor(fd)) {
236 HILOGE("Failed to send the fd");
237 return -EPIPE;
238 }
239
240 MessageParcel reply;
241 MessageOption option;
242 int32_t ret = Remote()->SendRequest(
243 static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_PUBLISH_SA_INCREMENTAL_FILE), data, reply, option);
244 if (ret != NO_ERROR) {
245 string str = "Failed to send out the request because of " + to_string(ret);
246 return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
247 }
248 return reply.ReadInt32();
249 }
250
AppIncrementalFileReady(const std::string & fileName,UniqueFd fd,UniqueFd manifestFd,int32_t errCode)251 ErrCode ServiceProxy::AppIncrementalFileReady(const std::string &fileName, UniqueFd fd, UniqueFd manifestFd,
252 int32_t errCode)
253 {
254 HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
255 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
256 MessageParcel data;
257 if (!data.WriteInterfaceToken(GetDescriptor())) {
258 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
259 }
260
261 if (!data.WriteString(fileName)) {
262 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the filename").GetCode();
263 }
264 bool fdFlag = (fd < 0 || manifestFd < 0) ? false : true;
265 data.WriteBool(fdFlag);
266 if (fdFlag == true && !data.WriteFileDescriptor(fd)) {
267 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the fd").GetCode();
268 }
269 if (fdFlag == true && !data.WriteFileDescriptor(manifestFd)) {
270 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the fd").GetCode();
271 }
272 if (!data.WriteInt32(errCode)) {
273 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the errCode").GetCode();
274 }
275
276 MessageParcel reply;
277 MessageOption option;
278 int32_t ret = Remote()->SendRequest(
279 static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APP_INCREMENTAL_FILE_READY), data, reply, option);
280 if (ret != NO_ERROR) {
281 string str = "Failed to send out the request because of " + to_string(ret);
282 return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
283 }
284 return reply.ReadInt32();
285 }
286
AppIncrementalDone(ErrCode errCode)287 ErrCode ServiceProxy::AppIncrementalDone(ErrCode errCode)
288 {
289 HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
290 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
291 MessageParcel data;
292 if (!data.WriteInterfaceToken(GetDescriptor())) {
293 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
294 }
295
296 if (!data.WriteInt32(errCode)) {
297 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the errCode").GetCode();
298 }
299
300 MessageParcel reply;
301 MessageOption option;
302 int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APP_INCREMENTAL_DONE),
303 data, reply, option);
304 if (ret != NO_ERROR) {
305 string str = "Failed to send out the request because of " + to_string(ret);
306 return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
307 }
308 return reply.ReadInt32();
309 }
310
GetIncrementalFileHandle(const std::string & bundleName,const std::string & fileName)311 ErrCode ServiceProxy::GetIncrementalFileHandle(const std::string &bundleName, const std::string &fileName)
312 {
313 HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
314 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
315 MessageParcel data;
316 if (!data.WriteInterfaceToken(GetDescriptor())) {
317 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
318 }
319
320 if (!data.WriteString(bundleName)) {
321 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the bundleName").GetCode();
322 }
323 if (!data.WriteString(fileName)) {
324 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the fileName").GetCode();
325 }
326
327 MessageParcel reply;
328 MessageOption option;
329 int32_t ret = Remote()->SendRequest(
330 static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_GET_INCREMENTAL_FILE_NAME), data, reply, option);
331 if (ret != NO_ERROR) {
332 string str = "Failed to send out the request because of " + to_string(ret);
333 return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
334 }
335 return ret;
336 }
337
338 template <typename T>
WriteParcelableVector(const std::vector<T> & parcelableVector,Parcel & data)339 bool ServiceProxy::WriteParcelableVector(const std::vector<T> &parcelableVector, Parcel &data)
340 {
341 HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
342 if (!data.WriteUint32(parcelableVector.size())) {
343 HILOGE("failed to WriteInt32 for parcelableVector.size()");
344 return false;
345 }
346
347 for (const auto &parcelable : parcelableVector) {
348 if (!data.WriteParcelable(&parcelable)) {
349 HILOGE("failed to WriteParcelable for parcelable");
350 return false;
351 }
352 }
353
354 return true;
355 }
356 } // namespace OHOS::FileManagement::Backup