• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
InitIncrementalBackupSession(sptr<IServiceReverse> remote,std::string & errMsg)135 ErrCode ServiceProxy::InitIncrementalBackupSession(sptr<IServiceReverse> remote, std::string &errMsg)
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     MessageParcel reply;
145     MessageOption option;
146     if (!remote) {
147         HILOGE("Empty reverse stub");
148         return BError(BError::Codes::SDK_INVAL_ARG, "Empty reverse stub").GetCode();
149     }
150     if (!data.WriteRemoteObject(remote->AsObject().GetRefPtr())) {
151         HILOGE("Failed to send the reverse stub");
152         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the reverse stub").GetCode();
153     }
154     int32_t ret = Remote()->SendRequest(
155         static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_INIT_INCREMENTAL_BACKUP_SESSION_MSG),
156         data, reply, option);
157     if (ret != NO_ERROR) {
158         HILOGE("Received error %{public}d when doing IPC", ret);
159         return BError(BError::Codes::SDK_INVAL_ARG, "Received error when doing IPC").GetCode();
160     }
161     if (!reply.ReadString(errMsg)) {
162         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to receive the errMsg").GetCode();
163     }
164     return reply.ReadInt32();
165 }
166 
AppendBundlesIncrementalBackupSession(const vector<BIncrementalData> & bundlesToBackup)167 ErrCode ServiceProxy::AppendBundlesIncrementalBackupSession(const vector<BIncrementalData> &bundlesToBackup)
168 {
169     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
170     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "remote is nullptr");
171     MessageParcel data;
172     if (!data.WriteInterfaceToken(GetDescriptor())) {
173         HILOGE("Failed to write descriptor");
174         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
175     }
176 
177     if (!WriteParcelableVector(bundlesToBackup, data)) {
178         HILOGE("Failed to send the bundleNames");
179         return UniqueFd(-EPERM);
180     }
181 
182     MessageParcel reply;
183     MessageOption option;
184 
185     int32_t ret = Remote()->SendRequest(
186         static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_INCREMENTAL_BACKUP_SESSION), data,
187         reply, option);
188     if (ret != NO_ERROR) {
189         HILOGE("Received error %{public}d when doing IPC", ret);
190         return BError(BError::Codes::SDK_INVAL_ARG, "Received error when doing IPC").GetCode();
191     }
192     return reply.ReadInt32();
193 }
194 
AppendBundlesIncrementalBackupSession(const vector<BIncrementalData> & bundlesToBackup,const std::vector<std::string> & infos)195 ErrCode ServiceProxy::AppendBundlesIncrementalBackupSession(const vector<BIncrementalData> &bundlesToBackup,
196     const std::vector<std::string> &infos)
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         HILOGE("Failed to write descriptor");
203         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
204     }
205 
206     if (!WriteParcelableVector(bundlesToBackup, data)) {
207         HILOGE("Failed to send the bundleNames");
208         return UniqueFd(-EPERM);
209     }
210 
211     if (!data.WriteStringVector(infos)) {
212         HILOGE("Failed to write infos");
213         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send bundleNames").GetCode();
214     }
215     MessageParcel reply;
216     MessageOption option;
217 
218     int32_t ret = Remote()->SendRequest(
219         static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_INCREMENTAL_BACKUP_SESSION_DETAILS),
220         data, reply, option);
221     if (ret != NO_ERROR) {
222         HILOGE("Received error %{public}d when doing IPC", ret);
223         return BError(BError::Codes::SDK_INVAL_ARG, "Received error when doing IPC").GetCode();
224     }
225     return reply.ReadInt32();
226 }
227 
PublishIncrementalFile(const BFileInfo & fileInfo)228 ErrCode ServiceProxy::PublishIncrementalFile(const BFileInfo &fileInfo)
229 {
230     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
231     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
232     MessageParcel data;
233     if (!data.WriteInterfaceToken(GetDescriptor())) {
234         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
235     }
236 
237     if (!data.WriteParcelable(&fileInfo)) {
238         HILOGE("Failed to send the fileInfo");
239         return -EPIPE;
240     }
241 
242     MessageParcel reply;
243     MessageOption option;
244     int32_t ret = Remote()->SendRequest(
245         static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_PUBLISH_INCREMENTAL_FILE), data, reply, option);
246     if (ret != NO_ERROR) {
247         string str = "Failed to send out the request because of " + to_string(ret);
248         return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
249     }
250     return reply.ReadInt32();
251 }
252 
PublishSAIncrementalFile(const BFileInfo & fileInfo,UniqueFd fd)253 ErrCode ServiceProxy::PublishSAIncrementalFile(const BFileInfo &fileInfo, UniqueFd fd)
254 {
255     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
256     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
257     MessageParcel data;
258     if (!data.WriteInterfaceToken(GetDescriptor())) {
259         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
260     }
261 
262     if (!data.WriteParcelable(&fileInfo)) {
263         HILOGE("Failed to send the fileInfo");
264         return -EPIPE;
265     }
266 
267     if (!data.WriteFileDescriptor(fd)) {
268         HILOGE("Failed to send the fd");
269         return -EPIPE;
270     }
271 
272     MessageParcel reply;
273     MessageOption option;
274     int32_t ret = Remote()->SendRequest(
275         static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_PUBLISH_SA_INCREMENTAL_FILE), data, reply, option);
276     if (ret != NO_ERROR) {
277         string str = "Failed to send out the request because of " + to_string(ret);
278         return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
279     }
280     return reply.ReadInt32();
281 }
282 
AppIncrementalFileReady(const std::string & fileName,UniqueFd fd,UniqueFd manifestFd,int32_t errCode)283 ErrCode ServiceProxy::AppIncrementalFileReady(const std::string &fileName, UniqueFd fd, UniqueFd manifestFd,
284     int32_t errCode)
285 {
286     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
287     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
288     MessageParcel data;
289     if (!data.WriteInterfaceToken(GetDescriptor())) {
290         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
291     }
292 
293     if (!data.WriteString(fileName)) {
294         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the filename").GetCode();
295     }
296     bool fdFlag = (fd < 0 || manifestFd < 0) ? false : true;
297     data.WriteBool(fdFlag);
298     if (fdFlag == true && !data.WriteFileDescriptor(fd)) {
299         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the fd").GetCode();
300     }
301     if (fdFlag == true && !data.WriteFileDescriptor(manifestFd)) {
302         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the fd").GetCode();
303     }
304     if (!data.WriteInt32(errCode)) {
305         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the errCode").GetCode();
306     }
307 
308     MessageParcel reply;
309     MessageOption option;
310     int32_t ret = Remote()->SendRequest(
311         static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APP_INCREMENTAL_FILE_READY), data, reply, option);
312     if (ret != NO_ERROR) {
313         string str = "Failed to send out the request because of " + to_string(ret);
314         return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
315     }
316     return reply.ReadInt32();
317 }
318 
AppIncrementalDone(ErrCode errCode)319 ErrCode ServiceProxy::AppIncrementalDone(ErrCode errCode)
320 {
321     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
322     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
323     MessageParcel data;
324     if (!data.WriteInterfaceToken(GetDescriptor())) {
325         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
326     }
327 
328     if (!data.WriteInt32(errCode)) {
329         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the errCode").GetCode();
330     }
331 
332     MessageParcel reply;
333     MessageOption option;
334     int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APP_INCREMENTAL_DONE),
335                                         data, reply, option);
336     if (ret != NO_ERROR) {
337         string str = "Failed to send out the request because of " + to_string(ret);
338         return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
339     }
340     return reply.ReadInt32();
341 }
342 
GetIncrementalFileHandle(const std::string & bundleName,const std::string & fileName)343 ErrCode ServiceProxy::GetIncrementalFileHandle(const std::string &bundleName, const std::string &fileName)
344 {
345     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
346     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
347     MessageParcel data;
348     if (!data.WriteInterfaceToken(GetDescriptor())) {
349         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
350     }
351 
352     if (!data.WriteString(bundleName)) {
353         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the bundleName").GetCode();
354     }
355     if (!data.WriteString(fileName)) {
356         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the fileName").GetCode();
357     }
358 
359     MessageParcel reply;
360     MessageOption option;
361     int32_t ret = Remote()->SendRequest(
362         static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_GET_INCREMENTAL_FILE_NAME), data, reply, option);
363     if (ret != NO_ERROR) {
364         string str = "Failed to send out the request because of " + to_string(ret);
365         return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
366     }
367     return ret;
368 }
369 
370 template <typename T>
WriteParcelableVector(const std::vector<T> & parcelableVector,Parcel & data)371 bool ServiceProxy::WriteParcelableVector(const std::vector<T> &parcelableVector, Parcel &data)
372 {
373     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
374     if (!data.WriteUint32(parcelableVector.size())) {
375         HILOGE("failed to WriteInt32 for parcelableVector.size()");
376         return false;
377     }
378 
379     for (const auto &parcelable : parcelableVector) {
380         if (!data.WriteParcelable(&parcelable)) {
381             HILOGE("failed to WriteParcelable for parcelable");
382             return false;
383         }
384     }
385 
386     return true;
387 }
388 
Cancel(std::string bundleName,int32_t & result)389 ErrCode ServiceProxy::Cancel(std::string bundleName, int32_t &result)
390 {
391     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
392     if (!Remote()) {
393         return BError(BError::Codes::SDK_INVAL_ARG, "Remote is nullptr").GetCode();
394     }
395     MessageParcel data;
396     if (!data.WriteInterfaceToken(GetDescriptor())) {
397         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
398     }
399 
400     if (!data.WriteString(bundleName)) {
401         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the bundleName").GetCode();
402     }
403 
404     MessageParcel reply;
405     MessageOption option;
406     int32_t ret = Remote()->SendRequest(
407         static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_CANCEL_BUNDLE), data, reply, option);
408     if (ret != NO_ERROR) {
409         string str = "Failed to send out the request because of " + to_string(ret);
410         return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
411     }
412     reply.ReadInt32(result);
413     HILOGI("ServiceProxy Cancel end, result:%{public}d", result);
414 
415     return BError(BError::Codes::OK, "success");
416 }
417 } // namespace OHOS::FileManagement::Backup