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_UPDATE_TIMER)] =
40         &ServiceStub::CmdUpdateTimer;
41     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_UPDATE_SENDRATE)] =
42         &ServiceStub::CmdUpdateSendRate;
43     opToInterfaceMap_[static_cast<uint32_t>(
44         IServiceInterfaceCode::SERVICE_CMD_GET_APP_LOCAL_LIST_AND_DO_INCREMENTAL_BACKUP)] =
45         &ServiceStub::CmdGetAppLocalListAndDoIncrementalBackup;
46     opToInterfaceMap_[static_cast<uint32_t>(
47         IServiceInterfaceCode::SERVICE_CMD_REPORT_APP_PROCESS_INFO)] =
48         &ServiceStub::CmdReportAppProcessInfo;
49     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_START_EXT_TIMER)] =
50         &ServiceStub::CmdStartExtTimer;
51     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_START_FWK_TIMER)] =
52         &ServiceStub::CmdStartFwkTimer;
53 }
54 
ServiceStubSuppAppendBundles()55 void ServiceStub::ServiceStubSuppAppendBundles()
56 {
57     opToInterfaceMap_[static_cast<uint32_t>(
58         IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_INCREMENTAL_BACKUP_SESSION)] =
59         &ServiceStub::CmdAppendBundlesIncrementalBackupSession;
60     opToInterfaceMap_[static_cast<uint32_t>(
61         IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_INCREMENTAL_BACKUP_SESSION_DETAILS)] =
62         &ServiceStub::CmdAppendBundlesDetailsIncrementalBackupSession;
63 }
64 
ServiceStub()65 ServiceStub::ServiceStub()
66 {
67     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_INIT_RESTORE_SESSION)] =
68         &ServiceStub::CmdInitRestoreSession;
69     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_INIT_BACKUP_SESSION)] =
70         &ServiceStub::CmdInitBackupSession;
71     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_GET_LOCAL_CAPABILITIES)] =
72         &ServiceStub::CmdGetLocalCapabilities;
73     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_PUBLISH_FILE)] =
74         &ServiceStub::CmdPublishFile;
75     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APP_FILE_READY)] =
76         &ServiceStub::CmdAppFileReady;
77     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APP_DONE)] = &ServiceStub::CmdAppDone;
78     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_RESULT_REPORT)] =
79         &ServiceStub::CmdResultReport;
80     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_START)] = &ServiceStub::CmdStart;
81     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_GET_FILE_NAME)] =
82         &ServiceStub::CmdGetFileHandle;
83     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_RESTORE_SESSION)] =
84         &ServiceStub::CmdAppendBundlesRestoreSession;
85     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_RESTORE_SESSION_DETAIL)] =
86         &ServiceStub::CmdAppendBundlesDetailsRestoreSession;
87     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_BACKUP_SESSION)] =
88         &ServiceStub::CmdAppendBundlesBackupSession;
89     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_BACKUP_SESSION_DETAILS)] =
90         &ServiceStub::CmdAppendBundlesDetailsBackupSession;
91     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_FINISH)] = &ServiceStub::CmdFinish;
92     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_RELSEASE_SESSION)] =
93         &ServiceStub::CmdRelease;
94     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_GET_LOCAL_CAPABILITIES_INCREMENTAL)] =
95         &ServiceStub::CmdGetLocalCapabilitiesIncremental;
96     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_INIT_INCREMENTAL_BACKUP_SESSION)] =
97         &ServiceStub::CmdInitIncrementalBackupSession;
98     ServiceStubSuppAppendBundles();
99     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_PUBLISH_INCREMENTAL_FILE)] =
100         &ServiceStub::CmdPublishIncrementalFile;
101     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_PUBLISH_SA_INCREMENTAL_FILE)] =
102         &ServiceStub::CmdPublishSAIncrementalFile;
103     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APP_INCREMENTAL_FILE_READY)] =
104         &ServiceStub::CmdAppIncrementalFileReady;
105     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APP_INCREMENTAL_DONE)] =
106         &ServiceStub::CmdAppIncrementalDone;
107     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_GET_INCREMENTAL_FILE_NAME)] =
108         &ServiceStub::CmdGetIncrementalFileHandle;
109     opToInterfaceMap_[static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_GET_BACKUP_INFO)] =
110         &ServiceStub::CmdGetBackupInfo;
111     ServiceStubSupplement();
112 }
113 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)114 int32_t ServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
115 {
116     const std::u16string descriptor = ServiceStub::GetDescriptor();
117     const std::u16string remoteDescriptor = data.ReadInterfaceToken();
118     if (descriptor != remoteDescriptor) {
119         return BError(BError::Codes::SA_INVAL_ARG, "Invalid remote descriptor");
120     }
121 
122     HILOGD("Begin to call procedure indexed %{public}u", code);
123     auto interfaceIndex = opToInterfaceMap_.find(code);
124     if (interfaceIndex == opToInterfaceMap_.end() || !interfaceIndex->second) {
125         HILOGE("Cannot response request %{public}d : unknown procedure", code);
126         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
127     }
128 
129     return BExcepUltils::ExceptionCatcherLocked(
130         [&]() { return ErrCode((this->*(interfaceIndex->second))(data, reply)); });
131 }
132 
CmdInitRestoreSession(MessageParcel & data,MessageParcel & reply)133 int32_t ServiceStub::CmdInitRestoreSession(MessageParcel &data, MessageParcel &reply)
134 {
135     auto remote = data.ReadRemoteObject();
136     if (!remote) {
137         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive the reverse stub");
138     }
139     auto iremote = iface_cast<IServiceReverse>(remote);
140     if (!iremote) {
141         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive the reverse stub");
142     }
143 
144     int32_t res = InitRestoreSession(iremote);
145     if (!reply.WriteInt32(res)) {
146         stringstream ss;
147         ss << "Failed to send the result " << res;
148         return BError(BError::Codes::SA_BROKEN_IPC, ss.str());
149     }
150     return BError(BError::Codes::OK);
151 }
152 
CmdInitBackupSession(MessageParcel & data,MessageParcel & reply)153 int32_t ServiceStub::CmdInitBackupSession(MessageParcel &data, MessageParcel &reply)
154 {
155     auto remote = data.ReadRemoteObject();
156     if (!remote) {
157         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive the reverse stub");
158     }
159     auto iremote = iface_cast<IServiceReverse>(remote);
160     if (!iremote) {
161         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive the reverse stub");
162     }
163 
164     int res = InitBackupSession(iremote);
165     if (!reply.WriteInt32(res)) {
166         stringstream ss;
167         ss << "Failed to send the result " << res;
168         return BError(BError::Codes::SA_BROKEN_IPC, ss.str());
169     }
170     return BError(BError::Codes::OK);
171 }
172 
CmdStart(MessageParcel & data,MessageParcel & reply)173 int32_t ServiceStub::CmdStart(MessageParcel &data, MessageParcel &reply)
174 {
175     int res = Start();
176     if (!reply.WriteInt32(res)) {
177         stringstream ss;
178         ss << "Failed to send the result " << res;
179         return BError(BError::Codes::SA_BROKEN_IPC, ss.str());
180     }
181     return BError(BError::Codes::OK);
182 }
183 
CmdGetLocalCapabilities(MessageParcel & data,MessageParcel & reply)184 int32_t ServiceStub::CmdGetLocalCapabilities(MessageParcel &data, MessageParcel &reply)
185 {
186     UniqueFd fd(GetLocalCapabilities());
187     if (!reply.WriteFileDescriptor(fd)) {
188         return BError(BError::Codes::SA_BROKEN_IPC, "Failed to send out the file");
189     }
190     return BError(BError::Codes::OK);
191 }
192 
CmdPublishFile(MessageParcel & data,MessageParcel & reply)193 int32_t ServiceStub::CmdPublishFile(MessageParcel &data, MessageParcel &reply)
194 {
195     unique_ptr<BFileInfo> fileInfo(data.ReadParcelable<BFileInfo>());
196     if (!fileInfo) {
197         return BError(BError::Codes::SA_BROKEN_IPC, "Failed to receive fileInfo");
198     }
199     int res = PublishFile(*fileInfo);
200     if (!reply.WriteInt32(res)) {
201         stringstream ss;
202         ss << "Failed to send the result " << res;
203         return BError(BError::Codes::SA_BROKEN_IPC, ss.str());
204     }
205     return BError(BError::Codes::OK);
206 }
207 
CmdAppFileReady(MessageParcel & data,MessageParcel & reply)208 int32_t ServiceStub::CmdAppFileReady(MessageParcel &data, MessageParcel &reply)
209 {
210     string fileName;
211     if (!data.ReadString(fileName)) {
212         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive fileName");
213     }
214     bool fdFlag = data.ReadBool();
215     UniqueFd fd = UniqueFd(INVALID_FD);
216     if (fdFlag == true) {
217         fd = UniqueFd(data.ReadFileDescriptor());
218         if (fd < 0) {
219             return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive fd");
220         }
221     }
222     int32_t errCode = data.ReadInt32();
223 
224     int res = AppFileReady(fileName, move(fd), errCode);
225     if (!reply.WriteInt32(res)) {
226         stringstream ss;
227         ss << "Failed to send the result " << res;
228         return BError(BError::Codes::SA_BROKEN_IPC, ss.str());
229     }
230     return BError(BError::Codes::OK);
231 }
232 
CmdAppDone(MessageParcel & data,MessageParcel & reply)233 int32_t ServiceStub::CmdAppDone(MessageParcel &data, MessageParcel &reply)
234 {
235     ErrCode errCode;
236     if (!data.ReadInt32(errCode)) {
237         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive errCode");
238     }
239     int res = AppDone(errCode);
240     if (!reply.WriteInt32(res)) {
241         stringstream ss;
242         ss << "Failed to send the result " << res;
243         return BError(BError::Codes::SA_BROKEN_IPC, ss.str());
244     }
245     return BError(BError::Codes::OK);
246 }
247 
CmdResultReport(MessageParcel & data,MessageParcel & reply)248 int32_t ServiceStub::CmdResultReport(MessageParcel &data, MessageParcel &reply)
249 {
250     std::string restoreRetInfo;
251     int32_t scenario;
252     ErrCode errCode;
253     if (!data.ReadString(restoreRetInfo)) {
254         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive restoreRetInfo");
255     }
256     if (!data.ReadInt32(scenario)) {
257         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive scenario");
258     }
259     BackupRestoreScenario secenrioInfo = static_cast<BackupRestoreScenario>(scenario);
260     if (!data.ReadInt32(errCode)) {
261         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive errCode");
262     }
263     int res = ServiceResultReport(restoreRetInfo, secenrioInfo, errCode);
264     if (!reply.WriteInt32(res)) {
265         stringstream ss;
266         ss << "Failed to send the result " << res;
267         return BError(BError::Codes::SA_BROKEN_IPC, ss.str());
268     }
269     return BError(BError::Codes::OK);
270 }
271 
CmdGetFileHandle(MessageParcel & data,MessageParcel & reply)272 int32_t ServiceStub::CmdGetFileHandle(MessageParcel &data, MessageParcel &reply)
273 {
274     string bundleName;
275     if (!data.ReadString(bundleName)) {
276         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bundleName").GetCode();
277     }
278     string fileName;
279     if (!data.ReadString(fileName)) {
280         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive fileName").GetCode();
281     }
282 
283     return GetFileHandle(bundleName, fileName);
284 }
285 
CmdAppendBundlesRestoreSession(MessageParcel & data,MessageParcel & reply)286 int32_t ServiceStub::CmdAppendBundlesRestoreSession(MessageParcel &data, MessageParcel &reply)
287 {
288     UniqueFd fd(data.ReadFileDescriptor());
289     if (fd < 0) {
290         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive fd");
291     }
292 
293     vector<string> bundleNames;
294     if (!data.ReadStringVector(&bundleNames)) {
295         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bundleNames");
296     }
297     int32_t type;
298     if (!data.ReadInt32(type)) {
299         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive restoreType");
300     }
301     RestoreTypeEnum restoreType = static_cast<RestoreTypeEnum>(type);
302     int32_t userId;
303     if (!data.ReadInt32(userId)) {
304         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive userId");
305     }
306 
307     int res = AppendBundlesRestoreSession(move(fd), bundleNames, restoreType, userId);
308     if (!reply.WriteInt32(res)) {
309         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to send the result ") + to_string(res));
310     }
311     return BError(BError::Codes::OK);
312 }
313 
CmdAppendBundlesDetailsRestoreSession(MessageParcel & data,MessageParcel & reply)314 int32_t ServiceStub::CmdAppendBundlesDetailsRestoreSession(MessageParcel &data, MessageParcel &reply)
315 {
316     UniqueFd fd(data.ReadFileDescriptor());
317     if (fd < 0) {
318         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive fd");
319     }
320 
321     vector<string> bundleNames;
322     if (!data.ReadStringVector(&bundleNames)) {
323         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bundleNames");
324     }
325     vector<string> detailInfos;
326     if (!data.ReadStringVector(&detailInfos)) {
327         HILOGE("Failed to receive bundle detailInfos");
328         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive detailInfos");
329     }
330     int32_t type;
331     if (!data.ReadInt32(type)) {
332         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive restoreType");
333     }
334     RestoreTypeEnum restoreType = static_cast<RestoreTypeEnum>(type);
335     int32_t userId;
336     if (!data.ReadInt32(userId)) {
337         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive userId");
338     }
339 
340     int res = AppendBundlesRestoreSession(move(fd), bundleNames, detailInfos, restoreType, userId);
341     if (!reply.WriteInt32(res)) {
342         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to send the result ") + to_string(res));
343     }
344     return BError(BError::Codes::OK);
345 }
346 
CmdAppendBundlesBackupSession(MessageParcel & data,MessageParcel & reply)347 int32_t ServiceStub::CmdAppendBundlesBackupSession(MessageParcel &data, MessageParcel &reply)
348 {
349     vector<string> bundleNames;
350     if (!data.ReadStringVector(&bundleNames)) {
351         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bundleNames");
352     }
353 
354     int32_t res = AppendBundlesBackupSession(bundleNames);
355     if (!reply.WriteInt32(res)) {
356         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to send the result ") + to_string(res));
357     }
358     return BError(BError::Codes::OK);
359 }
360 
CmdAppendBundlesDetailsBackupSession(MessageParcel & data,MessageParcel & reply)361 int32_t ServiceStub::CmdAppendBundlesDetailsBackupSession(MessageParcel &data, MessageParcel &reply)
362 {
363     vector<string> bundleNames;
364     if (!data.ReadStringVector(&bundleNames)) {
365         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bundleNames");
366     }
367 
368     vector<string> detailInfos;
369     if (!data.ReadStringVector(&detailInfos)) {
370         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive detailInfos");
371     }
372 
373     int32_t res = AppendBundlesDetailsBackupSession(bundleNames, detailInfos);
374     if (!reply.WriteInt32(res)) {
375         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to send the result ") + to_string(res));
376     }
377     return BError(BError::Codes::OK);
378 }
379 
CmdFinish(MessageParcel & data,MessageParcel & reply)380 int32_t ServiceStub::CmdFinish(MessageParcel &data, MessageParcel &reply)
381 {
382     int res = Finish();
383     if (!reply.WriteInt32(res)) {
384         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to send the result ") + to_string(res));
385     }
386     return BError(BError::Codes::OK);
387 }
388 
CmdGetBackupInfo(MessageParcel & data,MessageParcel & reply)389 int32_t ServiceStub::CmdGetBackupInfo(MessageParcel &data, MessageParcel &reply)
390 {
391     HILOGI("ServiceStub::CmdGetBackupInfo Begin.");
392     int ret = ERR_OK;
393     string bundleName;
394     if (!data.ReadString(bundleName)) {
395         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to recive bundleName"));
396     }
397     string result;
398     ret = GetBackupInfo(bundleName, result);
399     if (ret != ERR_OK) {
400         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to call GetBackupInfo"));
401     }
402     if (!reply.WriteString(result)) {
403         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to write result"));
404     }
405     HILOGI("ServiceStub::CmdGetBackupInfo end.");
406     return BError(BError::Codes::OK);
407 }
408 
CmdUpdateTimer(MessageParcel & data,MessageParcel & reply)409 int32_t ServiceStub::CmdUpdateTimer(MessageParcel &data, MessageParcel &reply)
410 {
411     HILOGI("ServiceStub::CmdUpdateTimer Begin.");
412     int ret = ERR_OK;
413     string bundleName;
414     if (!data.ReadString(bundleName)) {
415         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to recive bundleName"));
416     }
417     uint32_t timeout;
418     if (!data.ReadUint32(timeout)) {
419         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to recive timeout"));
420     }
421     bool result;
422     ret = UpdateTimer(bundleName, timeout, result);
423     if (ret != ERR_OK) {
424         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to call UpdateTimer"));
425     }
426     if (!reply.WriteBool(result)) {
427         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to write result"));
428     }
429     HILOGI("ServiceStub::CmdUpdateTimer end.");
430     return BError(BError::Codes::OK);
431 }
432 
CmdUpdateSendRate(MessageParcel & data,MessageParcel & reply)433 int32_t ServiceStub::CmdUpdateSendRate(MessageParcel &data, MessageParcel &reply)
434 {
435     HILOGI("ServiceStub::CmdUpdateSendRate Begin.");
436     int ret = ERR_OK;
437     string bundleName;
438     if (!data.ReadString(bundleName)) {
439         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to recive bundleName"));
440     }
441     int32_t sendRate;
442     if (!data.ReadInt32(sendRate)) {
443         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to recive sendRate"));
444     }
445     bool result;
446     ret = UpdateSendRate(bundleName, sendRate, result);
447     if (ret != ERR_OK) {
448         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to call UpdateSendRate"));
449     }
450     if (!reply.WriteBool(result)) {
451         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to write result"));
452     }
453     HILOGI("ServiceStub::CmdUpdateSendRate end.");
454     return BError(BError::Codes::OK);
455 }
456 
CmdStartExtTimer(MessageParcel & data,MessageParcel & reply)457 int32_t ServiceStub::CmdStartExtTimer(MessageParcel &data, MessageParcel &reply)
458 {
459     HILOGI("ServiceStub::CmdStartExtTimer Begin.");
460     int ret = ERR_OK;
461     bool isExtStart;
462     ret = StartExtTimer(isExtStart);
463     if (ret != ERR_OK) {
464         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to call UpdateTimer"));
465     }
466     if (!reply.WriteBool(isExtStart)) {
467         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to write result"));
468     }
469     HILOGI("ServiceStub::CmdStartExtTimer end.");
470     return BError(BError::Codes::OK);
471 }
472 
CmdStartFwkTimer(MessageParcel & data,MessageParcel & reply)473 int32_t ServiceStub::CmdStartFwkTimer(MessageParcel &data, MessageParcel &reply)
474 {
475     HILOGI("ServiceStub::CmdStartFwkTimer Begin.");
476     int ret = ERR_OK;
477     bool isFwkStart;
478     ret = StartFwkTimer(isFwkStart);
479     if (ret != ERR_OK) {
480         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to call UpdateTimer"));
481     }
482     if (!reply.WriteBool(isFwkStart)) {
483         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to write result"));
484     }
485     HILOGI("ServiceStub::CmdStartFwkTimer end.");
486     return BError(BError::Codes::OK);
487 }
488 
CmdRelease(MessageParcel & data,MessageParcel & reply)489 int32_t ServiceStub::CmdRelease(MessageParcel &data, MessageParcel &reply)
490 {
491     int res = Release();
492     if (!reply.WriteInt32(res)) {
493         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to send the result ") + to_string(res));
494     }
495     return BError(BError::Codes::OK);
496 }
497 
CmdGetLocalCapabilitiesIncremental(MessageParcel & data,MessageParcel & reply)498 int32_t ServiceStub::CmdGetLocalCapabilitiesIncremental(MessageParcel &data, MessageParcel &reply)
499 {
500     vector<BIncrementalData> bundleNames;
501     if (!ReadParcelableVector(bundleNames, data)) {
502         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bundleNames");
503     }
504 
505     UniqueFd fd(GetLocalCapabilitiesIncremental(bundleNames));
506     if (!reply.WriteFileDescriptor(fd)) {
507         return BError(BError::Codes::SA_BROKEN_IPC, "Failed to send out the file");
508     }
509     return BError(BError::Codes::OK);
510 }
511 
CmdGetAppLocalListAndDoIncrementalBackup(MessageParcel & data,MessageParcel & reply)512 int32_t ServiceStub::CmdGetAppLocalListAndDoIncrementalBackup(MessageParcel &data, MessageParcel &reply)
513 {
514     int32_t res = GetAppLocalListAndDoIncrementalBackup();
515     if (!reply.WriteInt32(res)) {
516         stringstream ss;
517         ss << "Failed to send the result " << res;
518         return BError(BError::Codes::SA_BROKEN_IPC, ss.str());
519     }
520     return BError(BError::Codes::OK);
521 }
522 
CmdInitIncrementalBackupSession(MessageParcel & data,MessageParcel & reply)523 int32_t ServiceStub::CmdInitIncrementalBackupSession(MessageParcel &data, MessageParcel &reply)
524 {
525     auto remote = data.ReadRemoteObject();
526     if (!remote) {
527         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive the reverse stub");
528     }
529     auto iremote = iface_cast<IServiceReverse>(remote);
530     if (!iremote) {
531         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive the reverse stub");
532     }
533 
534     int32_t res = InitIncrementalBackupSession(iremote);
535     if (!reply.WriteInt32(res)) {
536         stringstream ss;
537         ss << "Failed to send the result " << res;
538         return BError(BError::Codes::SA_BROKEN_IPC, ss.str());
539     }
540     return BError(BError::Codes::OK);
541 }
542 
CmdAppendBundlesIncrementalBackupSession(MessageParcel & data,MessageParcel & reply)543 int32_t ServiceStub::CmdAppendBundlesIncrementalBackupSession(MessageParcel &data, MessageParcel &reply)
544 {
545     vector<BIncrementalData> bundlesToBackup;
546     if (!ReadParcelableVector(bundlesToBackup, data)) {
547         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bundleNames");
548     }
549 
550     int32_t res = AppendBundlesIncrementalBackupSession(bundlesToBackup);
551     if (!reply.WriteInt32(res)) {
552         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to send the result ") + to_string(res));
553     }
554     return BError(BError::Codes::OK);
555 }
556 
CmdAppendBundlesDetailsIncrementalBackupSession(MessageParcel & data,MessageParcel & reply)557 int32_t ServiceStub::CmdAppendBundlesDetailsIncrementalBackupSession(MessageParcel &data, MessageParcel &reply)
558 {
559     vector<BIncrementalData> bundlesToBackup;
560     if (!ReadParcelableVector(bundlesToBackup, data)) {
561         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bundleNames");
562     }
563     std::vector<std::string> infos;
564     if (!data.ReadStringVector(&infos)) {
565         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive infos");
566     }
567     int32_t res = AppendBundlesIncrementalBackupSession(bundlesToBackup, infos);
568     if (!reply.WriteInt32(res)) {
569         return BError(BError::Codes::SA_BROKEN_IPC, string("Failed to send the result ") + to_string(res));
570     }
571     return BError(BError::Codes::OK);
572 }
573 
CmdPublishIncrementalFile(MessageParcel & data,MessageParcel & reply)574 int32_t ServiceStub::CmdPublishIncrementalFile(MessageParcel &data, MessageParcel &reply)
575 {
576     unique_ptr<BFileInfo> fileInfo(data.ReadParcelable<BFileInfo>());
577     if (!fileInfo) {
578         return BError(BError::Codes::SA_BROKEN_IPC, "Failed to receive fileInfo");
579     }
580     int res = PublishIncrementalFile(*fileInfo);
581     if (!reply.WriteInt32(res)) {
582         stringstream ss;
583         ss << "Failed to send the result " << res;
584         return BError(BError::Codes::SA_BROKEN_IPC, ss.str());
585     }
586     return BError(BError::Codes::OK);
587 }
588 
CmdPublishSAIncrementalFile(MessageParcel & data,MessageParcel & reply)589 int32_t ServiceStub::CmdPublishSAIncrementalFile(MessageParcel &data, MessageParcel &reply)
590 {
591     unique_ptr<BFileInfo> fileInfo(data.ReadParcelable<BFileInfo>());
592     if (!fileInfo) {
593         return BError(BError::Codes::SA_BROKEN_IPC, "Failed to receive fileInfo");
594     }
595     UniqueFd fd(data.ReadFileDescriptor());
596     int res = PublishSAIncrementalFile(*fileInfo, move(fd));
597     if (!reply.WriteInt32(res)) {
598         stringstream ss;
599         ss << "Failed to send the result " << res;
600         return BError(BError::Codes::SA_BROKEN_IPC, ss.str());
601     }
602     return BError(BError::Codes::OK);
603 }
604 
CmdAppIncrementalFileReady(MessageParcel & data,MessageParcel & reply)605 int32_t ServiceStub::CmdAppIncrementalFileReady(MessageParcel &data, MessageParcel &reply)
606 {
607     string fileName;
608     if (!data.ReadString(fileName)) {
609         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive fileName");
610     }
611     bool fdFlag = data.ReadBool();
612     UniqueFd fd = UniqueFd(INVALID_FD);
613     UniqueFd manifestFd = UniqueFd(INVALID_FD);
614     if (fdFlag == true) {
615         fd = UniqueFd(data.ReadFileDescriptor());
616         if (fd < 0) {
617             return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive fd");
618         }
619 
620         manifestFd = UniqueFd(data.ReadFileDescriptor());
621         if (manifestFd < 0) {
622             return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive manifestFd");
623         }
624     }
625     int32_t errCode = data.ReadInt32();
626 
627     int res = AppIncrementalFileReady(fileName, move(fd), move(manifestFd), errCode);
628     if (!reply.WriteInt32(res)) {
629         stringstream ss;
630         ss << "Failed to send the result " << res;
631         return BError(BError::Codes::SA_BROKEN_IPC, ss.str());
632     }
633     return BError(BError::Codes::OK);
634 }
635 
CmdAppIncrementalDone(MessageParcel & data,MessageParcel & reply)636 int32_t ServiceStub::CmdAppIncrementalDone(MessageParcel &data, MessageParcel &reply)
637 {
638     int32_t ret;
639     if (!data.ReadInt32(ret)) {
640         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bool flag");
641     }
642     int res = AppIncrementalDone(ret);
643     if (!reply.WriteInt32(res)) {
644         stringstream ss;
645         ss << "Failed to send the result " << res;
646         return BError(BError::Codes::SA_BROKEN_IPC, ss.str());
647     }
648     return BError(BError::Codes::OK);
649 }
650 
CmdGetIncrementalFileHandle(MessageParcel & data,MessageParcel & reply)651 int32_t ServiceStub::CmdGetIncrementalFileHandle(MessageParcel &data, MessageParcel &reply)
652 {
653     string bundleName;
654     if (!data.ReadString(bundleName)) {
655         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bundleName").GetCode();
656     }
657     string fileName;
658     if (!data.ReadString(fileName)) {
659         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive fileName").GetCode();
660     }
661     return GetIncrementalFileHandle(bundleName, fileName);
662 }
663 
CmdReportAppProcessInfo(MessageParcel & data,MessageParcel & reply)664 int32_t ServiceStub::CmdReportAppProcessInfo(MessageParcel &data, MessageParcel &reply)
665 {
666     string processInfo;
667     if (!data.ReadString(processInfo)) {
668         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive bundleName").GetCode();
669     }
670     int32_t scenario;
671     if (!data.ReadInt32(scenario)) {
672         return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive errCode");
673     }
674     BackupRestoreScenario secenrioInfo = static_cast<BackupRestoreScenario>(scenario);
675     return ReportAppProcessInfo(processInfo, secenrioInfo);
676 }
677 
678 template <typename T>
ReadParcelableVector(std::vector<T> & parcelableInfos,MessageParcel & data)679 bool ServiceStub::ReadParcelableVector(std::vector<T> &parcelableInfos, MessageParcel &data)
680 {
681     int32_t infoSize = 0;
682     if (!data.ReadInt32(infoSize)) {
683         HILOGE("Failed to read Parcelable size.");
684         return false;
685     }
686 
687     parcelableInfos.clear();
688     infoSize = (infoSize < BConstants::MAX_PARCELABLE_VECTOR_NUM) ? infoSize : BConstants::MAX_PARCELABLE_VECTOR_NUM;
689     for (int32_t index = 0; index < infoSize; index++) {
690         sptr<T> info = data.ReadParcelable<T>();
691         if (info == nullptr) {
692             HILOGE("Failed to read Parcelable infos.");
693             return false;
694         }
695         parcelableInfos.emplace_back(move(*info));
696     }
697 
698     return true;
699 }
700 } // namespace OHOS::FileManagement::Backup
701