• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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()) {
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()) {
81             HILOGE("Each item of array 'excludes' must be of the type string");
82             return {};
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 
GetJSonSource(string_view jsonFromRealWorld,any option)99 string BJsonEntityExtensionConfig::GetJSonSource(string_view jsonFromRealWorld, any option)
100 {
101     if (!BackupPara().GetBackupDebugOverrideExtensionConfig()) {
102         return string(jsonFromRealWorld);
103     }
104 
105     /* Server for test case, the user function does not execute the following code. */
106     if (!option.has_value()) {
107         if (getuid() == static_cast<uid_t>(BConstants::BACKUP_UID)) {
108             throw BError(BError::Codes::SA_INVAL_ARG, "Current process is not extension process");
109         }
110         string jsonFilePath = string(BConstants::BACKUP_CONFIG_EXTENSION_PATH).append(BConstants::BACKUP_CONFIG_JSON);
111         return BFile::ReadFile(UniqueFd(open(jsonFilePath.c_str(), O_RDONLY))).get();
112     }
113 
114     if (getuid() != static_cast<uid_t>(BConstants::BACKUP_UID)) {
115         throw BError(BError::Codes::SA_INVAL_ARG, "Current process is not service process");
116     }
117     string bundleName;
118     try {
119         bundleName = any_cast<string>(option);
120     } catch (const bad_any_cast &e) {
121         throw BError(BError::Codes::SA_INVAL_ARG, e.what());
122     }
123     string jsonFilePath = string(BConstants::GetSaBundleBackupRootDir(BConstants::DEFAULT_USER_ID)).
124                                 append(bundleName).
125                                 append("/").
126                                 append(BConstants::BACKUP_CONFIG_JSON);
127     return BFile::ReadFile(UniqueFd(open(jsonFilePath.c_str(), O_RDONLY))).get();
128 }
129 } // namespace OHOS::FileManagement::Backup