1 /*
2  * Copyright (c) 2022 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 #ifndef OHOS_DISTRIBUTED_DATA_FRAMEWORKS_KVDB_BACKUP_MANAGER_H
16 #define OHOS_DISTRIBUTED_DATA_FRAMEWORKS_KVDB_BACKUP_MANAGER_H
17 #include <map>
18 #include <string>
19 #include <vector>
20 #include "kv_store_nb_delegate.h"
21 #include "security_manager.h"
22 #include "store_errno.h"
23 #include "store_util.h"
24 #include "task_scheduler.h"
25 #include "task_executor.h"
26 namespace OHOS::DistributedKv {
27 class BackupManager {
28 public:
29     using DBStore = DistributedDB::KvStoreNbDelegate;
30     using DBPassword = DistributedKv::SecurityManager::DBPassword;
31     struct ResidueInfo {
32         size_t tmpBackupSize;
33         size_t tmpKeySize;
34         bool hasRawBackup;
35         bool hasTmpBackup;
36         bool hasRawKey;
37         bool hasTmpKey;
38     };
39     enum ClearType {
40         DO_NOTHING = 0,
41         ROLLBACK_DATA,
42         ROLLBACK_KEY,
43         ROLLBACK,
44         CLEAN_TMP,
45     };
46     static BackupManager &GetInstance();
47     void Init(const std::string &baseDir);
48     void Prepare(const std::string &path, const std::string &storeId);
49     Status Backup(const std::string &name, const std::string &baseDir,
50         const std::string &storeId, std::shared_ptr<DBStore> dbStore);
51     Status Restore(const std::string &name, const std::string &baseDir,
52         const std::string &appId, const std::string &storeId, std::shared_ptr<DBStore> dbStore);
53     Status DeleteBackup(std::map<std::string, Status> &deleteList,
54         const std::string &baseDir, const std::string &storeId);
55 private:
56     BackupManager();
57     ~BackupManager();
58 
59     void KeepData(const std::string &name, bool isCreated);
60     void RollBackData(const std::string &name, bool isCreated);
61     void CleanTmpData(const std::string &name);
62     StoreUtil::FileInfo GetBackupFileInfo(const std::string &name,
63         const std::string &baseDir, const std::string &storeId);
64     DBPassword GetRestorePassword(const std::string &name, const std::string &baseDir,
65         const std::string &appId, const std::string &storeId);
66     bool HaveResidueFile(const std::vector<StoreUtil::FileInfo> &files);
67     bool HaveResidueKey(const std::vector<StoreUtil::FileInfo> &files, std::string storeId);
68     std::string GetBackupName(const std::string &fileName);
69     void SetResidueInfo(ResidueInfo &residueInfo, const std::vector<StoreUtil::FileInfo> &files,
70         const std::string &name, const std::string &postFix);
71     std::map<std::string, ResidueInfo> BuildResidueInfo(const std::vector<StoreUtil::FileInfo> &files,
72         const std::vector<StoreUtil::FileInfo> &keys, const std::string &storeId);
73     ClearType GetClearType(const ResidueInfo &residueInfo);
74     void ClearResidueFile(std::map<std::string, ResidueInfo> residueInfo,
75         const std::string &baseDir, const std::string &storeId);
76     bool IsEndWith(const std::string &fullString, const std::string &end);
77     bool IsBeginWith(const std::string &fullString, const std::string &begin);
78 
79     static constexpr int MAX_BACKUP_NUM = 5;
80 };
81 } // namespace OHOS::DistributedKv
82 #endif // OHOS_DISTRIBUTED_DATA_FRAMEWORKS_KVDB_BACKUP_MANAGER_H
83