• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef OHOS_FILEMGMT_BACKUP_BACKUP_EXT_EXTENSION_H
17 #define OHOS_FILEMGMT_BACKUP_BACKUP_EXT_EXTENSION_H
18 
19 #include <chrono>
20 #include <shared_mutex>
21 #include <string>
22 #include <vector>
23 #include <tuple>
24 
25 #include <sys/stat.h>
26 
27 #include "b_json/b_json_entity_extension_config.h"
28 #include "b_json/b_json_entity_ext_manage.h"
29 #include "b_json/b_report_entity.h"
30 #include "b_radar/b_radar.h"
31 #include "ext_backup_js.h"
32 #include "ext_extension_stub.h"
33 #include "i_service.h"
34 #include "tar_file.h"
35 #include "thread_pool.h"
36 #include "timer.h"
37 #include "unique_fd.h"
38 #include "untar_file.h"
39 
40 namespace OHOS::FileManagement::Backup {
41 using CompareFilesResult = tuple<map<string, struct ReportFileInfo>,
42                                  map<string, struct ReportFileInfo>,
43                                  map<string, struct ReportFileInfo>>;
44 class BackupExtExtension : public ExtExtensionStub {
45 public:
46     UniqueFd GetFileHandle(const std::string &fileName, int32_t &errCode) override;
47     ErrCode HandleClear() override;
48     ErrCode PublishFile(const std::string &fileName) override;
49     ErrCode HandleBackup(bool isClearData) override;
50     ErrCode HandleRestore(bool isClearData) override;
51     ErrCode GetIncrementalFileHandle(const std::string &fileName) override;
52     ErrCode PublishIncrementalFile(const std::string &fileName) override;
53     ErrCode HandleIncrementalBackup(UniqueFd incrementalFd, UniqueFd manifestFd) override;
54     ErrCode IncrementalOnBackup(bool isClearData) override;
55     std::tuple<UniqueFd, UniqueFd> GetIncrementalBackupFileHandle() override;
56     ErrCode GetBackupInfo(std::string &result) override;
57     ErrCode UpdateFdSendRate(std::string &bundleName, int32_t sendRate) override;
58     void AsyncTaskRestoreForUpgrade(void);
59     void ExtClear(void);
60     void AsyncTaskIncrementalRestoreForUpgrade(void);
61     ErrCode User0OnBackup() override;
62 
63 public:
BackupExtExtension(const std::shared_ptr<Backup::ExtBackup> & extension,const std::string & bundleName)64     explicit BackupExtExtension(const std::shared_ptr<Backup::ExtBackup> &extension,
65         const std::string &bundleName) : extension_(extension)
66     {
67         if (extension_ != nullptr) {
68             extension_->SetBackupExtExtension(this);
69         }
70         bundleName_ = bundleName;
71         threadPool_.Start(BConstants::EXTENSION_THREAD_POOL_COUNT);
72         onProcessTaskPool_.Start(BConstants::EXTENSION_THREAD_POOL_COUNT);
73         SetStagingPathProperties();
74     }
~BackupExtExtension()75     ~BackupExtExtension()
76     {
77         onProcessTimeoutTimer_.Shutdown();
78         threadPool_.Stop();
79         onProcessTaskPool_.Stop();
80         if (callJsOnProcessThread_.joinable()) {
81             callJsOnProcessThread_.join();
82         }
83     }
84 
85 private:
86     /**
87      * @brief verify caller uid
88      *
89      */
90     void VerifyCaller();
91 
92     /**
93      * @brief backup
94      *
95      * @param usrConfig user configure
96      */
97     int DoBackup(const BJsonEntityExtensionConfig &usrConfig);
98 
99     /**
100      * @brief restore
101      *
102      * @param fileName name of the file that to be untar
103      */
104     int DoRestore(const string &fileName, const off_t fileSize);
105 
106     /**
107      * @brief incremental restore
108      *
109      */
110     int DoIncrementalRestore();
111 
112     /** @brief clear backup restore data */
113     void DoClear();
114 
115     /**
116      * @brief extension backup restore is done
117      *
118      * @param errCode
119      */
120     void AppDone(ErrCode errCode);
121 
122     /**
123      * @brief extension backup restore is done
124      *
125      * @param restoreRetInfo app restore reportInfo
126      */
127     void AppResultReport(const std::string restoreRetInfo, BackupRestoreScenario scenario,
128         ErrCode errCode = 0);
129 
130     /**
131      * @brief extension process Info
132      *
133      * @param restoreRetInfo app processInfo
134      * @param scenario backup or restore
135      */
136     void ReportAppProcessInfo(const std::string processInfo, BackupRestoreScenario scenario);
137 
138     /**
139      * @brief Executing Backup Tasks Asynchronously
140      *
141      * @param extAction action
142      *
143      * @param config user configure
144      */
145     void AsyncTaskBackup(const std::string config);
146 
147     /**
148      * @brief Executing Restoration Tasks Asynchronously
149      *
150      */
151     void AsyncTaskRestore(std::set<std::string> fileSet, const std::vector<ExtManageInfo> extManageInfo);
152 
153     /**
154      * @brief Executing Incremental Restoration Tasks Asynchronously
155      *
156      */
157     void AsyncTaskIncrementalRestore();
158 
159     /**
160      * @brief Executing Incremental Restoration Tasks Asynchronously for special clone & cloud
161      *
162      */
163     void AsyncTaskIncreRestoreSpecialVersion();
164 
165     void AsyncTaskOnBackup();
166 
167     bool IfAllowToBackupRestore();
168 
169     void AsyncTaskUser0Backup();
170 
171     void DoUser0Backup(const BJsonEntityExtensionConfig &usrConfig);
172 
173     int User0DoBackup(const BJsonEntityExtensionConfig &usrConfig);
174 
175     int DoIncrementalBackup(const std::vector<struct ReportFileInfo> &allFiles,
176                             const std::vector<struct ReportFileInfo> &smallFiles,
177                             const std::vector<struct ReportFileInfo> &bigFiles);
178 
179     void CompareFiles(UniqueFd incrementalFd,
180                       UniqueFd manifestFd,
181                       vector<struct ReportFileInfo> &allFiles,
182                       vector<struct ReportFileInfo> &smallFiles,
183                       vector<struct ReportFileInfo> &bigFiles);
184 
185     void AsyncTaskDoIncrementalBackup(UniqueFd incrementalFd, UniqueFd manifestFd);
186     void AsyncTaskOnIncrementalBackup();
187     int DoIncrementalBackupTask(UniqueFd incrementalFd, UniqueFd manifestFd);
188     ErrCode IncrementalBigFileReady(const TarMap &pkgInfo, const vector<struct ReportFileInfo> &bigInfos,
189         sptr<IService> proxy);
190     ErrCode BigFileReady(const TarMap &bigFileInfo, sptr<IService> proxy);
191     void WaitToSendFd(std::chrono::system_clock::time_point &startTime, int &fdSendNum);
192     void RefreshTimeInfo(std::chrono::system_clock::time_point &startTime, int &fdSendNum);
193     void IncrementalPacket(const vector<struct ReportFileInfo> &infos, TarMap &tar, sptr<IService> proxy);
194     void DoPacket(const map<string, size_t> &srcFiles, TarMap &tar, sptr<IService> proxy);
195     void CheckTmpDirFileInfos(bool isSpecialVersion = false);
196     std::map<std::string, off_t> GetIdxFileInfos(bool isSpecialVersion = false);
197     tuple<bool, vector<string>> CheckRestoreFileInfos();
198     /**
199      * @brief extension incremental backup restore is done
200      *
201      * @param errCode
202      */
203     void AppIncrementalDone(ErrCode errCode);
204 
205     /**
206      * @brief start extension timer by ipc
207      *
208      * @param result
209      */
210     void StartExtTimer(bool &isExtStart);
211 
212     /**
213      * @brief start fwk timer by ipc
214      *
215      * @param errCode
216      */
217     void StartFwkTimer(bool &isFwkStart);
218 
219     /**
220      * @brief get callbackEx for execute onRestore
221      *
222      * @param obj
223      */
224     std::function<void(ErrCode, const std::string)> IncreOnRestoreExCallback(wptr<BackupExtExtension> obj);
225 
226     /**
227      * @brief get increCallback for execute onRestore with string param
228      *
229      * @param obj
230      */
231     std::function<void(ErrCode, const std::string)> IncreOnRestoreCallback(wptr<BackupExtExtension> obj);
232 
233     /**
234      * @brief get callback for execute onRestore with string param
235      *
236      * @param obj
237      */
238     std::function<void(ErrCode, std::string)> OnRestoreCallback(wptr<BackupExtExtension> obj);
239 
240     /**
241      * @brief get increCallbackEx for execute onRestore with string param
242      *
243      * @param obj
244      */
245     std::function<void(ErrCode, std::string)> OnRestoreExCallback(wptr<BackupExtExtension> obj);
246 
247     /**
248      * @brief get callbackEx for execute appDone
249      */
250     std::function<void(ErrCode, std::string)> AppDoneCallbackEx(wptr<BackupExtExtension> obj);
251 
252     std::function<void(ErrCode, const std::string)> IncOnBackupExCallback(wptr<BackupExtExtension> obj);
253     std::function<void(ErrCode, const std::string)> IncOnBackupCallback(wptr<BackupExtExtension> obj);
254 
255     std::function<void(ErrCode, const std::string)> OnBackupExCallback(wptr<BackupExtExtension> obj);
256     std::function<void(ErrCode, const std::string)> OnBackupCallback(wptr<BackupExtExtension> obj);
257 
258     void HandleSpecialVersionRestore();
259     void DeleteBackupIncrementalIdxFile();
260     void DeleteBackupIdxFile();
261     void DeleteBackupIncrementalTars(const string &tarName);
262     void SetClearDataFlag(bool isClearData);
263     std::string GetBundlePath();
264     std::vector<ExtManageInfo> GetExtManageInfo();
265     ErrCode RestoreFilesForSpecialCloneCloud();
266     void RestoreBigFilesForSpecialCloneCloud(const ExtManageInfo &item);
267     ErrCode RestoreTarForSpecialCloneCloud(const ExtManageInfo &item);
268     void RestoreBigFiles(bool appendTargetPath);
269     void FillEndFileInfos(const std::string &path, const unordered_map<string, struct ReportFileInfo> &result);
270     void RestoreBigFileAfter(const string &filePath, const struct stat &sta);
271     void DealIncreUnPacketResult(const off_t tarFileSize, const std::string &tarFileName,
272         const std::tuple<int, EndFileInfo, ErrFileInfo> &result);
273 
274     ErrCode StartOnProcessTaskThread(wptr<BackupExtExtension> obj, BackupRestoreScenario scenario);
275     void FinishOnProcessTask();
276     void ExecCallOnProcessTask(wptr<BackupExtExtension> obj, BackupRestoreScenario scenario);
277     void AsyncCallJsOnProcessTask(wptr<BackupExtExtension> obj, BackupRestoreScenario scenario);
278     void SyncCallJsOnProcessTask(wptr<BackupExtExtension> obj, BackupRestoreScenario scenario);
279     void StartOnProcessTimeOutTimer(wptr<BackupExtExtension> obj, BackupRestoreScenario scenario);
280     void CloseOnProcessTimeOutTimer();
281     void UpdateOnStartTime();
282     int32_t GetOnStartTimeCost();
283     bool SetStagingPathProperties();
284 
285     std::function<void(std::string, int)> ReportErrFileByProc(wptr<BackupExtExtension> obj,
286         BackupRestoreScenario scenario);
287     ErrCode GetIncreFileHandleForNormalVersion(const std::string &fileName);
288     void RestoreOneBigFile(const std::string &path, const ExtManageInfo &item, const bool appendTargetPath);
289     int DealIncreRestoreBigAndTarFile();
290 
291 private:
292     std::shared_mutex lock_;
293     std::shared_ptr<ExtBackup> extension_;
294     std::string backupInfo_;
295     OHOS::ThreadPool threadPool_;
296     std::mutex updateSendRateLock_;
297     std::condition_variable startSendFdRateCon_;
298     std::condition_variable waitSendFdCon_;
299     std::mutex startSendMutex_;
300     std::mutex waitTimeLock_;
301     std::string bundleName_;
302     int32_t sendRate_ = BConstants::DEFAULT_FD_SEND_RATE;
303     bool isClearData_ {true};
304     bool isDebug_ {true};
305     std::map<std::string, off_t> endFileInfos_;
306     std::map<std::string, std::vector<ErrCode>> errFileInfos_;
307     bool isRpValid_ {false};
308 
309     std::thread callJsOnProcessThread_;
310     Utils::Timer onProcessTimeoutTimer_ {"onProcessTimeoutTimer_"};
311     uint32_t onProcessTimeoutTimerId_ { 0 };
312     std::atomic<int> onProcessTimeoutCnt_ { 0 };
313     std::atomic<bool> stopCallJsOnProcess_ {false};
314     std::condition_variable execOnProcessCon_;
315     std::mutex onProcessLock_;
316     std::atomic<bool> onProcessTimeout_ {false};
317     std::chrono::time_point<std::chrono::system_clock> g_onStart;
318     std::mutex onStartTimeLock_;
319     AppRadar::DoRestoreInfo radarRestoreInfo_ { 0 };
320     OHOS::ThreadPool onProcessTaskPool_;
321     std::atomic<bool> isFirstCallOnProcess_ {false};
322     std::atomic<bool> isExecAppDone_ {false};
323     BackupRestoreScenario curScenario_ { BackupRestoreScenario::FULL_BACKUP };
324 };
325 } // namespace OHOS::FileManagement::Backup
326 
327 #endif // OHOS_FILEMGMT_BACKUP_BACKUP_EXT_EXTENSION_H