1 /*
2  * Copyright (c) 2024 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 "native_lib_util.h"
17 
18 #include "constants.h"
19 #include "hilog_tag_wrapper.h"
20 
21 namespace OHOS {
22 namespace AppExecFwk {
GetLibPath(const std::string & hapPath,bool isPreInstallApp)23 std::string GetLibPath(const std::string &hapPath, bool isPreInstallApp)
24 {
25     std::string libPath = AbilityBase::Constants::LOCAL_CODE_PATH;
26     if (isPreInstallApp) {
27         auto pos = hapPath.rfind("/");
28         libPath = hapPath.substr(0, pos);
29     }
30     return libPath;
31 }
32 
GetHapSoPath(const HapModuleInfo & hapInfo,AppLibPathMap & appLibPaths,bool isPreInstallApp)33 void GetHapSoPath(const HapModuleInfo &hapInfo, AppLibPathMap &appLibPaths, bool isPreInstallApp)
34 {
35     if (hapInfo.nativeLibraryPath.empty()) {
36         TAG_LOGD(AAFwkTag::APPKIT, "Lib path of %{public}s is empty, lib isn't isolated or compressed",
37             hapInfo.moduleName.c_str());
38         return;
39     }
40 
41     std::string appLibPathKey = hapInfo.bundleName + "/" + hapInfo.moduleName;
42     std::string libPath = AbilityBase::Constants::LOCAL_CODE_PATH;
43     if (!hapInfo.compressNativeLibs) {
44         TAG_LOGD(AAFwkTag::APPKIT, "Lib of %{public}s will not be extracted from hap", hapInfo.moduleName.c_str());
45         libPath = GetLibPath(hapInfo.hapPath, isPreInstallApp);
46     }
47 
48     libPath += (libPath.back() == '/') ? hapInfo.nativeLibraryPath : "/" + hapInfo.nativeLibraryPath;
49     TAG_LOGD(
50         AAFwkTag::APPKIT, "appLibPathKey: %{private}s, lib path: %{private}s", appLibPathKey.c_str(), libPath.c_str());
51     appLibPaths[appLibPathKey].emplace_back(libPath);
52 }
53 
GetHspNativeLibPath(const BaseSharedBundleInfo & hspInfo,AppLibPathMap & appLibPaths,bool isPreInstallApp)54 void GetHspNativeLibPath(const BaseSharedBundleInfo &hspInfo, AppLibPathMap &appLibPaths, bool isPreInstallApp)
55 {
56     if (hspInfo.nativeLibraryPath.empty()) {
57         return;
58     }
59 
60     std::string appLibPathKey = hspInfo.bundleName + "/" + hspInfo.moduleName;
61     std::string libPath = AbilityBase::Constants::LOCAL_CODE_PATH;
62     if (!hspInfo.compressNativeLibs) {
63         libPath = GetLibPath(hspInfo.hapPath, isPreInstallApp);
64         libPath = libPath.back() == '/' ? libPath : libPath + "/";
65         if (isPreInstallApp) {
66             libPath += hspInfo.nativeLibraryPath;
67         } else {
68             libPath += hspInfo.bundleName + "/" + hspInfo.moduleName + "/" + hspInfo.nativeLibraryPath;
69         }
70     } else {
71         libPath = libPath.back() == '/' ? libPath : libPath + "/";
72         libPath += hspInfo.bundleName + "/" + hspInfo.nativeLibraryPath;
73     }
74 
75     TAG_LOGD(
76         AAFwkTag::APPKIT, "appLibPathKey: %{private}s, libPath: %{private}s", appLibPathKey.c_str(), libPath.c_str());
77     appLibPaths[appLibPathKey].emplace_back(libPath);
78 }
79 
GetPatchNativeLibPath(const HapModuleInfo & hapInfo,std::string & patchNativeLibraryPath,AppLibPathMap & appLibPaths)80 void GetPatchNativeLibPath(const HapModuleInfo &hapInfo, std::string &patchNativeLibraryPath,
81     AppLibPathMap &appLibPaths)
82 {
83     if (hapInfo.isLibIsolated) {
84         patchNativeLibraryPath = hapInfo.hqfInfo.nativeLibraryPath;
85     }
86 
87     if (patchNativeLibraryPath.empty()) {
88         TAG_LOGD(AAFwkTag::APPKIT, "Patch lib path of %{public}s is empty", hapInfo.moduleName.c_str());
89         return;
90     }
91 
92     if (hapInfo.compressNativeLibs && !hapInfo.isLibIsolated) {
93         TAG_LOGD(AAFwkTag::APPKIT, "Lib of %{public}s has compressed and isn't isolated, no need to set",
94             hapInfo.moduleName.c_str());
95         return;
96     }
97 
98     std::string appLibPathKey = hapInfo.bundleName + "/" + hapInfo.moduleName;
99     std::string patchLibPath = AbilityBase::Constants::LOCAL_CODE_PATH;
100     patchLibPath += (patchLibPath.back() == '/') ? patchNativeLibraryPath : "/" + patchNativeLibraryPath;
101     TAG_LOGD(AAFwkTag::APPKIT, "appLibPathKey: %{public}s, patch lib path: %{private}s", appLibPathKey.c_str(),
102         patchLibPath.c_str());
103     appLibPaths[appLibPathKey].emplace_back(patchLibPath);
104 }
105 } // AppExecFwk
106 } // namespace OHOS