1eace7efcSopenharmony_ci/* 2eace7efcSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 3eace7efcSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4eace7efcSopenharmony_ci * you may not use this file except in compliance with the License. 5eace7efcSopenharmony_ci * You may obtain a copy of the License at 6eace7efcSopenharmony_ci * 7eace7efcSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8eace7efcSopenharmony_ci * 9eace7efcSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10eace7efcSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11eace7efcSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12eace7efcSopenharmony_ci * See the License for the specific language governing permissions and 13eace7efcSopenharmony_ci * limitations under the License. 14eace7efcSopenharmony_ci */ 15eace7efcSopenharmony_ci 16eace7efcSopenharmony_ci#include "json_utils.h" 17eace7efcSopenharmony_ci#include <fstream> 18eace7efcSopenharmony_ci#include <sstream> 19eace7efcSopenharmony_ci#include <unistd.h> 20eace7efcSopenharmony_ci#include <regex> 21eace7efcSopenharmony_ci 22eace7efcSopenharmony_ci#include "config_policy_utils.h" 23eace7efcSopenharmony_ci#include "hilog_tag_wrapper.h" 24eace7efcSopenharmony_ci 25eace7efcSopenharmony_cinamespace OHOS { 26eace7efcSopenharmony_cinamespace AAFwk { 27eace7efcSopenharmony_cibool JsonUtils::LoadConfiguration(const std::string& path, nlohmann::json& jsonBuf, 28eace7efcSopenharmony_ci const std::string& defaultPath) 29eace7efcSopenharmony_ci{ 30eace7efcSopenharmony_ci std::string configPath = GetConfigPath(path, defaultPath); 31eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::ABILITYMGR, "config path is: %{public}s", configPath.c_str()); 32eace7efcSopenharmony_ci if (!ReadFileInfoJson(configPath, jsonBuf)) { 33eace7efcSopenharmony_ci return false; 34eace7efcSopenharmony_ci } 35eace7efcSopenharmony_ci return true; 36eace7efcSopenharmony_ci} 37eace7efcSopenharmony_ci 38eace7efcSopenharmony_cistd::string JsonUtils::GetConfigPath(const std::string& path, const std::string& defaultPath) 39eace7efcSopenharmony_ci{ 40eace7efcSopenharmony_ci char buf[MAX_PATH_LEN] = { 0 }; 41eace7efcSopenharmony_ci char *configPath = GetOneCfgFile(path.c_str(), buf, MAX_PATH_LEN); 42eace7efcSopenharmony_ci if (configPath == nullptr || configPath[0] == '\0' || strlen(configPath) > MAX_PATH_LEN) { 43eace7efcSopenharmony_ci return defaultPath; 44eace7efcSopenharmony_ci } 45eace7efcSopenharmony_ci return configPath; 46eace7efcSopenharmony_ci} 47eace7efcSopenharmony_ci 48eace7efcSopenharmony_cibool JsonUtils::ReadFileInfoJson(const std::string &filePath, nlohmann::json &jsonBuf) 49eace7efcSopenharmony_ci{ 50eace7efcSopenharmony_ci if (access(filePath.c_str(), F_OK) != 0) { 51eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "deepLink config not exist"); 52eace7efcSopenharmony_ci return false; 53eace7efcSopenharmony_ci } 54eace7efcSopenharmony_ci 55eace7efcSopenharmony_ci if (filePath.empty()) { 56eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "filePath empty"); 57eace7efcSopenharmony_ci return false; 58eace7efcSopenharmony_ci } 59eace7efcSopenharmony_ci 60eace7efcSopenharmony_ci char path[PATH_MAX] = {0}; 61eace7efcSopenharmony_ci if (realpath(filePath.c_str(), path) == nullptr) { 62eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "realpath error, errno: %{public}d", errno); 63eace7efcSopenharmony_ci return false; 64eace7efcSopenharmony_ci } 65eace7efcSopenharmony_ci 66eace7efcSopenharmony_ci std::fstream in; 67eace7efcSopenharmony_ci char errBuf[256]; 68eace7efcSopenharmony_ci errBuf[0] = '\0'; 69eace7efcSopenharmony_ci in.open(path, std::ios_base::in); 70eace7efcSopenharmony_ci if (!in.is_open()) { 71eace7efcSopenharmony_ci strerror_r(errno, errBuf, sizeof(errBuf)); 72eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "file not open: %{public}s", errBuf); 73eace7efcSopenharmony_ci return false; 74eace7efcSopenharmony_ci } 75eace7efcSopenharmony_ci 76eace7efcSopenharmony_ci in.seekg(0, std::ios::end); 77eace7efcSopenharmony_ci int64_t size = in.tellg(); 78eace7efcSopenharmony_ci if (size <= 0) { 79eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "empty file"); 80eace7efcSopenharmony_ci in.close(); 81eace7efcSopenharmony_ci return false; 82eace7efcSopenharmony_ci } 83eace7efcSopenharmony_ci 84eace7efcSopenharmony_ci in.seekg(0, std::ios::beg); 85eace7efcSopenharmony_ci jsonBuf = nlohmann::json::parse(in, nullptr, false); 86eace7efcSopenharmony_ci in.close(); 87eace7efcSopenharmony_ci if (jsonBuf.is_discarded()) { 88eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "bad profile file"); 89eace7efcSopenharmony_ci return false; 90eace7efcSopenharmony_ci } 91eace7efcSopenharmony_ci 92eace7efcSopenharmony_ci return true; 93eace7efcSopenharmony_ci} 94eace7efcSopenharmony_ci} // namespace AAFwk 95eace7efcSopenharmony_ci} // namespace OHOS