libhelper: fixup memory leak
This commit is contained in:
@@ -20,8 +20,9 @@ project(pmt VERSION 1.0.0)
|
|||||||
|
|
||||||
# Set compiler flags
|
# Set compiler flags
|
||||||
add_compile_options(-Wall -Werror)
|
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_compile_options(-gdwarf-5 -fsanitize=address -fstack-protector)
|
||||||
|
add_link_options(-fsanitize=address)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
# Add pmt's CMake module(s)
|
# Add pmt's CMake module(s)
|
||||||
@@ -29,7 +30,7 @@ include(cmake/generate_headers.cmake)
|
|||||||
|
|
||||||
# Generate header(s)
|
# Generate header(s)
|
||||||
get_property(FLAGS DIRECTORY PROPERTY COMPILE_OPTIONS)
|
get_property(FLAGS DIRECTORY PROPERTY COMPILE_OPTIONS)
|
||||||
generateBuildInfo(${FLAGS})
|
generateBuildInfo("${FLAGS}")
|
||||||
|
|
||||||
# Add include directories
|
# Add include directories
|
||||||
include_directories(include srclib/libhelper/include srclib/libpartition_map/include)
|
include_directories(include srclib/libhelper/include srclib/libpartition_map/include)
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ add_executable(libhelper_test tests/test.cpp)
|
|||||||
|
|
||||||
# Set linker flags
|
# Set linker flags
|
||||||
target_link_libraries(libhelper_test PRIVATE helper_shared)
|
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")
|
target_link_options(helper_shared PRIVATE "LINKER:-rpath,/data/data/com.termux/files/usr/lib")
|
||||||
|
|
||||||
# Set appropriate output names
|
# Set appropriate output names
|
||||||
|
|||||||
@@ -32,8 +32,9 @@ enum LogLevels {
|
|||||||
ABORT = (int)'A'
|
ABORT = (int)'A'
|
||||||
};
|
};
|
||||||
|
|
||||||
constexpr mode_t DEFAULT_FILE_PERMS = 0644;
|
constexpr mode_t DEFAULT_FILE_PERMS = 0644;
|
||||||
constexpr mode_t DEFAULT_DIR_PERMS = 0755;
|
constexpr mode_t DEFAULT_EXTENDED_FILE_PERMS = 0755;
|
||||||
|
constexpr mode_t DEFAULT_DIR_PERMS = 0755;
|
||||||
constexpr int YES = 1;
|
constexpr int YES = 1;
|
||||||
constexpr int NO = 0;
|
constexpr int NO = 0;
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,15 @@
|
|||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <libhelper/lib.hpp>
|
#include <libhelper/lib.hpp>
|
||||||
|
|
||||||
|
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 {
|
namespace Helper {
|
||||||
|
|
||||||
Error::Error(const char* format, ...)
|
Error::Error(const char* format, ...)
|
||||||
@@ -56,7 +65,7 @@ Logger::~Logger()
|
|||||||
currentTime().data(),
|
currentTime().data(),
|
||||||
_oss.str().data());
|
_oss.str().data());
|
||||||
|
|
||||||
if (!isExists(_logFile)) createFile(_logFile);
|
if (!isExists(_logFile)) __create_log_file(_logFile);
|
||||||
|
|
||||||
FILE* fp = fopen(_logFile, "a");
|
FILE* fp = fopen(_logFile, "a");
|
||||||
if (fp != NULL) {
|
if (fp != NULL) {
|
||||||
|
|||||||
@@ -246,6 +246,7 @@ bool eraseDirectoryRecursive(const std::string_view directory)
|
|||||||
std::string readSymlink(const std::string_view entry)
|
std::string readSymlink(const std::string_view entry)
|
||||||
{
|
{
|
||||||
LOGN(HELPER, INFO) << __func__ << "(): read symlink request: " << entry << std::endl;
|
LOGN(HELPER, INFO) << __func__ << "(): read symlink request: " << entry << std::endl;
|
||||||
|
|
||||||
char target[PATH_MAX];
|
char target[PATH_MAX];
|
||||||
ssize_t len = readlink(entry.data(), target, (sizeof(target) - 1));
|
ssize_t len = readlink(entry.data(), target, (sizeof(target) - 1));
|
||||||
if (len == -1) {
|
if (len == -1) {
|
||||||
|
|||||||
@@ -27,20 +27,22 @@ namespace Helper {
|
|||||||
|
|
||||||
std::optional<std::string> sha256Of(const std::string_view path)
|
std::optional<std::string> sha256Of(const std::string_view path)
|
||||||
{
|
{
|
||||||
LOGN(HELPER, INFO) << __func__ << "(): get sha256 of \"" << path << "\" request." << std::endl;
|
LOGN(HELPER, INFO) << __func__ << "(): get sha256 of \"" << path << "\" request. Getting full path (if input is link and exists)." << std::endl;
|
||||||
if (!fileIsExists(path)) {
|
std::string fp = (isLink(path)) ? readSymlink(path) : std::string(path);
|
||||||
throw Error("Is not exists or not file: %s", path.data());
|
|
||||||
|
if (!fileIsExists(fp)) {
|
||||||
|
throw Error("Is not exists or not file: %s", fp.data());
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ifstream file(path, std::ios::binary);
|
std::ifstream file(fp, std::ios::binary);
|
||||||
if (!file) {
|
if (!file) {
|
||||||
throw Error("Cannot open file: %s", path.data());
|
throw Error("Cannot open file: %s", fp.data());
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<unsigned char> hash(picosha2::k_digest_size);
|
std::vector<unsigned char> 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;
|
LOGN(HELPER, INFO) << __func__ << "(): get sha256 of \"" << path << "\" successfull." << std::endl;
|
||||||
return picosha2::bytes_to_hex_string(hash.begin(), hash.end());
|
return picosha2::bytes_to_hex_string(hash.begin(), hash.end());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -123,18 +123,21 @@ std::string currentTime()
|
|||||||
std::string runCommandWithOutput(const std::string_view cmd)
|
std::string runCommandWithOutput(const std::string_view cmd)
|
||||||
{
|
{
|
||||||
LOGN(HELPER, INFO) << __func__ << "(): run command and catch out request: " << cmd << std::endl;
|
LOGN(HELPER, INFO) << __func__ << "(): run command and catch out request: " << cmd << std::endl;
|
||||||
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.data(), "r"), pclose);
|
|
||||||
|
FILE* pipe = popen(cmd.data(), "r");
|
||||||
if (!pipe) {
|
if (!pipe) {
|
||||||
throw Error("Cannot run command: %s", cmd.data());
|
throw Error("Cannot run command: %s", cmd.data());
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string end;
|
std::unique_ptr<FILE, decltype(&pclose)> pipe_holder(pipe, pclose);
|
||||||
|
|
||||||
|
std::string output;
|
||||||
char buffer[1024];
|
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)
|
std::string pathJoin(std::string base, std::string relative)
|
||||||
|
|||||||
@@ -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_target_properties(partition_map_static PROPERTIES OUTPUT_NAME "partition_map")
|
||||||
|
|
||||||
# Set linker flags
|
# 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_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(libpartition_map_test PRIVATE partition_map_shared PRIVATE helper_shared)
|
||||||
target_link_libraries(partition_map_shared PRIVATE helper_shared)
|
target_link_libraries(partition_map_shared PRIVATE helper_shared)
|
||||||
|
|||||||
Reference in New Issue
Block a user