• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 /*
17  * 注意:
18  *     - 注意点1:本文件原则上只处理与IPC模块的IO,具体业务逻辑实现在service.cpp19  *     - 注意点2:所有调用开头处打印 Begin 字样,通过BError返回正常结果/错误码,这是出于防抵赖的目的
20  */
21 
22 #include "module_ipc/service_stub.h"
23 
24 #include <sstream>
25 
26 #include "b_error/b_error.h"
27 #include "b_error/b_excep_utils.h"
28 #include "b_resources/b_constants.h"
29 #include "filemgmt_libhilog.h"
30 #include "module_ipc/service_reverse_proxy.h"
31 
32 namespace OHOS::FileManagement::Backup {
33 using namespace std;
34 const int INVALID_FD = -1;
35 
36 
ServiceStubSupplement()37 void ServiceStub::ServiceStubSupplement()
38 {
39     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_GET_BACKUP_DATA_SIZE)] =
40         &ServiceStub::CmdGetBackupDataSize;
41     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_UPDATE_TIMER)] =
42         &ServiceStub::CmdUpdateTimer;
43     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_UPDATE_SENDRATE)] =
44         &ServiceStub::CmdUpdateSendRate;
45     opToInterfaceMap_[static_cast<uint32_t>(
46         IServiceInterfaceCode::SERVICE_CMD_GET_APP_LOCAL_LIST_AND_DO_INCREMENTAL_BACKUP)] =
47         &ServiceStub::CmdGetAppLocalListAndDoIncrementalBackup;
48     opToInterfaceMap_[static_cast<uint32_t>(
49         IServiceInterfaceCode::SERVICE_CMD_REPORT_APP_PROCESS_INFO)] =
50         &ServiceStub::CmdReportAppProcessInfo;
51     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_START_EXT_TIMER)] =
52         &ServiceStub::CmdStartExtTimer;
53     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_START_FWK_TIMER)] =
54         &ServiceStub::CmdStartFwkTimer;
55     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_CANCEL_BUNDLE)] =
56         &ServiceStub::CmdCancel;
57     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_INIT_BACKUP_SESSION_MSG)] =
58         &ServiceStub::CmdInitBackupSessionMsg;
59     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_INIT_INCREMENTAL_BACKUP_SESSION_MSG)] =
60         &ServiceStub::CmdInitIncrementalBackupSessionMsg;
61     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_INIT_RESTORE_SESSION_MSG)] =
62         &ServiceStub::CmdInitRestoreSessionMsg;
63     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_STOP_EXT_TIMER)] =
64         &ServiceStub::CmdStopExtTimer;
65     opToInterfaceMap_[static_cast<uint32_t>(
66         IServiceInterfaceCode::SERVICE_CMD_GET_LOCAL_CAPABILITIES_FOR_BUNDLE_INFOS)] =
67         &ServiceStub::CmdGetLocalCapabilitiesForBdInfos;
68 }
69 
ServiceStubSuppAppendBundles()70 void ServiceStub::ServiceStubSuppAppendBundles()
71 {
72     opToInterfaceMap_[static_cast<uint32_t>(
73         IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_INCREMENTAL_BACKUP_SESSION)] =
74         &ServiceStub::CmdAppendBundlesIncrementalBackupSession;
75     opToInterfaceMap_[static_cast<uint32_t>(
76         IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_INCREMENTAL_BACKUP_SESSION_DETAILS)] =
77         &ServiceStub::CmdAppendBundlesDetailsIncrementalBackupSession;
78 }
79 
ServiceStub()80 ServiceStub::ServiceStub()
81 {
82     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_INIT_RESTORE_SESSION)] =
83         &ServiceStub::CmdInitRestoreSession;
84     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_INIT_BACKUP_SESSION)] =
85         &ServiceStub::CmdInitBackupSession;
86     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_GET_LOCAL_CAPABILITIES)] =
87         &ServiceStub::CmdGetLocalCapabilities;
88     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_PUBLISH_FILE)] =
89         &ServiceStub::CmdPublishFile;
90     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APP_FILE_READY)] =
91         &ServiceStub::CmdAppFileReady;
92     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APP_DONE)] = &ServiceStub::CmdAppDone;
93     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_RESULT_REPORT)] =
94         &ServiceStub::CmdResultReport;
95     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_START)] = &ServiceStub::CmdStart;
96     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_GET_FILE_NAME)] =
97         &ServiceStub::CmdGetFileHandle;
98     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_RESTORE_SESSION)] =
99         &ServiceStub::CmdAppendBundlesRestoreSession;
100     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_RESTORE_SESSION_DETAIL)] =
101         &ServiceStub::CmdAppendBundlesDetailsRestoreSession;
102     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_BACKUP_SESSION)] =
103         &ServiceStub::CmdAppendBundlesBackupSession;
104     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_BACKUP_SESSION_DETAILS)] =
105         &ServiceStub::CmdAppendBundlesDetailsBackupSession;
106     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_FINISH)] = &ServiceStub::CmdFinish;
107     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_RELSEASE_SESSION)] =
108         &ServiceStub::CmdRelease;
109     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_GET_LOCAL_CAPABILITIES_INCREMENTAL)] =
110         &ServiceStub::CmdGetLocalCapabilitiesIncremental;
111     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_INIT_INCREMENTAL_BACKUP_SESSION)] =
112         &ServiceStub::CmdInitIncrementalBackupSession;
113     ServiceStubSuppAppendBundles();
114     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_PUBLISH_INCREMENTAL_FILE)] =
115         &ServiceStub::CmdPublishIncrementalFile;
116     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_PUBLISH_SA_INCREMENTAL_FILE)] =
117         &ServiceStub::CmdPublishSAIncrementalFile;
118     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APP_INCREMENTAL_FILE_READY)] =
119         &ServiceStub::CmdAppIncrementalFileReady;
120     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APP_INCREMENTAL_DONE)] =
121         &ServiceStub::CmdAppIncrementalDone;
122     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_GET_INCREMENTAL_FILE_NAME)] =
123         &ServiceStub::CmdGetIncrementalFileHandle;
124     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_GET_BACKUP_INFO)] =
125         &ServiceStub::CmdGetBackupInfo;
126     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_REFRESH_DATA_SIZE)] =
127         &ServiceStub::CmdRefreshDataSize;
128     ServiceStubSupplement();
129 }
130 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)131 int32_t ServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
132 {
133     const std::u16string descriptor = ServiceStub::GetDescriptor();
134     const std::u16string remoteDescriptor = data.ReadInterfaceToken();
135     if (descriptor != remoteDescriptor) {
136         return BError(BError::Codes::SA_INVAL_ARG, "Invalid remote descriptor");
137     }
138 
139     HILOGD("Begin to call procedure indexed %{public}u", code);
140     auto interfaceIndex = opToInterfaceMap_.find(code);
141     if (interfaceIndex == opToInterfaceMap_.end() || !interfaceIndex->second) {
142         HILOGE("Cannot response request %{public}d : unknown procedure", code);
143         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
144     }
145 
146     return BExcepUltils::ExceptionCatcherLocked(
147         [&]() { return ErrCode((this->*(interfaceIndex->second))(data, reply)); });
148 }
149 
CmdInitRestoreSession(MessageParcel & data,MessageParcel & reply)150 int32_t ServiceStub::CmdInitRestoreSession(MessageParcel &data, MessageParcel &reply)
151 {
152     auto remote = data.ReadRemoteObject();
153     if (!remote) {
154         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive the reverse stub");
155     }
156     auto iremote = iface_cast<IServiceReverse>(remote);
157     if (!iremote) {
158         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive the reverse stub");
159     }
160 
161     int32_t res = InitRestoreSession(iremote);
162     if (!reply.WriteInt32(res)) {
163         stringstream ss;
164         ss << "Failed to send the result " << res;
165         return BError(BError::Codes::SA_BROKEN_IPC, ss.str());
166     }
167     return BError(BError::Codes::OK);
168 }
169 
CmdInitRestoreSessionMsg(MessageParcel & data,MessageParcel & reply)170 int32_t ServiceStub::CmdInitRestoreSessionMsg(MessageParcel &data, MessageParcel &reply)
171 {
172     auto remote = data.ReadRemoteObject();
173     std::string errMsg;
174     if (!remote) {
175         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive the stub");
176     }
177     auto iremote = iface_cast<IServiceReverse>(remote);
178     if (!iremote) {
179         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive the reverse stub");
180     }
181     int32_t res = InitRestoreSession(iremote, errMsg);
182     if (!reply.WriteString(errMsg)) {
183         return BError(BError::Codes::SA_BROKEN_IPC, "Failed to send the errMsg");
184     }
185     if (!reply.WriteInt32(res)) {
186         stringstream ss;
187         ss << "Failed to send the result " << res;
188         return BError(BError::Codes::SA_BROKEN_IPC, ss.str());
189     }
190     return BError(BError::Codes::OK);
191 }
192 
CmdInitBackupSession(MessageParcel & data,MessageParcel & reply)193 int32_t ServiceStub::CmdInitBackupSession(MessageParcel &data, MessageParcel &reply)
194 {
195     auto remote = data.ReadRemoteObject();
196     if (!remote) {
197         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive the reverse stub");
198     }
199     auto iremote = iface_cast<IServiceReverse>(remote);
200     if (!iremote) {
201         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive the reverse stub");
202     }
203 
204     int res = InitBackupSession(iremote);
205     if (!reply.WriteInt32(res)) {
206         stringstream ss;
207         ss << "Failed to send the result " << res;
208         return BError(BError::Codes::SA_BROKEN_IPC, ss.str());
209     }
210     return BError(BError::Codes::OK);
211 }
212 
CmdInitBackupSessionMsg(MessageParcel & data,MessageParcel & reply)213 int32_t ServiceStub::CmdInitBackupSessionMsg(MessageParcel &data, MessageParcel &reply)
214 {
215     auto remote = data.ReadRemoteObject();
216     std::string errMsg;
217     if (!remote) {
218         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive the reverse stub");
219     }
220     auto iRemote = iface_cast<IServiceReverse>(remote);
221     if (!iRemote) {
222         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive the reverse stub");
223     }
224     int res = InitBackupSession(iRemote, errMsg);
225     if (!reply.WriteString(errMsg)) {
226         return BError(BError::Codes::SA_BROKEN_IPC, "Failed to send the errMsg");
227     }
228     if (!reply.WriteInt32(res)) {
229         stringstream ss;
230         ss << "Failed to send the result " << res;
231         return BError(BError::Codes::SA_BROKEN_IPC, ss.str());
232     }
233     return BError(BError::Codes::OK);
234 }
235 
CmdStart(MessageParcel & data,MessageParcel & reply)236 int32_t ServiceStub::CmdStart(MessageParcel &data, MessageParcel &reply)
237 {
238     int res = Start();
239     if (!reply.WriteInt32(res)) {
240         stringstream ss;
241         ss << "Failed to send the result " << res;
242         return BError(BError::Codes::SA_BROKEN_IPC, ss.str());
243     }
244     return BError(BError::Codes::OK);
245 }
246 
CmdGetLocalCapabilities(MessageParcel & data,MessageParcel & reply)247 int32_t ServiceStub::CmdGetLocalCapabilities(MessageParcel &data, MessageParcel &reply)
248 {
249     UniqueFd fd(GetLocalCapabilities());
250     if (!reply.WriteFileDescriptor(fd)) {
251         return BError(BError::Codes::SA_BROKEN_IPC, "Failed to send out the file");
252     }
253     return BError(BError::Codes::OK);
254 }
255 
CmdPublishFile(MessageParcel & data,MessageParcel & reply)256 int32_t ServiceStub::CmdPublishFile(MessageParcel &data, MessageParcel &reply)
257 {
258     unique_ptr<BFileInfo> fileInfo(data.ReadParcelable<BFileInfo>());
259     if (!fileInfo) {
260         return BError(BError::Codes::SA_BROKEN_IPC, "Failed to receive fileInfo");
261     }
262     int res = PublishFile(*fileInfo);
263     if (!reply.WriteInt32(res)) {
264         stringstream ss;
265         ss << "Failed to send the result " << res;
266         return BError(BError::Codes::SA_BROKEN_IPC, ss.str());
267     }
268     return BError(BError::Codes::OK);
269 }
270 
CmdAppFileReady(MessageParcel & data,MessageParcel & reply)271 int32_t ServiceStub::CmdAppFileReady(MessageParcel &data, MessageParcel &reply)
272 {
273     string fileName;
274     if (!data.ReadString(fileName)) {
275         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive fileName");
276     }
277     bool fdFlag = data.ReadBool();
278     UniqueFd fd = UniqueFd(INVALID_FD);
279     if (fdFlag == true) {
280         fd = UniqueFd(data.ReadFileDescriptor());
281         if (fd < 0) {
282             return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive fd");
283         }
284     }
285     int32_t errCode = data.ReadInt32();
286 
287     int res = AppFileReady(fileName, move(fd), errCode);
288     if (!reply.WriteInt32(res)) {
289         stringstream ss;
290         ss << "Failed to send the result " << res;
291         return BError(BError::Codes::SA_BROKEN_IPC, ss.str());
292     }
293     return BError(BError::Codes::OK);
294 }
295 
CmdAppDone(MessageParcel & data,MessageParcel & reply)296 int32_t ServiceStub::CmdAppDone(MessageParcel &data, MessageParcel &reply)
297 {
298     ErrCode errCode;
299     if (!data.ReadInt32(errCode)) {
300         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive errCode");
301     }
302     int res = AppDone(errCode);
303     if (!reply.WriteInt32(res)) {
304         stringstream ss;
305         ss << "Failed to send the result " << res;
306         return BError(BError::Codes::SA_BROKEN_IPC, ss.str());
307     }
308     return BError(BError::Codes::OK);
309 }
310 
CmdResultReport(MessageParcel & data,MessageParcel & reply)311 int32_t ServiceStub::CmdResultReport(MessageParcel &data, MessageParcel &reply)
312 {
313     std::string restoreRetInfo;
314     int32_t scenario;
315     ErrCode errCode;
316     if (!data.ReadString(restoreRetInfo)) {
317         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive restoreRetInfo");
318     }
319     if (!data.ReadInt32(scenario)) {
320         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive scenario");
321     }
322     BackupRestoreScenario secenrioInfo = static_cast<BackupRestoreScenario>(scenario);
323     if (!data.ReadInt32(errCode)) {
324         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive errCode");
325     }
326     int res = ServiceResultReport(restoreRetInfo, secenrioInfo, errCode);
327     if (!reply.WriteInt32(res)) {
328         stringstream ss;
329         ss << "Failed to send the result " << res;
330         return BError(BError::Codes::SA_BROKEN_IPC, ss.str());
331     }
332     return BError(BError::Codes::OK);
333 }
334 
CmdGetFileHandle(MessageParcel & data,MessageParcel & reply)335 int32_t ServiceStub::CmdGetFileHandle(MessageParcel &data, MessageParcel &reply)
336 {
337     string bundleName;
338     if (!data.ReadString(bundleName)) {
339         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bundleName").GetCode();
340     }
341     string fileName;
342     if (!data.ReadString(fileName)) {
343         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive fileName").GetCode();
344     }
345 
346     return GetFileHandle(bundleName, fileName);
347 }
348 
CmdAppendBundlesRestoreSession(MessageParcel & data,MessageParcel & reply)349 int32_t ServiceStub::CmdAppendBundlesRestoreSession(MessageParcel &data, MessageParcel &reply)
350 {
351     UniqueFd fd(data.ReadFileDescriptor());
352     if (fd < 0) {
353         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive fd");
354     }
355 
356     vector<string> bundleNames;
357     if (!data.ReadStringVector(&bundleNames)) {
358         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bundleNames");
359     }
360     int32_t type;
361     if (!data.ReadInt32(type)) {
362         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive restoreType");
363     }
364     RestoreTypeEnum restoreType = static_cast<RestoreTypeEnum>(type);
365     int32_t userId;
366     if (!data.ReadInt32(userId)) {
367         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive userId");
368     }
369 
370     int res = AppendBundlesRestoreSession(move(fd), bundleNames, restoreType, userId);
371     if (!reply.WriteInt32(res)) {
372         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to send the result ") + to_string(res));
373     }
374     return BError(BError::Codes::OK);
375 }
376 
CmdAppendBundlesDetailsRestoreSession(MessageParcel & data,MessageParcel & reply)377 int32_t ServiceStub::CmdAppendBundlesDetailsRestoreSession(MessageParcel &data, MessageParcel &reply)
378 {
379     UniqueFd fd(data.ReadFileDescriptor());
380     if (fd < 0) {
381         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive fd");
382     }
383 
384     vector<string> bundleNames;
385     if (!data.ReadStringVector(&bundleNames)) {
386         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bundleNames");
387     }
388     vector<string> detailInfos;
389     if (!data.ReadStringVector(&detailInfos)) {
390         HILOGE("Failed to receive bundle detailInfos");
391         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive detailInfos");
392     }
393     int32_t type;
394     if (!data.ReadInt32(type)) {
395         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive restoreType");
396     }
397     RestoreTypeEnum restoreType = static_cast<RestoreTypeEnum>(type);
398     int32_t userId;
399     if (!data.ReadInt32(userId)) {
400         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive userId");
401     }
402 
403     int res = AppendBundlesRestoreSession(move(fd), bundleNames, detailInfos, restoreType, userId);
404     if (!reply.WriteInt32(res)) {
405         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to send the result ") + to_string(res));
406     }
407     return BError(BError::Codes::OK);
408 }
409 
CmdAppendBundlesBackupSession(MessageParcel & data,MessageParcel & reply)410 int32_t ServiceStub::CmdAppendBundlesBackupSession(MessageParcel &data, MessageParcel &reply)
411 {
412     vector<string> bundleNames;
413     if (!data.ReadStringVector(&bundleNames)) {
414         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bundleNames");
415     }
416 
417     int32_t res = AppendBundlesBackupSession(bundleNames);
418     if (!reply.WriteInt32(res)) {
419         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to send the result ") + to_string(res));
420     }
421     return BError(BError::Codes::OK);
422 }
423 
CmdAppendBundlesDetailsBackupSession(MessageParcel & data,MessageParcel & reply)424 int32_t ServiceStub::CmdAppendBundlesDetailsBackupSession(MessageParcel &data, MessageParcel &reply)
425 {
426     vector<string> bundleNames;
427     if (!data.ReadStringVector(&bundleNames)) {
428         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bundleNames");
429     }
430 
431     vector<string> detailInfos;
432     if (!data.ReadStringVector(&detailInfos)) {
433         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive detailInfos");
434     }
435 
436     int32_t res = AppendBundlesDetailsBackupSession(bundleNames, detailInfos);
437     if (!reply.WriteInt32(res)) {
438         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to send the result ") + to_string(res));
439     }
440     return BError(BError::Codes::OK);
441 }
442 
CmdFinish(MessageParcel & data,MessageParcel & reply)443 int32_t ServiceStub::CmdFinish(MessageParcel &data, MessageParcel &reply)
444 {
445     int res = Finish();
446     if (!reply.WriteInt32(res)) {
447         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to send the result ") + to_string(res));
448     }
449     return BError(BError::Codes::OK);
450 }
451 
CmdGetBackupInfo(MessageParcel & data,MessageParcel & reply)452 int32_t ServiceStub::CmdGetBackupInfo(MessageParcel &data, MessageParcel &reply)
453 {
454     HILOGI("ServiceStub::CmdGetBackupInfo Begin.");
455     int ret = ERR_OK;
456     string bundleName;
457     if (!data.ReadString(bundleName)) {
458         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to recive bundleName"));
459     }
460     string result;
461     ret = GetBackupInfo(bundleName, result);
462     if (ret != ERR_OK) {
463         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to call GetBackupInfo"));
464     }
465     if (!reply.WriteString(result)) {
466         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to write result"));
467     }
468     HILOGI("ServiceStub::CmdGetBackupInfo end.");
469     return BError(BError::Codes::OK);
470 }
471 
CmdUpdateTimer(MessageParcel & data,MessageParcel & reply)472 int32_t ServiceStub::CmdUpdateTimer(MessageParcel &data, MessageParcel &reply)
473 {
474     HILOGI("ServiceStub::CmdUpdateTimer Begin.");
475     int ret = ERR_OK;
476     string bundleName;
477     if (!data.ReadString(bundleName)) {
478         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to recive bundleName"));
479     }
480     uint32_t timeout;
481     if (!data.ReadUint32(timeout)) {
482         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to recive timeout"));
483     }
484     bool result;
485     ret = UpdateTimer(bundleName, timeout, result);
486     if (ret != ERR_OK) {
487         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to call UpdateTimer"));
488     }
489     if (!reply.WriteBool(result)) {
490         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to write result"));
491     }
492     HILOGI("ServiceStub::CmdUpdateTimer end.");
493     return BError(BError::Codes::OK);
494 }
495 
CmdUpdateSendRate(MessageParcel & data,MessageParcel & reply)496 int32_t ServiceStub::CmdUpdateSendRate(MessageParcel &data, MessageParcel &reply)
497 {
498     HILOGI("ServiceStub::CmdUpdateSendRate Begin.");
499     int ret = ERR_OK;
500     string bundleName;
501     if (!data.ReadString(bundleName)) {
502         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to recive bundleName"));
503     }
504     int32_t sendRate;
505     if (!data.ReadInt32(sendRate)) {
506         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to recive sendRate"));
507     }
508     bool result;
509     ret = UpdateSendRate(bundleName, sendRate, result);
510     if (ret != ERR_OK) {
511         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to call UpdateSendRate"));
512     }
513     if (!reply.WriteBool(result)) {
514         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to write result"));
515     }
516     HILOGI("ServiceStub::CmdUpdateSendRate end.");
517     return BError(BError::Codes::OK);
518 }
519 
CmdStartExtTimer(MessageParcel & data,MessageParcel & reply)520 int32_t ServiceStub::CmdStartExtTimer(MessageParcel &data, MessageParcel &reply)
521 {
522     HILOGI("ServiceStub::CmdStartExtTimer Begin.");
523     int ret = ERR_OK;
524     bool isExtStart;
525     ret = StartExtTimer(isExtStart);
526     if (ret != ERR_OK) {
527         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to call UpdateTimer"));
528     }
529     if (!reply.WriteBool(isExtStart)) {
530         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to write result"));
531     }
532     HILOGI("ServiceStub::CmdStartExtTimer end.");
533     return BError(BError::Codes::OK);
534 }
535 
CmdStartFwkTimer(MessageParcel & data,MessageParcel & reply)536 int32_t ServiceStub::CmdStartFwkTimer(MessageParcel &data, MessageParcel &reply)
537 {
538     HILOGI("ServiceStub::CmdStartFwkTimer Begin.");
539     int ret = ERR_OK;
540     bool isFwkStart;
541     ret = StartFwkTimer(isFwkStart);
542     if (ret != ERR_OK) {
543         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to call UpdateTimer"));
544     }
545     if (!reply.WriteBool(isFwkStart)) {
546         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to write result"));
547     }
548     HILOGI("ServiceStub::CmdStartFwkTimer end.");
549     return BError(BError::Codes::OK);
550 }
551 
CmdRelease(MessageParcel & data,MessageParcel & reply)552 int32_t ServiceStub::CmdRelease(MessageParcel &data, MessageParcel &reply)
553 {
554     int res = Release();
555     if (!reply.WriteInt32(res)) {
556         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to send the result ") + to_string(res));
557     }
558     return BError(BError::Codes::OK);
559 }
560 
CmdGetLocalCapabilitiesIncremental(MessageParcel & data,MessageParcel & reply)561 int32_t ServiceStub::CmdGetLocalCapabilitiesIncremental(MessageParcel &data, MessageParcel &reply)
562 {
563     vector<BIncrementalData> bundleNames;
564     if (!ReadParcelableVector(bundleNames, data)) {
565         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bundleNames");
566     }
567 
568     UniqueFd fd(GetLocalCapabilitiesIncremental(bundleNames));
569     if (!reply.WriteFileDescriptor(fd)) {
570         return BError(BError::Codes::SA_BROKEN_IPC, "Failed to send out the file");
571     }
572     return BError(BError::Codes::OK);
573 }
574 
CmdGetAppLocalListAndDoIncrementalBackup(MessageParcel & data,MessageParcel & reply)575 int32_t ServiceStub::CmdGetAppLocalListAndDoIncrementalBackup(MessageParcel &data, MessageParcel &reply)
576 {
577     int32_t res = GetAppLocalListAndDoIncrementalBackup();
578     if (!reply.WriteInt32(res)) {
579         stringstream ss;
580         ss << "Failed to send the result " << res;
581         return BError(BError::Codes::SA_BROKEN_IPC, ss.str());
582     }
583     return BError(BError::Codes::OK);
584 }
585 
CmdInitIncrementalBackupSession(MessageParcel & data,MessageParcel & reply)586 int32_t ServiceStub::CmdInitIncrementalBackupSession(MessageParcel &data, MessageParcel &reply)
587 {
588     auto remote = data.ReadRemoteObject();
589     if (!remote) {
590         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive the reverse stub");
591     }
592     auto iremote = iface_cast<IServiceReverse>(remote);
593     if (!iremote) {
594         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive the reverse stub");
595     }
596 
597     int32_t res = InitIncrementalBackupSession(iremote);
598     if (!reply.WriteInt32(res)) {
599         stringstream ss;
600         ss << "Failed to send the result " << res;
601         return BError(BError::Codes::SA_BROKEN_IPC, ss.str());
602     }
603     return BError(BError::Codes::OK);
604 }
605 
CmdInitIncrementalBackupSessionMsg(MessageParcel & data,MessageParcel & reply)606 int32_t ServiceStub::CmdInitIncrementalBackupSessionMsg(MessageParcel &data, MessageParcel &reply)
607 {
608     auto remote = data.ReadRemoteObject();
609     std::string errMsg;
610     if (!remote) {
611         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive the reverse stub");
612     }
613     auto iRemote = iface_cast<IServiceReverse>(remote);
614     if (!iRemote) {
615         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive the reverse stub");
616     }
617     int32_t res = InitIncrementalBackupSession(iRemote, errMsg);
618     if (!reply.WriteString(errMsg)) {
619         return BError(BError::Codes::SA_BROKEN_IPC, "Failed to send the errMsg");
620     }
621     if (!reply.WriteInt32(res)) {
622         stringstream ss;
623         ss << "Failed to send the result " << res;
624         return BError(BError::Codes::SA_BROKEN_IPC, ss.str());
625     }
626     return BError(BError::Codes::OK);
627 }
628 
CmdAppendBundlesIncrementalBackupSession(MessageParcel & data,MessageParcel & reply)629 int32_t ServiceStub::CmdAppendBundlesIncrementalBackupSession(MessageParcel &data, MessageParcel &reply)
630 {
631     vector<BIncrementalData> bundlesToBackup;
632     if (!ReadParcelableVector(bundlesToBackup, data)) {
633         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bundleNames");
634     }
635 
636     int32_t res = AppendBundlesIncrementalBackupSession(bundlesToBackup);
637     if (!reply.WriteInt32(res)) {
638         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to send the result ") + to_string(res));
639     }
640     return BError(BError::Codes::OK);
641 }
642 
CmdAppendBundlesDetailsIncrementalBackupSession(MessageParcel & data,MessageParcel & reply)643 int32_t ServiceStub::CmdAppendBundlesDetailsIncrementalBackupSession(MessageParcel &data, MessageParcel &reply)
644 {
645     vector<BIncrementalData> bundlesToBackup;
646     if (!ReadParcelableVector(bundlesToBackup, data)) {
647         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bundleNames");
648     }
649     std::vector<std::string> infos;
650     if (!data.ReadStringVector(&infos)) {
651         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive infos");
652     }
653     int32_t res = AppendBundlesIncrementalBackupSession(bundlesToBackup, infos);
654     if (!reply.WriteInt32(res)) {
655         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to send the result ") + to_string(res));
656     }
657     return BError(BError::Codes::OK);
658 }
659 
CmdPublishIncrementalFile(MessageParcel & data,MessageParcel & reply)660 int32_t ServiceStub::CmdPublishIncrementalFile(MessageParcel &data, MessageParcel &reply)
661 {
662     unique_ptr<BFileInfo> fileInfo(data.ReadParcelable<BFileInfo>());
663     if (!fileInfo) {
664         return BError(BError::Codes::SA_BROKEN_IPC, "Failed to receive fileInfo");
665     }
666     int res = PublishIncrementalFile(*fileInfo);
667     if (!reply.WriteInt32(res)) {
668         stringstream ss;
669         ss << "Failed to send the result " << res;
670         return BError(BError::Codes::SA_BROKEN_IPC, ss.str());
671     }
672     return BError(BError::Codes::OK);
673 }
674 
CmdPublishSAIncrementalFile(MessageParcel & data,MessageParcel & reply)675 int32_t ServiceStub::CmdPublishSAIncrementalFile(MessageParcel &data, MessageParcel &reply)
676 {
677     unique_ptr<BFileInfo> fileInfo(data.ReadParcelable<BFileInfo>());
678     if (!fileInfo) {
679         return BError(BError::Codes::SA_BROKEN_IPC, "Failed to receive fileInfo");
680     }
681     UniqueFd fd(data.ReadFileDescriptor());
682     int res = PublishSAIncrementalFile(*fileInfo, move(fd));
683     if (!reply.WriteInt32(res)) {
684         stringstream ss;
685         ss << "Failed to send the result " << res;
686         return BError(BError::Codes::SA_BROKEN_IPC, ss.str());
687     }
688     return BError(BError::Codes::OK);
689 }
690 
CmdAppIncrementalFileReady(MessageParcel & data,MessageParcel & reply)691 int32_t ServiceStub::CmdAppIncrementalFileReady(MessageParcel &data, MessageParcel &reply)
692 {
693     string fileName;
694     if (!data.ReadString(fileName)) {
695         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive fileName");
696     }
697     bool fdFlag = data.ReadBool();
698     UniqueFd fd = UniqueFd(INVALID_FD);
699     UniqueFd manifestFd = UniqueFd(INVALID_FD);
700     if (fdFlag == true) {
701         fd = UniqueFd(data.ReadFileDescriptor());
702         if (fd < 0) {
703             return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive fd");
704         }
705 
706         manifestFd = UniqueFd(data.ReadFileDescriptor());
707         if (manifestFd < 0) {
708             return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive manifestFd");
709         }
710     }
711     int32_t errCode = data.ReadInt32();
712 
713     int res = AppIncrementalFileReady(fileName, move(fd), move(manifestFd), errCode);
714     if (!reply.WriteInt32(res)) {
715         stringstream ss;
716         ss << "Failed to send the result " << res;
717         return BError(BError::Codes::SA_BROKEN_IPC, ss.str());
718     }
719     return BError(BError::Codes::OK);
720 }
721 
CmdAppIncrementalDone(MessageParcel & data,MessageParcel & reply)722 int32_t ServiceStub::CmdAppIncrementalDone(MessageParcel &data, MessageParcel &reply)
723 {
724     int32_t ret;
725     if (!data.ReadInt32(ret)) {
726         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bool flag");
727     }
728     int res = AppIncrementalDone(ret);
729     if (!reply.WriteInt32(res)) {
730         stringstream ss;
731         ss << "Failed to send the result " << res;
732         return BError(BError::Codes::SA_BROKEN_IPC, ss.str());
733     }
734     return BError(BError::Codes::OK);
735 }
736 
CmdGetIncrementalFileHandle(MessageParcel & data,MessageParcel & reply)737 int32_t ServiceStub::CmdGetIncrementalFileHandle(MessageParcel &data, MessageParcel &reply)
738 {
739     string bundleName;
740     if (!data.ReadString(bundleName)) {
741         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bundleName").GetCode();
742     }
743     string fileName;
744     if (!data.ReadString(fileName)) {
745         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive fileName").GetCode();
746     }
747     return GetIncrementalFileHandle(bundleName, fileName);
748 }
749 
CmdReportAppProcessInfo(MessageParcel & data,MessageParcel & reply)750 int32_t ServiceStub::CmdReportAppProcessInfo(MessageParcel &data, MessageParcel &reply)
751 {
752     string processInfo;
753     if (!data.ReadString(processInfo)) {
754         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bundleName").GetCode();
755     }
756     int32_t scenario;
757     if (!data.ReadInt32(scenario)) {
758         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive errCode");
759     }
760     BackupRestoreScenario secenrioInfo = static_cast<BackupRestoreScenario>(scenario);
761     return ReportAppProcessInfo(processInfo, secenrioInfo);
762 }
763 
CmdStopExtTimer(MessageParcel & data,MessageParcel & reply)764 int32_t ServiceStub::CmdStopExtTimer(MessageParcel &data, MessageParcel &reply)
765 {
766     HILOGI("ServiceStub::CmdStopExtTimer Begin.");
767     bool isExtStop;
768     auto ret = StopExtTimer(isExtStop);
769     if (ret != ERR_OK) {
770         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to call StopExtTimer"));
771     }
772     if (!reply.WriteBool(isExtStop)) {
773         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to write result"));
774     }
775     HILOGI("ServiceStub::CmdStopExtTimer end.");
776     return BError(BError::Codes::OK);
777 }
778 
CmdRefreshDataSize(MessageParcel & data,MessageParcel & reply)779 int32_t ServiceStub::CmdRefreshDataSize(MessageParcel &data, MessageParcel &reply)
780 {
781     HILOGI("ServiceStub::CmdRefreshDataSize Begin.");
782     int64_t totalDatasize = 0;
783     if (!data.ReadInt64(totalDatasize)) {
784         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive totalDatasize").GetCode();
785     }
786     auto ret = RefreshDataSize(totalDatasize);
787     if (ret != ERR_OK) {
788         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to receive totalDatasize"));
789     }
790     bool ifRefreshSuccess = true;
791     if (!reply.WriteBool(ifRefreshSuccess)) {
792         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to write result"));
793     }
794     HILOGI("ServiceStub::CmdRefreshDataSize end.");
795     return BError(BError::Codes::OK);
796 }
797 
798 template <typename T>
ReadParcelableVector(std::vector<T> & parcelableInfos,MessageParcel & data)799 bool ServiceStub::ReadParcelableVector(std::vector<T> &parcelableInfos, MessageParcel &data)
800 {
801     int32_t infoSize = 0;
802     if (!data.ReadInt32(infoSize)) {
803         HILOGE("Failed to read Parcelable size.");
804         return false;
805     }
806 
807     parcelableInfos.clear();
808     infoSize = (infoSize < BConstants::MAX_PARCELABLE_VECTOR_NUM) ? infoSize : BConstants::MAX_PARCELABLE_VECTOR_NUM;
809     for (int32_t index = 0; index < infoSize; index++) {
810         sptr<T> info = data.ReadParcelable<T>();
811         if (info == nullptr) {
812             HILOGE("Failed to read Parcelable infos.");
813             return false;
814         }
815         parcelableInfos.emplace_back(move(*info));
816     }
817 
818     return true;
819 }
820 
CmdCancel(MessageParcel & data,MessageParcel & reply)821 int32_t ServiceStub::CmdCancel(MessageParcel &data, MessageParcel &reply)
822 {
823     HILOGI("ServiceStub::CmdCancel Begin.");
824     string bundleName;
825     if (!data.ReadString(bundleName)) {
826         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bundleName").GetCode();
827     }
828     int result = BError::BackupErrorCode::E_CANCEL_UNSTARTED_TASK;
829     ErrCode res = Cancel(bundleName, result);
830     if (!reply.WriteInt32(result)) {
831         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to send the result ") + to_string(res));
832     }
833     return res;
834 }
835 
CmdGetLocalCapabilitiesForBdInfos(MessageParcel & data,MessageParcel & reply)836 int32_t ServiceStub::CmdGetLocalCapabilitiesForBdInfos(MessageParcel &data, MessageParcel &reply)
837 {
838     UniqueFd fd(GetLocalCapabilitiesForBundleInfos());
839     if (!reply.WriteFileDescriptor(fd)) {
840         return BError(BError::Codes::SA_BROKEN_IPC, "Failed to send out the file");
841     }
842     return BError(BError::Codes::OK);
843 }
844 
CmdGetBackupDataSize(MessageParcel & data,MessageParcel & reply)845 int32_t ServiceStub::CmdGetBackupDataSize(MessageParcel &data, MessageParcel &reply)
846 {
847     HILOGI("ServiceStub CmdGetBackupDataSize Begin.");
848     bool isPreciseScan = true;
849     isPreciseScan = data.ReadBool();
850     vector<BIncrementalData> bundleNameList;
851     if (!ReadParcelableVector(bundleNameList, data)) {
852         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bundleNames");
853     }
854     auto ret = GetBackupDataSize(isPreciseScan, bundleNameList);
855     HILOGI("ServiceStub GetBackupDataSize End ret = %{public}d", ret);
856     return ret;
857 }
858 } // namespace OHOS::FileManagement::Backup
859