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