LCOV - code coverage report
Current view: top level - adump/common - lib_path.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 83.1 % 71 59
Test Date: 2026-07-28 10:54:24 Functions: 87.5 % 8 7

            Line data    Source code
       1              : /**
       2              :  * Copyright (c) 2025 Huawei Technologies Co., Ltd.
       3              :  * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
       4              :  * CANN Open Software License Agreement Version 2.0 (the "License").
       5              :  * Please refer to the License for details. You may not use this file except in compliance with the License.
       6              :  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
       7              :  * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
       8              :  * See LICENSE in the root of the software repository for the full text of the License.
       9              :  */
      10              : 
      11              : #include "lib_path.h"
      12              : #include "mmpa_api.h"
      13              : #include "log/hdc_log.h"
      14              : #include "log/adx_log.h"
      15              : #include "file_utils.h"
      16              : 
      17              : namespace Adx {
      18              : constexpr char LIBSUFFIX[] = ".so";
      19              : 
      20           51 : LibPath &LibPath::Instance()
      21              : {
      22           51 :     static LibPath libPath;
      23           51 :     return libPath;
      24              : }
      25              : 
      26            0 : Path LibPath::GetInstallParentPath() const
      27              : {
      28              :     // get install path from self path : <install path>/x86_64-linux/lib64/ --> <install path>/x86_64-linux
      29            0 :     Path currentPath(LibPath::GetSelfLibraryDir());
      30            0 :     auto installPath = currentPath.ParentPath();
      31            0 :     return installPath;
      32            0 : }
      33              : 
      34           26 : Path LibPath::GetInstallPath() const
      35              : {
      36              :     // get install path: <install path>/x86_64-linux/lib64/
      37           26 :     Path currentPath(LibPath::GetSelfLibraryDir());
      38           26 :     return currentPath;
      39              : }
      40              : 
      41              : /**
      42              :  * @brief       Get directory path where self dynamic library in
      43              :  * @return      Path
      44              :  */
      45           26 : Path LibPath::GetSelfLibraryDir() const
      46              : {
      47              :     mmDlInfo info;
      48           26 :     LibPath& (*instancePtr)() = &LibPath::Instance;
      49           26 :     const auto ret = mmDladdr(reinterpret_cast<void *>(instancePtr), &info);
      50           26 :     if (ret != EN_OK) {
      51            0 :         IDE_LOGE("Cannot find symbol GetSelfLibraryDir");
      52            0 :         return Path();
      53              :     }
      54           26 :     IDE_LOGD("Find symbol GetSelfLibraryDir in %s", info.dli_fname);
      55           26 :     Path path(info.dli_fname); // entire so name
      56           26 :     const auto name = path.GetFileName();
      57           52 :     if (name.length() <= strlen(LIBSUFFIX) ||
      58           26 :         name.compare(name.length() - strlen(LIBSUFFIX), strlen(LIBSUFFIX), LIBSUFFIX) != 0) {
      59           26 :         path = GetSelfPath();
      60              :     }
      61              : 
      62           26 :     if (!path.RealPath()) {
      63            0 :         IDE_LOGW("Can not get realpath self library path %s.", path.GetCString());
      64            0 :         return Path();
      65              :     }
      66              : 
      67           26 :     IDE_LOGI("Get self library path: %s", path.GetCString());
      68              : 
      69           26 :     return path.ParentPath();
      70           26 : }
      71              : 
      72              : /**
      73              :  * @brief       Get directory path where self binary
      74              :  * @return      Path
      75              :  */
      76           26 : Path LibPath::GetSelfPath() const
      77              : {
      78           26 :     Path selfPath;
      79           26 :     char curPath[MMPA_MAX_PATH + 1] = {0};
      80           26 :     const auto len = readlink("/proc/self/exe", curPath, MMPA_MAX_PATH);
      81           26 :     if (len <= 0 || len > MMPA_MAX_PATH) {
      82            0 :         IDE_LOGE("Get self path failed.");
      83            0 :         return selfPath;
      84              :     }
      85           26 :     curPath[len] = '\0';
      86              : 
      87           26 :     selfPath = curPath;
      88           26 :     IDE_LOGI("Get self path: %s", selfPath.GetCString());
      89           26 :     return selfPath;
      90            0 : }
      91              : 
      92           26 : std::string LibPath::GetTargetPath(const std::string &concatName) const
      93              : {
      94           26 :     Path installBasePath = GetInstallPath();
      95           26 :     IDE_CTRL_VALUE_FAILED(!installBasePath.Empty(), return "", "Failed to get install path.");
      96           26 :     return installBasePath.Concat(concatName).GetString();
      97           26 : }
      98              : 
      99            4 : bool LibPath::IsPluginSo(const std::string &fileName) const
     100              : {
     101            8 :     const std::string prefix = "adump_";
     102            4 :     const std::string suffix = "_plugin.so";
     103            4 :     if (fileName.size() < prefix.size() + suffix.size()) {
     104            1 :         return false;
     105              :     }
     106            3 :     if ((fileName.substr(0, prefix.size()) != prefix) || (fileName.substr(fileName.size() - suffix.size()) != suffix)) {
     107            1 :         return false;
     108              :     }
     109            2 :     return true;
     110            4 : }
     111              : 
     112           24 : std::vector<std::string> LibPath::ObtainAllPluginSo(const std::string &searchPath) const
     113              : {
     114           24 :     std::vector<std::string> result;
     115           24 :     std::string realPath;
     116           24 :     IDE_CTRL_VALUE_WARN(FileUtils::FileNameIsReal(searchPath, realPath) == IDE_DAEMON_OK, return result,
     117              :         "Unable to get real path of %s and the search path is %s.", realPath.c_str(), searchPath.c_str());
     118            1 :     IDE_CTRL_VALUE_WARN(FileUtils::IsFileExist(realPath), return result,
     119              :         "Unable to find so from %s.", realPath.c_str());
     120              : 
     121            1 :     DIR* dir = opendir(realPath.c_str());
     122            1 :     IDE_CTRL_VALUE_WARN(dir, return result, "Unable to open dir %s with info %s.", realPath.c_str(), strerror(errno));
     123              : 
     124              :     struct dirent* entry;
     125            8 :     while ((entry = readdir(dir)) != nullptr) {
     126            6 :         if (entry->d_type == DT_REG) {
     127            4 :             std::string fileName = entry->d_name;
     128            4 :             if (IsPluginSo(fileName)) {
     129            2 :                 result.push_back(realPath + "/" + fileName);
     130              :             }
     131            4 :         }
     132              :     }
     133            1 :     closedir(dir);
     134            1 :     return result;
     135           24 : }
     136              : 
     137              : } // namespace Adx
     138              : 
        

Generated by: LCOV version 2.0-1