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