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 "js_quickfix_callback.h"
17
18 #include "file_path_utils.h"
19 #include "hilog_wrapper.h"
20 #include "js_runtime.h"
21
22 namespace OHOS {
23 namespace AbilityRuntime {
24 namespace {
25 constexpr char MERGE_ABC_PATH[] = "/ets/modules.abc";
26 constexpr char BUNDLE_INSTALL_PATH[] = "/data/storage/el1/bundle/";
27 }
28
operator ()(std::string baseFileName,std::string & patchFileName,void ** patchBuffer,size_t & patchSize)29 bool JsQuickfixCallback::operator()(std::string baseFileName, std::string &patchFileName,
30 void **patchBuffer, size_t &patchSize)
31 {
32 HILOG_DEBUG("baseFileName: %{private}s", baseFileName.c_str());
33 auto position = baseFileName.find(".abc");
34 if (position == std::string::npos) {
35 HILOG_ERROR("invalid baseFileName!");
36 return false;
37 }
38 int baseFileNameLen = static_cast<int>(baseFileName.length());
39 int prefixLen = strlen(BUNDLE_INSTALL_PATH);
40 int suffixLen = strlen(MERGE_ABC_PATH);
41 int moduleLen = baseFileNameLen - prefixLen - suffixLen;
42 if (moduleLen < 0) {
43 HILOG_ERROR("invalid baseFileName!");
44 return false;
45 }
46 std::string moduleName = baseFileName.substr(prefixLen, moduleLen);
47 HILOG_DEBUG("moduleName: %{private}s", moduleName.c_str());
48
49 auto it = moduleAndHqfPath_.find(moduleName);
50 if (it == moduleAndHqfPath_.end()) {
51 return false;
52 }
53
54 std::string hqfFile = it->second;
55 std::string resolvedHqfFile(AbilityBase::GetLoadPath(hqfFile));
56 HILOG_DEBUG("hqfFile: %{private}s, resolvedHqfFile: %{private}s", hqfFile.c_str(), resolvedHqfFile.c_str());
57
58 if (!JsRuntime::GetFileBuffer(resolvedHqfFile, patchFileName, newpatchBuffer_)) {
59 HILOG_ERROR("GetFileBuffer failed");
60 return false;
61 }
62 *patchBuffer = newpatchBuffer_.data();
63 HILOG_DEBUG("patchFileName: %{private}s", patchFileName.c_str());
64 patchSize = newpatchBuffer_.size();
65 return true;
66 }
67 } // namespace AbilityRuntime
68 } // namespace OHOS
69