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 16 #ifndef OHOS_DISTRIBUTED_DATA_SERVICES_BACKUP_BACKUP_MANAGER_H 17 #define OHOS_DISTRIBUTED_DATA_SERVICES_BACKUP_BACKUP_MANAGER_H 18 19 #include "executor_pool.h" 20 #include "metadata/secret_key_meta_data.h" 21 #include "metadata/store_meta_data.h" 22 #include "types.h" 23 24 namespace OHOS::DistributedData { 25 class BackupManager { 26 public: 27 using Exporter = std::function<void(const StoreMetaData &, const std::string &, bool &)>; 28 struct BackupParam { 29 int64_t schedularDelay; 30 int64_t schedularInternal; 31 int64_t backupInternal; 32 int64_t backupNumber; 33 }; 34 enum ClearType { 35 DO_NOTHING = 0, 36 ROLLBACK, 37 CLEAN_DATA, 38 }; 39 static BackupManager &GetInstance(); 40 void Init(); 41 void BackSchedule(std::shared_ptr<ExecutorPool> executors); 42 void SetBackupParam(const BackupParam &backupParam); 43 void RegisterExporter(int32_t type, Exporter exporter); 44 std::vector<uint8_t> GetPassWord(const StoreMetaData &meta); 45 46 private: 47 BackupManager(); 48 ~BackupManager(); 49 ClearType GetClearType(const StoreMetaData &meta); 50 bool CanBackup(); 51 void DoBackup(const StoreMetaData &meta); 52 void CopyFile(const std::string &oldPath, const std::string &newPath, bool isCreate = false); 53 void KeepData(const std::string &path); 54 void SaveData(const std::string &path, const std::string &key, const SecretKeyMetaData &secretKey); 55 void CleanData(const std::string &path); 56 void RollBackData(const std::string &path); 57 bool IsFileExist(const std::string &path); 58 bool RemoveFile(const std::string &path); 59 60 static constexpr int MAX_STORE_TYPE = 20; 61 Exporter exporters_[MAX_STORE_TYPE]; 62 int64_t schedularDelay_; 63 int64_t schedularInternal_; 64 int64_t backupInternal_; 65 int64_t backupSuccessTime_ = 0; 66 int64_t backupNumber_ = 0; 67 int64_t startNum_ = 0; 68 std::shared_ptr<ExecutorPool> executors_; 69 }; 70 } // namespace OHOS::DistributedData 71 #endif // OHOS_DISTRIBUTED_DATA_SERVICES_BACKUP_BACKUP_MANAGER_H