diff --git a/CMakeLists.txt b/CMakeLists.txt index 983300a..469b8ac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,8 +20,9 @@ project(pmt VERSION 1.0.0) # Set compiler flags add_compile_options(-Wall -Werror) -if(CMAKE_BUILD_TYPE STREQUAL "Debug") +if(${CMAKE_BUILD_TYPE} STREQUAL Debug) add_compile_options(-gdwarf-5 -fsanitize=address -fstack-protector) + add_link_options(-fsanitize=address) endif() # Add pmt's CMake module(s) @@ -29,7 +30,7 @@ include(cmake/generate_headers.cmake) # Generate header(s) get_property(FLAGS DIRECTORY PROPERTY COMPILE_OPTIONS) -generateBuildInfo(${FLAGS}) +generateBuildInfo("${FLAGS}") # Add include directories include_directories(include srclib/libhelper/include srclib/libpartition_map/include) diff --git a/srclib/libhelper/CMakeLists.txt b/srclib/libhelper/CMakeLists.txt index aaaa278..8114ff0 100644 --- a/srclib/libhelper/CMakeLists.txt +++ b/srclib/libhelper/CMakeLists.txt @@ -29,7 +29,7 @@ add_executable(libhelper_test tests/test.cpp) # Set linker flags target_link_libraries(libhelper_test PRIVATE helper_shared) -target_link_options(libhelper_test PRIVATE "LINKER:-rpath,/data/data/com.termux/files/usr/lib") +target_link_options(libhelper_test PRIVATE "LINKER:-rpath,/data/data/com.termux/files/usr/lib" "LINKER:-rpath,/data/local") target_link_options(helper_shared PRIVATE "LINKER:-rpath,/data/data/com.termux/files/usr/lib") # Set appropriate output names diff --git a/srclib/libhelper/include/libhelper/lib.hpp b/srclib/libhelper/include/libhelper/lib.hpp index 269b5b3..cff06d9 100644 --- a/srclib/libhelper/include/libhelper/lib.hpp +++ b/srclib/libhelper/include/libhelper/lib.hpp @@ -32,8 +32,9 @@ enum LogLevels { ABORT = (int)'A' }; -constexpr mode_t DEFAULT_FILE_PERMS = 0644; -constexpr mode_t DEFAULT_DIR_PERMS = 0755; +constexpr mode_t DEFAULT_FILE_PERMS = 0644; +constexpr mode_t DEFAULT_EXTENDED_FILE_PERMS = 0755; +constexpr mode_t DEFAULT_DIR_PERMS = 0755; constexpr int YES = 1; constexpr int NO = 0; diff --git a/srclib/libhelper/src/Classes.cpp b/srclib/libhelper/src/Classes.cpp index ca32ed7..0b23981 100644 --- a/srclib/libhelper/src/Classes.cpp +++ b/srclib/libhelper/src/Classes.cpp @@ -23,6 +23,15 @@ #include #include +static void __create_log_file(const char* file) +{ + int fd = open(file, O_WRONLY | O_TRUNC, DEFAULT_EXTENDED_FILE_PERMS); + if (fd == -1) { + fd = open(file, O_WRONLY | O_CREAT, DEFAULT_EXTENDED_FILE_PERMS); + if (fd != -1) close(fd); + } else if (fd != -1) close(fd); +} + namespace Helper { Error::Error(const char* format, ...) @@ -56,7 +65,7 @@ Logger::~Logger() currentTime().data(), _oss.str().data()); - if (!isExists(_logFile)) createFile(_logFile); + if (!isExists(_logFile)) __create_log_file(_logFile); FILE* fp = fopen(_logFile, "a"); if (fp != NULL) { diff --git a/srclib/libhelper/src/FileUtil.cpp b/srclib/libhelper/src/FileUtil.cpp index 4d4029d..69d85d5 100644 --- a/srclib/libhelper/src/FileUtil.cpp +++ b/srclib/libhelper/src/FileUtil.cpp @@ -246,6 +246,7 @@ bool eraseDirectoryRecursive(const std::string_view directory) std::string readSymlink(const std::string_view entry) { LOGN(HELPER, INFO) << __func__ << "(): read symlink request: " << entry << std::endl; + char target[PATH_MAX]; ssize_t len = readlink(entry.data(), target, (sizeof(target) - 1)); if (len == -1) { diff --git a/srclib/libhelper/src/Sha256.cpp b/srclib/libhelper/src/Sha256.cpp index 1c39e63..a3fc67f 100644 --- a/srclib/libhelper/src/Sha256.cpp +++ b/srclib/libhelper/src/Sha256.cpp @@ -27,20 +27,22 @@ namespace Helper { std::optional sha256Of(const std::string_view path) { - LOGN(HELPER, INFO) << __func__ << "(): get sha256 of \"" << path << "\" request." << std::endl; - if (!fileIsExists(path)) { - throw Error("Is not exists or not file: %s", path.data()); + LOGN(HELPER, INFO) << __func__ << "(): get sha256 of \"" << path << "\" request. Getting full path (if input is link and exists)." << std::endl; + std::string fp = (isLink(path)) ? readSymlink(path) : std::string(path); + + if (!fileIsExists(fp)) { + throw Error("Is not exists or not file: %s", fp.data()); return std::nullopt; } - std::ifstream file(path, std::ios::binary); + std::ifstream file(fp, std::ios::binary); if (!file) { - throw Error("Cannot open file: %s", path.data()); + throw Error("Cannot open file: %s", fp.data()); return std::nullopt; } std::vector hash(picosha2::k_digest_size); - picosha2::hash256(path, hash.begin(), hash.end()); + picosha2::hash256(fp, hash.begin(), hash.end()); LOGN(HELPER, INFO) << __func__ << "(): get sha256 of \"" << path << "\" successfull." << std::endl; return picosha2::bytes_to_hex_string(hash.begin(), hash.end()); } diff --git a/srclib/libhelper/src/Utilities.cpp b/srclib/libhelper/src/Utilities.cpp index f25e15f..baa7c39 100644 --- a/srclib/libhelper/src/Utilities.cpp +++ b/srclib/libhelper/src/Utilities.cpp @@ -123,18 +123,21 @@ std::string currentTime() std::string runCommandWithOutput(const std::string_view cmd) { LOGN(HELPER, INFO) << __func__ << "(): run command and catch out request: " << cmd << std::endl; - std::unique_ptr pipe(popen(cmd.data(), "r"), pclose); + + FILE* pipe = popen(cmd.data(), "r"); if (!pipe) { throw Error("Cannot run command: %s", cmd.data()); return {}; } - std::string end; + std::unique_ptr pipe_holder(pipe, pclose); + + std::string output; char buffer[1024]; - while (fgets(buffer, sizeof(buffer), pipe.get()) != nullptr) end += buffer; + while (fgets(buffer, sizeof(buffer), pipe_holder.get()) != nullptr) output += buffer; - return end; + return output; } std::string pathJoin(std::string base, std::string relative) diff --git a/srclib/libpartition_map/CMakeLists.txt b/srclib/libpartition_map/CMakeLists.txt index 339aee2..cdf0728 100644 --- a/srclib/libpartition_map/CMakeLists.txt +++ b/srclib/libpartition_map/CMakeLists.txt @@ -31,7 +31,7 @@ set_target_properties(partition_map_shared PROPERTIES OUTPUT_NAME "partition_map set_target_properties(partition_map_static PROPERTIES OUTPUT_NAME "partition_map") # Set linker flags -target_link_options(libpartition_map_test PRIVATE "LINKER:-rpath,/data/data/com.termux/files/usr/lib") +target_link_options(libpartition_map_test PRIVATE "LINKER:-rpath,/data/data/com.termux/files/usr/lib" "LINKER:-rpath,/data/local") target_link_options(partition_map_shared PRIVATE "LINKER:-rpath,/data/data/com.termux/files/usr/lib") target_link_libraries(libpartition_map_test PRIVATE partition_map_shared PRIVATE helper_shared) target_link_libraries(partition_map_shared PRIVATE helper_shared)