1 /*
2 * Copyright (c) 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
16 #include "json_utils.h"
17 #include <fstream>
18 #include <sstream>
19 #include <unistd.h>
20 #include <regex>
21
22 #include "config_policy_utils.h"
23 #include "hilog_tag_wrapper.h"
24
25 namespace OHOS {
26 namespace AAFwk {
LoadConfiguration(const std::string & path,nlohmann::json & jsonBuf,const std::string & defaultPath)27 bool JsonUtils::LoadConfiguration(const std::string& path, nlohmann::json& jsonBuf,
28 const std::string& defaultPath)
29 {
30 std::string configPath = GetConfigPath(path, defaultPath);
31 TAG_LOGD(AAFwkTag::ABILITYMGR, "config path is: %{public}s", configPath.c_str());
32 if (!ReadFileInfoJson(configPath, jsonBuf)) {
33 return false;
34 }
35 return true;
36 }
37
GetConfigPath(const std::string & path,const std::string & defaultPath)38 std::string JsonUtils::GetConfigPath(const std::string& path, const std::string& defaultPath)
39 {
40 char buf[MAX_PATH_LEN] = { 0 };
41 char *configPath = GetOneCfgFile(path.c_str(), buf, MAX_PATH_LEN);
42 if (configPath == nullptr || configPath[0] == '\0' || strlen(configPath) > MAX_PATH_LEN) {
43 return defaultPath;
44 }
45 return configPath;
46 }
47
ReadFileInfoJson(const std::string & filePath,nlohmann::json & jsonBuf)48 bool JsonUtils::ReadFileInfoJson(const std::string &filePath, nlohmann::json &jsonBuf)
49 {
50 if (access(filePath.c_str(), F_OK) != 0) {
51 TAG_LOGE(AAFwkTag::ABILITYMGR, "Deeplink reserve config not exist.");
52 return false;
53 }
54
55 if (filePath.empty()) {
56 TAG_LOGE(AAFwkTag::ABILITYMGR, "File path is empty.");
57 return false;
58 }
59
60 char path[PATH_MAX] = {0};
61 if (realpath(filePath.c_str(), path) == nullptr) {
62 TAG_LOGE(AAFwkTag::ABILITYMGR, "realpath error, errno is %{public}d.", errno);
63 return false;
64 }
65
66 std::fstream in;
67 char errBuf[256];
68 errBuf[0] = '\0';
69 in.open(path, std::ios_base::in);
70 if (!in.is_open()) {
71 strerror_r(errno, errBuf, sizeof(errBuf));
72 TAG_LOGE(AAFwkTag::ABILITYMGR, "the file cannot be open due to %{public}s", errBuf);
73 return false;
74 }
75
76 in.seekg(0, std::ios::end);
77 int64_t size = in.tellg();
78 if (size <= 0) {
79 TAG_LOGE(AAFwkTag::ABILITYMGR, "the file is an empty file");
80 in.close();
81 return false;
82 }
83
84 in.seekg(0, std::ios::beg);
85 jsonBuf = nlohmann::json::parse(in, nullptr, false);
86 in.close();
87 if (jsonBuf.is_discarded()) {
88 TAG_LOGE(AAFwkTag::ABILITYMGR, "bad profile file");
89 return false;
90 }
91
92 return true;
93 }
94 } // namespace AAFwk
95 } // namespace OHOS