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 "parser_util.h"
17eace7efcSopenharmony_ci
18eace7efcSopenharmony_ci#include <fstream>
19eace7efcSopenharmony_ci#include <unistd.h>
20eace7efcSopenharmony_ci
21eace7efcSopenharmony_ci#include "config_policy_utils.h"
22eace7efcSopenharmony_ci#include "hilog_tag_wrapper.h"
23eace7efcSopenharmony_ci
24eace7efcSopenharmony_cinamespace OHOS {
25eace7efcSopenharmony_cinamespace AbilityRuntime {
26eace7efcSopenharmony_cinamespace {
27eace7efcSopenharmony_ciconstexpr const char *DEFAULT_PRE_BUNDLE_ROOT_DIR = "/system";
28eace7efcSopenharmony_ciconstexpr const char *PRODUCT_SUFFIX = "/etc/app";
29eace7efcSopenharmony_ciconstexpr const char *INSTALL_LIST_CAPABILITY_CONFIG = "/install_list_capability.json";
30eace7efcSopenharmony_ciconstexpr const char *INSTALL_LIST = "install_list";
31eace7efcSopenharmony_ciconstexpr const char *BUNDLE_NAME = "bundleName";
32eace7efcSopenharmony_ciconstexpr const char *KEEP_ALIVE = "keepAlive";
33eace7efcSopenharmony_ciconstexpr const char *KEEP_ALIVE_ENABLE = "keepAliveEnable";
34eace7efcSopenharmony_ciconstexpr const char *KEEP_ALIVE_CONFIGURED_LIST = "keepAliveConfiguredList";
35eace7efcSopenharmony_ci
36eace7efcSopenharmony_ci} // namespace
37eace7efcSopenharmony_ciParserUtil &ParserUtil::GetInstance()
38eace7efcSopenharmony_ci{
39eace7efcSopenharmony_ci    static ParserUtil instance;
40eace7efcSopenharmony_ci    return instance;
41eace7efcSopenharmony_ci}
42eace7efcSopenharmony_ci
43eace7efcSopenharmony_civoid ParserUtil::GetResidentProcessRawData(std::vector<std::tuple<std::string, std::string, std::string>> &list)
44eace7efcSopenharmony_ci{
45eace7efcSopenharmony_ci    std::vector<std::string> rootDirList;
46eace7efcSopenharmony_ci    GetPreInstallRootDirList(rootDirList);
47eace7efcSopenharmony_ci
48eace7efcSopenharmony_ci    for (auto &root : rootDirList) {
49eace7efcSopenharmony_ci        auto fileDir = root.append(PRODUCT_SUFFIX).append(INSTALL_LIST_CAPABILITY_CONFIG);
50eace7efcSopenharmony_ci        TAG_LOGD(AAFwkTag::ABILITYMGR, "Search file dir : %{public}s", fileDir.c_str());
51eace7efcSopenharmony_ci        ParsePreInstallAbilityConfig(fileDir, list);
52eace7efcSopenharmony_ci    }
53eace7efcSopenharmony_ci}
54eace7efcSopenharmony_ci
55eace7efcSopenharmony_civoid ParserUtil::ParsePreInstallAbilityConfig(
56eace7efcSopenharmony_ci    const std::string &filePath, std::vector<std::tuple<std::string, std::string, std::string>> &list)
57eace7efcSopenharmony_ci{
58eace7efcSopenharmony_ci    nlohmann::json jsonBuf;
59eace7efcSopenharmony_ci    if (!ReadFileIntoJson(filePath, jsonBuf)) {
60eace7efcSopenharmony_ci        return;
61eace7efcSopenharmony_ci    }
62eace7efcSopenharmony_ci
63eace7efcSopenharmony_ci    if (jsonBuf.is_discarded()) {
64eace7efcSopenharmony_ci        return;
65eace7efcSopenharmony_ci    }
66eace7efcSopenharmony_ci
67eace7efcSopenharmony_ci    FilterInfoFromJson(jsonBuf, list);
68eace7efcSopenharmony_ci}
69eace7efcSopenharmony_ci
70eace7efcSopenharmony_cibool ParserUtil::FilterInfoFromJson(
71eace7efcSopenharmony_ci    nlohmann::json &jsonBuf, std::vector<std::tuple<std::string, std::string, std::string>> &list)
72eace7efcSopenharmony_ci{
73eace7efcSopenharmony_ci    if (jsonBuf.is_discarded()) {
74eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::ABILITYMGR, "format error");
75eace7efcSopenharmony_ci        return false;
76eace7efcSopenharmony_ci    }
77eace7efcSopenharmony_ci
78eace7efcSopenharmony_ci    if (jsonBuf.find(INSTALL_LIST) == jsonBuf.end()) {
79eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::ABILITYMGR, "installList absent");
80eace7efcSopenharmony_ci        return false;
81eace7efcSopenharmony_ci    }
82eace7efcSopenharmony_ci
83eace7efcSopenharmony_ci    auto arrays = jsonBuf.at(INSTALL_LIST);
84eace7efcSopenharmony_ci    if (!arrays.is_array() || arrays.empty()) {
85eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::ABILITYMGR, "not found");
86eace7efcSopenharmony_ci        return false;
87eace7efcSopenharmony_ci    }
88eace7efcSopenharmony_ci
89eace7efcSopenharmony_ci    std::string bundleName;
90eace7efcSopenharmony_ci    std::string KeepAliveEnable = "1";
91eace7efcSopenharmony_ci    std::string KeepAliveConfiguredList;
92eace7efcSopenharmony_ci    for (const auto &array : arrays) {
93eace7efcSopenharmony_ci        if (!array.is_object()) {
94eace7efcSopenharmony_ci            continue;
95eace7efcSopenharmony_ci        }
96eace7efcSopenharmony_ci
97eace7efcSopenharmony_ci        // Judgment logic exists, not found, not bool, not resident process
98eace7efcSopenharmony_ci        if (!(array.find(KEEP_ALIVE) != array.end() && array.at(KEEP_ALIVE).is_boolean() &&
99eace7efcSopenharmony_ci                array.at(KEEP_ALIVE).get<bool>())) {
100eace7efcSopenharmony_ci            continue;
101eace7efcSopenharmony_ci        }
102eace7efcSopenharmony_ci
103eace7efcSopenharmony_ci        if (!(array.find(BUNDLE_NAME) != array.end() && array.at(BUNDLE_NAME).is_string())) {
104eace7efcSopenharmony_ci            continue;
105eace7efcSopenharmony_ci        }
106eace7efcSopenharmony_ci
107eace7efcSopenharmony_ci        bundleName = array.at(BUNDLE_NAME).get<std::string>();
108eace7efcSopenharmony_ci
109eace7efcSopenharmony_ci        if (array.find(KEEP_ALIVE_ENABLE) != array.end() && array.at(KEEP_ALIVE_ENABLE).is_boolean()) {
110eace7efcSopenharmony_ci            auto val = array.at(KEEP_ALIVE_ENABLE).get<bool>();
111eace7efcSopenharmony_ci            KeepAliveEnable = std::to_string(val);
112eace7efcSopenharmony_ci        }
113eace7efcSopenharmony_ci
114eace7efcSopenharmony_ci        if (array.find(KEEP_ALIVE_CONFIGURED_LIST) != array.end() && array.at(KEEP_ALIVE_CONFIGURED_LIST).is_array()) {
115eace7efcSopenharmony_ci            // Save directly in the form of an array and parse it when in use
116eace7efcSopenharmony_ci            KeepAliveConfiguredList = array.at(KEEP_ALIVE_CONFIGURED_LIST).dump();
117eace7efcSopenharmony_ci        }
118eace7efcSopenharmony_ci
119eace7efcSopenharmony_ci        list.emplace_back(std::make_tuple(bundleName, KeepAliveEnable, KeepAliveConfiguredList));
120eace7efcSopenharmony_ci        bundleName.clear();
121eace7efcSopenharmony_ci        KeepAliveEnable = "1";
122eace7efcSopenharmony_ci        KeepAliveConfiguredList.clear();
123eace7efcSopenharmony_ci    }
124eace7efcSopenharmony_ci
125eace7efcSopenharmony_ci    return true;
126eace7efcSopenharmony_ci}
127eace7efcSopenharmony_ci
128eace7efcSopenharmony_civoid ParserUtil::GetPreInstallRootDirList(std::vector<std::string> &rootDirList)
129eace7efcSopenharmony_ci{
130eace7efcSopenharmony_ci    auto cfgDirList = GetCfgDirList();
131eace7efcSopenharmony_ci    if (cfgDirList != nullptr) {
132eace7efcSopenharmony_ci        for (const auto &cfgDir : cfgDirList->paths) {
133eace7efcSopenharmony_ci            if (cfgDir == nullptr) {
134eace7efcSopenharmony_ci                continue;
135eace7efcSopenharmony_ci            }
136eace7efcSopenharmony_ci            rootDirList.emplace_back(cfgDir);
137eace7efcSopenharmony_ci        }
138eace7efcSopenharmony_ci
139eace7efcSopenharmony_ci        FreeCfgDirList(cfgDirList);
140eace7efcSopenharmony_ci    }
141eace7efcSopenharmony_ci    bool ret = std::find(rootDirList.begin(), rootDirList.end(), DEFAULT_PRE_BUNDLE_ROOT_DIR) != rootDirList.end();
142eace7efcSopenharmony_ci    if (!ret) {
143eace7efcSopenharmony_ci        rootDirList.emplace_back(DEFAULT_PRE_BUNDLE_ROOT_DIR);
144eace7efcSopenharmony_ci    }
145eace7efcSopenharmony_ci}
146eace7efcSopenharmony_ci
147eace7efcSopenharmony_cibool ParserUtil::ReadFileIntoJson(const std::string &filePath, nlohmann::json &jsonBuf)
148eace7efcSopenharmony_ci{
149eace7efcSopenharmony_ci    if (access(filePath.c_str(), F_OK) != 0) {
150eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::ABILITYMGR, "path not exist");
151eace7efcSopenharmony_ci        return false;
152eace7efcSopenharmony_ci    }
153eace7efcSopenharmony_ci
154eace7efcSopenharmony_ci    if (filePath.empty()) {
155eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::ABILITYMGR, "empty path");
156eace7efcSopenharmony_ci        return false;
157eace7efcSopenharmony_ci    }
158eace7efcSopenharmony_ci
159eace7efcSopenharmony_ci    char path[PATH_MAX] = {0};
160eace7efcSopenharmony_ci    if (realpath(filePath.c_str(), path) == nullptr) {
161eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::ABILITYMGR, "realpath error:%{public}d", errno);
162eace7efcSopenharmony_ci        return false;
163eace7efcSopenharmony_ci    }
164eace7efcSopenharmony_ci
165eace7efcSopenharmony_ci    std::ifstream fin(path);
166eace7efcSopenharmony_ci    if (!fin.is_open()) {
167eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::ABILITYMGR, "path exception");
168eace7efcSopenharmony_ci        return false;
169eace7efcSopenharmony_ci    }
170eace7efcSopenharmony_ci
171eace7efcSopenharmony_ci    fin.seekg(0, std::ios::end);
172eace7efcSopenharmony_ci    int64_t size = fin.tellg();
173eace7efcSopenharmony_ci    if (size <= 0) {
174eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::ABILITYMGR, "empty file");
175eace7efcSopenharmony_ci        fin.close();
176eace7efcSopenharmony_ci        return false;
177eace7efcSopenharmony_ci    }
178eace7efcSopenharmony_ci
179eace7efcSopenharmony_ci    fin.seekg(0, std::ios::beg);
180eace7efcSopenharmony_ci    jsonBuf = nlohmann::json::parse(fin, nullptr, false);
181eace7efcSopenharmony_ci    fin.close();
182eace7efcSopenharmony_ci    if (jsonBuf.is_discarded()) {
183eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::ABILITYMGR, "bad profile");
184eace7efcSopenharmony_ci        return false;
185eace7efcSopenharmony_ci    }
186eace7efcSopenharmony_ci    return true;
187eace7efcSopenharmony_ci}
188eace7efcSopenharmony_ci} // namespace AbilityRuntime
189eace7efcSopenharmony_ci} // namespace OHOS