• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 <fstream>
17 #include <sstream>
18 #include <unistd.h>
19 
20 #include "app_log_wrapper.h"
21 #include "bms_extension_profile.h"
22 
23 namespace OHOS {
24 namespace AppExecFwk {
25 namespace {
26 const int32_t BUFFER_SIZE = 256;
27 static const std::string BMS_EXTENSION_PROFILE = "bms-extensions";
28 static const std::string BMS_EXTENSION_PROFILE_BUNDLE_MGR = "bundle-mgr";
29 static const std::string BUNDLE_MGR_KEY_EXTENSION_NAME = "extension-name";
30 static const std::string BUNDLE_MGR_KEY_LIB_PATH = "libpath";
31 static const std::string BUNDLE_MGR_KEY_LIB64_PATH = "lib64path";
32 }
33 
ParseBmsExtension(const std::string & jsonPath,BmsExtension & bmsExtension) const34 ErrCode BmsExtensionProfile::ParseBmsExtension(
35     const std::string &jsonPath, BmsExtension &bmsExtension) const
36 {
37     APP_LOGD("Parse BmsExtension from %{private}s", jsonPath.c_str());
38     nlohmann::json jsonBuf;
39     if (!ReadFileIntoJson(jsonPath, jsonBuf)) {
40         APP_LOGE("Parse bms-extension.json file failed, jsonPath: %{public}s", jsonPath.c_str());
41         return ERR_APPEXECFWK_PARSE_FILE_FAILED;
42     }
43     return TransformTo(jsonBuf, bmsExtension);
44 }
45 
ReadFileIntoJson(const std::string & filePath,nlohmann::json & jsonBuf) const46 bool BmsExtensionProfile::ReadFileIntoJson(const std::string &filePath, nlohmann::json &jsonBuf) const
47 {
48     if (access(filePath.c_str(), F_OK) != 0) {
49         APP_LOGE("can not access the file: %{public}s", filePath.c_str());
50         return false;
51     }
52 
53     std::fstream in;
54     char errBuf[BUFFER_SIZE];
55     errBuf[0] = '\0';
56     in.open(filePath, std::ios_base::in);
57     if (!in.is_open()) {
58         strerror_r(errno, errBuf, sizeof(errBuf));
59         APP_LOGE("the file cannot be open due to  %{public}s", errBuf);
60         return false;
61     }
62 
63     in.seekg(0, std::ios::end);
64     int64_t size = in.tellg();
65     if (size <= 0) {
66         APP_LOGE("the file is an empty file");
67         in.close();
68         return false;
69     }
70 
71     in.seekg(0, std::ios::beg);
72     jsonBuf = nlohmann::json::parse(in, nullptr, false);
73     in.close();
74     if (jsonBuf.is_discarded()) {
75         APP_LOGE("bad profile file");
76         return false;
77     }
78 
79     return true;
80 }
81 
TransformTo(const nlohmann::json & jsonObject,BmsExtension & bmsExtension) const82 ErrCode BmsExtensionProfile::TransformTo(const nlohmann::json &jsonObject,
83     BmsExtension &bmsExtension) const
84 {
85     APP_LOGD("transform bms-extension.json stream to BmsExtension");
86     if (jsonObject.is_discarded()) {
87         APP_LOGE("profile format error");
88         return ERR_APPEXECFWK_PARSE_BAD_PROFILE;
89     }
90     if (jsonObject.find(BMS_EXTENSION_PROFILE) == jsonObject.end()) {
91         APP_LOGE("bms-extensions no exist");
92         return ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR;
93     }
94     nlohmann::json bmsExtensionJson = jsonObject.at(BMS_EXTENSION_PROFILE);
95     if (!bmsExtensionJson.is_object()) {
96         APP_LOGE("bms-extension.json file lacks of invalid bms-extensions property");
97         return ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR;
98     }
99     nlohmann::json bundleMgrJson = bmsExtensionJson.at(BMS_EXTENSION_PROFILE_BUNDLE_MGR);
100     if (!bundleMgrJson.is_object()) {
101         APP_LOGE("bms-extension.json file lacks of invalid bundle-mgr property");
102         return ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR;
103     }
104     const auto &jsonObjectEnd = bundleMgrJson.end();
105     int32_t parseResult = ERR_OK;
106     GetValueIfFindKey<std::string>(bundleMgrJson,
107         jsonObjectEnd,
108         BUNDLE_MGR_KEY_EXTENSION_NAME,
109         bmsExtension.bmsExtensionBundleMgr.extensionName,
110         JsonType::STRING,
111         true,
112         parseResult,
113         ArrayType::NOT_ARRAY);
114     GetValueIfFindKey<std::string>(bundleMgrJson,
115         jsonObjectEnd,
116         BUNDLE_MGR_KEY_LIB_PATH,
117         bmsExtension.bmsExtensionBundleMgr.libPath,
118         JsonType::STRING,
119         true,
120         parseResult,
121         ArrayType::NOT_ARRAY);
122     GetValueIfFindKey<std::string>(bundleMgrJson,
123         jsonObjectEnd,
124         BUNDLE_MGR_KEY_LIB64_PATH,
125         bmsExtension.bmsExtensionBundleMgr.lib64Path,
126         JsonType::STRING,
127         true,
128         parseResult,
129         ArrayType::NOT_ARRAY);
130     return parseResult;
131 }
132 }  // namespace AppExecFwk
133 }  // namespace OHOS
134 
135