pmt: Improve libhelper and libpartition_map, etc.
This commit is contained in:
@@ -133,7 +133,7 @@ int Main(int argc, char **argv) {
|
|||||||
|
|
||||||
CLI11_PARSE(AppMain, argc, argv);
|
CLI11_PARSE(AppMain, argc, argv);
|
||||||
|
|
||||||
if (VARS.verboseMode) Helper::LoggingProperties::setPrinting(YES);
|
if (VARS.verboseMode) Helper::LoggingProperties::setPrinting<YES>();
|
||||||
if (VARS.viewVersion) {
|
if (VARS.viewVersion) {
|
||||||
println("%s", getAppVersion().data());
|
println("%s", getAppVersion().data());
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ RUN_ASYNC(const std::string &partitionName, const std::string &outputName,
|
|||||||
if (!PART_MAP.hasPartition(partitionName))
|
if (!PART_MAP.hasPartition(partitionName))
|
||||||
return {format("Couldn't find partition: %s", partitionName.data()), false};
|
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;
|
<< std::endl;
|
||||||
|
|
||||||
if (VARS.onLogical && !PART_MAP.isLogical(partitionName)) {
|
if (VARS.onLogical && !PART_MAP.isLogical(partitionName)) {
|
||||||
@@ -68,7 +68,7 @@ RUN_ASYNC(const std::string &partitionName, const std::string &outputName,
|
|||||||
false};
|
false};
|
||||||
|
|
||||||
const int ffd = Helper::openAndAddToCloseList(
|
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)
|
if (ffd < 0)
|
||||||
return {format("Can't create/open output file %s: %s", outputName.data(),
|
return {format("Can't create/open output file %s: %s", outputName.data(),
|
||||||
strerror(errno)),
|
strerror(errno)),
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ INIT {
|
|||||||
|
|
||||||
RUN {
|
RUN {
|
||||||
LOGN(CFUN, INFO) << "Removing log file: " << VARS.logFile << std::endl;
|
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);
|
return Helper::eraseEntry(VARS.logFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,15 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <vector>
|
#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
|
#ifndef ONLY_HELPER_MACROS
|
||||||
|
|
||||||
@@ -78,8 +87,7 @@ public:
|
|||||||
// Close file descriptors and delete allocated array memory
|
// Close file descriptors and delete allocated array memory
|
||||||
class garbageCollector {
|
class garbageCollector {
|
||||||
private:
|
private:
|
||||||
std::vector<char *> _ptrs_c;
|
std::vector<std::function<void()>> _cleaners;
|
||||||
std::vector<uint8_t *> _ptrs_u;
|
|
||||||
std::vector<FILE *> _fps;
|
std::vector<FILE *> _fps;
|
||||||
std::vector<DIR *> _dps;
|
std::vector<DIR *> _dps;
|
||||||
std::vector<int> _fds;
|
std::vector<int> _fds;
|
||||||
@@ -88,11 +96,14 @@ private:
|
|||||||
public:
|
public:
|
||||||
~garbageCollector();
|
~garbageCollector();
|
||||||
|
|
||||||
void delAfterProgress(char *&_ptr);
|
template <typename T>
|
||||||
void delAfterProgress(uint8_t *&_ptr);
|
void delAfterProgress(T *_ptr) {
|
||||||
void delFileAfterProgress(const std::string &path);
|
_cleaners.push_back([_ptr] { delete[] _ptr; });
|
||||||
void closeAfterProgress(FILE *&_fp);
|
}
|
||||||
void closeAfterProgress(DIR *&_dp);
|
|
||||||
|
void delFileAfterProgress(const std::string &_path);
|
||||||
|
void closeAfterProgress(FILE *_fp);
|
||||||
|
void closeAfterProgress(DIR *_dp);
|
||||||
void closeAfterProgress(int _fd);
|
void closeAfterProgress(int _fd);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -103,8 +114,17 @@ extern bool PRINT, DISABLE;
|
|||||||
void set(std::string_view name, std::string_view file);
|
void set(std::string_view name, std::string_view file);
|
||||||
void setProgramName(std::string_view name);
|
void setProgramName(std::string_view name);
|
||||||
void setLogFile(std::string_view file);
|
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();
|
void reset();
|
||||||
} // namespace LoggingProperties
|
} // 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);
|
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
|
// Android - not throws Helper::Error
|
||||||
// -------------------------------
|
// -------------------------------
|
||||||
@@ -376,14 +407,6 @@ std::string getLibVersion();
|
|||||||
|
|
||||||
#define HELPER "libhelper"
|
#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 STYLE_RESET "\033[0m"
|
||||||
#define BOLD "\033[1m"
|
#define BOLD "\033[1m"
|
||||||
#define FAINT "\033[2m"
|
#define FAINT "\033[2m"
|
||||||
|
|||||||
@@ -21,6 +21,7 @@
|
|||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <exception>
|
#include <exception>
|
||||||
|
#include <functional>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <libgen.h>
|
#include <libgen.h>
|
||||||
#include <libhelper/lib.hpp>
|
#include <libhelper/lib.hpp>
|
||||||
@@ -96,10 +97,8 @@ Logger &Logger::operator<<(std::ostream &(*msg)(std::ostream &)) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
garbageCollector::~garbageCollector() {
|
garbageCollector::~garbageCollector() {
|
||||||
for (const auto &ptr : _ptrs_c)
|
for (auto &ptr_func : _cleaners)
|
||||||
delete[] ptr;
|
ptr_func();
|
||||||
for (const auto &ptr : _ptrs_u)
|
|
||||||
delete[] ptr;
|
|
||||||
for (const auto &fd : _fds)
|
for (const auto &fd : _fds)
|
||||||
close(fd);
|
close(fd);
|
||||||
for (const auto &fp : _fps)
|
for (const auto &fp : _fps)
|
||||||
@@ -110,18 +109,12 @@ garbageCollector::~garbageCollector() {
|
|||||||
eraseEntry(file);
|
eraseEntry(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
void garbageCollector::delAfterProgress(char *&_ptr) {
|
void garbageCollector::delFileAfterProgress(const std::string &_path) {
|
||||||
_ptrs_c.push_back(_ptr);
|
_files.push_back(_path);
|
||||||
}
|
|
||||||
void garbageCollector::delAfterProgress(uint8_t *&_ptr) {
|
|
||||||
_ptrs_u.push_back(_ptr);
|
|
||||||
}
|
|
||||||
void garbageCollector::delFileAfterProgress(const std::string &path) {
|
|
||||||
_files.push_back(path);
|
|
||||||
}
|
}
|
||||||
void garbageCollector::closeAfterProgress(const int _fd) {
|
void garbageCollector::closeAfterProgress(const int _fd) {
|
||||||
_fds.push_back(_fd);
|
_fds.push_back(_fd);
|
||||||
}
|
}
|
||||||
void garbageCollector::closeAfterProgress(FILE *&_fp) { _fps.push_back(_fp); }
|
void garbageCollector::closeAfterProgress(FILE *_fp) { _fps.push_back(_fp); }
|
||||||
void garbageCollector::closeAfterProgress(DIR *&_dp) { _dps.push_back(_dp); }
|
void garbageCollector::closeAfterProgress(DIR *_dp) { _dps.push_back(_dp); }
|
||||||
} // namespace Helper
|
} // namespace Helper
|
||||||
|
|||||||
@@ -85,16 +85,6 @@ void set(std::string_view file, std::string_view name) {
|
|||||||
|
|
||||||
void setProgramName(const std::string_view name) { NAME = name; }
|
void setProgramName(const std::string_view name) { NAME = name; }
|
||||||
void setLogFile(const std::string_view file) { FILE = file; }
|
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
|
} // namespace LoggingProperties
|
||||||
|
|
||||||
bool runCommand(const std::string_view cmd) {
|
bool runCommand(const std::string_view cmd) {
|
||||||
|
|||||||
@@ -136,7 +136,7 @@ basic_partition_map_builder::basic_partition_map_builder() {
|
|||||||
LOGN(MAP, ERROR) << "Cannot build map by any default search entry."
|
LOGN(MAP, ERROR) << "Cannot build map by any default search entry."
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
|
|
||||||
LOGN(MAP, INFO) << "default constructor successfully ended work."
|
LOGN(MAP, INFO) << "default constructor ended work."
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
_insert_logicals(_build_map("/dev/block/mapper", true));
|
_insert_logicals(_build_map("/dev/block/mapper", true));
|
||||||
_map_builded = true;
|
_map_builded = true;
|
||||||
|
|||||||
@@ -185,7 +185,7 @@ std::string basic_partition_map::find_(const std::string &name) const {
|
|||||||
size_t basic_partition_map::size() const { return _count; }
|
size_t basic_partition_map::size() const { return _count; }
|
||||||
|
|
||||||
bool basic_partition_map::empty() const {
|
bool basic_partition_map::empty() const {
|
||||||
return _count > 0;
|
return _count == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void basic_partition_map::clear() {
|
void basic_partition_map::clear() {
|
||||||
|
|||||||
Reference in New Issue
Block a user