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 const char* BACKUP_DEBUG_STATE = "sys.backup.check.enable";
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 char configParam[] = "false";
41 int length = GetParameter(key.c_str(), "", configParam, len + 1);
42 if (length <= 0) {
43 HILOGE("Fail to GetParameter name = %{public}s, length = %{public}d", key.c_str(), length);
44 return {false, ""};
45 }
46 return {true, configParam};
47 }
48
GetBackupDebugOverrideExtensionConfig()49 bool BackupPara::GetBackupDebugOverrideExtensionConfig()
50 {
51 auto [getCfgParaValSucc, value] = GetConfigParameterValue(BConstants::BACKUP_DEBUG_OVERRIDE_EXTENSION_CONFIG_KEY,
52 BConstants::BACKUP_PARA_VALUE_MAX);
53 if (!getCfgParaValSucc) {
54 HILOGE("Fail to get configuration parameter value of backup.para, return default value");
55 return BConstants::BACKUP_DEBUG_OVERRIDE_EXTENSION_CONFIG_DEFAULT_VALUE;
56 }
57 return value == "true";
58 }
59
GetBackupOverrideBackupSARelease()60 bool BackupPara::GetBackupOverrideBackupSARelease()
61 {
62 auto [getCfgParaValSucc, value] =
63 GetConfigParameterValue(BConstants::BACKUP_OVERRIDE_BACKUP_SA_RELEASE_KEY, BConstants::BACKUP_PARA_VALUE_MAX);
64 if (!getCfgParaValSucc) {
65 HILOGE("Fail to get configuration parameter value of backup.para, return default value");
66 return BConstants::BACKUP_DEBUG_OVERRIDE_BACKUP_SA_RELEASE_DEFAULT_VALUE;
67 }
68 return value == "true";
69 }
70
GetBackupOverrideIncrementalRestore()71 bool BackupPara::GetBackupOverrideIncrementalRestore()
72 {
73 auto [getCfgParaValSucc, value] =
74 GetConfigParameterValue(BConstants::BACKUP_OVERRIDE_INCREMENTAL_KEY, BConstants::BACKUP_PARA_VALUE_MAX);
75 if (!getCfgParaValSucc) {
76 HILOGE("Fail to get configuration parameter value of backup.para, return default value");
77 return BConstants::BACKUP_DEBUG_OVERRIDE_INCREMENTAL_DEFAULT_VALUE;
78 }
79 HILOGI("Get Parse IncrementalRestore result, value: %{public}s", value.c_str());
80 return value == "true";
81 }
82
GetBackupDebugOverrideAccount()83 tuple<bool, int32_t> BackupPara::GetBackupDebugOverrideAccount()
84 {
85 auto [getCfgParaValSucc, value] = GetConfigParameterValue(BConstants::BACKUP_DEBUG_OVERRIDE_ACCOUNT_CONFIG_KEY,
86 BConstants::BACKUP_PARA_VALUE_MAX);
87 if (!getCfgParaValSucc) {
88 return {false, 0};
89 }
90 if (value == "true") {
91 auto [getCfgParaValSucc, value] = GetConfigParameterValue(BConstants::BACKUP_DEBUG_OVERRIDE_ACCOUNT_NUMBER_KEY,
92 BConstants::BACKUP_PARA_VALUE_MAX);
93 if (!getCfgParaValSucc) {
94 return {false, 0};
95 }
96 return {true, stoi(value)};
97 }
98 return {false, 0};
99 }
100
GetBackupDebugState()101 bool BackupPara::GetBackupDebugState()
102 {
103 char paraValue[30] = {0}; // 30: for system paramter
104 auto res = GetParameter(BACKUP_DEBUG_STATE, "-1", paraValue, sizeof(paraValue));
105 if (res <= 0) {
106 HILOGE("GetParameter fail, key:%{public}s res:%{public}d", BACKUP_DEBUG_STATE, res);
107 return false;
108 }
109 std::string result(paraValue);
110 return result == "true";
111 }
112 } // namespace OHOS::FileManagement::Backup