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 #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 const int INVALID_FD = -1;
28 
GetFileHandle(const string & fileName,int32_t & errCode)29 UniqueFd SvcExtensionProxy::GetFileHandle(const string &fileName, int32_t &errCode)
30 {
31     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
32     HILOGI("Start");
33     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
34     MessageParcel data;
35     data.WriteInterfaceToken(GetDescriptor());
36 
37     if (!data.WriteString(fileName)) {
38         BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the fileName");
39         return UniqueFd(-1);
40     }
41 
42     MessageParcel reply;
43     MessageOption option;
44     int32_t ret =
45         Remote()->SendRequest(static_cast<uint32_t>(IExtensionInterfaceCode::CMD_GET_FILE_HANDLE), data, reply, option);
46     if (ret != NO_ERROR) {
47         HILOGE("Received error %{public}d when doing IPC", ret);
48         return UniqueFd(-ret);
49     }
50 
51     HILOGI("Successful");
52     bool fdFlag = reply.ReadBool();
53     errCode = reply.ReadInt32();
54     UniqueFd fd = UniqueFd(INVALID_FD);
55     if (fdFlag == true) {
56         fd = UniqueFd(reply.ReadFileDescriptor());
57     }
58     return UniqueFd(fd.Release());
59 }
60 
HandleClear()61 ErrCode SvcExtensionProxy::HandleClear()
62 {
63     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
64     HILOGI("Start");
65     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
66     MessageParcel data;
67     data.WriteInterfaceToken(GetDescriptor());
68 
69     MessageParcel reply;
70     MessageOption option;
71     int32_t ret =
72         Remote()->SendRequest(static_cast<uint32_t>(IExtensionInterfaceCode::CMD_HANDLE_CLAER), data, reply, option);
73     if (ret != NO_ERROR) {
74         HILOGE("Received error %{public}d when doing IPC", ret);
75         return ErrCode(ret);
76     }
77 
78     HILOGI("Successful");
79     return reply.ReadInt32();
80 }
81 
HandleBackup(bool isClearData)82 ErrCode SvcExtensionProxy::HandleBackup(bool isClearData)
83 {
84     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
85     HILOGI("Start");
86     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
87     MessageParcel data;
88     if (!data.WriteInterfaceToken(GetDescriptor()) || !data.WriteBool(isClearData)) {
89         return BError(BError::Codes::SDK_INVAL_ARG, "build param fail.");
90     }
91 
92     MessageParcel reply;
93     MessageOption option;
94     int32_t ret =
95         Remote()->SendRequest(static_cast<uint32_t>(IExtensionInterfaceCode::CMD_HANDLE_BACKUP), data, reply, option);
96     if (ret != NO_ERROR) {
97         HILOGE("Received error %{public}d when doing IPC", ret);
98         return ErrCode(ret);
99     }
100 
101     HILOGI("Successful");
102     return reply.ReadInt32();
103 }
104 
PublishFile(const string & fileName)105 ErrCode SvcExtensionProxy::PublishFile(const string &fileName)
106 {
107     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
108     HILOGI("Start");
109     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
110     MessageParcel data;
111     data.WriteInterfaceToken(GetDescriptor());
112 
113     if (!data.WriteString(fileName)) {
114         BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the fileName");
115         return ErrCode(EPERM);
116     }
117 
118     MessageParcel reply;
119     MessageOption option;
120     int32_t ret =
121         Remote()->SendRequest(static_cast<uint32_t>(IExtensionInterfaceCode::CMD_PUBLISH_FILE), data, reply, option);
122     if (ret != NO_ERROR) {
123         HILOGE("Received error %{public}d when doing IPC", ret);
124         return ErrCode(ret);
125     }
126 
127     HILOGD("Successful");
128     return reply.ReadInt32();
129 }
130 
HandleRestore(bool isClearData)131 ErrCode SvcExtensionProxy::HandleRestore(bool isClearData)
132 {
133     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
134     HILOGI("Start");
135     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
136     MessageParcel data;
137     if (!data.WriteInterfaceToken(GetDescriptor()) || !data.WriteBool(isClearData)) {
138         return BError(BError::Codes::SDK_INVAL_ARG, "build param fail.");
139     }
140 
141     MessageParcel reply;
142     MessageOption option;
143     int32_t ret =
144         Remote()->SendRequest(static_cast<uint32_t>(IExtensionInterfaceCode::CMD_HANDLE_RESTORE), data, reply, option);
145     if (ret != NO_ERROR) {
146         HILOGE("Received error %{public}d when doing IPC", ret);
147         return ErrCode(ret);
148     }
149 
150     HILOGI("Successful");
151     return reply.ReadInt32();
152 }
153 
GetBackupInfo(std::string & result)154 ErrCode SvcExtensionProxy::GetBackupInfo(std::string &result)
155 {
156     HILOGD("SvcExtensionProxy::GetBackupInfo begin.");
157     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
158     MessageParcel data;
159     data.WriteInterfaceToken(GetDescriptor());
160 
161     MessageParcel reply;
162     MessageOption option;
163     int32_t ret =
164         Remote()->SendRequest(static_cast<uint32_t>(IExtensionInterfaceCode::CMD_GET_BACKUP_INFO), data, reply, option);
165     if (ret != NO_ERROR) {
166         HILOGE("Received error %{public}d when doing IPC", ret);
167         return ErrCode(ret);
168     }
169     if (!reply.ReadInt32(ret)) {
170         HILOGE("fail to ReadInt32 ret");
171         return ErrCode(ret);
172     }
173     if (ret != NO_ERROR) {
174         HILOGE("ret is not NO_ERROR. ret = %d", ret);
175         return ErrCode(ret);
176     }
177     if (!reply.ReadString(result)) {
178         HILOGE("fail to ReadInt32 ret");
179         return ErrCode(ret);
180     }
181     HILOGI("SvcExtensionProxy::GetBackupInfo end. result: %s", result.c_str());
182     return ret;
183 }
184 
UpdateFdSendRate(std::string & bundleName,int32_t sendRate)185 ErrCode SvcExtensionProxy::UpdateFdSendRate(std::string &bundleName, int32_t sendRate)
186 {
187     HILOGD("SvcExtensionProxy::UpdateFdSendRate begin.");
188     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
189     MessageParcel data;
190     data.WriteInterfaceToken(GetDescriptor());
191     if (!data.WriteString(bundleName)) {
192         BError(BError::Codes::SDK_INVAL_ARG, "Failed to send bundleName");
193         return ErrCode(EPERM);
194     }
195     if (!data.WriteInt32(sendRate)) {
196         BError(BError::Codes::SDK_INVAL_ARG, "Failed to send sendRate");
197         return ErrCode(EPERM);
198     }
199     MessageParcel reply;
200     MessageOption option;
201     int32_t ret =
202         Remote()->SendRequest(static_cast<uint32_t>(IExtensionInterfaceCode::CMD_UPDATE_FD_SENDRATE), data, reply,
203             option);
204     if (ret != NO_ERROR) {
205         HILOGE("Received error %{public}d when doing IPC", ret);
206         return ErrCode(ret);
207     }
208     if (!reply.ReadInt32(ret)) {
209         HILOGE("fail to read ret, ret is %{public}d", ret);
210         return ErrCode(ret);
211     }
212     HILOGI("SvcExtensionProxy::UpdateFdSendRate end.");
213     return ret;
214 }
215 } // namespace OHOS::FileManagement::Backup
216