From b43d74549b835cd56a50e6b8713d11e2e07921a5 Mon Sep 17 00:00:00 2001 From: wangchen Date: Thu, 3 Jul 2025 21:23:34 +0800 Subject: [PATCH] =?UTF-8?q?[Bug]:=20=E4=BD=8E=E7=BC=96=E8=AF=91=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E9=80=82=E9=85=8D=20close=20#ICJU61=20Signed-off-by:?= =?UTF-8?q?=20wangchen=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- binary_sign_tool/BUILD.gn | 6 +- binary_sign_tool/cmd/src/cmd_util.cpp | 56 ++++++++++++++++-- binary_sign_tool/cmd/src/params_run_tool.cpp | 2 - .../hap/provider/include/sign_provider.h | 1 - binary_sign_tool/hap/sign/src/sign_elf.cpp | 1 - binary_sign_tool/utils/src/file_utils.cpp | 59 +++++-------------- hapsigntool_cpp/BUILD.gn | 4 +- hapsigntool_cpp/bundle.json | 7 ++- 8 files changed, 75 insertions(+), 61 deletions(-) diff --git a/binary_sign_tool/BUILD.gn b/binary_sign_tool/BUILD.gn index 733ffddd..0b586416 100644 --- a/binary_sign_tool/BUILD.gn +++ b/binary_sign_tool/BUILD.gn @@ -73,14 +73,14 @@ ohos_executable("binary-sign-tool") { sources = signature_tools_main_src deps = [ - "//third_party/elfio:elfio", - "//third_party/openssl:libcrypto_shared", - "//third_party/openssl:libssl_shared", ] external_deps = [ "bounds_checking_function:libsec_static", + "elfio:elfio", "json:nlohmann_json_static", + "openssl:libcrypto_static", + "openssl:libssl_static", ] cflags_cc = [ diff --git a/binary_sign_tool/cmd/src/cmd_util.cpp b/binary_sign_tool/cmd/src/cmd_util.cpp index 6b2b5b15..e900e8f1 100644 --- a/binary_sign_tool/cmd/src/cmd_util.cpp +++ b/binary_sign_tool/cmd/src/cmd_util.cpp @@ -12,10 +12,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "cmd_util.h" +#include #include -#include - +#include +#include "cmd_util.h" #include "params_run_tool.h" #include "constant.h" #include "param_constants.h" @@ -162,13 +162,55 @@ static bool UpdateParamForVariantBoolAdHoc(const ParamsSharedPtr& param) return true; } +static std::string GetParentPath(const std::string &outFilePath) +{ + char resolvedPath[PATH_MAX + 1]; + if (outFilePath.size() == 0) { + PrintErrorNumberMsg("COMMAND_PARAM_ERROR", COMMAND_PARAM_ERROR, + "get_parent_path realpath error, empty input"); + return ""; + } + + if (outFilePath.size() > PATH_MAX) { + PrintErrorNumberMsg("COMMAND_PARAM_ERROR", COMMAND_PARAM_ERROR, + "get_parent_path realpath error, input too long"); + return ""; + } + + if (realpath(outFilePath.c_str(), resolvedPath) == nullptr) { + PrintErrorNumberMsg("COMMAND_PARAM_ERROR", COMMAND_PARAM_ERROR, + "'" + outFilePath + "' get_parent_path realpath error"); + return ""; + } + + SIGNATURE_TOOLS_LOGI("GetParentPath, resolvedPath:%s", resolvedPath); + char *parentPath = dirname(resolvedPath); + if (parentPath == nullptr) { + PrintErrorNumberMsg("COMMAND_PARAM_ERROR", COMMAND_PARAM_ERROR, + "get_parent_path dirname error"); + return ""; + } + + SIGNATURE_TOOLS_LOGI("GetParentPath :%s", parentPath); + return std::string(parentPath); +} + +static std::string GetFileName(const std::string &path) +{ + size_t lastSlash = path.find_last_of('/'); + if (lastSlash == std::string::npos) { + return path; + } + + return path.substr(lastSlash + 1); +} + bool CmdUtil::UpdateParamForCheckOutFile(Options* options, const std::initializer_list& outFileKeys) { for (auto& key : outFileKeys) { if (options->count(key)) { std::string outFilePath = options->GetString(key); - std::filesystem::path filePath = outFilePath; - std::string parentPath = filePath.parent_path(); + std::string parentPath = GetParentPath(outFilePath); // Purpose: To prevent the user output path from passing an empty string. eg " " std::string tmpOutFilePath = outFilePath; @@ -191,11 +233,13 @@ bool CmdUtil::UpdateParamForCheckOutFile(Options* options, const std::initialize return false; } std::string charStr(realFilePath); - std::string fileName = filePath.filename(); + std::string fileName = GetFileName(outFilePath); if (fileName.empty()) { PrintErrorNumberMsg("FILE_NOT_FOUND", FILE_NOT_FOUND, "The file name cannot be empty '" + outFilePath + "', parameter name '-" + key + "'"); return false; + } else { + SIGNATURE_TOOLS_LOGI("UpdateParamForCheckOutFile GetFileName:%s", fileName.c_str()); } (*options)[key] = charStr + "/" + fileName; } diff --git a/binary_sign_tool/cmd/src/params_run_tool.cpp b/binary_sign_tool/cmd/src/params_run_tool.cpp index 1136bf6d..3f8abc93 100644 --- a/binary_sign_tool/cmd/src/params_run_tool.cpp +++ b/binary_sign_tool/cmd/src/params_run_tool.cpp @@ -16,8 +16,6 @@ #include "params_run_tool.h" #include #include -#include - #include "constant.h" #include "help.h" diff --git a/binary_sign_tool/hap/provider/include/sign_provider.h b/binary_sign_tool/hap/provider/include/sign_provider.h index 7a3a76b8..6bbd6424 100644 --- a/binary_sign_tool/hap/provider/include/sign_provider.h +++ b/binary_sign_tool/hap/provider/include/sign_provider.h @@ -21,7 +21,6 @@ #include #include #include -#include #include #include #include diff --git a/binary_sign_tool/hap/sign/src/sign_elf.cpp b/binary_sign_tool/hap/sign/src/sign_elf.cpp index bc54e3aa..3c59e5d4 100644 --- a/binary_sign_tool/hap/sign/src/sign_elf.cpp +++ b/binary_sign_tool/hap/sign/src/sign_elf.cpp @@ -14,7 +14,6 @@ */ #include "sign_elf.h" -#include #include #include "file_utils.h" diff --git a/binary_sign_tool/utils/src/file_utils.cpp b/binary_sign_tool/utils/src/file_utils.cpp index 24ed0f97..a7610bbf 100644 --- a/binary_sign_tool/utils/src/file_utils.cpp +++ b/binary_sign_tool/utils/src/file_utils.cpp @@ -14,11 +14,12 @@ */ #include "file_utils.h" #include -#include -#include -#include #include - +#include +#include +#include +#include +#include #include "string_utils.h" #include "signature_tools_errno.h" @@ -302,61 +303,33 @@ bool FileUtils::IsRunnableFile(const std::string& name) bool FileUtils::IsValidFile(std::string file) { - std::filesystem::path filePath = file; - bool flag = std::filesystem::exists(filePath); + struct stat fileStat; + bool flag = (stat(file.c_str(), &fileStat) == 0); if (!flag) { PrintErrorNumberMsg("FILE_NOT_FOUND", FILE_NOT_FOUND, "'" + file + "' file is not exists"); return false; } - flag = std::filesystem::is_directory(filePath); + flag = S_ISDIR(fileStat.st_mode); if (flag) { PrintErrorNumberMsg("FILE_NOT_FOUND", FILE_NOT_FOUND, "'" + file + "' file is a directory not file"); return false; } + SIGNATURE_TOOLS_LOGI("IsValidFile check pass %s", file.c_str()); return true; } -int64_t FileUtils::GetFileLen(const std::string& file) -{ - std::filesystem::path filePath = file; - bool flag = std::filesystem::exists(filePath) && std::filesystem::is_regular_file(filePath); - if (flag) { - return std::filesystem::file_size(filePath); - } - return -1; -} - -void FileUtils::DelDir(const std::string& file) -{ - std::filesystem::path filePath = file; - bool flag = std::filesystem::is_directory(filePath); - if (flag) { - for (auto& p : std::filesystem::recursive_directory_iterator(filePath)) { - DelDir(p.path()); - } - } - std::filesystem::remove(file); - return; -} - bool FileUtils::CopyTmpFileAndDel(const std::string& tmpFile, const std::string& output) { - if (!std::filesystem::exists(tmpFile)) { - SIGNATURE_TOOLS_LOGE("Error: tmpFile not exists"); - return false; - } - if (tmpFile == output) { - return true; - } - bool ret = std::filesystem::copy_file(tmpFile, output, std::filesystem::copy_options::overwrite_existing); - if (!ret) { - SIGNATURE_TOOLS_LOGE("Error: copy tmpFile to output failed"); - return false; - } - if (!std::filesystem::remove(tmpFile)) { + std::ifstream src(tmpFile, std::ios::binary); + std::ofstream dst(output, std::ios::binary); + SIGNATURE_TOOLS_LOGI("CopyTmpFileAndDel from %s to %s", tmpFile.c_str(), output.c_str()); + dst << src.rdbuf(); + + if (unlink(tmpFile.c_str()) != 0) { SIGNATURE_TOOLS_LOGE("Error: remove tmpFile"); return false; } + SIGNATURE_TOOLS_LOGI("CopyTmpFileAndDel finish"); return true; } } // namespace SignatureTools diff --git a/hapsigntool_cpp/BUILD.gn b/hapsigntool_cpp/BUILD.gn index 20858c40..b8a8cc72 100644 --- a/hapsigntool_cpp/BUILD.gn +++ b/hapsigntool_cpp/BUILD.gn @@ -84,13 +84,13 @@ ohos_executable("hap-sign-tool") { deps = [ "//third_party/bzip2:libbz2", - "//third_party/openssl:libcrypto_shared", - "//third_party/openssl:libssl_shared", ] external_deps = [ "c_utils:utils", "json:nlohmann_json_static", + "openssl:libcrypto_static", + "openssl:libssl_static", "zlib:shared_libz", ] diff --git a/hapsigntool_cpp/bundle.json b/hapsigntool_cpp/bundle.json index ee3d393c..d7c4f37c 100644 --- a/hapsigntool_cpp/bundle.json +++ b/hapsigntool_cpp/bundle.json @@ -33,13 +33,14 @@ "components": [ "bounds_checking_function", "c_utils", + "elfio", + "hilog", "json", - "zlib" + "openssl", + "zlib", ], "third_party": [ - "elfio", "bzip2", - "openssl", "jsoncpp" ] }, -- Gitee