From 0e6e2f9f39ca8035edf71d58c9b2b282a933618a Mon Sep 17 00:00:00 2001 From: PengC Date: Mon, 28 Jul 2025 17:03:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=91=8A=E8=AD=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/external/register/op_bin_info.h | 10 +++-- register/op_bin_info.cc | 62 ++++++++++++++++------------- register/op_bin_info_utils.h | 22 ++++++---- 3 files changed, 56 insertions(+), 38 deletions(-) diff --git a/inc/external/register/op_bin_info.h b/inc/external/register/op_bin_info.h index 23dbdaa7b0..acec7520e8 100644 --- a/inc/external/register/op_bin_info.h +++ b/inc/external/register/op_bin_info.h @@ -7,6 +7,9 @@ * See LICENSE in the root of the software repository for the full text of the License. * ===================================================================================================================*/ +#ifndef REGISTER_OP_BIN_INFO_H +#define REGISTER_OP_BIN_INFO_H + #include #include #include @@ -17,10 +20,10 @@ using OpInfo = std::vectord_type == DT_DIR) { if (std::string(entry->d_name) != "." && std::string(entry->d_name) != "..") { if (!RemoveDirectoryRecursively(entryPath)) { - GELOGE(ge::FAILED, "Failed to remove subdirectory: %s", entryPath.c_str()); + GELOGW("Failed to remove subdirectory: %s", entryPath.c_str()); closedir(dir); return false; } @@ -207,7 +207,7 @@ bool RemoveDirectoryRecursively(const std::string& path) } } else { if (unlink(entryPath.c_str()) != 0) { - GELOGE(ge::FAILED, "Failed to unlink file: %s", entryPath.c_str()); + GELOGW("Failed to unlink file: %s", entryPath.c_str()); closedir(dir); return false; } @@ -216,7 +216,7 @@ bool RemoveDirectoryRecursively(const std::string& path) entry = readdir(dir); } closedir(dir); - bool result = (rmdir(path.c_str()) == 0); + const bool result = (rmdir(path.c_str()) == 0); return result; } @@ -244,18 +244,18 @@ bool IsDirectoryEmpty(const std::string& dirPath) return true; } -void DestroyCustomOpRegistry(std::string basePath) +void DestroyCustomOpRegistry(std::string& basePath) { GELOGD("Running removed directory."); - size_t lastSlashPos = (basePath).rfind('/'); - size_t secondLastSlashPos = (basePath).rfind('/', lastSlashPos - 1); + const size_t lastSlashPos = (basePath).rfind('/'); + const size_t secondLastSlashPos = (basePath).rfind('/', lastSlashPos - 1); std::string oppBasePath = (basePath).substr(0, secondLastSlashPos); if (!RemoveDirectoryRecursively(oppBasePath)) { - GELOGE(ge::FAILED, "Failed to remove directory: %s", oppBasePath.c_str()); + GELOGW("Failed to remove directory: %s", oppBasePath.c_str()); } else { GELOGI("Successfully removed directory: %s", oppBasePath.c_str()); const std::string oppSubstring = "/opp/"; - size_t pos = oppBasePath.find(oppSubstring); + const size_t pos = oppBasePath.find(oppSubstring); std::string oppDir = oppBasePath.substr(0, pos + oppSubstring.length() - 1); if (IsDirectoryEmpty(oppDir)) { if (rmdir(oppDir.c_str()) != 0) { @@ -267,8 +267,13 @@ void DestroyCustomOpRegistry(std::string basePath) } } -OpBinInfo::OpBinInfo(const std::string& opType, OpInfo& opInfo) : opType_(opType), opInfo_(opInfo) { - GetBasePath(basePath_, opType); +OpBinInfo::OpBinInfo(const std::string& opType, const OpInfo& opInfo) : opType_(opType), opInfo_(opInfo) { + const bool ret = GetBasePath(basePath_, opType); + if (ret) { + GELOGI("Succeed to call OpBinInfo constructor."); + } else { + GELOGW("Failed to call OpBinInfo constructor."); + } } OpBinInfo::~OpBinInfo() { @@ -291,7 +296,7 @@ uint32_t OpBinInfo::Generate(ge::AscendString* opLibPath, const std::string& tar GELOGI("Get OpLibPath is %s", (basePath_ + opType_).c_str()); std::string arch = ""; - bool ret = GetSystemArchitecture(arch); + const bool ret = GetSystemArchitecture(arch); if (!ret) { GELOGE(ge::FAILED, "Failed to get system architecture name"); return 1; @@ -299,7 +304,7 @@ uint32_t OpBinInfo::Generate(ge::AscendString* opLibPath, const std::string& tar char resolvedPath[registerPathMax]; if (targetPath.size() > registerPathMax || realpath(targetPath.c_str(), resolvedPath) == nullptr) { - GELOGE(ge::FAILED, "Failed to resolve libcust_opapi.so path: %s", strerror(errno)); + GELOGE(ge::FAILED, "Failed to resolve libcust_opapi.so path."); return 1; } std::string targetResPath = std::string(resolvedPath); @@ -327,14 +332,16 @@ uint32_t OpBinInfo::Generate(ge::AscendString* opLibPath, const std::string& tar bool OpBinInfo::Check(const std::string& path) { Elf64_Ehdr ehdr; std::ifstream file(path, std::ios::binary); - if (!file) { - GELOGE(ge::FAILED, "Could not open file %s for checking shared library status.", path.c_str()); + std::streamsize sizeEhdr = static_cast(sizeof(ehdr)); + file.read(reinterpret_cast(&ehdr), sizeEhdr); + file.seekg(static_cast(ehdr.e_phoff)); + std::vector phdrs(static_cast(ehdr.e_phnum)); + std::streamsize sizePhnum = static_cast(sizeof(Elf64_Phdr)); + if (!file.read(reinterpret_cast(phdrs.data()), ehdr.e_phnum * sizePhnum)) { + file.close(); + GELOGE(ge::FAILED, "Failed to read file %s phdrs.", path.c_str()); return false; } - file.read(reinterpret_cast(&ehdr), sizeof(ehdr)); - file.seekg(ehdr.e_phoff); - std::vector phdrs(ehdr.e_phnum); - file.read(reinterpret_cast(phdrs.data()), ehdr.e_phnum * sizeof(Elf64_Phdr)); if (memcmp(ehdr.e_ident, "\x7F""ELF", opsFour) != 0) { file.close(); GELOGE(ge::FAILED, "File %s is not a valid ELF file.", path.c_str()); @@ -355,5 +362,4 @@ bool OpBinInfo::Check(const std::string& path) { file.close(); return true; } - } \ No newline at end of file diff --git a/register/op_bin_info_utils.h b/register/op_bin_info_utils.h index 80a86e028e..df6c81b03f 100644 --- a/register/op_bin_info_utils.h +++ b/register/op_bin_info_utils.h @@ -7,17 +7,25 @@ * See LICENSE in the root of the software repository for the full text of the License. * ===================================================================================================================*/ -#include -#include +#ifndef REGISTER_OP_BIN_INFO_UTILS_H +#define REGISTER_OP_BIN_INFO_UTILS_H + #include -#include "graph/ascend_string.h" namespace ops { -bool GetBasePathByEnv(std::string& basePath, const char *ascendWorkPathEnv, std::string currentTimestamp); -bool GetBasePath(std::string& basePath_, std::string vendorName); +bool FileExist(const std::string& filename); +bool DirectoryExists(const std::string& path); +std::string GetCurrentTimestamp(); +bool CheckWritePermission(const std::string& path); +bool GetBasePathByEnv(std::string& basePath, const char *ascendWorkPathEnv, std::string& currentTimestamp); +bool GetBasePath(std::string& basePath, std::string& opType); +bool CreateDirectory(const std::string& path); +void WriteBinaryFile(const std::string& path, const uint8_t* start, const uint8_t* end); std::string GetParentPath(const std::string& path); bool CreateSymlink(const std::string& targetPath, const std::string& linkPath); +bool GetSystemArchitecture(std::string& arch); bool RemoveDirectoryRecursively(const std::string& path); bool IsDirectoryEmpty(const std::string& dirPath); -bool GetSystemArchitecture(std::string& arch); -} \ No newline at end of file +void DestroyCustomOpRegistry(std::string& basePath); +} +#endif \ No newline at end of file -- Gitee