libhelper: fixup memory leak

This commit is contained in:
2025-07-26 10:28:17 +03:00
parent 7017cfc9a4
commit 40260d5ae3
8 changed files with 34 additions and 17 deletions

View File

@@ -23,6 +23,15 @@
#include <stdarg.h>
#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 {
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) {

View File

@@ -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) {

View File

@@ -27,20 +27,22 @@ namespace Helper {
std::optional<std::string> 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<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;
return picosha2::bytes_to_hex_string(hash.begin(), hash.end());
}

View File

@@ -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<FILE, decltype(&pclose)> 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<FILE, decltype(&pclose)> 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)