1 /*
2  * Copyright (c) 2023-2024 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 INTERFACES_KITS_JS_SRC_MOD_BACKUP_INCREMENTAL_BACKUP_DATA_H
16 #define INTERFACES_KITS_JS_SRC_MOD_BACKUP_INCREMENTAL_BACKUP_DATA_H
17 
18 #include <memory>
19 #include <node_api.h>
20 #include <string>
21 #include <tuple>
22 
23 #include "b_resources/b_constants.h"
24 #include "filemgmt_libn.h"
25 
26 namespace OHOS::FileManagement::Backup {
27 struct IncrementalBackupTime {
IncrementalBackupTimeIncrementalBackupTime28     explicit IncrementalBackupTime(const LibN::NVal &data)
29     {
30         LibN::NVal name = data.GetProp(BConstants::BUNDLE_NAME);
31         if (name.val_ != nullptr) {
32             auto [succ, str, ignore] = name.ToUTF8String();
33             if (succ) {
34                 bundleName = std::string(str.get());
35             }
36         }
37 
38         LibN::NVal time = data.GetProp(BConstants::LAST_INCREMENTAL_TIME);
39         if (time.val_ != nullptr) {
40             auto [succ, tm] = time.ToInt64();
41             if (succ) {
42                 lastIncrementalTime = tm;
43             }
44         }
45     }
46     std::string bundleName = "";
47     int64_t lastIncrementalTime = 0;
48 };
49 
50 struct FileManifestData {
FileManifestDataFileManifestData51     explicit FileManifestData(const LibN::NVal &data)
52     {
53         LibN::NVal fd = data.GetProp(BConstants::MANIFEST_FD);
54         if (fd.val_ != nullptr) {
55             auto [succ, tmp] = fd.ToInt32();
56             if (succ) {
57                 manifestFd = tmp;
58             }
59         }
60     }
61     int32_t manifestFd = -1;
62 };
63 
64 struct BackupParams {
BackupParamsBackupParams65     explicit BackupParams(const LibN::NVal &data)
66     {
67         LibN::NVal para = data.GetProp(BConstants::PARAMETERS);
68         if (para.val_ != nullptr) {
69             auto [succ, str, ignore] = para.ToUTF8String();
70             if (succ) {
71                 parameters = std::string(str.get());
72             }
73         }
74     }
75     std::string parameters = "";
76 };
77 
78 struct BackupPriority {
BackupPriorityBackupPriority79     explicit BackupPriority(const LibN::NVal &data)
80     {
81         LibN::NVal pr = data.GetProp(BConstants::PRIORITY);
82         if (pr.val_ != nullptr) {
83             auto [succ, tmp] = pr.ToInt32();
84             if (succ) {
85                 priority = tmp;
86             }
87         }
88     }
89     int32_t priority = -1;
90 };
91 
92 struct IncrementalBackupData
93     : public IncrementalBackupTime, public FileManifestData, public BackupParams, public BackupPriority {
IncrementalBackupDataIncrementalBackupData94     explicit IncrementalBackupData(const LibN::NVal &data)
95         : IncrementalBackupTime(data),
96           FileManifestData(data),
97           BackupParams(data),
98           BackupPriority(data) {};
99 };
100 } // namespace OHOS::FileManagement::Backup
101 #endif // INTERFACES_KITS_JS_SRC_MOD_BACKUP_INCREMENTAL_BACKUP_DATA_H