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 "module_ipc/svc_extension_proxy.h"
17 
18 #include "b_error/b_error.h"
19 #include "b_error/b_excep_utils.h"
20 #include "filemgmt_libhilog.h"
21 #include "iservice_registry.h"
22 #include "system_ability_definition.h"
23 #include "hitrace_meter.h"
24 
25 namespace OHOS::FileManagement::Backup {
26 using namespace std;
27 
GetIncrementalFileHandle(const string & fileName)28 ErrCode SvcExtensionProxy::GetIncrementalFileHandle(const string &fileName)
29 {
30     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
31     HILOGD("Start");
32     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
33     MessageParcel data;
34     data.WriteInterfaceToken(GetDescriptor());
35 
36     if (!data.WriteString(fileName)) {
37         BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the fileName");
38         return ErrCode(EPERM);
39     }
40 
41     MessageParcel reply;
42     MessageOption option;
43     int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(IExtensionInterfaceCode::CMD_GET_INCREMENTAL_FILE_HANDLE),
44                                         data, reply, option);
45     if (ret != NO_ERROR) {
46         HILOGE("Received error %{public}d when doing IPC", ret);
47         return ErrCode(ret);
48     }
49 
50     HILOGD("Successful");
51     return reply.ReadInt32();
52 }
53 
PublishIncrementalFile(const string & fileName)54 ErrCode SvcExtensionProxy::PublishIncrementalFile(const string &fileName)
55 {
56     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
57     HILOGD("Start");
58     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
59     MessageParcel data;
60     data.WriteInterfaceToken(GetDescriptor());
61 
62     if (!data.WriteString(fileName)) {
63         BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the fileName");
64         return ErrCode(EPERM);
65     }
66 
67     MessageParcel reply;
68     MessageOption option;
69     int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(IExtensionInterfaceCode::CMD_PUBLISH_INCREMENTAL_FILE),
70                                         data, reply, option);
71     if (ret != NO_ERROR) {
72         HILOGE("Received error %{public}d when doing IPC", ret);
73         return ErrCode(ret);
74     }
75 
76     HILOGD("Successful");
77     return reply.ReadInt32();
78 }
79 
HandleIncrementalBackup(UniqueFd incrementalFd,UniqueFd manifestFd)80 ErrCode SvcExtensionProxy::HandleIncrementalBackup(UniqueFd incrementalFd, UniqueFd manifestFd)
81 {
82     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
83     HILOGD("Start");
84     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
85     MessageParcel data;
86     data.WriteInterfaceToken(GetDescriptor());
87 
88     data.WriteFileDescriptor(incrementalFd);
89     data.WriteFileDescriptor(manifestFd);
90 
91     MessageParcel reply;
92     MessageOption option;
93     int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(IExtensionInterfaceCode::CMD_HANDLE_INCREMENTAL_BACKUP),
94                                         data, reply, option);
95     if (ret != NO_ERROR) {
96         HILOGE("Received error %{public}d when doing IPC", ret);
97         return ErrCode(ret);
98     }
99 
100     HILOGD("Successful");
101     return reply.ReadInt32();
102 }
103 
IncrementalOnBackup(bool isClearData)104 ErrCode SvcExtensionProxy::IncrementalOnBackup(bool isClearData)
105 {
106     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
107     HILOGD("Start");
108     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
109     MessageParcel data;
110     if (!data.WriteInterfaceToken(GetDescriptor()) || !data.WriteBool(isClearData)) {
111         return BError(BError::Codes::SDK_INVAL_ARG, "build param fail.");
112     }
113 
114     MessageParcel reply;
115     MessageOption option;
116     int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(IExtensionInterfaceCode::CMD_INCREMENTAL_ON_BACKUP),
117                                         data, reply, option);
118     if (ret != NO_ERROR) {
119         HILOGE("Received error %{public}d when doing IPC", ret);
120         return ErrCode(ret);
121     }
122 
123     HILOGD("Successful");
124     return BError(BError::Codes::OK);
125 }
126 
User0OnBackup()127 ErrCode SvcExtensionProxy::User0OnBackup()
128 {
129     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
130     HILOGD("Start");
131     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
132     MessageParcel data;
133     data.WriteInterfaceToken(GetDescriptor());
134 
135     MessageParcel reply;
136     MessageOption option;
137     int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(IExtensionInterfaceCode::CMD_HANDLE_USER_0_BACKUP),
138                                         data, reply, option);
139     if (ret != NO_ERROR) {
140         HILOGE("Received error %{public}d when doing IPC", ret);
141         return ErrCode(ret);
142     }
143 
144     HILOGD("Successful");
145     return reply.ReadInt32();
146 }
147 
GetIncrementalBackupFileHandle()148 tuple<UniqueFd, UniqueFd> SvcExtensionProxy::GetIncrementalBackupFileHandle()
149 {
150     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
151     HILOGD("Start");
152     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
153     MessageParcel data;
154     data.WriteInterfaceToken(GetDescriptor());
155 
156     MessageParcel reply;
157     MessageOption option;
158     int32_t ret = Remote()->SendRequest(
159         static_cast<uint32_t>(IExtensionInterfaceCode::CMD_GET_INCREMENTAL_BACKUP_FILE_HANDLE), data, reply, option);
160     if (ret != NO_ERROR) {
161         HILOGE("Received error %{public}d when doing IPC", ret);
162         return {UniqueFd(-1), UniqueFd(-1)};
163     }
164 
165     HILOGD("Successful");
166     UniqueFd incrementalFd(reply.ReadFileDescriptor());
167     UniqueFd manifestFd(reply.ReadFileDescriptor());
168     return {move(incrementalFd), move(manifestFd)};
169 }
170 } // namespace OHOS::FileManagement::Backup