1cb7eb8c9Sopenharmony_ci/*
2cb7eb8c9Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3cb7eb8c9Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4cb7eb8c9Sopenharmony_ci * you may not use this file except in compliance with the License.
5cb7eb8c9Sopenharmony_ci * You may obtain a copy of the License at
6cb7eb8c9Sopenharmony_ci *
7cb7eb8c9Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8cb7eb8c9Sopenharmony_ci *
9cb7eb8c9Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10cb7eb8c9Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11cb7eb8c9Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12cb7eb8c9Sopenharmony_ci * See the License for the specific language governing permissions and
13cb7eb8c9Sopenharmony_ci * limitations under the License.
14cb7eb8c9Sopenharmony_ci */
15cb7eb8c9Sopenharmony_ci
16cb7eb8c9Sopenharmony_ci#include "plugin_loader.h"
17cb7eb8c9Sopenharmony_ci
18cb7eb8c9Sopenharmony_ci#include <dlfcn.h>
19cb7eb8c9Sopenharmony_ci#include <unistd.h>
20cb7eb8c9Sopenharmony_ci#include <vector>
21cb7eb8c9Sopenharmony_ci
22cb7eb8c9Sopenharmony_ci#include "utils_log.h"
23cb7eb8c9Sopenharmony_ci
24cb7eb8c9Sopenharmony_cinamespace OHOS::FileManagement::CloudFile {
25cb7eb8c9Sopenharmony_ciusing namespace std;
26cb7eb8c9Sopenharmony_ci
27cb7eb8c9Sopenharmony_ciPluginLoader &PluginLoader::GetInstance()
28cb7eb8c9Sopenharmony_ci{
29cb7eb8c9Sopenharmony_ci    static PluginLoader instance;
30cb7eb8c9Sopenharmony_ci    return instance;
31cb7eb8c9Sopenharmony_ci}
32cb7eb8c9Sopenharmony_ci
33cb7eb8c9Sopenharmony_ciPluginLoader::~PluginLoader()
34cb7eb8c9Sopenharmony_ci{
35cb7eb8c9Sopenharmony_ci    if (cloudKitPulginHandle_ == nullptr) {
36cb7eb8c9Sopenharmony_ci        return;
37cb7eb8c9Sopenharmony_ci    }
38cb7eb8c9Sopenharmony_ci    dlclose(cloudKitPulginHandle_);
39cb7eb8c9Sopenharmony_ci    LOGI("succ to unload plugin");
40cb7eb8c9Sopenharmony_ci    cloudKitPulginHandle_ = nullptr;
41cb7eb8c9Sopenharmony_ci}
42cb7eb8c9Sopenharmony_ci
43cb7eb8c9Sopenharmony_cistring GetPluginPath(const string &pluginFileName)
44cb7eb8c9Sopenharmony_ci{
45cb7eb8c9Sopenharmony_ci    const string searchDirs[] = {"/system/lib64/", "system/lib/"};
46cb7eb8c9Sopenharmony_ci    string pluginFilePath = "";
47cb7eb8c9Sopenharmony_ci    for (auto &searchDir : searchDirs) {
48cb7eb8c9Sopenharmony_ci        std::string tmpPath = searchDir + pluginFileName;
49cb7eb8c9Sopenharmony_ci        if (access(tmpPath.c_str(), F_OK) == 0) {
50cb7eb8c9Sopenharmony_ci            pluginFilePath = tmpPath;
51cb7eb8c9Sopenharmony_ci            break;
52cb7eb8c9Sopenharmony_ci        }
53cb7eb8c9Sopenharmony_ci    }
54cb7eb8c9Sopenharmony_ci    return pluginFilePath;
55cb7eb8c9Sopenharmony_ci}
56cb7eb8c9Sopenharmony_ci
57cb7eb8c9Sopenharmony_civoid PluginLoader::LoadCloudKitPlugin(bool isSupportCloudSync)
58cb7eb8c9Sopenharmony_ci{
59cb7eb8c9Sopenharmony_ci    std::vector<std::string> pluginFileNames;
60cb7eb8c9Sopenharmony_ci    pluginFileNames.emplace_back(isSupportCloudSync ? "libcloudfile_ext.z.so" : "libcloudfile_ext_core.z.so");
61cb7eb8c9Sopenharmony_ci    pluginFileNames.emplace_back("libcloud_adapter.z.so");
62cb7eb8c9Sopenharmony_ci    for (auto &pluginFileName : pluginFileNames) {
63cb7eb8c9Sopenharmony_ci        auto pluginFilePath = GetPluginPath(pluginFileName);
64cb7eb8c9Sopenharmony_ci        if (!pluginFilePath.empty()) {
65cb7eb8c9Sopenharmony_ci            char resolvedPath[PATH_MAX] = {'\0'};
66cb7eb8c9Sopenharmony_ci            if (realpath(pluginFilePath.c_str(), resolvedPath) == nullptr) {
67cb7eb8c9Sopenharmony_ci                LOGE("realpath failed in line path: %s", pluginFilePath.c_str());
68cb7eb8c9Sopenharmony_ci                return;
69cb7eb8c9Sopenharmony_ci            }
70cb7eb8c9Sopenharmony_ci            cloudKitPulginHandle_ = dlopen(pluginFilePath.c_str(), RTLD_LAZY);
71cb7eb8c9Sopenharmony_ci            if (cloudKitPulginHandle_ == nullptr) {
72cb7eb8c9Sopenharmony_ci                LOGE("dlopen failed, path:%{public}s", pluginFilePath.c_str());
73cb7eb8c9Sopenharmony_ci            } else {
74cb7eb8c9Sopenharmony_ci                LOGI("succ to load plugin, path:%{public}s", pluginFilePath.c_str());
75cb7eb8c9Sopenharmony_ci            }
76cb7eb8c9Sopenharmony_ci            return;
77cb7eb8c9Sopenharmony_ci        }
78cb7eb8c9Sopenharmony_ci    }
79cb7eb8c9Sopenharmony_ci    LOGE("Load CloudKit Plugin failed");
80cb7eb8c9Sopenharmony_ci}
81cb7eb8c9Sopenharmony_ci} // namespace OHOS::FileManagement::CloudFile