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