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_json/b_json_entity_extension_config.h"
17
18 #include <any>
19 #include <fcntl.h>
20 #include <string_view>
21 #include <sys/types.h>
22 #include <unistd.h>
23
24 #include "b_error/b_error.h"
25 #include "b_filesystem/b_file.h"
26 #include "b_ohos/startup/backup_para.h"
27 #include "b_resources/b_constants.h"
28 #include "filemgmt_libhilog.h"
29
30 namespace OHOS::FileManagement::Backup {
31 using namespace std;
32
GetIncludes() const33 vector<string> BJsonEntityExtensionConfig::GetIncludes() const
34 {
35 if (!obj_) {
36 HILOGE("Uninitialized JSon Object reference");
37 return {BConstants::PATHES_TO_BACKUP.begin(), BConstants::PATHES_TO_BACKUP.end()};
38 }
39 if (!obj_.isMember("includes")) {
40 HILOGE("'includes' field not found");
41 return {BConstants::PATHES_TO_BACKUP.begin(), BConstants::PATHES_TO_BACKUP.end()};
42 }
43 if (!obj_["includes"].isArray()) {
44 HILOGE("'includes' field must be an array");
45 return {BConstants::PATHES_TO_BACKUP.begin(), BConstants::PATHES_TO_BACKUP.end()};
46 }
47
48 vector<string> dirs;
49 for (auto &&item : obj_["includes"]) {
50 if (!item.isString() || item.empty()) {
51 HILOGE("Each item of array 'includes' must be of the type string");
52 continue;
53 }
54 dirs.push_back(item.asString());
55 }
56
57 if (dirs.empty()) {
58 dirs.emplace_back("");
59 }
60 return dirs;
61 }
62
GetExcludes() const63 vector<string> BJsonEntityExtensionConfig::GetExcludes() const
64 {
65 if (!obj_) {
66 HILOGE("Uninitialized JSon Object reference");
67 return {};
68 }
69 if (!obj_.isMember("excludes")) {
70 HILOGE("'excludes' field not found");
71 return {};
72 }
73 if (!obj_["excludes"].isArray()) {
74 HILOGE("'excludes' field must be an array");
75 return {};
76 }
77
78 vector<string> dirs;
79 for (auto &&item : obj_["excludes"]) {
80 if (!item.isString() || item.empty()) {
81 HILOGE("Each item of array 'excludes' must be of the type string");
82 continue;
83 }
84 dirs.push_back(item.asString());
85 }
86 return dirs;
87 }
88
GetAllowToBackupRestore() const89 bool BJsonEntityExtensionConfig::GetAllowToBackupRestore() const
90 {
91 if (!obj_ || !obj_.isMember("allowToBackupRestore") || !obj_["allowToBackupRestore"].isBool()) {
92 HILOGE("Failed to init field allowToBackupRestore");
93 return false;
94 }
95
96 return obj_["allowToBackupRestore"].asBool();
97 }
98
GetFullBackupOnly() const99 bool BJsonEntityExtensionConfig::GetFullBackupOnly() const
100 {
101 if (!obj_ || !obj_.isMember("fullBackupOnly") || !obj_["fullBackupOnly"].isBool()) {
102 HILOGE("Failed to init field fullBackupOnly");
103 return false;
104 }
105
106 return obj_["fullBackupOnly"].asBool();
107 }
108
GetSupportScene() const109 string BJsonEntityExtensionConfig::GetSupportScene() const
110 {
111 if (!obj_ || !obj_.isMember("supportScene") || !obj_["supportScene"].isString()) {
112 HILOGE("Failed to init field supportScene");
113 return "";
114 }
115
116 return obj_["supportScene"].asString();
117 }
118
GetJSonSource(string_view jsonFromRealWorld,any option)119 string BJsonEntityExtensionConfig::GetJSonSource(string_view jsonFromRealWorld, any option)
120 {
121 if (!BackupPara().GetBackupDebugOverrideExtensionConfig()) {
122 return string(jsonFromRealWorld);
123 }
124
125 /* Server for test case, the user function does not execute the following code. */
126 if (!option.has_value()) {
127 if (getuid() == static_cast<uid_t>(BConstants::BACKUP_UID)) {
128 throw BError(BError::Codes::SA_INVAL_ARG, "Current process is not extension process");
129 }
130 string jsonFilePath = string(BConstants::BACKUP_CONFIG_EXTENSION_PATH).append(BConstants::BACKUP_CONFIG_JSON);
131 return BFile::ReadFile(UniqueFd(open(jsonFilePath.c_str(), O_RDONLY))).get();
132 }
133
134 if (getuid() != static_cast<uid_t>(BConstants::BACKUP_UID)) {
135 throw BError(BError::Codes::SA_INVAL_ARG, "Current process is not service process");
136 }
137 string bundleName;
138 try {
139 bundleName = any_cast<string>(option);
140 } catch (const bad_any_cast &e) {
141 throw BError(BError::Codes::SA_INVAL_ARG, e.what());
142 }
143 string jsonFilePath = string(BConstants::GetSaBundleBackupRootDir(BConstants::DEFAULT_USER_ID)).
144 append(bundleName).
145 append("/").
146 append(BConstants::BACKUP_CONFIG_JSON);
147 if (access(jsonFilePath.c_str(), F_OK) != 0) {
148 HILOGI("Failed to access jsonFilePath : %{public}s", jsonFilePath.c_str());
149 return string(jsonFromRealWorld);
150 }
151 return BFile::ReadFile(UniqueFd(open(jsonFilePath.c_str(), O_RDONLY))).get();
152 }
153
GetRestoreDeps() const154 string BJsonEntityExtensionConfig::GetRestoreDeps() const
155 {
156 if (!obj_ || !obj_.isMember("restoreDeps") || !obj_["restoreDeps"].isString()) {
157 HILOGE("Failed to init field restoreDeps");
158 return "";
159 }
160
161 return obj_["restoreDeps"].asString();
162 }
163
164 } // namespace OHOS::FileManagement::Backup
165