1 /*
2 * Copyright (c) 2022-2025 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 "b_session_backup.h"
17
18 #include "b_error/b_error.h"
19 #include "b_radar/b_radar.h"
20 #include "filemgmt_libhilog.h"
21 #include "service_client.h"
22 #include "service_reverse.h"
23
24 namespace OHOS::FileManagement::Backup {
25 using namespace std;
26
~BSessionBackup()27 BSessionBackup::~BSessionBackup()
28 {
29 HILOGE("BSessionBackup Destory");
30 deathRecipient_ = nullptr;
31 }
32
Init(Callbacks callbacks)33 unique_ptr<BSessionBackup> BSessionBackup::Init(Callbacks callbacks)
34 {
35 try {
36 HILOGI("Init BackupSession Begin");
37 auto backup = make_unique<BSessionBackup>();
38 ServiceClient::InvaildInstance();
39 auto proxy = ServiceClient::GetInstance();
40 if (proxy == nullptr) {
41 HILOGI("Failed to get backup service");
42 return nullptr;
43 }
44
45 int32_t res = proxy->InitBackupSession(sptr(new ServiceReverse(callbacks)));
46 if (res != ERR_OK) {
47 HILOGE("Failed to Backup because of %{public}d", res);
48 AppRadar::Info info("", "", "");
49 AppRadar::GetInstance().RecordBackupFuncRes(info, "BSessionBackup::Init",
50 AppRadar::GetInstance().GetUserId(), BizStageBackup::BIZ_STAGE_CREATE_BACKUP_SESSION_FAIL, res);
51 return nullptr;
52 }
53
54 backup->RegisterBackupServiceDied(callbacks.onBackupServiceDied);
55 return backup;
56 } catch (const exception &e) {
57 HILOGE("Failed to Backup because of %{public}s", e.what());
58 }
59 return nullptr;
60 }
61
Init(Callbacks callbacks,std::string & errMsg,ErrCode & errCode)62 unique_ptr<BSessionBackup> BSessionBackup::Init(Callbacks callbacks,
63 std::string &errMsg, ErrCode &errCode)
64 {
65 try {
66 HILOGI("Init BackupSession Begin");
67 auto backup = make_unique<BSessionBackup>();
68 ServiceClient::InvaildInstance();
69 auto proxy = ServiceClient::GetInstance();
70 if (proxy == nullptr) {
71 HILOGI("Failed to get backup service");
72 return nullptr;
73 }
74 proxy->InitBackupSessionWithErrMsg(sptr(new ServiceReverse(callbacks)), errCode, errMsg);
75 if (errCode != ERR_OK) {
76 HILOGE("Failed to Backup because of %{public}d, %{public}s", errCode, errMsg.c_str());
77 AppRadar::Info info("", "", "");
78 AppRadar::GetInstance().RecordBackupFuncRes(info, "BSessionBackup::Init",
79 AppRadar::GetInstance().GetUserId(), BizStageBackup::BIZ_STAGE_CREATE_BACKUP_SESSION_FAIL, errCode);
80 return nullptr;
81 }
82 backup->RegisterBackupServiceDied(callbacks.onBackupServiceDied);
83 return backup;
84 } catch (const exception &e) {
85 HILOGE("Failed to Backup because of %{public}s", e.what());
86 errCode = BError(BError::Codes::SDK_INVAL_ARG);
87 }
88 return nullptr;
89 }
90
RegisterBackupServiceDied(std::function<void ()> functor)91 void BSessionBackup::RegisterBackupServiceDied(std::function<void()> functor)
92 {
93 auto proxy = ServiceClient::GetInstance();
94 if (proxy == nullptr || !functor) {
95 return;
96 }
97 auto remoteObj = proxy->AsObject();
98 if (!remoteObj) {
99 throw BError(BError::Codes::SA_BROKEN_IPC, "Proxy's remote object can't be nullptr");
100 }
101
102 auto callback = [functor](const wptr<IRemoteObject> &obj) {
103 ServiceClient::InvaildInstance();
104 HILOGI("Backup service died");
105 functor();
106 };
107 deathRecipient_ = sptr(new SvcDeathRecipient(callback));
108 remoteObj->AddDeathRecipient(deathRecipient_);
109 }
110
Start()111 ErrCode BSessionBackup::Start()
112 {
113 auto proxy = ServiceClient::GetInstance();
114 if (proxy == nullptr) {
115 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
116 }
117
118 return proxy->Start();
119 }
120
GetLocalCapabilities()121 UniqueFd BSessionBackup::GetLocalCapabilities()
122 {
123 HILOGI("GetLocalCapabilities begin");
124 auto proxy = ServiceClient::GetInstance();
125 if (proxy == nullptr) {
126 HILOGE("Failed to get backup service");
127 return UniqueFd(-EPERM);
128 }
129 int fdvalue = INVALID_FD;
130 proxy->GetLocalCapabilitiesForBundleInfos(fdvalue);
131 UniqueFd fd(fdvalue);
132 if (fd < 0) {
133 HILOGE("Failed to get local capabilities for bundleinfos");
134 return UniqueFd(-EPERM);
135 }
136 return fd;
137 }
138
GetBackupDataSize(bool isPreciseScan,vector<BIncrementalData> bundleNameList)139 ErrCode BSessionBackup::GetBackupDataSize(bool isPreciseScan, vector<BIncrementalData> bundleNameList)
140 {
141 HILOGI("GetBackupDataSize Begin");
142 auto proxy = ServiceClient::GetInstance();
143 if (proxy == nullptr) {
144 HILOGE("Failed to get backup service");
145 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
146 }
147 ErrCode err = proxy->GetBackupDataSize(isPreciseScan, bundleNameList);
148 if (err != ERR_OK) {
149 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to GetBackupDataSize").GetCode();
150 }
151 HILOGI("GetBackupDataSize end");
152 return ERR_OK;
153 }
154
AppendBundles(vector<BundleName> bundlesToBackup)155 ErrCode BSessionBackup::AppendBundles(vector<BundleName> bundlesToBackup)
156 {
157 auto proxy = ServiceClient::GetInstance();
158 if (proxy == nullptr) {
159 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
160 }
161
162 int32_t res = proxy->AppendBundlesBackupSession(bundlesToBackup);
163 if (res != ERR_OK) {
164 std::string ss;
165 for (const auto &bundleName:bundlesToBackup) {
166 ss += bundleName + ", ";
167 }
168 AppRadar::Info info(ss.c_str(), "", "");
169 AppRadar::GetInstance().RecordBackupFuncRes(info, "BSessionBackup::AppendBundles",
170 AppRadar::GetInstance().GetUserId(), BizStageBackup::BIZ_STAGE_APPEND_BUNDLES_FAIL, res);
171 }
172 return res;
173 }
174
AppendBundles(vector<BundleName> bundlesToBackup,vector<std::string> detailInfos)175 ErrCode BSessionBackup::AppendBundles(vector<BundleName> bundlesToBackup, vector<std::string> detailInfos)
176 {
177 auto proxy = ServiceClient::GetInstance();
178 if (proxy == nullptr) {
179 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
180 }
181
182 int32_t res = proxy->AppendBundlesDetailsBackupSession(bundlesToBackup, detailInfos);
183 if (res != ERR_OK) {
184 std::string ss;
185 for (const auto &bundleName:bundlesToBackup) {
186 ss += bundleName + ", ";
187 }
188 AppRadar::Info info(ss.c_str(), "", "AppendBundles with infos");
189 AppRadar::GetInstance().RecordBackupFuncRes(info, "BSessionBackup::AppendBundles",
190 AppRadar::GetInstance().GetUserId(), BizStageBackup::BIZ_STAGE_APPEND_BUNDLES_FAIL, res);
191 }
192 return res;
193 }
194
Finish()195 ErrCode BSessionBackup::Finish()
196 {
197 auto proxy = ServiceClient::GetInstance();
198 if (proxy == nullptr) {
199 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
200 }
201
202 return proxy->Finish();
203 }
204
Release()205 ErrCode BSessionBackup::Release()
206 {
207 auto proxy = ServiceClient::GetInstance();
208 if (proxy == nullptr) {
209 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
210 }
211
212 return proxy->Release();
213 }
214
Cancel(std::string bundleName)215 ErrCode BSessionBackup::Cancel(std::string bundleName)
216 {
217 ErrCode result = BError::BackupErrorCode::E_CANCEL_UNSTARTED_TASK;
218 auto proxy = ServiceClient::GetInstance();
219 if (proxy == nullptr) {
220 HILOGE("Called Cancel, failed to get proxy.");
221 return result;
222 }
223
224 proxy->CancelForResult(bundleName, result);
225 return result;
226 }
227
CleanBundleTempDir(const std::string & bundleName)228 ErrCode BSessionBackup::CleanBundleTempDir(const std::string &bundleName)
229 {
230 HILOGI("BSessionBackup::CleanBundleTempDir");
231 auto proxy = ServiceClient::GetInstance();
232 if (proxy == nullptr) {
233 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
234 }
235 return proxy->CleanBundleTempDir(bundleName);
236 }
237
GetCompatibilityInfo(const std::string & bundleName,const std::string & extInfo,std::string & compatInfo)238 ErrCode BSessionBackup::GetCompatibilityInfo(const std::string &bundleName, const std::string &extInfo,
239 std::string &compatInfo)
240 {
241 HILOGI("BSessionBackup::GetCompatibilityInfo");
242 auto proxy = ServiceClient::GetInstance();
243 if (proxy == nullptr) {
244 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
245 }
246 return proxy->GetCompatibilityInfo(bundleName, extInfo, compatInfo);
247 }
248 } // namespace OHOS::FileManagement::Backup