1 /*
2 * Copyright (c) 2022-2023 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 #include "b_ohos/startup/backup_para.h"
17
18 #include <cstdint>
19 #include <memory>
20 #include <string>
21 #include <tuple>
22
23 #include "b_error/b_error.h"
24 #include "b_resources/b_constants.h"
25 #include "filemgmt_libhilog.h"
26 #include "parameter.h"
27
28 namespace OHOS::FileManagement::Backup {
29 using namespace std;
30
31 /**
32 * @brief 获取配置参数的值
33 *
34 * @param key 配置参数的参数名
35 * @param len 配置参数值的最大长度
36 * @return 成功获取配置参数的值则返回true,失败则返回false;以及表示配置参数值的字符串
37 */
GetConfigParameterValue(const string & key,uint32_t len)38 static tuple<bool, string> GetConfigParameterValue(const string &key, uint32_t len)
39 {
40 int handle = static_cast<int>(FindParameter(key.c_str()));
41 if (handle == -1) {
42 HILOGI("Fail to find parameter.");
43 return {false, ""};
44 }
45 try {
46 unique_ptr<char[]> buffer = make_unique<char[]>(len + 1);
47 int res = GetParameterValue(handle, buffer.get(), len + 1);
48 if (res < 0) {
49 HILOGI("Fail to get parameter value.");
50 return {false, ""};
51 }
52 return {true, buffer.get()};
53 } catch (const bad_alloc &e) {
54 HILOGI("Fail to get parameter value: %{public}s.", e.what());
55 return {false, ""};
56 }
57 }
58
GetBackupDebugOverrideExtensionConfig()59 bool BackupPara::GetBackupDebugOverrideExtensionConfig()
60 {
61 auto [getCfgParaValSucc, value] = GetConfigParameterValue(BConstants::BACKUP_DEBUG_OVERRIDE_EXTENSION_CONFIG_KEY,
62 BConstants::BACKUP_PARA_VALUE_MAX);
63 if (!getCfgParaValSucc) {
64 throw BError(BError::Codes::SA_INVAL_ARG, "Fail to get configuration parameter value of backup.para");
65 }
66 return value == "true";
67 }
68
GetBackupOverrideBackupSARelease()69 bool BackupPara::GetBackupOverrideBackupSARelease()
70 {
71 auto [getCfgParaValSucc, value] =
72 GetConfigParameterValue(BConstants::BACKUP_OVERRIDE_BACKUP_SA_RELEASE_KEY, BConstants::BACKUP_PARA_VALUE_MAX);
73 if (!getCfgParaValSucc) {
74 throw BError(BError::Codes::SA_INVAL_ARG, "Fail to get configuration parameter value of backup.para");
75 }
76 return value == "true";
77 }
78
GetBackupOverrideIncrementalRestore()79 bool BackupPara::GetBackupOverrideIncrementalRestore()
80 {
81 auto [getCfgParaValSucc, value] =
82 GetConfigParameterValue(BConstants::BACKUP_OVERRIDE_INCREMENTAL_KEY, BConstants::BACKUP_PARA_VALUE_MAX);
83 if (!getCfgParaValSucc) {
84 throw BError(BError::Codes::SA_INVAL_ARG, "Fail to get configuration parameter value of backup.para");
85 }
86 return value == "true";
87 }
88
GetBackupDebugOverrideAccount()89 tuple<bool, int32_t> BackupPara::GetBackupDebugOverrideAccount()
90 {
91 auto [getCfgParaValSucc, value] = GetConfigParameterValue(BConstants::BACKUP_DEBUG_OVERRIDE_ACCOUNT_CONFIG_KEY,
92 BConstants::BACKUP_PARA_VALUE_MAX);
93 if (!getCfgParaValSucc) {
94 return {false, 0};
95 }
96 if (value == "true") {
97 auto [getCfgParaValSucc, value] = GetConfigParameterValue(BConstants::BACKUP_DEBUG_OVERRIDE_ACCOUNT_NUMBER_KEY,
98 BConstants::BACKUP_PARA_VALUE_MAX);
99 if (!getCfgParaValSucc) {
100 return {false, 0};
101 }
102 return {true, stoi(value)};
103 }
104 return {false, 0};
105 }
106 } // namespace OHOS::FileManagement::Backup