pmt: Improve libhelper and libpartition_map, etc.

This commit is contained in:
2025-09-07 21:54:35 +03:00
parent 77760bd1d4
commit 9625bf6df0
8 changed files with 53 additions and 47 deletions

View File

@@ -133,7 +133,7 @@ int Main(int argc, char **argv) {
CLI11_PARSE(AppMain, argc, argv);
if (VARS.verboseMode) Helper::LoggingProperties::setPrinting(YES);
if (VARS.verboseMode) Helper::LoggingProperties::setPrinting<YES>();
if (VARS.viewVersion) {
println("%s", getAppVersion().data());
return EXIT_SUCCESS;

View File

@@ -33,7 +33,7 @@ RUN_ASYNC(const std::string &partitionName, const std::string &outputName,
if (!PART_MAP.hasPartition(partitionName))
return {format("Couldn't find partition: %s", partitionName.data()), false};
LOGN(BFUN, INFO) << "back upping " << partitionName << " as " << outputName
LOGN(BFUN, INFO) << "Back upping " << partitionName << " as " << outputName
<< std::endl;
if (VARS.onLogical && !PART_MAP.isLogical(partitionName)) {
@@ -68,7 +68,7 @@ RUN_ASYNC(const std::string &partitionName, const std::string &outputName,
false};
const int ffd = Helper::openAndAddToCloseList(
outputName, collector, O_WRONLY | O_CREAT | O_TRUNC, 0644);
outputName, collector, O_WRONLY | O_CREAT | O_TRUNC);
if (ffd < 0)
return {format("Can't create/open output file %s: %s", outputName.data(),
strerror(errno)),

View File

@@ -30,7 +30,7 @@ INIT {
RUN {
LOGN(CFUN, INFO) << "Removing log file: " << VARS.logFile << std::endl;
Helper::LoggingProperties::setLoggingState(YES); // eraseEntry writes log!
Helper::LoggingProperties::setLoggingState<YES>(); // eraseEntry writes log!
return Helper::eraseEntry(VARS.logFile);
}

View File

@@ -25,6 +25,15 @@
#include <string>
#include <string_view>
#include <vector>
#include <functional>
#define KB(x) (static_cast<uint64_t>(x) * 1024) // KB(8) = 8192 (8 * 1024)
#define MB(x) (KB(x) * 1024) // MB(4) = 4194304 (KB(4) * 1024)
#define GB(x) (MB(x) * 1024) // GB(1) = 1073741824 (MB(1) * 1024)
#define TO_KB(x) (x / 1024) // TO_KB(1024) = 1
#define TO_MB(x) (TO_KB(x) / 1024) // TO_MB(2048) (2048 / 1024)
#define TO_GB(x) (TO_MB(x) / 1024) // TO_GB(1048576) (TO_MB(1048576) / 1024)
#ifndef ONLY_HELPER_MACROS
@@ -78,8 +87,7 @@ public:
// Close file descriptors and delete allocated array memory
class garbageCollector {
private:
std::vector<char *> _ptrs_c;
std::vector<uint8_t *> _ptrs_u;
std::vector<std::function<void()>> _cleaners;
std::vector<FILE *> _fps;
std::vector<DIR *> _dps;
std::vector<int> _fds;
@@ -88,11 +96,14 @@ private:
public:
~garbageCollector();
void delAfterProgress(char *&_ptr);
void delAfterProgress(uint8_t *&_ptr);
void delFileAfterProgress(const std::string &path);
void closeAfterProgress(FILE *&_fp);
void closeAfterProgress(DIR *&_dp);
template <typename T>
void delAfterProgress(T *_ptr) {
_cleaners.push_back([_ptr] { delete[] _ptr; });
}
void delFileAfterProgress(const std::string &_path);
void closeAfterProgress(FILE *_fp);
void closeAfterProgress(DIR *_dp);
void closeAfterProgress(int _fd);
};
@@ -103,8 +114,17 @@ extern bool PRINT, DISABLE;
void set(std::string_view name, std::string_view file);
void setProgramName(std::string_view name);
void setLogFile(std::string_view file);
void setPrinting(int state);
void setLoggingState(int state); // Disable/enable logging
template <int state>
void setPrinting() {
if (state == 1 || state == 0) PRINT = state;
else PRINT = NO;
}
template <int state>
void setLoggingState() {
if (state == 1 || state == 0) DISABLE = state;
else DISABLE = NO;
}
void reset();
} // namespace LoggingProperties
@@ -329,6 +349,17 @@ uint64_t getRandomOffset(uint64_t size, uint64_t bufferSize);
*/
std::string convertTo(uint64_t size, const std::string &multiple);
/**
* Convert input size to input multiple
*/
template <uint64_t size>
std::string convertTo(const std::string &multiple) {
if (multiple == "KB") return std::to_string(TO_KB(size));
if (multiple == "MB") return std::to_string(TO_MB(size));
if (multiple == "GB") return std::to_string(TO_GB(size));
return std::to_string(size);
}
// -------------------------------
// Android - not throws Helper::Error
// -------------------------------
@@ -376,14 +407,6 @@ std::string getLibVersion();
#define HELPER "libhelper"
#define KB(x) (static_cast<uint64_t>(x) * 1024) // KB(8) = 8192 (8 * 1024)
#define MB(x) (KB(x) * 1024) // MB(4) = 4194304 (KB(4) * 1024)
#define GB(x) (MB(x) * 1024) // GB(1) = 1073741824 (MB(1) * 1024)
#define TO_KB(x) (x / 1024) // TO_KB(1024) = 1
#define TO_MB(x) (TO_KB(x) / 1024) // TO_MB(2048) (2048 / 1024)
#define TO_GB(x) (TO_MB(x) / 1024) // TO_GB(1048576) (TO_MB(1048576) / 1024)
#define STYLE_RESET "\033[0m"
#define BOLD "\033[1m"
#define FAINT "\033[2m"

View File

@@ -21,6 +21,7 @@
#include <cstring>
#include <dirent.h>
#include <exception>
#include <functional>
#include <fcntl.h>
#include <libgen.h>
#include <libhelper/lib.hpp>
@@ -96,10 +97,8 @@ Logger &Logger::operator<<(std::ostream &(*msg)(std::ostream &)) {
}
garbageCollector::~garbageCollector() {
for (const auto &ptr : _ptrs_c)
delete[] ptr;
for (const auto &ptr : _ptrs_u)
delete[] ptr;
for (auto &ptr_func : _cleaners)
ptr_func();
for (const auto &fd : _fds)
close(fd);
for (const auto &fp : _fps)
@@ -110,18 +109,12 @@ garbageCollector::~garbageCollector() {
eraseEntry(file);
}
void garbageCollector::delAfterProgress(char *&_ptr) {
_ptrs_c.push_back(_ptr);
}
void garbageCollector::delAfterProgress(uint8_t *&_ptr) {
_ptrs_u.push_back(_ptr);
}
void garbageCollector::delFileAfterProgress(const std::string &path) {
_files.push_back(path);
void garbageCollector::delFileAfterProgress(const std::string &_path) {
_files.push_back(_path);
}
void garbageCollector::closeAfterProgress(const int _fd) {
_fds.push_back(_fd);
}
void garbageCollector::closeAfterProgress(FILE *&_fp) { _fps.push_back(_fp); }
void garbageCollector::closeAfterProgress(DIR *&_dp) { _dps.push_back(_dp); }
void garbageCollector::closeAfterProgress(FILE *_fp) { _fps.push_back(_fp); }
void garbageCollector::closeAfterProgress(DIR *_dp) { _dps.push_back(_dp); }
} // namespace Helper

View File

@@ -85,16 +85,6 @@ void set(std::string_view file, std::string_view name) {
void setProgramName(const std::string_view name) { NAME = name; }
void setLogFile(const std::string_view file) { FILE = file; }
void setPrinting(const int state) {
if (state == 1 || state == 0) PRINT = state;
else PRINT = NO;
}
void setLoggingState(const int state) {
if (state == 1 || state == 0) DISABLE = state;
else DISABLE = NO;
}
} // namespace LoggingProperties
bool runCommand(const std::string_view cmd) {

View File

@@ -136,7 +136,7 @@ basic_partition_map_builder::basic_partition_map_builder() {
LOGN(MAP, ERROR) << "Cannot build map by any default search entry."
<< std::endl;
LOGN(MAP, INFO) << "default constructor successfully ended work."
LOGN(MAP, INFO) << "default constructor ended work."
<< std::endl;
_insert_logicals(_build_map("/dev/block/mapper", true));
_map_builded = true;

View File

@@ -185,7 +185,7 @@ std::string basic_partition_map::find_(const std::string &name) const {
size_t basic_partition_map::size() const { return _count; }
bool basic_partition_map::empty() const {
return _count > 0;
return _count == 0;
}
void basic_partition_map::clear() {