1 /* 2 * Copyright (c) 2021 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 #include "hiview_platform_config.h" 16 17 #include <chrono> 18 #include <fstream> 19 #include <regex> 20 21 #include "file_util.h" 22 #include "logger.h" 23 namespace OHOS { 24 namespace HiviewDFX { 25 DEFINE_LOG_TAG("HiView-HiviewPlatformConfig"); 26 namespace { 27 constexpr char DEFAULT_PLATFORM_CONFIG_PATH[] = "/system/etc/hiview/hiview_platform_config"; 28 constexpr int DATA_FIELD = 1; 29 constexpr int CONFIG_NAME_NO = 0; 30 constexpr int CONFIG_FILE_DIR_NO = 1; 31 constexpr int LIB_DIR_NO = 2; 32 constexpr int LIB64_DIR_NO = 3; 33 constexpr int WORK_DIR_NO = 4; 34 constexpr int COMMERCIAL_WORK_DIR_NO = 5; 35 constexpr int PRESIST_DIR_NO = 6; 36 } 37 HiviewPlatformConfig(const std::string & configPath)38HiviewPlatformConfig::HiviewPlatformConfig(const std::string& configPath) 39 { 40 if (configPath != "") { 41 configPath_ = configPath; 42 } else { 43 configPath_ = DEFAULT_PLATFORM_CONFIG_PATH; 44 } 45 } 46 InitData()47void HiviewPlatformConfig::InitData() 48 { 49 RegexList_.push_back(std::regex("DEFAULT_PLUGIN_CONFIG_NAME\\s*=\\s*\"(.*)\".*")); 50 RegexList_.push_back(std::regex("PLUGIN_CONFIG_FILE_DIR\\s*=\\s*\"(.*)\".*")); 51 RegexList_.push_back(std::regex("DYNAMIC_LIB_SEARCH_DIR\\s*=\\s*\"(.*)\".*")); 52 RegexList_.push_back(std::regex("DYNAMIC_LIB64_SEARCH_DIR\\s*=\\s*\"(.*)\".*")); 53 RegexList_.push_back(std::regex("WORK_DIR\\s*=\\s*\"(.*)\".*")); 54 RegexList_.push_back(std::regex("COMMERCIAL_WORK_DIR\\s*=\\s*\"(.*)\".*")); 55 RegexList_.push_back(std::regex("PERSIST_DIR\\s*=\\s*\"(.*)\".*")); 56 } 57 ParsesConfig(HiviewPlatformConfig::PlatformConfigInfo & ret)58bool HiviewPlatformConfig::ParsesConfig(HiviewPlatformConfig::PlatformConfigInfo& ret) 59 { 60 if (!FileUtil::FileExists(configPath_) || FileUtil::IsDirectory(configPath_)) { 61 return false; 62 } 63 64 std::ifstream in; 65 in.open(configPath_); 66 if (!in.is_open()) { 67 HIVIEW_LOGW("fail to open config file.\n"); 68 return false; 69 } 70 71 InitData(); 72 73 std::string buf = ""; 74 int index = 0; 75 std::vector<std::string> retString; 76 while (getline(in, buf)) { 77 std::smatch result; 78 if (!regex_search(buf, result, RegexList_.front())) { 79 HIVIEW_LOGW("match version failed.\n"); 80 return false; 81 } 82 retString.push_back(result[DATA_FIELD]); 83 HIVIEW_LOGD("_%{public}s_", retString[index].c_str()); 84 RegexList_.pop_front(); 85 ++index; 86 if (RegexList_.empty()) { 87 break; 88 } 89 } 90 ret.defaultPluginConfigName = retString[CONFIG_NAME_NO]; 91 ret.pluginConfigFileDir = retString[CONFIG_FILE_DIR_NO]; 92 ret.dynamicLibSearchDir = retString[LIB_DIR_NO]; 93 ret.dynamicLib64SearchDir = retString[LIB64_DIR_NO]; 94 ret.workDir = retString[WORK_DIR_NO]; 95 ret.commercialWorkDir = retString[COMMERCIAL_WORK_DIR_NO]; 96 ret.persistDir = retString[PRESIST_DIR_NO]; 97 return true; 98 } 99 } // namespace HiviewDFX 100 } // namespace OHOS