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