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