pmt: New features and improvements

- Added type getter and reboot function
 - Writed a garbage collector, so manually freeing memory and closing file descriptors is removed
 - Some other improvements
This commit is contained in:
2025-08-09 12:48:04 +03:00
parent 8b3e886eee
commit 8e629d60d0
28 changed files with 1826 additions and 1265 deletions

View File

@@ -24,30 +24,27 @@
#include <libhelper/lib.hpp>
namespace Helper {
std::optional<std::string> sha256Of(const std::string_view path) {
LOGN(HELPER, INFO) << "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);
std::optional<std::string> sha256Of(const std::string_view path)
{
LOGN(HELPER, INFO) << "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());
if (!fileIsExists(fp)) throw Error("Is not exists or not file: %s", fp.data());
if (const std::ifstream file(fp, std::ios::binary); !file) throw Error("Cannot open file: %s", fp.data());
if (const std::ifstream file(fp, std::ios::binary); !file) throw Error("Cannot open file: %s", fp.data());
std::vector<unsigned char> hash(picosha2::k_digest_size);
picosha2::hash256(fp, hash.begin(), hash.end());
LOGN(HELPER, INFO) << "get sha256 of \"" << path << "\" successfull." << std::endl;
return picosha2::bytes_to_hex_string(hash.begin(), hash.end());
}
bool sha256Compare(const std::string_view file1, const std::string_view file2)
{
LOGN(HELPER, INFO) << "comparing sha256 signatures of input files." << std::endl;
const auto f1 = sha256Of(file1);
const auto f2 = sha256Of(file2);
if (f1->empty() || f2->empty()) return false;
LOGN_IF(HELPER, INFO, *f1 == *f2) << "(): input files is contains same sha256 signature." << std::endl;
return (*f1 == *f2);
}
std::vector<unsigned char> hash(picosha2::k_digest_size);
picosha2::hash256(fp, hash.begin(), hash.end());
LOGN(HELPER, INFO) << "get sha256 of \"" << path << "\" successfully." << std::endl;
return picosha2::bytes_to_hex_string(hash.begin(), hash.end());
}
bool sha256Compare(const std::string_view file1, const std::string_view file2) {
LOGN(HELPER, INFO) << "comparing sha256 signatures of input files." << std::endl;
const auto f1 = sha256Of(file1);
const auto f2 = sha256Of(file2);
if (f1->empty() || f2->empty()) return false;
LOGN_IF(HELPER, INFO, *f1 == *f2) << "(): input files is contains same sha256 signature." << std::endl;
return (*f1 == *f2);
}
} // namespace Helper