1 /*
2 * Copyright (c) 2022 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 "adapter/ohos/entrance/utils.h"
17
18 #include <cstdio>
19 #include <regex>
20 #include <sstream>
21 #include <string>
22
23 #include "runtime_extractor.h"
24
25 #include "adapter/ohos/entrance/file_asset_provider.h"
26 #include "adapter/ohos/entrance/hap_asset_provider.h"
27
28 namespace OHOS::Ace {
29
GetIsArkFromConfig(const std::string & packagePath,bool isHap)30 bool GetIsArkFromConfig(const std::string& packagePath, bool isHap)
31 {
32 std::string jsonStr = isHap ? GetStringFromHap(packagePath, "config.json") :
33 GetStringFromFile(packagePath, "config.json");
34 if (jsonStr.empty()) {
35 LOGE("return not arkApp.");
36 return false;
37 }
38 auto rootJson = JsonUtil::ParseJsonString(jsonStr);
39 auto module = rootJson->GetValue("module");
40 auto distro = module->GetValue("distro");
41 std::string virtualMachine = distro->GetString("virtualMachine");
42 return virtualMachine.find("ark") != std::string::npos;
43 }
44
GetStringFromFile(const std::string & packagePathStr,const std::string & fileName)45 std::string GetStringFromFile(const std::string& packagePathStr, const std::string& fileName)
46 {
47 auto configPath = packagePathStr + fileName;
48 char realPath[PATH_MAX] = { 0x00 };
49 if (realpath(configPath.c_str(), realPath) == nullptr) {
50 LOGE("realpath fail! filePath: %{private}s, fail reason: %{public}s", configPath.c_str(), strerror(errno));
51 return "";
52 }
53 std::unique_ptr<FILE, decltype(&fclose)> file(fopen(realPath, "rb"), fclose);
54 if (!file) {
55 LOGE("open file failed, filePath: %{private}s, fail reason: %{public}s", configPath.c_str(), strerror(errno));
56 return "";
57 }
58 if (std::fseek(file.get(), 0, SEEK_END) != 0) {
59 LOGE("seek file tail error");
60 return "";
61 }
62
63 int64_t size = std::ftell(file.get());
64 if (size == -1L) {
65 return "";
66 }
67
68 std::string fileData;
69 fileData.resize(size);
70
71 rewind(file.get());
72 size_t result = std::fread(fileData.data(), 1, fileData.size(), file.get());
73 if (result != static_cast<size_t>(size)) {
74 LOGE("read file failed");
75 return "";
76 }
77
78 return fileData;
79 }
80
GetStringFromHap(const std::string & hapPath,const std::string & fileName)81 std::string GetStringFromHap(const std::string& hapPath, const std::string& fileName)
82 {
83 std::shared_ptr<AbilityRuntime::RuntimeExtractor> runtimeExtractor =
84 AbilityRuntime::RuntimeExtractor::Create(hapPath);
85 if (!runtimeExtractor) {
86 LOGE("read file %{public}s error\n", hapPath.c_str());
87 return "";
88 }
89
90 std::ostringstream osstream;
91 bool hasFile = runtimeExtractor->GetFileBuffer(fileName, osstream);
92 if (!hasFile) {
93 LOGE("read file %{public}s /config.json error\n", hapPath.c_str());
94 return "";
95 }
96
97 return osstream.str();
98 }
99
CreateAssetProvider(const std::string & packagePath,const std::vector<std::string> & assetBasePaths)100 RefPtr<FlutterAssetProvider> CreateAssetProvider(const std::string& packagePath,
101 const std::vector<std::string>& assetBasePaths)
102 {
103 if (std::regex_match(packagePath, std::regex(".*\\.hap"))) {
104 auto assetProvider = AceType::MakeRefPtr<HapAssetProvider>();
105 if (assetProvider->Initialize(packagePath, assetBasePaths)) {
106 return assetProvider;
107 }
108 } else {
109 auto assetProvider = AceType::MakeRefPtr<FileAssetProvider>();
110 if (assetProvider->Initialize(packagePath, assetBasePaths)) {
111 return assetProvider;
112 }
113 }
114 return nullptr;
115 }
116 } // namespace OHOS::Ace